ctkMenuComboBoxEventTranslator.cpp 4.8 KB

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