~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/removeconfirmationmessagebox.cpp

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * removeconfirmationmessagebox.cpp - generic confirmation of destructive action
 
3
 * Copyright (C) 2008-2010  Yandex LLC (Michail Pishchagin)
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 *
 
19
 */
 
20
 
 
21
#include "removeconfirmationmessagebox.h"
 
22
 
 
23
#include <QApplication>
 
24
#include <QPushButton>
 
25
#include <QTimer>
 
26
#include <QStyle>
 
27
 
 
28
#include "applicationinfo.h"
 
29
 
 
30
//----------------------------------------------------------------------------
 
31
// RemoveConfirmationMessageBoxManager
 
32
//----------------------------------------------------------------------------
 
33
 
 
34
RemoveConfirmationMessageBoxManager* RemoveConfirmationMessageBoxManager::instance_ = 0;
 
35
int RemoveConfirmationMessageBoxManager::onlineId_ = 0;
 
36
 
 
37
RemoveConfirmationMessageBoxManager* RemoveConfirmationMessageBoxManager::instance()
 
38
{
 
39
        if (!instance_) {
 
40
                instance_ = new RemoveConfirmationMessageBoxManager();
 
41
        }
 
42
        return instance_;
 
43
}
 
44
 
 
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)
 
46
{
 
47
        Data data;
 
48
        data.onlineId = ++onlineId_;
 
49
        data.id = id;
 
50
        data.callbacks = callbacks;
 
51
        data.title = title;
 
52
        data.informativeText = informativeText;
 
53
        data.buttons = actionNames;
 
54
        data.parent = parent;
 
55
        data.icon = icon;
 
56
 
 
57
        // FIXME: duplicates mustn't be allowed
 
58
        foreach(Data d, data_) {
 
59
                Q_ASSERT(d.id != id);
 
60
                if (d.id == id)
 
61
                        return;
 
62
        }
 
63
 
 
64
        data_ << data;
 
65
        QTimer::singleShot(0, this, SLOT(update()));
 
66
}
 
67
 
 
68
void RemoveConfirmationMessageBoxManager::showInformation(const QString& id, const QString& title, const QString& informativeText, QWidget* parent)
 
69
{
 
70
        QStringList buttons;
 
71
        buttons << RemoveConfirmationMessageBox::tr("OK");
 
72
 
 
73
        QList<DataCallback> callbacks;
 
74
 
 
75
        processData(id, callbacks, title, informativeText, parent, buttons, QMessageBox::Information);
 
76
}
 
77
 
 
78
void RemoveConfirmationMessageBoxManager::removeConfirmation(const QString& id, QObject* obj, const char* slot, const QString& title, const QString& informativeText, QWidget* parent, const QString& destructiveActionName)
 
79
{
 
80
        QStringList buttons;
 
81
        if (!destructiveActionName.isEmpty())
 
82
                buttons << destructiveActionName;
 
83
        else
 
84
                buttons << RemoveConfirmationMessageBox::tr("Delete");
 
85
        buttons << RemoveConfirmationMessageBox::tr("Cancel");
 
86
 
 
87
        QList<DataCallback> callbacks;
 
88
        callbacks << DataCallback(obj, slot);
 
89
 
 
90
        processData(id, callbacks, title, informativeText, parent, buttons, QMessageBox::Warning);
 
91
}
 
92
 
 
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)
 
94
{
 
95
        QStringList buttons;
 
96
        buttons << action1name;
 
97
        buttons << action2name;
 
98
        buttons << RemoveConfirmationMessageBox::tr("Cancel");
 
99
 
 
100
        QList<DataCallback> callbacks;
 
101
        callbacks << DataCallback(obj1, action1slot);
 
102
        callbacks << DataCallback(obj2, action2slot);
 
103
 
 
104
        processData(id, callbacks, title, informativeText, parent, buttons, QMessageBox::Warning);
 
105
}
 
106
 
 
107
void RemoveConfirmationMessageBoxManager::update()
 
108
{
 
109
        while (!data_.isEmpty()) {
 
110
                Data data = data_.takeFirst();
 
111
 
 
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);
 
115
 
 
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());
 
123
                        }
 
124
                }
 
