kmdi Library API Documentation

kmditaskbar.cpp

00001 //----------------------------------------------------------------------------
00002 //    filename             : kmditaskbar.cpp
00003 //----------------------------------------------------------------------------
00004 //    Project              : KDE MDI extension
00005 //
00006 //    begin                : 07/1999       by Szymon Stefanek as part of kvirc
00007 //                                         (an IRC application)
00008 //    changes              : 09/1999       by Falk Brettschneider to create an
00009 //                           - 06/2000     stand-alone Qt extension set of
00010 //                                         classes and a Qt-based library
00011 //                           2000-2003     maintained by the KDevelop project
00012 //    patches              : 02/2000       by Massimo Morin (mmorin@schedsys.com)
00013 //
00014 //    copyright            : (C) 1999-2003 by Szymon Stefanek (stefanek@tin.it)
00015 //                                         and
00016 //                                         Falk Brettschneider
00017 //    email                :  falkbr@kdevelop.org (Falk Brettschneider)
00018 //----------------------------------------------------------------------------
00019 //
00020 //----------------------------------------------------------------------------
00021 //
00022 //    This program is free software; you can redistribute it and/or modify
00023 //    it under the terms of the GNU Library General Public License as
00024 //    published by the Free Software Foundation; either version 2 of the
00025 //    License, or (at your option) any later version.
00026 //
00027 //----------------------------------------------------------------------------
00028 
00029 #include "kmditaskbar.h"
00030 #include "kmditaskbar.moc"
00031 
00032 #include "kmdimainfrm.h"
00033 #include "kmdichildview.h"
00034 #include "kmdidefines.h"
00035 
00036 #include <qtooltip.h>
00037 #include <qlabel.h>
00038 #include <qwidget.h>
00039 #include <qstyle.h>
00040 
00041 #include <qnamespace.h>
00042 
00043 /*
00044    @quickhelp: KMdiTaskBar
00045    @widget: Taskbar
00046       This window lists the currently open windows.<br>
00047       Each button corresponds to a single MDI (child) window.<br>
00048       The button is enabled (clickable) when the window is docked , and can be
00049       pressed to bring it to the top of the other docked windows.<br>
00050       The button text becomes red when new output is shown in the window and it is not the active one.<br>
00051 */
00052 
00053 //####################################################################
00054 //
00055 // KMdiTaskBarButton
00056 //
00057 //####################################################################
00058 KMdiTaskBarButton::KMdiTaskBarButton(KMdiTaskBar *pTaskBar,KMdiChildView *win_ptr)
00059 :QPushButton(pTaskBar),
00060  m_actualText("")
00061 {
00062    setToggleButton( true);
00063    m_pWindow      = win_ptr;
00064    QToolTip::add(this,win_ptr->caption());
00065    setFocusPolicy(NoFocus);
00066 }
00067 
00068 KMdiTaskBarButton::~KMdiTaskBarButton()
00069 {
00070 }
00071 
00072 void KMdiTaskBarButton::mousePressEvent( QMouseEvent* e)
00073 {
00074    switch(e->button()) {
00075    case QMouseEvent::LeftButton:
00076       emit leftMouseButtonClicked( m_pWindow);
00077       break;
00078    case QMouseEvent::RightButton:
00079       emit rightMouseButtonClicked( m_pWindow);
00080       break;
00081    default:
00082       break;
00083    }
00084    emit clicked( m_pWindow);
00085 }
00086 
00088 void KMdiTaskBarButton::setNewText(const QString& s)
00089 {
00090    setText( s);
00091    emit buttonTextChanged( 0);
00092 }
00093 
00094 void KMdiTaskBarButton::setText(const QString& s)
00095 {
00096    m_actualText = s;
00097    QButton::setText( s);
00098 }
00099 
00100 void KMdiTaskBarButton::fitText(const QString& origStr, int newWidth)
00101 {
00102    QButton::setText( m_actualText);
00103 
00104    int actualWidth = sizeHint().width();
00105    int realLetterCount = origStr.length();
00106    int newLetterCount = (newWidth * realLetterCount) / actualWidth;
00107    int w = newWidth+1;
00108    QString s = origStr;
00109    while((w > newWidth) && (newLetterCount >= 1)) {
00110       if( newLetterCount < realLetterCount) {
00111          if(newLetterCount > 3)
00112             s = origStr.left( newLetterCount/2) + "..." + origStr.right( newLetterCount/2);
00113          else {
00114             if(newLetterCount > 1)
00115                s = origStr.left( newLetterCount) + "..";
00116             else
00117                s = origStr.left(1);
00118          }
00119       }
00120       QFontMetrics fm = fontMetrics();
00121       w = fm.width(s);
00122       newLetterCount--;
00123    }
00124 
00125    QButton::setText( s);
00126 }
00127 
00128 QString KMdiTaskBarButton::actualText() const
00129 {
00130    return m_actualText;
00131 }
00132 
00133 //####################################################################
00134 //
00135 // KMdiTaskBar
00136 //
00137 //####################################################################
00138 
00139 KMdiTaskBar::KMdiTaskBar(KMdiMainFrm *parent,QMainWindow::ToolBarDock dock)
00140 :  KToolBar( parent, "KMdiTaskBar", /*honor_style*/ false, /*readConfig*/ true)
00141    ,m_pCurrentFocusedWindow(0)
00142    ,m_pStretchSpace(0)
00143    ,m_layoutIsPending(false)
00144    ,m_bSwitchedOn(false)
00145 {
00146    m_pFrm = parent;
00147    m_pButtonList = new QPtrList<KMdiTaskBarButton>;
00148    m_pButtonList->setAutoDelete(true);
00149 //QT30   setFontPropagation(QWidget::SameFont);
00150    setMinimumWidth(1);
00151    setFocusPolicy(NoFocus);
00152    parent->moveToolBar( this, dock); //XXX obsolete!
00153 }
00154 
00155 KMdiTaskBar::~KMdiTaskBar()
00156 {
00157    delete m_pButtonList;
00158 }
00159 
00160 KMdiTaskBarButton * KMdiTaskBar::addWinButton(KMdiChildView *win_ptr)
00161 {
00162    if( m_pStretchSpace) {
00163       delete m_pStretchSpace;
00164       m_pStretchSpace = 0L;
00165       setStretchableWidget( 0L);
00166    }
00167 
00168    KMdiTaskBarButton *b=new KMdiTaskBarButton( this, win_ptr);
00169    QObject::connect( b, SIGNAL(clicked()), win_ptr, SLOT(setFocus()) );
00170    QObject::connect( b, SIGNAL(clicked(KMdiChildView*)), this, SLOT(setActiveButton(KMdiChildView*)) );
00171    QObject::connect( b, SIGNAL(leftMouseButtonClicked(KMdiChildView*)), m_pFrm, SLOT(activateView(KMdiChildView*)) );
00172    QObject::connect( b, SIGNAL(rightMouseButtonClicked(KMdiChildView*)), m_pFrm, SLOT(taskbarButtonRightClicked(KMdiChildView*)) );
00173    QObject::connect( b, SIGNAL(buttonTextChanged(int)), this, SLOT(layoutTaskBar(int)) );
00174    m_pButtonList->append(b);
00175    b->setToggleButton( true);
00176    b->setText(win_ptr->tabCaption());
00177 
00178    layoutTaskBar();
00179 
00180    m_pStretchSpace = new QLabel(this, "empty");
00181    m_pStretchSpace->setText("");
00182    setStretchableWidget( m_pStretchSpace);
00183    m_pStretchSpace->show();
00184 
00185    if (m_bSwitchedOn) {
00186       b->show();
00187       show();
00188    }
00189    return b;
00190 }
00191 
00192 void KMdiTaskBar::removeWinButton(KMdiChildView *win_ptr, bool haveToLayoutTaskBar)
00193 {
00194    KMdiTaskBarButton *b=getButton(win_ptr);
00195    if (b){
00196       m_pButtonList->removeRef(b);
00197       if( haveToLayoutTaskBar) layoutTaskBar();
00198    }
00199    if (m_pButtonList->count() == 0) {
00200       if (m_pStretchSpace != 0L) {
00201          delete m_pStretchSpace;
00202          m_pStretchSpace = 0L;
00203          hide();
00204       }
00205    }
00206 }
00207 
00208 void KMdiTaskBar::switchOn(bool bOn)
00209 {
00210    m_bSwitchedOn = bOn;
00211    if (!bOn) {
00212       hide();
00213    }
00214    else {
00215       if (m_pButtonList->count() > 0) {
00216          show();
00217       }
00218       else {
00219          hide();
00220       }
00221    }
00222 }
00223 
00224 KMdiTaskBarButton * KMdiTaskBar::getButton(KMdiChildView *win_ptr)
00225 {
00226    for(KMdiTaskBarButton *b=m_pButtonList->first();b;b=m_pButtonList->next()){
00227       if(b->m_pWindow == win_ptr)return b;
00228    }
00229    return 0;
00230 }
00231 
00232 KMdiTaskBarButton * KMdiTaskBar::getNextWindowButton(bool bRight,KMdiChildView *win_ptr)
00233 {
00234    if(bRight){
00235       for(KMdiTaskBarButton *b=m_pButtonList->first();b;b=m_pButtonList->next()){
00236          if(b->m_pWindow == win_ptr){
00237             b = m_pButtonList->next();
00238             if(!b)b = m_pButtonList->first();
00239             if(win_ptr != b->m_pWindow)return b;
00240             else return 0;
00241          }
00242       }
00243    } else {
00244       for(KMdiTaskBarButton *b=m_pButtonList->first();b;b=m_pButtonList->next()){
00245          if(b->m_pWindow == win_ptr){
00246             b = m_pButtonList->prev();
00247             if(!b)b = m_pButtonList->last();
00248             if(win_ptr != b->m_pWindow)return b;
00249             else return 0;
00250          }
00251       }
00252    }
00253    return 0;
00254 }
00255 
00256 void KMdiTaskBar::setActiveButton(KMdiChildView *win_ptr)
00257 {
00258    KMdiTaskBarButton* newPressedButton = 0L;
00259    KMdiTaskBarButton* oldPressedButton = 0L;
00260    for(KMdiTaskBarButton *b=m_pButtonList->first();b;b=m_pButtonList->next()){
00261       if( b->m_pWindow == win_ptr)
00262          newPressedButton = b;
00263       if( b->m_pWindow == m_pCurrentFocusedWindow)
00264          oldPressedButton = b;
00265    }
00266 
00267    if( newPressedButton != 0L && newPressedButton != oldPressedButton) {
00268          if( oldPressedButton != 0L)
00269             oldPressedButton->toggle();// switch off
00270          newPressedButton->toggle();   // switch on
00271          m_pCurrentFocusedWindow = win_ptr;
00272    }
00273 }
00274 
00275 void KMdiTaskBar::layoutTaskBar( int taskBarWidth)
00276 {
00277    if (m_layoutIsPending) return;
00278    m_layoutIsPending = true;
00279 
00280    if( !taskBarWidth)
00281       // no width is given
00282       taskBarWidth = width();
00283 
00284    // calculate current width of all taskbar buttons
00285    int allButtonsWidth = 0;
00286    KMdiTaskBarButton *b = 0;
00287    for(b=m_pButtonList->first();b;b=m_pButtonList->next()){
00288       allButtonsWidth += b->width();
00289    }
00290 
00291    // calculate actual width of all taskbar buttons
00292    int allButtonsWidthHint = 0;
00293    for(b=m_pButtonList->first();b;b=m_pButtonList->next()){
00294       QFontMetrics fm = b->fontMetrics();
00295       QString s = b->actualText();
00296       QSize sz = fm.size(ShowPrefix, s);
00297       int w = sz.width()+6;
00298       int h = sz.height()+sz.height()/8+10;
00299       w += h;
00300       allButtonsWidthHint += w;
00301    }
00302 
00303    // if there's enough space, use actual width
00304    int buttonCount = m_pButtonList->count();
00305    int tbHandlePixel;
00306    tbHandlePixel = style().pixelMetric(QStyle::PM_DockWindowHandleExtent, this);
00307    int buttonAreaWidth = taskBarWidth - tbHandlePixel - style().pixelMetric(QStyle::PM_DefaultFrameWidth, this) - 5;
00308    if( ((allButtonsWidthHint) <= buttonAreaWidth) || (width() < parentWidget()->width())) {
00309       for(b=m_pButtonList->first();b;b=m_pButtonList->next()){
00310          b->setText( b->actualText());
00311          if (b->width() != b->sizeHint().width()) {
00312             b->setFixedWidth( b->sizeHint().width());
00313             b->show();
00314          }
00315       }
00316    }
00317    else {
00318       // too many buttons for actual width
00319       int newButtonWidth;
00320       if( buttonCount != 0)
00321          newButtonWidth = buttonAreaWidth / buttonCount;
00322       else
00323          newButtonWidth = 0;
00324       if( orientation() == Qt::Vertical)
00325          newButtonWidth = 80;
00326       if(newButtonWidth > 0)
00327          for(b=m_pButtonList->first();b;b=m_pButtonList->next()){
00328             b->fitText( b->actualText(), newButtonWidth);
00329             if (b->width() != newButtonWidth) {
00330                b->setFixedWidth( newButtonWidth);
00331                b->show();
00332             }
00333          }
00334    }
00335    m_layoutIsPending = false;
00336 }
00337 
00338 void KMdiTaskBar::resizeEvent( QResizeEvent* rse)
00339 {
00340    if (!m_layoutIsPending) {
00341       if (m_pButtonList->count() != 0) {
00342          layoutTaskBar( rse->size().width());
00343       }
00344    }
00345    KToolBar::resizeEvent( rse);
00346 }
KDE Logo
This file is part of the documentation for kmdi Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Nov 27 13:49:59 2004 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003