lib

KoToolBox.cpp

00001 /*
00002    Copyright (c) 2005 Boudewijn Rempt <boud@valdyas.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qbuttongroup.h>
00021 #include <qnamespace.h>
00022 #include <qtoolbutton.h>
00023 #include <qlabel.h>
00024 #include <qtooltip.h>
00025 #include <qlayout.h>
00026 #include <qpixmap.h>
00027 #include <qtoolbar.h>
00028 #include <qdockwindow.h>
00029 
00030 #include <kdebug.h>
00031 #include <kparts/event.h>
00032 #include <klocale.h>
00033 #include <ktoolbar.h>
00034 #include <kiconloader.h>
00035 #include <kseparator.h>
00036 #include <kaction.h>
00037 #include <kactioncollection.h>
00038 #include <kactionclasses.h>
00039 
00040 #include <KoMainWindow.h>
00041 #include "KoToolBox.h"
00042 
00043 #ifdef HAVE_CONFIG_H
00044 #include <config.h>
00045 #endif
00046 
00047 KoToolBox::KoToolBox( KMainWindow *mainWin, const char* name, KInstance* instance, int numberOfTooltypes )
00048     : KToolBar( mainWin, Qt::DockLeft, false, name, true, true), m_instance(instance)
00049 {
00050     setFullSize( false );
00051     setMargin(2);
00052 
00053     m_buttonGroup = new QButtonGroup( 0L );
00054     m_buttonGroup->setExclusive( true );
00055     connect( m_buttonGroup, SIGNAL( pressed( int ) ), this, SLOT( slotButtonPressed( int ) ) );
00056 
00057     m_tools.setAutoDelete( true );
00058     // Create separate lists for the various sorts of tools
00059     for (int i = 0; i < numberOfTooltypes ; ++i) {
00060         ToolList * tl = new ToolList();
00061         m_tools.append(tl);
00062     }
00063 }
00064 
00065 KoToolBox::~KoToolBox()
00066 {
00067     delete m_buttonGroup;
00068 }
00069 
00070 
00071 void KoToolBox::slotPressButton( int id )
00072 {
00073     m_buttonGroup->setButton( id );
00074     slotButtonPressed( id );
00075 }
00076 
00077 void KoToolBox::slotButtonPressed( int id )
00078 {
00079     if( id != m_buttonGroup->selectedId() && m_buttonGroup->selected() ) {
00080         m_buttonGroup->selected()->setDown( false );
00081     }
00082     m_idToActionMap.at(id)->activate();
00083 
00084 }
00085 
00086 void KoToolBox::registerTool( KAction *tool, int toolType, Q_UINT32 priority )
00087 {
00088     uint prio = priority;
00089     ToolList * tl = m_tools.at(toolType);
00090     while( (*tl)[prio] ) prio++;
00091     (*tl)[prio] = tool;
00092 }
00093 
00094 QToolButton *KoToolBox::createButton(QWidget * parent,  const char* iconName, QString tooltip)
00095 {
00096     QToolButton *button = new QToolButton(parent);
00097 
00098     if ( iconName != "" ) {
00099         QPixmap pixmap = BarIcon( iconName, m_instance );
00100         button->setPixmap( pixmap );
00101         button->setToggleButton( true );
00102     }
00103 
00104     if ( !tooltip.isEmpty() ) {
00105         QToolTip::add( button, tooltip );
00106     }
00107     return button;
00108 }
00109 
00110 
00111 void KoToolBox::setupTools()
00112 {
00113     int id = 0;
00114     // Loop through tooltypes
00115     for (uint i = 0; i < m_tools.count(); ++i) {
00116         ToolList * tl = m_tools.at(i);
00117 
00118         if (!tl) continue;
00119         if (tl->isEmpty()) continue;
00120 
00121         ToolArea *tools = new ToolArea( this );
00122         ToolList::Iterator it;
00123         for ( it = tl->begin(); it != tl->end(); ++it )
00124         {
00125             KAction *tool = it.data();
00126             if(! tool)
00127                 continue;
00128             QToolButton *bn = createButton(tools->getNextParent(), tool->icon().latin1(), tool->toolTip());
00129             tools->add(bn);
00130             m_buttonGroup->insert( bn, id++ );
00131             m_idToActionMap.append( tool );
00132         }
00133         addSeparator();
00134         m_toolBoxes.append(tools);
00135     }
00136     // select first (select tool)
00137     m_buttonGroup->setButton( 0 );
00138     m_numberOfButtons = id;
00139 }
00140 
00141 
00142 void KoToolBox::setOrientation ( Qt::Orientation o )
00143 {
00144     if ( barPos() == Floating ) { // when floating, make it a standing toolbox.
00145         o = o == Qt::Vertical ? Qt::Horizontal : Qt::Vertical;
00146     }
00147 
00148     QDockWindow::setOrientation( o );
00149 
00150     for (uint i = 0; i < m_toolBoxes.count(); ++i) {
00151         ToolArea *t = m_toolBoxes.at(i);
00152         t->setOrientation(o);
00153     }
00154 }
00155 
00156 
00157 void KoToolBox::enableTools(bool enable)
00158 {
00159     if (m_tools.isEmpty()) return;
00160     if (!m_buttonGroup) return;
00161     if (m_numberOfButtons <= 0) return;
00162 
00163     for (uint i = 0; i < m_tools.count(); ++i) {
00164         ToolList * tl = m_tools.at(i);
00165 
00166         if (!tl) continue;
00167 
00168         ToolList::Iterator it;
00169         for ( it = tl->begin(); it != tl->end(); ++it )
00170             if (it != 0 && it.data())
00171                 it.data()->setEnabled(enable);
00172     }
00173     m_buttonGroup->setEnabled(enable);
00174     for (Q_UINT32 i = 0; i < m_numberOfButtons; ++i) {
00175         m_buttonGroup->find( i )->setEnabled( enable );
00176     }
00177 }
00178 
00179 void KoToolBox::slotSetTool(const QString & toolname)
00180 {
00181     for (uint i = 0; i < m_idToActionMap.count(); ++i) {
00182         KAction * a = m_idToActionMap.at(i);
00183         if (!a || a->name() != toolname) continue;
00184 
00185         m_buttonGroup->setButton(i);
00186         return;
00187 
00188     }
00189 }
00190 
00191 
00192 // ----------------------------------------------------------------
00193 //                         class ToolArea
00194 
00195 
00196 ToolArea::ToolArea(QWidget *parent)
00197     : QWidget(parent), m_left(true)
00198 {
00199     m_layout = new QBoxLayout(this, QBoxLayout::LeftToRight, 0, 0, 0);
00200     QWidget *w = new QWidget(this);
00201     m_layout->addWidget(w);
00202 
00203     QGridLayout *grid = new QGridLayout(w, 2, 2);
00204     m_leftRow = new QWidget(w);
00205     grid->addWidget(m_leftRow, 0, 0);
00206     m_leftLayout = new QBoxLayout(m_leftRow, QBoxLayout::TopToBottom, 0, 1, 0);
00207 
00208     w = new QWidget(this);
00209     m_layout->addWidget(w);
00210 
00211     grid = new QGridLayout(w, 2, 2);
00212     m_rightRow = new QWidget(w);
00213     grid->addWidget(m_rightRow, 0, 0);
00214     m_rightLayout = new QBoxLayout(m_rightRow, QBoxLayout::TopToBottom, 0, 1, 0);
00215 }
00216 
00217 
00218 ToolArea::~ToolArea()
00219 {
00220 }
00221 
00222 
00223 void ToolArea::add(QWidget *button)
00224 {
00225     if (m_left)
00226         m_leftLayout->addWidget(button);
00227     else
00228         m_rightLayout->addWidget(button);
00229     button->show();
00230     m_left = !m_left;
00231 }
00232 
00233 
00234 QWidget* ToolArea::getNextParent()
00235 {
00236     if (m_left)
00237         return m_leftRow;
00238     return m_rightRow;
00239 }
00240 
00241 
00242 void ToolArea::setOrientation ( Qt::Orientation o )
00243 {
00244     QBoxLayout::Direction  dir = (o != Qt::Horizontal 
00245                   ? QBoxLayout::TopToBottom
00246                   : QBoxLayout::LeftToRight);
00247     m_leftLayout->setDirection(dir);
00248     m_rightLayout->setDirection(dir);
00249 
00250     m_layout->setDirection(o == Qt::Horizontal
00251                ? QBoxLayout::TopToBottom
00252                : QBoxLayout::LeftToRight);
00253 }
00254 
00255 
00256 #include "KoToolBox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys