ctkTreeComboBox.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.apache.org/licenses/LICENSE-2.0.txt
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QDesktopWidget>
  17. #include <QEvent>
  18. #include <QHeaderView>
  19. #include <QKeyEvent>
  20. #include <QLayout>
  21. #include <QScrollBar>
  22. #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
  23. # include <QInputContext>
  24. #endif
  25. #include <QMouseEvent>
  26. #include <QModelIndex>
  27. #include <QStack>
  28. #include <QTreeView>
  29. #include <QDebug>
  30. // CTK includes
  31. #include "ctkTreeComboBox.h"
  32. // -------------------------------------------------------------------------
  33. class ctkTreeComboBoxPrivate
  34. {
  35. Q_DECLARE_PUBLIC(ctkTreeComboBox);
  36. protected:
  37. ctkTreeComboBox* const q_ptr;
  38. public:
  39. ctkTreeComboBoxPrivate(ctkTreeComboBox& object);
  40. int computeWidthHint()const;
  41. bool SkipNextHide;
  42. bool RootSet;
  43. bool SendCurrentItem;
  44. QPersistentModelIndex Root;
  45. int VisibleModelColumn;
  46. };
  47. // -------------------------------------------------------------------------
  48. ctkTreeComboBoxPrivate::ctkTreeComboBoxPrivate(ctkTreeComboBox& object)
  49. :q_ptr(&object)
  50. {
  51. this->SkipNextHide = false;
  52. this->RootSet = false;
  53. this->SendCurrentItem = false;
  54. this->VisibleModelColumn = -1; // all visible by default
  55. }
  56. // -------------------------------------------------------------------------
  57. int ctkTreeComboBoxPrivate::computeWidthHint()const
  58. {
  59. Q_Q(const ctkTreeComboBox);
  60. return q->view()->sizeHintForColumn(q->modelColumn());
  61. }
  62. // -------------------------------------------------------------------------
  63. ctkTreeComboBox::ctkTreeComboBox(QWidget* _parent):Superclass(_parent)
  64. , d_ptr(new ctkTreeComboBoxPrivate(*this))
  65. {
  66. QTreeView* treeView = new QTreeView(this);
  67. treeView->setHeaderHidden(true);
  68. this->setView(treeView);
  69. // we install the filter AFTER the QComboBox installed it.
  70. // so that our eventFilter will be called first
  71. this->view()->viewport()->installEventFilter(this);
  72. connect(treeView, SIGNAL(collapsed(QModelIndex)),
  73. this, SLOT(resizePopup()));
  74. connect(treeView, SIGNAL(expanded(QModelIndex)),
  75. this, SLOT(resizePopup()));
  76. }
  77. // -------------------------------------------------------------------------
  78. ctkTreeComboBox::~ctkTreeComboBox()
  79. {
  80. }
  81. // -------------------------------------------------------------------------
  82. int ctkTreeComboBox::visibleModelColumn()const
  83. {
  84. Q_D(const ctkTreeComboBox);
  85. return d->VisibleModelColumn;
  86. }
  87. // -------------------------------------------------------------------------
  88. void ctkTreeComboBox::setVisibleModelColumn(int index)
  89. {
  90. Q_D(ctkTreeComboBox);
  91. d->VisibleModelColumn = index;
  92. }
  93. // -------------------------------------------------------------------------
  94. bool ctkTreeComboBox::eventFilter(QObject* object, QEvent* _event)
  95. {
  96. Q_D(ctkTreeComboBox);
  97. Q_UNUSED(object);
  98. bool res = false;
  99. d->SendCurrentItem = false;
  100. switch (_event->type())
  101. {
  102. default:
  103. break;
  104. case QEvent::ShortcutOverride:
  105. switch (static_cast<QKeyEvent*>(_event)->key())
  106. {
  107. case Qt::Key_Enter:
  108. case Qt::Key_Return:
  109. case Qt::Key_Select:
  110. d->SendCurrentItem = true;
  111. break;
  112. default:
  113. break;
  114. }
  115. break;
  116. case QEvent::MouseButtonRelease:
  117. QMouseEvent* mouseEvent = dynamic_cast<QMouseEvent*>(_event);
  118. QModelIndex index = this->view()->indexAt(mouseEvent->pos());
  119. // do we click the branch (+ or -) or the item itself ?
  120. if (this->view()->model()->hasChildren(index) &&
  121. (index.flags() & Qt::ItemIsSelectable) &&
  122. !this->view()->visualRect(index).contains(mouseEvent->pos()))
  123. {//qDebug() << "Set skip on";
  124. // if the branch is clicked, then we don't want to close the
  125. // popup. (we don't want to select the item, just expand it.)
  126. // of course, all that doesn't apply with unselectable items, as
  127. // they won't close the popup.
  128. d->SkipNextHide = true;
  129. }
  130. // we want to get rid of an odd behavior.
  131. // If the user highlight a selectable item and then
  132. // click on the branch of an unselectable item while keeping the
  133. // previous selection. The popup would be normally closed in that
  134. // case. We don't want that.
  135. if ( this->view()->model()->hasChildren(index) &&
  136. !(index.flags() & Qt::ItemIsSelectable) &&
  137. !this->view()->visualRect(index).contains(mouseEvent->pos()))
  138. {//qDebug() << "eat";
  139. // eat the event, don't go to the QComboBox event filters.
  140. res = true;
  141. }
  142. d->SendCurrentItem = this->view()->rect().contains(mouseEvent->pos()) &&
  143. this->view()->currentIndex().isValid() &&
  144. (this->view()->currentIndex().flags() & Qt::ItemIsEnabled) &&
  145. (this->view()->currentIndex().flags() & Qt::ItemIsSelectable);
  146. break;
  147. }
  148. return res;
  149. }
  150. // -------------------------------------------------------------------------
  151. void ctkTreeComboBox::showPopup()
  152. {
  153. Q_D(ctkTreeComboBox);
  154. QHeaderView* header = qobject_cast<QTreeView*>(this->view())->header();
  155. for (int i = 0; i < header->count(); ++i)
  156. {
  157. header->setSectionHidden(i, d->VisibleModelColumn != -1 &&
  158. i != d->VisibleModelColumn);
  159. }
  160. this->QComboBox::showPopup();
  161. emit this->popupShow();
  162. }
  163. // -------------------------------------------------------------------------
  164. void ctkTreeComboBox::hidePopup()
  165. {
  166. Q_D(ctkTreeComboBox);
  167. if (d->SkipNextHide)
  168. {// don't hide the popup if the selected item is a parent.
  169. d->SkipNextHide = false;
  170. //this->setCurrentIndex(-1);
  171. //qDebug() << "skip";
  172. //this->QComboBox::showPopup();
  173. }
  174. else
  175. {
  176. //QModelIndex _currentIndex = this->view()->currentIndex();
  177. //qDebug() << "ctkTreeComboBox::hidePopup() " << _currentIndex << " " << _currentIndex.row();
  178. //qDebug() << "before: " << this->currentIndex() << this->view()->currentIndex();
  179. this->QComboBox::hidePopup();
  180. //qDebug() << "after: " << this->currentIndex() << this->view()->currentIndex();
  181. //this->setRootModelIndex(_currentIndex.parent());
  182. //this->setCurrentIndex(_currentIndex.row());
  183. if (d->SendCurrentItem)
  184. {
  185. d->SendCurrentItem = false;
  186. QKeyEvent event(QEvent::ShortcutOverride, Qt::Key_Enter, Qt::NoModifier);
  187. QApplication::sendEvent(this->view(), &event);
  188. }
  189. emit this->popupHide();
  190. //qDebug() << "after2: " << this->currentIndex() << this->view()->currentIndex();
  191. }
  192. }
  193. // -------------------------------------------------------------------------
  194. void ctkTreeComboBox::paintEvent(QPaintEvent *p)
  195. {
  196. //qDebug() << __FUNCTION__ << " " << this->currentText() << " " << this->currentIndex() ;
  197. //qDebug() << this->itemText(0) << this->itemText(1);
  198. this->QComboBox::paintEvent(p);
  199. }
  200. // -------------------------------------------------------------------------
  201. QTreeView* ctkTreeComboBox::treeView()const
  202. {
  203. return qobject_cast<QTreeView*>(this->view());
  204. }
  205. // -------------------------------------------------------------------------
  206. void ctkTreeComboBox::resizePopup()
  207. {
  208. // copied from QComboBox.cpp
  209. Q_D(ctkTreeComboBox);
  210. QStyle * const style = this->style();
  211. QWidget* container = qobject_cast<QWidget*>(this->view()->parent());
  212. QStyleOptionComboBox opt;
  213. this->initStyleOption(&opt);
  214. QRect listRect(style->subControlRect(QStyle::CC_ComboBox, &opt,
  215. QStyle::SC_ComboBoxListBoxPopup, this));
  216. QRect screen = QApplication::desktop()->availableGeometry(
  217. QApplication::desktop()->screenNumber(this));
  218. QPoint below = this->mapToGlobal(listRect.bottomLeft());
  219. int belowHeight = screen.bottom() - below.y();
  220. QPoint above = this->mapToGlobal(listRect.topLeft());
  221. int aboveHeight = above.y() - screen.y();
  222. bool boundToScreen = !this->window()->testAttribute(Qt::WA_DontShowOnScreen);
  223. const bool usePopup = style->styleHint(QStyle::SH_ComboBox_Popup, &opt, this);
  224. {
  225. int listHeight = 0;
  226. int count = 0;
  227. QStack<QModelIndex> toCheck;
  228. toCheck.push(this->view()->rootIndex());
  229. #ifndef QT_NO_TREEVIEW
  230. QTreeView *treeView = qobject_cast<QTreeView*>(this->view());
  231. if (treeView && treeView->header() && !treeView->header()->isHidden())
  232. listHeight += treeView->header()->height();
  233. #endif
  234. while (!toCheck.isEmpty())
  235. {
  236. QModelIndex parent = toCheck.pop();
  237. for (int i = 0; i < this->model()->rowCount(parent); ++i)
  238. {
  239. QModelIndex idx = this->model()->index(i, this->modelColumn(), parent);
  240. if (!idx.isValid())
  241. {
  242. continue;
  243. }
  244. listHeight += this->view()->visualRect(idx).height(); /* + container->spacing() */;
  245. #ifndef QT_NO_TREEVIEW
  246. if (this->model()->hasChildren(idx) && treeView && treeView->isExpanded(idx))
  247. {
  248. toCheck.push(idx);
  249. }
  250. #endif
  251. ++count;
  252. if (!usePopup && count > this->maxVisibleItems())
  253. {
  254. toCheck.clear();
  255. break;
  256. }
  257. }
  258. }
  259. listRect.setHeight(listHeight);
  260. }
  261. {
  262. // add the spacing for the grid on the top and the bottom;
  263. int heightMargin = 0;//2*container->spacing();
  264. // add the frame of the container
  265. int marginTop, marginBottom;
  266. container->getContentsMargins(0, &marginTop, 0, &marginBottom);
  267. heightMargin += marginTop + marginBottom;
  268. //add the frame of the view
  269. this->view()->getContentsMargins(0, &marginTop, 0, &marginBottom);
  270. //marginTop += static_cast<QAbstractScrollAreaPrivate *>(QObjectPrivate::get(this->view()))->top;
  271. //marginBottom += static_cast<QAbstractScrollAreaPrivate *>(QObjectPrivate::get(this->view()))->bottom;
  272. heightMargin += marginTop + marginBottom;
  273. listRect.setHeight(listRect.height() + heightMargin);
  274. }
  275. // Add space for margin at top and bottom if the style wants it.
  276. if (usePopup)
  277. {
  278. listRect.setHeight(listRect.height() + style->pixelMetric(QStyle::PM_MenuVMargin, &opt, this) * 2);
  279. }
  280. // Make sure the popup is wide enough to display its contents.
  281. if (usePopup)
  282. {
  283. const int diff = d->computeWidthHint() - this->width();
  284. if (diff > 0)
  285. {
  286. listRect.setWidth(listRect.width() + diff);
  287. }
  288. }
  289. //we need to activate the layout to make sure the min/maximum size are set when the widget was not yet show
  290. container->layout()->activate();
  291. //takes account of the minimum/maximum size of the container
  292. listRect.setSize( listRect.size().expandedTo(container->minimumSize())
  293. .boundedTo(container->maximumSize()));
  294. // make sure the widget fits on screen
  295. if (boundToScreen)
  296. {
  297. if (listRect.width() > screen.width() )
  298. {
  299. listRect.setWidth(screen.width());
  300. }
  301. if (this->mapToGlobal(listRect.bottomRight()).x() > screen.right())
  302. {
  303. below.setX(screen.x() + screen.width() - listRect.width());
  304. above.setX(screen.x() + screen.width() - listRect.width());
  305. }
  306. if (this->mapToGlobal(listRect.topLeft()).x() < screen.x() )
  307. {
  308. below.setX(screen.x());
  309. above.setX(screen.x());
  310. }
  311. }
  312. if (usePopup)
  313. {
  314. // Position horizontally.
  315. listRect.moveLeft(above.x());
  316. #ifndef Q_WS_S60
  317. // Position vertically so the curently selected item lines up
  318. // with the combo box.
  319. const QRect currentItemRect = this->view()->visualRect(this->view()->currentIndex());
  320. const int offset = listRect.top() - currentItemRect.top();
  321. listRect.moveTop(above.y() + offset - listRect.top());
  322. #endif
  323. // Clamp the listRect height and vertical position so we don't expand outside the
  324. // available screen geometry.This may override the vertical position, but it is more
  325. // important to show as much as possible of the popup.
  326. const int height = !boundToScreen ? listRect.height() : qMin(listRect.height(), screen.height());
  327. #ifdef Q_WS_S60
  328. //popup needs to be stretched with screen minimum dimension
  329. listRect.setHeight(qMin(screen.height(), screen.width()));
  330. #else
  331. listRect.setHeight(height);
  332. #endif
  333. if (boundToScreen)
  334. {
  335. if (listRect.top() < screen.top())
  336. {
  337. listRect.moveTop(screen.top());
  338. }
  339. if (listRect.bottom() > screen.bottom())
  340. {
  341. listRect.moveBottom(screen.bottom());
  342. }
  343. }
  344. #ifdef Q_WS_S60
  345. if (screen.width() < screen.height())
  346. {
  347. // in portait, menu should be positioned above softkeys
  348. listRect.moveBottom(screen.bottom());
  349. }
  350. else
  351. {
  352. TRect staConTopRect = TRect();
  353. AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EStaconTop, staConTopRect);
  354. listRect.setWidth(listRect.height());
  355. //by default popup is centered on screen in landscape
  356. listRect.moveCenter(screen.center());
  357. if (staConTopRect.IsEmpty())
  358. {
  359. // landscape without stacon, menu should be at the right
  360. (opt.direction == Qt::LeftToRight) ? listRect.setRight(screen.right()) :
  361. listRect.setLeft(screen.left());
  362. }
  363. }
  364. #endif
  365. }
  366. else if (!boundToScreen || listRect.height() <= belowHeight)
  367. {
  368. listRect.moveTopLeft(below);
  369. }
  370. else if (listRect.height() <= aboveHeight)
  371. {
  372. listRect.moveBottomLeft(above);
  373. }
  374. else if (belowHeight >= aboveHeight)
  375. {
  376. listRect.setHeight(belowHeight);
  377. listRect.moveTopLeft(below);
  378. }
  379. else
  380. {
  381. listRect.setHeight(aboveHeight);
  382. listRect.moveBottomLeft(above);
  383. }
  384. #if QT_VERSION < QT_VERSION_CHECK(5,0,0) && !defined QT_NO_IM
  385. if (QInputContext *qic = this->inputContext())
  386. {
  387. qic->reset();
  388. }
  389. #endif
  390. QScrollBar *sb = this->view()->horizontalScrollBar();
  391. Qt::ScrollBarPolicy policy = this->view()->horizontalScrollBarPolicy();
  392. bool needHorizontalScrollBar =
  393. (policy == Qt::ScrollBarAsNeeded || policy == Qt::ScrollBarAlwaysOn)
  394. && sb->minimum() < sb->maximum();
  395. if (needHorizontalScrollBar)
  396. {
  397. listRect.adjust(0, 0, 0, sb->height());
  398. }
  399. container->setGeometry(listRect);
  400. }