00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "qxtconfirmationmessage.h"
00026
00027 #include <QCoreApplication>
00028 #if QT_VERSION >= 0x040200
00029 #include <QDialogButtonBox>
00030 #endif // QT_VERSION
00031 #include <QPushButton>
00032 #include <QGridLayout>
00033 #include <QCheckBox>
00034
00035 static const QLatin1String DEFAULT_ORGANIZATION("QxtGui");
00036 static const QLatin1String DEFAULT_APPLICATION("QxtConfirmationMessage");
00037
00038 class QxtConfirmationMessagePrivate : public QxtPrivate<QxtConfirmationMessage>
00039 {
00040 public:
00041 QXT_DECLARE_PUBLIC(QxtConfirmationMessage);
00042 void init(const QString& message = QString());
00043
00044 QString key() const;
00045 QString applicationName() const;
00046 QString organizationName() const;
00047
00048 int showAgain();
00049 void doNotShowAgain(int result);
00050 void reset();
00051
00052 bool remember;
00053 QCheckBox* confirm;
00054 QString overrideApp;
00055 QString overrideKey;
00056 QString overrideOrg;
00057
00058 static QString path;
00059 static QSettings::Scope scope;
00060 static QSettings::Format format;
00061 };
00062
00063 QString QxtConfirmationMessagePrivate::path;
00064 QSettings::Scope QxtConfirmationMessagePrivate::scope = QSettings::UserScope;
00065 QSettings::Format QxtConfirmationMessagePrivate::format = QSettings::NativeFormat;
00066
00067 void QxtConfirmationMessagePrivate::init(const QString& message)
00068 {
00069 remember = false;
00070 #if QT_VERSION >= 0x040200
00071 confirm = new QCheckBox(&qxt_p());
00072 if (!message.isNull())
00073 confirm->setText(message);
00074 else
00075 confirm->setText(QxtConfirmationMessage::tr("Do not show again."));
00076
00077 QGridLayout* grid = qobject_cast<QGridLayout*>(qxt_p().layout());
00078 QDialogButtonBox* buttons = qFindChild<QDialogButtonBox*>(&qxt_p());
00079 if (grid && buttons)
00080 {
00081 const int idx = grid->indexOf(buttons);
00082 int row, column, rowSpan, columnSpan = 0;
00083 grid->getItemPosition(idx, &row, &column, &rowSpan, &columnSpan);
00084 QLayoutItem* buttonsItem = grid->takeAt(idx);
00085 grid->addWidget(confirm, row, column, rowSpan, columnSpan, Qt::AlignLeft | Qt::AlignTop);
00086 grid->addItem(buttonsItem, ++row, column, rowSpan, columnSpan);
00087 }
00088 #endif // QT_VERSION
00089 }
00090
00091 QString QxtConfirmationMessagePrivate::key() const
00092 {
00093 QString value = overrideKey;
00094 if (value.isEmpty())
00095 {
00096 const QString all = qxt_p().windowTitle() + qxt_p().text()
00097 #if QT_VERSION >= 0x040200
00098 + qxt_p().informativeText()
00099 #endif // QT_VERSION
00100 ;
00101 const QByteArray data = all.toLocal8Bit();
00102 value = QString::number(qChecksum(data.constData(), data.length()));
00103 }
00104 return value;
00105 }
00106
00107 QString QxtConfirmationMessagePrivate::applicationName() const
00108 {
00109 QString name = overrideApp;
00110 if (name.isEmpty())
00111 name = QCoreApplication::applicationName();
00112 if (name.isEmpty())
00113 name = DEFAULT_APPLICATION;
00114 return name;
00115 }
00116
00117 QString QxtConfirmationMessagePrivate::organizationName() const
00118 {
00119 QString name = overrideOrg;
00120 if (name.isEmpty())
00121 name = QCoreApplication::organizationName();
00122 if (name.isEmpty())
00123 name = DEFAULT_ORGANIZATION;
00124 return name;
00125 }
00126
00127 int QxtConfirmationMessagePrivate::showAgain()
00128 {
00129 QSettings settings(format, scope, organizationName(), applicationName());
00130 if (!path.isEmpty())
00131 settings.beginGroup(path);
00132 return settings.value(key(), -1).toInt();
00133 }
00134
00135 void QxtConfirmationMessagePrivate::doNotShowAgain(int result)
00136 {
00137 QSettings settings(format, scope, organizationName(), applicationName());
00138 if (!path.isEmpty())
00139 settings.beginGroup(path);
00140 settings.setValue(key(), result);
00141 }
00142
00143 void QxtConfirmationMessagePrivate::reset()
00144 {
00145 QSettings settings(format, scope, organizationName(), applicationName());
00146 if (!path.isEmpty())
00147 settings.beginGroup(path);
00148 settings.remove(key());
00149 }
00150
00182 QxtConfirmationMessage::QxtConfirmationMessage(QWidget* parent)
00183 : QMessageBox(parent)
00184 {
00185 QXT_INIT_PRIVATE(QxtConfirmationMessage);
00186 qxt_d().init();
00187 }
00188
00192 #if QT_VERSION >= 0x040200
00193 QxtConfirmationMessage::QxtConfirmationMessage(QMessageBox::Icon icon, const QString& title, const QString& text, const QString& confirmation,
00194 QMessageBox::StandardButtons buttons, QWidget* parent, Qt::WindowFlags flags)
00195 : QMessageBox(icon, title, text, buttons, parent, flags)
00196 {
00197 QXT_INIT_PRIVATE(QxtConfirmationMessage);
00198 qxt_d().init(confirmation);
00199 }
00200 #endif // QT_VERSION
00201
00205 QxtConfirmationMessage::~QxtConfirmationMessage()
00206 {}
00207
00208
00209 #if QT_VERSION >= 0x040200
00210 QMessageBox::StandardButton QxtConfirmationMessage::confirm(QWidget* parent,
00211 const QString& title, const QString& text, const QString& confirmation,
00212 QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
00213 {
00214 QxtConfirmationMessage msgBox(QMessageBox::NoIcon, title, text, confirmation, QMessageBox::NoButton, parent);
00215 QDialogButtonBox* buttonBox = qFindChild<QDialogButtonBox*>(&msgBox);
00216 Q_ASSERT(buttonBox != 0);
00217
00218 uint mask = QMessageBox::FirstButton;
00219 while (mask <= QMessageBox::LastButton)
00220 {
00221 uint sb = buttons & mask;
00222 mask <<= 1;
00223 if (!sb)
00224 continue;
00225 QPushButton* button = msgBox.addButton((QMessageBox::StandardButton)sb);
00226
00227 if (msgBox.defaultButton())
00228 continue;
00229 if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
00230 || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton)))
00231 msgBox.setDefaultButton(button);
00232 }
00233 if (msgBox.exec() == -1)
00234 return QMessageBox::Cancel;
00235 return msgBox.standardButton(msgBox.clickedButton());
00236 }
00237 #endif // QT_VERSION
00238
00245 QString QxtConfirmationMessage::confirmationText() const
00246 {
00247 return qxt_d().confirm->text();
00248 }
00249
00250 void QxtConfirmationMessage::setConfirmationText(const QString& confirmation)
00251 {
00252 qxt_d().confirm->setText(confirmation);
00253 }
00254
00265 QString QxtConfirmationMessage::overrideSettingsApplication() const
00266 {
00267 return qxt_d().overrideApp;
00268 }
00269
00270 void QxtConfirmationMessage::setOverrideSettingsApplication(const QString& application)
00271 {
00272 qxt_d().overrideApp = application;
00273 }
00274
00284 QString QxtConfirmationMessage::overrideSettingsKey() const
00285 {
00286 return qxt_d().overrideKey;
00287 }
00288
00289 void QxtConfirmationMessage::setOverrideSettingsKey(const QString& key)
00290 {
00291 qxt_d().overrideKey = key;
00292 }
00293
00304 QString QxtConfirmationMessage::overrideSettingsOrganization() const
00305 {
00306 return qxt_d().overrideOrg;
00307 }
00308
00309 void QxtConfirmationMessage::setOverrideSettingsOrganization(const QString& organization)
00310 {
00311 qxt_d().overrideOrg = organization;
00312 }
00313
00322 bool QxtConfirmationMessage::rememberOnReject() const
00323 {
00324 return qxt_d().remember;
00325 }
00326
00327 void QxtConfirmationMessage::setRememberOnReject(bool remember)
00328 {
00329 qxt_d().remember = remember;
00330 }
00331
00337 QSettings::Format QxtConfirmationMessage::settingsFormat()
00338 {
00339 return QxtConfirmationMessagePrivate::format;
00340 }
00341
00345 void QxtConfirmationMessage::setSettingsFormat(QSettings::Format format)
00346 {
00347 QxtConfirmationMessagePrivate::format = format;
00348 }
00349
00355 QSettings::Scope QxtConfirmationMessage::settingsScope()
00356 {
00357 return QxtConfirmationMessagePrivate::scope;
00358 }
00359
00363 void QxtConfirmationMessage::setSettingsScope(QSettings::Scope scope)
00364 {
00365 QxtConfirmationMessagePrivate::scope = scope;
00366 }
00367
00373 QString QxtConfirmationMessage::settingsPath()
00374 {
00375 return QxtConfirmationMessagePrivate::path;
00376 }
00377
00381 void QxtConfirmationMessage::setSettingsPath(const QString& path)
00382 {
00383 QxtConfirmationMessagePrivate::path = path;
00384 }
00385
00401 int QxtConfirmationMessage::exec()
00402 {
00403 int res = qxt_d().showAgain();
00404 if (res == -1)
00405 res = QMessageBox::exec();
00406 return res;
00407 }
00408
00412 void QxtConfirmationMessage::done(int result)
00413 {
00414 #if QT_VERSION >= 0x040200
00415 QDialogButtonBox* buttons = qFindChild<QDialogButtonBox*>(this);
00416 Q_ASSERT(buttons != 0);
00417
00418 int role = buttons->buttonRole(clickedButton());
00419 if (qxt_d().confirm->isChecked() &&
00420 (qxt_d().remember || role != QDialogButtonBox::RejectRole))
00421 {
00422 qxt_d().doNotShowAgain(result);
00423 }
00424 #endif // QT_VERSION
00425 QMessageBox::done(result);
00426 }
00427
00433 void QxtConfirmationMessage::reset()
00434 {
00435 #if QT_VERSION >= 0x040200
00436 qxt_d().reset();
00437 #endif // QT_VERSION
00438 }