ctkTreeComboBoxEventPlayer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <QApplication>
  17. #include <QDebug>
  18. #include <QKeyEvent>
  19. // CTK includes
  20. #include "ctkTreeComboBox.h"
  21. #include "ctkTreeComboBoxEventPlayer.h"
  22. // ----------------------------------------------------------------------------
  23. ctkTreeComboBoxEventPlayer::ctkTreeComboBoxEventPlayer(QObject *parent)
  24. : pqWidgetEventPlayer(parent)
  25. {
  26. }
  27. // ----------------------------------------------------------------------------
  28. bool ctkTreeComboBoxEventPlayer::playEvent(QObject *Object,
  29. const QString &Command,
  30. const QString &Arguments,
  31. bool &Error)
  32. {
  33. if (Command != "showpopup" &&
  34. Command != "hidepopup" &&
  35. Command != "indexChanged")
  36. {
  37. return false;
  38. }
  39. if(ctkTreeComboBox* const object =
  40. qobject_cast<ctkTreeComboBox*>(Object))
  41. {
  42. // if (Command == "showpopup")
  43. // {
  44. // object->showPopup();
  45. // object->update();
  46. // return true;
  47. // }
  48. // if (Command == "hidepopup")
  49. // {
  50. // object->hidePopup();
  51. // object->update();
  52. // return true;
  53. // }
  54. // if (Command == "set_string")
  55. // {
  56. //// qDebug() << "setstring";
  57. // // to implemented
  58. //// qDebug() << object->currentText() << object->view()->currentIndex().data().toString();
  59. //// object->setItemText(0, object->view()->currentIndex().data().toString());
  60. //// return true;
  61. // object->view()->setCurrentIndex(object->view()->currentIndex());
  62. // QKeyEvent event(QEvent::ShortcutOverride, Qt::Key_Enter, Qt::NoModifier);
  63. // // here we conditionally send the event, otherwise, nodeActivated would be
  64. // // fired even if the user didn't manually select the node.
  65. // // Warning: please note that sending a KeyEvent will close the popup menu
  66. // // of the combobox if it is open.
  67. // QApplication::sendEvent(object->view(), &event);
  68. // return true;
  69. // }
  70. if(Command == "indexChanged")
  71. {
  72. QModelIndexList indexes = object->model()->match(
  73. object->model()->index(0, 0), 0, QVariant(Arguments), 1,
  74. Qt::MatchRecursive | Qt::MatchExactly | Qt::MatchWrap);
  75. if(indexes.count() > 0)
  76. {
  77. object->view()->setCurrentIndex(indexes[0]);
  78. QKeyEvent event(QEvent::ShortcutOverride, Qt::Key_Enter, Qt::NoModifier);
  79. // here we conditionally send the event, otherwise, nodeActivated would be
  80. // fired even if the user didn't manually select the node.
  81. // Warning: please note that sending a KeyEvent will close the popup menu
  82. // of the combobox if it is open.
  83. QApplication::sendEvent(object->view(), &event);
  84. }
  85. return true;
  86. }
  87. }
  88. qCritical() << "calling showpopup/hidepopup on unhandled type " << Object;
  89. Error = true;
  90. return true;
  91. }