ctkMenuComboBox.cpp 15 KB

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