ctkMenuComboBox.cpp 13 KB

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