ctkConfirmExitDialog.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <QCheckBox>
  16. #include <QDebug>
  17. #include <QDialogButtonBox>
  18. #include <QGridLayout>
  19. #include <QMessageBox>
  20. #include <QLabel>
  21. #include <QSettings>
  22. #include <QTimer>
  23. // CTK includes
  24. #include "ctkConfirmExitDialog.h"
  25. //-----------------------------------------------------------------------------
  26. // ctkConfirmExitDialogPrivate methods
  27. //-----------------------------------------------------------------------------
  28. class ctkConfirmExitDialogPrivate
  29. {
  30. Q_DECLARE_PUBLIC(ctkConfirmExitDialog);
  31. protected:
  32. ctkConfirmExitDialog* const q_ptr;
  33. public:
  34. explicit ctkConfirmExitDialogPrivate(ctkConfirmExitDialog& object);
  35. virtual ~ctkConfirmExitDialogPrivate();
  36. void init();
  37. void readSettings();
  38. void writeSettings();
  39. public:
  40. QString DontShowKey;
  41. QCheckBox* DontShowCheckBox;
  42. QLabel* TextLabel;
  43. QLabel* IconLabel;
  44. };
  45. //-----------------------------------------------------------------------------
  46. ctkConfirmExitDialogPrivate::ctkConfirmExitDialogPrivate(ctkConfirmExitDialog& object)
  47. : q_ptr(&object)
  48. {
  49. this->DontShowCheckBox = 0;
  50. this->TextLabel = 0;
  51. this->IconLabel = 0;
  52. }
  53. //-----------------------------------------------------------------------------
  54. ctkConfirmExitDialogPrivate::~ctkConfirmExitDialogPrivate()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. void ctkConfirmExitDialogPrivate::init()
  59. {
  60. Q_Q(ctkConfirmExitDialog);
  61. QGridLayout* grid = new QGridLayout(q);
  62. this->IconLabel = new QLabel(q);
  63. this->IconLabel->setPixmap(QMessageBox::standardIcon(QMessageBox::Question));
  64. this->IconLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
  65. grid->addWidget(this->IconLabel, 0, 0, Qt::AlignTop);
  66. this->TextLabel = new QLabel(q);
  67. this->TextLabel->setText(q->tr("Are you sure you want to quit?"));
  68. grid->addWidget(this->TextLabel, 0, 1);
  69. this->DontShowCheckBox = new QCheckBox(q);
  70. this->DontShowCheckBox->setText(q->tr("Don't show this message again"));
  71. this->DontShowCheckBox->setChecked(false);
  72. grid->addWidget(this->DontShowCheckBox, 1, 1, Qt::AlignTop);
  73. QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Yes
  74. | QDialogButtonBox::No);
  75. QObject::connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept()));
  76. QObject::connect(buttonBox, SIGNAL(rejected()), q, SLOT(reject()));
  77. grid->addWidget(buttonBox, 2, 0, 1, 2, Qt::AlignCenter);
  78. grid->setSizeConstraint(QLayout::SetFixedSize);
  79. }
  80. //-----------------------------------------------------------------------------
  81. void ctkConfirmExitDialogPrivate::readSettings()
  82. {
  83. if (this->DontShowKey.isEmpty())
  84. {
  85. return;
  86. }
  87. QSettings settings;
  88. bool dontShow = settings.value(this->DontShowKey,
  89. this->DontShowCheckBox->isChecked()).toBool();
  90. this->DontShowCheckBox->setChecked(dontShow);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void ctkConfirmExitDialogPrivate::writeSettings()
  94. {
  95. if (this->DontShowKey.isEmpty())
  96. {
  97. return;
  98. }
  99. QSettings settings;
  100. settings.setValue(this->DontShowKey, this->DontShowCheckBox->isChecked());
  101. }
  102. //-----------------------------------------------------------------------------
  103. // ctkConfirmExitDialog methods
  104. //-----------------------------------------------------------------------------
  105. ctkConfirmExitDialog::ctkConfirmExitDialog(QWidget* newParent)
  106. : Superclass(newParent)
  107. , d_ptr(new ctkConfirmExitDialogPrivate(*this))
  108. {
  109. Q_D(ctkConfirmExitDialog);
  110. d->init();
  111. }
  112. //-----------------------------------------------------------------------------
  113. ctkConfirmExitDialog::~ctkConfirmExitDialog()
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. void ctkConfirmExitDialog::setPixmap(const QPixmap& pixmap)
  118. {
  119. Q_D(ctkConfirmExitDialog);
  120. d->IconLabel->setPixmap(pixmap);
  121. }
  122. //-----------------------------------------------------------------------------
  123. void ctkConfirmExitDialog::setText(const QString& text)
  124. {
  125. Q_D(ctkConfirmExitDialog);
  126. d->TextLabel->setText(text);
  127. }
  128. //-----------------------------------------------------------------------------
  129. void ctkConfirmExitDialog::setDontShowAnymoreSettingsKey(const QString& key)
  130. {
  131. Q_D(ctkConfirmExitDialog);
  132. if (key == d->DontShowKey)
  133. {
  134. return;
  135. }
  136. d->DontShowKey = key;
  137. d->readSettings();
  138. }
  139. //-----------------------------------------------------------------------------
  140. QString ctkConfirmExitDialog::dontShowAnymoreSettingsKey()const
  141. {
  142. Q_D(const ctkConfirmExitDialog);
  143. return d->DontShowKey;
  144. }
  145. //-----------------------------------------------------------------------------
  146. bool ctkConfirmExitDialog::dontShowAnymore()const
  147. {
  148. Q_D(const ctkConfirmExitDialog);
  149. return d->DontShowCheckBox->isChecked();
  150. }
  151. //-----------------------------------------------------------------------------
  152. void ctkConfirmExitDialog::setDontShowAnymore(bool dontShow)
  153. {
  154. Q_D(ctkConfirmExitDialog);
  155. d->DontShowCheckBox->setChecked(dontShow);
  156. d->writeSettings();
  157. }
  158. //-----------------------------------------------------------------------------
  159. void ctkConfirmExitDialog::accept()
  160. {
  161. Q_D(ctkConfirmExitDialog);
  162. d->writeSettings();
  163. this->Superclass::accept();
  164. }
  165. //-----------------------------------------------------------------------------
  166. void ctkConfirmExitDialog::setVisible(bool visible)
  167. {
  168. Q_D(ctkConfirmExitDialog);
  169. if (visible)
  170. {
  171. d->readSettings();
  172. if (d->DontShowCheckBox->isChecked())
  173. {
  174. QTimer::singleShot(0, this, SLOT(accept()));
  175. return;
  176. }
  177. }
  178. this->Superclass::setVisible(visible);
  179. }
  180. //-----------------------------------------------------------------------------
  181. bool ctkConfirmExitDialog
  182. ::confirmExit(const QString& dontShowAgainKey, QWidget* parentWidget)
  183. {
  184. ctkConfirmExitDialog dialog(parentWidget);
  185. dialog.setDontShowAnymoreSettingsKey(dontShowAgainKey);
  186. return dialog.exec() == QDialog::Accepted;
  187. }