kdeui Library API Documentation

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 #ifdef KDE_USE_FINAL
00027 #undef Always
00028 #include <qdockwindow.h>
00029 #endif
00030 #include "ktoolbar.h"
00031 #include "kmainwindow.h"
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 
00044 #include <config.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 <kipc.h>
00059 #include <kwin.h>
00060 #include <kdebug.h>
00061 #include <qlayout.h>
00062 
00063 #include "ktoolbarbutton.h"
00064 
00065 
00066 enum {
00067     CONTEXT_TOP = 0,
00068     CONTEXT_LEFT = 1,
00069     CONTEXT_RIGHT = 2,
00070     CONTEXT_BOTTOM = 3,
00071     CONTEXT_FLOAT = 4,
00072     CONTEXT_FLAT = 5,
00073     CONTEXT_ICONS = 6,
00074     CONTEXT_TEXT = 7,
00075     CONTEXT_TEXTRIGHT = 8,
00076     CONTEXT_TEXTUNDER = 9,
00077     CONTEXT_ICONSIZES = 50 // starting point for the icon size list, put everything else before
00078 };
00079 
00080 class KToolBarPrivate
00081 {
00082 public:
00083     KToolBarPrivate() {
00084         m_iconSize     = 0;
00085         m_iconText     = KToolBar::IconOnly;
00086         m_highlight    = true;
00087         m_transparent  = true;
00088         m_honorStyle   = false;
00089 
00090         m_enableContext  = true;
00091 
00092         m_xmlguiClient   = 0;
00093         m_configurePlugged = false;
00094 
00095         oldPos = Qt::DockUnmanaged;
00096 
00097         modified = m_isHorizontal = positioned = FALSE;
00098     }
00099 
00100     int m_iconSize;
00101     KToolBar::IconText m_iconText;
00102     bool m_highlight : 1;
00103     bool m_transparent : 1;
00104     bool m_honorStyle : 1;
00105     bool m_isHorizontal : 1;
00106     bool m_enableContext : 1;
00107     bool m_configurePlugged : 1;
00108     bool modified : 1;
00109     bool positioned : 1;
00110 
00111     QWidget *m_parent;
00112 
00113     QMainWindow::ToolBarDock oldPos;
00114 
00115     KXMLGUIClient *m_xmlguiClient;
00116 
00117     struct ToolBarInfo
00118     {
00119         ToolBarInfo() : index( 0 ), offset( -1 ), newline( FALSE ), dock( Qt::DockTop ) {}
00120         ToolBarInfo( Qt::Dock d, int i, bool n, int o ) : index( i ), offset( o ), newline( n ), dock( d ) {}
00121         int index, offset;
00122         bool newline;
00123         Qt::Dock dock;
00124     };
00125 
00126     ToolBarInfo toolBarInfo;
00127     QValueList<int> iconSizes;
00128     QTimer repaintTimer;
00129 };
00130 
00131 KToolBarSeparator::KToolBarSeparator(Orientation o , bool l, QToolBar *parent,
00132                                      const char* name )
00133     :QFrame( parent, name ), line( l )
00134 {
00135     connect( parent, SIGNAL(orientationChanged(Orientation)),
00136              this, SLOT(setOrientation(Orientation)) );
00137     setOrientation( o );
00138     setBackgroundMode( parent->backgroundMode() );
00139     setBackgroundOrigin( ParentOrigin );
00140 }
00141 
00142 void KToolBarSeparator::setOrientation( Orientation o )
00143 {
00144     orient = o;
00145     if ( line ) {
00146         if ( orientation() == Vertical )
00147             setFrameStyle( HLine + Sunken );
00148         else
00149             setFrameStyle( VLine + Sunken );
00150     } else {
00151             setFrameStyle( NoFrame );
00152     }
00153 }
00154 
00155 void KToolBarSeparator::styleChange( QStyle& )
00156 {
00157     setOrientation( orient );
00158 }
00159 
00160 QSize KToolBarSeparator::sizeHint() const
00161 {
00162     return orientation() == Vertical ? QSize( 0, 6 ) : QSize( 6, 0 );
00163 }
00164 
00165 QSizePolicy KToolBarSeparator::sizePolicy() const
00166 {
00167     return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
00168 }
00169 
00170 KToolBar::KToolBar( QWidget *parent, const char *name, bool honorStyle, bool readConfig )
00171     : QToolBar( QString::fromLatin1( name ),
00172       parent && parent->inherits( "QMainWindow" ) ? (QMainWindow*)parent : 0,
00173       parent, FALSE,
00174       name ? name : "mainToolBar")
00175 {
00176     init( readConfig, honorStyle );
00177 }
00178 
00179 KToolBar::KToolBar( QMainWindow *parentWindow, QMainWindow::ToolBarDock dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
00180     : QToolBar( QString::fromLatin1( name ),
00181       parentWindow, dock, newLine,
00182       name ? name : "mainToolBar")
00183 {
00184     init( readConfig, honorStyle );
00185 }
00186 
00187 KToolBar::KToolBar( QMainWindow *parentWindow, QWidget *dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
00188     : QToolBar( QString::fromLatin1( name ),
00189       parentWindow, dock, newLine,
00190       name ? name : "mainToolBar")
00191 {
00192     init( readConfig, honorStyle );
00193 }
00194 
00195 KToolBar::~KToolBar()
00196 {
00197     emit toolbarDestroyed();
00198     delete d;
00199 }
00200 
00201 void KToolBar::init( bool readConfig, bool honorStyle )
00202 {
00203     d = new KToolBarPrivate;
00204     setFullSize( TRUE );
00205     d->m_honorStyle = honorStyle;
00206     context = 0;
00207     layoutTimer = new QTimer( this );
00208     connect( layoutTimer, SIGNAL( timeout() ),
00209              this, SLOT( rebuildLayout() ) );
00210     connect( &(d->repaintTimer), SIGNAL( timeout() ),
00211              this, SLOT( slotRepaint() ) );
00212 
00213     if ( kapp ) { // may be null when started inside designer
00214         connect(kapp, SIGNAL(toolbarAppearanceChanged(int)), this, SLOT(slotAppearanceChanged()));
00215         // request notification of changes in icon style
00216         kapp->addKipcEventMask(KIPC::IconChanged);
00217         connect(kapp, SIGNAL(iconChanged(int)), this, SLOT(slotIconChanged(int)));
00218     }
00219 
00220     // finally, read in our configurable settings
00221     if ( readConfig )
00222         slotReadConfig();
00223 
00224     if ( mainWindow() )
00225         connect( mainWindow(), SIGNAL( toolBarPositionChanged( QToolBar * ) ),
00226                  this, SLOT( toolBarPosChanged( QToolBar * ) ) );
00227 }
00228 
00229 int KToolBar::insertButton(const QString& icon, int id, bool enabled,
00230                             const QString& text, int index, KInstance *_instance )
00231 {
00232     KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text, _instance );
00233 
00234     insertWidgetInternal( button, index, id );
00235     button->setEnabled( enabled );
00236     doConnections( button );
00237     return index;
00238 }
00239 
00240 
00241 int KToolBar::insertButton(const QString& icon, int id, const char *signal,
00242                             const QObject *receiver, const char *slot,
00243                             bool enabled, const QString& text, int index, KInstance *_instance )
00244 {
00245     KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text, _instance);
00246     insertWidgetInternal( button, index, id );
00247     button->setEnabled( enabled );
00248     connect( button, signal, receiver, slot );
00249     doConnections( button );
00250     return index;
00251 }
00252 
00253 
00254 int KToolBar::insertButton(const QPixmap& pixmap, int id, bool enabled,
00255                             const QString& text, int index )
00256 {
00257     KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text);
00258     insertWidgetInternal( button, index, id );
00259     button->setEnabled( enabled );
00260     doConnections( button );
00261     return index;
00262 }
00263 
00264 
00265 int KToolBar::insertButton(const QPixmap& pixmap, int id, const char *signal,
00266                             const QObject *receiver, const char *slot,
00267                             bool enabled, const QString& text,
00268                             int index )
00269 {
00270     KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text);
00271     insertWidgetInternal( button, index, id );
00272     button->setEnabled( enabled );
00273     connect( button, signal, receiver, slot );
00274     doConnections( button );
00275     return index;
00276 }
00277 
00278 
00279 int KToolBar::insertButton(const QString& icon, int id, QPopupMenu *popup,
00280                             bool enabled, const QString &text, int index )
00281 {
00282     KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text );
00283     insertWidgetInternal( button, index, id );
00284     button->setEnabled( enabled );
00285     button->setPopup( popup );
00286     doConnections( button );
00287     return index;
00288 }
00289 
00290 
00291 int KToolBar::insertButton(const QPixmap& pixmap, int id, QPopupMenu *popup,
00292                             bool enabled, 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     button->setPopup( popup );
00298     doConnections( button );
00299     return index;
00300 }
00301 
00302 
00303 int KToolBar::insertLined (const QString& text, int id,
00304                             const char *signal,
00305                             const QObject *receiver, const char *slot,
00306                             bool enabled ,
00307                             const QString& toolTipText,
00308                             int size, int index )
00309 {
00310     KLineEdit *lined = new KLineEdit ( this, 0 );
00311     if ( !toolTipText.isEmpty() )
00312         QToolTip::add( lined, toolTipText );
00313     if ( size > 0 )
00314         lined->setMinimumWidth( size );
00315     insertWidgetInternal( lined, index, id );
00316     connect( lined, signal, receiver, slot );
00317     lined->setText(text);
00318     lined->setEnabled( enabled );
00319     return index;
00320 }
00321 
00322 int KToolBar::insertCombo (const QStringList &list, int id, bool writable,
00323                             const char *signal, const QObject *receiver,
00324                             const char *slot, bool enabled,
00325                             const QString& tooltiptext,
00326                             int size, int index,
00327                             QComboBox::Policy policy )
00328 {
00329     KComboBox *combo = new KComboBox ( writable, this );
00330 
00331     insertWidgetInternal( combo, index, id );
00332     combo->insertStringList (list);
00333     combo->setInsertionPolicy(policy);
00334     combo->setEnabled( enabled );
00335     if ( !tooltiptext.isEmpty() )
00336         QToolTip::add( combo, tooltiptext );
00337     if ( size > 0 )
00338         combo->setMinimumWidth( size );
00339     if (!tooltiptext.isNull())
00340         QToolTip::add( combo, tooltiptext );
00341 
00342     if ( signal && receiver && slot )
00343         connect ( combo, signal, receiver, slot );
00344     return index;
00345 }
00346 
00347 
00348 int KToolBar::insertCombo (const QString& text, int id, bool writable,
00349                             const char *signal, QObject *receiver,
00350                             const char *slot, bool enabled,
00351                             const QString& tooltiptext,
00352                             int size, int index,
00353                             QComboBox::Policy policy )
00354 {
00355     KComboBox *combo = new KComboBox ( writable, this );
00356     insertWidgetInternal( combo, index, id );
00357     combo->insertItem (text);
00358     combo->setInsertionPolicy(policy);
00359     combo->setEnabled( enabled );
00360     if ( !tooltiptext.isEmpty() )
00361         QToolTip::add( combo, tooltiptext );
00362     if ( size > 0 )
00363         combo->setMinimumWidth( size );
00364     if (!tooltiptext.isNull())
00365         QToolTip::add( combo, tooltiptext );
00366     connect (combo, signal, receiver, slot);
00367     return index;
00368 }
00369 
00370 int KToolBar::insertSeparator(int index, int id)
00371 {
00372     QWidget *w = new KToolBarSeparator( orientation(), FALSE, this, "tool bar separator" );
00373     insertWidgetInternal( w, index, id );
00374     return index;
00375 }
00376 
00377 int KToolBar::insertLineSeparator(int index, int id)
00378 {
00379     QWidget *w = new KToolBarSeparator( orientation(), TRUE, this, "tool bar separator" );
00380     insertWidgetInternal( w, index, id );
00381     return index;
00382 }
00383 
00384 
00385 int KToolBar::insertWidget(int id, int /*width*/, QWidget *widget, int index)
00386 {
00387     removeWidgetInternal( widget ); // in case we already have it ?
00388     insertWidgetInternal( widget, index, id );
00389     return index;
00390 }
00391 
00392 int KToolBar::insertAnimatedWidget(int id, QObject *receiver, const char *slot,
00393                                     const QString& icons, int index )
00394 {
00395     KAnimWidget *anim = new KAnimWidget( icons, d->m_iconSize, this );
00396     insertWidgetInternal( anim, index, id );
00397 
00398     if ( receiver )
00399         connect( anim, SIGNAL(clicked()), receiver, slot);
00400 
00401     return index;
00402 }
00403 
00404 KAnimWidget *KToolBar::animatedWidget( int id )
00405 {
00406     Id2WidgetMap::Iterator it = id2widget.find( id );
00407     if ( it == id2widget.end() )
00408         return 0;
00409     if ( (*it) && (*it)->inherits( "KAnimWidget" ) )
00410         return (KAnimWidget*)(*it);
00411     QObjectList *l = queryList( "KAnimWidget" );
00412     if ( !l || !l->first() ) {
00413         delete l;
00414         return 0;
00415     }
00416 
00417     for ( QObject *o = l->first(); o; o = l->next() ) {
00418         if ( o->inherits( "KAnimWidget" ) )
00419         {
00420             delete l;
00421             return (KAnimWidget*)o;
00422         }
00423     }
00424 
00425     delete l;
00426     return 0;
00427 }
00428 
00429 
00430 void KToolBar::addConnection (int id, const char *signal,
00431                                const QObject *receiver, const char *slot)
00432 {
00433     Id2WidgetMap::Iterator it = id2widget.find( id );
00434     if ( it == id2widget.end() )
00435         return;
00436     if ( (*it) )
00437         connect( (*it), signal, receiver, slot );
00438 }
00439 
00440 void KToolBar::setItemEnabled( int id, bool enabled )
00441 {
00442     Id2WidgetMap::Iterator it = id2widget.find( id );
00443     if ( it == id2widget.end() )
00444         return;
00445     if ( (*it) )
00446         (*it)->setEnabled( enabled );
00447 }
00448 
00449 
00450 void KToolBar::setButtonPixmap( int id, const QPixmap& _pixmap )
00451 {
00452     Id2WidgetMap::Iterator it = id2widget.find( id );
00453     if ( it == id2widget.end() )
00454         return;
00455     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00456     if ( button )
00457         button->setPixmap( _pixmap );
00458 }
00459 
00460 
00461 void KToolBar::setButtonIcon( int id, const QString& _icon )
00462 {
00463     Id2WidgetMap::Iterator it = id2widget.find( id );
00464     if ( it == id2widget.end() )
00465         return;
00466     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00467     if ( button )
00468         button->setIcon( _icon );
00469 }
00470 
00471 void KToolBar::setButtonIconSet( int id, const QIconSet& iconset )
00472 {
00473     Id2WidgetMap::Iterator it = id2widget.find( id );
00474     if ( it == id2widget.end() )
00475         return;
00476     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00477     if ( button )
00478         button->setIconSet( iconset );
00479 }
00480 
00481 
00482 void KToolBar::setDelayedPopup (int id , QPopupMenu *_popup, bool toggle )
00483 {
00484     Id2WidgetMap::Iterator it = id2widget.find( id );
00485     if ( it == id2widget.end() )
00486         return;
00487     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00488     if ( button )
00489         button->setDelayedPopup( _popup, toggle );
00490 }
00491 
00492 
00493 void KToolBar::setAutoRepeat (int id, bool flag)
00494 {
00495     Id2WidgetMap::Iterator it = id2widget.find( id );
00496     if ( it == id2widget.end() )
00497         return;
00498     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00499     if ( button )
00500         button->setAutoRepeat( flag );
00501 }
00502 
00503 
00504 void KToolBar::setToggle (int id, bool flag )
00505 {
00506     Id2WidgetMap::Iterator it = id2widget.find( id );
00507     if ( it == id2widget.end() )
00508         return;
00509     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00510     if ( button )
00511         button->setToggle( flag );
00512 }
00513 
00514 
00515 void KToolBar::toggleButton (int id)
00516 {
00517     Id2WidgetMap::Iterator it = id2widget.find( id );
00518     if ( it == id2widget.end() )
00519         return;
00520     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00521     if ( button )
00522         button->toggle();
00523 }
00524 
00525 
00526 void KToolBar::setButton (int id, bool flag)
00527 {
00528     Id2WidgetMap::Iterator it = id2widget.find( id );
00529     if ( it == id2widget.end() )
00530         return;
00531     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00532     if ( button )
00533         button->on( flag );
00534 }
00535 
00536 
00537 bool KToolBar::isButtonOn (int id) const
00538 {
00539     Id2WidgetMap::ConstIterator it = id2widget.find( id );
00540     if ( it == id2widget.end() )
00541         return false;
00542     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00543     return button ? button->isOn() : false;
00544 }
00545 
00546 
00547 void KToolBar::setLinedText (int id, const QString& text)
00548 {
00549     Id2WidgetMap::Iterator it = id2widget.find( id );
00550     if ( it == id2widget.end() )
00551         return;
00552     QLineEdit * lineEdit = dynamic_cast<QLineEdit *>( *it );
00553     if ( lineEdit )
00554         lineEdit->setText( text );
00555 }
00556 
00557 
00558 QString KToolBar::getLinedText (int id) const
00559 {
00560     Id2WidgetMap::ConstIterator it = id2widget.find( id );
00561     if ( it == id2widget.end() )
00562         return QString::null;
00563     QLineEdit * lineEdit = dynamic_cast<QLineEdit *>( *it );
00564     return lineEdit ? lineEdit->text() : QString::null;
00565 }
00566 
00567 
00568 void KToolBar::insertComboItem (int id, const QString& text, int index)
00569 {
00570     Id2WidgetMap::Iterator it = id2widget.find( id );
00571     if ( it == id2widget.end() )
00572         return;
00573     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00574     if (comboBox)
00575         comboBox->insertItem( text, index );
00576 }
00577 
00578 void KToolBar::insertComboList (int id, const QStringList &list, int index)
00579 {
00580     Id2WidgetMap::Iterator it = id2widget.find( id );
00581     if ( it == id2widget.end() )
00582         return;
00583     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00584     if (comboBox)
00585         comboBox->insertStringList( list, index );
00586 }
00587 
00588 
00589 void KToolBar::removeComboItem (int id, int index)
00590 {
00591     Id2WidgetMap::Iterator it = id2widget.find( id );
00592     if ( it == id2widget.end() )
00593         return;
00594     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00595     if (comboBox)
00596         comboBox->removeItem( index );
00597 }
00598 
00599 
00600 void KToolBar::setCurrentComboItem (int id, int index)
00601 {
00602     Id2WidgetMap::Iterator it = id2widget.find( id );
00603     if ( it == id2widget.end() )
00604         return;
00605     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00606     if (comboBox)
00607         comboBox->setCurrentItem( index );
00608 }
00609 
00610 
00611 void KToolBar::changeComboItem  (int id, const QString& text, int index)
00612 {
00613     Id2WidgetMap::Iterator it = id2widget.find( id );
00614     if ( it == id2widget.end() )
00615         return;
00616     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00617     if (comboBox)
00618         comboBox->changeItem( text, index );
00619 }
00620 
00621 
00622 void KToolBar::clearCombo (int id)
00623 {
00624     Id2WidgetMap::Iterator it = id2widget.find( id );
00625     if ( it == id2widget.end() )
00626         return;
00627     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00628     if (comboBox)
00629         comboBox->clear();
00630 }
00631 
00632 
00633 QString KToolBar::getComboItem (int id, int index) const
00634 {
00635     Id2WidgetMap::ConstIterator it = id2widget.find( id );
00636     if ( it == id2widget.end() )
00637         return QString::null;
00638     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00639     return comboBox ? comboBox->text( index ) : QString::null;
00640 }
00641 
00642 
00643 KComboBox * KToolBar::getCombo(int id)
00644 {
00645     Id2WidgetMap::Iterator it = id2widget.find( id );
00646     if ( it == id2widget.end() )
00647         return 0;
00648     return dynamic_cast<KComboBox *>( *it );
00649 }
00650 
00651 
00652 KLineEdit * KToolBar::getLined (int id)
00653 {
00654     Id2WidgetMap::Iterator it = id2widget.find( id );
00655     if ( it == id2widget.end() )
00656         return 0;
00657     return dynamic_cast<KLineEdit *>( *it );
00658 }
00659 
00660 
00661 KToolBarButton * KToolBar::getButton (int id)
00662 {
00663     Id2WidgetMap::Iterator it = id2widget.find( id );
00664     if ( it == id2widget.end() )
00665         return 0;
00666     return dynamic_cast<KToolBarButton *>( *it );
00667 }
00668 
00669 
00670 void KToolBar::alignItemRight (int id, bool right )
00671 {
00672     Id2WidgetMap::Iterator it = id2widget.find( id );
00673     if ( it == id2widget.end() )
00674         return;
00675     if ( rightAligned && !right && (*it) == rightAligned )
00676         rightAligned = 0;
00677     if ( (*it) && right )
00678         rightAligned = (*it);
00679 }
00680 
00681 
00682 QWidget *KToolBar::getWidget (int id)
00683 {
00684     Id2WidgetMap::Iterator it = id2widget.find( id );
00685     return ( it == id2widget.end() ) ? 0 : (*it);
00686 }
00687 
00688 
00689 void KToolBar::setItemAutoSized (int id, bool yes )
00690 {
00691     QWidget *w = getWidget(id);
00692     if ( w && yes )
00693         setStretchableWidget( w );
00694 }
00695 
00696 
00697 void KToolBar::clear ()
00698 {
00699     QToolBar::clear();
00700     widget2id.clear();
00701     id2widget.clear();
00702 }
00703 
00704 
00705 void KToolBar::removeItem (int id)
00706 {
00707     Id2WidgetMap::Iterator it = id2widget.find( id );
00708     if ( it == id2widget.end() )
00709     {
00710         kdDebug(220) << "KToolBar::removeItem item " << id << " not found" << endl;
00711         return;
00712     }
00713     QWidget * w = (*it);
00714     id2widget.remove( id );
00715     widget2id.remove( w );
00716     widgets.removeRef( w );
00717     delete w;
00718 }
00719 
00720 
00721 void KToolBar::hideItem (int id)
00722 {
00723     QWidget *w = getWidget(id);
00724     if ( w )
00725         w->hide();
00726 }
00727 
00728 
00729 void KToolBar::showItem (int id)
00730 {
00731     QWidget *w = getWidget(id);
00732     if ( w )
00733         w->show();
00734 }
00735 
00736 
00737 void KToolBar::setFullSize(bool flag )
00738 {
00739     setHorizontalStretchable( flag );
00740     setVerticalStretchable( flag );
00741 }
00742 
00743 
00744 bool KToolBar::fullSize() const
00745 {
00746     return isHorizontalStretchable() || isVerticalStretchable();
00747 }
00748 
00749 
00750 void KToolBar::enableMoving(bool flag )
00751 {
00752     setMovingEnabled(flag);
00753 }
00754 
00755 
00756 void KToolBar::setBarPos (BarPosition bpos)
00757 {
00758     if ( !mainWindow() )
00759         return;
00760     mainWindow()->moveDockWindow( this, (Dock)bpos );
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     }
00840 
00841     if (update == false)
00842         return;
00843 
00844     if (doUpdate)
00845         emit modechange(); // tell buttons what happened
00846 
00847     // ugly hack to force a QMainWindow::triggerLayout( TRUE )
00848     if ( mainWindow() ) {
00849         QMainWindow *mw = mainWindow();
00850         mw->setUpdatesEnabled( FALSE );
00851         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00852         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00853         mw->setUpdatesEnabled( TRUE );
00854     }
00855 }
00856 
00857 
00858 KToolBar::IconText KToolBar::iconText() const
00859 {
00860     return d->m_iconText;
00861 }
00862 
00863 
00864 void KToolBar::setIconSize(int size)
00865 {
00866     setIconSize( size, true );
00867 }
00868 
00869 void KToolBar::setIconSize(int size, bool update)
00870 {
00871     bool doUpdate=false;
00872 
00873     if ( size != d->m_iconSize ) {
00874             d->m_iconSize = size;
00875             doUpdate=true;
00876     }
00877 
00878     if (update == false)
00879         return;
00880 
00881     if (doUpdate)
00882         emit modechange(); // tell buttons what happened
00883 
00884     // ugly hack to force a QMainWindow::triggerLayout( TRUE )
00885     if ( mainWindow() ) {
00886         QMainWindow *mw = mainWindow();
00887         mw->setUpdatesEnabled( FALSE );
00888         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00889         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00890         mw->setUpdatesEnabled( TRUE );
00891     }
00892 }
00893 
00894 
00895 int KToolBar::iconSize() const
00896 {
00897     if ( !d->m_iconSize ) // default value?
00898     {
00899          if (!::qstrcmp(QObject::name(), "mainToolBar"))
00900              return KGlobal::iconLoader()->currentSize(KIcon::MainToolbar);
00901          else
00902              return KGlobal::iconLoader()->currentSize(KIcon::Toolbar);
00903     }
00904     return d->m_iconSize;
00905 }
00906 
00907 
00908 void KToolBar::setEnableContextMenu(bool enable )
00909 {
00910     d->m_enableContext = enable;
00911 }
00912 
00913 
00914 bool KToolBar::contextMenuEnabled() const
00915 {
00916     return d->m_enableContext;
00917 }
00918 
00919 
00920 void KToolBar::setItemNoStyle(int id, bool no_style )
00921 {
00922     Id2WidgetMap::Iterator it = id2widget.find( id );
00923     if ( it == id2widget.end() )
00924         return;
00925     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00926     if (button)
00927         button->setNoStyle( no_style );
00928 }
00929 
00930 
00931 void KToolBar::setFlat (bool flag)
00932 {
00933     if ( !mainWindow() )
00934         return;
00935     if ( flag )
00936         mainWindow()->moveDockWindow( this, DockMinimized );
00937     else
00938         mainWindow()->moveDockWindow( this, DockTop );
00939     // And remember to save the new look later
00940     if ( mainWindow()->inherits( "KMainWindow" ) )
00941         static_cast<KMainWindow *>(mainWindow())->setSettingsDirty();
00942 }
00943 
00944 
00945 int KToolBar::count() const
00946 {
00947     return id2widget.count();
00948 }
00949 
00950 
00951 void KToolBar::saveState()
00952 {
00953     QString position, icontext, index, offset, newLine;
00954     getAttributes( position, icontext, index, offset, newLine );
00955 
00956     // first, try to save to the xml file
00957     if ( d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty() ) {
00958         // go down one level to get to the right tags
00959         QDomElement elem = d->m_xmlguiClient->domDocument().documentElement().toElement();
00960         elem = elem.firstChild().toElement();
00961         QString barname(!::qstrcmp(name(), "unnamed") ? "mainToolBar" : name());
00962         QDomElement current;
00963         // now try to find our toolbar
00964         d->modified = false;
00965         for( ; !elem.isNull(); elem = elem.nextSibling().toElement() ) {
00966             current = elem;
00967 
00968             if ( current.tagName().lower() != "toolbar" )
00969                 continue;
00970 
00971             QString curname(current.attribute( "name" ));
00972 
00973             if ( curname == barname ) {
00974                 saveState( current );
00975                 break;
00976             }
00977         }
00978         // if we didn't make changes, then just return
00979         if ( !d->modified )
00980             return;
00981 
00982         // now we load in the (non-merged) local file
00983         QString local_xml(KXMLGUIFactory::readConfigFile(d->m_xmlguiClient->xmlFile(), true, d->m_xmlguiClient->instance()));
00984         QDomDocument local;
00985         local.setContent(local_xml);
00986 
00987         // make sure we don't append if this toolbar already exists locally
00988         bool just_append = true;
00989         elem = local.documentElement().toElement();
00990         KXMLGUIFactory::removeDOMComments( elem );
00991         elem = elem.firstChild().toElement();
00992         for( ; !elem.isNull(); elem = elem.nextSibling().toElement() ) {
00993             if ( elem.tagName().lower() != "toolbar" )
00994                 continue;
00995 
00996             QString curname(elem.attribute( "name" ));
00997 
00998             if ( curname == barname ) {
00999                 just_append = false;
01000                 local.documentElement().replaceChild( current, elem );
01001                 break;
01002             }
01003         }
01004 
01005         if (just_append)
01006             local.documentElement().appendChild( current );
01007 
01008         KXMLGUIFactory::saveConfigFile(local, d->m_xmlguiClient->localXMLFile(), d->m_xmlguiClient->instance() );
01009 
01010         return;
01011     }
01012 
01013     // if that didn't work, we save to the config file
01014     KConfig *config = KGlobal::config();
01015     saveSettings(config, QString::null);
01016     config->sync();
01017 }
01018 
01019 QString KToolBar::settingsGroup() const
01020 {
01021     QString configGroup;
01022     if (!::qstrcmp(name(), "unnamed") || !::qstrcmp(name(), "mainToolBar"))
01023         configGroup = "Toolbar style";
01024     else
01025         configGroup = QString(name()) + " Toolbar style";
01026     if ( this->mainWindow() )
01027     {
01028         configGroup.prepend(" ");
01029         configGroup.prepend( this->mainWindow()->name() );
01030     }
01031     return configGroup;
01032 }
01033 
01034 void KToolBar::saveSettings(KConfig *config, const QString &_configGroup)
01035 {
01036     QString configGroup = _configGroup;
01037     if (configGroup.isEmpty())
01038         configGroup = settingsGroup();
01039     //kdDebug(220) << "KToolBar::saveSettings group=" << _configGroup << " -> " << configGroup << endl;
01040 
01041     QString position, icontext, index, offset, newLine;
01042     getAttributes( position, icontext, index, offset, newLine );
01043 
01044     //kdDebug(220) << "KToolBar::saveSettings " << name() << " newLine=" << newLine << endl;
01045 
01046     KConfigGroupSaver saver(config, configGroup);
01047 
01048     config->writeEntry("Position", position);
01049     config->writeEntry("IconText", icontext);
01050     config->writeEntry("IconSize", iconSize());
01051     config->writeEntry("Hidden", isHidden());
01052 
01053     if ( !index.isEmpty() )
01054         config->writeEntry( "Index", index );
01055     if ( !offset.isEmpty() )
01056         config->writeEntry( "Offset", offset );
01057     if ( !newLine.isEmpty() )
01058         config->writeEntry( "NewLine", newLine );
01059 }
01060 
01061 
01062 void KToolBar::setXMLGUIClient( KXMLGUIClient *client )
01063 {
01064     d->m_xmlguiClient = client;
01065 }
01066 
01067 void KToolBar::setText( const QString & txt )
01068 {
01069     setLabel( txt );
01070 }
01071 
01072 
01073 QString KToolBar::text() const
01074 {
01075     return label();
01076 }
01077 
01078 
01079 void KToolBar::doConnections( KToolBarButton *button )
01080 {
01081     connect(button, SIGNAL(clicked(int)), this, SIGNAL( clicked( int ) ) );
01082     connect(button, SIGNAL(doubleClicked(int)), this, SIGNAL( doubleClicked( int ) ) );
01083     connect(button, SIGNAL(released(int)), this, SIGNAL( released( int ) ) );
01084     connect(button, SIGNAL(pressed(int)), this, SIGNAL( pressed( int ) ) );
01085     connect(button, SIGNAL(toggled(int)), this, SIGNAL( toggled( int ) ) );
01086     connect(button, SIGNAL(highlighted(int, bool)), this, SIGNAL( highlighted( int, bool ) ) );
01087 }
01088 
01089 void KToolBar::mousePressEvent ( QMouseEvent *m )
01090 {
01091     if ( !mainWindow() )
01092         return;
01093     QMainWindow *mw = mainWindow();
01094     if ( mw->toolBarsMovable() && d->m_enableContext ) {
01095         if ( m->button() == RightButton ) {
01096             int i = contextMenu()->exec( m->globalPos(), 0 );
01097             switch ( i ) {
01098             case -1:
01099                 return; // popup cancelled
01100             case CONTEXT_LEFT:
01101                 mw->moveDockWindow( this, DockLeft );
01102                 break;
01103             case CONTEXT_RIGHT:
01104                 mw->moveDockWindow( this, DockRight );
01105                 break;
01106             case CONTEXT_TOP:
01107                 mw->moveDockWindow( this, DockTop );
01108                 break;
01109             case CONTEXT_BOTTOM:
01110                 mw->moveDockWindow( this, DockBottom );
01111                 break;
01112             case CONTEXT_FLOAT:
01113                 break;
01114             case CONTEXT_FLAT:
01115                 mw->moveDockWindow( this, DockMinimized );
01116                 break;
01117             case CONTEXT_ICONS:
01118                 setIconText( IconOnly );
01119                 break;
01120             case CONTEXT_TEXTRIGHT:
01121                 setIconText( IconTextRight );
01122                 break;
01123             case CONTEXT_TEXT:
01124                 setIconText( TextOnly );
01125                 break;
01126             case CONTEXT_TEXTUNDER:
01127                 setIconText( IconTextBottom );
01128                 break;
01129             default:
01130                 if ( i >= CONTEXT_ICONSIZES )
01131                     setIconSize( i - CONTEXT_ICONSIZES );
01132                 else
01133                     return; // assume this was an action handled elsewhere, no need for setSettingsDirty()
01134             }
01135             if ( mw->inherits("KMainWindow") )
01136                 static_cast<KMainWindow *>(mw)->setSettingsDirty();
01137         }
01138     }
01139 }
01140 
01141 
01142 void KToolBar::rebuildLayout()
01143 {
01144     layoutTimer->stop();
01145     QApplication::sendPostedEvents( this, QEvent::ChildInserted );
01146     QBoxLayout *l = boxLayout();
01147 
01148     // clear the old layout
01149     QLayoutIterator it = l->iterator();
01150     while ( it.current() )
01151         it.deleteCurrent();
01152 
01153     for ( QWidget *w = widgets.first(); w; w = widgets.next() ) {
01154         if ( w == rightAligned )
01155             continue;
01156         if ( w->inherits( "KToolBarSeparator" ) &&
01157              !( (KToolBarSeparator*)w )->showLine() ) {
01158             l->addSpacing( 6 );
01159             w->hide();
01160             continue;
01161         }
01162         if ( w->inherits( "QPopupMenu" ) )
01163             continue;
01164         l->addWidget( w );
01165         w->show();
01166     }
01167     if ( rightAligned ) {
01168         l->addStretch();
01169         l->addWidget( rightAligned );
01170         rightAligned->show();
01171     }
01172 
01173     if ( fullSize() ) {
01174         // This code sucks. It makes the last combo in a toolbar VERY big (e.g. zoom combo in kword).
01175         //if ( !stretchableWidget && widgets.last() &&
01176         //     !widgets.last()->inherits( "QButton" ) && !widgets.last()->inherits( "KAnimWidget" ) )
01177         //    setStretchableWidget( widgets.last() );
01178         if ( !rightAligned )
01179             l->addStretch();
01180         if ( stretchableWidget )
01181             l->setStretchFactor( stretchableWidget, 10 );
01182     }
01183     l->invalidate();
01184     QApplication::postEvent( this, new QEvent( QEvent::LayoutHint ) );
01185 }
01186 
01187 void KToolBar::childEvent( QChildEvent *e )
01188 {
01189     if ( e->child()->isWidgetType() ) {
01190         QWidget * w = (QWidget*)e->child();
01191         if ( e->type() == QEvent::ChildInserted ) {
01192             if ( !e->child()->inherits( "QPopupMenu" ) &&
01193                  ::qstrcmp( "qt_dockwidget_internal", e->child()->name() ) != 0 ) {
01194 
01195                 // prevent items that have been explicitly inserted by insert*() from
01196                 // being inserted again
01197                 if ( !widget2id.contains( w ) )
01198                 {
01199                     int dummy = -1;
01200                     insertWidgetInternal( w, dummy, -1 );
01201                 }
01202             }
01203         } else {
01204             removeWidgetInternal( w );
01205         }
01206         if ( isVisibleTo( 0 ) )
01207             layoutTimer->start( 50, TRUE );
01208     }
01209     QToolBar::childEvent( e );
01210 }
01211 
01212 void KToolBar::insertWidgetInternal( QWidget *w, int &index, int id )
01213 {
01214     // we can't have it in widgets, or something is really wrong
01215     //widgets.removeRef( w );
01216 
01217     connect( w, SIGNAL( destroyed() ),
01218              this, SLOT( widgetDestroyed() ) );
01219     if ( index == -1 || index > (int)widgets.count() ) {
01220         widgets.append( w );
01221         index = (int)widgets.count();
01222     }
01223     else
01224         widgets.insert( index, w );
01225     if ( id == -1 )
01226         id = id2widget.count();
01227     id2widget.insert( id, w );
01228     widget2id.insert( w, id );
01229 }
01230 
01231 void KToolBar::showEvent( QShowEvent *e )
01232 {
01233     QToolBar::showEvent( e );
01234     rebuildLayout();
01235 }
01236 
01237 void KToolBar::setStretchableWidget( QWidget *w )
01238 {
01239     QToolBar::setStretchableWidget( w );
01240     stretchableWidget = w;
01241 }
01242 
01243 QSizePolicy KToolBar::sizePolicy() const
01244 {
01245     if ( orientation() == Horizontal )
01246         return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
01247     else
01248         return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
01249 }
01250 
01251 QSize KToolBar::sizeHint() const
01252 {
01253     QSize minSize(0,0);
01254     KToolBar *this_too = (KToolBar *)this;
01255 
01256     this_too->polish();
01257 
01258     int margin = ((QWidget *)this)->layout()->margin();
01259     switch( barPos() )
01260     {
01261      case KToolBar::Top:
01262      case KToolBar::Bottom:
01263        for ( QWidget *w = this_too->widgets.first(); w; w = this_too->widgets.next() )
01264        {
01265           if ( w->inherits( "KToolBarSeparator" ) &&
01266              !( (KToolBarSeparator*)w )->showLine() )
01267           {
01268              minSize += QSize(6, 0);
01269           }
01270           else
01271           {
01272              QSize sh = w->sizeHint();
01273              if (!sh.isValid())
01274                 sh = w->minimumSize();
01275              minSize = minSize.expandedTo(QSize(0, sh.height()));
01276              minSize += QSize(sh.width()+1, 0);
01277           }
01278        }
01279        minSize += QSize(QApplication::style().pixelMetric( QStyle::PM_DockWindowHandleExtent ), 0);
01280        minSize += QSize(margin*2, margin*2);
01281        break;
01282 
01283      case KToolBar::Left:
01284      case KToolBar::Right:
01285        for ( QWidget *w = this_too->widgets.first(); w; w = this_too->widgets.next() )
01286        {
01287           if ( w->inherits( "KToolBarSeparator" ) &&
01288              !( (KToolBarSeparator*)w )->showLine() )
01289           {
01290              minSize += QSize(0, 6);
01291           }
01292           else
01293           {
01294              QSize sh = w->sizeHint();
01295              if (!sh.isValid())
01296                 sh = w->minimumSize();
01297              minSize = minSize.expandedTo(QSize(sh.width(), 0));
01298              minSize += QSize(0, sh.height()+1);
01299           }
01300        }
01301        minSize += QSize(0, QApplication::style().pixelMetric( QStyle::PM_DockWindowHandleExtent ));
01302        minSize += QSize(margin*2, margin*2);
01303        break;
01304 
01305      default:
01306        minSize = QToolBar::sizeHint();
01307        break;
01308     }
01309     return minSize;
01310 }
01311 
01312 QSize KToolBar::minimumSize() const
01313 {
01314     return minimumSizeHint();
01315 }
01316 
01317 QSize KToolBar::minimumSizeHint() const
01318 {
01319     return sizeHint();
01320 }
01321 
01322 bool KToolBar::highlight() const
01323 {
01324     return d->m_highlight;
01325 }
01326 
01327 void KToolBar::hide()
01328 {
01329     QToolBar::hide();
01330 }
01331 
01332 void KToolBar::show()
01333 {
01334     QToolBar::show();
01335 }
01336 
01337 void KToolBar::resizeEvent( QResizeEvent *e )
01338 {
01339     bool b = isUpdatesEnabled();
01340     setUpdatesEnabled( FALSE );
01341     QToolBar::resizeEvent( e );
01342     if (b)
01343        d->repaintTimer.start( 100, true );
01344 }
01345 
01346 void KToolBar::slotIconChanged(int group)
01347 {
01348     if ((group != KIcon::Toolbar) && (group != KIcon::MainToolbar))
01349         return;
01350     if ((group == KIcon::MainToolbar) != !::qstrcmp(name(), "mainToolBar"))
01351         return;
01352 
01353     emit modechange();
01354     if (isVisible())
01355         updateGeometry();
01356 }
01357 
01358 void KToolBar::slotReadConfig()
01359 {
01360     //kdDebug(220) << "KToolBar::slotReadConfig" << endl;
01361     // Read appearance settings (hmm, we used to do both here,
01362     // but a well behaved application will call applyMainWindowSettings
01363     // anyway, right ?)
01364     applyAppearanceSettings(KGlobal::config(), QString::null );
01365 }
01366 
01367 void KToolBar::slotAppearanceChanged()
01368 {
01369     // Read appearance settings from global file.
01370     applyAppearanceSettings(KGlobal::config(), QString::null, true /* lose local settings */ );
01371     // And remember to save the new look later
01372     if ( mainWindow() && mainWindow()->inherits( "KMainWindow" ) )
01373         static_cast<KMainWindow *>(mainWindow())->setSettingsDirty();
01374 }
01375 
01376 //static
01377 bool KToolBar::highlightSetting()
01378 {
01379     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01380     KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01381     return KGlobal::config()->readBoolEntry(QString::fromLatin1("Highlighting"),true);
01382 }
01383 
01384 //static
01385 bool KToolBar::transparentSetting()
01386 {
01387     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01388     KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01389     return KGlobal::config()->readBoolEntry(QString::fromLatin1("TransparentMoving"),true);
01390 }
01391 
01392 //static
01393 KToolBar::IconText KToolBar::iconTextSetting()
01394 {
01395     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01396     KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01397     QString icontext = KGlobal::config()->readEntry(QString::fromLatin1("IconText"),QString::fromLatin1("IconOnly"));
01398     if ( icontext == "IconTextRight" )
01399         return IconTextRight;
01400     else if ( icontext == "IconTextBottom" )
01401         return IconTextBottom;
01402     else if ( icontext == "TextOnly" )
01403         return TextOnly;
01404     else
01405         return IconOnly;
01406 }
01407 
01408 void KToolBar::applyAppearanceSettings(KConfig *config, const QString &_configGroup, bool forceGlobal)
01409 {
01410     QString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
01411     //kdDebug(220) << "KToolBar::applyAppearanceSettings " << configGroup << endl;
01412     // We have application-specific settings in the XML file,
01413     // and nothing in the application's config file
01414     // -> don't apply the global defaults, the XML ones are preferred
01415     // See applySettings for a full explanation
01416     if ( d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty() &&
01417            !config->hasGroup(configGroup) )
01418         return;
01419 
01420     KConfig *gconfig = KGlobal::config();
01421 
01422     static const QString &attrIconText  = KGlobal::staticQString("IconText");
01423     static const QString &attrHighlight = KGlobal::staticQString("Highlighting");
01424     static const QString &attrTrans     = KGlobal::staticQString("TransparentMoving");
01425     static const QString &attrSize      = KGlobal::staticQString("IconSize");
01426 
01427     // we actually do this in two steps.
01428     // First, we read in the global styles [Toolbar style] (from the KControl module).
01429     // Then, if the toolbar is NOT 'mainToolBar', we will also try to read in [barname Toolbar style]
01430     bool highlight;
01431     int transparent;
01432     QString icontext;
01433     int iconsize = 0;
01434 
01435     // this is the first iteration
01436     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01437     { // start block for KConfigGroupSaver
01438         KConfigGroupSaver saver(gconfig, grpToolbar);
01439 
01440         // first, get the generic settings
01441         highlight   = gconfig->readBoolEntry(attrHighlight, true);
01442         transparent = gconfig->readBoolEntry(attrTrans, true);
01443 
01444         // we read in the IconText property *only* if we intend on actually
01445         // honoring it
01446         if (d->m_honorStyle)
01447             icontext = gconfig->readEntry(attrIconText, "IconOnly");
01448         else
01449             icontext = "IconOnly";
01450 
01451         // Use the default icon size for toolbar icons.
01452         iconsize = gconfig->readNumEntry(attrSize, 0);
01453 
01454         if ( !forceGlobal && config->hasGroup(configGroup) )
01455         {
01456             config->setGroup(configGroup);
01457 
01458             // first, get the generic settings
01459             highlight   = config->readBoolEntry(attrHighlight, highlight);
01460             transparent = config->readBoolEntry(attrTrans, transparent);
01461             // now we always read in the IconText property
01462             icontext = config->readEntry(attrIconText, icontext);
01463 
01464             // now get the size
01465             iconsize = config->readNumEntry(attrSize, iconsize);
01466         }
01467         // revert back to the old group
01468     } // end block for KConfigGroupSaver
01469 
01470     bool doUpdate = false;
01471 
01472     IconText icon_text;
01473     if ( icontext == "IconTextRight" )
01474         icon_text = IconTextRight;
01475     else if ( icontext == "IconTextBottom" )
01476         icon_text = IconTextBottom;
01477     else if ( icontext == "TextOnly" )
01478         icon_text = TextOnly;
01479     else
01480         icon_text = IconOnly;
01481 
01482     // check if the icon/text has changed
01483     if (icon_text != d->m_iconText) {
01484         //kdDebug(220) << "KToolBar::applyAppearanceSettings setIconText" << icontext << endl;
01485         setIconText(icon_text, false);
01486         doUpdate = true;
01487     }
01488 
01489     // ...and check if the icon size has changed
01490     if (iconsize != d->m_iconSize) {
01491         setIconSize(iconsize, false);
01492         doUpdate = true;
01493     }
01494 
01495     QMainWindow *mw = mainWindow();
01496 
01497     // ...and if we should highlight
01498     if ( highlight != d->m_highlight ) {
01499         d->m_highlight = highlight;
01500         doUpdate = true;
01501     }
01502 
01503     // ...and if we should move transparently
01504     if ( mw && transparent != (!mw->opaqueMoving()) ) {
01505         mw->setOpaqueMoving( !transparent );
01506     }
01507 
01508     if (doUpdate)
01509         emit modechange(); // tell buttons what happened
01510     if (isVisible ())
01511         updateGeometry();
01512 }
01513 
01514 void KToolBar::applySettings(KConfig *config, const QString &_configGroup)
01515 {
01516     //kdDebug(220) << "KToolBar::applySettings group=" << _configGroup << endl;
01517 
01518     QString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
01519 
01520     /*
01521       Let's explain this a bit more in details.
01522       The order in which we apply settings is :
01523        Global config / <appnamerc> user settings                        if no XMLGUI is used
01524        Global config / App-XML attributes / <appnamerc> user settings   if XMLGUI is used
01525 
01526       So in the first case, we simply read everything from KConfig as below,
01527       but in the second case we don't do anything here if there is no app-specific config,
01528       and the XMLGUI uses the static methods of this class to get the global defaults.
01529 
01530       Global config doesn't include position (index, offset, newline and hidden/shown).
01531     */
01532 
01533     // First the appearance stuff - the one which has a global config
01534     applyAppearanceSettings( config, _configGroup );
01535 
01536     // ...and now the position stuff
01537     if ( config->hasGroup(configGroup) )
01538     {
01539         KConfigGroupSaver cgs(config, configGroup);
01540 
01541         static const QString &attrPosition  = KGlobal::staticQString("Position");
01542         static const QString &attrIndex  = KGlobal::staticQString("Index");
01543         static const QString &attrOffset  = KGlobal::staticQString("Offset");
01544         static const QString &attrNewLine  = KGlobal::staticQString("NewLine");
01545         static const QString &attrHidden  = KGlobal::staticQString("Hidden");
01546 
01547         QString position = config->readEntry(attrPosition, "Top");
01548         int index = config->readNumEntry(attrIndex, 0 );
01549         int offset = config->readNumEntry(attrOffset, -1 );
01550         bool newLine = config->readEntry(attrNewLine).lower() == "true"; // someone used "TRUE" we can't use readBoolEntry :(
01551         bool hidden = config->readBoolEntry(attrHidden, false);
01552 
01553         Dock pos(DockTop);
01554         if ( position == "Top" )
01555             pos = DockTop;
01556         else if ( position == "Bottom" )
01557             pos = DockBottom;
01558         else if ( position == "Left" )
01559             pos = DockLeft;
01560         else if ( position == "Right" )
01561             pos = DockRight;
01562         else if ( position == "Floating" )
01563             pos = DockTornOff;
01564         else if ( position == "Flat" )
01565             pos = DockMinimized;
01566 
01567         //kdDebug(220) << "KToolBar::applySettings hidden=" << hidden << endl;
01568         if (hidden)
01569             hide();
01570         else
01571             show();
01572 
01573         if ( mainWindow() )
01574         {
01575             QMainWindow *mw = mainWindow();
01576 
01577             //kdDebug(220) << "KToolBar::applySettings updating ToolbarInfo" << endl;
01578             d->toolBarInfo = KToolBarPrivate::ToolBarInfo( pos, index, newLine, offset );
01579 
01580             // moveDockWindow calls QDockArea which does a reparent() on us with
01581             // showIt = true, so we loose our visibility status
01582             bool doHide = isHidden();
01583 
01584             mw->moveDockWindow( this, pos, newLine, index, offset );
01585             //kdDebug(220) << "KToolBar::applySettings " << name() << " moveDockWindow with pos=" << pos << " newLine=" << newLine << " idx=" << index << " offs=" << offset << endl;
01586             if ( doHide )
01587                 hide();
01588         }
01589         if (isVisible ())
01590             updateGeometry();
01591     }
01592 }
01593 
01594 bool KToolBar::event( QEvent *e )
01595 {
01596     if ( (e->type() == QEvent::LayoutHint) && isUpdatesEnabled() )
01597        d->repaintTimer.start( 100, true );
01598 
01599     if (e->type() == QEvent::ChildInserted )
01600     {
01601        // By pass QToolBar::event,
01602        // it will show() the inserted child and we don't want to
01603        // do that until we have rebuild the layout.
01604        childEvent((QChildEvent *)e);
01605        return true;
01606     }
01607 
01608     return QToolBar::event( e );
01609 }
01610 
01611 void KToolBar::slotRepaint()
01612 {
01613     setUpdatesEnabled( FALSE );
01614     // Send a resizeEvent to update the "toolbar extension arrow"
01615     // (The button you get when your toolbar-items don't fit in
01616     // the available space)
01617     QResizeEvent ev(size(), size());
01618     resizeEvent(&ev);
01619     QApplication::sendPostedEvents( this, QEvent::LayoutHint );
01620     setUpdatesEnabled( TRUE );
01621     repaint( FALSE );
01622 }
01623 
01624 void KToolBar::toolBarPosChanged( QToolBar *tb )
01625 {
01626     if ( tb != this )
01627         return;
01628     if ( d->oldPos == DockMinimized )
01629         rebuildLayout();
01630     d->oldPos = (QMainWindow::ToolBarDock)barPos();
01631     if ( mainWindow() && mainWindow()->inherits( "KMainWindow" ) )
01632         static_cast<KMainWindow *>(mainWindow())->setSettingsDirty();
01633 }
01634 
01635 void KToolBar::loadState( const QDomElement &element )
01636 {
01637     //kdDebug(220) << "KToolBar::loadState " << this << endl;
01638     if ( !mainWindow() || !mainWindow()->inherits( "KMainWindow") )
01639         return;
01640     KMainWindow *mw = static_cast<KMainWindow *>( mainWindow() );
01641 
01642     QCString text = element.namedItem( "text" ).toElement().text().utf8();
01643     if ( text.isEmpty() )
01644         text = element.namedItem( "Text" ).toElement().text().utf8();
01645 
01646     if ( !text.isEmpty() )
01647         setText( i18n( text ) );
01648 
01649     mw->addToolBar( this );
01650     QCString attrFullWidth = element.attribute( "fullWidth" ).lower().latin1();
01651     QCString attrPosition = element.attribute( "position" ).lower().latin1();
01652     QCString attrIconText = element.attribute( "iconText" ).lower().latin1();
01653     QString attrIconSize = element.attribute( "iconSize" ).lower();
01654     QString attrIndex = element.attribute( "index" ).lower();
01655     QString attrOffset = element.attribute( "offset" ).lower();
01656     QString attrNewLine = element.attribute( "newline" ).lower();
01657     QString attrHidden = element.attribute( "hidden" ).lower();
01658 
01659     if ( !attrFullWidth.isEmpty() ) {
01660         if ( attrFullWidth == "true" )
01661             setFullSize( TRUE );
01662         else
01663             setFullSize( FALSE );
01664     }
01665 
01666     Dock dock = DockTop;
01667     int index = -1 /*append by default*/, offset = -1;
01668     bool nl = FALSE;
01669 
01670     //kdDebug(220) << "KToolBar::loadState attrPosition=" << attrPosition << endl;
01671     if ( !attrPosition.isEmpty() ) {
01672         if ( attrPosition == "top" )
01673             dock = DockTop;
01674         else if ( attrPosition == "left" )
01675             dock = DockLeft;
01676         else if ( attrPosition == "right" )
01677             dock = DockRight;
01678         else if ( attrPosition == "bottom" )
01679             dock = DockBottom;
01680         else if ( attrPosition == "floating" )
01681             dock = DockTornOff;
01682         else if ( attrPosition == "flat" )
01683             dock = DockMinimized;
01684     }
01685 
01686     if ( !attrIndex.isEmpty() )
01687         index = attrIndex.toInt();
01688     if ( !attrOffset.isEmpty() )
01689         offset = attrOffset.toInt();
01690     if ( !attrNewLine.isEmpty() )
01691         nl = attrNewLine == "true" ? TRUE : FALSE;
01692 
01693     //kdDebug(220) << "KToolBar::loadState creating ToolBarInfo with dock=" << dock << endl;
01694     d->toolBarInfo = KToolBarPrivate::ToolBarInfo( dock, index, nl, offset );
01695     if ( mw )
01696     {
01697        mw->moveDockWindow( this, dock, nl, index, offset );
01698        //kdDebug(220) << "moveDockWindow in loadState for " << name() << " dock=" << dock << " nl=" << nl << " offset=" << offset << endl;
01699     }
01700 
01701     if ( !attrIconText.isEmpty() ) {
01702         //kdDebug(220) << "KToolBar::loadState attrIconText=" << attrIconText << endl;
01703         if ( attrIconText == "icontextright" )
01704             setIconText( KToolBar::IconTextRight );
01705         else if ( attrIconText == "textonly" )
01706             setIconText( KToolBar::TextOnly );
01707         else if ( attrIconText == "icontextbottom" )
01708             setIconText( KToolBar::IconTextBottom );
01709         else if ( attrIconText == "icononly" )
01710             setIconText( KToolBar::IconOnly );
01711     } else
01712         // Use global setting
01713         setIconText( iconTextSetting() );
01714 
01715     if ( !attrIconSize.isEmpty() )
01716         setIconSize( attrIconSize.toInt() );
01717 
01718     // Apply the highlight button setting
01719     d->m_highlight = highlightSetting();
01720 
01721     // Apply transparent-toolbar-moving setting (ok, this is global to the mainwindow,
01722     // but we do it only if there are toolbars...)
01723     if ( transparentSetting() != (!mw->opaqueMoving()) )
01724         mw->setOpaqueMoving( !transparentSetting() );
01725 
01726     if ( attrHidden == "true" )
01727         hide();
01728     else
01729         show();
01730 }
01731 
01732 void KToolBar::getAttributes( QString &position, QString &icontext, QString &index, QString &offset, QString &newLine )
01733 {
01734     // get all of the stuff to save
01735     switch ( barPos() ) {
01736     case KToolBar::Flat:
01737         position = "Flat";
01738         break;
01739     case KToolBar::Bottom:
01740         position = "Bottom";
01741         break;
01742     case KToolBar::Left:
01743         position = "Left";
01744         break;
01745     case KToolBar::Right:
01746         position = "Right";
01747         break;
01748     case KToolBar::Floating:
01749         position = "Floating";
01750         break;
01751     case KToolBar::Top:
01752     default:
01753         position = "Top";
01754         break;
01755     }
01756     if ( mainWindow() ) {
01757         QMainWindow::ToolBarDock dock;
01758         int index_;
01759         bool nl;
01760         int offset_;
01761         mainWindow()->getLocation( (QToolBar*)this, dock, index_, nl, offset_ );
01762         index = QString::number( index_ );
01763         offset = QString::number( offset_ );
01764         newLine = nl ? "true" : "false";
01765     }
01766 
01767     switch (d->m_iconText) {
01768     case KToolBar::IconTextRight:
01769         icontext = "IconTextRight";
01770         break;
01771     case KToolBar::IconTextBottom:
01772         icontext = "IconTextBottom";
01773         break;
01774     case KToolBar::TextOnly:
01775         icontext = "TextOnly";
01776         break;
01777     case KToolBar::IconOnly:
01778     default:
01779         icontext = "IconOnly";
01780         break;
01781     }
01782 }
01783 
01784 void KToolBar::saveState( QDomElement &current )
01785 {
01786     QString position, icontext, index, offset, newLine;
01787     getAttributes( position, icontext, index, offset, newLine );
01788 
01789     current.setAttribute( "noMerge", "1" );
01790     current.setAttribute( "position", position );
01791     current.setAttribute( "iconText", icontext );
01792     if ( !index.isEmpty() )
01793         current.setAttribute( "index", index );
01794     if ( !offset.isEmpty() )
01795         current.setAttribute( "offset", offset );
01796     if ( !newLine.isEmpty() )
01797         current.setAttribute( "newline", newLine );
01798     if ( isHidden() )
01799         current.setAttribute( "hidden", "true" );
01800     d->modified = true;
01801 }
01802 
01803 void KToolBar::positionYourself( bool force )
01804 {
01805     if (force)
01806         d->positioned = false;
01807 
01808     if ( d->positioned || !mainWindow() )
01809     {
01810         //kdDebug(220) << "KToolBar::positionYourself d->positioned=true  ALREADY DONE" << endl;
01811         return;
01812     }
01813     // we can't test for ForceHide after moveDockWindow because QDockArea
01814     // does a reparent() with showIt == true
01815     bool doHide = isHidden();
01816     //kdDebug(220) << "positionYourself " << name() << " dock=" << d->toolBarInfo.dock << " newLine=" << d->toolBarInfo.newline << " offset=" << d->toolBarInfo.offset << endl;
01817     mainWindow()->moveDockWindow( this, d->toolBarInfo.dock,
01818                                d->toolBarInfo.newline,
01819                                d->toolBarInfo.index,
01820                                d->toolBarInfo.offset );
01821     if ( doHide )
01822         hide();
01823     // This method can only have an effect once - unless force is set
01824     d->positioned = TRUE;
01825 }
01826 
01827 KPopupMenu *KToolBar::contextMenu()
01828 {
01829   if ( context )
01830     return context;
01831 
01832   // Construct our context popup menu. Name it qt_dockwidget_internal so it
01833   // won't be deleted by QToolBar::clear().
01834   context = new KPopupMenu( this, "qt_dockwidget_internal" );
01835   context->insertTitle(i18n("Toolbar Menu"));
01836 
01837   KPopupMenu *orient = new KPopupMenu( context, "orient" );
01838   orient->insertItem( i18n("toolbar position string","Top"),  CONTEXT_TOP );
01839   orient->insertItem( i18n("toolbar position string","Left"), CONTEXT_LEFT );
01840   orient->insertItem( i18n("toolbar position string","Right"), CONTEXT_RIGHT );
01841   orient->insertItem( i18n("toolbar position string","Bottom"), CONTEXT_BOTTOM );
01842   orient->insertSeparator(-1);
01843   //orient->insertItem( i18n("toolbar position string","Floating"), CONTEXT_FLOAT );
01844   orient->insertItem( i18n("min toolbar", "Flat"), CONTEXT_FLAT );
01845 
01846   KPopupMenu *mode = new KPopupMenu( context, "mode" );
01847   mode->insertItem( i18n("Icons Only"), CONTEXT_ICONS );
01848   mode->insertItem( i18n("Text Only"), CONTEXT_TEXT );
01849   mode->insertItem( i18n("Text Alongside Icons"), CONTEXT_TEXTRIGHT );
01850   mode->insertItem( i18n("Text Under Icons"), CONTEXT_TEXTUNDER );
01851 
01852   KPopupMenu *size = new KPopupMenu( context, "size" );
01853   size->insertItem( i18n("Default"), CONTEXT_ICONSIZES );
01854   // Query the current theme for available sizes
01855   KIconTheme *theme = KGlobal::instance()->iconLoader()->theme();
01856   QValueList<int> avSizes;
01857   if (!::qstrcmp(QObject::name(), "mainToolBar"))
01858       avSizes = theme->querySizes( KIcon::MainToolbar);
01859   else
01860       avSizes = theme->querySizes( KIcon::Toolbar);
01861 
01862   d->iconSizes = avSizes;
01863 
01864   QValueList<int>::Iterator it;
01865   for (it=avSizes.begin(); it!=avSizes.end(); it++) {
01866       QString text;
01867       if ( *it < 19 )
01868           text = i18n("Small (%1x%2)").arg(*it).arg(*it);
01869       else if (*it < 25)
01870           text = i18n("Medium (%1x%2)").arg(*it).arg(*it);
01871       else
01872           text = i18n("Large (%1x%2)").arg(*it).arg(*it);
01873       //we use the size as an id, with an offset
01874       size->insertItem( text, CONTEXT_ICONSIZES + *it );
01875   }
01876 
01877   context->insertItem( i18n("Orientation"), orient );
01878   orient->setItemChecked(CONTEXT_TOP, true);
01879   context->insertItem( i18n("Text Position"), mode );
01880   context->setItemChecked(CONTEXT_ICONS, true);
01881   context->insertItem( i18n("Icon Size"), size );
01882 
01883   if (mainWindow()->inherits("KMainWindow"))
01884   {
01885       if ( (static_cast<KMainWindow*>(mainWindow())->toolBarMenuAction()) &&
01886         (static_cast<KMainWindow*>(mainWindow())->hasMenuBar()) )
01887 
01888       (static_cast<KMainWindow*>(mainWindow()))->toolBarMenuAction()->plug(context);
01889   }
01890 
01891 
01892   connect( context, SIGNAL( aboutToShow() ), this, SLOT( slotContextAboutToShow() ) );
01893   return context;
01894 }
01895 
01896 void KToolBar::slotContextAboutToShow()
01897 {
01898   if (!d->m_configurePlugged)
01899   {
01900     // try to find "configure toolbars" action
01901     KXMLGUIClient *xmlGuiClient = d->m_xmlguiClient;
01902     if ( !xmlGuiClient && mainWindow() && mainWindow()->inherits( "KMainWindow" ) )
01903       xmlGuiClient = (KMainWindow *)mainWindow();
01904     if ( xmlGuiClient )
01905     {
01906         KAction *configureAction = xmlGuiClient->actionCollection()->action(KStdAction::stdName(KStdAction::ConfigureToolbars));
01907         if ( configureAction )
01908         {
01909           configureAction->plug(context);
01910           d->m_configurePlugged = true;
01911         }
01912     }
01913   }
01914 
01915   for(int i = CONTEXT_ICONS; i <= CONTEXT_TEXTUNDER; ++i)
01916     context->setItemChecked(i, false);
01917 
01918   switch( d->m_iconText )
01919   {
01920         case IconOnly:
01921         default:
01922             context->setItemChecked(CONTEXT_ICONS, true);
01923             break;
01924         case IconTextRight:
01925             context->setItemChecked(CONTEXT_TEXTRIGHT, true);
01926             break;
01927         case TextOnly:
01928             context->setItemChecked(CONTEXT_TEXT, true);
01929             break;
01930         case IconTextBottom:
01931             context->setItemChecked(CONTEXT_TEXTUNDER, true);
01932             break;
01933   }
01934 
01935   QValueList<int>::ConstIterator iIt = d->iconSizes.begin();
01936   QValueList<int>::ConstIterator iEnd = d->iconSizes.end();
01937   for (; iIt != iEnd; ++iIt )
01938       context->setItemChecked( CONTEXT_ICONSIZES + *iIt, false );
01939 
01940   context->setItemChecked( CONTEXT_ICONSIZES, false );
01941 
01942   context->setItemChecked( CONTEXT_ICONSIZES + d->m_iconSize, true );
01943 
01944   for ( int i = CONTEXT_TOP; i <= CONTEXT_FLAT; ++i )
01945       context->setItemChecked( i, false );
01946 
01947   switch ( barPos() )
01948   {
01949   case KToolBar::Flat:
01950       context->setItemChecked( CONTEXT_FLAT, true );
01951       break;
01952   case KToolBar::Bottom:
01953       context->setItemChecked( CONTEXT_BOTTOM, true );
01954       break;
01955   case KToolBar::Left:
01956       context->setItemChecked( CONTEXT_LEFT, true );
01957       break;
01958   case KToolBar::Right:
01959       context->setItemChecked( CONTEXT_RIGHT, true );
01960       break;
01961   case KToolBar::Floating:
01962       context->setItemChecked( CONTEXT_FLOAT, true );
01963       break;
01964   case KToolBar::Top:
01965       context->setItemChecked( CONTEXT_TOP, true );
01966       break;
01967   default: break;
01968   }
01969 }
01970 
01971 void KToolBar::widgetDestroyed()
01972 {
01973     removeWidgetInternal( (QWidget*)sender() );
01974 }
01975 
01976 void KToolBar::removeWidgetInternal( QWidget * w )
01977 {
01978     widgets.removeRef( w );
01979     QMap< QWidget*, int >::Iterator it = widget2id.find( w );
01980     if ( it == widget2id.end() )
01981         return;
01982     id2widget.remove( *it );
01983     widget2id.remove( it );
01984 }
01985 
01986 void KToolBar::virtual_hook( int, void* )
01987 { /*BASE::virtual_hook( id, data );*/ }
01988 
01989 #include "ktoolbar.moc"
01990 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:02 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001