ctkCheckableHeaderView.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. /*=========================================================================
  11. Program: ParaView
  12. Module: $RCSfile: pqCheckableHeaderView.cxx,v $
  13. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  14. All rights reserved.
  15. ParaView is a free software; you can redistribute it and/or modify it
  16. under the terms of the ParaView license version 1.2.
  17. See License_v1.2.txt for the full ParaView license.
  18. A copy of this license can be obtained by contacting
  19. Kitware Inc.
  20. 28 Corporate Drive
  21. Clifton Park, NY 12065
  22. USA
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. =========================================================================*/
  35. // Qt includes
  36. #include <QAbstractItemModel>
  37. #include <QApplication>
  38. #include <QDebug>
  39. #include <QEvent>
  40. #include <QList>
  41. #include <QMouseEvent>
  42. #include <QPainter>
  43. #include <QPixmap>
  44. #include <QStyle>
  45. // CTK includes
  46. #include "ctkCheckableHeaderView.h"
  47. #include "ctkCheckBoxPixmaps.h"
  48. //-----------------------------------------------------------------------------
  49. class ctkCheckableHeaderViewPrivate: public ctkPrivate<ctkCheckableHeaderView>
  50. {
  51. CTK_DECLARE_PUBLIC(ctkCheckableHeaderView)
  52. public:
  53. ctkCheckableHeaderViewPrivate();
  54. ~ctkCheckableHeaderViewPrivate();
  55. void init();
  56. int Pressed;
  57. ctkCheckBoxPixmaps* CheckBoxPixmaps;
  58. bool HeaderIsUpdating;
  59. bool ItemsAreUpdating;
  60. bool PropagateToItems;
  61. };
  62. //----------------------------------------------------------------------------
  63. ctkCheckableHeaderViewPrivate::ctkCheckableHeaderViewPrivate()
  64. {
  65. this->HeaderIsUpdating = false;
  66. this->ItemsAreUpdating = false;
  67. this->CheckBoxPixmaps = 0;
  68. this->Pressed = -1;
  69. this->PropagateToItems = false;
  70. }
  71. //-----------------------------------------------------------------------------
  72. ctkCheckableHeaderViewPrivate::~ctkCheckableHeaderViewPrivate()
  73. {
  74. if (this->CheckBoxPixmaps)
  75. {
  76. delete this->CheckBoxPixmaps;
  77. this->CheckBoxPixmaps = 0;
  78. }
  79. }
  80. //----------------------------------------------------------------------------
  81. void ctkCheckableHeaderViewPrivate::init()
  82. {
  83. CTK_P(ctkCheckableHeaderView);
  84. this->CheckBoxPixmaps = new ctkCheckBoxPixmaps(p);
  85. }
  86. //----------------------------------------------------------------------------
  87. ctkCheckableHeaderView::ctkCheckableHeaderView(
  88. Qt::Orientation orient, QWidget *widgetParent)
  89. : QHeaderView(orient, widgetParent)
  90. {
  91. CTK_INIT_PRIVATE(ctkCheckableHeaderView);
  92. ctk_d()->init();
  93. if(widgetParent)
  94. {
  95. // Listen for focus change events.
  96. widgetParent->installEventFilter(this);
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. ctkCheckableHeaderView::~ctkCheckableHeaderView()
  101. {
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool ctkCheckableHeaderView::eventFilter(QObject *, QEvent *e)
  105. {
  106. if(e->type() != QEvent::FocusIn &&
  107. e->type() != QEvent::FocusOut)
  108. {
  109. return false;
  110. }
  111. this->updateHeaders();
  112. return false;
  113. }
  114. //-----------------------------------------------------------------------------
  115. void ctkCheckableHeaderView::setModel(QAbstractItemModel *newModel)
  116. {
  117. CTK_D(ctkCheckableHeaderView);
  118. QAbstractItemModel *current = this->model();
  119. if (current == newModel)
  120. {
  121. return;
  122. }
  123. if(current)
  124. {
  125. this->disconnect(
  126. current, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
  127. this, SLOT(updateHeaderData(Qt::Orientation, int, int)));
  128. this->disconnect(
  129. current, SIGNAL(modelReset()),
  130. this, SLOT(updateHeaders()));
  131. this->disconnect(
  132. current, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  133. this, SLOT(updateHeadersFromItems(const QModelIndex&, const QModelIndex&)));
  134. this->disconnect(
  135. current, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
  136. this, SLOT(insertHeaderSection(const QModelIndex &, int, int)));
  137. this->disconnect(
  138. current, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  139. this, SLOT(insertHeaderSection(const QModelIndex &, int, int)));
  140. }
  141. this->QHeaderView::setModel(newModel);
  142. if(newModel)
  143. {
  144. this->connect(
  145. newModel, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
  146. this, SLOT(updateHeaderData(Qt::Orientation, int, int)));
  147. this->connect(
  148. newModel, SIGNAL(modelReset()),
  149. this, SLOT(updateHeaders()));
  150. if (d->PropagateToItems)
  151. {
  152. this->connect(
  153. newModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  154. this, SLOT(updateHeadersFromItems(const QModelIndex&, const QModelIndex&)));
  155. }
  156. if(this->orientation() == Qt::Horizontal)
  157. {
  158. this->connect(
  159. newModel, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
  160. this, SLOT(insertHeaderSection(const QModelIndex &, int, int)));
  161. }
  162. else
  163. {
  164. this->connect(
  165. newModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  166. this, SLOT(insertHeaderSection(const QModelIndex &, int, int)));
  167. }
  168. }
  169. // Determine which sections are clickable and setup the icons.
  170. this->updateHeaders();
  171. }
  172. //-----------------------------------------------------------------------------
  173. void ctkCheckableHeaderView::setRootIndex(const QModelIndex &index)
  174. {
  175. this->QHeaderView::setRootIndex(index);
  176. this->updateHeaders();
  177. }
  178. //-----------------------------------------------------------------------------
  179. void ctkCheckableHeaderView::setPropagateToItems(bool propagate)
  180. {
  181. CTK_D(ctkCheckableHeaderView);
  182. d->PropagateToItems = propagate;
  183. if (!this->model())
  184. {
  185. return;
  186. }
  187. if (propagate)
  188. {
  189. this->connect(
  190. this->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  191. this, SLOT(updateHeadersFromItems(const QModelIndex&, const QModelIndex&)));
  192. }
  193. else
  194. {
  195. this->disconnect(
  196. this->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  197. this, SLOT(updateHeadersFromItems(const QModelIndex&, const QModelIndex&)));
  198. }
  199. }
  200. //-----------------------------------------------------------------------------
  201. bool ctkCheckableHeaderView::propagateToItems()const
  202. {
  203. CTK_D(const ctkCheckableHeaderView);
  204. return d->PropagateToItems;
  205. }
  206. //-----------------------------------------------------------------------------
  207. void ctkCheckableHeaderView::toggleCheckState(int section)
  208. {
  209. // If the section is checkable, toggle the check state.
  210. if(!this->isCheckable(section))
  211. {
  212. return;
  213. }
  214. // I've no strong feeling to turn the state checked or unchecked when the
  215. // state is PartiallyChecked.
  216. this->setCheckState(section, this->checkState(section) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
  217. }
  218. //-----------------------------------------------------------------------------
  219. void ctkCheckableHeaderView::setCheckState(int section, Qt::CheckState checkState)
  220. {
  221. // If the section is checkable, toggle the check state.
  222. QAbstractItemModel *current = this->model();
  223. if(current == 0)
  224. {
  225. return;
  226. }
  227. // If the state is unchecked or partially checked, the state
  228. // should be changed to checked.
  229. current->setHeaderData(section, this->orientation(),
  230. checkState, Qt::CheckStateRole);
  231. }
  232. //-----------------------------------------------------------------------------
  233. void ctkCheckableHeaderView::updateHeaderData(Qt::Orientation orient,
  234. int first, int last)
  235. {
  236. if(orient != this->orientation())
  237. {
  238. return;
  239. }
  240. this->updateHeaders(first, last);
  241. }
  242. //-----------------------------------------------------------------------------
  243. void ctkCheckableHeaderView::updateHeaders(int first, int last)
  244. {
  245. CTK_D(ctkCheckableHeaderView);
  246. if(d->HeaderIsUpdating)
  247. {
  248. return;
  249. }
  250. d->HeaderIsUpdating = true;
  251. QAbstractItemModel *current = this->model();
  252. Q_ASSERT(current);
  253. first = qBound(0, first, this->count() -1);
  254. last = qBound(0, last, this->count() -1);
  255. bool active = true;
  256. if(this->parentWidget())
  257. {
  258. active = this->parentWidget()->hasFocus();
  259. }
  260. int maxJ = this->orientation() == Qt::Horizontal ?
  261. current->rowCount() : current->columnCount();
  262. for(int i = first; i <= last; i++)
  263. {
  264. QVariant decoration;
  265. Qt::CheckState checkState;
  266. if (this->checkState(i, checkState))
  267. {
  268. decoration = d->CheckBoxPixmaps->getPixmap(checkState, active);
  269. if (d->PropagateToItems &&
  270. checkState != Qt::PartiallyChecked &&
  271. !d->ItemsAreUpdating)
  272. {
  273. for (int j = 0 ; j < maxJ; ++j)
  274. {
  275. QModelIndex index = this->orientation() == Qt::Horizontal ?
  276. current->index(j, i,this->rootIndex()) :
  277. current->index(i, j,this->rootIndex()) ;
  278. current->setData(index, checkState, Qt::CheckStateRole);
  279. }
  280. }
  281. }
  282. current->setHeaderData(i, this->orientation(), decoration,
  283. Qt::DecorationRole);
  284. }
  285. d->HeaderIsUpdating = false;
  286. }
  287. //-----------------------------------------------------------------------------
  288. void ctkCheckableHeaderView::updateHeadersFromItems(const QModelIndex & topLeft,
  289. const QModelIndex & bottomRight)
  290. {
  291. CTK_D(ctkCheckableHeaderView);
  292. if(d->ItemsAreUpdating || !d->PropagateToItems ||
  293. topLeft.parent() != this->rootIndex())
  294. {
  295. return;
  296. }
  297. d->ItemsAreUpdating = true;
  298. QAbstractItemModel *current = this->model();
  299. Q_ASSERT(current);
  300. int lastI;
  301. int lastJ;
  302. if (this->orientation() == Qt::Horizontal)
  303. {
  304. lastI = this->count();
  305. lastJ = current->rowCount();
  306. }
  307. else
  308. {
  309. lastI = this->count();
  310. lastJ = current->columnCount();
  311. }
  312. for(int i = 0; i <= lastI; ++i)
  313. {
  314. Qt::CheckState sectionState;
  315. if (!this->checkState(i, sectionState))
  316. {
  317. continue;
  318. }
  319. bool itemIsCheckable = false;
  320. // get the first item state
  321. Qt::CheckState itemsState;
  322. int j = 0;
  323. for ( ; j <= lastJ; ++j)
  324. {
  325. QModelIndex index = this->orientation() == Qt::Horizontal ?
  326. current->index(j, i, topLeft.parent()) :
  327. current->index(i, j, topLeft.parent());
  328. itemsState = static_cast<Qt::CheckState>(
  329. index.data(Qt::CheckStateRole).toInt(&itemIsCheckable));
  330. if (itemIsCheckable)
  331. {
  332. break;
  333. }
  334. }
  335. if (j > lastJ)
  336. {// the first item check state couldn't be found
  337. continue;
  338. }
  339. // check the other states to make sure it is the same state
  340. for (; j <= lastJ; ++j)
  341. {
  342. QModelIndex index = this->orientation() == Qt::Horizontal ?
  343. current->index(j, i, topLeft.parent()) :
  344. current->index(i, j, topLeft.parent());
  345. Qt::CheckState itemState =
  346. static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt(&itemIsCheckable));
  347. if (itemIsCheckable && itemState!= itemsState)
  348. {// there is at least 1 item with a different state
  349. this->setCheckState(i, Qt::PartiallyChecked);
  350. break;
  351. }
  352. }
  353. if (j > lastJ)
  354. {
  355. this->setCheckState(i, itemsState);
  356. }
  357. }
  358. d->ItemsAreUpdating = false;
  359. }
  360. //-----------------------------------------------------------------------------
  361. void ctkCheckableHeaderView::insertHeaderSection(const QModelIndex &parentIndex,
  362. int first, int last)
  363. {
  364. if (this->rootIndex() != parentIndex)
  365. {
  366. return;
  367. }
  368. this->updateHeaders(first, last);
  369. }
  370. //-----------------------------------------------------------------------------
  371. bool ctkCheckableHeaderView::isCheckable(int section)const
  372. {
  373. return !this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).isNull();
  374. }
  375. //-----------------------------------------------------------------------------
  376. Qt::CheckState ctkCheckableHeaderView::checkState(int section)const
  377. {
  378. return static_cast<Qt::CheckState>(
  379. this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt());
  380. }
  381. //-----------------------------------------------------------------------------
  382. bool ctkCheckableHeaderView::checkState(int section, Qt::CheckState& checkState)const
  383. {
  384. bool checkable = false;
  385. checkState = static_cast<Qt::CheckState>(
  386. this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt(&checkable));
  387. return checkable;
  388. }
  389. //-----------------------------------------------------------------------------
  390. void ctkCheckableHeaderView::mousePressEvent(QMouseEvent *e)
  391. {
  392. CTK_D(ctkCheckableHeaderView);
  393. if (e->button() != Qt::LeftButton ||
  394. d->Pressed >= 0)
  395. {
  396. d->Pressed = -1;
  397. this->QHeaderView::mousePressEvent(e);
  398. return;
  399. }
  400. d->Pressed = -1;
  401. //check if the check box is pressed
  402. int pos = this->orientation() == Qt::Horizontal ? e->x() : e->y();
  403. int section = this->logicalIndexAt(pos);
  404. if (this->isCheckable(section) &&
  405. this->isPointInCheckBox(section, e->pos()))
  406. {
  407. d->Pressed = section;
  408. }
  409. this->QHeaderView::mousePressEvent(e);
  410. }
  411. //-----------------------------------------------------------------------------
  412. void ctkCheckableHeaderView::mouseReleaseEvent(QMouseEvent *e)
  413. {
  414. CTK_D(ctkCheckableHeaderView);
  415. if (e->button() != Qt::LeftButton ||
  416. d->Pressed < 0)
  417. {
  418. d->Pressed = -1;
  419. this->QHeaderView::mouseReleaseEvent(e);
  420. return;
  421. }
  422. //check if the check box is pressed
  423. int pos = this->orientation() == Qt::Horizontal ? e->x() : e->y();
  424. int section = this->logicalIndexAt(pos);
  425. if (section == d->Pressed &&
  426. this->isPointInCheckBox(section, e->pos()))
  427. {
  428. d->Pressed = -1;
  429. this->toggleCheckState(section);
  430. }
  431. this->QHeaderView::mousePressEvent(e);
  432. }
  433. //-----------------------------------------------------------------------------
  434. bool ctkCheckableHeaderView::isPointInCheckBox(int section, QPoint pos)const
  435. {
  436. QRect sectionRect = this->orientation() == Qt::Horizontal ?
  437. QRect(this->sectionPosition(section), 0,
  438. this->sectionSize(section), this->height()):
  439. QRect(0, this->sectionPosition(section),
  440. this->width(), this->sectionSize(section));
  441. QStyleOptionHeader opt;
  442. this->initStyleOption(&opt);
  443. this->initStyleSectionOption(&opt, section, sectionRect);
  444. QRect headerLabelRect = this->style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
  445. // from qcommonstyle.cpp:1541
  446. if (opt.icon.isNull())
  447. {
  448. return false;
  449. }
  450. QPixmap pixmap
  451. = opt.icon.pixmap(this->style()->pixelMetric(QStyle::PM_SmallIconSize),
  452. (opt.state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled);
  453. QRect aligned = this->style()->alignedRect(opt.direction, QFlag(opt.iconAlignment),
  454. pixmap.size(), headerLabelRect);
  455. QRect inter = aligned.intersected(headerLabelRect);
  456. return inter.contains(pos);
  457. }
  458. //-----------------------------------------------------------------------------
  459. void ctkCheckableHeaderView::initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const
  460. {
  461. // from qheaderview.cpp:paintsection
  462. QStyle::State state = QStyle::State_None;
  463. if (this->isEnabled())
  464. {
  465. state |= QStyle::State_Enabled;
  466. }
  467. if (this->window()->isActiveWindow())
  468. {
  469. state |= QStyle::State_Active;
  470. }
  471. if (this->isSortIndicatorShown() &&
  472. this->sortIndicatorSection() == section)
  473. {
  474. option->sortIndicator = (this->sortIndicatorOrder() == Qt::AscendingOrder)
  475. ? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp;
  476. }
  477. // setup the style option structure
  478. QVariant textAlignment =
  479. this->model()->headerData(section, this->orientation(),
  480. Qt::TextAlignmentRole);
  481. option->rect = rect;
  482. option->section = section;
  483. option->state |= state;
  484. option->textAlignment = Qt::Alignment(textAlignment.isValid()
  485. ? Qt::Alignment(textAlignment.toInt())
  486. : this->defaultAlignment());
  487. option->iconAlignment = Qt::AlignVCenter;
  488. option->text = this->model()->headerData(section, this->orientation(),
  489. Qt::DisplayRole).toString();
  490. if (this->textElideMode() != Qt::ElideNone)
  491. {
  492. option->text = option->fontMetrics.elidedText(option->text, this->textElideMode() , rect.width() - 4);
  493. }
  494. QVariant variant = this->model()->headerData(section, this->orientation(),
  495. Qt::DecorationRole);
  496. option->icon = qvariant_cast<QIcon>(variant);
  497. if (option->icon.isNull())
  498. {
  499. option->icon = qvariant_cast<QPixmap>(variant);
  500. }
  501. QVariant foregroundBrush = this->model()->headerData(section, this->orientation(),
  502. Qt::ForegroundRole);
  503. if (qVariantCanConvert<QBrush>(foregroundBrush))
  504. {
  505. option->palette.setBrush(QPalette::ButtonText, qvariant_cast<QBrush>(foregroundBrush));
  506. }
  507. //QPointF oldBO = painter->brushOrigin();
  508. QVariant backgroundBrush = this->model()->headerData(section, this->orientation(),
  509. Qt::BackgroundRole);
  510. if (qVariantCanConvert<QBrush>(backgroundBrush))
  511. {
  512. option->palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
  513. option->palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
  514. //painter->setBrushOrigin(option->rect.topLeft());
  515. }
  516. // the section position
  517. int visual = this->visualIndex(section);
  518. Q_ASSERT(visual != -1);
  519. if (this->count() == 1)
  520. {
  521. option->position = QStyleOptionHeader::OnlyOneSection;
  522. }
  523. else if (visual == 0)
  524. {
  525. option->position = QStyleOptionHeader::Beginning;
  526. }
  527. else if (visual == this->count() - 1)
  528. {
  529. option->position = QStyleOptionHeader::End;
  530. }
  531. else
  532. {
  533. option->position = QStyleOptionHeader::Middle;
  534. }
  535. option->orientation = this->orientation();
  536. /* the selected position
  537. bool previousSelected = d->isSectionSelected(this->logicalIndex(visual - 1));
  538. bool nextSelected = d->isSectionSelected(this->logicalIndex(visual + 1));
  539. if (previousSelected && nextSelected)
  540. option->selectedPosition = QStyleOptionHeader::NextAndPreviousAreSelected;
  541. else if (previousSelected)
  542. option->selectedPosition = QStyleOptionHeader::PreviousIsSelected;
  543. else if (nextSelected)
  544. option->selectedPosition = QStyleOptionHeader::NextIsSelected;
  545. else
  546. option->selectedPosition = QStyleOptionHeader::NotAdjacent;
  547. */
  548. }