ctkCheckableComboBox.cpp 11 KB

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