Преглед на файлове

Add ctkConfirmExitDialog a QDialog to confirm exit

Julien Finet преди 14 години
родител
ревизия
aef3114846

+ 3 - 0
Libs/Widgets/CMakeLists.txt

@@ -38,6 +38,8 @@ SET(KIT_SRCS
   ctkColorPickerButton.h
   ctkConsoleWidget.cpp
   ctkConsoleWidget.h
+  ctkConfirmExitDialog.cpp
+  ctkConfirmExitDialog.h
   ctkCoordinatesWidget.cpp
   ctkCoordinatesWidget.h
   ctkDirectoryButton.cpp
@@ -132,6 +134,7 @@ SET(KIT_MOC_SRCS
   ctkCollapsibleGroupBox.h
   ctkColorDialog.h
   ctkColorPickerButton.h
+  ctkConfirmExitDialog.h
   ctkConsoleWidget.h
   ctkCoordinatesWidget.h
   ctkDirectoryButton.h

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

@@ -13,6 +13,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cxx
   ctkColorDialogTest1.cpp
   ctkColorPickerButtonTest1.cpp
   ctkComboBoxTest1.cpp
+  ctkConfirmExitDialogTest1.cpp
   ctkConsoleWidgetTest1.cpp
   ctkCoordinatesWidgetTest1.cpp
   ctkDirectoryButtonTest1.cpp
@@ -88,6 +89,7 @@ SIMPLE_TEST( ctkCollapsibleGroupBoxTest1 )
 SIMPLE_TEST( ctkColorDialogTest1 )
 SIMPLE_TEST( ctkColorPickerButtonTest1 )
 SIMPLE_TEST( ctkComboBoxTest1 )
+SIMPLE_TEST( ctkConfirmExitDialogTest1 )
 SIMPLE_TEST( ctkConsoleWidgetTest1 )
 SIMPLE_TEST( ctkCoordinatesWidgetTest1 )
 SIMPLE_TEST( ctkDirectoryButtonTest1 )

+ 85 - 0
Libs/Widgets/Testing/Cpp/ctkConfirmExitDialogTest1.cpp

@@ -0,0 +1,85 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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 <QApplication>
+#include <QCheckBox>
+#include <QSettings>
+#include <QStyle>
+#include <QTimer>
+
+// CTK includes
+#include "ctkConfirmExitDialog.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkConfirmExitDialogTest1(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  /// set the names for QSettings to work
+  app.setOrganizationName("CommonToolKit");
+  app.setOrganizationDomain("www.commontk.org");
+  app.setApplicationName("CTK");
+
+  ctkConfirmExitDialog confirmDialog;
+  if (confirmDialog.dontShowAnymore() != false)
+    {
+    std::cerr << "ctkConfirmExitDialog::dontShowAnymore failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  confirmDialog.setText("Are you sure you want to exit?");
+  confirmDialog.setPixmap(confirmDialog.style()->standardPixmap(QStyle::SP_MessageBoxQuestion));
+  
+  QSettings settings;
+  settings.setValue("DontShow", true);
+  
+  confirmDialog.setDontShowAnymoreSettingsKey("DontShow");
+  if (confirmDialog.dontShowAnymoreSettingsKey() != "DontShow")
+    {
+    std::cerr << "ctkConfirmExitDialog::setDontShowAnymoreSettingsKey failed:"
+              << confirmDialog.dontShowAnymoreSettingsKey().toStdString() << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (confirmDialog.dontShowAnymore() != true)
+    {
+    std::cerr << "ctkConfirmExitDialog::setDontShowAnymoreSettingsKey failed:"
+              << confirmDialog.dontShowAnymore() << std::endl;
+    return EXIT_FAILURE;
+    }
+  
+  // exec() should return automatically because DontShowAnymore is true
+  if (confirmDialog.exec() != QDialog::Accepted)
+    {
+    std::cerr << "ctkConfirmExitDialog::exec failed:" << std::endl;
+    return EXIT_FAILURE;
+    }
+  confirmDialog.setDontShowAnymore(false);
+  // modal dialog
+  confirmDialog.open();
+  // the following is only in interactive mode
+  if (argc < 2 || QString(argv[1]) != "-I" )
+    {
+    QTimer::singleShot(200, &confirmDialog, SLOT(accept()));
+    }
+  return app.exec();
+}

+ 206 - 0
Libs/Widgets/ctkConfirmExitDialog.cpp

@@ -0,0 +1,206 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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);
+}

+ 63 - 0
Libs/Widgets/ctkConfirmExitDialog.h

@@ -0,0 +1,63 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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;
+
+///
+class CTK_WIDGETS_EXPORT ctkConfirmExitDialog : public QDialog
+{
+  Q_OBJECT
+public:
+  typedef QDialog Superclass;
+  ctkConfirmExitDialog(QWidget* newParent = 0);
+  virtual ~ctkConfirmExitDialog();
+
+  void setPixmap(const QPixmap& pixmap);
+  void setText(const QString& text);
+  
+  void setDontShowAnymoreSettingsKey(const QString& key);
+  QString dontShowAnymoreSettingsKey()const;
+  
+  bool dontShowAnymore()const;
+
+  virtual void setVisible(bool visible);
+public slots:
+  virtual void accept();
+  
+  void setDontShowAnymore(bool dontShow);
+
+protected:
+  QScopedPointer<ctkConfirmExitDialogPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkConfirmExitDialog);
+  Q_DISABLE_COPY(ctkConfirmExitDialog);
+};
+
+#endif