ctkFileDialog.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. button->installEventFilter(p);
  50. }
  51. //------------------------------------------------------------------------------
  52. QPushButton* ctkFileDialogPrivate::acceptButton()const
  53. {
  54. CTK_P(const ctkFileDialog);
  55. QDialogButtonBox* buttonBox = p->findChild<QDialogButtonBox*>();
  56. Q_ASSERT(buttonBox);
  57. QDialogButtonBox::StandardButton button =
  58. (p->acceptMode() == QFileDialog::AcceptOpen ? QDialogButtonBox::Open : QDialogButtonBox::Save);
  59. return buttonBox->button(button);
  60. }
  61. //------------------------------------------------------------------------------
  62. ctkFileDialog::ctkFileDialog(QWidget *parentWidget,
  63. const QString &caption,
  64. const QString &directory,
  65. const QString &filter)
  66. :QFileDialog(parentWidget, caption, directory, filter)
  67. {
  68. CTK_INIT_PRIVATE(ctkFileDialog);
  69. CTK_D(ctkFileDialog);
  70. d->init();
  71. }
  72. //------------------------------------------------------------------------------
  73. ctkFileDialog::~ctkFileDialog()
  74. {
  75. }
  76. //------------------------------------------------------------------------------
  77. void ctkFileDialog::setBottomWidget(QWidget* widget, const QString& label)
  78. {
  79. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  80. QWidget* oldBottomWidget = this->bottomWidget();
  81. if (oldBottomWidget)
  82. {
  83. if (oldBottomWidget == widget)
  84. {
  85. return;
  86. }
  87. gridLayout->removeWidget(oldBottomWidget);
  88. delete oldBottomWidget;
  89. }
  90. if (widget == 0)
  91. {
  92. return;
  93. }
  94. if (!label.isEmpty())
  95. {
  96. gridLayout->addWidget(new QLabel(label), 4, 0);
  97. gridLayout->addWidget(widget,4, 1,1, 1);
  98. }
  99. else
  100. {
  101. gridLayout->addWidget(widget,4, 0,1, 2);
  102. }
  103. QDialogButtonBox* buttonBox = this->findChild<QDialogButtonBox*>();
  104. Q_ASSERT(buttonBox);
  105. gridLayout->removeWidget(buttonBox);
  106. gridLayout->addWidget(buttonBox, 2, 2, 3, 1);
  107. }
  108. //------------------------------------------------------------------------------
  109. QWidget* ctkFileDialog::bottomWidget()const
  110. {
  111. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(this->layout());
  112. QLayoutItem* item = gridLayout->itemAtPosition(4,1);
  113. return item ? item->widget() : 0;
  114. }
  115. //------------------------------------------------------------------------------
  116. void ctkFileDialog::setAcceptButtonEnable(bool enable)
  117. {
  118. CTK_D(ctkFileDialog);
  119. d->AcceptButtonEnable = enable;
  120. d->acceptButton()->setEnabled(d->AcceptButtonEnable && d->AcceptButtonState);
  121. }
  122. //------------------------------------------------------------------------------
  123. bool ctkFileDialog::eventFilter(QObject *obj, QEvent *event)
  124. {
  125. CTK_D(ctkFileDialog);
  126. QPushButton* button = d->acceptButton();
  127. if (obj == button && event->type() == QEvent::EnabledChange &&
  128. !d->IgnoreEvent)
  129. {
  130. d->IgnoreEvent = true;
  131. d->AcceptButtonState = button->isEnabledTo(qobject_cast<QWidget*>(button->parent()));
  132. button->setEnabled(d->AcceptButtonEnable && d->AcceptButtonState);
  133. d->IgnoreEvent = false;
  134. }
  135. return QFileDialog::eventFilter(obj, event);
  136. }