ctkMenuComboBoxEventPlayer.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <QComboBox>
  16. #include <QCompleter>
  17. #include <QDebug>
  18. #include <QLineEdit>
  19. #include <QToolButton>
  20. #include <QTreeView>
  21. // CTK includes
  22. #include "ctkMenuComboBox.h"
  23. #include "ctkMenuComboBoxEventPlayer.h"
  24. QAction* actionByTitle(const QString& text, const QMenu* parentMenu);
  25. // ----------------------------------------------------------------------------
  26. ctkMenuComboBoxEventPlayer::ctkMenuComboBoxEventPlayer(QObject *parent)
  27. : pqWidgetEventPlayer(parent)
  28. {
  29. }
  30. // ----------------------------------------------------------------------------
  31. bool ctkMenuComboBoxEventPlayer::playEvent(QObject *Object,
  32. const QString &Command,
  33. const QString &Arguments,
  34. bool &Error)
  35. {
  36. if (Command != "set_edit_string_menu" &&
  37. Command != "set_edit" &&
  38. Command != "show_popup" &&
  39. Command != "triggered" &&
  40. Command != "edit_finish")
  41. {
  42. return false;
  43. }
  44. if(ctkMenuComboBox* const object =
  45. qobject_cast<ctkMenuComboBox*>(Object))
  46. {
  47. if (Command == "show_popup")
  48. {
  49. if (Arguments == "true")
  50. {
  51. object->menuComboBoxInternal()->showPopup();
  52. }
  53. else
  54. {
  55. object->menu()->hide();
  56. }
  57. return true;
  58. }
  59. if (Command == "set_edit_string_menu")
  60. {
  61. object->menuComboBoxInternal()->setEditText(Arguments);
  62. // object->menuComboBoxInternal()->completer()->popup()->show();
  63. // qDebug() << "count" << qApp->topLevelWidgets().count();
  64. // foreach (QWidget * o, qApp->topLevelWidgets())
  65. // {
  66. // qDebug() << "o" << o;
  67. // }
  68. object->menuComboBoxInternal()->completer()->popup()->show();
  69. // object->menuComboBoxInternal()->completer()->popup()->hide();
  70. // qDebug() << "count" << qApp->topLevelWidgets().count();
  71. // foreach (QWidget * o, qApp->topLevelWidgets())
  72. // {
  73. // qDebug() << "o" << o;
  74. // }
  75. // object->menuComboBoxInternal()->lineEdit()->completer()->popup()->hide();
  76. // qDebug() << "count" << qApp->topLevelWidgets().count();
  77. // foreach (QWidget * o, qApp->topLevelWidgets())
  78. // {
  79. // qDebug() << "o" << o;
  80. // }
  81. return true;
  82. }
  83. if (Command == "set_edit")
  84. {
  85. const bool state = Arguments == "true" ? true : false;
  86. object->toolButtonInternal()->setChecked(state);
  87. return true;
  88. }
  89. if (Command == "triggered")
  90. {
  91. QAction* action = actionByTitle(Arguments, object->menu());
  92. action->trigger();
  93. return true;
  94. }
  95. }
  96. qCritical() << "calling set_edit_string_menu/set_edit on unhandled type " << Object;
  97. Error = true;
  98. return true;
  99. }
  100. // ------------------------------------------------------------------------
  101. QAction* actionByTitle(const QString& text, const QMenu* parentMenu)
  102. {
  103. if (parentMenu->title() == text)
  104. {
  105. return 0;
  106. }
  107. foreach(QAction* action, parentMenu->actions())
  108. {
  109. if (!action->menu() && action->text().toLower() == text.toLower())
  110. {
  111. return action;
  112. }
  113. if (action->menu())
  114. {
  115. QAction* subAction = actionByTitle(text, action->menu());
  116. if(subAction)
  117. {
  118. return subAction;
  119. }
  120. }
  121. }
  122. return 0;
  123. }