lib

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., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 #include "tkaction.h"
00021 #include "tktoolbarbutton.h"
00022 #include "tkcombobox.h"
00023 
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 
00027 #include <ktoolbar.h>
00028 #include <kiconloader.h>
00029 
00030 #define SET_FOR_ALL_CONTAINER(WIDGET_TYPE,METHOD_NAME,VALUE)             \
00031   for( int id = 0; id < containerCount(); ++id ) {                       \
00032     QWidget* w = container(id);                                          \
00033     if ( w->inherits("KToolBar") ) {                                     \
00034       QWidget* r = static_cast<KToolBar*>(w)->getWidget(itemId(id));     \
00035       if (qstrcmp(r->name(),"KTToolBarLayout")==0)                       \
00036         r = (QWidget*)r->child("widget");                                \
00037       if ( r && r->inherits(#WIDGET_TYPE) ) {                            \
00038         WIDGET_TYPE* b = static_cast<WIDGET_TYPE*>(r);                   \
00039         b->METHOD_NAME(VALUE);                                         \
00040       }                                                                  \
00041     }                                                                    \
00042   }
00043 
00044 TKAction::TKAction(QObject* parent, const char* name)
00045 : KAction( "", 0, parent, name )
00046 {
00047   m_imode = TK::IconOnly;
00048 }
00049 
00050 TKAction::~TKAction()
00051 {
00052 }
00053 
00054 int TKAction::plug(QWidget* widget, int index)
00055 {
00056   if ( widget->inherits("KToolBar") ) {
00057     KToolBar* bar = static_cast<KToolBar*>(widget);
00058     int id_ = KAction::getToolButtonID();
00059     KInstance *instance;
00060 
00061     if ( parentCollection() )
00062       instance = parentCollection()->instance();
00063     else
00064       instance = KGlobal::instance();
00065 
00066     TKToolBarButton* b = new TKToolBarButton(icon(),plainText(),bar,name(),instance);
00067     // we don't need clicked() and buttonClicked(), do we?
00068     // connect(b,SIGNAL(clicked()),SLOT(slotActivated()));
00069     b->setIconMode(m_imode);
00070     initToolBarButton(b);
00071 
00072     bar->insertWidget( id_, 100, b, index );
00073     addContainer(bar,id_);
00074     connect( bar, SIGNAL(destroyed()), this, SLOT(slotDestroyed()) );
00075 
00076     return containerCount() - 1;
00077   }
00078   return KAction::plug(widget,index);
00079 }
00080 
00081 void TKAction::initToolBarButton(TKToolBarButton* button)
00082 {
00083   connect(button,SIGNAL(buttonClicked()),SLOT(slotActivated()));
00084 }
00085 
00086 TK::IconMode TKAction::iconMode()
00087 {
00088   return m_imode;
00089 }
00090 
00091 void TKAction::setIconMode(TK::IconMode mode)
00092 {
00093   m_imode = mode;
00094   SET_FOR_ALL_CONTAINER(TKToolBarButton,setIconMode,mode)
00095 }
00096 
00097 void TKAction::setText(const QString& text)
00098 {
00099   KAction::setText(text);
00100   updateLayout();
00101 }
00102 
00103 void TKAction::setIcon(const QString& icon)
00104 {
00105   KAction::setIcon(icon);
00106   updateLayout();
00107 }
00108 
00109 void TKAction::updateLayout()
00110 {
00111   int len = containerCount();
00112   for( int id = 0; id < len; ++id ) {
00113     QWidget* w = container( id );
00114     if (w->inherits("KToolBar")) {
00115       QWidget* r = static_cast<KToolBar*>(w)->getWidget(itemId(id));
00116       if (qstrcmp(r->name(),"KTToolBarLayout")==0) {
00117         updateLayout(r);
00118       }
00119     }
00120   }
00121 }
00122 
00123 QWidget* TKAction::createLayout(QWidget* parent, QWidget* children)
00124 {
00125   QWidget* base = new QWidget(parent,"KTToolBarLayout");
00126   QLabel* textLabel = new QLabel(base,"text");
00127   textLabel->setMinimumHeight(1);
00128   QLabel* pixLabel = new QLabel(base,"pixmap");
00129   children->reparent(base,QPoint(0,0));
00130   children->setName("widget");
00131   QHBoxLayout* layout = new QHBoxLayout(base,0,3);
00132   layout->setResizeMode(QLayout::Minimum);
00133   layout->addWidget(textLabel);
00134   layout->addWidget(pixLabel);
00135   layout->addWidget(children,1);
00136 
00137   updateLayout(base);
00138   return base;
00139 }
00140 
00141 void TKAction::updateLayout(QWidget* base)
00142 {
00143   QLabel* textLabel = (QLabel*)base->child("text");
00144   QLabel* pixLabel = (QLabel*)base->child("pixmap");
00145   QWidget* w = (QWidget*)base->child("widget");
00146 
00147   if (!textLabel || !pixLabel || !w)
00148     return;
00149 
00150   if (!text().isEmpty() && m_imode != TK::IconOnly ) {
00151     textLabel->setText(text());
00152     textLabel->show();
00153   } else
00154     textLabel->hide();
00155 
00156   QPixmap pix;
00157   if (hasIcon())
00158     pix = iconSet(KIcon::Small).pixmap();
00159 
00160   if (!icon().isEmpty())
00161     pix = BarIcon(icon());
00162 
00163   if (!pix.isNull() && m_imode != TK::TextOnly) {
00164     pixLabel->setPixmap(pix);
00165     pixLabel->show();
00166   } else
00167     pixLabel->hide();
00168 
00169   base->setFixedWidth( w->sizeHint().width() +
00170                        (textLabel->isVisible() ? textLabel->sizeHint().width():0) +
00171                        (pixLabel->isVisible() ? pixLabel->sizeHint().width():0) );
00172 }
00173 /******************************************************************************/
00174 TKBaseSelectAction::TKBaseSelectAction( QObject* parent, const char* name )
00175 : TKAction(parent,name)
00176 {
00177   m_current = 0;
00178   m_editable = false;
00179 }
00180 
00181 TKBaseSelectAction::~TKBaseSelectAction()
00182 {
00183 }
00184 
00185 int TKBaseSelectAction::plug(QWidget* widget, int index)
00186 {
00187   if ( widget->inherits("KToolBar") )
00188   {
00189     KToolBar* bar = static_cast<KToolBar*>( widget );
00190     int id_ = KAction::getToolButtonID();
00191 
00192     TKComboBox* cb = new TKComboBox(m_editable,bar);
00193     initComboBox(cb);
00194     cb->setMinimumWidth( cb->sizeHint().width() );
00195     QWidget* base = createLayout(bar,cb);
00196 
00197     bar->insertWidget( id_, 100, base, index );
00198     addContainer( bar, id_ );
00199 
00200     connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00201 
00202     setCurrentItem(currentItem());
00203 
00204     return containerCount() - 1;
00205   }
00206   return -1;
00207 }
00208 
00209 int TKBaseSelectAction::currentItem()
00210 {
00211   return m_current;
00212 }
00213 
00214 void TKBaseSelectAction::initComboBox(TKComboBox* cb)
00215 {
00216   connect(cb,SIGNAL(activated(int)),SLOT(slotActivated(int)));
00217 }
00218 
00219 void TKBaseSelectAction::setEditable(bool editable)
00220 {
00221   m_editable = editable;
00222   SET_FOR_ALL_CONTAINER(TKComboBox,setEditable,editable)
00223 }
00224 
00225 bool TKBaseSelectAction::isEditable()
00226 {
00227   return m_editable;
00228 }
00229 
00230 void TKBaseSelectAction::setCurrentItem(int index)
00231 {
00232   m_current = index;
00233   SET_FOR_ALL_CONTAINER(TKComboBox,setCurrentItem,index)
00234 }
00235 
00236 void TKBaseSelectAction::slotActivated(int id)
00237 {
00238   if ( m_current == id )
00239     return;
00240 
00241   m_current = id;
00242   setCurrentItem(id);
00243   activate(id);
00244 }
00245 
00246 void TKBaseSelectAction::activate(int id)
00247 {
00248   emit activated(id);
00249 }
00250 /******************************************************************************/
00251 TKSelectAction::TKSelectAction( QObject* parent, const char* name )
00252 : TKBaseSelectAction(parent,name)
00253 {
00254 }
00255 
00256 TKSelectAction::~TKSelectAction()
00257 {
00258 }
00259 
00260 void TKSelectAction::initComboBox(TKComboBox* cb)
00261 {
00262   TKBaseSelectAction::initComboBox(cb);
00263   connect(cb,SIGNAL(activated(const QString&)),SLOT(slotActivated(const QString&)));
00264   cb->insertStringList(items());
00265 }
00266 
00267 void TKSelectAction::slotActivated(const QString& text)
00268 {
00269   emit activated(text);
00270 }
00271 
00272 void TKSelectAction::setItems(const QStringList& lst )
00273 {
00274   m_list = lst;
00275   m_current = -1;
00276 
00277   SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
00278   SET_FOR_ALL_CONTAINER(TKComboBox,insertStringList,lst)
00279 
00280   // Disable if empty and not editable
00281   setEnabled ( lst.count() > 0 || m_editable );
00282 }
00283 
00284 QStringList TKSelectAction::items() const
00285 {
00286   return m_list;
00287 }
00288 
00289 void TKSelectAction::clear()
00290 {
00291   SET_FOR_ALL_CONTAINER(TKComboBox,clear, )
00292 }
00293 
00294 void TKSelectAction::setEditText(const QString& text)
00295 {
00296   SET_FOR_ALL_CONTAINER(TKComboBox,setEditText,text)
00297 }
00298 
00299 #undef SET_FOR_ALL_CONTAINER
00300 #include "tkaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys