lib Library API Documentation

tkaction.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2000 theKompany.com
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 #include "tkaction.h"
00020 #include "tktoolbarbutton.h"
00021 #include "tkcombobox.h"
00022 
00023 #include <qlabel.h>
00024 #include <qlayout.h>
00025 
00026 #include <ktoolbar.h>
00027 #include <kiconloader.h>
00028 
00029 #define SET_FOR_ALL_CONTAINER(WIDGET_TYPE,METHOD_NAME,VALUE)             \
00030   for( int id = 0; id < containerCount(); ++id ) {                       \
00031     QWidget* w = container(id);                                          \
00032     if ( w->inherits("KToolBar") ) {                                     \
00033       QWidget* r = static_cast<KToolBar*>(w)->getWidget(itemId(id));     \
00034       if (qstrcmp(r->name(),"KTToolBarLayout")==0)                       \
00035         r = (QWidget*)r->child("widget");                                \
00036       if ( r && r->inherits(#WIDGET_TYPE) ) {                            \
00037         WIDGET_TYPE* b = static_cast<WIDGET_TYPE*>(r);                   \
00038         b->METHOD_NAME(VALUE);                                         \
00039       }                                                                  \
00040     }                                                                    \
00041   }
00042 
00043 TKAction::TKAction(QObject* parent, const char* name)
00044 : KAction( "", 0, parent, name )
00045 {
00046   m_imode = TK::IconOnly;
00047 }
00048 
00049 TKAction::~TKAction()
00050 {
00051 }
00052 
00053 int TKAction::plug(QWidget* widget, int index)
00054 {
00055   if ( widget->inherits("KToolBar") ) {
00056     KToolBar* bar = static_cast<KToolBar*>(widget);
00057     int id_ = KAction::getToolButtonID();
00058     KInstance *instance;
00059 
00060     if ( parentCollection() )
00061       instance = parentCollection()->instance();
00062     else
00063       instance = KGlobal::instance();
00064 
00065     TKToolBarButton* b = new TKToolBarButton(icon(),plainText(),bar,name(),instance);
00066     // we don't need clicked() and buttonClicked(), do we?
00067     // connect(b,SIGNAL(clicked()),SLOT(slotActivated()));
00068     b->setIconMode(m_imode);
00069     initToolBarButton(b);
00070 
00071     bar->insertWidget( id_, 100, b, index );
00072     addContainer(bar,id_);
00073     connect( bar, SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
00074 
00075     return containerCount() - 1;
00076   }
00077   return KAction::plug(widget,index);
00078 }
00079 
00080 void TKAction::initToolBarButton(TKToolBarButton* button)
00081 {
00082   connect(button,SIGNAL(buttonClicked()),SLOT(slotActivated()));
00083 }
00084 
00085 TK::IconMode TKAction::iconMode()
00086 {
00087   return m_imode;
00088 }
00089 
00090 void TKAction::setIconMode(TK::IconMode mode)
00091 {
00092   m_imode = mode;
00093   SET_FOR_ALL_CONTAINER(TKToolBarButton,setIconMode,mode)
00094 }
00095 
00096 void TKAction::setText(const QString& text)
00097 {
00098   KAction::setText(text);
00099   updateLayout();
00100 }
00101 
00102 void TKAction::setIcon(const QString& icon)
00103 {
00104   KAction::setIcon(icon);
00105   updateLayout();
00106 }
00107 
00108 void TKAction::updateLayout()
00109 {
00110   int len = containerCount();
00111   for( int id = 0; id < len; ++id ) {
00112     QWidget* w = container( id );
00113     if (w->inherits("KToolBar")) {
00114       QWidget* r = static_cast<KToolBar*>(w)->getWidget(itemId(id));
00115       if (qstrcmp(r->name(),"KTToolBarLayout")==0) {
00116         updateLayout(r);
00117       }
00118     }
00119   }
00120 }
00121 
00122 QWidget* TKAction::createLayout(QWidget* parent, QWidget* children)
00123 {
00124   QWidget* base = new QWidget(parent,"KTToolBarLayout");
00125   QLabel* textLabel = new QLabel(base,"text");
00126   textLabel->setMinimumHeight(1);
00127   QLabel* pixLabel = new QLabel(base,"pixmap");
00128   children->reparent(base,QPoint(0,0));
00129   children->setName("widget");
00130   QHBoxLayout* layout = new QHBoxLayout(base,0,3);
00131   layout->setResizeMode(QLayout::Minimum);
00132   layout->addWidget(textLabel);
00133   layout->addWidget(pixLabel);
00134   layout->addWidget(children,1);
00135 
00136   updateLayout(base);
00137   return base;
00138 }
00139 
00140 void TKAction::updateLayout(QWidget* base)
00141 {
00142   QLabel* textLabel = (QLabel*)base->child("text");
00143   QLabel* pixLabel = (QLabel*)base->child("pixmap");
00144   QWidget* w = (QWidget*)base->child("widget");
00145 
00146   if (!textLabel || !pixLabel || !w)
00147     return;
00148 
00149   if (!text().isEmpty() && m_imode != TK::IconOnly ) {
00150     textLabel->setText(text());
00151     textLabel->show();
00152   } else
00153     textLabel->hide();
00154 
00155   QPixmap pix;
00156   if (hasIcon())
00157     pix = iconSet(KIcon::Small).pixmap();
00158 
00159   if (!icon().isEmpty())
00160     pix = BarIcon(icon());
00161 
00162   if (!pix.isNull() && m_imode != TK::TextOnly) {
00163     pixLabel->setPixmap(pix);
00164     pixLabel->show();
00165   } else
00166     pixLabel->hide();
00167 
00168   base->setFixedWidth( w->sizeHint().width() +
00169                        (textLabel->isVisible() ? textLabel->sizeHint().width():0) +
00170                        (pixLabel->isVisible() ? pixLabel->sizeHint().width():0) );
00171 }
00172 /******************************************************************************/
00173 TKBaseSelectAction::TKBaseSelectAction( QObject* parent, const char* name )
00174 : TKAction(parent,name)
00175 {
00176   m_current = 0;
00177   m_editable = false;
00178 }
00179 
00180 TKBaseSelectAction::~TKBaseSelectAction()
00181 {
00182 }
00183 
00184 int TKBaseSelectAction::plug(QWidget* widget, int index)
00185 {
00186   if ( widget->inherits("KToolBar") )
00187   {
00188     KToolBar* bar = static_cast<KToolBar*>( widget );
00189     int id_ = KAction::getToolButtonID();
00190 
00191     TKComboBox* cb = new TKComboBox(m_editable,bar);
00192     initComboBox(cb);
00193     cb->setMinimumWidth( cb->sizeHint().width() );
00194     QWidget* base = createLayout(bar,cb);
00195 
00196     bar->insertWidget( id_, 100, base, index );
00197     addContainer( bar, id_ );
00198 
00199     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00200 
00201     setCurrentItem(currentItem());
00202 
00203     return containerCount() - 1;
00204   }
00205   return -1;
00206 }
00207 
00208 int TKBaseSelectAction::currentItem()
00209 {
00210   return m_current;
00211 }
00212 
00213 void TKBaseSelectAction::initComboBox(TKComboBox* cb)
00214 {
00215   connect(cb,SIGNAL(activated(int)),SLOT(slotActivated(int)));
00216 }
00217 
00218 void TKBaseSelectAction::setEditable(bool editable)
00219 {
00220   m_editable = editable;
00221   SET_FOR_ALL_CONTAINER(TKComboBox,setEditable,editable)
00222 }
00223 
00224 bool TKBaseSelectAction::isEditable()
00225 {
00226   return m_editable;
00227 }
00228 
00229 void TKBaseSelectAction::setCurrentItem(int index)
00230 {
00231   m_current = index;
00232   SET_FOR_ALL_CONTAINER(TKComboBox,setCurrentItem,index)
00233 }
00234 
00235 void TKBaseSelectAction::slotActivated(int id)
00236 {
00237   if ( m_current == id )
00238     return;
00239 
00240   m_current = id;
00241   setCurrentItem(id);
00242   activate(id);
00243 }
00244 
00245 void TKBaseSelectAction::activate(int id)
00246 {
00247   emit activated(id);
00248 }
00249 /******************************************************************************/
00250 TKSelectAction::TKSelectAction( QObject* parent, const char* name )
00251 : TKBaseSelectAction(parent,name)
00252 {
00253 }
00254 
00255 TKSelectAction::~TKSelectAction()
00256 {
00257 }
00258 
00259 void TKSelectAction::initComboBox(TKComboBox* cb)
00260 {
00261   TKBaseSelectAction::initComboBox(cb);
00262   connect(cb,SIGNAL(activated(const QString&)),SLOT(slotActivated(const QString&)));
00263   cb->insertStringList(items());
00264 }
00265 
00266 void TKSelectAction::slotActivated(const QString& text)
00267 {
00268   emit activated(text);
00269 }
00270 
00271 void TKSelectAction::setItems(const QStringList& lst )
00272 {
00273   m_list = lst;
00274   m_current = -1;
00275 
00276   SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
00277   SET_FOR_ALL_CONTAINER(TKComboBox,insertStringList,lst)
00278 
00279   // Disable if empty and not editable
00280   setEnabled ( lst.count() > 0 || m_editable );
00281 }
00282 
00283 QStringList TKSelectAction::items() const
00284 {
00285   return m_list;
00286 }
00287 
00288 void TKSelectAction::clear()
00289 {
00290   SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
00291 }
00292 
00293 void TKSelectAction::setEditText(const QString& text)
00294 {
00295   SET_FOR_ALL_CONTAINER(TKComboBox,setEditText,text)
00296 }
00297 
00298 #undef SET_FOR_ALL_CONTAINER
00299 #include "tkaction.moc"
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Mar 11 11:47:46 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003