ctkFileDialogEventTranslator.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <QDebug>
  16. #include <QEvent>
  17. #include <QVariant>
  18. // CTK includes
  19. #include "ctkFileDialog.h"
  20. #include "ctkFileDialogEventTranslator.h"
  21. // ----------------------------------------------------------------------------
  22. ctkFileDialogEventTranslator::ctkFileDialogEventTranslator(pqTestUtility* util, QObject *parent)
  23. : pqNativeFileDialogEventTranslator(util, parent),
  24. CurrentObject(0)
  25. {
  26. }
  27. // ----------------------------------------------------------------------------
  28. bool ctkFileDialogEventTranslator::translateEvent(QObject *Object,
  29. QEvent *Event,
  30. bool &Error)
  31. {
  32. Q_UNUSED(Error);
  33. ctkFileDialog* fileDialog = NULL;
  34. for(QObject* test = Object; fileDialog == NULL && test != NULL; test = test->parent())
  35. {
  36. fileDialog = qobject_cast<ctkFileDialog*>(test);
  37. }
  38. if(!fileDialog)
  39. {
  40. return this->Superclass::translateEvent(Object, Event, Error);
  41. }
  42. if((Event->type() == QEvent::Enter || Event->type() == QEvent::ShowToParent) &&
  43. Object == fileDialog)
  44. {
  45. if(this->CurrentObject != Object)
  46. {
  47. if(this->CurrentObject)
  48. {
  49. disconnect(this->CurrentObject, 0, this, 0);
  50. }
  51. this->CurrentObject = Object;
  52. connect(fileDialog, SIGNAL(filesSelected(const QStringList&)),
  53. this, SLOT(onFileSelectionChanged(const QStringList&)));
  54. connect(fileDialog, SIGNAL(rejected()), this, SLOT(onRejected()));
  55. connect(fileDialog, SIGNAL(destroyed(QObject*)),
  56. this, SLOT(onDestroyed(QObject*)));
  57. }
  58. }
  59. // All events performed in the dialog widgets are swallowed (e.g. scrollbar
  60. // events)...
  61. bool acceptEvent = true;
  62. // ...except for the events on the bottom widget (if any).
  63. QWidget* bottomWidget = fileDialog->bottomWidget();
  64. if (bottomWidget)
  65. {
  66. for(QObject* eventAncestor = Object; eventAncestor != NULL; eventAncestor = eventAncestor->parent())
  67. {
  68. if (bottomWidget == eventAncestor)
  69. {
  70. // The event originated from a widget of the bottom widget, let the
  71. // other translators record it.
  72. acceptEvent = false;
  73. break;
  74. }
  75. }
  76. }
  77. return acceptEvent;
  78. }
  79. // ----------------------------------------------------------------------------
  80. void ctkFileDialogEventTranslator::onDestroyed(QObject*)
  81. {
  82. this->CurrentObject = 0;
  83. }
  84. // ----------------------------------------------------------------------------
  85. void ctkFileDialogEventTranslator::onFileSelectionChanged(const QStringList& files)
  86. {
  87. foreach(QString file, files)
  88. {
  89. QFileInfo info(file);
  90. emit recordEvent(this->CurrentObject, "comment", QString("Loading %1 ... ").arg(info.fileName()));
  91. }
  92. QString file = files.join("#");
  93. emit recordEvent(this->CurrentObject, "newFile", file);
  94. }
  95. // ----------------------------------------------------------------------------
  96. void ctkFileDialogEventTranslator::onRejected()
  97. {
  98. emit recordEvent(this->CurrentObject, "rejected", "");
  99. }