ctkCheckableHeaderView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 <ctkCheckableModelHelper.h>
  52. #include "ctkCheckBoxPixmaps.h"
  53. //-----------------------------------------------------------------------------
  54. class ctkCheckableHeaderViewPrivate
  55. {
  56. Q_DECLARE_PUBLIC(ctkCheckableHeaderView);
  57. protected:
  58. ctkCheckableHeaderView* const q_ptr;
  59. public:
  60. ctkCheckableHeaderViewPrivate(ctkCheckableHeaderView& object);
  61. ~ctkCheckableHeaderViewPrivate();
  62. void init();
  63. ctkCheckableModelHelper* CheckableModelHelper;
  64. int Pressed;
  65. ctkCheckBoxPixmaps* CheckBoxPixmaps;
  66. bool HeaderIsUpdating;
  67. };
  68. //----------------------------------------------------------------------------
  69. ctkCheckableHeaderViewPrivate::ctkCheckableHeaderViewPrivate(ctkCheckableHeaderView& object)
  70. :q_ptr(&object)
  71. {
  72. this->CheckableModelHelper = 0;
  73. this->Pressed = -1;
  74. this->CheckBoxPixmaps = 0;
  75. this->HeaderIsUpdating = false;
  76. }
  77. //-----------------------------------------------------------------------------
  78. ctkCheckableHeaderViewPrivate::~ctkCheckableHeaderViewPrivate()
  79. {
  80. if (this->CheckBoxPixmaps)
  81. {
  82. delete this->CheckBoxPixmaps;
  83. this->CheckBoxPixmaps = 0;
  84. }
  85. if (this->CheckableModelHelper)
  86. {
  87. delete this->CheckableModelHelper;
  88. this->CheckableModelHelper = 0;
  89. }
  90. }
  91. //----------------------------------------------------------------------------
  92. void ctkCheckableHeaderViewPrivate::init()
  93. {
  94. Q_Q(ctkCheckableHeaderView);
  95. this->CheckBoxPixmaps = new ctkCheckBoxPixmaps(q);
  96. this->CheckableModelHelper = new ctkCheckableModelHelper(q->orientation(), q);
  97. }
  98. //----------------------------------------------------------------------------
  99. ctkCheckableHeaderView::ctkCheckableHeaderView(
  100. Qt::Orientation orient, QWidget *widgetParent)
  101. : QHeaderView(orient, widgetParent)
  102. , d_ptr(new ctkCheckableHeaderViewPrivate(*this))
  103. {
  104. Q_D(ctkCheckableHeaderView);
  105. d->init();
  106. // TODO: doesn't support reparenting here.
  107. if(widgetParent)
  108. {
  109. // Listen for focus change events.
  110. widgetParent->installEventFilter(this);
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. ctkCheckableHeaderView::~ctkCheckableHeaderView()
  115. {
  116. }
  117. //-----------------------------------------------------------------------------
  118. Qt::CheckState ctkCheckableHeaderView::checkState(int section)const
  119. {
  120. Q_D(const ctkCheckableHeaderView);
  121. return d->CheckableModelHelper->headerCheckState(section);
  122. }
  123. //-----------------------------------------------------------------------------
  124. bool ctkCheckableHeaderView::checkState(int section, Qt::CheckState& checkState)const
  125. {
  126. Q_D(const ctkCheckableHeaderView);
  127. return d->CheckableModelHelper->headerCheckState(section, checkState);
  128. }
  129. //-----------------------------------------------------------------------------
  130. void ctkCheckableHeaderView::setCheckState(int section, Qt::CheckState checkState)
  131. {
  132. Q_D(ctkCheckableHeaderView);
  133. d->CheckableModelHelper->setHeaderCheckState(section, checkState);
  134. }
  135. //-----------------------------------------------------------------------------
  136. ctkCheckableModelHelper* ctkCheckableHeaderView::checkableModelHelper()const
  137. {
  138. Q_D(const ctkCheckableHeaderView);
  139. return d->CheckableModelHelper;
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool ctkCheckableHeaderView::eventFilter(QObject *, QEvent *e)
  143. {
  144. if(e->type() != QEvent::FocusIn &&
  145. e->type() != QEvent::FocusOut)
  146. {
  147. return false;
  148. }
  149. //this->updateHeaderPixmaps();
  150. return false;
  151. }
  152. //-----------------------------------------------------------------------------
  153. void ctkCheckableHeaderView::setModel(QAbstractItemModel *newModel)
  154. {
  155. Q_D(ctkCheckableHeaderView);
  156. QAbstractItemModel *current = this->model();
  157. if (current)
  158. {
  159. this->disconnect(
  160. current, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
  161. this, SLOT(onHeaderDataChanged(Qt::Orientation, int, int)));
  162. this->disconnect(
  163. current, SIGNAL(modelReset()),
  164. this, SLOT(updateHeaderPixmaps()));
  165. this->disconnect(
  166. current, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
  167. this, SLOT(onHeaderSectionInserted()));
  168. this->disconnect(
  169. current, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  170. this, SLOT(onHeaderSectionInserted()));
  171. }
  172. this->QHeaderView::setModel(newModel);
  173. d->CheckableModelHelper->setModel(newModel);
  174. if(newModel)
  175. {
  176. this->connect(
  177. newModel, SIGNAL(headerDataChanged(Qt::Orientation, int, int)),
  178. this, SLOT(onHeaderDataChanged(Qt::Orientation, int, int)));
  179. this->connect(
  180. newModel, SIGNAL(modelReset()),
  181. this, SLOT(updateHeaderPixmaps()));
  182. if(this->orientation() == Qt::Horizontal)
  183. {
  184. this->connect(
  185. newModel, SIGNAL(columnsInserted(const QModelIndex &, int, int)),
  186. this, SLOT(onHeaderSectionInserted()));
  187. }
  188. else
  189. {
  190. this->connect(
  191. newModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
  192. this, SLOT(onHeaderSectionInserted()));
  193. }
  194. }
  195. this->updateHeaderPixmaps();
  196. }
  197. //-----------------------------------------------------------------------------
  198. void ctkCheckableHeaderView::setRootIndex(const QModelIndex &index)
  199. {
  200. Q_D(ctkCheckableHeaderView);
  201. this->QHeaderView::setRootIndex(index);
  202. d->CheckableModelHelper->setRootIndex(index);
  203. }
  204. //-----------------------------------------------------------------------------
  205. void ctkCheckableHeaderView::onHeaderDataChanged(Qt::Orientation orient,
  206. int firstSection,
  207. int lastSection)
  208. {
  209. if(orient != this->orientation())
  210. {
  211. return;
  212. }
  213. // update pixmap
  214. this->updateHeaderPixmaps(firstSection, lastSection);
  215. }
  216. //-----------------------------------------------------------------------------
  217. void ctkCheckableHeaderView::updateHeaderPixmaps(int firstSection, int lastSection)
  218. {
  219. Q_D(ctkCheckableHeaderView);
  220. QAbstractItemModel *current = this->model();
  221. if(d->HeaderIsUpdating || !current)
  222. {
  223. return;
  224. }
  225. d->HeaderIsUpdating = true;
  226. firstSection = qBound(0, firstSection, this->count() -1);
  227. lastSection = qBound(0, lastSection, this->count() -1);
  228. bool active = true;
  229. if(this->parentWidget())
  230. {
  231. active = this->parentWidget()->hasFocus();
  232. }
  233. for(int i = firstSection; i <= lastSection; i++)
  234. {
  235. QVariant decoration;
  236. Qt::CheckState checkState;
  237. if (d->CheckableModelHelper->headerCheckState(i, checkState))
  238. {
  239. decoration = d->CheckBoxPixmaps->pixmap(checkState, active);
  240. }
  241. current->setHeaderData(i, this->orientation(), decoration,
  242. Qt::DecorationRole);
  243. }
  244. d->HeaderIsUpdating = false;
  245. }
  246. //-----------------------------------------------------------------------------
  247. void ctkCheckableHeaderView::onHeaderSectionInserted()
  248. {
  249. this->updateHeaderPixmaps();
  250. }
  251. //-----------------------------------------------------------------------------
  252. void ctkCheckableHeaderView::mousePressEvent(QMouseEvent *e)
  253. {
  254. Q_D(ctkCheckableHeaderView);
  255. if (e->button() != Qt::LeftButton ||
  256. d->Pressed >= 0)
  257. {
  258. d->Pressed = -1;
  259. this->QHeaderView::mousePressEvent(e);
  260. return;
  261. }
  262. d->Pressed = -1;
  263. //check if the check box is pressed
  264. int pos = this->orientation() == Qt::Horizontal ? e->x() : e->y();
  265. int section = this->logicalIndexAt(pos);
  266. if (d->CheckableModelHelper->isHeaderCheckable(section) &&
  267. this->isPointInCheckBox(section, e->pos()))
  268. {
  269. d->Pressed = section;
  270. }
  271. else
  272. {
  273. this->QHeaderView::mousePressEvent(e);
  274. }
  275. }
  276. //-----------------------------------------------------------------------------
  277. void ctkCheckableHeaderView::mouseReleaseEvent(QMouseEvent *e)
  278. {
  279. Q_D(ctkCheckableHeaderView);
  280. if (e->button() != Qt::LeftButton ||
  281. d->Pressed < 0)
  282. {
  283. d->Pressed = -1;
  284. this->QHeaderView::mouseReleaseEvent(e);
  285. return;
  286. }
  287. //check if the check box is pressed
  288. int pos = this->orientation() == Qt::Horizontal ? e->x() : e->y();
  289. int section = this->logicalIndexAt(pos);
  290. if (section == d->Pressed &&
  291. this->isPointInCheckBox(section, e->pos()))
  292. {
  293. d->Pressed = -1;
  294. d->CheckableModelHelper->toggleHeaderCheckState(section);
  295. }
  296. this->QHeaderView::mousePressEvent(e);
  297. }
  298. //-----------------------------------------------------------------------------
  299. bool ctkCheckableHeaderView::isPointInCheckBox(int section, QPoint pos)const
  300. {
  301. QRect sectionRect = this->orientation() == Qt::Horizontal ?
  302. QRect(this->sectionPosition(section), 0,
  303. this->sectionSize(section), this->height()):
  304. QRect(0, this->sectionPosition(section),
  305. this->width(), this->sectionSize(section));
  306. QStyleOptionHeader opt;
  307. this->initStyleOption(&opt);
  308. this->initStyleSectionOption(&opt, section, sectionRect);
  309. QRect headerLabelRect = this->style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
  310. // from qcommonstyle.cpp:1541
  311. if (opt.icon.isNull())
  312. {
  313. return false;
  314. }
  315. QPixmap pixmap
  316. = opt.icon.pixmap(this->style()->pixelMetric(QStyle::PM_SmallIconSize),
  317. (opt.state & QStyle::State_Enabled) ? QIcon::Normal : QIcon::Disabled);
  318. QRect aligned = this->style()->alignedRect(opt.direction, QFlag(opt.iconAlignment),
  319. pixmap.size(), headerLabelRect);
  320. QRect inter = aligned.intersected(headerLabelRect);
  321. return inter.contains(pos);
  322. }
  323. //-----------------------------------------------------------------------------
  324. void ctkCheckableHeaderView::initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const
  325. {
  326. // from qheaderview.cpp:paintsection
  327. QStyle::State state = QStyle::State_None;
  328. if (this->isEnabled())
  329. {
  330. state |= QStyle::State_Enabled;
  331. }
  332. if (this->window()->isActiveWindow())
  333. {
  334. state |= QStyle::State_Active;
  335. }
  336. if (this->isSortIndicatorShown() &&
  337. this->sortIndicatorSection() == section)
  338. {
  339. option->sortIndicator = (this->sortIndicatorOrder() == Qt::AscendingOrder)
  340. ? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp;
  341. }
  342. // setup the style option structure
  343. QVariant textAlignment =
  344. this->model()->headerData(section, this->orientation(),
  345. Qt::TextAlignmentRole);
  346. option->rect = rect;
  347. option->section = section;
  348. option->state |= state;
  349. option->textAlignment = Qt::Alignment(textAlignment.isValid()
  350. ? Qt::Alignment(textAlignment.toInt())
  351. : this->defaultAlignment());
  352. option->iconAlignment = Qt::AlignVCenter;
  353. option->text = this->model()->headerData(section, this->orientation(),
  354. Qt::DisplayRole).toString();
  355. if (this->textElideMode() != Qt::ElideNone)
  356. {
  357. option->text = option->fontMetrics.elidedText(option->text, this->textElideMode() , rect.width() - 4);
  358. }
  359. QVariant variant = this->model()->headerData(section, this->orientation(),
  360. Qt::DecorationRole);
  361. option->icon = qvariant_cast<QIcon>(variant);
  362. if (option->icon.isNull())
  363. {
  364. option->icon = qvariant_cast<QPixmap>(variant);
  365. }
  366. QVariant foregroundBrush = this->model()->headerData(section, this->orientation(),
  367. Qt::ForegroundRole);
  368. if (qVariantCanConvert<QBrush>(foregroundBrush))
  369. {
  370. option->palette.setBrush(QPalette::ButtonText, qvariant_cast<QBrush>(foregroundBrush));
  371. }
  372. //QPointF oldBO = painter->brushOrigin();
  373. QVariant backgroundBrush = this->model()->headerData(section, this->orientation(),
  374. Qt::BackgroundRole);
  375. if (qVariantCanConvert<QBrush>(backgroundBrush))
  376. {
  377. option->palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
  378. option->palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
  379. //painter->setBrushOrigin(option->rect.topLeft());
  380. }
  381. // the section position
  382. int visual = this->visualIndex(section);
  383. Q_ASSERT(visual != -1);
  384. if (this->count() == 1)
  385. {
  386. option->position = QStyleOptionHeader::OnlyOneSection;
  387. }
  388. else if (visual == 0)
  389. {
  390. option->position = QStyleOptionHeader::Beginning;
  391. }
  392. else if (visual == this->count() - 1)
  393. {
  394. option->position = QStyleOptionHeader::End;
  395. }
  396. else
  397. {
  398. option->position = QStyleOptionHeader::Middle;
  399. }
  400. option->orientation = this->orientation();
  401. /* the selected position
  402. bool previousSelected = d->isSectionSelected(this->logicalIndex(visual - 1));
  403. bool nextSelected = d->isSectionSelected(this->logicalIndex(visual + 1));
  404. if (previousSelected && nextSelected)
  405. option->selectedPosition = QStyleOptionHeader::NextAndPreviousAreSelected;
  406. else if (previousSelected)
  407. option->selectedPosition = QStyleOptionHeader::PreviousIsSelected;
  408. else if (nextSelected)
  409. option->selectedPosition = QStyleOptionHeader::NextIsSelected;
  410. else
  411. option->selectedPosition = QStyleOptionHeader::NotAdjacent;
  412. */
  413. }