| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 | /*=========================================================================  Library:   CTK  Copyright (c) Kitware Inc.   All rights reserved.  Distributed under a BSD License. See LICENSE.txt file.  This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the above copyright notice for more information.=========================================================================*/// Qt includes#include <QDebug>// CTK includes#include <ctkPimpl.h>#include "ctkAddRemoveComboBox.h"#include "ui_ctkAddRemoveComboBox.h"//-----------------------------------------------------------------------------class ctkAddRemoveComboBoxPrivate : public ctkPrivate<ctkAddRemoveComboBox>,                                    public Ui_ctkAddRemoveComboBox{public:  ctkAddRemoveComboBoxPrivate();  ///   /// Insert 'None' item  /// Note: Also make sure that no signal is emitted while the item is inserted  /// That function doesn't prevent from inserting multiple time the 'None' item  void insertEmptyItem();  void connectComboBox(QComboBox* combobox);public:  /// Empty item  QString EmptyText;  /// Set to true when inserting the 'None' item.  /// Will prevent the itemAdded signal from being sent  bool    AddingEmptyItem;   /// Set to true when removing the 'None' item.  /// Will prevent the itemRemoved signal from being sent  bool    RemovingEmptyItem;  /// Actions state  bool    AddEnabled;  bool    RemoveEnabled;  bool    EditEnabled;  /// If true, it means there is no item beside of the 'None' one  bool    HasEmptyItem;};// --------------------------------------------------------------------------// ctkAddRemoveComboBoxPrivate methods// --------------------------------------------------------------------------ctkAddRemoveComboBoxPrivate::ctkAddRemoveComboBoxPrivate(){  this->EmptyText = "None";  this->AddingEmptyItem = false;  this->RemovingEmptyItem = false;  this->AddEnabled = true;  this->RemoveEnabled = true;  this->EditEnabled = true;  this->HasEmptyItem = false;}// --------------------------------------------------------------------------void ctkAddRemoveComboBoxPrivate::insertEmptyItem(){  if (!this->HasEmptyItem )    {    this->AddingEmptyItem = true;    this->ComboBox->insertItem(0, this->EmptyText);    this->AddingEmptyItem = false;    this->HasEmptyItem = true;    }}// --------------------------------------------------------------------------void ctkAddRemoveComboBoxPrivate::connectComboBox(QComboBox* comboBox){  CTK_P(ctkAddRemoveComboBox);  QObject::connect(comboBox, SIGNAL(activated(int)),                p, SIGNAL(activated(int)));  QObject::connect(comboBox, SIGNAL(currentIndexChanged(int)),                p, SIGNAL(currentIndexChanged(int)));  /*  this->connect(ctk_d()->ComboBox->model(),  SIGNAL(rowsAboutToBeInserted(const QModelIndex & _parent, int start, int end )),  SLOT(onRowsAboutToBeInserted(const QModelIndex & _parent, int start, int end )));  */  QObject::connect(comboBox->model(),                SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),                p, SLOT(onRowsAboutToBeRemoved(const QModelIndex & , int , int  )));  QObject::connect(comboBox->model(),                SIGNAL(rowsInserted(const QModelIndex &, int, int )),                p, SLOT(onRowsInserted(const QModelIndex &, int, int)));  QObject::connect(comboBox->model(),                SIGNAL(rowsRemoved(const QModelIndex &, int, int)),                p, SLOT(onRowsRemoved(const QModelIndex &, int, int )));}// --------------------------------------------------------------------------// ctkAddRemoveComboBox methods// --------------------------------------------------------------------------ctkAddRemoveComboBox::ctkAddRemoveComboBox(QWidget* _parent) : Superclass(_parent){  CTK_INIT_PRIVATE(ctkAddRemoveComboBox);  CTK_D(ctkAddRemoveComboBox);  d->setupUi(this);    // connect  d->connectComboBox(d->ComboBox);      this->connect(d->AddPushButton, SIGNAL(pressed()), SLOT(onAdd()));  this->connect(d->RemovePushButton, SIGNAL(pressed()), SLOT(onRemove()));  this->connect(d->EditPushButton, SIGNAL(pressed()), SLOT(onEdit()));  // Add default 'empty item'  d->insertEmptyItem();}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::printAdditionalInfo(){  CTK_D(ctkAddRemoveComboBox);  qDebug() << "ctkAddRemoveComboBox:" << this << endl           << " EmptyText:" << d->EmptyText << endl           << " AddingEmptyItem:" << d->AddingEmptyItem << endl           << " RemovingEmptyItem:" << d->RemovingEmptyItem << endl           << " AddEnabled:" << d->AddEnabled << endl           << " RemoveEnabled:" << d->RemoveEnabled << endl           << " EditEnabled:" << d->EditEnabled << endl           << " HasEmptyItem:" << d->HasEmptyItem;}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setComboBox(QComboBox* comboBox){  CTK_D(ctkAddRemoveComboBox);  if ((comboBox == d->ComboBox) ||      comboBox->count())    {    return;    }    QLayoutItem* oldComboBoxItem = this->layout()->takeAt(0);  QComboBox* oldComboBox = qobject_cast<QComboBox*>(oldComboBoxItem->widget());  comboBox->setSizePolicy(oldComboBox->sizePolicy());  comboBox->setEnabled(this->comboBoxEnabled());  delete oldComboBoxItem;  dynamic_cast<QBoxLayout*>(this->layout())->insertWidget(0, comboBox);  d->connectComboBox(comboBox);  d->ComboBox = comboBox;  delete oldComboBox;  // Add default 'empty item'  d->insertEmptyItem();}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setEmptyText(const QString& text){  CTK_D(ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    Q_ASSERT(d->ComboBox->count() == 1);    this->setItemText(0, text);    }  d->EmptyText = text;}// --------------------------------------------------------------------------CTK_GET_CXX(ctkAddRemoveComboBox, QString, emptyText, EmptyText);// --------------------------------------------------------------------------void ctkAddRemoveComboBox::onRowsInserted(const QModelIndex & _parent, int start, int end){  CTK_D(ctkAddRemoveComboBox);    if (_parent != d->ComboBox->rootModelIndex())    {// Rows that are to be added in the model are not displayed by the combobox    return;    }      if (d->HasEmptyItem && !d->AddingEmptyItem)    {    // Remove the Empty item as some real items have been added    d->HasEmptyItem = false;    d->RemovingEmptyItem = true;    d->ComboBox->removeItem(start == 0 ? end + 1 : 0);    d->RemovingEmptyItem = false;        if (d->RemoveEnabled)      {      d->RemovePushButton->setEnabled(true);      }    if (d->EditEnabled)      {      d->EditPushButton->setEnabled(true);      }    // Since we just removed the empty item, we need to shift the start/end items if needed    if (start > 0 )      {      --start;      --end;      }    }  // Emit signal only if the items added is *NOT* the Empty item  if (!d->AddingEmptyItem)    {    for (int i = start; i <= end; ++i)      {      emit this->itemAdded(i);      }    } }// --------------------------------------------------------------------------void ctkAddRemoveComboBox::onRowsAboutToBeRemoved(const QModelIndex & _parent, int start, int end){  CTK_D(ctkAddRemoveComboBox);  if (_parent != d->ComboBox->rootModelIndex())    {//rows that are to be added in the model are not displayed by the combobox    return;    }  // if the user try to remove the Empty item, don't send event  if (d->RemovingEmptyItem)    {    return;    }  for (int i = start; i <= end; ++i)    {    emit this->itemAboutToBeRemoved(i);    }}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::onRowsRemoved(const QModelIndex & _parent, int start, int end){  CTK_D(ctkAddRemoveComboBox);    if (_parent != d->ComboBox->rootModelIndex())    {//rows that are to be added in the model are not displayed by the combobox    return;    }  // the combobox is now empty, add the EmptyItem if needed  if (d->ComboBox->count() == 0)    {    // Add default 'empty item'    d->insertEmptyItem();       if (d->RemoveEnabled)      {      d->RemovePushButton->setEnabled(false);      }    if (d->EditEnabled)      {      d->EditPushButton->setEnabled(false);      }    }  if (!d->RemovingEmptyItem)    {    for (int i = start; i <= end; ++i)      {      emit this->itemRemoved(i);      }    }}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setComboBoxEnabled(bool enable){  ctk_d()->ComboBox->setEnabled(enable);}// --------------------------------------------------------------------------bool ctkAddRemoveComboBox::comboBoxEnabled()const{  //const cast as I'm not sure why isEnabledTo doesn't take a const  return ctk_d()->ComboBox->isEnabledTo(const_cast<ctkAddRemoveComboBox*>(this));}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setAddEnabled(bool enable){  CTK_D(ctkAddRemoveComboBox);    d->AddPushButton->setEnabled(enable);  d->AddEnabled = enable;}// --------------------------------------------------------------------------bool ctkAddRemoveComboBox::addEnabled()const{  return ctk_d()->AddEnabled;}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setRemoveEnabled(bool enable){  CTK_D(ctkAddRemoveComboBox);    if (this->count() > 0)    {    d->RemovePushButton->setEnabled(enable);    }  d->RemoveEnabled = enable;}// --------------------------------------------------------------------------bool ctkAddRemoveComboBox::removeEnabled()const{  return ctk_d()->RemoveEnabled;}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setEditEnabled(bool enable){  CTK_D(ctkAddRemoveComboBox);    if (this->count() > 0)    {     d->EditPushButton->setEnabled(enable);    }  d->EditEnabled = enable;}// --------------------------------------------------------------------------bool ctkAddRemoveComboBox::editEnabled()const{  return ctk_d()->EditEnabled;}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::onAdd(){}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::onRemove(){}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::onEdit(){}// --------------------------------------------------------------------------int ctkAddRemoveComboBox::count()const{  CTK_D(const ctkAddRemoveComboBox);  return (d->HasEmptyItem ? 0 : d->ComboBox->count());}// --------------------------------------------------------------------------CTK_GET_CXX(ctkAddRemoveComboBox, bool, empty, HasEmptyItem);// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setCurrentIndex(int index){  return ctk_d()->ComboBox->setCurrentIndex(index);}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::insertItem(int index, const QString &text, const QVariant &userDataVariable){  //qDebug() << __FUNCTION__ << " " << index <<  " " << text << " " << userDataVariable ;  ctk_d()->ComboBox->insertItem(index, text, userDataVariable);}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userDataVariable){  ctk_d()->ComboBox->insertItem(index, icon, text, userDataVariable);}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::insertItems(int index, const QStringList &texts){  ctk_d()->ComboBox->insertItems(index, texts);}  // --------------------------------------------------------------------------int ctkAddRemoveComboBox::findText(const QString & text, Qt::MatchFlags flags)const{  CTK_D(const ctkAddRemoveComboBox);    if (d->HasEmptyItem)    { // if the scene is empty, don't even try to find the text (it could be the      // one of the EmptyText prop.    return -1;    }  return d->ComboBox->findText(text, flags);}// --------------------------------------------------------------------------int ctkAddRemoveComboBox::findData(const QVariant &dataVariable, int role, Qt::MatchFlags flags)const{  CTK_D(const ctkAddRemoveComboBox);    if (d->HasEmptyItem)    { // if the scene is empty, don't even try to find the dataVariable     return -1;    }  return d->ComboBox->findData(dataVariable, role, flags);}// --------------------------------------------------------------------------QString ctkAddRemoveComboBox::itemText(int index) const{  CTK_D(const ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    return QString();    }  return d->ComboBox->itemText(index);}  // --------------------------------------------------------------------------QVariant ctkAddRemoveComboBox::itemData(int index, int role) const{  CTK_D(const ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    return QVariant();    }  return d->ComboBox->itemData(index,role);}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setItemText(int index, const QString& text){  CTK_D(ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    return;    }  return d->ComboBox->setItemText(index, text);}  // --------------------------------------------------------------------------void ctkAddRemoveComboBox::setItemData(int index, const QVariant& dataVariable, int role){  CTK_D(ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    return;    }  d->ComboBox->setItemData(index, dataVariable, role);}// --------------------------------------------------------------------------int ctkAddRemoveComboBox::currentIndex() const{  CTK_D(const ctkAddRemoveComboBox);    return d->HasEmptyItem ? -1 : d->ComboBox->currentIndex();}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::removeItem(int index){  CTK_D(ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    return;     }  d->ComboBox->removeItem(index);}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::clear(){  CTK_D(ctkAddRemoveComboBox);    if (d->HasEmptyItem)    {    return;    }  d->ComboBox->clear();}// --------------------------------------------------------------------------QModelIndex ctkAddRemoveComboBox::rootModelIndex()const{  return ctk_d()->ComboBox->rootModelIndex();}// --------------------------------------------------------------------------void ctkAddRemoveComboBox::setRootModelIndex(const QModelIndex& root){  ctk_d()->ComboBox->setRootModelIndex(root);}// --------------------------------------------------------------------------int ctkAddRemoveComboBox::modelColumn()const{  return ctk_d()->ComboBox->modelColumn();}// --------------------------------------------------------------------------QAbstractItemModel* ctkAddRemoveComboBox::model()const{  return ctk_d()->ComboBox->model();}
 |