ctkMenuComboBox.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 <QActionEvent>
  16. #include <QCompleter>
  17. #include <QDebug>
  18. #include <QEvent>
  19. #include <QHBoxLayout>
  20. #include <QLineEdit>
  21. #include <QStringList>
  22. #include <QStringListModel>
  23. // CTK includes
  24. #include "ctkSearchBox.h"
  25. #include "ctkMenuComboBox_p.h"
  26. // -------------------------------------------------------------------------
  27. ctkMenuComboBoxInternal::ctkMenuComboBoxInternal()
  28. {
  29. }
  30. // -------------------------------------------------------------------------
  31. ctkMenuComboBoxInternal::~ctkMenuComboBoxInternal()
  32. {
  33. }
  34. // -------------------------------------------------------------------------
  35. void ctkMenuComboBoxInternal::showPopup()
  36. {
  37. QMenu* menu = this->Menu.data();
  38. if (!menu)
  39. {
  40. return;
  41. }
  42. menu->popup(this->mapToGlobal(this->rect().bottomLeft()));
  43. static int minWidth = menu->sizeHint().width();
  44. menu->setFixedWidth(qMax(this->width(), minWidth));
  45. emit popupShown();
  46. }
  47. // -------------------------------------------------------------------------
  48. QSize ctkMenuComboBoxInternal::minimumSizeHint()const
  49. {
  50. // Cached QComboBox::minimumSizeHint is not recomputed when the current
  51. // index change, however QComboBox::sizeHint is. Use it instead.
  52. return this->sizeHint();
  53. }
  54. // -------------------------------------------------------------------------
  55. ctkMenuComboBoxPrivate::ctkMenuComboBoxPrivate(ctkMenuComboBox& object)
  56. :q_ptr(&object)
  57. {
  58. this->MenuComboBox = 0;
  59. this->SearchCompleter = 0;
  60. this->EditBehavior = ctkMenuComboBox::EditableOnPopup;
  61. this->IsDefaultTextCurrent = true;
  62. this->IsDefaultIconCurrent = true;
  63. }
  64. // -------------------------------------------------------------------------
  65. void ctkMenuComboBoxPrivate::init()
  66. {
  67. Q_Q(ctkMenuComboBox);
  68. this->setParent(q);
  69. QHBoxLayout* layout = new QHBoxLayout(q);
  70. layout->setContentsMargins(0,0,0,0);
  71. layout->setSizeConstraint(QLayout::SetMinimumSize);
  72. this->MenuComboBox = new ctkMenuComboBoxInternal();
  73. this->MenuComboBox->setMinimumContentsLength(12);
  74. layout->addWidget(this->MenuComboBox);
  75. this->MenuComboBox->installEventFilter(q);
  76. this->MenuComboBox->setSizeAdjustPolicy(QComboBox::AdjustToContents);
  77. this->MenuComboBox->addItem(this->DefaultIcon, this->DefaultText);
  78. this->SearchCompleter = new QCompleter(QStringList(), q);
  79. this->SearchCompleter->setCaseSensitivity(Qt::CaseInsensitive);
  80. // Automatically set the minimumSizeHint of the layout to the widget
  81. layout->setSizeConstraint(QLayout::SetMinimumSize);
  82. // Behave like a QComboBox
  83. q->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed,
  84. QSizePolicy::ComboBox));
  85. q->setDefaultText(ctkMenuComboBox::tr("Search..."));
  86. }
  87. // ------------------------------------------------------------------------
  88. QAction* ctkMenuComboBoxPrivate::actionByTitle(const QString& text, const QMenu* parentMenu)
  89. {
  90. if (parentMenu->title() == text)
  91. {
  92. return 0;
  93. }
  94. foreach(QAction* action, parentMenu->actions())
  95. {
  96. if (!action->menu() && action->text().toLower() == text.toLower())
  97. {
  98. return action;
  99. }
  100. if (action->menu())
  101. {
  102. QAction* subAction = this->actionByTitle(text, action->menu());
  103. if(subAction)
  104. {
  105. return subAction;
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. // ------------------------------------------------------------------------
  112. void ctkMenuComboBoxPrivate::setCurrentText(const QString& newCurrentText)
  113. {
  114. if (this->MenuComboBox->lineEdit())
  115. {
  116. static_cast<ctkSearchBox*>(this->MenuComboBox->lineEdit())
  117. ->setPlaceholderText(newCurrentText);
  118. }
  119. this->MenuComboBox->setItemText(this->MenuComboBox->currentIndex(),
  120. newCurrentText);
  121. }
  122. // ------------------------------------------------------------------------
  123. QString ctkMenuComboBoxPrivate::currentText()const
  124. {
  125. return this->MenuComboBox->itemText(this->MenuComboBox->currentIndex());
  126. }
  127. // ------------------------------------------------------------------------
  128. QIcon ctkMenuComboBoxPrivate::currentIcon()const
  129. {
  130. return this->MenuComboBox->itemIcon(this->MenuComboBox->currentIndex());
  131. }
  132. // ------------------------------------------------------------------------
  133. void ctkMenuComboBoxPrivate::setCurrentIcon(const QIcon& newCurrentIcon)
  134. {
  135. this->MenuComboBox->setItemIcon(this->MenuComboBox->currentIndex(),
  136. newCurrentIcon);
  137. }
  138. // -------------------------------------------------------------------------
  139. void ctkMenuComboBoxPrivate::setComboBoxEditable(bool edit)
  140. {
  141. Q_Q(ctkMenuComboBox);
  142. if(edit)
  143. {
  144. if (!this->MenuComboBox->lineEdit())
  145. {
  146. ctkSearchBox* line = new ctkSearchBox();
  147. this->MenuComboBox->setLineEdit(line);
  148. q->connect(line, SIGNAL(returnPressed()),
  149. q,SLOT(onReturnPressed()));
  150. }
  151. this->MenuComboBox->setCompleter(this->SearchCompleter);
  152. }
  153. this->MenuComboBox->setEditable(edit);
  154. }
  155. // -------------------------------------------------------------------------
  156. void ctkMenuComboBoxPrivate::addAction(QAction *action)
  157. {
  158. if (action->menu())
  159. {
  160. this->addMenuToCompleter(action->menu());
  161. }
  162. else
  163. {
  164. this->addActionToCompleter(action);
  165. }
  166. }
  167. // -------------------------------------------------------------------------
  168. void ctkMenuComboBoxPrivate::addMenuToCompleter(QMenu* menu)
  169. {
  170. Q_Q(ctkMenuComboBox);
  171. menu->installEventFilter(q);
  172. // Bug QT : see this link for more details
  173. // https://bugreports.qt.nokia.com/browse/QTBUG-20929?focusedCommentId=161370#comment-161370
  174. // if the submenu doesn't have a parent, the submenu triggered(QAction*)
  175. // signal is not propagated. So we listened this submenu to fix the bug.
  176. QObject* emptyObject = 0;
  177. if(menu->parent() == emptyObject)
  178. {
  179. q->connect(menu, SIGNAL(triggered(QAction*)),
  180. q, SLOT(onActionSelected(QAction*)), Qt::UniqueConnection);
  181. }
  182. foreach (QAction* action, menu->actions())
  183. {
  184. this->addAction(action);
  185. }
  186. }
  187. // -------------------------------------------------------------------------
  188. void ctkMenuComboBoxPrivate::addActionToCompleter(QAction *action)
  189. {
  190. QStringListModel* model = qobject_cast<QStringListModel* >(this->SearchCompleter->model());
  191. Q_ASSERT(model);
  192. QModelIndex start = this->SearchCompleter->model()->index(0,0);
  193. QModelIndexList indexList = this->SearchCompleter->model()->match(
  194. start, 0, action->text());
  195. if (indexList.count())
  196. {
  197. return;
  198. }
  199. int actionCount = this->SearchCompleter->model()->rowCount();
  200. this->SearchCompleter->model()->insertRow(actionCount);
  201. QModelIndex index = this->SearchCompleter->model()->index(actionCount, 0);
  202. this->SearchCompleter->model()->setData(index, action->text());
  203. }
  204. // ------------------------------------------------------------------------
  205. void ctkMenuComboBoxPrivate::removeActionToCompleter(QAction *action)
  206. {
  207. QStringListModel* model = qobject_cast<QStringListModel* >(this->SearchCompleter->model());
  208. Q_ASSERT(model);
  209. if (!model->stringList().contains(action->text()) )
  210. {
  211. return;
  212. }
  213. // Maybe the action is present multiple times in different submenus
  214. // Don't remove its entry from the completer model if there are still some action instances
  215. // in the menus.
  216. if (this->actionByTitle(action->text(), this->Menu.data()))
  217. {
  218. return;
  219. }
  220. QModelIndex start = this->SearchCompleter->model()->index(0,0);
  221. QModelIndexList indexList = this->SearchCompleter->model()->match(
  222. start, 0, action->text());
  223. Q_ASSERT(indexList.count() == 1);
  224. foreach (QModelIndex index, indexList)
  225. {
  226. // Search completer model is a flat list
  227. this->SearchCompleter->model()->removeRow(index.row());
  228. }
  229. }
  230. // ------------------------------------------------------------------------
  231. ctkMenuComboBox::ctkMenuComboBox(QWidget* _parent)
  232. :QWidget(_parent)
  233. , d_ptr(new ctkMenuComboBoxPrivate(*this))
  234. {
  235. Q_D(ctkMenuComboBox);
  236. d->init();
  237. }
  238. // ------------------------------------------------------------------------
  239. ctkMenuComboBox::~ctkMenuComboBox()
  240. {
  241. }
  242. // ------------------------------------------------------------------------
  243. void ctkMenuComboBox::setMenu(QMenu* menu)
  244. {
  245. Q_D(ctkMenuComboBox);
  246. if (d->Menu.data() == menu)
  247. {
  248. return;
  249. }
  250. if (d->Menu)
  251. {
  252. this->removeAction(d->Menu.data()->menuAction());
  253. QObject::disconnect(d->Menu.data(),SIGNAL(triggered(QAction*)),
  254. this,SLOT(onActionSelected(QAction*)));
  255. }
  256. d->Menu = menu;
  257. d->MenuComboBox->Menu = menu;
  258. d->addMenuToCompleter(menu);
  259. if (d->Menu)
  260. {
  261. this->addAction(d->Menu.data()->menuAction());
  262. QObject::connect(d->Menu.data(),SIGNAL(triggered(QAction*)),
  263. this,SLOT(onActionSelected(QAction*)), Qt::UniqueConnection);
  264. }
  265. }
  266. // -------------------------------------------------------------------------
  267. QMenu* ctkMenuComboBox::menu()const
  268. {
  269. Q_D(const ctkMenuComboBox);
  270. return d->Menu.data();
  271. }
  272. // -------------------------------------------------------------------------
  273. void ctkMenuComboBox::setDefaultText(const QString& newDefaultText)
  274. {
  275. Q_D(ctkMenuComboBox);
  276. d->DefaultText = newDefaultText;
  277. if (d->IsDefaultTextCurrent)
  278. {
  279. d->setCurrentText(d->DefaultText);
  280. }
  281. }
  282. // -------------------------------------------------------------------------
  283. QString ctkMenuComboBox::defaultText()const
  284. {
  285. Q_D(const ctkMenuComboBox);
  286. return d->DefaultText;
  287. }
  288. // -------------------------------------------------------------------------
  289. void ctkMenuComboBox::setDefaultIcon(const QIcon& newIcon)
  290. {
  291. Q_D(ctkMenuComboBox);
  292. d->DefaultIcon = newIcon;
  293. if (d->IsDefaultIconCurrent)
  294. {
  295. d->setCurrentIcon(d->DefaultIcon);
  296. }
  297. }
  298. // -------------------------------------------------------------------------
  299. QIcon ctkMenuComboBox::defaultIcon()const
  300. {
  301. Q_D(const ctkMenuComboBox);
  302. return d->DefaultIcon;
  303. }
  304. // -------------------------------------------------------------------------
  305. void ctkMenuComboBox::setEditableBehavior(ctkMenuComboBox::EditableBehavior edit)
  306. {
  307. Q_D(ctkMenuComboBox);
  308. d->EditBehavior = edit;
  309. this->disconnect(d->MenuComboBox, SIGNAL(popupShown()),
  310. d, SLOT(setComboBoxEditable()));
  311. switch (edit)
  312. {
  313. case ctkMenuComboBox::Editable:
  314. d->MenuComboBox->setContextMenuPolicy(Qt::DefaultContextMenu);
  315. d->setComboBoxEditable(true);
  316. break;
  317. case ctkMenuComboBox::NotEditable:
  318. d->MenuComboBox->setContextMenuPolicy(Qt::DefaultContextMenu);
  319. d->setComboBoxEditable(false);
  320. break;
  321. case ctkMenuComboBox::EditableOnFocus:
  322. d->setComboBoxEditable(this->hasFocus());
  323. // Here we set the context menu policy to fix a crash on the right click.
  324. // When the line edit lost the focus, the comboBox become not editable,
  325. // and the line edit is deleted. that cause a crash when we have the right click,
  326. // which call a popup, because the focus is losted.
  327. d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
  328. break;
  329. case ctkMenuComboBox::EditableOnPopup:
  330. d->setComboBoxEditable(false);
  331. this->connect(d->MenuComboBox, SIGNAL(popupShown()),
  332. d, SLOT(setComboBoxEditable()));
  333. // Same reason.
  334. d->MenuComboBox->setContextMenuPolicy(Qt::NoContextMenu);
  335. break;
  336. }
  337. }
  338. // -------------------------------------------------------------------------
  339. ctkMenuComboBox::EditableBehavior ctkMenuComboBox::editableBehavior()const
  340. {
  341. Q_D(const ctkMenuComboBox);
  342. return d->EditBehavior;
  343. }
  344. // -------------------------------------------------------------------------
  345. void ctkMenuComboBox::setMinimumContentsLength(int characters)
  346. {
  347. Q_D(ctkMenuComboBox);
  348. d->MenuComboBox->setMinimumContentsLength(characters);
  349. }
  350. // -------------------------------------------------------------------------
  351. void ctkMenuComboBox::onActionSelected(QAction* action)
  352. {
  353. Q_D(ctkMenuComboBox);
  354. /// Set the action selected in the combobox.
  355. d->IsDefaultTextCurrent = true;
  356. QString newText = d->DefaultText;
  357. if (action && !action->text().isEmpty())
  358. {
  359. newText = action->text();
  360. d->IsDefaultTextCurrent = false;
  361. }
  362. d->setCurrentText(newText);
  363. d->IsDefaultIconCurrent = true;
  364. QIcon newIcon = d->DefaultIcon;
  365. if (action && !action->icon().isNull())
  366. {
  367. d->IsDefaultIconCurrent = false;
  368. newIcon = action->icon();
  369. }
  370. d->setCurrentIcon(newIcon);
  371. d->MenuComboBox->clearFocus();
  372. emit ctkMenuComboBox::actionChanged(action);
  373. }
  374. // -------------------------------------------------------------------------
  375. void ctkMenuComboBox::clearActiveAction()
  376. {
  377. this->onActionSelected(0);
  378. }
  379. // -------------------------------------------------------------------------
  380. void ctkMenuComboBox::onReturnPressed()
  381. {
  382. Q_D(ctkMenuComboBox);
  383. QAction* action = d->actionByTitle(d->MenuComboBox->lineEdit()->text(), d->Menu.data());
  384. if (!action)
  385. {
  386. return;
  387. }
  388. action->trigger();
  389. }
  390. // -------------------------------------------------------------------------
  391. bool ctkMenuComboBox::eventFilter(QObject* target, QEvent* event)
  392. {
  393. Q_D(ctkMenuComboBox);
  394. if (target == d->MenuComboBox)
  395. {
  396. if (event->type() == QEvent::Resize)
  397. {
  398. this->layout()->invalidate();
  399. }
  400. if (event->type() == QEvent::FocusIn &&
  401. d->EditBehavior == ctkMenuComboBox::EditableOnFocus)
  402. {
  403. d->setComboBoxEditable(true);
  404. }
  405. if (event->type() == QEvent::FocusOut &&
  406. (d->EditBehavior == ctkMenuComboBox::EditableOnFocus ||
  407. d->EditBehavior == ctkMenuComboBox::EditableOnPopup))
  408. {
  409. d->setComboBoxEditable(false);
  410. }
  411. }
  412. else if (event->type() == QEvent::ActionAdded)
  413. {
  414. QActionEvent* actionEvent = static_cast<QActionEvent *>(event);
  415. d->addAction(actionEvent->action());
  416. }
  417. else if (event->type() == QEvent::ActionRemoved)
  418. {
  419. QActionEvent* actionEvent = static_cast<QActionEvent *>(event);
  420. d->removeActionToCompleter(actionEvent->action());
  421. }
  422. return this->Superclass::eventFilter(target, event);
  423. }