ctkFileDialogEventPlayer.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <QLineEdit>
  17. #include <QListView>
  18. #include <QPushButton>
  19. // CTK includes
  20. #include "ctkFileDialog.h"
  21. #include "ctkFileDialogEventPlayer.h"
  22. // QtTesting includes
  23. #include <pqTestUtility.h>
  24. // ----------------------------------------------------------------------------
  25. ctkFileDialogEventPlayer::ctkFileDialogEventPlayer(pqTestUtility* util, QObject *parent)
  26. : pqNativeFileDialogEventPlayer(util, parent)
  27. {
  28. }
  29. // ----------------------------------------------------------------------------
  30. bool ctkFileDialogEventPlayer::playEvent(QObject *object,
  31. const QString &command,
  32. const QString &arguments,
  33. bool &error)
  34. {
  35. if (command == "FileOpen" || command == "DirOpen" ||
  36. command == "FileSave" || command == "FilesOpen")
  37. {
  38. return this->Superclass::playEvent(object, command, arguments, error);
  39. }
  40. if (command != "newFile" &&
  41. command != "rejected" &&
  42. command != "fileSave")
  43. {
  44. return false;
  45. }
  46. if(ctkFileDialog* const fileDialog = qobject_cast<ctkFileDialog*>(object))
  47. {
  48. if (command == "newFile")
  49. {
  50. // set the directory
  51. QStringList files;
  52. foreach(const QString& file, arguments.split("#"))
  53. {
  54. files.append(mUtil->convertFromDataDirectory(file));
  55. }
  56. QFileInfo infoFile(files.at(0));
  57. if(!infoFile.exists())
  58. {
  59. qWarning() << "File does not exist or can't be found.";
  60. return false;
  61. }
  62. fileDialog->setDirectory(infoFile.absoluteDir().absolutePath());
  63. if (fileDialog->directory() != infoFile.absoluteDir())
  64. {
  65. qWarning() << "The Directory wasn't selected.";
  66. }
  67. // select the file
  68. QList<QLineEdit*> line = object->findChildren<QLineEdit*>();
  69. if (line.count() > 0)
  70. {
  71. QStringList text;
  72. foreach (QString file, files)
  73. {
  74. QFileInfo info(file);
  75. text << info.fileName();
  76. }
  77. QString lineText = "\"" + text.join("\" \"") + "\"";
  78. line[0]->setText(lineText);
  79. // qDebug() << lineText;
  80. }
  81. else
  82. {
  83. qWarning() << "Files wasn't set in the line edit";
  84. }
  85. QList<QPushButton*> buttons = object->findChildren<QPushButton*>();
  86. if(buttons.count() > 0)
  87. {
  88. foreach(QPushButton* button, buttons)
  89. {
  90. if(button->text().contains("Open") ||
  91. button->text().contains("Ok") ||
  92. button->text().contains("Choose"))
  93. {
  94. button->setEnabled(true);
  95. button->setChecked(true);
  96. button->click();
  97. }
  98. }
  99. }
  100. return true;
  101. }
  102. if (command == "rejected")
  103. {
  104. fileDialog->close();
  105. return true;
  106. }
  107. }
  108. qCritical() << "calling newFile/rejected on unhandled type " << object;
  109. error = true;
  110. return true;
  111. }