Vidalia  0.2.21
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 
21 /** Default constructor. */
22 VMessageBox::VMessageBox(QWidget *parent)
23  : QMessageBox(parent)
24 {
25 }
26 
27 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Default,
28  * or 0 if none are. */
29 int
30 VMessageBox::defaultButton(int button0, int button1, int button2)
31 {
32  Q_UNUSED(button0);
33  int defaultButton = 0;
34  if (button1 & QMessageBox::Default) {
35  defaultButton = 1;
36  } else if (button2 & QMessageBox::Default) {
37  defaultButton = 2;
38  }
39  return defaultButton;
40 }
41 
42 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Escape,
43  * or -1 if none are. */
44 int
45 VMessageBox::escapeButton(int button0, int button1, int button2)
46 {
47  int escapeButton = -1;
48  if (button0 & QMessageBox::Escape) {
49  escapeButton = 0;
50  } else if (button1 & QMessageBox::Escape) {
51  escapeButton = 1;
52  } else if (button2 & QMessageBox::Escape) {
53  escapeButton = 2;
54  }
55  return escapeButton;
56 }
57 
58 /** Returns the Button enum value from the given return value. */
59 int
60 VMessageBox::selected(int ret, int button0, int button1, int button2)
61 {
62  if (ret == 0) {
63  return (button0 & QMessageBox::ButtonMask);
64  } else if (ret == 1) {
65  return (button1 & QMessageBox::ButtonMask);
66  }
67  return (button2 & QMessageBox::ButtonMask);
68 }
69 
70 /** Converts a Button enum value to a translated string. */
71 QString
73 {
74  QString text;
75  int button = (btn & ~QMessageBox::FlagMask);
76  switch (button) {
77  case Ok: text = tr("OK"); break;
78  case Cancel: text = tr("Cancel"); break;
79  case Yes: text = tr("Yes"); break;
80  case No: text = tr("No"); break;
81  case Help: text = tr("Help"); break;
82  case Retry: text = tr("Retry"); break;
83  case ShowLog: text = tr("Show Log"); break;
84  case ShowSettings: text = tr("Show Settings"); break;
85  case Continue: text = tr("Continue"); break;
86  case Quit: text = tr("Quit"); break;
87  case Browse: text = tr("Browse"); break;
88  default: break;
89  }
90  return text;
91 }
92 
93 /** Displays a critical message box with the given caption, message text, and
94  * visible buttons. To specify a button as a default button or an escape
95  * button, OR the Button enum value with QMessageBox::Default or
96  * QMessageBox::Escape, respectively. */
97 int
98 VMessageBox::critical(QWidget *parent, QString caption, QString text,
99  int button0, int button1, int button2)
100 {
101  int ret = QMessageBox::critical(parent, caption, p(text),
102  VMessageBox::buttonText(button0),
103  VMessageBox::buttonText(button1),
104  VMessageBox::buttonText(button2),
105  VMessageBox::defaultButton(button0, button1, button2),
106  VMessageBox::escapeButton(button0, button1, button2));
107  return VMessageBox::selected(ret, button0, button1, button2);
108 }
109 
110 /** Displays an question message box with the given caption, message text, and
111  * visible buttons. To specify a button as a default button or an escape
112  * button, OR the Button enum value with QMessageBox::Default or
113  * QMessageBox::Escape, respectively. */
114 int
115 VMessageBox::question(QWidget *parent, QString caption, QString text,
116  int button0, int button1, int button2)
117 {
118  int ret = QMessageBox::question(parent, caption, p(text),
119  VMessageBox::buttonText(button0),
120  VMessageBox::buttonText(button1),
121  VMessageBox::buttonText(button2),
122  VMessageBox::defaultButton(button0, button1, button2),
123  VMessageBox::escapeButton(button0, button1, button2));
124  return VMessageBox::selected(ret, button0, button1, button2);
125 }
126 
127 /** Displays an information message box with the given caption, message text, and
128  * visible buttons. To specify a button as a default button or an escape
129  * button, OR the Button enum value with QMessageBox::Default or
130  * QMessageBox::Escape, respectively. */
131 int
132 VMessageBox::information(QWidget *parent, QString caption, QString text,
133  int button0, int button1, int button2)
134 {
135  int ret = QMessageBox::information(parent, caption, p(text),
136  VMessageBox::buttonText(button0),
137  VMessageBox::buttonText(button1),
138  VMessageBox::buttonText(button2),
139  VMessageBox::defaultButton(button0, button1, button2),
140  VMessageBox::escapeButton(button0, button1, button2));
141  return VMessageBox::selected(ret, button0, button1, button2);
142 }
143 
144 /** Displays a warning message box with the given caption, message text, and
145  * visible buttons. To specify a button as a default button or an escape
146  * button, OR the Button enum value with QMessageBox::Default or
147  * QMessageBox::Escape, respectively. */
148 int
149 VMessageBox::warning(QWidget *parent, QString caption, QString text,
150  int button0, int button1, int button2)
151 {
152  int ret = QMessageBox::warning(parent, caption, p(text),
153  VMessageBox::buttonText(button0),
154  VMessageBox::buttonText(button1),
155  VMessageBox::buttonText(button2),
156  VMessageBox::defaultButton(button0, button1, button2),
157  VMessageBox::escapeButton(button0, button1, button2));
158  return VMessageBox::selected(ret, button0, button1, button2);
159 }
160