ctkAddRemoveComboBox.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. // CTK includes
  17. #include <ctkPimpl.h>
  18. #include "ctkAddRemoveComboBox.h"
  19. #include "ui_ctkAddRemoveComboBox.h"
  20. //-----------------------------------------------------------------------------
  21. class ctkAddRemoveComboBoxPrivate : public Ui_ctkAddRemoveComboBox
  22. {
  23. Q_DECLARE_PUBLIC(ctkAddRemoveComboBox);
  24. protected:
  25. ctkAddRemoveComboBox* const q_ptr;
  26. public:
  27. ctkAddRemoveComboBoxPrivate(ctkAddRemoveComboBox& object);
  28. ///
  29. /// Insert 'None' item
  30. /// Note: Also make sure that no signal is emitted while the item is inserted
  31. /// That function doesn't prevent from inserting multiple time the 'None' item
  32. void insertEmptyItem();
  33. void connectComboBox(QComboBox* combobox);
  34. public:
  35. /// Empty item
  36. QString EmptyText;
  37. /// Set to true when inserting the 'None' item.
  38. /// Will prevent the itemAdded signal from being sent
  39. bool AddingEmptyItem;
  40. /// Set to true when removing the 'None' item.
  41. /// Will prevent the itemRemoved signal from being sent
  42. bool RemovingEmptyItem;
  43. /// Actions state
  44. bool AddEnabled;
  45. bool RemoveEnabled;
  46. bool EditEnabled;
  47. /// If true, it means there is no item beside of the 'None' one
  48. bool HasEmptyItem;
  49. };
  50. // --------------------------------------------------------------------------
  51. // ctkAddRemoveComboBoxPrivate methods
  52. // --------------------------------------------------------------------------
  53. ctkAddRemoveComboBoxPrivate::ctkAddRemoveComboBoxPrivate(ctkAddRemoveComboBox& object)
  54. :q_ptr(&object)
  55. {
  56. this->EmptyText = "None";
  57. this->AddingEmptyItem = false;
  58. this->RemovingEmptyItem = false;
  59. this->AddEnabled = true;
  60. this->RemoveEnabled = true;
  61. this->EditEnabled = true;
  62. this->HasEmptyItem = false;
  63. }
  64. // --------------------------------------------------------------------------
  65. void ctkAddRemoveComboBoxPrivate::insertEmptyItem()
  66. {
  67. if (!this->HasEmptyItem )
  68. {
  69. this->AddingEmptyItem = true;
  70. this->ComboBox->insertItem(0, this->EmptyText);
  71. this->AddingEmptyItem = false;
  72. this->HasEmptyItem = true;
  73. }
  74. }
  75. // --------------------------------------------------------------------------
  76. void ctkAddRemoveComboBoxPrivate::connectComboBox(QComboBox* comboBox)
  77. {
  78. Q_Q(ctkAddRemoveComboBox);
  79. QObject::connect(comboBox, SIGNAL(activated(int)),
  80. q, SIGNAL(activated(int)));
  81. QObject::connect(comboBox, SIGNAL(currentIndexChanged(int)),
  82. q, SIGNAL(currentIndexChanged(int)));
  83. /*
  84. this->connect(d->ComboBox->model(),
  85. SIGNAL(rowsAboutToBeInserted(QModelIndex&_parent,int start,int end)),
  86. SLOT(onRowsAboutToBeInserted(QModelIndex&_parent,int start,int end)));
  87. */
  88. QObject::connect(comboBox->model(),
  89. SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
  90. q, SLOT(onRowsAboutToBeRemoved(QModelIndex,int,int)));
  91. QObject::connect(comboBox->model(),
  92. SIGNAL(rowsInserted(QModelIndex,int,int)),
  93. q, SLOT(onRowsInserted(QModelIndex,int,int)));
  94. QObject::connect(comboBox->model(),
  95. SIGNAL(rowsRemoved(QModelIndex,int,int)),
  96. q, SLOT(onRowsRemoved(QModelIndex,int,int)));
  97. }
  98. // --------------------------------------------------------------------------
  99. // ctkAddRemoveComboBox methods
  100. // --------------------------------------------------------------------------
  101. ctkAddRemoveComboBox::ctkAddRemoveComboBox(QWidget* _parent) : Superclass(_parent)
  102. , d_ptr(new ctkAddRemoveComboBoxPrivate(*this))
  103. {
  104. Q_D(ctkAddRemoveComboBox);
  105. d->setupUi(this);
  106. // connect
  107. d->connectComboBox(d->ComboBox);
  108. this->connect(d->AddPushButton, SIGNAL(pressed()), SLOT(onAdd()));
  109. this->connect(d->RemovePushButton, SIGNAL(pressed()), SLOT(onRemove()));
  110. this->connect(d->EditPushButton, SIGNAL(pressed()), SLOT(onEdit()));
  111. // Add default 'empty item'
  112. d->insertEmptyItem();
  113. }
  114. // --------------------------------------------------------------------------
  115. ctkAddRemoveComboBox::~ctkAddRemoveComboBox()
  116. {
  117. }
  118. // --------------------------------------------------------------------------
  119. void ctkAddRemoveComboBox::printAdditionalInfo()
  120. {
  121. Q_D(ctkAddRemoveComboBox);
  122. qDebug() << "ctkAddRemoveComboBox:" << this << endl
  123. << " EmptyText:" << d->EmptyText << endl
  124. << " AddingEmptyItem:" << d->AddingEmptyItem << endl
  125. << " RemovingEmptyItem:" << d->RemovingEmptyItem << endl
  126. << " AddEnabled:" << d->AddEnabled << endl
  127. << " RemoveEnabled:" << d->RemoveEnabled << endl
  128. << " EditEnabled:" << d->EditEnabled << endl
  129. << " HasEmptyItem:" << d->HasEmptyItem;
  130. }
  131. // --------------------------------------------------------------------------
  132. void ctkAddRemoveComboBox::setComboBox(QComboBox* comboBox)
  133. {
  134. Q_D(ctkAddRemoveComboBox);
  135. if ((comboBox == d->ComboBox) ||
  136. comboBox->count())
  137. {
  138. return;
  139. }
  140. QLayoutItem* oldComboBoxItem = this->layout()->takeAt(0);
  141. QComboBox* oldComboBox = qobject_cast<QComboBox*>(oldComboBoxItem->widget());
  142. comboBox->setSizePolicy(oldComboBox->sizePolicy());
  143. comboBox->setEnabled(this->comboBoxEnabled());
  144. delete oldComboBoxItem;
  145. dynamic_cast<QBoxLayout*>(this->layout())->insertWidget(0, comboBox);
  146. d->connectComboBox(comboBox);
  147. d->ComboBox = comboBox;
  148. delete oldComboBox;
  149. // Add default 'empty item'
  150. d->insertEmptyItem();
  151. }
  152. // --------------------------------------------------------------------------
  153. void ctkAddRemoveComboBox::setEmptyText(const QString& text)
  154. {
  155. Q_D(ctkAddRemoveComboBox);
  156. if (d->HasEmptyItem)
  157. {
  158. Q_ASSERT(d->ComboBox->count() == 1);
  159. this->setItemText(0, text);
  160. }
  161. d->EmptyText = text;
  162. }
  163. // --------------------------------------------------------------------------
  164. CTK_GET_CPP(ctkAddRemoveComboBox, QString, emptyText, EmptyText);
  165. // --------------------------------------------------------------------------
  166. void ctkAddRemoveComboBox::onRowsInserted(const QModelIndex & _parent, int start, int end)
  167. {
  168. Q_D(ctkAddRemoveComboBox);
  169. if (_parent != d->ComboBox->rootModelIndex())
  170. {// Rows that are to be added in the model are not displayed by the combobox
  171. return;
  172. }
  173. if (d->HasEmptyItem && !d->AddingEmptyItem)
  174. {
  175. // Remove the Empty item as some real items have been added
  176. d->HasEmptyItem = false;
  177. d->RemovingEmptyItem = true;
  178. d->ComboBox->removeItem(start == 0 ? end + 1 : 0);
  179. d->RemovingEmptyItem = false;
  180. if (d->RemoveEnabled)
  181. {
  182. d->RemovePushButton->setEnabled(true);
  183. }
  184. if (d->EditEnabled)
  185. {
  186. d->EditPushButton->setEnabled(true);
  187. }
  188. // Since we just removed the empty item, we need to shift the start/end items if needed
  189. if (start > 0 )
  190. {
  191. --start;
  192. --end;
  193. }
  194. }
  195. // Emit signal only if the items added is *NOT* the Empty item
  196. if (!d->AddingEmptyItem)
  197. {
  198. for (int i = start; i <= end; ++i)
  199. {
  200. emit this->itemAdded(i);
  201. }
  202. }
  203. }
  204. // --------------------------------------------------------------------------
  205. void ctkAddRemoveComboBox::onRowsAboutToBeRemoved(const QModelIndex & _parent, int start, int end)
  206. {
  207. Q_D(ctkAddRemoveComboBox);
  208. if (_parent != d->ComboBox->rootModelIndex())
  209. {//rows that are to be added in the model are not displayed by the combobox
  210. return;
  211. }
  212. // if the user try to remove the Empty item, don't send event
  213. if (d->RemovingEmptyItem)
  214. {
  215. return;
  216. }
  217. for (int i = start; i <= end; ++i)
  218. {
  219. emit this->itemAboutToBeRemoved(i);
  220. }
  221. }
  222. // --------------------------------------------------------------------------
  223. void ctkAddRemoveComboBox::onRowsRemoved(const QModelIndex & _parent, int start, int end)
  224. {
  225. Q_D(ctkAddRemoveComboBox);
  226. if (_parent != d->ComboBox->rootModelIndex())
  227. {//rows that are to be added in the model are not displayed by the combobox
  228. return;
  229. }
  230. // the combobox is now empty, add the EmptyItem if needed
  231. if (d->ComboBox->count() == 0)
  232. {
  233. // Add default 'empty item'
  234. d->insertEmptyItem();
  235. if (d->RemoveEnabled)
  236. {
  237. d->RemovePushButton->setEnabled(false);
  238. }
  239. if (d->EditEnabled)
  240. {
  241. d->EditPushButton->setEnabled(false);
  242. }
  243. }
  244. if (!d->RemovingEmptyItem)
  245. {
  246. for (int i = start; i <= end; ++i)
  247. {
  248. emit this->itemRemoved(i);
  249. }
  250. }
  251. }
  252. // --------------------------------------------------------------------------
  253. void ctkAddRemoveComboBox::setComboBoxEnabled(bool enable)
  254. {
  255. Q_D(ctkAddRemoveComboBox);
  256. d->ComboBox->setEnabled(enable);
  257. }
  258. // --------------------------------------------------------------------------
  259. bool ctkAddRemoveComboBox::comboBoxEnabled()const
  260. {
  261. Q_D(const ctkAddRemoveComboBox);
  262. //const cast as I'm not sure why isEnabledTo doesn't take a const
  263. return d->ComboBox->isEnabledTo(const_cast<ctkAddRemoveComboBox*>(this));
  264. }
  265. // --------------------------------------------------------------------------
  266. void ctkAddRemoveComboBox::setAddEnabled(bool enable)
  267. {
  268. Q_D(ctkAddRemoveComboBox);
  269. d->AddPushButton->setEnabled(enable);
  270. d->AddEnabled = enable;
  271. }
  272. // --------------------------------------------------------------------------
  273. bool ctkAddRemoveComboBox::addEnabled()const
  274. {
  275. Q_D(const ctkAddRemoveComboBox);
  276. return d->AddEnabled;
  277. }
  278. // --------------------------------------------------------------------------
  279. void ctkAddRemoveComboBox::setRemoveEnabled(bool enable)
  280. {
  281. Q_D(ctkAddRemoveComboBox);
  282. if (this->count() > 0)
  283. {
  284. d->RemovePushButton->setEnabled(enable);
  285. }
  286. d->RemoveEnabled = enable;
  287. }
  288. // --------------------------------------------------------------------------
  289. bool ctkAddRemoveComboBox::removeEnabled()const
  290. {
  291. Q_D(const ctkAddRemoveComboBox);
  292. return d->RemoveEnabled;
  293. }
  294. // --------------------------------------------------------------------------
  295. void ctkAddRemoveComboBox::setEditEnabled(bool enable)
  296. {
  297. Q_D(ctkAddRemoveComboBox);
  298. if (this->count() > 0)
  299. {
  300. d->EditPushButton->setEnabled(enable);
  301. }
  302. d->EditEnabled = enable;
  303. }
  304. // --------------------------------------------------------------------------
  305. bool ctkAddRemoveComboBox::editEnabled()const
  306. {
  307. Q_D(const ctkAddRemoveComboBox);
  308. return d->EditEnabled;
  309. }
  310. // --------------------------------------------------------------------------
  311. void ctkAddRemoveComboBox::onAdd()
  312. {
  313. }
  314. // --------------------------------------------------------------------------
  315. void ctkAddRemoveComboBox::onRemove()
  316. {
  317. }
  318. // --------------------------------------------------------------------------
  319. void ctkAddRemoveComboBox::onEdit()
  320. {
  321. }
  322. // --------------------------------------------------------------------------
  323. int ctkAddRemoveComboBox::count()const
  324. {
  325. Q_D(const ctkAddRemoveComboBox);
  326. return (d->HasEmptyItem ? 0 : d->ComboBox->count());
  327. }
  328. // --------------------------------------------------------------------------
  329. CTK_GET_CPP(ctkAddRemoveComboBox, bool, empty, HasEmptyItem);
  330. // --------------------------------------------------------------------------
  331. void ctkAddRemoveComboBox::setCurrentIndex(int index)
  332. {
  333. Q_D(ctkAddRemoveComboBox);
  334. return d->ComboBox->setCurrentIndex(index);
  335. }
  336. // --------------------------------------------------------------------------
  337. void ctkAddRemoveComboBox::insertItem(int index, const QString &text, const QVariant &userDataVariable)
  338. {
  339. Q_D(ctkAddRemoveComboBox);
  340. //qDebug() << __FUNCTION__ << " " << index << " " << text << " " << userDataVariable ;
  341. d->ComboBox->insertItem(index, text, userDataVariable);
  342. }
  343. // --------------------------------------------------------------------------
  344. void ctkAddRemoveComboBox::insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userDataVariable)
  345. {
  346. Q_D(ctkAddRemoveComboBox);
  347. d->ComboBox->insertItem(index, icon, text, userDataVariable);
  348. }
  349. // --------------------------------------------------------------------------
  350. void ctkAddRemoveComboBox::insertItems(int index, const QStringList &texts)
  351. {
  352. Q_D(ctkAddRemoveComboBox);
  353. d->ComboBox->insertItems(index, texts);
  354. }
  355. // --------------------------------------------------------------------------
  356. int ctkAddRemoveComboBox::findText(const QString & text, Qt::MatchFlags flags)const
  357. {
  358. Q_D(const ctkAddRemoveComboBox);
  359. if (d->HasEmptyItem)
  360. { // if the scene is empty, don't even try to find the text (it could be the
  361. // one of the EmptyText prop.
  362. return -1;
  363. }
  364. return d->ComboBox->findText(text, flags);
  365. }
  366. // --------------------------------------------------------------------------
  367. int ctkAddRemoveComboBox::findData(const QVariant &dataVariable, int role, Qt::MatchFlags flags)const
  368. {
  369. Q_D(const ctkAddRemoveComboBox);
  370. if (d->HasEmptyItem)
  371. { // if the scene is empty, don't even try to find the dataVariable
  372. return -1;
  373. }
  374. return d->ComboBox->findData(dataVariable, role, flags);
  375. }
  376. // --------------------------------------------------------------------------
  377. QString ctkAddRemoveComboBox::itemText(int index) const
  378. {
  379. Q_D(const ctkAddRemoveComboBox);
  380. if (d->HasEmptyItem)
  381. {
  382. return QString();
  383. }
  384. return d->ComboBox->itemText(index);
  385. }
  386. // --------------------------------------------------------------------------
  387. QVariant ctkAddRemoveComboBox::itemData(int index, int role) const
  388. {
  389. Q_D(const ctkAddRemoveComboBox);
  390. if (d->HasEmptyItem)
  391. {
  392. return QVariant();
  393. }
  394. return d->ComboBox->itemData(index,role);
  395. }
  396. // --------------------------------------------------------------------------
  397. void ctkAddRemoveComboBox::setItemText(int index, const QString& text)
  398. {
  399. Q_D(ctkAddRemoveComboBox);
  400. if (d->HasEmptyItem)
  401. {
  402. return;
  403. }
  404. return d->ComboBox->setItemText(index, text);
  405. }
  406. // --------------------------------------------------------------------------
  407. void ctkAddRemoveComboBox::setItemData(int index, const QVariant& dataVariable, int role)
  408. {
  409. Q_D(ctkAddRemoveComboBox);
  410. if (d->HasEmptyItem)
  411. {
  412. return;
  413. }
  414. d->ComboBox->setItemData(index, dataVariable, role);
  415. }
  416. // --------------------------------------------------------------------------
  417. int ctkAddRemoveComboBox::currentIndex() const
  418. {
  419. Q_D(const ctkAddRemoveComboBox);
  420. return d->HasEmptyItem ? -1 : d->ComboBox->currentIndex();
  421. }
  422. // --------------------------------------------------------------------------
  423. void ctkAddRemoveComboBox::removeItem(int index)
  424. {
  425. Q_D(ctkAddRemoveComboBox);
  426. if (d->HasEmptyItem)
  427. {
  428. return;
  429. }
  430. d->ComboBox->removeItem(index);
  431. }
  432. // --------------------------------------------------------------------------
  433. void ctkAddRemoveComboBox::clear()
  434. {
  435. Q_D(ctkAddRemoveComboBox);
  436. if (d->HasEmptyItem)
  437. {
  438. return;
  439. }
  440. d->ComboBox->clear();
  441. }
  442. // --------------------------------------------------------------------------
  443. QModelIndex ctkAddRemoveComboBox::rootModelIndex()const
  444. {
  445. Q_D(const ctkAddRemoveComboBox);
  446. return d->ComboBox->rootModelIndex();
  447. }
  448. // --------------------------------------------------------------------------
  449. void ctkAddRemoveComboBox::setRootModelIndex(const QModelIndex& root)
  450. {
  451. Q_D(ctkAddRemoveComboBox);
  452. d->ComboBox->setRootModelIndex(root);
  453. }
  454. // --------------------------------------------------------------------------
  455. int ctkAddRemoveComboBox::modelColumn()const
  456. {
  457. Q_D(const ctkAddRemoveComboBox);
  458. return d->ComboBox->modelColumn();
  459. }
  460. // --------------------------------------------------------------------------
  461. QAbstractItemModel* ctkAddRemoveComboBox::model()const
  462. {
  463. Q_D(const ctkAddRemoveComboBox);
  464. return d->ComboBox->model();
  465. }