ctkMenuComboBox.cpp 15 KB

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