ctkMenuComboBox.cpp 13 KB

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