ctkFileDialog.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <QChildEvent>
  16. #include <QDebug>
  17. #include <QDialogButtonBox>
  18. #include <QEvent>
  19. #include <QGridLayout>
  20. #include <QLabel>
  21. #include <QLineEdit>
  22. #include <QListView>
  23. #include <QPushButton>
  24. // CTK includes
  25. #include "ctkFileDialog.h"
  26. //------------------------------------------------------------------------------
  27. class ctkFileDialogPrivate
  28. {
  29. Q_DECLARE_PUBLIC(ctkFileDialog);
  30. protected:
  31. ctkFileDialog* const q_ptr;
  32. public:
  33. ctkFileDialogPrivate(ctkFileDialog& object);
  34. void init();
  35. void observeAcceptButton();
  36. QPushButton* acceptButton()const;
  37. QListView* listView()const;
  38. bool AcceptButtonEnable;
  39. bool AcceptButtonState;
  40. bool IgnoreEvent;
  41. };
  42. //------------------------------------------------------------------------------
  43. ctkFileDialogPrivate::ctkFileDialogPrivate(ctkFileDialog& object)
  44. :q_ptr(&object)
  45. {
  46. this->IgnoreEvent = false;
  47. this->AcceptButtonEnable = true;
  48. this->AcceptButtonState = true;
  49. }
  50. //------------------------------------------------------------------------------
  51. void ctkFileDialogPrivate::init()
  52. {
  53. Q_Q(ctkFileDialog);
  54. this->observeAcceptButton();
  55. QObject::connect(this->listView()->selectionModel(),
  56. SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  57. q, SLOT(onSelectionChanged()));
  58. }
  59. //------------------------------------------------------------------------------
  60. QPushButton* ctkFileDialogPrivate::acceptButton()const
  61. {
  62. Q_Q(const ctkFileDialog);
  63. QDialogButtonBox* buttonBox = q->findChild<QDialogButtonBox*>();
  64. Q_ASSERT(buttonBox);
  65. QDialogButtonBox::StandardButton button =
  66. (q->acceptMode() == QFileDialog::AcceptOpen ? QDialogButtonBox::Open : QDialogButtonBox::Save);
  67. return buttonBox->button(button);
  68. }
  69. //------------------------------------------------------------------------------
  70. QListView* ctkFileDialogPrivate::listView()const
  71. {
  72. Q_Q(const ctkFileDialog);
  73. QListView* listView= q->findChild<QListView*>("listView");
  74. Q_ASSERT(listView);
  75. return listView;
  76. }
  77. //------------------------------------------------------------------------------
  78. void ctkFileDialogPrivate::observeAcceptButton()
  79. {
  80. Q_Q(ctkFileDialog);
  81. QPushButton* button = this->acceptButton();
  82. Q_ASSERT(button);
  83. this->AcceptButtonState =
  84. button->isEnabledTo(qobject_cast<QWidget*>(button->parent()));
  85. // TODO: catching the event of the enable state is not enough, if the user
  86. // double click on the file, the dialog will be accepted, that event should
  87. // be intercepted as well
  88. button->installEventFilter(q);
  89. }
  90. //------------------------------------------------------------------------------
  91. ctkFileDialog::ctkFileDialog(QWidget *parentWidget,
  92. const QString &caption,
  93. const QString &directory,
  94. const QString &filter)
  95. :QFileDialog(parentWidget, caption, directory, filter)
  96. , d_ptr(new ctkFileDialogPrivate(*this))
  97. {
  98. Q_D(ctkFileDialog);
  99. // The findChild<QDialogButtonBox*>() call fails on Mac/Qt5 because native
  100. // dialogs don't publish any internals. No problems on other OS.
  101. // Can be applied to Qt4 as well, if problems arise there.
  102. #if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
  103. this->setOptions(DontUseNativeDialog);
  104. #endif
  105. d->init();
  106. }
  107. //------------------------------------------------------------------------------
  108. ctkFileDialog::~ctkFileDialog()
  109. {
  110. }
  111. //------------------------------------------------------------------------------
  112. void ctkFileDialog::setBottomWidget(QWidget* widget, const QString& label)
  113. {
  114. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  115. QWidget* oldBottomWidget = this->bottomWidget();
  116. // remove the old widget from the layout if any
  117. if (oldBottomWidget)
  118. {
  119. if (oldBottomWidget == widget)
  120. {
  121. return;
  122. }
  123. gridLayout->removeWidget(oldBottomWidget);
  124. delete oldBottomWidget;
  125. }
  126. if (widget == 0)
  127. {
  128. return;
  129. }
  130. if (!label.isEmpty())
  131. {
  132. gridLayout->addWidget(new QLabel(label), 4, 0);
  133. gridLayout->addWidget(widget,4, 1,1, 1);
  134. }
  135. else
  136. {
  137. gridLayout->addWidget(widget,4, 0,1, 2);
  138. }
  139. // The dialog button box is no longer spanned on 2 rows but on 3 rows if
  140. // there is a "bottom widget"
  141. QDialogButtonBox* buttonBox = this->findChild<QDialogButtonBox*>();
  142. Q_ASSERT(buttonBox);
  143. gridLayout->removeWidget(buttonBox);
  144. gridLayout->addWidget(buttonBox, 2, 2, widget ? 3 : 2, 1);
  145. }
  146. //------------------------------------------------------------------------------
  147. QWidget* ctkFileDialog::bottomWidget()const
  148. {
  149. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  150. QLayoutItem* item = gridLayout->itemAtPosition(4,1);
  151. return item ? item->widget() : 0;
  152. }
  153. //------------------------------------------------------------------------------
  154. void ctkFileDialog::setAcceptButtonEnable(bool enable)
  155. {
  156. Q_D(ctkFileDialog);
  157. d->AcceptButtonEnable = enable;
  158. d->IgnoreEvent = true;
  159. d->acceptButton()->setEnabled(d->AcceptButtonEnable && d->AcceptButtonState);
  160. d->IgnoreEvent = false;
  161. }
  162. //------------------------------------------------------------------------------
  163. bool ctkFileDialog::eventFilter(QObject *obj, QEvent *event)
  164. {
  165. Q_D(ctkFileDialog);
  166. QPushButton* button = d->acceptButton();
  167. QDialogButtonBox* dialogButtonBox = qobject_cast<QDialogButtonBox*>(obj);
  168. if (obj == button && event->type() == QEvent::EnabledChange &&
  169. !d->IgnoreEvent)
  170. {
  171. d->IgnoreEvent = true;
  172. d->AcceptButtonState = button->isEnabledTo(qobject_cast<QWidget*>(button->parent()));
  173. button->setEnabled(d->AcceptButtonEnable && d->AcceptButtonState);
  174. d->IgnoreEvent = false;
  175. }
  176. else if (obj == button && event->type() == QEvent::Destroy)
  177. {
  178. // The accept button is deleted probably because setAcceptMode() is being called.
  179. // observe the parent to check when the accept button is added back
  180. obj->parent()->installEventFilter(this);
  181. }
  182. else if (dialogButtonBox && event->type() == QEvent::ChildAdded)
  183. {
  184. dynamic_cast<QChildEvent*>(event)->child()->installEventFilter(this);
  185. }
  186. return QFileDialog::eventFilter(obj, event);
  187. }
  188. //------------------------------------------------------------------------------
  189. void ctkFileDialog::onSelectionChanged()
  190. {
  191. emit this->fileSelectionChanged(this->selectedFiles());
  192. }
  193. //------------------------------------------------------------------------------
  194. void ctkFileDialog::accept()
  195. {
  196. QLineEdit* fileNameEdit = qobject_cast<QLineEdit*>(this->sender());
  197. if (fileNameEdit)
  198. {
  199. QFileInfo info(fileNameEdit->text());
  200. if (info.isDir())
  201. {
  202. setDirectory(info.absoluteFilePath());
  203. return;
  204. }
  205. }
  206. // Don't accept read-only directories if we are in AcceptSave mode.
  207. if ((this->fileMode() == Directory || this->fileMode() == DirectoryOnly) &&
  208. this->acceptMode() == AcceptSave)
  209. {
  210. QStringList files = this->selectedFiles();
  211. QString fn = files.first();
  212. QFileInfo info(fn);
  213. if (info.isDir() && !info.isWritable())
  214. {
  215. this->setDirectory(info.absoluteFilePath());
  216. return;
  217. }
  218. }
  219. this->Superclass::accept();
  220. }