Debian Codegraph @

src/Gui/Dialogs/DlgCheckableMessageBox.cpp

raw
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation ([email protected])
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include <QHeaderView>
31 #include <QPushButton>
32
33 #include <App/Application.h>
34
35 #include "Dialogs/DlgCheckableMessageBox.h"
36 #include "ui_DlgCheckableMessageBox.h"
37 #include "MainWindow.h"
38
39
40 namespace Gui
41 {
42 namespace Dialog
43 {
44 QByteArray toParamEntry(QString name)
45 {
46 name.replace(QStringLiteral(" "), QStringLiteral("_"));
47 return name.toLatin1();
48 }
49
50 QPixmap getStandardIcon(QWidget* widget, QStyle::StandardPixmap standardPixmap)
51 {
52 int iconSize = widget->style()->pixelMetric(QStyle::PM_MessageBoxIconSize, nullptr, widget);
53 QIcon icon = widget->style()->standardIcon(standardPixmap);
54 if (!icon.isNull()) {
55 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
56 return icon.pixmap(QSize(iconSize, iconSize));
57 #else
58 qreal dpr = widget->devicePixelRatio();
59 return icon.pixmap(QSize(iconSize, iconSize), dpr);
60 #endif
61 }
62
63 return {};
64 }
65
66 void DlgCheckableMessageBox::showMessage(
67 const QString& header,
68 const QString& message,
69 bool check,
70 const QString& checkText
71 )
72 {
73 bool checked = App::GetApplication()
74 .GetParameterGroupByPath(QByteArray("User parameter:BaseApp/CheckMessages"))
75 ->GetBool(toParamEntry(header));
76
77 if (!checked) {
78 auto mb = new DlgCheckableMessageBox(Gui::getMainWindow());
79
80 mb->setWindowTitle(header);
81 mb->setIconPixmap(getStandardIcon(mb, QStyle::SP_MessageBoxWarning));
82 mb->setText(message);
83 mb->setPrefEntry(header);
84 mb->setCheckBoxText(checkText);
85 mb->setChecked(check);
86 mb->setStandardButtons(QDialogButtonBox::Ok);
87 mb->setDefaultButton(QDialogButtonBox::Ok);
88 mb->show();
89 }
90 }
91
92 void DlgCheckableMessageBox::showMessage(
93 const QString& header,
94 const QString& message,
95 const QString& prefPath,
96 const QString& paramEntry,
97 bool entryDefault,
98 bool check,
99 const QString& checkText
100 )
101 {
102 bool checked = App::GetApplication()
103 .GetParameterGroupByPath(prefPath.toLatin1())
104 ->GetBool(paramEntry.toLatin1(), entryDefault);
105
106 if (checked == entryDefault) {
107 auto mb = new Gui::Dialog::DlgCheckableMessageBox(Gui::getMainWindow());
108
109 mb->setWindowTitle(header);
110 mb->setIconPixmap(getStandardIcon(mb, QStyle::SP_MessageBoxWarning));
111 mb->setText(message);
112 mb->setPrefPath(prefPath);
113 mb->setPrefEntry(paramEntry);
114 mb->setCheckBoxText(checkText);
115 mb->setChecked(check);
116 mb->setStandardButtons(QDialogButtonBox::Ok);
117 mb->setDefaultButton(QDialogButtonBox::Ok);
118 mb->show();
119 }
120 }
121
122 struct DlgCheckableMessageBoxPrivate
123 {
124 DlgCheckableMessageBoxPrivate() = default;
125
126 Ui::DlgCheckableMessageBox ui;
127 QAbstractButton* clickedButton {nullptr};
128 };
129
130 DlgCheckableMessageBox::DlgCheckableMessageBox(QWidget* parent)
131 : QDialog(parent)
132 , m_d(new DlgCheckableMessageBoxPrivate)
133 , prefPath(QLatin1String("User parameter:BaseApp/CheckMessages"))
134 {
135 setModal(true);
136 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
137 m_d->ui.setupUi(this);
138 m_d->ui.pixmapLabel->setVisible(false);
139 connect(m_d->ui.buttonBox, &QDialogButtonBox::accepted, this, &DlgCheckableMessageBox::accept);
140 connect(m_d->ui.buttonBox, &QDialogButtonBox::rejected, this, &DlgCheckableMessageBox::reject);
141 connect(m_d->ui.buttonBox, &QDialogButtonBox::clicked, this, &DlgCheckableMessageBox::slotClicked);
142 }
143
144 DlgCheckableMessageBox::~DlgCheckableMessageBox()
145 {
146 delete m_d;
147 }
148
149 void DlgCheckableMessageBox::setPrefEntry(const QString& entry)
150 {
151 paramEntry = toParamEntry(entry);
152 bool checked
153 = App::GetApplication().GetParameterGroupByPath(prefPath.toLatin1())->GetBool(paramEntry);
154 setChecked(checked);
155 }
156
157 void DlgCheckableMessageBox::setPrefPath(const QString& path)
158 {
159 prefPath = path;
160 }
161
162 void DlgCheckableMessageBox::slotClicked(QAbstractButton* b)
163 {
164 m_d->clickedButton = b;
165 }
166
167 QAbstractButton* DlgCheckableMessageBox::clickedButton() const
168 {
169 return m_d->clickedButton;
170 }
171
172 QDialogButtonBox::StandardButton DlgCheckableMessageBox::clickedStandardButton() const
173 {
174 if (m_d->clickedButton) {
175 return m_d->ui.buttonBox->standardButton(m_d->clickedButton);
176 }
177 return QDialogButtonBox::NoButton;
178 }
179
180 QString DlgCheckableMessageBox::text() const
181 {
182 return m_d->ui.messageLabel->text();
183 }
184
185 void DlgCheckableMessageBox::setText(const QString& t)
186 {
187 m_d->ui.messageLabel->setText(t);
188 }
189
190 QPixmap DlgCheckableMessageBox::iconPixmap() const
191 {
192 return m_d->ui.pixmapLabel->pixmap(Qt::ReturnByValue);
193 }
194
195 void DlgCheckableMessageBox::setIconPixmap(const QPixmap& p)
196 {
197 m_d->ui.pixmapLabel->setPixmap(p);
198 m_d->ui.pixmapLabel->setVisible(!p.isNull());
199 }
200
201 bool DlgCheckableMessageBox::isChecked() const
202 {
203 return m_d->ui.checkBox->isChecked();
204 }
205
206 void DlgCheckableMessageBox::setChecked(bool s)
207 {
208 m_d->ui.checkBox->setChecked(s);
209 }
210
211 QString DlgCheckableMessageBox::checkBoxText() const
212 {
213 return m_d->ui.checkBox->text();
214 }
215
216 void DlgCheckableMessageBox::setCheckBoxText(const QString& t)
217 {
218 m_d->ui.checkBox->setText(t);
219 }
220
221 QDialogButtonBox::StandardButtons DlgCheckableMessageBox::standardButtons() const
222 {
223 return m_d->ui.buttonBox->standardButtons();
224 }
225
226 void DlgCheckableMessageBox::setStandardButtons(QDialogButtonBox::StandardButtons s)
227 {
228 m_d->ui.buttonBox->setStandardButtons(s);
229 }
230
231 QDialogButtonBox::StandardButton DlgCheckableMessageBox::defaultButton() const
232 {
233 Q_FOREACH (QAbstractButton* b, m_d->ui.buttonBox->buttons()) {
234 if (auto pb = qobject_cast<QPushButton*>(b)) {
235 if (pb->isDefault()) {
236 return m_d->ui.buttonBox->standardButton(pb);
237 }
238 }
239 }
240 return QDialogButtonBox::NoButton;
241 }
242
243 void DlgCheckableMessageBox::setDefaultButton(QDialogButtonBox::StandardButton s)
244 {
245 if (QPushButton* b = m_d->ui.buttonBox->button(s)) {
246 b->setDefault(true);
247 b->setFocus();
248 }
249 }
250
251 void DlgCheckableMessageBox::accept()
252 {
253 if (!paramEntry.isEmpty()) {
254 App::GetApplication()
255 .GetParameterGroupByPath(prefPath.toLatin1())
256 ->SetBool(paramEntry, isChecked());
257 }
258 QDialog::accept();
259 }
260
261 void DlgCheckableMessageBox::reject()
262 {
263 if (!paramEntry.isEmpty()) {
264 App::GetApplication()
265 .GetParameterGroupByPath(prefPath.toLatin1())
266 ->SetBool(paramEntry, isChecked());
267 }
268 QDialog::reject();
269 }
270
271 QDialogButtonBox::StandardButton DlgCheckableMessageBox::question(
272 QWidget* parent,
273 const QString& title,
274 const QString& question,
275 const QString& checkBoxText,
276 bool* checkBoxSetting,
277 QDialogButtonBox::StandardButtons buttons,
278 QDialogButtonBox::StandardButton defaultButton
279 )
280 {
281 DlgCheckableMessageBox mb(parent);
282 mb.setWindowTitle(title);
283 mb.setIconPixmap(getStandardIcon(&mb, QStyle::SP_MessageBoxQuestion));
284 mb.setText(question);
285 mb.setCheckBoxText(checkBoxText);
286 mb.setChecked(*checkBoxSetting);
287 mb.setStandardButtons(buttons);
288 mb.setDefaultButton(defaultButton);
289 mb.exec();
290 *checkBoxSetting = mb.isChecked();
291 return mb.clickedStandardButton();
292 }
293
294 QMessageBox::StandardButton DlgCheckableMessageBox::dialogButtonBoxToMessageBoxButton(
295 QDialogButtonBox::StandardButton db
296 )
297 {
298 return static_cast<QMessageBox::StandardButton>(int(db));
299 }
300
301 } // namespace Dialog
302 } // namespace Gui
303
304 #include "moc_DlgCheckableMessageBox.cpp"