浏览代码

ENH: Give more information for not showing the message box again

This change was inspired by using a ctkMessageBox with three options
and wanting to be clear what would happen if the don't show again
option was used. Add the text from the button with the accept role
to the text string so that the user knows what will happen
by default if they chose not to show this message box again.
Nicole Aucoin 10 年之前
父节点
当前提交
aa4257acea
共有 2 个文件被更改,包括 62 次插入2 次删除
  1. 53 2
      Libs/Widgets/Testing/Cpp/ctkMessageBoxDontShowAgainTest.cpp
  2. 9 0
      Libs/Widgets/ctkMessageBox.cpp

+ 53 - 2
Libs/Widgets/Testing/Cpp/ctkMessageBoxDontShowAgainTest.cpp

@@ -50,15 +50,20 @@ private Q_SLOTS:
   // Check default values of ctkMessageBox
   void testDefaults();
 
-  // Ensure the "dont's show again" checkbox works correctly with 1 button (Ok)
+  // Ensure the "don't show again" checkbox works correctly with 1 button (Ok)
   void testDontShowAgain();
   void testDontShowAgain_data();
 
-  // Ensure the "dont's show again" checkbox works correctly with 2 buttons
+  // Ensure the "don't show again" checkbox works correctly with 2 buttons
   // Ok and Cancel
   void testOkCancel();
   void testOkCancel_data();
 
+  // Ensure the "don't show again" checkbox works correctly with 2 buttons
+  /// with custom text
+  void testDontShowAgainCustomText();
+  void testDontShowAgainCustomText_data();
+
   // Test Settings key with 1 button (Ok)
   void testDontShowAgainSettingsKey();
   void testDontShowAgainSettingsKey_data();
@@ -232,6 +237,52 @@ void ctkMessageBoxDontShowAgainTester::testOkCancel_data()
 }
 
 // ----------------------------------------------------------------------------
+void ctkMessageBoxDontShowAgainTester::testDontShowAgainCustomText()
+{
+  ctkMessageBox messageBox;
+
+  messageBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
+
+  QAbstractButton *okButton = messageBox.button(QMessageBox::Ok);
+  okButton->setText("SaveData");
+  QAbstractButton *cancelButton = messageBox.button(QMessageBox::Cancel);
+  cancelButton->setText("Discard Data");
+
+  QFETCH(bool, visible);
+  messageBox.setDontShowAgainVisible(visible);
+
+  QFETCH(bool, dontShowAgain);
+  messageBox.setDontShowAgain(dontShowAgain);
+
+  this->testExecMessageBox(messageBox);
+
+  // check that the don't show again text has been updated
+  QCheckBox *checkBox = messageBox.findChild<QCheckBox*>("ctk_msgbox_dontshowcheckbox");
+  QString dontShowAgainText = checkBox->text();
+
+  if (visible)
+    {
+    // the custom text was set from the Ok box as it has the accept role
+    QString expectedString = QString("Don't show this message again and always ")
+      + okButton->text();
+    QCOMPARE(dontShowAgainText, expectedString);
+    }
+  else
+    {
+    // the custom text was not added to the end of the standard message
+    QString expectedString = QString("Don't show this message again");
+    QCOMPARE(dontShowAgainText, expectedString);
+    }
+}
+
+// ----------------------------------------------------------------------------
+void ctkMessageBoxDontShowAgainTester::testDontShowAgainCustomText_data()
+{
+  // use the basic test set up
+  this->testDontShowAgain_data();
+}
+
+// ----------------------------------------------------------------------------
 void ctkMessageBoxDontShowAgainTester::testDontShowAgainSettingsKey()
 {
   ctkMessageBox messageBox;

+ 9 - 0
Libs/Widgets/ctkMessageBox.cpp

@@ -200,6 +200,15 @@ void ctkMessageBox::setDontShowAgainVisible(bool visible)
     }
   QGridLayout *grid = static_cast<QGridLayout *>(this->layout());
   d->DontShowAgainCheckBox->setVisible(true);
+  // update the text from the button with the accept role
+  QAbstractButton *acceptButton = d->button(QMessageBox::AcceptRole);
+  if (acceptButton && !acceptButton->text().isEmpty())
+    {
+    QString dontShowAgainText =
+      this->tr("Don't show this message again and always %1").arg(acceptButton->text());
+
+    d->DontShowAgainCheckBox->setText(dontShowAgainText);
+    }
   grid->addWidget(d->DontShowAgainCheckBox, 1, 1, 1, 1);
 }