ctkMessageBox.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <QGridLayout>
  18. #include <QSettings>
  19. #include <QTimer>
  20. // CTK includes
  21. #include "ctkMessageBox.h"
  22. //-----------------------------------------------------------------------------
  23. // ctkMessageBoxPrivate methods
  24. //-----------------------------------------------------------------------------
  25. class ctkMessageBoxPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkMessageBox);
  28. protected:
  29. ctkMessageBox* const q_ptr;
  30. public:
  31. explicit ctkMessageBoxPrivate(ctkMessageBox& object);
  32. virtual ~ctkMessageBoxPrivate();
  33. void init();
  34. void readSettings();
  35. void writeSettings();
  36. public:
  37. QString DontShowAgainSettingsKey;
  38. QCheckBox* DontShowAgainCheckBox;
  39. };
  40. //-----------------------------------------------------------------------------
  41. ctkMessageBoxPrivate::ctkMessageBoxPrivate(ctkMessageBox& object)
  42. : q_ptr(&object)
  43. {
  44. this->DontShowAgainCheckBox = 0;
  45. }
  46. //-----------------------------------------------------------------------------
  47. ctkMessageBoxPrivate::~ctkMessageBoxPrivate()
  48. {
  49. }
  50. //-----------------------------------------------------------------------------
  51. void ctkMessageBoxPrivate::init()
  52. {
  53. Q_Q(ctkMessageBox);
  54. this->DontShowAgainCheckBox = new QCheckBox(q);
  55. this->DontShowAgainCheckBox->setObjectName(QLatin1String("ctk_msgbox_dontshowcheckbox"));
  56. this->DontShowAgainCheckBox->setText(q->tr("Don't show this message again"));
  57. // The height policy being Fixed by default on a checkbox, if the message box icon
  58. // is bigger than the text+checkbox height, it would be truncated.
  59. this->DontShowAgainCheckBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  60. this->DontShowAgainCheckBox->setChecked(false);
  61. }
  62. //-----------------------------------------------------------------------------
  63. void ctkMessageBoxPrivate::readSettings()
  64. {
  65. if (this->DontShowAgainSettingsKey.isEmpty())
  66. {
  67. return;
  68. }
  69. QSettings settings;
  70. bool dontShow = settings.value(this->DontShowAgainSettingsKey,
  71. this->DontShowAgainCheckBox->isChecked()).toBool();
  72. this->DontShowAgainCheckBox->setChecked(dontShow);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkMessageBoxPrivate::writeSettings()
  76. {
  77. if (this->DontShowAgainSettingsKey.isEmpty())
  78. {
  79. return;
  80. }
  81. QSettings settings;
  82. settings.setValue(this->DontShowAgainSettingsKey, this->DontShowAgainCheckBox->isChecked());
  83. qDebug() << "write...";
  84. }
  85. //-----------------------------------------------------------------------------
  86. // ctkMessageBox methods
  87. //-----------------------------------------------------------------------------
  88. ctkMessageBox::ctkMessageBox(QWidget* newParent)
  89. : Superclass(newParent)
  90. , d_ptr(new ctkMessageBoxPrivate(*this))
  91. {
  92. Q_D(ctkMessageBox);
  93. d->init();
  94. }
  95. //-----------------------------------------------------------------------------
  96. ctkMessageBox::ctkMessageBox(Icon icon, const QString& title,
  97. const QString& text, StandardButtons buttons,
  98. QWidget* parent, Qt::WindowFlags f)
  99. : QMessageBox(icon, title, text, buttons, parent, f)
  100. {
  101. Q_D(ctkMessageBox);
  102. d->init();
  103. }
  104. //-----------------------------------------------------------------------------
  105. ctkMessageBox::~ctkMessageBox()
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. void ctkMessageBox::setDontShowAgainVisible(bool visible)
  110. {
  111. Q_D(ctkMessageBox);
  112. if (!visible)
  113. {
  114. this->layout()->removeWidget(d->DontShowAgainCheckBox);
  115. d->DontShowAgainCheckBox->hide();
  116. return;
  117. }
  118. QGridLayout *grid = static_cast<QGridLayout *>(this->layout());
  119. grid->addWidget(d->DontShowAgainCheckBox, 1, 1, 1, 1);
  120. }
  121. //-----------------------------------------------------------------------------
  122. bool ctkMessageBox::isDontShowAgainVisible()const
  123. {
  124. Q_D(const ctkMessageBox);
  125. return d->DontShowAgainCheckBox->isVisibleTo(const_cast<ctkMessageBox*>(this));
  126. }
  127. //-----------------------------------------------------------------------------
  128. void ctkMessageBox::setDontShowAgainSettingsKey(const QString& key)
  129. {
  130. Q_D(ctkMessageBox);
  131. if (key == d->DontShowAgainSettingsKey)
  132. {
  133. return;
  134. }
  135. d->DontShowAgainSettingsKey = key;
  136. d->readSettings();
  137. }
  138. //-----------------------------------------------------------------------------
  139. QString ctkMessageBox::dontShowAgainSettingsKey()const
  140. {
  141. Q_D(const ctkMessageBox);
  142. return d->DontShowAgainSettingsKey;
  143. }
  144. //-----------------------------------------------------------------------------
  145. bool ctkMessageBox::dontShowAgain()const
  146. {
  147. Q_D(const ctkMessageBox);
  148. return d->DontShowAgainCheckBox->isChecked();
  149. }
  150. //-----------------------------------------------------------------------------
  151. void ctkMessageBox::setDontShowAgain(bool dontShow)
  152. {
  153. Q_D(ctkMessageBox);
  154. d->DontShowAgainCheckBox->setChecked(dontShow);
  155. d->writeSettings();
  156. }
  157. //-----------------------------------------------------------------------------
  158. void ctkMessageBox::done(int resultCode)
  159. {
  160. Q_D(ctkMessageBox);
  161. if (resultCode == QDialog::Accepted)
  162. {
  163. d->writeSettings();
  164. }
  165. this->Superclass::done(resultCode);
  166. }
  167. //-----------------------------------------------------------------------------
  168. void ctkMessageBox::setVisible(bool visible)
  169. {
  170. Q_D(ctkMessageBox);
  171. if (visible)
  172. {
  173. d->readSettings();
  174. if (d->DontShowAgainCheckBox->isChecked())
  175. {
  176. QTimer::singleShot(0, this, SLOT(accept()));
  177. return;
  178. }
  179. }
  180. this->Superclass::setVisible(visible);
  181. }
  182. //-----------------------------------------------------------------------------
  183. bool ctkMessageBox
  184. ::confirmExit(const QString& dontShowAgainKey, QWidget* parentWidget)
  185. {
  186. ctkMessageBox dialog(parentWidget);
  187. dialog.setText(tr("Are you sure you want to exit?"));
  188. dialog.setDontShowAgainVisible(!dontShowAgainKey.isEmpty());
  189. dialog.setDontShowAgainSettingsKey(dontShowAgainKey);
  190. return dialog.exec() == QDialog::Accepted;
  191. }