ctkAddRemoveComboBox.cpp 15 KB

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