ctkMenuComboBox.cpp 17 KB

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