ctkCheckableHeaderView.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. /*=========================================================================
  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
  54. {
  55. Q_DECLARE_PUBLIC(ctkCheckableHeaderView);
  56. protected:
  57. ctkCheckableHeaderView* const q_ptr;
  58. public:
  59. ctkCheckableHeaderViewPrivate(ctkCheckableHeaderView& object);
  60. ~ctkCheckableHeaderViewPrivate();
  61. void init();
  62. /// Set index checkstate and call propagate
  63. void setIndexCheckState(const QModelIndex& index, Qt::CheckState checkState);
  64. /// Return the depth in the model tree of the index.
  65. /// -1 if the index is the root element a header or a header, 0 if the index
  66. /// is a toplevel index, 1 if its parent is toplevel, 2 if its grandparent is
  67. /// toplevel, etc.
  68. int indexDepth(const QModelIndex& modelIndex)const;
  69. /// Set the checkstate of the index based on its children and grand children
  70. void updateCheckState(const QModelIndex& modelIndex);
  71. /// Set the check state of the index to all its children and grand children
  72. void propagateCheckStateToChildren(const QModelIndex& modelIndex);
  73. Qt::CheckState checkState(const QModelIndex& index, bool *checkable)const;
  74. void setCheckState(const QModelIndex& index, Qt::CheckState newCheckState);
  75. int Pressed;
  76. ctkCheckBoxPixmaps* CheckBoxPixmaps;
  77. bool HeaderIsUpdating;
  78. bool ItemsAreUpdating;
  79. bool ForceCheckability;
  80. /// 0 means no propagation
  81. /// -1 means unlimited propagation
  82. /// 1 means propagate to top-level indexes
  83. /// 2 means propagate to top-level and their children
  84. /// ...
  85. int PropagateDepth;
  86. };
  87. //----------------------------------------------------------------------------
  88. ctkCheckableHeaderViewPrivate::ctkCheckableHeaderViewPrivate(ctkCheckableHeaderView& object)
  89. :q_ptr(&object)
  90. {
  91. this->HeaderIsUpdating = false;
  92. this->ItemsAreUpdating = false;
  93. this->CheckBoxPixmaps = 0;
  94. this->Pressed = -1;
  95. this->ForceCheckability = false;
  96. this->PropagateDepth = -1;
  97. }
  98. //-----------------------------------------------------------------------------
  99. ctkCheckableHeaderViewPrivate::~ctkCheckableHeaderViewPrivate()
  100. {
  101. if (this->CheckBoxPixmaps)
  102. {
  103. delete this->CheckBoxPixmaps;
  104. this->CheckBoxPixmaps = 0;
  105. }
  106. }
  107. //----------------------------------------------------------------------------
  108. void ctkCheckableHeaderViewPrivate::init()
  109. {
  110. Q_Q(ctkCheckableHeaderView);
  111. this->CheckBoxPixmaps = new ctkCheckBoxPixmaps(q);
  112. }
  113. //----------------------------------------------------------------------------
  114. Qt::CheckState ctkCheckableHeaderViewPrivate::checkState(
  115. const QModelIndex& index, bool *checkable)const
  116. {
  117. Q_Q(const ctkCheckableHeaderView);
  118. QVariant indexCheckState = index != q->rootIndex() ?
  119. q->model()->data(index, Qt::CheckStateRole):
  120. q->model()->headerData(0, q->orientation(), Qt::CheckStateRole);
  121. return static_cast<Qt::CheckState>(indexCheckState.toInt(checkable));
  122. }
  123. //----------------------------------------------------------------------------
  124. void ctkCheckableHeaderViewPrivate::setCheckState(
  125. const QModelIndex& modelIndex, Qt::CheckState newCheckState)
  126. {
  127. Q_Q(ctkCheckableHeaderView);
  128. if (modelIndex != q->rootIndex())
  129. {
  130. q->model()->setData(modelIndex, newCheckState, Qt::CheckStateRole);
  131. }
  132. else
  133. {
  134. q->model()->setHeaderData(0, q->orientation(), newCheckState, Qt::CheckStateRole);
  135. }
  136. }
  137. //----------------------------------------------------------------------------
  138. void ctkCheckableHeaderViewPrivate::setIndexCheckState(
  139. const QModelIndex& index, Qt::CheckState checkState)
  140. {
  141. Q_Q(ctkCheckableHeaderView);
  142. bool checkable = false;
  143. this->checkState(index, &checkable);
  144. if (!checkable && !this->ForceCheckability)
  145. {
  146. // The index is not checkable and we don't want to force checkability
  147. return;
  148. }
  149. this->setCheckState(index, checkState);
  150. this->propagateCheckStateToChildren(index);
  151. }
  152. //-----------------------------------------------------------------------------
  153. int ctkCheckableHeaderViewPrivate::indexDepth(const QModelIndex& modelIndex)const
  154. {
  155. int depth = -1;
  156. QModelIndex parentIndex = modelIndex;
  157. while (parentIndex.isValid())
  158. {
  159. ++depth;
  160. parentIndex = parentIndex.parent();
  161. }
  162. return depth;
  163. }
  164. //-----------------------------------------------------------------------------
  165. void ctkCheckableHeaderViewPrivate
  166. ::updateCheckState(const QModelIndex& modelIndex)
  167. {
  168. Q_Q(ctkCheckableHeaderView);
  169. bool checkable = false;
  170. int oldCheckState = this->checkState(modelIndex, &checkable);
  171. if (!checkable)
  172. {
  173. return;
  174. }
  175. Qt::CheckState newCheckState = Qt::PartiallyChecked;
  176. bool firstCheckableChild = true;
  177. const int rowCount = q->orientation() == Qt::Horizontal ?
  178. q->model()->rowCount(modelIndex) : 1;
  179. const int columnCount = q->orientation() == Qt::Vertical ?
  180. q->model()->columnCount(modelIndex) : 1;
  181. for (int r = 0; r < rowCount; ++r)
  182. {
  183. for (int c = 0; c < columnCount; ++c)
  184. {
  185. QModelIndex child = q->model()->index(r, c, modelIndex);
  186. QVariant childCheckState = q->model()->data(child, Qt::CheckStateRole);
  187. int childState = childCheckState.toInt(&checkable);
  188. if (!checkable)
  189. {
  190. continue;
  191. }
  192. if (firstCheckableChild)
  193. {
  194. newCheckState = static_cast<Qt::CheckState>(childState);
  195. firstCheckableChild = false;
  196. }
  197. if (newCheckState != childState)
  198. {
  199. newCheckState = Qt::PartiallyChecked;
  200. }
  201. if (newCheckState == Qt::PartiallyChecked)
  202. {
  203. break;
  204. }
  205. }
  206. if (!firstCheckableChild && newCheckState == Qt::PartiallyChecked)
  207. {
  208. break;
  209. }
  210. }
  211. if (oldCheckState == newCheckState)
  212. {
  213. return;
  214. }
  215. this->setCheckState(modelIndex, newCheckState);
  216. if (modelIndex != q->rootIndex())
  217. {
  218. this->updateCheckState(modelIndex.parent());
  219. }
  220. }
  221. //-----------------------------------------------------------------------------
  222. void ctkCheckableHeaderViewPrivate
  223. ::propagateCheckStateToChildren(const QModelIndex& modelIndex)
  224. {
  225. Q_Q(ctkCheckableHeaderView);
  226. int indexDepth = this->indexDepth(modelIndex);
  227. if (this->PropagateDepth == 0 ||
  228. !(indexDepth < this->PropagateDepth || this->PropagateDepth == -1))
  229. {
  230. return;
  231. }
  232. bool checkable = false;
  233. Qt::CheckState checkState = this->checkState(modelIndex, &checkable);
  234. if (!checkable || checkState == Qt::PartiallyChecked)
  235. {
  236. return;
  237. }
  238. while (this->ForceCheckability && q->model()->canFetchMore(modelIndex))
  239. {
  240. q->model()->fetchMore(modelIndex);
  241. }
  242. const int rowCount = q->orientation() == Qt::Horizontal ?
  243. q->model()->rowCount(modelIndex) : 1;
  244. const int columnCount = q->orientation() == Qt::Vertical ?
  245. q->model()->columnCount(modelIndex) : 1;
  246. for (int r = 0; r < rowCount; ++r)
  247. {
  248. for (int c = 0; c < columnCount; ++c)
  249. {
  250. QModelIndex child = q->model()->index(r, c, modelIndex);
  251. this->setIndexCheckState(child, checkState);
  252. }
  253. }
  254. }
  255. //----------------------------------------------------------------------------
  256. ctkCheckableHeaderView::ctkCheckableHeaderView(
  257. Qt::Orientation orient, QWidget *widgetParent)
  258. : QHeaderView(orient, widgetParent)
  259. , d_ptr(new ctkCheckableHeaderViewPrivate(*this))
  260. {
  261. Q_D(ctkCheckableHeaderView);
  262. d->init();
  263. // TODO: doesn't support reparenting here.
  264. if(widgetParent)
  265. {
  266. // Listen for focus change events.
  267. widgetParent->installEventFilter(this);
  268. }
  269. }
  270. //-----------------------------------------------------------------------------
  271. ctkCheckableHeaderView::~ctkCheckableHeaderView()
  272. {
  273. }
  274. //-----------------------------------------------------------------------------
  275. bool ctkCheckableHeaderView::eventFilter(QObject *, QEvent *e)
  276. {
  277. if(e->type() != QEvent::FocusIn &&
  278. e->type() != QEvent::FocusOut)
  279. {
  280. return false;
  281. }
  282. //this->updateHeaderPixmaps();
  283. return false;
  284. }
  285. //-----------------------------------------------------------------------------
  286. void ctkCheckableHeaderView::setModel(QAbstractItemModel *newModel)
  287. {
  288. Q_D(ctkCheckableHeaderView);
  289. QAbstractItemModel *current = this->model();
  290. if (current == newModel)
  291. {
  292. return;
  293. }
  294. if(current)
  295. {
  296. this->disconnect(
  297. current, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
  298. this, SLOT(onHeaderDataChanged(Qt::Orientation, int, int)));
  299. this->disconnect(
  300. current, SIGNAL(modelReset()),
  301. this, SLOT(updateHeaderPixmaps()));
  302. this->disconnect(
  303. current, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  304. this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
  305. this->disconnect(
  306. current, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
  307. this, SLOT(onHeaderSectionInserted(const QModelIndex &, int, int)));
  308. this->disconnect(
  309. current, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  310. this, SLOT(onHeaderSectionInserted(const QModelIndex &, int, int)));
  311. }
  312. this->QHeaderView::setModel(newModel);
  313. if(newModel)
  314. {
  315. this->connect(
  316. newModel, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
  317. this, SLOT(onHeaderDataChanged(Qt::Orientation, int, int)));
  318. this->connect(
  319. newModel, SIGNAL(modelReset()),
  320. this, SLOT(updateHeaderPixmaps()));
  321. if (d->PropagateDepth != 0)
  322. {
  323. this->connect(
  324. newModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  325. this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
  326. this->updateHeadersFromItems();
  327. }
  328. if(this->orientation() == Qt::Horizontal)
  329. {
  330. this->connect(
  331. newModel, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
  332. this, SLOT(onHeaderSectionInserted(const QModelIndex &, int, int)));
  333. }
  334. else
  335. {
  336. this->connect(
  337. newModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  338. this, SLOT(onHeaderSectionInserted(const QModelIndex &, int, int)));
  339. }
  340. }
  341. this->updateHeaderPixmaps();
  342. }
  343. //-----------------------------------------------------------------------------
  344. void ctkCheckableHeaderView::setRootIndex(const QModelIndex &index)
  345. {
  346. Q_D(ctkCheckableHeaderView);
  347. this->QHeaderView::setRootIndex(index);
  348. if (d->PropagateDepth != 0)
  349. {
  350. this->updateHeadersFromItems();
  351. }
  352. }
  353. //-----------------------------------------------------------------------------
  354. void ctkCheckableHeaderView::setPropagateDepth(int depth)
  355. {
  356. Q_D(ctkCheckableHeaderView);
  357. if (d->PropagateDepth == depth)
  358. {
  359. return;
  360. }
  361. d->PropagateDepth = depth;
  362. if (!this->model())
  363. {
  364. return;
  365. }
  366. if (depth != 0)
  367. {
  368. this->connect(
  369. this->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  370. this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)), Qt::UniqueConnection);
  371. this->updateHeadersFromItems();
  372. }
  373. else
  374. {
  375. this->disconnect(
  376. this->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
  377. this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
  378. }
  379. }
  380. //-----------------------------------------------------------------------------
  381. int ctkCheckableHeaderView::propagateDepth()const
  382. {
  383. Q_D(const ctkCheckableHeaderView);
  384. return d->PropagateDepth;
  385. }
  386. //-----------------------------------------------------------------------------
  387. void ctkCheckableHeaderView::setForceCheckability(bool force)
  388. {
  389. Q_D(ctkCheckableHeaderView);
  390. if (d->ForceCheckability == force)
  391. {
  392. return;
  393. }
  394. d->ForceCheckability = force;
  395. if (this->model())
  396. {
  397. d->propagateCheckStateToChildren(this->rootIndex());
  398. }
  399. }
  400. //-----------------------------------------------------------------------------
  401. bool ctkCheckableHeaderView::forceCheckability()const
  402. {
  403. Q_D(const ctkCheckableHeaderView);
  404. return d->ForceCheckability;
  405. }
  406. //-----------------------------------------------------------------------------
  407. void ctkCheckableHeaderView::toggleCheckState(int section)
  408. {
  409. // If the section is checkable, toggle the check state.
  410. if(!this->isCheckable(section))
  411. {
  412. return;
  413. }
  414. // I've no strong feeling to turn the state checked or unchecked when the
  415. // state is PartiallyChecked.
  416. this->setCheckState(section, this->checkState(section) == Qt::Checked ? Qt::Unchecked : Qt::Checked);
  417. }
  418. //-----------------------------------------------------------------------------
  419. void ctkCheckableHeaderView::setCheckState(int section, Qt::CheckState checkState)
  420. {
  421. Q_D(ctkCheckableHeaderView);
  422. QAbstractItemModel *current = this->model();
  423. if(current == 0)
  424. {
  425. return;
  426. }
  427. current->setHeaderData(section, this->orientation(),
  428. checkState, Qt::CheckStateRole);
  429. }
  430. //-----------------------------------------------------------------------------
  431. void ctkCheckableHeaderView::onHeaderDataChanged(Qt::Orientation orient,
  432. int firstSection,
  433. int lastSection)
  434. {
  435. Q_D(ctkCheckableHeaderView);
  436. if(orient != this->orientation())
  437. {
  438. return;
  439. }
  440. bool oldItemsAreUpdating = d->ItemsAreUpdating;
  441. if (!d->ItemsAreUpdating)
  442. {
  443. d->ItemsAreUpdating = true;
  444. d->propagateCheckStateToChildren(this->rootIndex());
  445. }
  446. // update pixmap
  447. this->updateHeaderPixmaps(firstSection, lastSection);
  448. d->ItemsAreUpdating = oldItemsAreUpdating;
  449. }
  450. //-----------------------------------------------------------------------------
  451. void ctkCheckableHeaderView::updateHeaderPixmaps(int firstSection, int lastSection)
  452. {
  453. Q_D(ctkCheckableHeaderView);
  454. QAbstractItemModel *current = this->model();
  455. if(d->HeaderIsUpdating || !current)
  456. {
  457. return;
  458. }
  459. d->HeaderIsUpdating = true;
  460. firstSection = qBound(0, firstSection, this->count() -1);
  461. lastSection = qBound(0, lastSection, this->count() -1);
  462. bool active = true;
  463. if(this->parentWidget())
  464. {
  465. active = this->parentWidget()->hasFocus();
  466. }
  467. for(int i = firstSection; i <= lastSection; i++)
  468. {
  469. QVariant decoration;
  470. Qt::CheckState checkState;
  471. if (this->checkState(i, checkState))
  472. {
  473. decoration = d->CheckBoxPixmaps->pixmap(checkState, active);
  474. }
  475. current->setHeaderData(i, this->orientation(), decoration,
  476. Qt::DecorationRole);
  477. }
  478. d->HeaderIsUpdating = false;
  479. }
  480. //-----------------------------------------------------------------------------
  481. void ctkCheckableHeaderView::updateHeadersFromItems()
  482. {
  483. Q_D(ctkCheckableHeaderView);
  484. QAbstractItemModel *currentModel = this->model();
  485. if (!currentModel)
  486. {
  487. return;
  488. }
  489. d->updateCheckState(QModelIndex());
  490. }
  491. //-----------------------------------------------------------------------------
  492. void ctkCheckableHeaderView::onDataChanged(const QModelIndex & topLeft,
  493. const QModelIndex & bottomRight)
  494. {
  495. Q_UNUSED(bottomRight);
  496. Q_D(ctkCheckableHeaderView);
  497. if(d->ItemsAreUpdating || d->PropagateDepth == 0)
  498. {
  499. return;
  500. }
  501. bool checkable = false;
  502. d->checkState(topLeft, &checkable);
  503. if (!checkable)
  504. {
  505. return;
  506. }
  507. d->ItemsAreUpdating = true;
  508. // TODO: handle topLeft "TO bottomRight"
  509. d->propagateCheckStateToChildren(topLeft);
  510. d->updateCheckState(topLeft.parent());
  511. d->ItemsAreUpdating = false;
  512. }
  513. //-----------------------------------------------------------------------------
  514. void ctkCheckableHeaderView::onHeaderSectionInserted(const QModelIndex &parentIndex,
  515. int first, int last)
  516. {
  517. // only handle toplevel columns.
  518. if (this->rootIndex() != parentIndex)
  519. {
  520. return;
  521. }
  522. this->updateHeaderPixmaps(first, last);
  523. }
  524. //-----------------------------------------------------------------------------
  525. bool ctkCheckableHeaderView::isCheckable(int section)const
  526. {
  527. return !this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).isNull();
  528. }
  529. //-----------------------------------------------------------------------------
  530. Qt::CheckState ctkCheckableHeaderView::checkState(int section)const
  531. {
  532. return static_cast<Qt::CheckState>(
  533. this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt());
  534. }
  535. //-----------------------------------------------------------------------------
  536. bool ctkCheckableHeaderView::checkState(int section, Qt::CheckState& checkState)const
  537. {
  538. bool checkable = false;
  539. checkState = static_cast<Qt::CheckState>(
  540. this->model()->headerData(section, this->orientation(), Qt::CheckStateRole).toInt(&checkable));
  541. return checkable;
  542. }
  543. //-----------------------------------------------------------------------------
  544. void ctkCheckableHeaderView::mousePressEvent(QMouseEvent *e)
  545. {
  546. Q_D(ctkCheckableHeaderView);
  547. if (e->button() != Qt::LeftButton ||
  548. d->Pressed >= 0)
  549. {
  550. d->Pressed = -1;
  551. this->QHeaderView::mousePressEvent(e);
  552. return;
  553. }
  554. d->Pressed = -1;
  555. //check if the check box is pressed
  556. int pos = this->orientation() == Qt::Horizontal ? e->x() : e->y();
  557. int section = this->logicalIndexAt(pos);
  558. if (this->isCheckable(section) &&
  559. this->isPointInCheckBox(section, e->pos()))
  560. {
  561. d->Pressed = section;
  562. }
  563. else
  564. {
  565. this->QHeaderView::mousePressEvent(e);
  566. }
  567. }
  568. //-----------------------------------------------------------------------------
  569. void ctkCheckableHeaderView::mouseReleaseEvent(QMouseEvent *e)
  570. {
  571. Q_D(ctkCheckableHeaderView);
  572. if (e->button() != Qt::LeftButton ||
  573. d->Pressed < 0)
  574. {
  575. d->Pressed = -1;
  576. this->QHeaderView::mouseReleaseEvent(e);
  577. return;
  578. }
  579. //check if the check box is pressed
  580. int pos = this->orientation() == Qt::Horizontal ? e->x() : e->y();
  581. int section = this->logicalIndexAt(pos);
  582. if (section == d->Pressed &&
  583. this->isPointInCheckBox(section, e->pos()))
  584. {
  585. d->Pressed = -1;
  586. this->toggleCheckState(section);
  587. }
  588. this->QHeaderView::mousePressEvent(e);
  589. }
  590. //-----------------------------------------------------------------------------
  591. bool ctkCheckableHeaderView::isPointInCheckBox(int section, QPoint pos)const
  592. {
  593. QRect sectionRect = this->orientation() == Qt::Horizontal ?
  594. QRect(this->sectionPosition(section), 0,
  595. this->sectionSize(section), this->height()):
  596. QRect(0, this->sectionPosition(section),
  597. this->width(), this->sectionSize(section));
  598. QStyleOptionHeader opt;
  599. this->initStyleOption(&opt);
  600. this->initStyleSectionOption(&opt, section, sectionRect);
  601. QRect headerLabelRect = this->style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
  602. // from qcommonstyle.cpp:1541
  603. if (opt.icon.isNull())
  604. {
  605. return false;
  606. }
  607. QPixmap pixmap
  608. = opt.icon.pixmap(this->style()->pixelMetric(QStyle::PM_SmallIconSize),
  609. (opt.state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled);
  610. QRect aligned = this->style()->alignedRect(opt.direction, QFlag(opt.iconAlignment),
  611. pixmap.size(), headerLabelRect);
  612. QRect inter = aligned.intersected(headerLabelRect);
  613. return inter.contains(pos);
  614. }
  615. //-----------------------------------------------------------------------------
  616. void ctkCheckableHeaderView::initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const
  617. {
  618. // from qheaderview.cpp:paintsection
  619. QStyle::State state = QStyle::State_None;
  620. if (this->isEnabled())
  621. {
  622. state |= QStyle::State_Enabled;
  623. }
  624. if (this->window()->isActiveWindow())
  625. {
  626. state |= QStyle::State_Active;
  627. }
  628. if (this->isSortIndicatorShown() &&
  629. this->sortIndicatorSection() == section)
  630. {
  631. option->sortIndicator = (this->sortIndicatorOrder() == Qt::AscendingOrder)
  632. ? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp;
  633. }
  634. // setup the style option structure
  635. QVariant textAlignment =
  636. this->model()->headerData(section, this->orientation(),
  637. Qt::TextAlignmentRole);
  638. option->rect = rect;
  639. option->section = section;
  640. option->state |= state;
  641. option->textAlignment = Qt::Alignment(textAlignment.isValid()
  642. ? Qt::Alignment(textAlignment.toInt())
  643. : this->defaultAlignment());
  644. option->iconAlignment = Qt::AlignVCenter;
  645. option->text = this->model()->headerData(section, this->orientation(),
  646. Qt::DisplayRole).toString();
  647. if (this->textElideMode() != Qt::ElideNone)
  648. {
  649. option->text = option->fontMetrics.elidedText(option->text, this->textElideMode() , rect.width() - 4);
  650. }
  651. QVariant variant = this->model()->headerData(section, this->orientation(),
  652. Qt::DecorationRole);
  653. option->icon = qvariant_cast<QIcon>(variant);
  654. if (option->icon.isNull())
  655. {
  656. option->icon = qvariant_cast<QPixmap>(variant);
  657. }
  658. QVariant foregroundBrush = this->model()->headerData(section, this->orientation(),
  659. Qt::ForegroundRole);
  660. if (qVariantCanConvert<QBrush>(foregroundBrush))
  661. {
  662. option->palette.setBrush(QPalette::ButtonText, qvariant_cast<QBrush>(foregroundBrush));
  663. }
  664. //QPointF oldBO = painter->brushOrigin();
  665. QVariant backgroundBrush = this->model()->headerData(section, this->orientation(),
  666. Qt::BackgroundRole);
  667. if (qVariantCanConvert<QBrush>(backgroundBrush))
  668. {
  669. option->palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
  670. option->palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
  671. //painter->setBrushOrigin(option->rect.topLeft());
  672. }
  673. // the section position
  674. int visual = this->visualIndex(section);
  675. Q_ASSERT(visual != -1);
  676. if (this->count() == 1)
  677. {
  678. option->position = QStyleOptionHeader::OnlyOneSection;
  679. }
  680. else if (visual == 0)
  681. {
  682. option->position = QStyleOptionHeader::Beginning;
  683. }
  684. else if (visual == this->count() - 1)
  685. {
  686. option->position = QStyleOptionHeader::End;
  687. }
  688. else
  689. {
  690. option->position = QStyleOptionHeader::Middle;
  691. }
  692. option->orientation = this->orientation();
  693. /* the selected position
  694. bool previousSelected = d->isSectionSelected(this->logicalIndex(visual - 1));
  695. bool nextSelected = d->isSectionSelected(this->logicalIndex(visual + 1));
  696. if (previousSelected && nextSelected)
  697. option->selectedPosition = QStyleOptionHeader::NextAndPreviousAreSelected;
  698. else if (previousSelected)
  699. option->selectedPosition = QStyleOptionHeader::PreviousIsSelected;
  700. else if (nextSelected)
  701. option->selectedPosition = QStyleOptionHeader::NextIsSelected;
  702. else
  703. option->selectedPosition = QStyleOptionHeader::NotAdjacent;
  704. */
  705. }