浏览代码

Add ctkMessageBox, a more generic version of ctkConfirmExitDialog

Julien Finet 13 年之前
父节点
当前提交
9e2471eabd

+ 3 - 3
Libs/Widgets/CMakeLists.txt

@@ -45,8 +45,6 @@ set(KIT_SRCS
   ctkConsole.cpp
   ctkConsole.h
   ctkConsole_p.h
-  ctkConfirmExitDialog.cpp
-  ctkConfirmExitDialog.h
   ctkCoordinatesWidget.cpp
   ctkCoordinatesWidget.h
   ctkCrosshairLabel.cpp
@@ -92,6 +90,8 @@ set(KIT_SRCS
   ctkMenuComboBox.cpp
   ctkMenuComboBox.h
   ctkMenuComboBox_p.h
+  ctkMessageBox.cpp
+  ctkMessageBox.h
   ctkModalityWidget.cpp
   ctkModalityWidget.h
   ctkPathLineEdit.cpp
@@ -185,7 +185,6 @@ set(KIT_MOC_SRCS
   ctkCollapsibleGroupBox.h
   ctkColorDialog.h
   ctkColorPickerButton.h
-  ctkConfirmExitDialog.h
   ctkConsole.h
   ctkConsole_p.h
   ctkCoordinatesWidget.h
@@ -211,6 +210,7 @@ set(KIT_MOC_SRCS
   ctkMenuButton.h
   ctkMenuComboBox.h
   ctkMenuComboBox_p.h
+  ctkMessageBox.h
   ctkModalityWidget.h
   ctkPathLineEdit.h
   ctkPopupWidget.h

+ 2 - 2
Libs/Widgets/Testing/Cpp/CMakeLists.txt

@@ -20,7 +20,6 @@ set(TEST_SOURCES
   ctkColorPickerButtonTest1.cpp
   ctkComboBoxTest1.cpp
   ctkCompleterTest1.cpp
-  ctkConfirmExitDialogTest1.cpp
   ctkConsoleTest1.cpp
   ctkCoordinatesWidgetTest1.cpp
   ctkCrosshairLabelTest1.cpp
@@ -48,6 +47,7 @@ set(TEST_SOURCES
   ctkMenuComboBoxTest1.cpp
   ctkMenuComboBoxTest2.cpp
   ctkMenuComboBoxTest3.cpp
+  ctkMessageBoxDontShowAgainTest.cpp
   ctkModalityWidgetTest1.cpp
   ctkPathLineEditTest1.cpp
   ctkPopupWidgetTest1.cpp
@@ -148,7 +148,6 @@ SIMPLE_TEST( ctkColorDialogTest2 )
 SIMPLE_TEST( ctkColorPickerButtonTest1 )
 SIMPLE_TEST( ctkComboBoxTest1 )
 SIMPLE_TEST( ctkCompleterTest1 )
-SIMPLE_TEST( ctkConfirmExitDialogTest1 )
 SIMPLE_TEST( ctkConsoleTest1 )
 SIMPLE_TEST( ctkCoordinatesWidgetTest1 )
 SIMPLE_TEST( ctkCrosshairLabelTest1 )
@@ -179,6 +178,7 @@ SIMPLE_TEST( ctkMenuButtonTest1 )
 SIMPLE_TEST( ctkMenuComboBoxTest1 )
 SIMPLE_TEST( ctkMenuComboBoxTest2 )
 SIMPLE_TEST( ctkMenuComboBoxTest3 )
+SIMPLE_TEST( ctkMessageBoxDontShowAgainTest )
 SIMPLE_TEST( ctkModalityWidgetTest1 )
 SIMPLE_TEST( ctkPathLineEditTest1 )
 SIMPLE_TEST( ctkPopupWidgetTest1 )

+ 28 - 22
Libs/Widgets/Testing/Cpp/ctkConfirmExitDialogTest1.cpp

@@ -26,14 +26,14 @@
 #include <QTimer>
 
 // CTK includes
-#include "ctkConfirmExitDialog.h"
+#include "ctkMessageBox.h"
 
 // STD includes
 #include <cstdlib>
 #include <iostream>
 
 //-----------------------------------------------------------------------------
-int ctkConfirmExitDialogTest1(int argc, char * argv [] )
+int ctkMessageBoxDontShowAgainTest(int argc, char * argv [] )
 {
   QApplication app(argc, argv);
   /// set the names for QSettings to work
@@ -41,47 +41,53 @@ int ctkConfirmExitDialogTest1(int argc, char * argv [] )
   app.setOrganizationDomain("www.commontk.org");
   app.setApplicationName("CTK");
 
-  ctkConfirmExitDialog confirmDialog;
-  if (confirmDialog.dontShowAnymore() != false)
+  ctkMessageBox confirmDialog;
+
+  // Test default values.
+  if (confirmDialog.dontShowAgainVisible() != false ||
+      confirmDialog.dontShowAgain() != false ||
+      confirmDialog.dontShowAgainSettingsKey().isEmpty() != true)
     {
-    std::cerr << "ctkConfirmExitDialog::dontShowAnymore failed" << std::endl;
+    std::cerr << "ctkMessageBox default values failed" << std::endl;
     return EXIT_FAILURE;
     }
   confirmDialog.setText("Are you sure you want to exit?");
-  confirmDialog.setPixmap(confirmDialog.style()->standardPixmap(QStyle::SP_MessageBoxQuestion));
-  
+  confirmDialog.setIcon(QMessageBox::Question);
+  confirmDialog.setDontShowAgainVisible(true);
+
   QSettings settings;
   settings.setValue("DontShow", true);
-  
-  confirmDialog.setDontShowAnymoreSettingsKey("DontShow");
-  if (confirmDialog.dontShowAnymoreSettingsKey() != "DontShow")
+
+  confirmDialog.setDontShowAgainSettingsKey("DontShow");
+  if (confirmDialog.dontShowAgainSettingsKey() != "DontShow")
     {
-    std::cerr << "ctkConfirmExitDialog::setDontShowAnymoreSettingsKey failed:"
-              << confirmDialog.dontShowAnymoreSettingsKey().toStdString() << std::endl;
+    std::cerr << "ctkMessageBox::setDontShowAgainSettingsKey failed:"
+              << confirmDialog.dontShowAgainSettingsKey().toStdString() << std::endl;
     return EXIT_FAILURE;
     }
-  if (confirmDialog.dontShowAnymore() != true)
+  if (confirmDialog.dontShowAgain() != true)
     {
-    std::cerr << "ctkConfirmExitDialog::setDontShowAnymoreSettingsKey failed:"
-              << confirmDialog.dontShowAnymore() << std::endl;
+    std::cerr << "ctkMessageBox::setDontShowAgainSettingsKey failed:"
+              << confirmDialog.dontShowAgain() << std::endl;
     return EXIT_FAILURE;
     }
-  
-  // exec() should return automatically because DontShowAnymore is true
+
+  // exec() should return automatically because DontShowAgain is true
   if (confirmDialog.exec() != QDialog::Accepted)
     {
-    std::cerr << "ctkConfirmExitDialog::exec failed:" << std::endl;
+    std::cerr << "ctkMessageBox::exec failed:" << std::endl;
     return EXIT_FAILURE;
     }
-  
+
   // test the static version
-  if (ctkConfirmExitDialog::confirmExit("DontShow") != true)
+  if (ctkMessageBox::confirmExit("DontShow") != true)
     {
-    std::cerr << "ctkConfirmExitDialog::confirmExit failed:" << std::endl;
+    std::cerr << "ctkMessageBox::confirmExit failed:" << std::endl;
     return EXIT_FAILURE;
     }
 
-  confirmDialog.setDontShowAnymore(false);
+  confirmDialog.setDontShowAgain(false);
+
   // modal dialog
   confirmDialog.open();
 

+ 0 - 215
Libs/Widgets/ctkConfirmExitDialog.cpp

@@ -1,215 +0,0 @@
-/*=========================================================================
-
-  Library:   CTK
-
-  Copyright (c) Kitware Inc.
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-      http://www.apache.org/licenses/LICENSE-2.0.txt
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-
-=========================================================================*/
-
-// Qt includes
-#include <QCheckBox>
-#include <QDebug>
-#include <QDialogButtonBox>
-#include <QGridLayout>
-#include <QMessageBox>
-#include <QLabel>
-#include <QSettings>
-#include <QTimer>
-
-// CTK includes
-#include "ctkConfirmExitDialog.h"
-
-//-----------------------------------------------------------------------------
-// ctkConfirmExitDialogPrivate methods
-
-//-----------------------------------------------------------------------------
-class ctkConfirmExitDialogPrivate
-{
-  Q_DECLARE_PUBLIC(ctkConfirmExitDialog);
-protected:
-  ctkConfirmExitDialog* const q_ptr;
-public:
-  explicit ctkConfirmExitDialogPrivate(ctkConfirmExitDialog& object);
-  virtual ~ctkConfirmExitDialogPrivate();
-
-  void init();
-  void readSettings();
-  void writeSettings();
-public:
-  QString        DontShowKey;
-  QCheckBox*     DontShowCheckBox;
-  QLabel*        TextLabel;
-  QLabel*        IconLabel;
-};
-
-//-----------------------------------------------------------------------------
-ctkConfirmExitDialogPrivate::ctkConfirmExitDialogPrivate(ctkConfirmExitDialog& object)
-  : q_ptr(&object)
-{
-  this->DontShowCheckBox = 0;
-  this->TextLabel = 0;
-  this->IconLabel = 0;
-}
-
-//-----------------------------------------------------------------------------
-ctkConfirmExitDialogPrivate::~ctkConfirmExitDialogPrivate()
-{
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialogPrivate::init()
-{
-  Q_Q(ctkConfirmExitDialog);
-  QGridLayout* grid = new QGridLayout(q);
-  this->IconLabel = new QLabel(q);
-  this->IconLabel->setPixmap(QMessageBox::standardIcon(QMessageBox::Question));
-  this->IconLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
-  grid->addWidget(this->IconLabel, 0, 0, Qt::AlignTop);
-  this->TextLabel = new QLabel(q);
-  this->TextLabel->setText(q->tr("Are you sure you want to quit?"));
-  grid->addWidget(this->TextLabel, 0, 1);
-  this->DontShowCheckBox = new QCheckBox(q);
-  this->DontShowCheckBox->setText(q->tr("Don't show this message again"));
-  this->DontShowCheckBox->setChecked(false);
-  grid->addWidget(this->DontShowCheckBox, 1, 1, Qt::AlignTop);
-  QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Yes
-                                                     | QDialogButtonBox::No);
-  QObject::connect(buttonBox, SIGNAL(accepted()), q, SLOT(accept()));
-  QObject::connect(buttonBox, SIGNAL(rejected()), q, SLOT(reject()));
-  grid->addWidget(buttonBox, 2, 0, 1, 2, Qt::AlignCenter);
-  grid->setSizeConstraint(QLayout::SetFixedSize);
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialogPrivate::readSettings()
-{
-  if (this->DontShowKey.isEmpty())
-    {
-    return;
-    }
-  QSettings settings;
-  bool dontShow = settings.value(this->DontShowKey,
-    this->DontShowCheckBox->isChecked()).toBool();
-  this->DontShowCheckBox->setChecked(dontShow);
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialogPrivate::writeSettings()
-{
-  if (this->DontShowKey.isEmpty())
-    {
-    return;
-    }
-  QSettings settings;
-  settings.setValue(this->DontShowKey, this->DontShowCheckBox->isChecked());
-}
-
-//-----------------------------------------------------------------------------
-// ctkConfirmExitDialog methods
-
-//-----------------------------------------------------------------------------
-ctkConfirmExitDialog::ctkConfirmExitDialog(QWidget* newParent)
-  : Superclass(newParent)
-  , d_ptr(new ctkConfirmExitDialogPrivate(*this))
-{
-  Q_D(ctkConfirmExitDialog);
-  d->init();
-}
-
-//-----------------------------------------------------------------------------
-ctkConfirmExitDialog::~ctkConfirmExitDialog()
-{
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialog::setPixmap(const QPixmap& pixmap)
-{
-  Q_D(ctkConfirmExitDialog);
-  d->IconLabel->setPixmap(pixmap);
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialog::setText(const QString& text)
-{
-  Q_D(ctkConfirmExitDialog);
-  d->TextLabel->setText(text);
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialog::setDontShowAnymoreSettingsKey(const QString& key)
-{
-  Q_D(ctkConfirmExitDialog);
-  if (key == d->DontShowKey)
-    {
-    return;
-    }
-  d->DontShowKey = key;
-  d->readSettings();
-}
-
-//-----------------------------------------------------------------------------
-QString ctkConfirmExitDialog::dontShowAnymoreSettingsKey()const
-{
-  Q_D(const ctkConfirmExitDialog);
-  return d->DontShowKey;
-}
-
-//-----------------------------------------------------------------------------
-bool ctkConfirmExitDialog::dontShowAnymore()const
-{
-  Q_D(const ctkConfirmExitDialog);
-  return d->DontShowCheckBox->isChecked();
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialog::setDontShowAnymore(bool dontShow)
-{
-  Q_D(ctkConfirmExitDialog);
-  d->DontShowCheckBox->setChecked(dontShow);
-  d->writeSettings();
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialog::accept()
-{
-  Q_D(ctkConfirmExitDialog);
-  d->writeSettings();
-  this->Superclass::accept();
-}
-
-//-----------------------------------------------------------------------------
-void ctkConfirmExitDialog::setVisible(bool visible)
-{
-  Q_D(ctkConfirmExitDialog);
-  if (visible)
-    {
-    d->readSettings();
-    if (d->DontShowCheckBox->isChecked())
-      {
-      QTimer::singleShot(0, this, SLOT(accept()));
-      return;
-      }
-    }
-  this->Superclass::setVisible(visible);
-}
-
-//-----------------------------------------------------------------------------
-bool ctkConfirmExitDialog
-::confirmExit(const QString& dontShowAgainKey, QWidget* parentWidget)
-{
-  ctkConfirmExitDialog dialog(parentWidget);
-  dialog.setDontShowAnymoreSettingsKey(dontShowAgainKey);
-  return dialog.exec() == QDialog::Accepted;
-}

+ 0 - 77
Libs/Widgets/ctkConfirmExitDialog.h

@@ -1,77 +0,0 @@
-/*=========================================================================
-
-  Library:   CTK
-
-  Copyright (c) Kitware Inc.
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-      http://www.apache.org/licenses/LICENSE-2.0.txt
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-
-=========================================================================*/
-
-#ifndef __ctkConfirmExitDialog_h
-#define __ctkConfirmExitDialog_h
-
-// Qt includes
-#include <QDialog>
-
-// CTK includes
-#include "ctkWidgetsExport.h"
-
-class ctkConfirmExitDialogPrivate;
-
-/// \ingroup Widgets
-class CTK_WIDGETS_EXPORT ctkConfirmExitDialog : public QDialog
-{
-  Q_OBJECT
-public:
-  typedef QDialog Superclass;
-  ctkConfirmExitDialog(QWidget* newParent = 0);
-  virtual ~ctkConfirmExitDialog();
-
-  /// Customize the pixmap
-  void setPixmap(const QPixmap& pixmap);
-  
-  /// Customize the text
-  void setText(const QString& text);
-  
-  /// Synchronize the state of the checkbox "Don't show this message again"
-  /// with the given settings key.
-  void setDontShowAnymoreSettingsKey(const QString& key);
-  QString dontShowAnymoreSettingsKey()const;
-  
-  /// Is the checkbox "Don't show this message again" checked ?
-  bool dontShowAnymore()const;
-
-  /// Utility function that opens a dialog to confirm exit.
-  static bool confirmExit(const QString& dontShowAgainKey = QString(),
-                          QWidget* parentWidget = 0);
-
-  /// Reimplemented for internal reasons
-  virtual void setVisible(bool visible);
-
-public Q_SLOTS:
-  /// reimplemented for internal reasons
-  virtual void accept();
-
-  /// Change the checkbox and the settings if any
-  void setDontShowAnymore(bool dontShow);
-
-protected:
-  QScopedPointer<ctkConfirmExitDialogPrivate> d_ptr;
-
-private:
-  Q_DECLARE_PRIVATE(ctkConfirmExitDialog);
-  Q_DISABLE_COPY(ctkConfirmExitDialog);
-};
-
-#endif

+ 220 - 0
Libs/Widgets/ctkMessageBox.cpp

@@ -0,0 +1,220 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QCheckBox>
+#include <QDebug>
+#include <QGridLayout>
+#include <QSettings>
+#include <QTimer>
+
+// CTK includes
+#include "ctkMessageBox.h"
+
+//-----------------------------------------------------------------------------
+// ctkMessageBoxPrivate methods
+
+//-----------------------------------------------------------------------------
+class ctkMessageBoxPrivate
+{
+  Q_DECLARE_PUBLIC(ctkMessageBox);
+protected:
+  ctkMessageBox* const q_ptr;
+public:
+  explicit ctkMessageBoxPrivate(ctkMessageBox& object);
+  virtual ~ctkMessageBoxPrivate();
+
+  void init();
+  void readSettings();
+  void writeSettings();
+public:
+  QString        DontShowAgainSettingsKey;
+  QCheckBox*     DontShowAgainCheckBox;
+};
+
+//-----------------------------------------------------------------------------
+ctkMessageBoxPrivate::ctkMessageBoxPrivate(ctkMessageBox& object)
+  : q_ptr(&object)
+{
+  this->DontShowAgainCheckBox = 0;
+}
+
+//-----------------------------------------------------------------------------
+ctkMessageBoxPrivate::~ctkMessageBoxPrivate()
+{
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBoxPrivate::init()
+{
+  Q_Q(ctkMessageBox);
+  this->DontShowAgainCheckBox = new QCheckBox(q);
+  this->DontShowAgainCheckBox->setObjectName(QLatin1String("ctk_msgbox_dontshowcheckbox"));
+  this->DontShowAgainCheckBox->setText(q->tr("Don't show this message again"));
+  // The height policy being Fixed by default on a checkbox, if the message box icon
+  // is bigger than the text+checkbox height, it would be truncated.
+  this->DontShowAgainCheckBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+  this->DontShowAgainCheckBox->setChecked(false);
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBoxPrivate::readSettings()
+{
+  if (this->DontShowAgainSettingsKey.isEmpty())
+    {
+    return;
+    }
+  QSettings settings;
+  bool dontShow = settings.value(this->DontShowAgainSettingsKey,
+    this->DontShowAgainCheckBox->isChecked()).toBool();
+  this->DontShowAgainCheckBox->setChecked(dontShow);
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBoxPrivate::writeSettings()
+{
+  if (this->DontShowAgainSettingsKey.isEmpty())
+    {
+    return;
+    }
+  QSettings settings;
+  settings.setValue(this->DontShowAgainSettingsKey, this->DontShowAgainCheckBox->isChecked());
+  qDebug() << "write...";
+}
+
+//-----------------------------------------------------------------------------
+// ctkMessageBox methods
+
+//-----------------------------------------------------------------------------
+ctkMessageBox::ctkMessageBox(QWidget* newParent)
+  : Superclass(newParent)
+  , d_ptr(new ctkMessageBoxPrivate(*this))
+{
+  Q_D(ctkMessageBox);
+  d->init();
+}
+
+//-----------------------------------------------------------------------------
+ctkMessageBox::ctkMessageBox(Icon icon, const QString& title,
+                             const QString& text, StandardButtons buttons,
+                             QWidget* parent, Qt::WindowFlags f)
+  : QMessageBox(icon, title, text, buttons, parent, f)
+{
+  Q_D(ctkMessageBox);
+  d->init();
+}
+
+//-----------------------------------------------------------------------------
+ctkMessageBox::~ctkMessageBox()
+{
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBox::setDontShowAgainVisible(bool visible)
+{
+  Q_D(ctkMessageBox);
+  if (!visible)
+    {
+    this->layout()->removeWidget(d->DontShowAgainCheckBox);
+    d->DontShowAgainCheckBox->hide();
+    return;
+    }
+  QGridLayout *grid = static_cast<QGridLayout *>(this->layout());
+  grid->addWidget(d->DontShowAgainCheckBox, 1, 1, 1, 1);
+}
+
+//-----------------------------------------------------------------------------
+bool ctkMessageBox::isDontShowAgainVisible()const
+{
+  Q_D(const ctkMessageBox);
+  return d->DontShowAgainCheckBox->isVisibleTo(const_cast<ctkMessageBox*>(this));
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBox::setDontShowAgainSettingsKey(const QString& key)
+{
+  Q_D(ctkMessageBox);
+  if (key == d->DontShowAgainSettingsKey)
+    {
+    return;
+    }
+  d->DontShowAgainSettingsKey = key;
+  d->readSettings();
+}
+
+//-----------------------------------------------------------------------------
+QString ctkMessageBox::dontShowAgainSettingsKey()const
+{
+  Q_D(const ctkMessageBox);
+  return d->DontShowAgainSettingsKey;
+}
+
+//-----------------------------------------------------------------------------
+bool ctkMessageBox::dontShowAgain()const
+{
+  Q_D(const ctkMessageBox);
+  return d->DontShowAgainCheckBox->isChecked();
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBox::setDontShowAgain(bool dontShow)
+{
+  Q_D(ctkMessageBox);
+  d->DontShowAgainCheckBox->setChecked(dontShow);
+  d->writeSettings();
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBox::done(int resultCode)
+{
+  Q_D(ctkMessageBox);
+  if (resultCode == QDialog::Accepted)
+    {
+    d->writeSettings();
+    }
+  this->Superclass::done(resultCode);
+}
+
+//-----------------------------------------------------------------------------
+void ctkMessageBox::setVisible(bool visible)
+{
+  Q_D(ctkMessageBox);
+  if (visible)
+    {
+    d->readSettings();
+    if (d->DontShowAgainCheckBox->isChecked())
+      {
+      QTimer::singleShot(0, this, SLOT(accept()));
+      return;
+      }
+    }
+  this->Superclass::setVisible(visible);
+}
+
+//-----------------------------------------------------------------------------
+bool ctkMessageBox
+::confirmExit(const QString& dontShowAgainKey, QWidget* parentWidget)
+{
+  ctkMessageBox dialog(parentWidget);
+  dialog.setText(tr("Are you sure you want to exit?"));
+  dialog.setDontShowAgainVisible(!dontShowAgainKey.isEmpty());
+  dialog.setDontShowAgainSettingsKey(dontShowAgainKey);
+  return dialog.exec() == QDialog::Accepted;
+}

+ 98 - 0
Libs/Widgets/ctkMessageBox.h

@@ -0,0 +1,98 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkMessageBox_h
+#define __ctkMessageBox_h
+
+// Qt includes
+#include <QMessageBox>
+
+// CTK includes
+#include "ctkWidgetsExport.h"
+
+class ctkMessageBoxPrivate;
+
+/// \ingroup Widgets
+/// ctkMessageBox is an advanced QMessageBox with extra features such as a
+/// checkbox to automatically accept the messagebox next time exec() is called.
+class CTK_WIDGETS_EXPORT ctkMessageBox : public QMessageBox
+{
+  Q_OBJECT
+  /// This property controls the visibility of the "Dont show again" checkbox.
+  Q_PROPERTY(bool dontShowAgainVisible READ isDontShowAgainVisible WRITE setDontShowAgainVisible)
+
+  /// This property holds wether the dialog should be shown next time exec()
+  /// is called.
+  /// \a dontShowAgain can be changed programatically by calling
+  ///  setDontShowAgain or when the user checks the "Dont show again"
+  /// checkbox and the dialog is accepted.
+  /// If the checkbox is checked by the user but the dialog is rejected (click
+  /// on Cancel), \a dontShowAgain is not changed.
+  /// If \a dontShowAgainSettingsKey is set, the \a dontShowAgain is set as
+  /// the value for the key.
+  /// By default, dontShowAgain is false.
+  Q_PROPERTY(bool dontShowAgain READ dontShowAgain WRITE setDontShowAgain)
+
+  /// This property holds the settings key that is used to synchronize the state
+  /// of the checkbox "Don't show this message again"
+  /// with the given key value within QSettings.
+  /// By default, no key is set
+  Q_PROPERTY(QString dontShowAgainSettingsKey READ dontShowAgainSettingsKey WRITE setDontShowAgainSettingsKey)
+
+public:
+  typedef QMessageBox Superclass;
+  ctkMessageBox(QWidget* newParent = 0);
+  ctkMessageBox(Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton,
+                QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint );
+  virtual ~ctkMessageBox();
+
+  void setDontShowAgainVisible(bool visible);
+  bool isDontShowAgainVisible()const;
+
+  bool dontShowAgain()const;
+
+  void setDontShowAgainSettingsKey(const QString& key);
+  QString dontShowAgainSettingsKey()const;
+
+  /// Utility function that opens a dialog to confirm exit.
+  /// If \a dontShowAgainKey is empty, the dontShowAgain checkbox is hidden
+  /// and the message box is always open for the user to confirm exit.
+  static bool confirmExit(const QString& dontShowAgainKey = QString(),
+                          QWidget* parentWidget = 0);
+
+  /// Reimplemented for internal reasons
+  virtual void setVisible(bool visible);
+
+  /// Reimplemented for internal reasons
+  virtual void done(int resultCode);
+
+public Q_SLOTS:
+  /// Change the checkbox and the settings if any
+  void setDontShowAgain(bool dontShow);
+
+protected:
+  QScopedPointer<ctkMessageBoxPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkMessageBox);
+  Q_DISABLE_COPY(ctkMessageBox);
+};
+
+#endif