ctkFileDialog.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <QDialogButtonBox>
  17. #include <QEvent>
  18. #include <QGridLayout>
  19. #include <QLabel>
  20. #include <QLineEdit>
  21. #include <QListView>
  22. #include <QPushButton>
  23. // CTK includes
  24. #include "ctkFileDialog.h"
  25. //------------------------------------------------------------------------------
  26. class ctkFileDialogPrivate
  27. {
  28. Q_DECLARE_PUBLIC(ctkFileDialog);
  29. protected:
  30. ctkFileDialog* const q_ptr;
  31. public:
  32. ctkFileDialogPrivate(ctkFileDialog& object);
  33. void init();
  34. QPushButton* acceptButton()const;
  35. QListView* listView()const;
  36. bool AcceptButtonEnable;
  37. bool AcceptButtonState;
  38. bool IgnoreEvent;
  39. };
  40. //------------------------------------------------------------------------------
  41. ctkFileDialogPrivate::ctkFileDialogPrivate(ctkFileDialog& object)
  42. :q_ptr(&object)
  43. {
  44. this->IgnoreEvent = false;
  45. this->AcceptButtonEnable = true;
  46. this->AcceptButtonState = true;
  47. }
  48. //------------------------------------------------------------------------------
  49. void ctkFileDialogPrivate::init()
  50. {
  51. Q_Q(ctkFileDialog);
  52. QPushButton* button = this->acceptButton();
  53. Q_ASSERT(button);
  54. this->AcceptButtonState =
  55. button->isEnabledTo(qobject_cast<QWidget*>(button->parent()));
  56. // TODO: catching the event of the enable state is not enough, if the user
  57. // double click on the file, the dialog will be accepted, that event should
  58. // be intercepted as well
  59. button->installEventFilter(q);
  60. QObject::connect(this->listView()->selectionModel(),
  61. SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
  62. q, SLOT(onSelectionChanged()));
  63. }
  64. //------------------------------------------------------------------------------
  65. QPushButton* ctkFileDialogPrivate::acceptButton()const
  66. {
  67. Q_Q(const ctkFileDialog);
  68. QDialogButtonBox* buttonBox = q->findChild<QDialogButtonBox*>();
  69. Q_ASSERT(buttonBox);
  70. QDialogButtonBox::StandardButton button =
  71. (q->acceptMode() == QFileDialog::AcceptOpen ? QDialogButtonBox::Open : QDialogButtonBox::Save);
  72. return buttonBox->button(button);
  73. }
  74. //------------------------------------------------------------------------------
  75. QListView* ctkFileDialogPrivate::listView()const
  76. {
  77. Q_Q(const ctkFileDialog);
  78. QListView* listView= q->findChild<QListView*>("listView");
  79. Q_ASSERT(listView);
  80. return listView;
  81. }
  82. //------------------------------------------------------------------------------
  83. ctkFileDialog::ctkFileDialog(QWidget *parentWidget,
  84. const QString &caption,
  85. const QString &directory,
  86. const QString &filter)
  87. :QFileDialog(parentWidget, caption, directory, filter)
  88. , d_ptr(new ctkFileDialogPrivate(*this))
  89. {
  90. Q_D(ctkFileDialog);
  91. d->init();
  92. }
  93. //------------------------------------------------------------------------------
  94. ctkFileDialog::~ctkFileDialog()
  95. {
  96. }
  97. //------------------------------------------------------------------------------
  98. void ctkFileDialog::setBottomWidget(QWidget* widget, const QString& label)
  99. {
  100. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  101. QWidget* oldBottomWidget = this->bottomWidget();
  102. // remove the old widget from the layout if any
  103. if (oldBottomWidget)
  104. {
  105. if (oldBottomWidget == widget)
  106. {
  107. return;
  108. }
  109. gridLayout->removeWidget(oldBottomWidget);
  110. delete oldBottomWidget;
  111. }
  112. if (widget == 0)
  113. {
  114. return;
  115. }
  116. if (!label.isEmpty())
  117. {
  118. gridLayout->addWidget(new QLabel(label), 4, 0);
  119. gridLayout->addWidget(widget,4, 1,1, 1);
  120. }
  121. else
  122. {
  123. gridLayout->addWidget(widget,4, 0,1, 2);
  124. }
  125. // The dialog button box is no longer spanned on 2 rows but on 3 rows if
  126. // there is a "bottom widget"
  127. QDialogButtonBox* buttonBox = this->findChild<QDialogButtonBox*>();
  128. Q_ASSERT(buttonBox);
  129. gridLayout->removeWidget(buttonBox);
  130. gridLayout->addWidget(buttonBox, 2, 2, widget ? 3 : 2, 1);
  131. }
  132. //------------------------------------------------------------------------------
  133. QWidget* ctkFileDialog::bottomWidget()const
  134. {
  135. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  136. QLayoutItem* item = gridLayout->itemAtPosition(4,1);
  137. return item ? item->widget() : 0;
  138. }
  139. //------------------------------------------------------------------------------
  140. void ctkFileDialog::setAcceptButtonEnable(bool enable)
  141. {
  142. Q_D(ctkFileDialog);
  143. d->AcceptButtonEnable = enable;
  144. d->IgnoreEvent = true;
  145. d->acceptButton()->setEnabled(d->AcceptButtonEnable && d->AcceptButtonState);
  146. d->IgnoreEvent = false;
  147. }
  148. //------------------------------------------------------------------------------
  149. bool ctkFileDialog::eventFilter(QObject *obj, QEvent *event)
  150. {
  151. Q_D(ctkFileDialog);
  152. QPushButton* button = d->acceptButton();
  153. if (obj == button && event->type() == QEvent::EnabledChange &&
  154. !d->IgnoreEvent)
  155. {
  156. d->IgnoreEvent = true;
  157. d->AcceptButtonState = button->isEnabledTo(qobject_cast<QWidget*>(button->parent()));
  158. button->setEnabled(d->AcceptButtonEnable && d->AcceptButtonState);
  159. d->IgnoreEvent = false;
  160. }
  161. return QFileDialog::eventFilter(obj, event);
  162. }
  163. //------------------------------------------------------------------------------
  164. void ctkFileDialog::onSelectionChanged()
  165. {
  166. emit this->fileSelectionChanged(this->selectedFiles());
  167. }
  168. //------------------------------------------------------------------------------
  169. void ctkFileDialog::accept()
  170. {
  171. QLineEdit* fileNameEdit = qobject_cast<QLineEdit*>(this->sender());
  172. if (fileNameEdit)
  173. {
  174. QFileInfo info(fileNameEdit->text());
  175. if (info.isDir())
  176. {
  177. setDirectory(info.absoluteFilePath());
  178. return;
  179. }
  180. }
  181. this->Superclass::accept();
  182. }