File: messagebox.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (180 lines) | stat: -rw-r--r-- 6,475 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "messagebox.h"
#include "acceleratormanager.h"
#include "config.h"
#include "dialog.h"
#include "gtkstyle.h"
#include "icon.h"
#include <QAbstractButton>
#include <QGridLayout>
#include <QLabel>
#include <QListWidget>
#include <QStyle>

MessageBox::ButtonCode map(QMessageBox::StandardButton c)
{
	switch (c) {
	case QMessageBox::Yes: return MessageBox::Yes;
	case QMessageBox::No: return MessageBox::No;
	default:
	case QMessageBox::Cancel: return MessageBox::Cancel;
	}
}

#ifdef Q_OS_MAC
static void splitMessage(const QString& orig, QString& msg, QString& sub)
{
	static QStringList constSeps = QStringList() << QLatin1String("\n\n") << QLatin1String("<br/><br/>");
	msg = orig;

	for (const QString& sep : constSeps) {
		QStringList parts = orig.split(sep);
		if (parts.count() > 1) {
			msg = parts.takeAt(0);
			sub = parts.join(sep);
			return;
		}
	}
}

void MessageBox::error(QWidget* parent, const QString& message, const QString& title)
{
	QString msg;
	QString sub;
	splitMessage(message, msg, sub);
	QMessageBox box(QMessageBox::Critical, title.isEmpty() ? QObject::tr("Error") : title, msg, QMessageBox::Ok, parent, Qt::Sheet);
	box.setInformativeText(sub);
	//AcceleratorManager::manage(&box);
	box.exec();
}

void MessageBox::information(QWidget* parent, const QString& message, const QString& title)
{
	QString msg;
	QString sub;
	splitMessage(message, msg, sub);
	QMessageBox box(QMessageBox::Information, title.isEmpty() ? QObject::tr("Information") : title, msg, QMessageBox::Ok, parent, Qt::Sheet);
	box.setInformativeText(sub);
	//AcceleratorManager::manage(&box);
	box.exec();
}
#endif

MessageBox::ButtonCode MessageBox::questionYesNoCancel(QWidget* parent, const QString& message, const QString& title,
                                                       const GuiItem& yesText, const GuiItem& noText, bool showCancel, bool isWarning)
{
#ifdef Q_OS_MAC
	QString msg;
	QString sub;
	splitMessage(message, msg, sub);
	QMessageBox box(isWarning ? QMessageBox::Warning : QMessageBox::Question, title.isEmpty() ? (isWarning ? QObject::tr("Warning") : QObject::tr("Question")) : title,
	                msg, QMessageBox::Yes | QMessageBox::No | (showCancel ? QMessageBox::Cancel : QMessageBox::NoButton), parent, Qt::Sheet);
	box.setInformativeText(sub);
#else
	QMessageBox box(isWarning ? QMessageBox::Warning : QMessageBox::Question, title.isEmpty() ? (isWarning ? QObject::tr("Warning") : QObject::tr("Question")) : title,
	                message, QMessageBox::Yes | QMessageBox::No | (showCancel ? QMessageBox::Cancel : QMessageBox::NoButton), parent);
#endif

	bool btnIcons = box.style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons);
	box.setDefaultButton(isWarning && !showCancel ? QMessageBox::No : QMessageBox::Yes);
	if (!yesText.text.isEmpty()) {
		QAbstractButton* btn = box.button(QMessageBox::Yes);
		btn->setText(yesText.text);
		btn->setIcon(!yesText.icon.isEmpty() && btnIcons ? Icon::get(yesText.icon) : QIcon());
	}
	if (!noText.text.isEmpty()) {
		QAbstractButton* btn = box.button(QMessageBox::No);
		btn->setText(noText.text);
		btn->setIcon(!noText.icon.isEmpty() && btnIcons ? Icon::get(noText.icon) : QIcon());
	}
	AcceleratorManager::manage(&box);
	return -1 == box.exec() ? Cancel : map(box.standardButton(box.clickedButton()));
}

namespace MessageBox {
class YesNoListDialog : public Dialog {
public:
	YesNoListDialog(QWidget* p) : Dialog(p) {}
	void slotButtonClicked(int b) override
	{
		switch (b) {
		case Dialog::Ok:
		case Dialog::Yes:
			accept();
			break;
		case Dialog::No:
			reject();
			break;
		}
	}
};
}// namespace MessageBox

MessageBox::ButtonCode MessageBox::msgListEx(QWidget* parent, Type type, const QString& message, const QStringList& strlist, const QString& title)
{
	MessageBox::YesNoListDialog* dlg = new MessageBox::YesNoListDialog(parent);
	dlg->setAttribute(Qt::WA_DeleteOnClose);
	QWidget* wid = new QWidget(dlg);
	QGridLayout* lay = new QGridLayout(wid);
	QLabel* iconLabel = new QLabel(wid);
	int iconSize = Icon::dlgIconSize();
	iconLabel->setFixedSize(iconSize, iconSize);
	switch (type) {
	case Error:
		dlg->setCaption(title.isEmpty() ? QObject::tr("Error") : title);
		dlg->setButtons(Dialog::Ok);
		iconLabel->setPixmap(qApp->style()->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(iconSize, iconSize));
		break;
	case Question:
		dlg->setCaption(title.isEmpty() ? QObject::tr("Question") : title);
		dlg->setButtons(Dialog::Yes | Dialog::No);
		iconLabel->setPixmap(qApp->style()->standardIcon(QStyle::SP_MessageBoxQuestion).pixmap(iconSize, iconSize));
		break;
	case Warning:
		dlg->setCaption(title.isEmpty() ? QObject::tr("Warning") : title);
		dlg->setButtons(Dialog::Yes | Dialog::No);
		iconLabel->setPixmap(qApp->style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(iconSize, iconSize));
		break;
	case Information:
		dlg->setCaption(title.isEmpty() ? QObject::tr("Information") : title);
		dlg->setButtons(Dialog::Ok);
		iconLabel->setPixmap(qApp->style()->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(iconSize, iconSize));
		break;
	}
	lay->addWidget(iconLabel, 0, 0, 1, 1);
	QLabel* msgLabel = new QLabel(message, wid);
	msgLabel->setWordWrap(true);
	msgLabel->setTextInteractionFlags(Qt::NoTextInteraction);
	lay->addWidget(msgLabel, 0, 1, 1, 1);
	QListWidget* list = new QListWidget(wid);
	lay->addWidget(list, 1, 0, 1, 2);
	lay->setContentsMargins(0, 0, 0, 0);
	list->insertItems(0, strlist);
	dlg->setMainWidget(wid);
#ifdef Q_OS_MAC
	dlg->setWindowFlags((dlg->windowFlags() & (~Qt::WindowType_Mask)) | Qt::Sheet);
#endif
	return QDialog::Accepted == dlg->exec() ? Yes : No;
}