kdeui Library: ktoolbar.cpp Source File (kdeui Library)

| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# ktoolbar.cpp

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

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:24 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003