125
                else {
 
126
                        msgBox.setInfoActionName(buttons.takeFirst());
 
127
                }
 
128
 
 
129
                msgBox.doExec();
 
130
 
 
131
                QList<bool> callbackData;
 
132
                for (int i = 0; i < data.callbacks.count(); ++i) {
 
133
                        if (i == 0)
 
134
                                callbackData << msgBox.removeAction();
 
135
                        else if (i == 1)
 
136
                                callbackData << msgBox.complimentaryAction();
 
137
                        else
 
138
                                callbackData << false;
 
139
                }
 
140
 
 
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]));
 
146
                }
 
147
        }
 
148
}
 
149
 
 
150
RemoveConfirmationMessageBoxManager::RemoveConfirmationMessageBoxManager()
 
151
        : QObject(QCoreApplication::instance())
 
152
{
 
153
}
 
154
 
 
155
RemoveConfirmationMessageBoxManager::~RemoveConfirmationMessageBoxManager()
 
156
{
 
157
}
 
158
 
 
159
//----------------------------------------------------------------------------
 
160
// RemoveConfirmationMessageBox
 
161
//----------------------------------------------------------------------------
 
162
RemoveConfirmationMessageBox::RemoveConfirmationMessageBox(const QString& title, const QString& informativeText, QWidget* parent)
 
163
        : QMessageBox()
 
164
        , removeButton_(0)
 
165
        , complimentaryButton_(0)
 
166
        , infoButton_(0)
 
167
        , cancelButton_(0)
 
168
{
 
169
        setWindowTitle(ApplicationInfo::name());
 
170
 
 
171
        setText(title);
 
172
        setInformativeText(informativeText);
 
173
 
 
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));
 
179
 
 
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);
 
184
        Q_UNUSED(parent);
 
185
}
 
186
 
 
187
void RemoveConfirmationMessageBox::setDestructiveActionName(const QString& destructiveAction)
 
188
{
 
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_);
 
195
}
 
196
 
 
197
void RemoveConfirmationMessageBox::setComplimentaryActionName(const QString& complimentaryAction)
 
198
{
 
199
        Q_ASSERT(removeButton_);
 
200
        Q_ASSERT(cancelButton_);
 
201
        Q_ASSERT(!infoButton_);
 
202
        Q_ASSERT(!complimentaryButton_);
 
203
        complimentaryButton_ = addButton(complimentaryAction, QMessageBox::AcceptRole);
 
204
}
 
205
 
 
206
void RemoveConfirmationMessageBox::setInfoActionName(const QString& infoAction)
 
207
{
 
208
        Q_ASSERT(!removeButton_);
 
209
        Q_ASSERT(!cancelButton_);
 
210
        Q_ASSERT(!infoButton_);
 
211
        infoButton_ = addButton(infoAction, QMessageBox::AcceptRole);
 
212
        setDefaultButton(infoButton_);
 
213
}
 
214
 
 
215
QString RemoveConfirmationMessageBox::processInformativeText(const QString& informativeText)
 
216
{
 
217
        QString text = informativeText;
 
218
        text.replace("<br>", "\n");
 
219
        QRegExp rx("<.+>");
 
220
        rx.setMinimal(true);
 
221
        text.replace(rx, "");
 
222
        return text;
 
223
}
 
224
 
 
225
void RemoveConfirmationMessageBox::doExec()
 
226
{
 
227
        if (!removeButton_ && !infoButton_) {
 
228
                setDestructiveActionName(tr("Delete"));
 
229
        }
 
230
 
 
231
        setText(processInformativeText(informativeText()));
 
232
        setInformativeText(QString());
 
233
 
 
234
        Q_ASSERT((removeButton_ && cancelButton_) || infoButton_);
 
235
        exec();
 
236
}
 
237
 
 
238
bool RemoveConfirmationMessageBox::removeAction() const
 
239
{
 
240
        return clickedButton() == removeButton_;
 
241
}
 
242
 
 
243
bool RemoveConfirmationMessageBox::complimentaryAction() const
 
244
{
 
245
        return clickedButton() == complimentaryButton_;
 
246
}
 
247
 
 
248
bool RemoveConfirmationMessageBox::infoAction() const
 
249
{
 
250
        return clickedButton() == infoButton_;
 
251
}