ctkTreeComboBox.cpp 15 KB

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