ctkMenuComboBox.cpp 17 KB

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