ctkFileDialog.cpp 5.0 KB

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