2
* removeconfirmationmessagebox.cpp - generic confirmation of destructive action
3
* Copyright (C) 2008-2010 Yandex LLC (Michail Pishchagin)
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
#include "removeconfirmationmessagebox.h"
23
#include <QApplication>
24
#include <QPushButton>
28
#include "applicationinfo.h"
30
//----------------------------------------------------------------------------
31
// RemoveConfirmationMessageBoxManager
32
//----------------------------------------------------------------------------
34
RemoveConfirmationMessageBoxManager* RemoveConfirmationMessageBoxManager::instance_ = 0;
35
int RemoveConfirmationMessageBoxManager::onlineId_ = 0;
37
RemoveConfirmationMessageBoxManager* RemoveConfirmationMessageBoxManager::instance()
40
instance_ = new RemoveConfirmationMessageBoxManager();
45
void RemoveConfirmationMessageBoxManager::processData(const QString& id, const QList<DataCallback> callbacks, const QString& title, const QString& informativeText, QWidget* parent, const QStringList& actionNames, QMessageBox::Icon icon)
48
data.onlineId = ++onlineId_;
50
data.callbacks = callbacks;
52
data.informativeText = informativeText;
53
data.buttons = actionNames;
57
// FIXME: duplicates mustn't be allowed
58
foreach(Data d, data_) {
65
QTimer::singleShot(0, this, SLOT(update()));
68
void RemoveConfirmationMessageBoxManager::showInformation(const QString& id, const QString& title, const QString& informativeText, QWidget* parent)
71
buttons << RemoveConfirmationMessageBox::tr("OK");
73
QList<DataCallback> callbacks;
75
processData(id, callbacks, title, informativeText, parent, buttons, QMessageBox::Information);
78
void RemoveConfirmationMessageBoxManager::removeConfirmation(const QString& id, QObject* obj, const char* slot, const QString& title, const QString& informativeText, QWidget* parent, const QString& destructiveActionName)
81
if (!destructiveActionName.isEmpty())
82
buttons << destructiveActionName;
84
buttons << RemoveConfirmationMessageBox::tr("Delete");
85
buttons << RemoveConfirmationMessageBox::tr("Cancel");
87
QList<DataCallback> callbacks;
88
callbacks << DataCallback(obj, slot);
90
processData(id, callbacks, title, informativeText, parent, buttons, QMessageBox::Warning);
93
void RemoveConfirmationMessageBoxManager::removeConfirmation(const QString& id, QObject* obj1, const char* action1slot, QObject* obj2, const char* action2slot, const QString& title, const QString& informativeText, QWidget* parent, const QString& action1name, const QString& action2name)
96
buttons << action1name;
97
buttons << action2name;
98
buttons << RemoveConfirmationMessageBox::tr("Cancel");
100
QList<DataCallback> callbacks;
101
callbacks << DataCallback(obj1, action1slot);
102
callbacks << DataCallback(obj2, action2slot);
104
processData(id, callbacks, title, informativeText, parent, buttons, QMessageBox::Warning);
107
void RemoveConfirmationMessageBoxManager::update()
109
while (!data_.isEmpty()) {
110
Data data = data_.takeFirst();
112
Q_ASSERT(data.buttons.count() >= 1 && data.buttons.count() <= 3);
113
RemoveConfirmationMessageBox msgBox(data.title, data.informativeText, data.parent);
114
msgBox.setIcon(data.icon);
116
QStringList buttons = data.buttons;
117
if (data.icon == QMessageBox::Warning) {
118
buttons.takeLast(); // Cancel
119
Q_ASSERT(!buttons.isEmpty());
120
msgBox.setDestructiveActionName(buttons.takeFirst());
121
if (!buttons.isEmpty()) {
122
msgBox.setComplimentaryActionName(buttons.takeFirst());
126
msgBox.setInfoActionName(buttons.takeFirst());
131
QList<bool> callbackData;
132
for (int i = 0; i < data.callbacks.count(); ++i) {
134
callbackData << msgBox.removeAction();
136
callbackData << msgBox.complimentaryAction();
138
callbackData << false;
141
for (int i = 0; i < data.callbacks.count(); ++i) {
142
QMetaObject::invokeMethod(data.callbacks[i].obj, data.callbacks[i].slot, Qt::DirectConnection,
143
QGenericReturnArgument(),
144
Q_ARG(QString, data.id),
145
Q_ARG(bool, callbackData[i]));
150
RemoveConfirmationMessageBoxManager::RemoveConfirmationMessageBoxManager()
151
: QObject(QCoreApplication::instance())
155
RemoveConfirmationMessageBoxManager::~RemoveConfirmationMessageBoxManager()
159
//----------------------------------------------------------------------------
160
// RemoveConfirmationMessageBox
161
//----------------------------------------------------------------------------
162
RemoveConfirmationMessageBox::RemoveConfirmationMessageBox(const QString& title, const QString& informativeText, QWidget* parent)
165
, complimentaryButton_(0)
169
setWindowTitle(ApplicationInfo::name());
172
setInformativeText(informativeText);
174
setIcon(QMessageBox::Warning);
175
int iconSize = style()->pixelMetric(QStyle::PM_MessageBoxIconSize);
176
QIcon tmpIcon= style()->standardIcon(QStyle::SP_MessageBoxWarning);
177
if (!tmpIcon.isNull())
178
setIconPixmap(tmpIcon.pixmap(iconSize, iconSize));
180
// doesn't work with borderless top-level windows on Mac OS X
181
// QWidget* window = parent->window();
182
// msgBox.setParent(window);
183
// msgBox.setWindowFlags(Qt::Sheet);
187
void RemoveConfirmationMessageBox::setDestructiveActionName(const QString& destructiveAction)
189
Q_ASSERT(!removeButton_);
190
Q_ASSERT(!cancelButton_);
191
Q_ASSERT(!infoButton_);
192
removeButton_ = addButton(destructiveAction, QMessageBox::AcceptRole /*QMessageBox::DestructiveRole*/);
193
cancelButton_ = addButton(QMessageBox::Cancel);
194
setDefaultButton(removeButton_);
197
void RemoveConfirmationMessageBox::setComplimentaryActionName(const QString& complimentaryAction)
199
Q_ASSERT(removeButton_);
200
Q_ASSERT(cancelButton_);
201
Q_ASSERT(!infoButton_);
202
Q_ASSERT(!complimentaryButton_);
203
complimentaryButton_ = addButton(complimentaryAction, QMessageBox::AcceptRole);
206
void RemoveConfirmationMessageBox::setInfoActionName(const QString& infoAction)
208
Q_ASSERT(!removeButton_);
209
Q_ASSERT(!cancelButton_);
210
Q_ASSERT(!infoButton_);
211
infoButton_ = addButton(infoAction, QMessageBox::AcceptRole);
212
setDefaultButton(infoButton_);
215
QString RemoveConfirmationMessageBox::processInformativeText(const QString& informativeText)
217
QString text = informativeText;
218
text.replace("<br>", "\n");
221
text.replace(rx, "");
225
void RemoveConfirmationMessageBox::doExec()
227
if (!removeButton_ && !infoButton_) {
228
setDestructiveActionName(tr("Delete"));
231
setText(processInformativeText(informativeText()));
232
setInformativeText(QString());
234
Q_ASSERT((removeButton_ && cancelButton_) || infoButton_);
238
bool RemoveConfirmationMessageBox::removeAction() const
240
return clickedButton() == removeButton_;
243
bool RemoveConfirmationMessageBox::complimentaryAction() const
245
return clickedButton() == complimentaryButton_;
248
bool RemoveConfirmationMessageBox::infoAction() const
250
return clickedButton() == infoButton_;