ctkCheckableHeaderView.cpp 19 KB

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