ctkCheckableComboBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. public:
  102. ctkCheckableComboBoxPrivate(ctkCheckableComboBox& object);
  103. void init();
  104. void updateCheckedList();
  105. ctkCheckableModelHelper* CheckableModelHelper;
  106. QModelIndexList CheckedList;
  107. };
  108. //-----------------------------------------------------------------------------
  109. ctkCheckableComboBoxPrivate::ctkCheckableComboBoxPrivate(ctkCheckableComboBox& object)
  110. : q_ptr(&object)
  111. {
  112. this->CheckableModelHelper = 0;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void ctkCheckableComboBoxPrivate::init()
  116. {
  117. Q_Q(ctkCheckableComboBox);
  118. this->CheckableModelHelper = new ctkCheckableModelHelper(Qt::Horizontal, q);
  119. this->CheckableModelHelper->setForceCheckability(true);
  120. q->setCheckableModel(q->model());
  121. q->view()->installEventFilter(q);
  122. q->view()->viewport()->installEventFilter(q);
  123. // QCleanLooksStyle uses a delegate that doesn't show the checkboxes in the
  124. // popup list.
  125. q->setItemDelegate(new ctkComboBoxDelegate(q->view(), q));
  126. }
  127. //-----------------------------------------------------------------------------
  128. void ctkCheckableComboBoxPrivate::updateCheckedList()
  129. {
  130. Q_Q(ctkCheckableComboBox);
  131. QModelIndexList newCheckedList = this->checkedIndexes();
  132. if (newCheckedList == this->CheckedList)
  133. {
  134. return;
  135. }
  136. this->CheckedList = newCheckedList;
  137. emit q->checkedIndexesChanged();
  138. }
  139. //-----------------------------------------------------------------------------
  140. QModelIndexList ctkCheckableComboBoxPrivate::checkedIndexes()const
  141. {
  142. Q_Q(const ctkCheckableComboBox);
  143. return q->model()->match(
  144. q->model()->index(0,0), Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchRecursive);
  145. }
  146. //-----------------------------------------------------------------------------
  147. ctkCheckableComboBox::ctkCheckableComboBox(QWidget* parentWidget)
  148. : QComboBox(parentWidget)
  149. , d_ptr(new ctkCheckableComboBoxPrivate(*this))
  150. {
  151. Q_D(ctkCheckableComboBox);
  152. d->init();
  153. }
  154. //-----------------------------------------------------------------------------
  155. ctkCheckableComboBox::~ctkCheckableComboBox()
  156. {
  157. }
  158. //-----------------------------------------------------------------------------
  159. bool ctkCheckableComboBox::eventFilter(QObject *o, QEvent *e)
  160. {
  161. Q_D(ctkCheckableComboBox);
  162. switch (e->type())
  163. {
  164. case QEvent::MouseButtonRelease:
  165. {
  166. QMouseEvent *m = static_cast<QMouseEvent *>(e);
  167. if (this->view()->isVisible() &&
  168. this->view()->rect().contains(m->pos()) &&
  169. this->view()->currentIndex().isValid()
  170. //&& !blockMouseReleaseTimer.isActive()
  171. && (this->view()->currentIndex().flags() & Qt::ItemIsEnabled)
  172. && (this->view()->currentIndex().flags() & Qt::ItemIsSelectable))
  173. {
  174. // make the item current, it will then call QComboBox::update (and
  175. // repaint) when the current index data is changed (checkstate toggled).
  176. this->setCurrentIndex(this->view()->currentIndex().row());
  177. d->CheckableModelHelper->toggleCheckState(this->view()->currentIndex());
  178. return true;
  179. }
  180. break;
  181. }
  182. default:
  183. break;
  184. }
  185. return this->QComboBox::eventFilter(o, e);
  186. }
  187. //-----------------------------------------------------------------------------
  188. void ctkCheckableComboBox::setCheckableModel(QAbstractItemModel* newModel)
  189. {
  190. Q_D(ctkCheckableComboBox);
  191. this->disconnect(this->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  192. this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
  193. if (newModel != this->model())
  194. {
  195. this->setModel(newModel);
  196. }
  197. this->connect(this->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  198. this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
  199. d->CheckableModelHelper->setModel(newModel);
  200. d->updateCheckedList();
  201. }
  202. //-----------------------------------------------------------------------------
  203. QAbstractItemModel* ctkCheckableComboBox::checkableModel()const
  204. {
  205. return this->model();
  206. }
  207. //-----------------------------------------------------------------------------
  208. QModelIndexList ctkCheckableComboBox::checkedIndexes()const
  209. {
  210. Q_D(const ctkCheckableComboBox);
  211. return d->CheckedList;
  212. }
  213. //-----------------------------------------------------------------------------
  214. bool ctkCheckableComboBox::allChecked()const
  215. {
  216. Q_D(const ctkCheckableComboBox);
  217. return d->CheckableModelHelper->headerCheckState(0) == Qt::Checked;
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool ctkCheckableComboBox::noneChecked()const
  221. {
  222. Q_D(const ctkCheckableComboBox);
  223. return d->CheckableModelHelper->headerCheckState(0) == Qt::Unchecked;
  224. }
  225. //-----------------------------------------------------------------------------
  226. void ctkCheckableComboBox::setCheckState(const QModelIndex& index, Qt::CheckState check)
  227. {
  228. Q_D(ctkCheckableComboBox);
  229. return d->CheckableModelHelper->setCheckState(index, check);
  230. }
  231. //-----------------------------------------------------------------------------
  232. Qt::CheckState ctkCheckableComboBox::checkState(const QModelIndex& index)const
  233. {
  234. Q_D(const ctkCheckableComboBox);
  235. return d->CheckableModelHelper->checkState(index);
  236. }
  237. //-----------------------------------------------------------------------------
  238. void ctkCheckableComboBox::onDataChanged(const QModelIndex& start, const QModelIndex& end)
  239. {
  240. Q_D(ctkCheckableComboBox);
  241. Q_UNUSED(start);
  242. Q_UNUSED(end);
  243. d->updateCheckedList();
  244. }
  245. //-----------------------------------------------------------------------------
  246. void ctkCheckableComboBox::paintEvent(QPaintEvent *)
  247. {
  248. QStylePainter painter(this);
  249. painter.setPen(palette().color(QPalette::Text));
  250. // draw the combobox frame, focusrect and selected etc.
  251. QStyleOptionComboBox opt;
  252. this->initStyleOption(&opt);
  253. if (this->allChecked())
  254. {
  255. opt.currentText = "All";
  256. opt.currentIcon = QIcon();
  257. }
  258. else if (this->noneChecked())
  259. {
  260. opt.currentText = "None";
  261. opt.currentIcon = QIcon();
  262. }
  263. else
  264. {
  265. //search the checked items
  266. QModelIndexList indexes = this->checkedIndexes();
  267. if (indexes.count() == 1)
  268. {
  269. opt.currentText = this->model()->data(indexes[0], Qt::DisplayRole).toString();
  270. opt.currentIcon = qvariant_cast<QIcon>(this->model()->data(indexes[0], Qt::DecorationRole));
  271. }
  272. else
  273. {
  274. QStringList indexesText;
  275. foreach(QModelIndex checkedIndex, indexes)
  276. {
  277. indexesText << this->model()->data(checkedIndex, Qt::DisplayRole).toString();
  278. }
  279. opt.currentText = indexesText.join(", ");
  280. opt.currentIcon = QIcon();
  281. }
  282. }
  283. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  284. // draw the icon and text
  285. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  286. }