ctkFileDialog.cpp 5.1 KB

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