Vidalia 0.3.1
VMessageBox.cpp
Go to the documentation of this file.
1/*
2** This file is part of Vidalia, and is subject to the license terms in the
3** LICENSE file, found in the top level directory of this distribution. If you
4** did not receive the LICENSE file with this file, you may obtain it from the
5** Vidalia source package distributed by the Vidalia Project at
6** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7** including this file, may be copied, modified, propagated, or distributed
8** except according to the terms described in the LICENSE file.
9*/
10
11/*
12** \file VMessageBox.cpp
13** \brief Provides a custom Vidalia mesage box
14*/
15
16#include "VMessageBox.h"
17
18#include "html.h"
19
20#include <QGridLayout>
21#include <QCheckBox>
22#include <QDialogButtonBox>
23#include <QPushButton>
24
25/** Default constructor. */
27 : QMessageBox(parent)
28{
29 _chkRemember = new QCheckBox("");
30 QGridLayout *gl = qobject_cast<QGridLayout *>(layout());
31 gl->addWidget(_chkRemember, gl->rowCount() - 2, 1);
32 _chkRemember->setVisible(false);
33}
34
35/** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Default,
36 * or 0 if none are. */
37int
38VMessageBox::defaultButton(int button0, int button1, int button2)
39{
40 Q_UNUSED(button0);
41 int defaultButton = 0;
42 if (button1 & QMessageBox::Default) {
43 defaultButton = 1;
44 } else if (button2 & QMessageBox::Default) {
45 defaultButton = 2;
46 }
47 return defaultButton;
48}
49
50/** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Escape,
51 * or -1 if none are. */
52int
53VMessageBox::escapeButton(int button0, int button1, int button2)
54{
55 int escapeButton = -1;
56 if (button0 & QMessageBox::Escape) {
57 escapeButton = 0;
58 } else if (button1 & QMessageBox::Escape) {
59 escapeButton = 1;
60 } else if (button2 & QMessageBox::Escape) {
61 escapeButton = 2;
62 }
63 return escapeButton;
64}
65
66/** Returns the Button enum value from the given return value. */
67int
68VMessageBox::selected(int ret, int button0, int button1, int button2)
69{
70 if (ret == 0) {
71 return (button0 & QMessageBox::ButtonMask);
72 } else if (ret == 1) {
73 return (button1 & QMessageBox::ButtonMask);
74 }
75 return (button2 & QMessageBox::ButtonMask);
76}
77
78/** Converts a Button enum value to a translated string. */
79QString
81{
82 QString text;
83 int button = (btn & ~QMessageBox::FlagMask);
84 switch (button) {
85 case Ok: text = tr("OK"); break;
86 case Cancel: text = tr("Cancel"); break;
87 case Yes: text = tr("Yes"); break;
88 case No: text = tr("No"); break;
89 case Help: text = tr("Help"); break;
90 case Retry: text = tr("Retry"); break;
91 case ShowLog: text = tr("Show Log"); break;
92 case ShowSettings: text = tr("Show Settings"); break;
93 case Continue: text = tr("Continue"); break;
94 case Quit: text = tr("Quit"); break;
95 case Browse: text = tr("Browse"); break;
96 default: break;
97 }
98 return text;
99}
100
101/** Displays a critical message box with the given caption, message text, and
102 * visible buttons. To specify a button as a default button or an escape
103 * button, OR the Button enum value with QMessageBox::Default or
104 * QMessageBox::Escape, respectively. */
105int
106VMessageBox::critical(QWidget *parent, QString caption, QString text,
107 int button0, int button1, int button2)
108{
109 int ret = QMessageBox::critical(parent, caption, p(text),
110 VMessageBox::buttonText(button0),
111 VMessageBox::buttonText(button1),
113 VMessageBox::defaultButton(button0, button1, button2),
114 VMessageBox::escapeButton(button0, button1, button2));
115 return VMessageBox::selected(ret, button0, button1, button2);
116}
117
118/** Displays an question message box with the given caption, message text, and
119 * visible buttons. To specify a button as a default button or an escape
120 * button, OR the Button enum value with QMessageBox::Default or
121 * QMessageBox::Escape, respectively. */
122int
123VMessageBox::question(QWidget *parent, QString caption, QString text,
124 int button0, int button1, int button2,
125 QString remember, VSettings *settings, QString key)
126{
127 VMessageBox messageBox(parent);
128
129 messageBox.setIcon(QMessageBox::Question);
130 messageBox.setWindowTitle(caption);
131 messageBox.setText(text);
132 messageBox.setStandardButtons(QMessageBox::NoButton);
133
134 if(settings) {
135 messageBox._chkRemember->setVisible(true);
136 messageBox._chkRemember->setText(remember);
137 }
138
139 QString myButton0Text = VMessageBox::buttonText(button0);
140 if (myButton0Text.isEmpty())
141 myButton0Text = QDialogButtonBox::tr("OK");
142 messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
143
144 if (!VMessageBox::buttonText(button1).isEmpty())
145 messageBox.addButton(VMessageBox::buttonText(button1), QMessageBox::ActionRole);
146
147 if (!VMessageBox::buttonText(button2).isEmpty())
148 messageBox.addButton(VMessageBox::buttonText(button2), QMessageBox::ActionRole);
149
150 const QList<QAbstractButton *> &buttonList = messageBox.buttons();
151 messageBox.setDefaultButton(qobject_cast<QPushButton *>(buttonList
152 .value(VMessageBox::defaultButton(button0,
153 button1,
154 button2))));
155 messageBox.setEscapeButton(buttonList.value(VMessageBox::escapeButton(button0, button1, button2)));
156 int ret = messageBox.exec();
157
158 if(!settings)
159 return ret;
160 settings->setValue(key, messageBox._chkRemember->checkState() == Qt::Checked);
161
162 return ret;
163}
164
165/** Displays an information message box with the given caption, message text, and
166 * visible buttons. To specify a button as a default button or an escape
167 * button, OR the Button enum value with QMessageBox::Default or
168 * QMessageBox::Escape, respectively. */
169int
170VMessageBox::information(QWidget *parent, QString caption, QString text,
171 int button0, int button1, int button2)
172{
173 int ret = QMessageBox::information(parent, caption, p(text),
174 VMessageBox::buttonText(button0),
175 VMessageBox::buttonText(button1),
177 VMessageBox::defaultButton(button0, button1, button2),
178 VMessageBox::escapeButton(button0, button1, button2));
179 return VMessageBox::selected(ret, button0, button1, button2);
180}
181
182/** Displays a warning message box with the given caption, message text, and
183 * visible buttons. To specify a button as a default button or an escape
184 * button, OR the Button enum value with QMessageBox::Default or
185 * QMessageBox::Escape, respectively. */
186int
187VMessageBox::warning(QWidget *parent, QString caption, QString text,
188 int button0, int button1, int button2)
189{
190 int ret = QMessageBox::warning(parent, caption, p(text),
191 VMessageBox::buttonText(button0),
192 VMessageBox::buttonText(button1),
194 VMessageBox::defaultButton(button0, button1, button2),
195 VMessageBox::escapeButton(button0, button1, button2));
196 return VMessageBox::selected(ret, button0, button1, button2);
197}
static int information(QWidget *parent, QString caption, QString text, int button0, int button1=NoButton, int button2=NoButton)
static int critical(QWidget *parent, QString caption, QString text, int button0, int button1=NoButton, int button2=NoButton)
QCheckBox * _chkRemember
Definition: VMessageBox.h:96
static int selected(int ret, int button0, int button1, int button2)
Definition: VMessageBox.cpp:68
static QString buttonText(int button)
Definition: VMessageBox.cpp:80
static int warning(QWidget *parent, QString caption, QString text, int button0, int button1=NoButton, int button2=NoButton)
static int escapeButton(int button0, int button1, int button2)
Definition: VMessageBox.cpp:53
static int question(QWidget *parent, QString caption, QString text, int button0, int button1=NoButton, int button2=NoButton, QString remember=QString(), VSettings *settings=0, QString key=QString())
VMessageBox(QWidget *parent=0)
Definition: VMessageBox.cpp:26
static int defaultButton(int button0, int button1, int button2)
Definition: VMessageBox.cpp:38
virtual void setValue(const QString &key, const QVariant &val)
Definition: VSettings.cpp:61
QString p(QString str)
Definition: html.cpp:22