ctkFileDialog.cpp 5.8 KB

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