00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <config.h>
00027
00028 #ifdef KDE_USE_FINAL
00029 #undef Always
00030 #include <qdockwindow.h>
00031 #endif
00032 #include "ktoolbar.h"
00033 #include "kmainwindow.h"
00034
00035 #include <string.h>
00036
00037 #include <qpainter.h>
00038 #include <qtooltip.h>
00039 #include <qdrawutil.h>
00040 #include <qstring.h>
00041 #include <qrect.h>
00042 #include <qobjectlist.h>
00043 #include <qtimer.h>
00044 #include <qstyle.h>
00045
00046 #include "klineedit.h"
00047 #include "kseparator.h"
00048 #include <klocale.h>
00049 #include <kapplication.h>
00050 #include <kaction.h>
00051 #include <kstdaction.h>
00052 #include <kglobal.h>
00053 #include <kconfig.h>
00054 #include <kiconloader.h>
00055 #include <kcombobox.h>
00056 #include <kpopupmenu.h>
00057 #include <kanimwidget.h>
00058 #include <kedittoolbar.h>
00059
00060 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00061 #include <kipc.h>
00062 #endif
00063
00064 #include <kwin.h>
00065 #include <kdebug.h>
00066 #include <qlayout.h>
00067
00068 #include "ktoolbarbutton.h"
00069
00070 enum {
00071 CONTEXT_TOP = 0,
00072 CONTEXT_LEFT = 1,
00073 CONTEXT_RIGHT = 2,
00074 CONTEXT_BOTTOM = 3,
00075 CONTEXT_FLOAT = 4,
00076 CONTEXT_FLAT = 5,
00077 CONTEXT_ICONS = 6,
00078 CONTEXT_TEXT = 7,
00079 CONTEXT_TEXTRIGHT = 8,
00080 CONTEXT_TEXTUNDER = 9,
00081 CONTEXT_ICONSIZES = 50
00082 };
00083
00084 class KToolBarPrivate
00085 {
00086 public:
00087 KToolBarPrivate() {
00088 m_iconSize = 0;
00089 m_iconText = KToolBar::IconOnly;
00090 m_highlight = true;
00091 m_transparent = true;
00092 m_honorStyle = false;
00093
00094 m_enableContext = true;
00095
00096 m_xmlguiClient = 0;
00097
00098 oldPos = Qt::DockUnmanaged;
00099
00100 modified = m_isHorizontal = positioned = false;
00101
00102 IconSizeDefault = 0;
00103 IconTextDefault = "IconOnly";
00104
00105 NewLineDefault = false;
00106 OffsetDefault = 0;
00107 PositionDefault = "Top";
00108 HiddenDefault = false;
00109 idleButtons.setAutoDelete(true);
00110 }
00111
00112 int m_iconSize;
00113 KToolBar::IconText m_iconText;
00114 bool m_highlight : 1;
00115 bool m_transparent : 1;
00116 bool m_honorStyle : 1;
00117 bool m_isHorizontal : 1;
00118 bool m_enableContext : 1;
00119 bool modified : 1;
00120 bool positioned : 1;
00121
00122 QWidget *m_parent;
00123
00124 QMainWindow::ToolBarDock oldPos;
00125
00126 KXMLGUIClient *m_xmlguiClient;
00127
00128 struct ToolBarInfo
00129 {
00130 ToolBarInfo() : index( -1 ), offset( -1 ), newline( false ), dock( Qt::DockTop ) {}
00131 ToolBarInfo( Qt::Dock d, int i, bool n, int o ) : index( i ), offset( o ), newline( n ), dock( d ) {}
00132 int index, offset;
00133 bool newline;
00134 Qt::Dock dock;
00135 };
00136
00137 ToolBarInfo toolBarInfo;
00138 QValueList<int> iconSizes;
00139 QTimer repaintTimer;
00140
00141
00142 bool HiddenDefault;
00143 int IconSizeDefault;
00144 QString IconTextDefault;
00145 bool NewLineDefault;
00146 int OffsetDefault;
00147 QString PositionDefault;
00148
00149 QPtrList<QWidget> idleButtons;
00150 };
00151
00152 KToolBarSeparator::KToolBarSeparator(Orientation o , bool l, QToolBar *parent,
00153 const char* name )
00154 :QFrame( parent, name ), line( l )
00155 {
00156 connect( parent, SIGNAL(orientationChanged(Orientation)),
00157 this, SLOT(setOrientation(Orientation)) );
00158 setOrientation( o );
00159 setBackgroundMode( parent->backgroundMode() );
00160 setBackgroundOrigin( ParentOrigin );
00161 }
00162
00163 void KToolBarSeparator::setOrientation( Orientation o )
00164 {
00165 orient = o;
00166 setFrameStyle( NoFrame );
00167 }
00168
00169 void KToolBarSeparator::drawContents( QPainter* p )
00170 {
00171 if ( line ) {
00172 QStyle::SFlags flags = QStyle::Style_Default;
00173
00174 if ( orientation() == Horizontal )
00175 flags = flags | QStyle::Style_Horizontal;
00176
00177 style().drawPrimitive(QStyle::PE_DockWindowSeparator, p,
00178 contentsRect(), colorGroup(), flags);
00179 } else {
00180 QFrame::drawContents(p);
00181 }
00182 }
00183
00184 void KToolBarSeparator::styleChange( QStyle& )
00185 {
00186 setOrientation( orient );
00187 }
00188
00189 QSize KToolBarSeparator::sizeHint() const
00190 {
00191 int dim = style().pixelMetric( QStyle::PM_DockWindowSeparatorExtent, this );
00192 return orientation() == Vertical ? QSize( 0, dim ) : QSize( dim, 0 );
00193 }
00194
00195 QSizePolicy KToolBarSeparator::sizePolicy() const
00196 {
00197 return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
00198 }
00199
00200 KToolBar::KToolBar( QWidget *parent, const char *name, bool honorStyle, bool readConfig )
00201 : QToolBar( QString::fromLatin1( name ),
00202 dynamic_cast<QMainWindow*>(parent),
00203 parent, false,
00204 name ? name : "mainToolBar")
00205 {
00206 init( readConfig, honorStyle );
00207 }
00208
00209 KToolBar::KToolBar( QMainWindow *parentWindow, QMainWindow::ToolBarDock dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
00210 : QToolBar( QString::fromLatin1( name ),
00211 parentWindow, dock, newLine,
00212 name ? name : "mainToolBar")
00213 {
00214 init( readConfig, honorStyle );
00215 }
00216
00217 KToolBar::KToolBar( QMainWindow *parentWindow, QWidget *dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
00218 : QToolBar( QString::fromLatin1( name ),
00219 parentWindow, dock, newLine,
00220 name ? name : "mainToolBar")
00221 {
00222 init( readConfig, honorStyle );
00223 }
00224
00225 KToolBar::~KToolBar()
00226 {
00227 emit toolbarDestroyed();
00228 delete d;
00229 }
00230
00231 void KToolBar::init( bool readConfig, bool honorStyle )
00232 {
00233 d = new KToolBarPrivate;
00234
00235 d->IconSizeDefault = iconSize();
00236 setFullSize( true );
00237 d->m_honorStyle = honorStyle;
00238 context = 0;
00239 layoutTimer = new QTimer( this );
00240 connect( layoutTimer, SIGNAL( timeout() ),
00241 this, SLOT( rebuildLayout() ) );
00242 connect( &(d->repaintTimer), SIGNAL( timeout() ),
00243 this, SLOT( slotRepaint() ) );
00244
00245 if ( kapp ) {
00246 connect(kapp, SIGNAL(toolbarAppearanceChanged(int)), this, SLOT(slotAppearanceChanged()));
00247
00248 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00249 kapp->addKipcEventMask(KIPC::IconChanged);
00250 #endif
00251 connect(kapp, SIGNAL(iconChanged(int)), this, SLOT(slotIconChanged(int)));
00252 }
00253
00254
00255 if ( readConfig )
00256 slotReadConfig();
00257
00258 if ( mainWindow() )
00259 connect( mainWindow(), SIGNAL( toolBarPositionChanged( QToolBar * ) ),
00260 this, SLOT( toolBarPosChanged( QToolBar * ) ) );
00261
00262
00263 connect( this, SIGNAL(placeChanged(QDockWindow::Place)), SLOT(rebuildLayout()) );
00264 }
00265
00266 int KToolBar::insertButton(const QString& icon, int id, bool enabled,
00267 const QString& text, int index, KInstance *_instance )
00268 {
00269 KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text, _instance );
00270
00271 insertWidgetInternal( button, index, id );
00272 button->setEnabled( enabled );
00273 doConnections( button );
00274 return index;
00275 }
00276
00277
00278 int KToolBar::insertButton(const QString& icon, int id, const char *signal,
00279 const QObject *receiver, const char *slot,
00280 bool enabled, const QString& text, int index, KInstance *_instance )
00281 {
00282 KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text, _instance);
00283 insertWidgetInternal( button, index, id );
00284 button->setEnabled( enabled );
00285 connect( button, signal, receiver, slot );
00286 doConnections( button );
00287 return index;
00288 }
00289
00290
00291 int KToolBar::insertButton(const QPixmap& pixmap, int id, bool enabled,
00292 const QString& text, int index )
00293 {
00294 KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text);
00295 insertWidgetInternal( button, index, id );
00296 button->setEnabled( enabled );
00297 doConnections( button );
00298 return index;
00299 }
00300
00301
00302 int KToolBar::insertButton(const QPixmap& pixmap, int id, const char *signal,
00303 const QObject *receiver, const char *slot,
00304 bool enabled, const QString& text,
00305 int index )
00306 {
00307 KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text);
00308 insertWidgetInternal( button, index, id );
00309 button->setEnabled( enabled );
00310 connect( button, signal, receiver, slot );
00311 doConnections( button );
00312 return index;
00313 }
00314
00315
00316 int KToolBar::insertButton(const QString& icon, int id, QPopupMenu *popup,
00317 bool enabled, const QString &text, int index )
00318 {
00319 KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text );
00320 insertWidgetInternal( button, index, id );
00321 button->setEnabled( enabled );
00322 button->setPopup( popup );
00323 doConnections( button );
00324 return index;
00325 }
00326
00327
00328 int KToolBar::insertButton(const QPixmap& pixmap, int id, QPopupMenu *popup,
00329 bool enabled, const QString &text, int index )
00330 {
00331 KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text );
00332 insertWidgetInternal( button, index, id );
00333 button->setEnabled( enabled );
00334 button->setPopup( popup );
00335 doConnections( button );
00336 return index;
00337 }
00338
00339
00340 int KToolBar::insertLined (const QString& text, int id,
00341 const char *signal,
00342 const QObject *receiver, const char *slot,
00343 bool enabled ,
00344 const QString& toolTipText,
00345 int size, int index )
00346 {
00347 KLineEdit *lined = new KLineEdit ( this, 0 );
00348 if ( !toolTipText.isEmpty() )
00349 QToolTip::add( lined, toolTipText );
00350 if ( size > 0 )
00351 lined->setMinimumWidth( size );
00352 insertWidgetInternal( lined, index, id );
00353 connect( lined, signal, receiver, slot );
00354 lined->setText(text);
00355 lined->setEnabled( enabled );
00356 return index;
00357 }
00358
00359 int KToolBar::insertCombo (const QStringList &list, int id, bool writable,
00360 const char *signal, const QObject *receiver,
00361 const char *slot, bool enabled,
00362 const QString& tooltiptext,
00363 int size, int index,
00364 QComboBox::Policy policy )
00365 {
00366 KComboBox *combo = new KComboBox ( writable, this );
00367
00368 insertWidgetInternal( combo, index, id );
00369 combo->insertStringList (list);
00370 combo->setInsertionPolicy(policy);
00371 combo->setEnabled( enabled );
00372 if ( size > 0 )
00373 combo->setMinimumWidth( size );
00374 if (!tooltiptext.isNull())
00375 QToolTip::add( combo, tooltiptext );
00376
00377 if ( signal && receiver && slot )
00378 connect ( combo, signal, receiver, slot );
00379 return index;
00380 }
00381
00382
00383 int KToolBar::insertCombo (const QString& text, int id, bool writable,
00384 const char *signal, QObject *receiver,
00385 const char *slot, bool enabled,
00386 const QString& tooltiptext,
00387 int size, int index,
00388 QComboBox::Policy policy )
00389 {
00390 KComboBox *combo = new KComboBox ( writable, this );
00391 insertWidgetInternal( combo, index, id );
00392 combo->insertItem (text);
00393 combo->setInsertionPolicy(policy);
00394 combo->setEnabled( enabled );
00395 if ( size > 0 )
00396 combo->setMinimumWidth( size );
00397 if (!tooltiptext.isNull())
00398 QToolTip::add( combo, tooltiptext );
00399 connect (combo, signal, receiver, slot);
00400 return index;
00401 }
00402
00403 int KToolBar::insertSeparator(int index, int id)
00404 {
00405 QWidget *w = new KToolBarSeparator( orientation(), false, this, "tool bar separator" );
00406 insertWidgetInternal( w, index, id );
00407 return index;
00408 }
00409
00410 int KToolBar::insertLineSeparator(int index, int id)
00411 {
00412 QWidget *w = new KToolBarSeparator( orientation(), true, this, "tool bar separator" );
00413 insertWidgetInternal( w, index, id );
00414 return index;
00415 }
00416
00417
00418 int KToolBar::insertWidget(int id, int , QWidget *widget, int index)
00419 {
00420 removeWidgetInternal( widget );
00421 insertWidgetInternal( widget, index, id );
00422 return index;
00423 }
00424
00425 int KToolBar::insertAnimatedWidget(int id, QObject *receiver, const char *slot,
00426 const QString& icons, int index )
00427 {
00428 KAnimWidget *anim = new KAnimWidget( icons, d->m_iconSize, this );
00429 insertWidgetInternal( anim, index, id );
00430
00431 if ( receiver )
00432 connect( anim, SIGNAL(clicked()), receiver, slot);
00433
00434 return index;
00435 }
00436
00437 KAnimWidget *KToolBar::animatedWidget( int id )
00438 {
00439 Id2WidgetMap::Iterator it = id2widget.find( id );
00440 if ( it == id2widget.end() )
00441 return 0;
00442 KAnimWidget *aw = dynamic_cast<KAnimWidget *>(*it);
00443 if ( aw )
00444 return aw;
00445 QObjectList *l = queryList( "KAnimWidget" );
00446 if ( !l || !l->first() ) {
00447 delete l;
00448 return 0;
00449 }
00450
00451 for ( QObject *o = l->first(); o; o = l->next() ) {
00452 KAnimWidget *aw = dynamic_cast<KAnimWidget *>(o);
00453 if ( aw )
00454 {
00455 delete l;
00456 return aw;
00457 }
00458 }
00459
00460 delete l;
00461 return 0;
00462 }
00463
00464
00465 void KToolBar::addConnection (int id, const char *signal,
00466 const QObject *receiver, const char *slot)
00467 {
00468 QWidget* w = getWidget( id );
00469 if ( w )
00470 connect( w, signal, receiver, slot );
00471 }
00472
00473 void KToolBar::setItemEnabled( int id, bool enabled )
00474 {
00475 QWidget* w = getWidget( id );
00476 if ( w )
00477 w->setEnabled( enabled );
00478 }
00479
00480
00481 void KToolBar::setButtonPixmap( int id, const QPixmap& _pixmap )
00482 {
00483 KToolBarButton * button = getButton( id );
00484 if ( button )
00485 button->setPixmap( _pixmap );
00486 }
00487
00488
00489 void KToolBar::setButtonIcon( int id, const QString& _icon )
00490 {
00491 KToolBarButton * button = getButton( id );
00492 if ( button )
00493 button->setIcon( _icon );
00494 }
00495
00496 void KToolBar::setButtonIconSet( int id, const QIconSet& iconset )
00497 {
00498 KToolBarButton * button = getButton( id );
00499 if ( button )
00500 button->setIconSet( iconset );
00501 }
00502
00503
00504 void KToolBar::setDelayedPopup (int id , QPopupMenu *_popup, bool toggle )
00505 {
00506 KToolBarButton * button = getButton( id );
00507 if ( button )
00508 button->setDelayedPopup( _popup, toggle );
00509 }
00510
00511
00512 void KToolBar::setAutoRepeat (int id, bool flag)
00513 {
00514 KToolBarButton * button = getButton( id );
00515 if ( button )
00516 button->setAutoRepeat( flag );
00517 }
00518
00519
00520 void KToolBar::setToggle (int id, bool flag )
00521 {
00522 KToolBarButton * button = getButton( id );
00523 if ( button )
00524 button->setToggle( flag );
00525 }
00526
00527
00528 void KToolBar::toggleButton (int id)
00529 {
00530 KToolBarButton * button = getButton( id );
00531 if ( button )
00532 button->toggle();
00533 }
00534
00535
00536 void KToolBar::setButton (int id, bool flag)
00537 {
00538 KToolBarButton * button = getButton( id );
00539 if ( button )
00540 button->on( flag );
00541 }
00542
00543
00544 bool KToolBar::isButtonOn (int id) const
00545 {
00546 KToolBarButton * button = const_cast<KToolBar*>( this )->getButton( id );
00547 return button ? button->isOn() : false;
00548 }
00549
00550
00551 void KToolBar::setLinedText (int id, const QString& text)
00552 {
00553 KLineEdit * lineEdit = getLined( id );
00554 if ( lineEdit )
00555 lineEdit->setText( text );
00556 }
00557
00558
00559 QString KToolBar::getLinedText (int id) const
00560 {
00561 KLineEdit * lineEdit = const_cast<KToolBar*>( this )->getLined( id );
00562 return lineEdit ? lineEdit->text() : QString::null;
00563 }
00564
00565
00566 void KToolBar::insertComboItem (int id, const QString& text, int index)
00567 {
00568 KComboBox * comboBox = getCombo( id );
00569 if (comboBox)
00570 comboBox->insertItem( text, index );
00571 }
00572
00573 void KToolBar::insertComboList (int id, const QStringList &list, int index)
00574 {
00575 KComboBox * comboBox = getCombo( id );
00576 if (comboBox)
00577 comboBox->insertStringList( list, index );
00578 }
00579
00580
00581 void KToolBar::removeComboItem (int id, int index)
00582 {
00583 KComboBox * comboBox = getCombo( id );
00584 if (comboBox)
00585 comboBox->removeItem( index );
00586 }
00587
00588
00589 void KToolBar::setCurrentComboItem (int id, int index)
00590 {
00591 KComboBox * comboBox = getCombo( id );
00592 if (comboBox)
00593 comboBox->setCurrentItem( index );
00594 }
00595
00596
00597 void KToolBar::changeComboItem (int id, const QString& text, int index)
00598 {
00599 KComboBox * comboBox = getCombo( id );
00600 if (comboBox)
00601 comboBox->changeItem( text, index );
00602 }
00603
00604
00605 void KToolBar::clearCombo (int id)
00606 {
00607 KComboBox * comboBox = getCombo( id );
00608 if (comboBox)
00609 comboBox->clear();
00610 }
00611
00612
00613 QString KToolBar::getComboItem (int id, int index) const
00614 {
00615 KComboBox * comboBox = const_cast<KToolBar*>( this )->getCombo( id );
00616 return comboBox ? comboBox->text( index ) : QString::null;
00617 }
00618
00619
00620 KComboBox * KToolBar::getCombo(int id)
00621 {
00622 Id2WidgetMap::Iterator it = id2widget.find( id );
00623 if ( it == id2widget.end() )
00624 return 0;
00625 return dynamic_cast<KComboBox *>( *it );
00626 }
00627
00628
00629 KLineEdit * KToolBar::getLined (int id)
00630 {
00631 Id2WidgetMap::Iterator it = id2widget.find( id );
00632 if ( it == id2widget.end() )
00633 return 0;
00634 return dynamic_cast<KLineEdit *>( *it );
00635 }
00636
00637
00638 KToolBarButton * KToolBar::getButton (int id)
00639 {
00640 Id2WidgetMap::Iterator it = id2widget.find( id );
00641 if ( it == id2widget.end() )
00642 return 0;
00643 return dynamic_cast<KToolBarButton *>( *it );
00644 }
00645
00646
00647 void KToolBar::alignItemRight (int id, bool right )
00648 {
00649 Id2WidgetMap::Iterator it = id2widget.find( id );
00650 if ( it == id2widget.end() )
00651 return;
00652 if ( rightAligned && !right && (*it) == rightAligned )
00653 rightAligned = 0;
00654 if ( (*it) && right )
00655 rightAligned = (*it);
00656 }
00657
00658
00659 QWidget *KToolBar::getWidget (int id)
00660 {
00661 Id2WidgetMap::Iterator it = id2widget.find( id );
00662 return ( it == id2widget.end() ) ? 0 : (*it);
00663 }
00664
00665
00666 void KToolBar::setItemAutoSized (int id, bool yes )
00667 {
00668 QWidget *w = getWidget(id);
00669 if ( w && yes )
00670 setStretchableWidget( w );
00671 }
00672
00673
00674 void KToolBar::clear ()
00675 {
00676 QToolBar::clear();
00677 widget2id.clear();
00678 id2widget.clear();
00679 }
00680
00681
00682 void KToolBar::removeItem(int id)
00683 {
00684 Id2WidgetMap::Iterator it = id2widget.find( id );
00685 if ( it == id2widget.end() )
00686 {
00687 kdDebug(220) << name() << " KToolBar::removeItem item " << id << " not found" << endl;
00688 return;
00689 }
00690 QWidget * w = (*it);
00691 id2widget.remove( id );
00692 widget2id.remove( w );
00693 widgets.removeRef( w );
00694 delete w;
00695 }
00696
00697
00698 void KToolBar::removeItemDelayed(int id)
00699 {
00700 Id2WidgetMap::Iterator it = id2widget.find( id );
00701 if ( it == id2widget.end() )
00702 {
00703 kdDebug(220) << name() << " KToolBar::removeItem item " << id << " not found" << endl;
00704 return;
00705 }
00706 QWidget * w = (*it);
00707 id2widget.remove( id );
00708 widget2id.remove( w );
00709 widgets.removeRef( w );
00710
00711 w->blockSignals(true);
00712 d->idleButtons.append(w);
00713 layoutTimer->start( 50, true );
00714 }
00715
00716
00717 void KToolBar::hideItem (int id)
00718 {
00719 QWidget *w = getWidget(id);
00720 if ( w )
00721 w->hide();
00722 }
00723
00724
00725 void KToolBar::showItem (int id)
00726 {
00727 QWidget *w = getWidget(id);
00728 if ( w )
00729 w->show();
00730 }
00731
00732
00733 int KToolBar::itemIndex (int id)
00734 {
00735 QWidget *w = getWidget(id);
00736 return w ? widgets.findRef(w) : -1;
00737 }
00738
00739 int KToolBar::idAt (int index)
00740 {
00741 QWidget *w = widgets.at(index);
00742 return widget2id[w];
00743 }
00744
00745 void KToolBar::setFullSize(bool flag )
00746 {
00747 setHorizontalStretchable( flag );
00748 setVerticalStretchable( flag );
00749 }
00750
00751
00752 bool KToolBar::fullSize() const
00753 {
00754 return isHorizontalStretchable() || isVerticalStretchable();
00755 }
00756
00757
00758 void KToolBar::enableMoving(bool flag )
00759 {
00760 setMovingEnabled(flag);
00761 }
00762
00763
00764 void KToolBar::setBarPos (BarPosition bpos)
00765 {
00766 if ( !mainWindow() )
00767 return;
00768 mainWindow()->moveDockWindow( this, (Dock)bpos );
00769
00770 }
00771
00772
00773 KToolBar::BarPosition KToolBar::barPos() const
00774 {
00775 if ( !this->mainWindow() )
00776 return KToolBar::Top;
00777 Dock dock;
00778 int dm1, dm2;
00779 bool dm3;
00780 this->mainWindow()->getLocation( (QToolBar*)this, dock, dm1, dm3, dm2 );
00781 if ( dock == DockUnmanaged ) {
00782 return (KToolBar::BarPosition)DockTop;
00783 }
00784 return (BarPosition)dock;
00785 }
00786
00787
00788 bool KToolBar::enable(BarStatus stat)
00789 {
00790 bool mystat = isVisible();
00791
00792 if ( (stat == Toggle && mystat) || stat == Hide )
00793 hide();
00794 else
00795 show();
00796
00797 return isVisible() == mystat;
00798 }
00799
00800
00801 void KToolBar::setMaxHeight ( int h )
00802 {
00803 setMaximumHeight( h );
00804 }
00805
00806 int KToolBar::maxHeight()
00807 {
00808 return maximumHeight();
00809 }
00810
00811
00812 void KToolBar::setMaxWidth (int dw)
00813 {
00814 setMaximumWidth( dw );
00815 }
00816
00817
00818 int KToolBar::maxWidth()
00819 {
00820 return maximumWidth();
00821 }
00822
00823
00824 void KToolBar::setTitle (const QString& _title)
00825 {
00826 setLabel( _title );
00827 }
00828
00829
00830 void KToolBar::enableFloating (bool )
00831 {
00832 }
00833
00834
00835 void KToolBar::setIconText(IconText it)
00836 {
00837 setIconText( it, true );
00838 }
00839
00840
00841 void KToolBar::setIconText(IconText icontext, bool update)
00842 {
00843 bool doUpdate=false;
00844
00845 if (icontext != d->m_iconText) {
00846 d->m_iconText = icontext;
00847 doUpdate=true;
00848
00849 }
00850 else {
00851
00852 }
00853
00854 if (update == false)
00855 return;
00856
00857 if (doUpdate)
00858 doModeChange();
00859
00860
00861 QMainWindow *mw = mainWindow();
00862 if ( mw ) {
00863 mw->setUpdatesEnabled( false );
00864 mw->setToolBarsMovable( !mw->toolBarsMovable() );
00865 mw->setToolBarsMovable( !mw->toolBarsMovable() );
00866 mw->setUpdatesEnabled( true );
00867 }
00868 }
00869
00870
00871 KToolBar::IconText KToolBar::iconText() const
00872 {
00873 return d->m_iconText;
00874 }
00875
00876
00877 void KToolBar::setIconSize(int size)
00878 {
00879 setIconSize( size, true );
00880 }
00881
00882 void KToolBar::setIconSize(int size, bool update)
00883 {
00884 bool doUpdate=false;
00885
00886 if ( size != d->m_iconSize ) {
00887 d->m_iconSize = size;
00888 doUpdate=true;
00889 }
00890
00891 if (update == false)
00892 return;
00893
00894 if (doUpdate)
00895 doModeChange();
00896
00897
00898 if ( mainWindow() ) {
00899 QMainWindow *mw = mainWindow();
00900 mw->setUpdatesEnabled( false );
00901 mw->setToolBarsMovable( !mw->toolBarsMovable() );
00902 mw->setToolBarsMovable( !mw->toolBarsMovable() );
00903 mw->setUpdatesEnabled( true );
00904 }
00905 }
00906
00907
00908 int KToolBar::iconSize() const
00909 {
00910 if ( !d->m_iconSize )
00911 {
00912 if (!::qstrcmp(QObject::name(), "mainToolBar"))
00913 return KGlobal::iconLoader()->currentSize(KIcon::MainToolbar);
00914 else
00915 return KGlobal::iconLoader()->currentSize(KIcon::Toolbar);
00916 }
00917 return d->m_iconSize;
00918 }
00919
00920
00921 void KToolBar::setEnableContextMenu(bool enable )
00922 {
00923 d->m_enableContext = enable;
00924 }
00925
00926
00927 bool KToolBar::contextMenuEnabled() const
00928 {
00929 return d->m_enableContext;
00930 }
00931
00932
00933 void KToolBar::setItemNoStyle(int id, bool no_style )
00934 {
00935 KToolBarButton * button = getButton( id );
00936 if (button)
00937 button->setNoStyle( no_style );
00938 }
00939
00940
00941 void KToolBar::setFlat (bool flag)
00942 {
00943 if ( !mainWindow() )
00944 return;
00945 if ( flag )
00946 mainWindow()->moveDockWindow( this, DockMinimized );
00947 else
00948 mainWindow()->moveDockWindow( this, DockTop );
00949
00950 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
00951 if ( kmw )
00952 kmw->setSettingsDirty();
00953 }
00954
00955
00956 int KToolBar::count() const
00957 {
00958 return id2widget.count();
00959 }
00960
00961
00962 void KToolBar::saveState()
00963 {
00964
00965 if ( d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty() ) {
00966
00967 QString barname(!::qstrcmp(name(), "unnamed") ? "mainToolBar" : name());
00968
00969 d->modified = false;
00970
00971 QDomElement current;
00972 for( QDomNode n = d->m_xmlguiClient->domDocument().documentElement().firstChild();
00973 !n.isNull(); n = n.nextSibling()) {
00974 current = n.toElement();
00975
00976 if ( current.tagName().lower() != "toolbar" )
00977 continue;
00978
00979 QString curname(current.attribute( "name" ));
00980
00981 if ( curname == barname ) {
00982 saveState( current );
00983 break;
00984 }
00985 }
00986
00987 if ( !d->modified )
00988 return;
00989
00990
00991 QString local_xml(KXMLGUIFactory::readConfigFile(d->m_xmlguiClient->xmlFile(), true, d->m_xmlguiClient->instance()));
00992 QDomDocument local;
00993 local.setContent(local_xml);
00994
00995
00996 bool just_append = true;
00997
00998 for( QDomNode n = local.documentElement().firstChild();
00999 !n.isNull(); n = n.nextSibling()) {
01000 QDomElement elem = n.toElement();
01001
01002 if ( elem.tagName().lower() != "toolbar" )
01003 continue;
01004
01005 QString curname(elem.attribute( "name" ));
01006
01007 if ( curname == barname ) {
01008 just_append = false;
01009 local.documentElement().replaceChild( current, elem );
01010 break;
01011 }
01012 }
01013
01014 if (just_append)
01015 local.documentElement().appendChild( current );
01016
01017 KXMLGUIFactory::saveConfigFile(local, d->m_xmlguiClient->localXMLFile(), d->m_xmlguiClient->instance() );
01018
01019 return;
01020 }
01021
01022
01023 KConfig *config = KGlobal::config();
01024 saveSettings(config, QString::null);
01025 config->sync();
01026 }
01027
01028 QString KToolBar::settingsGroup() const
01029 {
01030 QString configGroup;
01031 if (!::qstrcmp(name(), "unnamed") || !::qstrcmp(name(), "mainToolBar"))
01032 configGroup = "Toolbar style";
01033 else
01034 configGroup = QString(name()) + " Toolbar style";
01035 if ( this->mainWindow() )
01036 {
01037 configGroup.prepend(" ");
01038 configGroup.prepend( this->mainWindow()->name() );
01039 }
01040 return configGroup;
01041 }
01042
01043 void KToolBar::saveSettings(KConfig *config, const QString &_configGroup)
01044 {
01045 QString configGroup = _configGroup;
01046 if (configGroup.isEmpty())
01047 configGroup = settingsGroup();
01048
01049
01050 QString position, icontext;
01051 int index;
01052 getAttributes( position, icontext, index );
01053
01054
01055
01056 KConfigGroupSaver saver(config, configGroup);
01057
01058 if(!config->hasDefault("Position") && position == d->PositionDefault )
01059 config->revertToDefault("Position");
01060 else
01061 config->writeEntry("Position", position);
01062
01063
01064
01065 if(d->m_honorStyle && icontext == d->IconTextDefault && !config->hasDefault("IconText") )
01066 {
01067
01068 config->revertToDefault("IconText");
01069 }
01070 else
01071 {
01072
01073 config->writeEntry("IconText", icontext);
01074 }
01075
01076 if(!config->hasDefault("IconSize") && iconSize() == d->IconSizeDefault )
01077 config->revertToDefault("IconSize");
01078 else
01079 config->writeEntry("IconSize", iconSize());
01080
01081 if(!config->hasDefault("Hidden") && isHidden() == d->HiddenDefault )
01082 config->revertToDefault("Hidden");
01083 else
01084 config->writeEntry("Hidden", isHidden());
01085
01086
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
01097
01098
01099
01100
01101 QPtrList<KToolBar> toolbarList;
01102 QPtrList<QToolBar> lst;
01103 for ( int i = (int)QMainWindow::DockUnmanaged; i <= (int)DockMinimized; ++i ) {
01104 lst = kmw->toolBars( (ToolBarDock)i );
01105 for ( QToolBar *tb = lst.first(); tb; tb = lst.next() ) {
01106 if ( !tb->inherits( "KToolBar" ) )
01107 continue;
01108 toolbarList.append( (KToolBar*)tb );
01109 }
01110 }
01111 QPtrListIterator<KToolBar> toolbarIterator( toolbarList );
01112 if ( !kmw || toolbarIterator.count() > 1 )
01113 config->writeEntry("Index", index);
01114 else
01115 config->revertToDefault("Index");
01116
01117 if(!config->hasDefault("Offset") && offset() == d->OffsetDefault )
01118 config->revertToDefault("Offset");
01119 else
01120 config->writeEntry("Offset", offset());
01121
01122 if(!config->hasDefault("NewLine") && newLine() == d->NewLineDefault )
01123 config->revertToDefault("NewLine");
01124 else
01125 config->writeEntry("NewLine", newLine());
01126 }
01127
01128
01129 void KToolBar::setXMLGUIClient( KXMLGUIClient *client )
01130 {
01131 d->m_xmlguiClient = client;
01132 }
01133
01134 void KToolBar::setText( const QString & txt )
01135 {
01136 setLabel( txt + " (" + kapp->caption() + ") " );
01137 }
01138
01139
01140 QString KToolBar::text() const
01141 {
01142 return label();
01143 }
01144
01145
01146 void KToolBar::doConnections( KToolBarButton *button )
01147 {
01148 connect(button, SIGNAL(clicked(int)), this, SIGNAL( clicked( int ) ) );
01149 connect(button, SIGNAL(doubleClicked(int)), this, SIGNAL( doubleClicked( int ) ) );
01150 connect(button, SIGNAL(released(int)), this, SIGNAL( released( int ) ) );
01151 connect(button, SIGNAL(pressed(int)), this, SIGNAL( pressed( int ) ) );
01152 connect(button, SIGNAL(toggled(int)), this, SIGNAL( toggled( int ) ) );
01153 connect(button, SIGNAL(highlighted(int, bool)), this, SIGNAL( highlighted( int, bool ) ) );
01154 }
01155
01156 void KToolBar::mousePressEvent ( QMouseEvent *m )
01157 {
01158 if ( !mainWindow() )
01159 return;
01160 QMainWindow *mw = mainWindow();
01161 if ( mw->toolBarsMovable() && d->m_enableContext ) {
01162 if ( m->button() == RightButton ) {
01163 QGuardedPtr<KToolBar> guard( this );
01164 int i = contextMenu()->exec( m->globalPos(), 0 );
01165
01166 if ( guard )
01167 slotContextAboutToHide();
01168 switch ( i ) {
01169 case -1:
01170 return;
01171 case CONTEXT_LEFT:
01172 mw->moveDockWindow( this, DockLeft );
01173 break;
01174 case CONTEXT_RIGHT:
01175 mw->moveDockWindow( this, DockRight );
01176 break;
01177 case CONTEXT_TOP:
01178 mw->moveDockWindow( this, DockTop );
01179 break;
01180 case CONTEXT_BOTTOM:
01181 mw->moveDockWindow( this, DockBottom );
01182 break;
01183 case CONTEXT_FLOAT:
01184 mw->moveDockWindow( this, DockTornOff );
01185 break;
01186 case CONTEXT_FLAT:
01187 mw->moveDockWindow( this, DockMinimized );
01188 break;
01189 case CONTEXT_ICONS:
01190 setIconText( IconOnly );
01191 break;
01192 case CONTEXT_TEXTRIGHT:
01193 setIconText( IconTextRight );
01194 break;
01195 case CONTEXT_TEXT:
01196 setIconText( TextOnly );
01197 break;
01198 case CONTEXT_TEXTUNDER:
01199 setIconText( IconTextBottom );
01200 break;
01201 default:
01202 if ( i >= CONTEXT_ICONSIZES )
01203 setIconSize( i - CONTEXT_ICONSIZES );
01204 else
01205 return;
01206 }
01207 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mw);
01208 if ( kmw )
01209 kmw->setSettingsDirty();
01210 }
01211 }
01212 }
01213
01214 void KToolBar::doModeChange()
01215 {
01216 for(QWidget *w=d->idleButtons.first(); w; w=d->idleButtons.next())
01217 w->blockSignals(false);
01218 d->idleButtons.clear();
01219
01220 emit modechange();
01221 }
01222
01223 void KToolBar::rebuildLayout()
01224 {
01225 for(QWidget *w=d->idleButtons.first(); w; w=d->idleButtons.next())
01226 w->blockSignals(false);
01227 d->idleButtons.clear();
01228
01229 layoutTimer->stop();
01230 QApplication::sendPostedEvents( this, QEvent::ChildInserted );
01231 QBoxLayout *l = boxLayout();
01232
01233
01234 QLayoutIterator it = l->iterator();
01235 while ( it.current() )
01236 it.deleteCurrent();
01237
01238 for ( QWidget *w = widgets.first(); w; w = widgets.next() ) {
01239 if ( w == rightAligned )
01240 continue;
01241 KToolBarSeparator *ktbs = dynamic_cast<KToolBarSeparator *>(w);
01242 if ( ktbs && !ktbs->showLine() ) {
01243 l->addSpacing( orientation() == Vertical ? w->sizeHint().height() : w->sizeHint().width() );
01244 w->hide();
01245 continue;
01246 }
01247 if ( dynamic_cast<QPopupMenu *>(w) )
01248 continue;
01249 l->addWidget( w );
01250 w->show();
01251 if ((orientation() == Horizontal) && dynamic_cast<QLineEdit *>(w))
01252 l->addSpacing(2);
01253 }
01254 if ( rightAligned ) {
01255 l->addStretch();
01256 l->addWidget( rightAligned );
01257 rightAligned->show();
01258 }
01259
01260 if ( fullSize() ) {
01261 if ( !rightAligned )
01262 l->addStretch();
01263 if ( stretchableWidget )
01264 l->setStretchFactor( stretchableWidget, 10 );
01265 }
01266 l->invalidate();
01267 QApplication::postEvent( this, new QEvent( QEvent::LayoutHint ) );
01268 }
01269
01270 void KToolBar::childEvent( QChildEvent *e )
01271 {
01272 if ( e->child()->isWidgetType() ) {
01273 QWidget * w = dynamic_cast<QWidget *>(e->child());
01274 if (!w || (::qstrcmp( "qt_dockwidget_internal", w->name()) == 0))
01275 {
01276 QToolBar::childEvent( e );
01277 return;
01278 }
01279 if ( e->type() == QEvent::ChildInserted ) {
01280 if ( !dynamic_cast<QPopupMenu *>(w)) {
01281
01282
01283 if ( !widget2id.contains( w ) )
01284 {
01285 int dummy = -1;
01286 insertWidgetInternal( w, dummy, -1 );
01287 }
01288 }
01289 } else {
01290 removeWidgetInternal( w );
01291 }
01292 if ( isVisibleTo( 0 ) )
01293 {
01294 layoutTimer->start( 50, true );
01295 QBoxLayout *l = boxLayout();
01296
01297
01298
01299 QLayoutIterator it = l->iterator();
01300 while ( it.current() )
01301 it.deleteCurrent();
01302 }
01303 }
01304 QToolBar::childEvent( e );
01305 }
01306
01307 void KToolBar::insertWidgetInternal( QWidget *w, int &index, int id )
01308 {
01309
01310
01311
01312 connect( w, SIGNAL( destroyed() ),
01313 this, SLOT( widgetDestroyed() ) );
01314 if ( index == -1 || index > (int)widgets.count() ) {
01315 index = (int)widgets.count();
01316 widgets.append( w );
01317 }
01318 else
01319 widgets.insert( index, w );
01320 if ( id == -1 )
01321 id = id2widget.count();
01322 id2widget.insert( id, w );
01323 widget2id.insert( w, id );
01324 }
01325
01326 void KToolBar::showEvent( QShowEvent *e )
01327 {
01328 QToolBar::showEvent( e );
01329 rebuildLayout();
01330 }
01331
01332 void KToolBar::setStretchableWidget( QWidget *w )
01333 {
01334 QToolBar::setStretchableWidget( w );
01335 stretchableWidget = w;
01336 }
01337
01338 QSizePolicy KToolBar::sizePolicy() const
01339 {
01340 if ( orientation() == Horizontal )
01341 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
01342 else
01343 return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
01344 }
01345
01346 QSize KToolBar::sizeHint() const
01347 {
01348 QSize minSize(0,0);
01349 KToolBar *ncThis = const_cast<KToolBar *>(this);
01350
01351 ncThis->polish();
01352
01353 int margin = static_cast<QWidget*>(ncThis)->layout()->margin() + frameWidth();
01354 switch( barPos() )
01355 {
01356 case KToolBar::Top:
01357 case KToolBar::Bottom:
01358 for ( QWidget *w = ncThis->widgets.first(); w; w = ncThis->widgets.next() )
01359 {
01360 QSize sh = w->sizeHint();
01361 if ( w->sizePolicy().horData() == QSizePolicy::Ignored )
01362 sh.setWidth( 1 );
01363 if ( w->sizePolicy().verData() == QSizePolicy::Ignored )
01364 sh.setHeight( 1 );
01365 sh = sh.boundedTo( w->maximumSize() )
01366 .expandedTo( w->minimumSize() ).expandedTo( QSize(1, 1) );
01367
01368 minSize = minSize.expandedTo(QSize(0, sh.height()));
01369 minSize += QSize(sh.width()+1, 0);
01370 if (dynamic_cast<QLineEdit *>(w))
01371 minSize += QSize(2, 0);
01372 }
01373
01374 minSize += QSize(QApplication::style().pixelMetric( QStyle::PM_DockWindowHandleExtent ), 0);
01375 minSize += QSize(margin*2, margin*2);
01376 break;
01377
01378 case KToolBar::Left:
01379 case KToolBar::Right:
01380 for ( QWidget *w = ncThis->widgets.first(); w; w = ncThis->widgets.next() )
01381 {
01382 QSize sh = w->sizeHint();
01383 if ( w->sizePolicy().horData() == QSizePolicy::Ignored )
01384 sh.setWidth( 1 );
01385 if ( w->sizePolicy().verData() == QSizePolicy::Ignored )
01386 sh.setHeight( 1 );
01387 sh = sh.boundedTo( w->maximumSize() )
01388 .expandedTo( w->minimumSize() ).expandedTo( QSize(1, 1) );
01389
01390 minSize = minSize.expandedTo(QSize(sh.width(), 0));
01391 minSize += QSize(0, sh.height()+1);
01392 }
01393 minSize += QSize(0, QApplication::style().pixelMetric( QStyle::PM_DockWindowHandleExtent ));
01394 minSize += QSize(margin*2, margin*2);
01395 break;
01396
01397 default:
01398 minSize = QToolBar::sizeHint();
01399 break;
01400 }
01401 return minSize;
01402 }
01403
01404 QSize KToolBar::minimumSize() const
01405 {
01406 return minimumSizeHint();
01407 }
01408
01409 QSize KToolBar::minimumSizeHint() const
01410 {
01411 return sizeHint();
01412 }
01413
01414 bool KToolBar::highlight() const
01415 {
01416 return d->m_highlight;
01417 }
01418
01419 void KToolBar::hide()
01420 {
01421 QToolBar::hide();
01422 }
01423
01424 void KToolBar::show()
01425 {
01426 QToolBar::show();
01427 }
01428
01429 void KToolBar::resizeEvent( QResizeEvent *e )
01430 {
01431 bool b = isUpdatesEnabled();
01432 setUpdatesEnabled( false );
01433 QToolBar::resizeEvent( e );
01434 if (b)
01435 {
01436 if (layoutTimer->isActive())
01437 {
01438
01439 d->repaintTimer.start( 100, true );
01440 }
01441 else
01442 {
01443
01444 slotRepaint();
01445 }
01446 }
01447 }
01448
01449 void KToolBar::slotIconChanged(int group)
01450 {
01451 if ((group != KIcon::Toolbar) && (group != KIcon::MainToolbar))
01452 return;
01453 if ((group == KIcon::MainToolbar) != !::qstrcmp(name(), "mainToolBar"))
01454 return;
01455
01456 doModeChange();
01457
01458 if (isVisible())
01459 updateGeometry();
01460 }
01461
01462 void KToolBar::slotReadConfig()
01463 {
01464
01465
01466
01467
01468 applyAppearanceSettings(KGlobal::config(), QString::null );
01469 }
01470
01471 void KToolBar::slotAppearanceChanged()
01472 {
01473
01474 applyAppearanceSettings(KGlobal::config(), QString::null, true );
01475
01476
01477 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
01478 if ( kmw )
01479 kmw->setSettingsDirty();
01480 }
01481
01482
01483 bool KToolBar::highlightSetting()
01484 {
01485 QString grpToolbar(QString::fromLatin1("Toolbar style"));
01486 KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01487 return KGlobal::config()->readBoolEntry(QString::fromLatin1("Highlighting"),true);
01488 }
01489
01490
01491 bool KToolBar::transparentSetting()
01492 {
01493 QString grpToolbar(QString::fromLatin1("Toolbar style"));
01494 KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01495 return KGlobal::config()->readBoolEntry(QString::fromLatin1("TransparentMoving"),true);
01496 }
01497
01498
01499 KToolBar::IconText KToolBar::iconTextSetting()
01500 {
01501 QString grpToolbar(QString::fromLatin1("Toolbar style"));
01502 KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01503 QString icontext = KGlobal::config()->readEntry(QString::fromLatin1("IconText"),QString::fromLatin1("IconOnly"));
01504 if ( icontext == "IconTextRight" )
01505 return IconTextRight;
01506 else if ( icontext == "IconTextBottom" )
01507 return IconTextBottom;
01508 else if ( icontext == "TextOnly" )
01509 return TextOnly;
01510 else
01511 return IconOnly;
01512 }
01513
01514 void KToolBar::applyAppearanceSettings(KConfig *config, const QString &_configGroup, bool forceGlobal)
01515 {
01516 QString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
01517
01518
01519
01520
01521
01522
01523
01524 bool xmlgui = d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty();
01525
01526 KConfig *gconfig = KGlobal::config();
01527
01528 static const QString &attrIconText = KGlobal::staticQString("IconText");
01529 static const QString &attrHighlight = KGlobal::staticQString("Highlighting");
01530 static const QString &attrTrans = KGlobal::staticQString("TransparentMoving");
01531 static const QString &attrIconSize = KGlobal::staticQString("IconSize");
01532
01533
01534
01535
01536 bool highlight;
01537 int transparent;
01538 bool applyIconText = !xmlgui;
01539 bool applyIconSize = !xmlgui;
01540
01541 int iconSize = d->IconSizeDefault;
01542 QString iconText = d->IconTextDefault;
01543
01544
01545 QString grpToolbar(QString::fromLatin1("Toolbar style"));
01546 {
01547 KConfigGroupSaver saver(gconfig, grpToolbar);
01548
01549
01550 highlight = gconfig->readBoolEntry(attrHighlight, true);
01551 transparent = gconfig->readBoolEntry(attrTrans, true);
01552
01553
01554
01555 if (d->m_honorStyle)
01556 d->IconTextDefault = gconfig->readEntry(attrIconText, d->IconTextDefault);
01557 else
01558 d->IconTextDefault = "IconOnly";
01559
01560
01561 d->IconSizeDefault = gconfig->readNumEntry(attrIconSize, d->IconSizeDefault);
01562
01563 iconSize = d->IconSizeDefault;
01564 iconText = d->IconTextDefault;
01565
01566 if ( !forceGlobal && config->hasGroup(configGroup) )
01567 {
01568 config->setGroup(configGroup);
01569
01570
01571 highlight = config->readBoolEntry(attrHighlight, highlight);
01572 transparent = config->readBoolEntry(attrTrans, transparent);
01573
01574
01575 if ( config->hasKey( attrIconText ) ) {
01576 iconText = config->readEntry(attrIconText);
01577 applyIconText = true;
01578
01579 }
01580
01581
01582 if ( config->hasKey( attrIconSize ) ) {
01583 iconSize = config->readNumEntry(attrIconSize);
01584 applyIconSize = true;
01585 }
01586 }
01587
01588
01589 }
01590
01591 bool doUpdate = false;
01592
01593 IconText icon_text;
01594 if ( iconText == "IconTextRight" )
01595 icon_text = IconTextRight;
01596 else if ( iconText == "IconTextBottom" )
01597 icon_text = IconTextBottom;
01598 else if ( iconText == "TextOnly" )
01599 icon_text = TextOnly;
01600 else
01601 icon_text = IconOnly;
01602
01603
01604 if (icon_text != d->m_iconText && applyIconText) {
01605
01606 setIconText(icon_text, false);
01607 doUpdate = true;
01608 }
01609
01610
01611 if (iconSize != d->m_iconSize && applyIconSize) {
01612 setIconSize(iconSize, false);
01613 doUpdate = true;
01614 }
01615
01616 QMainWindow *mw = mainWindow();
01617
01618
01619 if ( highlight != d->m_highlight ) {
01620 d->m_highlight = highlight;
01621 doUpdate = true;
01622 }
01623
01624
01625 if ( mw && transparent != (!mw->opaqueMoving()) ) {
01626 mw->setOpaqueMoving( !transparent );
01627 }
01628
01629 if (doUpdate)
01630 doModeChange();
01631
01632 if (isVisible ())
01633 updateGeometry();
01634 }
01635
01636 void KToolBar::applySettings(KConfig *config, const QString &_configGroup)
01637 {
01638 return applySettings(config,_configGroup,false);
01639 }
01640
01641 void KToolBar::applySettings(KConfig *config, const QString &_configGroup,bool force)
01642 {
01643
01644
01645 QString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661
01662 applyAppearanceSettings( config, configGroup );
01663
01664
01665 if ( config->hasGroup(configGroup) || force )
01666 {
01667 KConfigGroupSaver cgs(config, configGroup);
01668
01669 static const QString &attrPosition = KGlobal::staticQString("Position");
01670 static const QString &attrIndex = KGlobal::staticQString("Index");
01671 static const QString &attrOffset = KGlobal::staticQString("Offset");
01672 static const QString &attrNewLine = KGlobal::staticQString("NewLine");
01673 static const QString &attrHidden = KGlobal::staticQString("Hidden");
01674
01675 QString position = config->readEntry(attrPosition, d->PositionDefault);
01676 int index = config->readNumEntry(attrIndex, -1);
01677 int offset = config->readNumEntry(attrOffset, d->OffsetDefault);
01678 bool newLine = config->readBoolEntry(attrNewLine, d->NewLineDefault);
01679 bool hidden = config->readBoolEntry(attrHidden, d->HiddenDefault);
01680
01681 Dock pos(DockTop);
01682 if ( position == "Top" )
01683 pos = DockTop;
01684 else if ( position == "Bottom" )
01685 pos = DockBottom;
01686 else if ( position == "Left" )
01687 pos = DockLeft;
01688 else if ( position == "Right" )
01689 pos = DockRight;
01690 else if ( position == "Floating" )
01691 pos = DockTornOff;
01692 else if ( position == "Flat" )
01693 pos = DockMinimized;
01694
01695
01696 if (hidden)
01697 hide();
01698 else
01699 show();
01700
01701 if ( mainWindow() )
01702 {
01703
01704 d->toolBarInfo = KToolBarPrivate::ToolBarInfo( pos, index, newLine, offset );
01705 positionYourself( true );
01706 }
01707 if (isVisible ())
01708 updateGeometry();
01709 }
01710 }
01711
01712 bool KToolBar::event( QEvent *e )
01713 {
01714 if ( (e->type() == QEvent::LayoutHint) && isUpdatesEnabled() )
01715 d->repaintTimer.start( 100, true );
01716
01717 if (e->type() == QEvent::ChildInserted )
01718 {
01719
01720
01721
01722 childEvent((QChildEvent *)e);
01723 return true;
01724 }
01725
01726 return QToolBar::event( e );
01727 }
01728
01729 void KToolBar::slotRepaint()
01730 {
01731 setUpdatesEnabled( false );
01732
01733
01734
01735 QResizeEvent ev(size(), size());
01736 resizeEvent(&ev);
01737 QApplication::sendPostedEvents( this, QEvent::LayoutHint );
01738 setUpdatesEnabled( true );
01739 repaint( true );
01740 }
01741
01742 void KToolBar::toolBarPosChanged( QToolBar *tb )
01743 {
01744 if ( tb != this )
01745 return;
01746 if ( d->oldPos == DockMinimized )
01747 rebuildLayout();
01748 d->oldPos = (QMainWindow::ToolBarDock)barPos();
01749 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
01750 if ( kmw )
01751 kmw->setSettingsDirty();
01752 }
01753
01754 void KToolBar::loadState( const QDomElement &element )
01755 {
01756
01757 QMainWindow *mw = mainWindow();
01758
01759 if ( !mw )
01760 return;
01761
01762 {
01763 QCString text = element.namedItem( "text" ).toElement().text().utf8();
01764 if ( text.isEmpty() )
01765 text = element.namedItem( "Text" ).toElement().text().utf8();
01766 if ( !text.isEmpty() )
01767 setText( i18n( text ) );
01768 }
01769
01770 {
01771 QCString attrFullWidth = element.attribute( "fullWidth" ).lower().latin1();
01772 if ( !attrFullWidth.isEmpty() )
01773 setFullSize( attrFullWidth == "true" );
01774 }
01775
01776 Dock dock = DockTop;
01777 {
01778 QCString attrPosition = element.attribute( "position" ).lower().latin1();
01779
01780 if ( !attrPosition.isEmpty() ) {
01781 if ( attrPosition == "top" )
01782 dock = DockTop;
01783 else if ( attrPosition == "left" )
01784 dock = DockLeft;
01785 else if ( attrPosition == "right" )
01786 dock = DockRight;
01787 else if ( attrPosition == "bottom" )
01788 dock = DockBottom;
01789 else if ( attrPosition == "floating" )
01790 dock = DockTornOff;
01791 else if ( attrPosition == "flat" )
01792 dock = DockMinimized;
01793 }
01794 }
01795
01796 {
01797 QCString attrIconText = element.attribute( "iconText" ).lower().latin1();
01798 if ( !attrIconText.isEmpty() ) {
01799
01800 if ( attrIconText == "icontextright" )
01801 setIconText( KToolBar::IconTextRight );
01802 else if ( attrIconText == "textonly" )
01803 setIconText( KToolBar::TextOnly );
01804 else if ( attrIconText == "icontextbottom" )
01805 setIconText( KToolBar::IconTextBottom );
01806 else if ( attrIconText == "icononly" )
01807 setIconText( KToolBar::IconOnly );
01808 } else
01809 {
01810
01811
01812 if (d->m_honorStyle)
01813 setIconText( iconTextSetting() );
01814 else
01815 setIconText( d->IconTextDefault);
01816 }
01817 }
01818
01819 {
01820 QString attrIconSize = element.attribute( "iconSize" ).lower();
01821 if ( !attrIconSize.isEmpty() )
01822 d->IconSizeDefault = attrIconSize.toInt();
01823 setIconSize( d->IconSizeDefault );
01824 }
01825
01826 int index = -1;
01827
01828 {
01829 QString attrIndex = element.attribute( "index" ).lower();
01830 if ( !attrIndex.isEmpty() )
01831 index = attrIndex.toInt();
01832 }
01833
01834 {
01835 QString attrOffset = element.attribute( "offset" ).lower();
01836 if ( !attrOffset.isEmpty() )
01837 d->OffsetDefault = attrOffset.toInt();
01838 }
01839
01840 {
01841 QString attrNewLine = element.attribute( "newline" ).lower();
01842 if ( !attrNewLine.isEmpty() )
01843 d->NewLineDefault = attrNewLine == "true";
01844 }
01845
01846 {
01847 QString attrHidden = element.attribute( "hidden" ).lower();
01848 if ( !attrHidden.isEmpty() )
01849 d->HiddenDefault = attrHidden == "true";
01850 }
01851
01852 d->toolBarInfo = KToolBarPrivate::ToolBarInfo( dock, index, d->NewLineDefault, d->OffsetDefault );
01853 mw->addDockWindow( this, dock, d->NewLineDefault );
01854 mw->moveDockWindow( this, dock, d->NewLineDefault, index, d->OffsetDefault );
01855
01856
01857 d->m_highlight = highlightSetting();
01858
01859
01860
01861 if ( transparentSetting() != !mw->opaqueMoving() )
01862 mw->setOpaqueMoving( !transparentSetting() );
01863
01864 if ( d->HiddenDefault )
01865 hide();
01866 else
01867 show();
01868
01869 getAttributes( d->PositionDefault, d->IconTextDefault, index );
01870
01871 }
01872
01873 int KToolBar::dockWindowIndex()
01874 {
01875 int index = 0;
01876 Q_ASSERT( mainWindow() );
01877 if ( mainWindow() ) {
01878 QMainWindow::ToolBarDock dock;
01879 bool newLine;
01880 int offset;
01881 mainWindow()->getLocation( this, dock, index, newLine, offset );
01882 }
01883 return index;
01884 }
01885
01886 void KToolBar::getAttributes( QString &position, QString &icontext, int &index )
01887 {
01888
01889 switch ( barPos() ) {
01890 case KToolBar::Flat:
01891 position = "Flat";
01892 break;
01893 case KToolBar::Bottom:
01894 position = "Bottom";
01895 break;
01896 case KToolBar::Left:
01897 position = "Left";
01898 break;
01899 case KToolBar::Right:
01900 position = "Right";
01901 break;
01902 case KToolBar::Floating:
01903 position = "Floating";
01904 break;
01905 case KToolBar::Top:
01906 default:
01907 position = "Top";
01908 break;
01909 }
01910
01911 index = dockWindowIndex();
01912
01913 switch (d->m_iconText) {
01914 case KToolBar::IconTextRight:
01915 icontext = "IconTextRight";
01916 break;
01917 case KToolBar::IconTextBottom:
01918 icontext = "IconTextBottom";
01919 break;
01920 case KToolBar::TextOnly:
01921 icontext = "TextOnly";
01922 break;
01923 case KToolBar::IconOnly:
01924 default:
01925 icontext = "IconOnly";
01926 break;
01927 }
01928
01929 }
01930
01931 void KToolBar::saveState( QDomElement ¤t )
01932 {
01933 Q_ASSERT( !current.isNull() );
01934 QString position, icontext;
01935 int index = -1;
01936 getAttributes( position, icontext, index );
01937
01938 current.setAttribute( "noMerge", "1" );
01939 current.setAttribute( "position", position );
01940 current.setAttribute( "iconText", icontext );
01941 current.setAttribute( "index", index );
01942 current.setAttribute( "offset", offset() );
01943 current.setAttribute( "newline", newLine() );
01944 if ( isHidden() )
01945 current.setAttribute( "hidden", "true" );
01946 d->modified = true;
01947
01948 }
01949
01950
01951 void KToolBar::positionYourself( bool force )
01952 {
01953 if (force)
01954 d->positioned = false;
01955
01956 if ( d->positioned || !mainWindow() )
01957 {
01958
01959 return;
01960 }
01961
01962
01963 bool doHide = isHidden();
01964
01965 mainWindow()->moveDockWindow( this, d->toolBarInfo.dock,
01966 d->toolBarInfo.newline,
01967 d->toolBarInfo.index,
01968 d->toolBarInfo.offset );
01969
01970
01971 if ( doHide )
01972 hide();
01973
01974 d->positioned = true;
01975 }
01976
01977 KPopupMenu *KToolBar::contextMenu()
01978 {
01979 if ( context )
01980 return context;
01981
01982
01983 context = new KPopupMenu( this, "qt_dockwidget_internal" );
01984 context->insertTitle(i18n("Toolbar Menu"));
01985
01986 KPopupMenu *orient = new KPopupMenu( context, "orient" );
01987 orient->insertItem( i18n("toolbar position string","Top"), CONTEXT_TOP );
01988 orient->insertItem( i18n("toolbar position string","Left"), CONTEXT_LEFT );
01989 orient->insertItem( i18n("toolbar position string","Right"), CONTEXT_RIGHT );
01990 orient->insertItem( i18n("toolbar position string","Bottom"), CONTEXT_BOTTOM );
01991 orient->insertSeparator(-1);
01992 orient->insertItem( i18n("toolbar position string","Floating"), CONTEXT_FLOAT );
01993 orient->insertItem( i18n("min toolbar", "Flat"), CONTEXT_FLAT );
01994
01995 KPopupMenu *mode = new KPopupMenu( context, "mode" );
01996 mode->insertItem( i18n("Icons Only"), CONTEXT_ICONS );
01997 mode->insertItem( i18n("Text Only"), CONTEXT_TEXT );
01998 mode->insertItem( i18n("Text Alongside Icons"), CONTEXT_TEXTRIGHT );
01999 mode->insertItem( i18n("Text Under Icons"), CONTEXT_TEXTUNDER );
02000
02001 KPopupMenu *size = new KPopupMenu( context, "size" );
02002 size->insertItem( i18n("Default"), CONTEXT_ICONSIZES );
02003
02004 KIconTheme *theme = KGlobal::instance()->iconLoader()->theme();
02005 QValueList<int> avSizes;
02006 if (theme)
02007 {
02008 if (!::qstrcmp(QObject::name(), "mainToolBar"))
02009 avSizes = theme->querySizes( KIcon::MainToolbar);
02010 else
02011 avSizes = theme->querySizes( KIcon::Toolbar);
02012 }
02013
02014 d->iconSizes = avSizes;
02015 qHeapSort(avSizes);
02016
02017 QValueList<int>::Iterator it;
02018 if (avSizes.count() < 10) {
02019
02020 for (it=avSizes.begin(); it!=avSizes.end(); it++) {
02021 QString text;
02022 if ( *it < 19 )
02023 text = i18n("Small (%1x%2)").arg(*it).arg(*it);
02024 else if (*it < 25)
02025 text = i18n("Medium (%1x%2)").arg(*it).arg(*it);
02026 else if (*it < 35)
02027 text = i18n("Large (%1x%2)").arg(*it).arg(*it);
02028 else
02029 text = i18n("Huge (%1x%2)").arg(*it).arg(*it);
02030
02031 size->insertItem( text, CONTEXT_ICONSIZES + *it );
02032 }
02033 }
02034 else {
02035
02036 const int progression[] = {16, 22, 32, 48, 64, 96, 128, 192, 256};
02037
02038 it = avSizes.begin();
02039 for (uint i = 0; i < 9; i++) {
02040 while (it++ != avSizes.end()) {
02041 if (*it >= progression[i]) {
02042 QString text;
02043 if ( *it < 19 )
02044 text = i18n("Small (%1x%2)").arg(*it).arg(*it);
02045 else if (*it < 25)
02046 text = i18n("Medium (%1x%2)").arg(*it).arg(*it);
02047 else if (*it < 35)
02048 text = i18n("Large (%1x%2)").arg(*it).arg(*it);
02049 else
02050 text = i18n("Huge (%1x%2)").arg(*it).arg(*it);
02051
02052 size->insertItem( text, CONTEXT_ICONSIZES + *it );
02053 break;
02054 }
02055 }
02056 }
02057 }
02058
02059 context->insertItem( i18n("Orientation"), orient );
02060 orient->setItemChecked(CONTEXT_TOP, true);
02061 context->insertItem( i18n("Text Position"), mode );
02062 context->setItemChecked(CONTEXT_ICONS, true);
02063 context->insertItem( i18n("Icon Size"), size );
02064
02065 connect( context, SIGNAL( aboutToShow() ), this, SLOT( slotContextAboutToShow() ) );
02066
02067
02068
02069 return context;
02070 }
02071
02072 void KToolBar::slotContextAboutToShow()
02073 {
02074
02075
02076
02077
02078
02079 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
02080 if ( kmw ) {
02081 kmw->setupToolbarMenuActions();
02082
02083 KAction *tbAction = kmw->toolBarMenuAction();
02084 if ( tbAction && tbAction->containerCount() > 0 )
02085 tbAction->plug(context);
02086 }
02087
02088
02089 KAction *configureAction = 0;
02090 const char* actionName = KStdAction::name(KStdAction::ConfigureToolbars);
02091 if ( d->m_xmlguiClient )
02092 configureAction = d->m_xmlguiClient->actionCollection()->action(actionName);
02093 if ( !configureAction && kmw )
02094 configureAction = kmw->actionCollection()->action(actionName);
02095 if ( configureAction )
02096 configureAction->plug(context);
02097 KEditToolbar::setDefaultToolbar(QObject::name());
02098
02099 for(int i = CONTEXT_ICONS; i <= CONTEXT_TEXTUNDER; ++i)
02100 context->setItemChecked(i, false);
02101
02102 switch( d->m_iconText )
02103 {
02104 case IconOnly:
02105 default:
02106 context->setItemChecked(CONTEXT_ICONS, true);
02107 break;
02108 case IconTextRight:
02109 context->setItemChecked(CONTEXT_TEXTRIGHT, true);
02110 break;
02111 case TextOnly:
02112 context->setItemChecked(CONTEXT_TEXT, true);
02113 break;
02114 case IconTextBottom:
02115 context->setItemChecked(CONTEXT_TEXTUNDER, true);
02116 break;
02117 }
02118
02119 QValueList<int>::ConstIterator iIt = d->iconSizes.begin();
02120 QValueList<int>::ConstIterator iEnd = d->iconSizes.end();
02121 for (; iIt != iEnd; ++iIt )
02122 context->setItemChecked( CONTEXT_ICONSIZES + *iIt, false );
02123
02124 context->setItemChecked( CONTEXT_ICONSIZES, false );
02125
02126 context->setItemChecked( CONTEXT_ICONSIZES + d->m_iconSize, true );
02127
02128 for ( int i = CONTEXT_TOP; i <= CONTEXT_FLAT; ++i )
02129 context->setItemChecked( i, false );
02130
02131 switch ( barPos() )
02132 {
02133 case KToolBar::Flat:
02134 context->setItemChecked( CONTEXT_FLAT, true );
02135 break;
02136 case KToolBar::Bottom:
02137 context->setItemChecked( CONTEXT_BOTTOM, true );
02138 break;
02139 case KToolBar::Left:
02140 context->setItemChecked( CONTEXT_LEFT, true );
02141 break;
02142 case KToolBar::Right:
02143 context->setItemChecked( CONTEXT_RIGHT, true );
02144 break;
02145 case KToolBar::Floating:
02146 context->setItemChecked( CONTEXT_FLOAT, true );
02147 break;
02148 case KToolBar::Top:
02149 context->setItemChecked( CONTEXT_TOP, true );
02150 break;
02151 default: break;
02152 }
02153 }
02154
02155 void KToolBar::slotContextAboutToHide()
02156 {
02157
02158
02159 KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
02160 if ( kmw && kmw->toolBarMenuAction() )
02161 if ( kmw->toolBarMenuAction()->containerCount() > 1 )
02162 kmw->toolBarMenuAction()->unplug(context);
02163
02164
02165 KAction *configureAction = 0;
02166 const char* actionName = KStdAction::name(KStdAction::ConfigureToolbars);
02167 if ( d->m_xmlguiClient )
02168 configureAction = d->m_xmlguiClient->actionCollection()->action(actionName);
02169 if ( !configureAction && kmw )
02170 configureAction = kmw->actionCollection()->action(actionName);
02171 if ( configureAction )
02172 configureAction->unplug(context);
02173
02174 QPtrListIterator<QWidget> it( widgets );
02175 QWidget *wdg;
02176 while ( ( wdg = it.current() ) != 0 ) {
02177 if ( wdg->inherits( "QToolButton" ) )
02178 static_cast<QToolButton*>( wdg )->setDown( false );
02179 ++it;
02180 }
02181 }
02182
02183 void KToolBar::widgetDestroyed()
02184 {
02185 removeWidgetInternal( (QWidget*)sender() );
02186 }
02187
02188 void KToolBar::removeWidgetInternal( QWidget * w )
02189 {
02190 widgets.removeRef( w );
02191 QMap< QWidget*, int >::Iterator it = widget2id.find( w );
02192 if ( it == widget2id.end() )
02193 return;
02194 id2widget.remove( *it );
02195 widget2id.remove( it );
02196 }
02197
02198 void KToolBar::virtual_hook( int, void* )
02199 { }
02200
02201 #include "ktoolbar.moc"
02202