ctkTreeComboBox.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. }
  160. // -------------------------------------------------------------------------
  161. void ctkTreeComboBox::hidePopup()
  162. {
  163. Q_D(ctkTreeComboBox);
  164. if (d->SkipNextHide)
  165. {// don't hide the popup if the selected item is a parent.
  166. d->SkipNextHide = false;
  167. //this->setCurrentIndex(-1);
  168. //qDebug() << "skip";
  169. //this->QComboBox::showPopup();
  170. }
  171. else
  172. {
  173. //QModelIndex _currentIndex = this->view()->currentIndex();
  174. //qDebug() << "ctkTreeComboBox::hidePopup() " << _currentIndex << " " << _currentIndex.row();
  175. //qDebug() << "before: " << this->currentIndex() << this->view()->currentIndex();
  176. this->QComboBox::hidePopup();
  177. //qDebug() << "after: " << this->currentIndex() << this->view()->currentIndex();
  178. //this->setRootModelIndex(_currentIndex.parent());
  179. //this->setCurrentIndex(_currentIndex.row());
  180. if (d->SendCurrentItem)
  181. {
  182. d->SendCurrentItem = false;
  183. QKeyEvent event(QEvent::ShortcutOverride, Qt::Key_Enter, Qt::NoModifier);
  184. QApplication::sendEvent(this->view(), &event);
  185. }
  186. //qDebug() << "after2: " << this->currentIndex() << this->view()->currentIndex();
  187. }
  188. }
  189. // -------------------------------------------------------------------------
  190. void ctkTreeComboBox::paintEvent(QPaintEvent *p)
  191. {
  192. //qDebug() << __FUNCTION__ << " " << this->currentText() << " " << this->currentIndex() ;
  193. //qDebug() << this->itemText(0) << this->itemText(1);
  194. this->QComboBox::paintEvent(p);
  195. }
  196. // -------------------------------------------------------------------------
  197. QTreeView* ctkTreeComboBox::treeView()const
  198. {
  199. return qobject_cast<QTreeView*>(this->view());
  200. }
  201. // -------------------------------------------------------------------------
  202. void ctkTreeComboBox::resizePopup()
  203. {
  204. // copied from QComboBox.cpp
  205. Q_D(ctkTreeComboBox);
  206. QStyle * const style = this->style();
  207. QWidget* container = qobject_cast<QWidget*>(this->view()->parent());
  208. QStyleOptionComboBox opt;
  209. this->initStyleOption(&opt);
  210. QRect listRect(style->subControlRect(QStyle::CC_ComboBox, &opt,
  211. QStyle::SC_ComboBoxListBoxPopup, this));
  212. QRect screen = QApplication::desktop()->availableGeometry(
  213. QApplication::desktop()->screenNumber(this));
  214. QPoint below = this->mapToGlobal(listRect.bottomLeft());
  215. int belowHeight = screen.bottom() - below.y();
  216. QPoint above = this->mapToGlobal(listRect.topLeft());
  217. int aboveHeight = above.y() - screen.y();
  218. bool boundToScreen = !this->window()->testAttribute(Qt::WA_DontShowOnScreen);
  219. const bool usePopup = style->styleHint(QStyle::SH_ComboBox_Popup, &opt, this);
  220. {
  221. int listHeight = 0;
  222. int count = 0;
  223. QStack<QModelIndex> toCheck;
  224. toCheck.push(this->view()->rootIndex());
  225. #ifndef QT_NO_TREEVIEW
  226. QTreeView *treeView = qobject_cast<QTreeView*>(this->view());
  227. if (treeView && treeView->header() && !treeView->header()->isHidden())
  228. listHeight += treeView->header()->height();
  229. #endif
  230. while (!toCheck.isEmpty())
  231. {
  232. QModelIndex parent = toCheck.pop();
  233. for (int i = 0; i < this->model()->rowCount(parent); ++i)
  234. {
  235. QModelIndex idx = this->model()->index(i, this->modelColumn(), parent);
  236. if (!idx.isValid())
  237. {
  238. continue;
  239. }
  240. listHeight += this->view()->visualRect(idx).height(); /* + container->spacing() */;
  241. #ifndef QT_NO_TREEVIEW
  242. if (this->model()->hasChildren(idx) && treeView && treeView->isExpanded(idx))
  243. {
  244. toCheck.push(idx);
  245. }
  246. #endif
  247. ++count;
  248. if (!usePopup && count > this->maxVisibleItems())
  249. {
  250. toCheck.clear();
  251. break;
  252. }
  253. }
  254. }
  255. listRect.setHeight(listHeight);
  256. }
  257. {
  258. // add the spacing for the grid on the top and the bottom;
  259. int heightMargin = 0;//2*container->spacing();
  260. // add the frame of the container
  261. int marginTop, marginBottom;
  262. container->getContentsMargins(0, &marginTop, 0, &marginBottom);
  263. heightMargin += marginTop + marginBottom;
  264. //add the frame of the view
  265. this->view()->getContentsMargins(0, &marginTop, 0, &marginBottom);
  266. //marginTop += static_cast<QAbstractScrollAreaPrivate *>(QObjectPrivate::get(this->view()))->top;
  267. //marginBottom += static_cast<QAbstractScrollAreaPrivate *>(QObjectPrivate::get(this->view()))->bottom;
  268. heightMargin += marginTop + marginBottom;
  269. listRect.setHeight(listRect.height() + heightMargin);
  270. }
  271. // Add space for margin at top and bottom if the style wants it.
  272. if (usePopup)
  273. {
  274. listRect.setHeight(listRect.height() + style->pixelMetric(QStyle::PM_MenuVMargin, &opt, this) * 2);
  275. }
  276. // Make sure the popup is wide enough to display its contents.
  277. if (usePopup)
  278. {
  279. const int diff = d->computeWidthHint() - this->width();
  280. if (diff > 0)
  281. {
  282. listRect.setWidth(listRect.width() + diff);
  283. }
  284. }
  285. //we need to activate the layout to make sure the min/maximum size are set when the widget was not yet show
  286. container->layout()->activate();
  287. //takes account of the minimum/maximum size of the container
  288. listRect.setSize( listRect.size().expandedTo(container->minimumSize())
  289. .boundedTo(container->maximumSize()));
  290. // make sure the widget fits on screen
  291. if (boundToScreen)
  292. {
  293. if (listRect.width() > screen.width() )
  294. {
  295. listRect.setWidth(screen.width());
  296. }
  297. if (this->mapToGlobal(listRect.bottomRight()).x() > screen.right())
  298. {
  299. below.setX(screen.x() + screen.width() - listRect.width());
  300. above.setX(screen.x() + screen.width() - listRect.width());
  301. }
  302. if (this->mapToGlobal(listRect.topLeft()).x() < screen.x() )
  303. {
  304. below.setX(screen.x());
  305. above.setX(screen.x());
  306. }
  307. }
  308. if (usePopup)
  309. {
  310. // Position horizontally.
  311. listRect.moveLeft(above.x());
  312. #ifndef Q_WS_S60
  313. // Position vertically so the curently selected item lines up
  314. // with the combo box.
  315. const QRect currentItemRect = this->view()->visualRect(this->view()->currentIndex());
  316. const int offset = listRect.top() - currentItemRect.top();
  317. listRect.moveTop(above.y() + offset - listRect.top());
  318. #endif
  319. // Clamp the listRect height and vertical position so we don't expand outside the
  320. // available screen geometry.This may override the vertical position, but it is more
  321. // important to show as much as possible of the popup.
  322. const int height = !boundToScreen ? listRect.height() : qMin(listRect.height(), screen.height());
  323. #ifdef Q_WS_S60
  324. //popup needs to be stretched with screen minimum dimension
  325. listRect.setHeight(qMin(screen.height(), screen.width()));
  326. #else
  327. listRect.setHeight(height);
  328. #endif
  329. if (boundToScreen)
  330. {
  331. if (listRect.top() < screen.top())
  332. {
  333. listRect.moveTop(screen.top());
  334. }
  335. if (listRect.bottom() > screen.bottom())
  336. {
  337. listRect.moveBottom(screen.bottom());
  338. }
  339. }
  340. #ifdef Q_WS_S60
  341. if (screen.width() < screen.height())
  342. {
  343. // in portait, menu should be positioned above softkeys
  344. listRect.moveBottom(screen.bottom());
  345. }
  346. else
  347. {
  348. TRect staConTopRect = TRect();
  349. AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EStaconTop, staConTopRect);
  350. listRect.setWidth(listRect.height());
  351. //by default popup is centered on screen in landscape
  352. listRect.moveCenter(screen.center());
  353. if (staConTopRect.IsEmpty())
  354. {
  355. // landscape without stacon, menu should be at the right
  356. (opt.direction == Qt::LeftToRight) ? listRect.setRight(screen.right()) :
  357. listRect.setLeft(screen.left());
  358. }
  359. }
  360. #endif
  361. }
  362. else if (!boundToScreen || listRect.height() <= belowHeight)
  363. {
  364. listRect.moveTopLeft(below);
  365. }
  366. else if (listRect.height() <= aboveHeight)
  367. {
  368. listRect.moveBottomLeft(above);
  369. }
  370. else if (belowHeight >= aboveHeight)
  371. {
  372. listRect.setHeight(belowHeight);
  373. listRect.moveTopLeft(below);
  374. }
  375. else
  376. {
  377. listRect.setHeight(aboveHeight);
  378. listRect.moveBottomLeft(above);
  379. }
  380. #ifndef QT_NO_IM
  381. if (QInputContext *qic = this->inputContext())
  382. {
  383. qic->reset();
  384. }
  385. #endif
  386. QScrollBar *sb = this->view()->horizontalScrollBar();
  387. Qt::ScrollBarPolicy policy = this->view()->horizontalScrollBarPolicy();
  388. bool needHorizontalScrollBar =
  389. (policy == Qt::ScrollBarAsNeeded || policy == Qt::ScrollBarAlwaysOn)
  390. && sb->minimum() < sb->maximum();
  391. if (needHorizontalScrollBar)
  392. {
  393. listRect.adjust(0, 0, 0, sb->height());
  394. }
  395. container->setGeometry(listRect);
  396. }