ctkCheckableHeaderView.cpp 23 KB

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