ctkMenuComboBoxEventTranslator.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <QDebug>
  17. #include <QEvent>
  18. #include <QLineEdit>
  19. #include <QModelIndex>
  20. #include <QToolButton>
  21. // CTK includes
  22. #include "ctkCompleter.h"
  23. #include "ctkMenuComboBox.h"
  24. #include "ctkMenuComboBoxEventTranslator.h"
  25. // ----------------------------------------------------------------------------
  26. ctkMenuComboBoxEventTranslator::ctkMenuComboBoxEventTranslator(QObject *parent)
  27. : pqWidgetEventTranslator(parent)
  28. {
  29. this->CurrentObject = 0;
  30. }
  31. // ----------------------------------------------------------------------------
  32. bool ctkMenuComboBoxEventTranslator::translateEvent(QObject *Object,
  33. QEvent *Event,
  34. bool &Error)
  35. {
  36. Q_UNUSED(Error);
  37. ctkMenuComboBox* menuCombo = NULL;
  38. for(QObject* test = Object; menuCombo == NULL && test != NULL; test = test->parent())
  39. {
  40. menuCombo = qobject_cast<ctkMenuComboBox*>(test);
  41. }
  42. if(!menuCombo)
  43. {
  44. return false;
  45. }
  46. if(Event->type() == QEvent::Enter && Object == menuCombo)
  47. {
  48. if(this->CurrentObject != Object)
  49. {
  50. if(this->CurrentObject)
  51. {
  52. disconnect(this->CurrentObject, 0, this, 0);
  53. }
  54. this->CurrentObject = Object;
  55. connect(menuCombo, SIGNAL(destroyed(QObject*)),
  56. this, SLOT(onDestroyed()));
  57. // to show hide the Menu
  58. // connect(menuCombo->menu(), SIGNAL(triggered(QAction*)),
  59. // this, SLOT(onActionTriggered(QAction*)));
  60. connect(menuCombo->menu(), SIGNAL(aboutToHide()),
  61. this, SLOT(onAboutToHide()));
  62. connect(menuCombo->menu(), SIGNAL(aboutToShow()),
  63. this, SLOT(onAboutToShow()));
  64. // connect for the completer
  65. connect(menuCombo->menuComboBoxInternal(), SIGNAL(editTextChanged(QString)),
  66. this, SLOT(onEditTextChanged(const QString&)));
  67. connect(menuCombo->toolButtonInternal(), SIGNAL(clicked(bool)),
  68. this, SLOT(onToolButtonClicked(bool)));
  69. connect(menuCombo, SIGNAL(actionChanged(QAction*)),
  70. this, SLOT(onActionTriggered(QAction*)));
  71. //connect for the QMenu
  72. }
  73. }
  74. return true;
  75. }
  76. // ----------------------------------------------------------------------------
  77. void ctkMenuComboBoxEventTranslator::onDestroyed()
  78. {
  79. this->CurrentObject = 0;
  80. }
  81. // ----------------------------------------------------------------------------
  82. void ctkMenuComboBoxEventTranslator::onAboutToShow()
  83. {
  84. emit recordEvent(this->CurrentObject, "show_popup", "true");
  85. }
  86. // ----------------------------------------------------------------------------
  87. void ctkMenuComboBoxEventTranslator::onAboutToHide()
  88. {
  89. emit recordEvent(this->CurrentObject, "show_popup", "false");
  90. }
  91. // ----------------------------------------------------------------------------
  92. void ctkMenuComboBoxEventTranslator::onEditTextChanged(const QString& text)
  93. {
  94. emit recordEvent(this->CurrentObject, "set_edit_string_menu", text);
  95. }
  96. // ----------------------------------------------------------------------------
  97. void ctkMenuComboBoxEventTranslator::onToolButtonClicked(bool state)
  98. {
  99. QString arg = state ? "true" : "false";
  100. emit recordEvent(this->CurrentObject, "set_edit", arg);
  101. }
  102. // ----------------------------------------------------------------------------
  103. void ctkMenuComboBoxEventTranslator::onActionTriggered(QAction* action)
  104. {
  105. emit recordEvent(this->CurrentObject, "triggered", action->text());
  106. }
  107. // ----------------------------------------------------------------------------
  108. void ctkMenuComboBoxEventTranslator::connectAllMenu(QMenu* menu)
  109. {
  110. connect(menu, SIGNAL(triggered(QAction*)),
  111. this, SLOT(onActionTriggered(QAction*)));
  112. foreach (QAction* action, menu->actions())
  113. {
  114. if (action->menu())
  115. {
  116. this->connectAllMenu(action->menu());
  117. }
  118. }
  119. }