ctkAddRemoveComboBox.cpp

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Library:   CTK
00004  
00005   Copyright (c) 2010  Kitware Inc.
00006 
00007   Licensed under the Apache License, Version 2.0 (the "License");
00008   you may not use this file except in compliance with the License.
00009   You may obtain a copy of the License at
00010 
00011       http://www.commontk.org/LICENSE
00012 
00013   Unless required by applicable law or agreed to in writing, software
00014   distributed under the License is distributed on an "AS IS" BASIS,
00015   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016   See the License for the specific language governing permissions and
00017   limitations under the License.
00018  
00019 =========================================================================*/
00020 
00021 // Qt includes
00022 #include <QDebug>
00023 
00024 // CTK includes
00025 #include <ctkPimpl.h>
00026 #include "ctkAddRemoveComboBox.h"
00027 #include "ui_ctkAddRemoveComboBox.h"
00028 
00029 //-----------------------------------------------------------------------------
00030 class ctkAddRemoveComboBoxPrivate : public ctkPrivate<ctkAddRemoveComboBox>,
00031                                     public Ui_ctkAddRemoveComboBox
00032 {
00033 public:
00034   ctkAddRemoveComboBoxPrivate();
00035 
00040   void insertEmptyItem();
00041 
00042   void connectComboBox(QComboBox* combobox);
00043 
00044 public:
00046   QString EmptyText;
00047 
00050   bool    AddingEmptyItem; 
00051 
00054   bool    RemovingEmptyItem;
00055 
00057   bool    AddEnabled;
00058   bool    RemoveEnabled;
00059   bool    EditEnabled;
00060 
00062   bool    HasEmptyItem;
00063 };
00064 
00065 // --------------------------------------------------------------------------
00066 // ctkAddRemoveComboBoxPrivate methods
00067 
00068 // --------------------------------------------------------------------------
00069 ctkAddRemoveComboBoxPrivate::ctkAddRemoveComboBoxPrivate()
00070 {
00071   this->EmptyText = "None";
00072 
00073   this->AddingEmptyItem = false;
00074   this->RemovingEmptyItem = false;
00075 
00076   this->AddEnabled = true;
00077   this->RemoveEnabled = true;
00078   this->EditEnabled = true;
00079 
00080   this->HasEmptyItem = false;
00081 }
00082 
00083 // --------------------------------------------------------------------------
00084 void ctkAddRemoveComboBoxPrivate::insertEmptyItem()
00085 {
00086   if (!this->HasEmptyItem )
00087     {
00088     this->AddingEmptyItem = true;
00089     this->ComboBox->insertItem(0, this->EmptyText);
00090     this->AddingEmptyItem = false;
00091     this->HasEmptyItem = true;
00092     }
00093 }
00094 
00095 // --------------------------------------------------------------------------
00096 void ctkAddRemoveComboBoxPrivate::connectComboBox(QComboBox* comboBox)
00097 {
00098   CTK_P(ctkAddRemoveComboBox);
00099   QObject::connect(comboBox, SIGNAL(activated(int)),
00100                 p, SIGNAL(activated(int)));
00101   QObject::connect(comboBox, SIGNAL(currentIndexChanged(int)),
00102                 p, SIGNAL(currentIndexChanged(int)));
00103   /*
00104   this->connect(ctk_d()->ComboBox->model(),
00105   SIGNAL(rowsAboutToBeInserted(const QModelIndex & _parent, int start, int end )),
00106   SLOT(onRowsAboutToBeInserted(const QModelIndex & _parent, int start, int end )));
00107   */
00108   QObject::connect(comboBox->model(),
00109                 SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
00110                 p, SLOT(onRowsAboutToBeRemoved(const QModelIndex & , int , int  )));
00111 
00112   QObject::connect(comboBox->model(),
00113                 SIGNAL(rowsInserted(const QModelIndex &, int, int )),
00114                 p, SLOT(onRowsInserted(const QModelIndex &, int, int)));
00115   QObject::connect(comboBox->model(),
00116                 SIGNAL(rowsRemoved(const QModelIndex &, int, int)),
00117                 p, SLOT(onRowsRemoved(const QModelIndex &, int, int )));
00118 }
00119 
00120 // --------------------------------------------------------------------------
00121 // ctkAddRemoveComboBox methods
00122 
00123 // --------------------------------------------------------------------------
00124 ctkAddRemoveComboBox::ctkAddRemoveComboBox(QWidget* _parent) : Superclass(_parent)
00125 {
00126   CTK_INIT_PRIVATE(ctkAddRemoveComboBox);
00127   CTK_D(ctkAddRemoveComboBox);
00128   d->setupUi(this);
00129   
00130   // connect
00131   d->connectComboBox(d->ComboBox);
00132     
00133   this->connect(d->AddPushButton, SIGNAL(pressed()), SLOT(onAdd()));
00134   this->connect(d->RemovePushButton, SIGNAL(pressed()), SLOT(onRemove()));
00135   this->connect(d->EditPushButton, SIGNAL(pressed()), SLOT(onEdit()));
00136 
00137   // Add default 'empty item'
00138   d->insertEmptyItem();
00139 }
00140 
00141 // --------------------------------------------------------------------------
00142 void ctkAddRemoveComboBox::printAdditionalInfo()
00143 {
00144   CTK_D(ctkAddRemoveComboBox);
00145   qDebug() << "ctkAddRemoveComboBox:" << this << endl
00146            << " EmptyText:" << d->EmptyText << endl
00147            << " AddingEmptyItem:" << d->AddingEmptyItem << endl
00148            << " RemovingEmptyItem:" << d->RemovingEmptyItem << endl
00149            << " AddEnabled:" << d->AddEnabled << endl
00150            << " RemoveEnabled:" << d->RemoveEnabled << endl
00151            << " EditEnabled:" << d->EditEnabled << endl
00152            << " HasEmptyItem:" << d->HasEmptyItem;
00153 }
00154 
00155 // --------------------------------------------------------------------------
00156 void ctkAddRemoveComboBox::setComboBox(QComboBox* comboBox)
00157 {
00158   CTK_D(ctkAddRemoveComboBox);
00159   if ((comboBox == d->ComboBox) ||
00160       comboBox->count())
00161     {
00162     return;
00163     }
00164   
00165   QLayoutItem* oldComboBoxItem = this->layout()->takeAt(0);
00166   QComboBox* oldComboBox = qobject_cast<QComboBox*>(oldComboBoxItem->widget());
00167   comboBox->setSizePolicy(oldComboBox->sizePolicy());
00168   comboBox->setEnabled(this->comboBoxEnabled());
00169 
00170   delete oldComboBoxItem;
00171 
00172   dynamic_cast<QBoxLayout*>(this->layout())->insertWidget(0, comboBox);
00173   d->connectComboBox(comboBox);
00174   d->ComboBox = comboBox;
00175   delete oldComboBox;
00176 
00177   // Add default 'empty item'
00178   d->insertEmptyItem();
00179 }
00180 
00181 // --------------------------------------------------------------------------
00182 void ctkAddRemoveComboBox::setEmptyText(const QString& text)
00183 {
00184   CTK_D(ctkAddRemoveComboBox);
00185   
00186   if (d->HasEmptyItem)
00187     {
00188     Q_ASSERT(d->ComboBox->count() == 1);
00189     this->setItemText(0, text);
00190     }
00191   d->EmptyText = text;
00192 }
00193 
00194 // --------------------------------------------------------------------------
00195 CTK_GET_CXX(ctkAddRemoveComboBox, QString, emptyText, EmptyText);
00196 
00197 // --------------------------------------------------------------------------
00198 void ctkAddRemoveComboBox::onRowsInserted(const QModelIndex & _parent, int start, int end)
00199 {
00200   CTK_D(ctkAddRemoveComboBox);
00201   
00202   if (_parent != d->ComboBox->rootModelIndex())
00203     {// Rows that are to be added in the model are not displayed by the combobox
00204     return;
00205     }
00206     
00207   if (d->HasEmptyItem && !d->AddingEmptyItem)
00208     {
00209     // Remove the Empty item as some real items have been added
00210     d->HasEmptyItem = false;
00211     d->RemovingEmptyItem = true;
00212     d->ComboBox->removeItem(start == 0 ? end + 1 : 0);
00213     d->RemovingEmptyItem = false;
00214     
00215     if (d->RemoveEnabled)
00216       {
00217       d->RemovePushButton->setEnabled(true);
00218       }
00219     if (d->EditEnabled)
00220       {
00221       d->EditPushButton->setEnabled(true);
00222       }
00223     // Since we just removed the empty item, we need to shift the start/end items if needed
00224     if (start > 0 )
00225       {
00226       --start;
00227       --end;
00228       }
00229     }
00230 
00231   // Emit signal only if the items added is *NOT* the Empty item
00232   if (!d->AddingEmptyItem)
00233     {
00234     for (int i = start; i <= end; ++i)
00235       {
00236       emit this->itemAdded(i);
00237       }
00238     }
00239  }
00240 
00241 // --------------------------------------------------------------------------
00242 void ctkAddRemoveComboBox::onRowsAboutToBeRemoved(const QModelIndex & _parent, int start, int end)
00243 {
00244   CTK_D(ctkAddRemoveComboBox);
00245 
00246   if (_parent != d->ComboBox->rootModelIndex())
00247     {//rows that are to be added in the model are not displayed by the combobox
00248     return;
00249     }
00250 
00251   // if the user try to remove the Empty item, don't send event
00252   if (d->RemovingEmptyItem)
00253     {
00254     return;
00255     }
00256   for (int i = start; i <= end; ++i)
00257     {
00258     emit this->itemAboutToBeRemoved(i);
00259     }
00260 }
00261 
00262 
00263 // --------------------------------------------------------------------------
00264 void ctkAddRemoveComboBox::onRowsRemoved(const QModelIndex & _parent, int start, int end)
00265 {
00266   CTK_D(ctkAddRemoveComboBox);
00267   
00268   if (_parent != d->ComboBox->rootModelIndex())
00269     {//rows that are to be added in the model are not displayed by the combobox
00270     return;
00271     }
00272   // the combobox is now empty, add the EmptyItem if needed
00273   if (d->ComboBox->count() == 0)
00274     {
00275     // Add default 'empty item'
00276     d->insertEmptyItem();
00277    
00278     if (d->RemoveEnabled)
00279       {
00280       d->RemovePushButton->setEnabled(false);
00281       }
00282     if (d->EditEnabled)
00283       {
00284       d->EditPushButton->setEnabled(false);
00285       }
00286     }
00287 
00288   if (!d->RemovingEmptyItem)
00289     {
00290     for (int i = start; i <= end; ++i)
00291       {
00292       emit this->itemRemoved(i);
00293       }
00294     }
00295 }
00296 
00297 // --------------------------------------------------------------------------
00298 void ctkAddRemoveComboBox::setComboBoxEnabled(bool enable)
00299 {
00300   ctk_d()->ComboBox->setEnabled(enable);
00301 }
00302 
00303 // --------------------------------------------------------------------------
00304 bool ctkAddRemoveComboBox::comboBoxEnabled()const
00305 {
00306   //const cast as I'm not sure why isEnabledTo doesn't take a const
00307   return ctk_d()->ComboBox->isEnabledTo(const_cast<ctkAddRemoveComboBox*>(this));
00308 }
00309 
00310 // --------------------------------------------------------------------------
00311 void ctkAddRemoveComboBox::setAddEnabled(bool enable)
00312 {
00313   CTK_D(ctkAddRemoveComboBox);
00314   
00315   d->AddPushButton->setEnabled(enable);
00316   d->AddEnabled = enable;
00317 }
00318 
00319 // --------------------------------------------------------------------------
00320 bool ctkAddRemoveComboBox::addEnabled()const
00321 {
00322   return ctk_d()->AddEnabled;
00323 }
00324 
00325 // --------------------------------------------------------------------------
00326 void ctkAddRemoveComboBox::setRemoveEnabled(bool enable)
00327 {
00328   CTK_D(ctkAddRemoveComboBox);
00329   
00330   if (this->count() > 0)
00331     {
00332     d->RemovePushButton->setEnabled(enable);
00333     }
00334   d->RemoveEnabled = enable;
00335 }
00336 
00337 // --------------------------------------------------------------------------
00338 bool ctkAddRemoveComboBox::removeEnabled()const
00339 {
00340   return ctk_d()->RemoveEnabled;
00341 }
00342 
00343 // --------------------------------------------------------------------------
00344 void ctkAddRemoveComboBox::setEditEnabled(bool enable)
00345 {
00346   CTK_D(ctkAddRemoveComboBox);
00347   
00348   if (this->count() > 0)
00349     { 
00350     d->EditPushButton->setEnabled(enable);
00351     }
00352   d->EditEnabled = enable;
00353 }
00354 
00355 // --------------------------------------------------------------------------
00356 bool ctkAddRemoveComboBox::editEnabled()const
00357 {
00358   return ctk_d()->EditEnabled;
00359 }
00360 
00361 // --------------------------------------------------------------------------
00362 void ctkAddRemoveComboBox::onAdd()
00363 {
00364 }
00365 
00366 // --------------------------------------------------------------------------
00367 void ctkAddRemoveComboBox::onRemove()
00368 {
00369 }
00370 
00371 // --------------------------------------------------------------------------
00372 void ctkAddRemoveComboBox::onEdit()
00373 {
00374 }
00375 
00376 // --------------------------------------------------------------------------
00377 int ctkAddRemoveComboBox::count()const
00378 {
00379   CTK_D(const ctkAddRemoveComboBox);
00380   return (d->HasEmptyItem ? 0 : d->ComboBox->count());
00381 }
00382 
00383 // --------------------------------------------------------------------------
00384 CTK_GET_CXX(ctkAddRemoveComboBox, bool, empty, HasEmptyItem);
00385 
00386 // --------------------------------------------------------------------------
00387 void ctkAddRemoveComboBox::setCurrentIndex(int index)
00388 {
00389   return ctk_d()->ComboBox->setCurrentIndex(index);
00390 }
00391 
00392 // --------------------------------------------------------------------------
00393 void ctkAddRemoveComboBox::insertItem(int index, const QString &text, const QVariant &userDataVariable)
00394 {
00395   //qDebug() << __FUNCTION__ << " " << index <<  " " << text << " " << userDataVariable ;
00396   ctk_d()->ComboBox->insertItem(index, text, userDataVariable);
00397 }
00398 
00399 // --------------------------------------------------------------------------
00400 void ctkAddRemoveComboBox::insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userDataVariable)
00401 {
00402   ctk_d()->ComboBox->insertItem(index, icon, text, userDataVariable);
00403 }
00404 
00405 // --------------------------------------------------------------------------
00406 void ctkAddRemoveComboBox::insertItems(int index, const QStringList &texts)
00407 {
00408   ctk_d()->ComboBox->insertItems(index, texts);
00409 }
00410   
00411 // --------------------------------------------------------------------------
00412 int ctkAddRemoveComboBox::findText(const QString & text, Qt::MatchFlags flags)const
00413 {
00414   CTK_D(const ctkAddRemoveComboBox);
00415   
00416   if (d->HasEmptyItem)
00417     { // if the scene is empty, don't even try to find the text (it could be the
00418       // one of the EmptyText prop.
00419     return -1;
00420     }
00421   return d->ComboBox->findText(text, flags);
00422 }
00423 
00424 // --------------------------------------------------------------------------
00425 int ctkAddRemoveComboBox::findData(const QVariant &dataVariable, int role, Qt::MatchFlags flags)const
00426 {
00427   CTK_D(const ctkAddRemoveComboBox);
00428   
00429   if (d->HasEmptyItem)
00430     { // if the scene is empty, don't even try to find the dataVariable 
00431     return -1;
00432     }
00433   return d->ComboBox->findData(dataVariable, role, flags);
00434 }
00435 
00436 // --------------------------------------------------------------------------
00437 QString ctkAddRemoveComboBox::itemText(int index) const
00438 {
00439   CTK_D(const ctkAddRemoveComboBox);
00440   
00441   if (d->HasEmptyItem)
00442     {
00443     return QString();
00444     }
00445   return d->ComboBox->itemText(index);
00446 }
00447   
00448 // --------------------------------------------------------------------------
00449 QVariant ctkAddRemoveComboBox::itemData(int index, int role) const
00450 {
00451   CTK_D(const ctkAddRemoveComboBox);
00452   
00453   if (d->HasEmptyItem)
00454     {
00455     return QVariant();
00456     }
00457   return d->ComboBox->itemData(index,role);
00458 }
00459 
00460 // --------------------------------------------------------------------------
00461 void ctkAddRemoveComboBox::setItemText(int index, const QString& text)
00462 {
00463   CTK_D(ctkAddRemoveComboBox);
00464   
00465   if (d->HasEmptyItem)
00466     {
00467     return;
00468     }
00469   return d->ComboBox->setItemText(index, text);
00470 }
00471   
00472 // --------------------------------------------------------------------------
00473 void ctkAddRemoveComboBox::setItemData(int index, const QVariant& dataVariable, int role)
00474 {
00475   CTK_D(ctkAddRemoveComboBox);
00476   
00477   if (d->HasEmptyItem)
00478     {
00479     return;
00480     }
00481   d->ComboBox->setItemData(index, dataVariable, role);
00482 }
00483 
00484 // --------------------------------------------------------------------------
00485 int ctkAddRemoveComboBox::currentIndex() const
00486 {
00487   CTK_D(const ctkAddRemoveComboBox);
00488   
00489   return d->HasEmptyItem ? -1 : d->ComboBox->currentIndex();
00490 }
00491 
00492 // --------------------------------------------------------------------------
00493 void ctkAddRemoveComboBox::removeItem(int index)
00494 {
00495   CTK_D(ctkAddRemoveComboBox);
00496   
00497   if (d->HasEmptyItem)
00498     {
00499     return; 
00500     }
00501   d->ComboBox->removeItem(index);
00502 }
00503 
00504 // --------------------------------------------------------------------------
00505 void ctkAddRemoveComboBox::clear()
00506 {
00507   CTK_D(ctkAddRemoveComboBox);
00508   
00509   if (d->HasEmptyItem)
00510     {
00511     return;
00512     }
00513   d->ComboBox->clear();
00514 }
00515 
00516 // --------------------------------------------------------------------------
00517 QModelIndex ctkAddRemoveComboBox::rootModelIndex()const
00518 {
00519   return ctk_d()->ComboBox->rootModelIndex();
00520 }
00521 
00522 // --------------------------------------------------------------------------
00523 void ctkAddRemoveComboBox::setRootModelIndex(const QModelIndex& root)
00524 {
00525   ctk_d()->ComboBox->setRootModelIndex(root);
00526 }
00527 
00528 // --------------------------------------------------------------------------
00529 int ctkAddRemoveComboBox::modelColumn()const
00530 {
00531   return ctk_d()->ComboBox->modelColumn();
00532 }
00533 
00534 // --------------------------------------------------------------------------
00535 QAbstractItemModel* ctkAddRemoveComboBox::model()const
00536 {
00537   return ctk_d()->ComboBox->model();
00538 }
00539 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1