ctkCheckableComboBoxEventTranslator.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <QEvent>
  16. // CTK includes
  17. #include "ctkCheckableComboBox.h"
  18. #include "ctkCheckableComboBoxEventTranslator.h"
  19. #include "ctkCheckableModelHelper.h"
  20. // ----------------------------------------------------------------------------
  21. ctkCheckableComboBoxEventTranslator::ctkCheckableComboBoxEventTranslator(QObject *parent)
  22. : pqWidgetEventTranslator(parent)
  23. {
  24. this->CurrentObject = 0;
  25. }
  26. // ----------------------------------------------------------------------------
  27. bool ctkCheckableComboBoxEventTranslator::translateEvent(QObject *Object,
  28. QEvent *Event,
  29. bool &Error)
  30. {
  31. Q_UNUSED(Error);
  32. ctkCheckableComboBox* checkableCombo = NULL;
  33. for(QObject* test = Object; checkableCombo == NULL && test != NULL; test = test->parent())
  34. {
  35. checkableCombo = qobject_cast<ctkCheckableComboBox*>(test);
  36. }
  37. if(!checkableCombo)
  38. {
  39. // not for me
  40. return false;
  41. }
  42. if(Event->type() == QEvent::Enter && Object == checkableCombo)
  43. {
  44. if(this->CurrentObject != Object)
  45. {
  46. if(this->CurrentObject)
  47. {
  48. disconnect(this->CurrentObject, 0, this, 0);
  49. }
  50. this->CurrentObject = Object;
  51. connect(checkableCombo, SIGNAL(destroyed(QObject*)), this, SLOT(onDestroyed(QObject*)));
  52. connect(checkableCombo, SIGNAL(activated(const QString&)), this, SLOT(onStateChanged(const QString&)));
  53. connect(checkableCombo, SIGNAL(editTextChanged(const QString&)), this, SLOT(onStateChanged(const QString&)));
  54. connect(checkableCombo->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onDataChanged(const QModelIndex&, const QModelIndex&)));
  55. }
  56. }
  57. return true;
  58. }
  59. // ----------------------------------------------------------------------------
  60. void ctkCheckableComboBoxEventTranslator::onDestroyed(QObject* /*Object*/)
  61. {
  62. this->CurrentObject = 0;
  63. }
  64. // ----------------------------------------------------------------------------
  65. void ctkCheckableComboBoxEventTranslator::onStateChanged(const QString& State)
  66. {
  67. emit recordEvent(this->CurrentObject, "set_string", State);
  68. }
  69. // ----------------------------------------------------------------------------
  70. void ctkCheckableComboBoxEventTranslator::onDataChanged(const QModelIndex&,const QModelIndex&)
  71. {
  72. ctkCheckableComboBox* checkableCombo = qobject_cast<ctkCheckableComboBox*>(this->CurrentObject);
  73. if (checkableCombo->checkedIndexes().count() > this->OldIndexList.count())
  74. {
  75. QModelIndexList checkList = checkableCombo->checkedIndexes();
  76. QStringList checkStringList;
  77. foreach(QModelIndex index, checkList)
  78. {
  79. checkStringList << QString::number(index.row());
  80. }
  81. emit recordEvent(this->CurrentObject,"check_indexes", checkStringList.join(" "));
  82. }
  83. else
  84. {
  85. QModelIndex startIndex = checkableCombo->model()->index(0,0, checkableCombo->rootModelIndex());
  86. QModelIndexList uncheckedList = checkableCombo->model()->match(
  87. startIndex, Qt::CheckStateRole, Qt::Unchecked, -1, Qt::MatchRecursive);
  88. QStringList uncheckedStringList;
  89. foreach (QModelIndex index, uncheckedList)
  90. {
  91. uncheckedStringList << QString::number(index.row());
  92. }
  93. emit recordEvent(this->CurrentObject,"uncheck_indexes", uncheckedStringList.join(" "));
  94. }
  95. this->OldIndexList = checkableCombo->checkedIndexes();
  96. }