ctkCheckableComboBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 <QApplication>
  16. #include <QAbstractItemView>
  17. #include <QDebug>
  18. #include <QDesktopWidget>
  19. #include <QItemDelegate>
  20. #include <QLayout>
  21. #include <QMouseEvent>
  22. #include <QMenu>
  23. #include <QPainter>
  24. #include <QPointer>
  25. #include <QPushButton>
  26. #include <QStandardItemModel>
  27. #include <QStyle>
  28. #include <QStyleOptionButton>
  29. #include <QStylePainter>
  30. #include <QToolBar>
  31. // CTK includes
  32. #include "ctkCheckableComboBox.h"
  33. #include <ctkCheckableModelHelper.h>
  34. // Similar to QComboBoxDelegate
  35. class ctkComboBoxDelegate : public QItemDelegate
  36. {
  37. public:
  38. ctkComboBoxDelegate(QObject *parent, QComboBox *cmb)
  39. : QItemDelegate(parent), ComboBox(cmb)
  40. {}
  41. static bool isSeparator(const QModelIndex &index)
  42. {
  43. return index.data(Qt::AccessibleDescriptionRole).toString() == QLatin1String("separator");
  44. }
  45. static void setSeparator(QAbstractItemModel *model, const QModelIndex &index)
  46. {
  47. model->setData(index, QString::fromLatin1("separator"), Qt::AccessibleDescriptionRole);
  48. if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(model))
  49. {
  50. if (QStandardItem *item = m->itemFromIndex(index))
  51. {
  52. item->setFlags(item->flags() & ~(Qt::ItemIsSelectable|Qt::ItemIsEnabled));
  53. }
  54. }
  55. }
  56. protected:
  57. void paint(QPainter *painter,
  58. const QStyleOptionViewItem &option,
  59. const QModelIndex &index) const
  60. {
  61. if (isSeparator(index))
  62. {
  63. QRect rect = option.rect;
  64. if (const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option))
  65. {
  66. if (const QAbstractItemView *view = qobject_cast<const QAbstractItemView*>(v3->widget))
  67. {
  68. rect.setWidth(view->viewport()->width());
  69. }
  70. }
  71. QStyleOption opt;
  72. opt.rect = rect;
  73. this->ComboBox->style()->drawPrimitive(QStyle::PE_IndicatorToolBarSeparator, &opt, painter, this->ComboBox);
  74. }
  75. else
  76. {
  77. QItemDelegate::paint(painter, option, index);
  78. }
  79. }
  80. QSize sizeHint(const QStyleOptionViewItem &option,
  81. const QModelIndex &index) const
  82. {
  83. if (isSeparator(index))
  84. {
  85. int pm = this->ComboBox->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this->ComboBox);
  86. return QSize(pm, pm);
  87. }
  88. return this->QItemDelegate::sizeHint(option, index);
  89. }
  90. private:
  91. QComboBox* ComboBox;
  92. };
  93. //-----------------------------------------------------------------------------
  94. class ctkCheckableComboBoxPrivate
  95. {
  96. Q_DECLARE_PUBLIC(ctkCheckableComboBox);
  97. protected:
  98. ctkCheckableComboBox* const q_ptr;
  99. QModelIndexList checkedIndexes()const;
  100. QModelIndexList uncheckedIndexes()const;
  101. public:
  102. ctkCheckableComboBoxPrivate(ctkCheckableComboBox& object);
  103. void init();
  104. QModelIndexList cachedCheckedIndexes()const;
  105. void updateCheckedList();
  106. ctkCheckableModelHelper* CheckableModelHelper;
  107. bool MouseButtonPressed;
  108. private:
  109. QModelIndexList persistentIndexesToModelIndexes(
  110. const QList<QPersistentModelIndex>& persistentModels)const;
  111. QList<QPersistentModelIndex> modelIndexesToPersistentIndexes(
  112. const QModelIndexList& modelIndexes)const;
  113. mutable QList<QPersistentModelIndex> CheckedList;
  114. };
  115. //-----------------------------------------------------------------------------
  116. ctkCheckableComboBoxPrivate::ctkCheckableComboBoxPrivate(ctkCheckableComboBox& object)
  117. : q_ptr(&object)
  118. {
  119. this->CheckableModelHelper = 0;
  120. this->MouseButtonPressed = false;
  121. }
  122. //-----------------------------------------------------------------------------
  123. void ctkCheckableComboBoxPrivate::init()
  124. {
  125. Q_Q(ctkCheckableComboBox);
  126. this->CheckableModelHelper = new ctkCheckableModelHelper(Qt::Horizontal, q);
  127. this->CheckableModelHelper->setForceCheckability(true);
  128. q->setCheckableModel(q->model());
  129. q->view()->installEventFilter(q);
  130. q->view()->viewport()->installEventFilter(q);
  131. // QCleanLooksStyle uses a delegate that doesn't show the checkboxes in the
  132. // popup list.
  133. q->setItemDelegate(new ctkComboBoxDelegate(q->view(), q));
  134. }
  135. //-----------------------------------------------------------------------------
  136. void ctkCheckableComboBoxPrivate::updateCheckedList()
  137. {
  138. Q_Q(ctkCheckableComboBox);
  139. QList<QPersistentModelIndex> newCheckedPersistentList =
  140. this->modelIndexesToPersistentIndexes(this->checkedIndexes());
  141. if (newCheckedPersistentList == this->CheckedList)
  142. {
  143. return;
  144. }
  145. this->CheckedList = newCheckedPersistentList;
  146. emit q->checkedIndexesChanged();
  147. }
  148. //-----------------------------------------------------------------------------
  149. QList<QPersistentModelIndex> ctkCheckableComboBoxPrivate
  150. ::modelIndexesToPersistentIndexes(const QModelIndexList& indexes)const
  151. {
  152. QList<QPersistentModelIndex> res;
  153. foreach(const QModelIndex& index, indexes)
  154. {
  155. QPersistentModelIndex persistent(index);
  156. if (persistent.isValid())
  157. {
  158. res << persistent;
  159. }
  160. }
  161. return res;
  162. }
  163. //-----------------------------------------------------------------------------
  164. QModelIndexList ctkCheckableComboBoxPrivate
  165. ::persistentIndexesToModelIndexes(
  166. const QList<QPersistentModelIndex>& indexes)const
  167. {
  168. QModelIndexList res;
  169. foreach(const QPersistentModelIndex& index, indexes)
  170. {
  171. if (index.isValid())
  172. {
  173. res << index;
  174. }
  175. }
  176. return res;
  177. }
  178. //-----------------------------------------------------------------------------
  179. QModelIndexList ctkCheckableComboBoxPrivate::cachedCheckedIndexes()const
  180. {
  181. return this->persistentIndexesToModelIndexes(this->CheckedList);
  182. }
  183. //-----------------------------------------------------------------------------
  184. QModelIndexList ctkCheckableComboBoxPrivate::checkedIndexes()const
  185. {
  186. Q_Q(const ctkCheckableComboBox);
  187. QModelIndex startIndex = q->model()->index(0,0, q->rootModelIndex());
  188. return q->model()->match(
  189. startIndex, Qt::CheckStateRole,
  190. static_cast<int>(Qt::Checked), -1, Qt::MatchRecursive);
  191. }
  192. //-----------------------------------------------------------------------------
  193. QModelIndexList ctkCheckableComboBoxPrivate::uncheckedIndexes()const
  194. {
  195. Q_Q(const ctkCheckableComboBox);
  196. QModelIndex startIndex = q->model()->index(0,0, q->rootModelIndex());
  197. return q->model()->match(
  198. startIndex, Qt::CheckStateRole,
  199. static_cast<int>(Qt::Unchecked), -1, Qt::MatchRecursive);
  200. }
  201. //-----------------------------------------------------------------------------
  202. ctkCheckableComboBox::ctkCheckableComboBox(QWidget* parentWidget)
  203. : QComboBox(parentWidget)
  204. , d_ptr(new ctkCheckableComboBoxPrivate(*this))
  205. {
  206. Q_D(ctkCheckableComboBox);
  207. d->init();
  208. }
  209. //-----------------------------------------------------------------------------
  210. ctkCheckableComboBox::~ctkCheckableComboBox()
  211. {
  212. }
  213. //-----------------------------------------------------------------------------
  214. bool ctkCheckableComboBox::eventFilter(QObject *o, QEvent *e)
  215. {
  216. Q_D(ctkCheckableComboBox);
  217. switch (e->type())
  218. {
  219. case QEvent::MouseButtonPress:
  220. {
  221. if (this->view()->isVisible())
  222. {
  223. d->MouseButtonPressed = true;
  224. }
  225. break;
  226. }
  227. case QEvent::MouseButtonRelease:
  228. {
  229. QMouseEvent *m = static_cast<QMouseEvent *>(e);
  230. if (this->view()->isVisible() &&
  231. this->view()->rect().contains(m->pos()) &&
  232. this->view()->currentIndex().isValid()
  233. //&& !blockMouseReleaseTimer.isActive()
  234. && (this->view()->currentIndex().flags() & Qt::ItemIsEnabled)
  235. && (this->view()->currentIndex().flags() & Qt::ItemIsSelectable))
  236. {
  237. // The signal to open the menu is fired when the mouse button is
  238. // pressed, we don't want to toggle the item under the mouse cursor
  239. // when the button used to open the popup is released.
  240. if (d->MouseButtonPressed)
  241. {
  242. // make the item current, it will then call QComboBox::update (and
  243. // repaint) when the current index data is changed (checkstate
  244. // toggled fires dataChanged signal which is observed).
  245. this->setCurrentIndex(this->view()->currentIndex().row());
  246. d->CheckableModelHelper->toggleCheckState(this->view()->currentIndex());
  247. }
  248. d->MouseButtonPressed = false;
  249. return true;
  250. }
  251. d->MouseButtonPressed = false;
  252. break;
  253. }
  254. default:
  255. break;
  256. }
  257. return this->QComboBox::eventFilter(o, e);
  258. }
  259. //-----------------------------------------------------------------------------
  260. void ctkCheckableComboBox::setCheckableModel(QAbstractItemModel* newModel)
  261. {
  262. Q_D(ctkCheckableComboBox);
  263. this->disconnect(this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  264. this, SLOT(onDataChanged(QModelIndex,QModelIndex)));
  265. if (newModel != this->model())
  266. {
  267. this->setModel(newModel);
  268. }
  269. this->connect(this->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  270. this, SLOT(onDataChanged(QModelIndex,QModelIndex)));
  271. d->CheckableModelHelper->setModel(newModel);
  272. d->updateCheckedList();
  273. }
  274. //-----------------------------------------------------------------------------
  275. QAbstractItemModel* ctkCheckableComboBox::checkableModel()const
  276. {
  277. return this->model();
  278. }
  279. //-----------------------------------------------------------------------------
  280. QModelIndexList ctkCheckableComboBox::checkedIndexes()const
  281. {
  282. Q_D(const ctkCheckableComboBox);
  283. return d->cachedCheckedIndexes();
  284. }
  285. //-----------------------------------------------------------------------------
  286. bool ctkCheckableComboBox::allChecked()const
  287. {
  288. Q_D(const ctkCheckableComboBox);
  289. return d->uncheckedIndexes().count() == 0;
  290. }
  291. //-----------------------------------------------------------------------------
  292. bool ctkCheckableComboBox::noneChecked()const
  293. {
  294. Q_D(const ctkCheckableComboBox);
  295. return d->cachedCheckedIndexes().count() == 0;
  296. }
  297. //-----------------------------------------------------------------------------
  298. void ctkCheckableComboBox::setCheckState(const QModelIndex& index, Qt::CheckState check)
  299. {
  300. Q_D(ctkCheckableComboBox);
  301. return d->CheckableModelHelper->setCheckState(index, check);
  302. }
  303. //-----------------------------------------------------------------------------
  304. Qt::CheckState ctkCheckableComboBox::checkState(const QModelIndex& index)const
  305. {
  306. Q_D(const ctkCheckableComboBox);
  307. return d->CheckableModelHelper->checkState(index);
  308. }
  309. //-----------------------------------------------------------------------------
  310. void ctkCheckableComboBox::onDataChanged(const QModelIndex& start, const QModelIndex& end)
  311. {
  312. Q_D(ctkCheckableComboBox);
  313. Q_UNUSED(start);
  314. Q_UNUSED(end);
  315. d->updateCheckedList();
  316. }
  317. //-----------------------------------------------------------------------------
  318. void ctkCheckableComboBox::paintEvent(QPaintEvent *)
  319. {
  320. Q_D(ctkCheckableComboBox);
  321. QStylePainter painter(this);
  322. painter.setPen(palette().color(QPalette::Text));
  323. // draw the combobox frame, focusrect and selected etc.
  324. QStyleOptionComboBox opt;
  325. this->initStyleOption(&opt);
  326. if (this->allChecked())
  327. {
  328. opt.currentText = "All";
  329. opt.currentIcon = QIcon();
  330. }
  331. else if (this->noneChecked())
  332. {
  333. opt.currentText = "None";
  334. opt.currentIcon = QIcon();
  335. }
  336. else
  337. {
  338. //search the checked items
  339. QModelIndexList indexes = d->cachedCheckedIndexes();
  340. if (indexes.count() == 1)
  341. {
  342. opt.currentText = this->model()->data(indexes[0], Qt::DisplayRole).toString();
  343. opt.currentIcon = qvariant_cast<QIcon>(this->model()->data(indexes[0], Qt::DecorationRole));
  344. }
  345. else
  346. {
  347. QStringList indexesText;
  348. foreach(QModelIndex checkedIndex, indexes)
  349. {
  350. indexesText << this->model()->data(checkedIndex, Qt::DisplayRole).toString();
  351. }
  352. opt.currentText = indexesText.join(", ");
  353. opt.currentIcon = QIcon();
  354. }
  355. }
  356. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  357. // draw the icon and text
  358. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  359. }