ctkFileDialog.cpp 8.9 KB

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