1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8                   scmessagebox.cpp  -  replacement for QMessageBox
9                              -------------------
10     begin                : Oct 2005
11     copyright            : (C) 2005 by Craig Bradney
12     email                : cbradney@zip.com.au
13     copyright            : (C) 2014 by William Bader
14     email                : williambader@hotmail.com
15  ***************************************************************************/
16 
17 /***************************************************************************
18  *                                                                         *
19  *   This program is free software; you can redistribute it and/or modify  *
20  *   it under the terms of the GNU General Public License as published by  *
21  *   the Free Software Foundation; either version 2 of the License, or     *
22  *   (at your option) any later version.                                   *
23  *                                                                         *
24  ***************************************************************************/
25 
26 #include <QMessageBox>
27 #include <iostream>
28 
29 #include "scconfig.h"
30 #include "scribuscore.h"
31 #include "ui/scmessagebox.h"
32 
initScMessageBox()33 void ScMessageBox::initScMessageBox()
34 {
35 	messageTitle = "";
36 	defaultBatchPushButton = nullptr;
37 	defaultBatchStandardButton = QMessageBox::NoButton;
38 }
39 
40 // Constructors for the property-based API
41 
ScMessageBox(QWidget * parent)42 ScMessageBox::ScMessageBox(QWidget *parent):
43 			QMessageBox(parent)
44 {
45 	initScMessageBox();
46 }
47 
ScMessageBox(Icon icon,const QString & title,const QString & text,StandardButtons buttons,QWidget * parent,Qt::WindowFlags flags)48 ScMessageBox::ScMessageBox(Icon icon, const QString &title, const QString &text, StandardButtons buttons, QWidget *parent, Qt::WindowFlags flags):
49 			QMessageBox(icon, title, text, buttons, parent, flags)
50 {
51 	initScMessageBox();
52 	messageTitle = title;
53 }
54 
55 // Find a default button in batch mode
56 //	Take the default batch button if present
57 //	Otherwise take the normal default button if present
58 //	Otherwise look for a standard button
59 
findDefaultButton(QMessageBox::StandardButtons buttons,QMessageBox::StandardButton defaultButton,QMessageBox::StandardButton defaultBatchButton)60 QMessageBox::StandardButton ScMessageBox::findDefaultButton(QMessageBox::StandardButtons buttons,
61 				QMessageBox::StandardButton defaultButton, QMessageBox::StandardButton defaultBatchButton)
62 {
63 	QMessageBox::StandardButton result = ((defaultBatchButton != QMessageBox::NoButton)? defaultBatchButton: defaultButton);
64 	if (result == QMessageBox::NoButton)
65 	{
66 		QMessageBox::StandardButton buttonList[] = { QMessageBox::Yes, QMessageBox::Ok,
67 					QMessageBox::Cancel, QMessageBox::Close, QMessageBox::Discard,
68 					QMessageBox::No, QMessageBox::Abort, QMessageBox::Ignore, QMessageBox::NoButton };
69 		for (int i = 0; buttonList[i] != QMessageBox::NoButton; i++)
70 		{
71 			if ((buttons & buttonList[i]) != 0)
72 			{
73 				result = buttonList[i];
74 				break;
75 			}
76 		}
77 	}
78 	return result;
79 }
80 
setDefaultBatchButton(QPushButton * button)81 void ScMessageBox::setDefaultBatchButton(QPushButton *button)
82 {
83 	defaultBatchPushButton = button;
84 }
85 
setDefaultBatchButton(StandardButton button)86 void ScMessageBox::setDefaultBatchButton(StandardButton button)
87 {
88 	defaultBatchStandardButton = button;
89 }
90 
exec()91 int ScMessageBox::exec()
92 {
93 	if (ScCore->usingGUI())
94 		return QMessageBox::exec();
95 	QString msg = messageTitle + text() + " " + informativeText() + " " + detailedText();
96 	std::cerr << msg.toLocal8Bit().data() << std::endl;
97 	return static_cast<int>(findDefaultButton(QMessageBox::standardButtons(), QMessageBox::NoButton, defaultBatchStandardButton));
98 }
99 
clickedButton() const100 QAbstractButton *ScMessageBox::clickedButton() const
101 {
102 	if (ScCore->usingGUI())
103 		return QMessageBox::clickedButton();
104 	return defaultBatchPushButton? defaultBatchPushButton: QMessageBox::defaultButton();
105 }
106 
107 // Static function API
108 //	These functions correspond to the QMessageBox functions
109 //	except that they have an additional optional parameter
110 //	to set the returned button when there is no GUI.
111 
information(QWidget * parent,const QString & title,const QString & text,StandardButtons buttons,StandardButton defaultButton,StandardButton defaultBatchButton)112 QMessageBox::StandardButton ScMessageBox::information(QWidget *parent, const QString &title, const QString &text,
113 				StandardButtons buttons, StandardButton defaultButton, StandardButton defaultBatchButton)
114 {
115 	if (ScCore->usingGUI())
116 		return QMessageBox::information(parent, title, text, buttons, defaultButton);
117 	std::cerr << title.toLocal8Bit().data() << " " << text.toLocal8Bit().data() << std::endl;
118 	return findDefaultButton(buttons, defaultButton, defaultBatchButton);
119 }
120 
question(QWidget * parent,const QString & title,const QString & text,StandardButtons buttons,StandardButton defaultButton,StandardButton defaultBatchButton)121 QMessageBox::StandardButton ScMessageBox::question(QWidget *parent, const QString &title, const QString &text,
122 				StandardButtons buttons, StandardButton defaultButton, StandardButton defaultBatchButton)
123 {
124 	if (ScCore->usingGUI())
125 		return QMessageBox::question(parent, title, text, buttons, defaultButton);
126 	std::cerr << title.toLocal8Bit().data() << " " << text.toLocal8Bit().data() << std::endl;
127 	return findDefaultButton(buttons, defaultButton, defaultBatchButton);
128 }
129 
warning(QWidget * parent,const QString & title,const QString & text,StandardButtons buttons,StandardButton defaultButton,StandardButton defaultBatchButton)130 QMessageBox::StandardButton ScMessageBox::warning(QWidget *parent, const QString &title, const QString &text,
131 				StandardButtons buttons, StandardButton defaultButton, StandardButton defaultBatchButton)
132 {
133 	if (ScCore->usingGUI())
134 		return QMessageBox::warning(parent, title, text, buttons, defaultButton);
135 	std::cerr << title.toLocal8Bit().data() << " " << text.toLocal8Bit().data() << std::endl;
136 	return findDefaultButton(buttons, defaultButton, defaultBatchButton);
137 }
138 
critical(QWidget * parent,const QString & title,const QString & text,StandardButtons buttons,StandardButton defaultButton,StandardButton defaultBatchButton)139 QMessageBox::StandardButton ScMessageBox::critical(QWidget *parent, const QString &title, const QString &text,
140 				StandardButtons buttons, StandardButton defaultButton, StandardButton defaultBatchButton)
141 {
142 	if (ScCore->usingGUI())
143 		return QMessageBox::critical(parent, title, text, buttons, defaultButton);
144 	std::cerr << title.toLocal8Bit().data() << " " << text.toLocal8Bit().data() << std::endl;
145 	return findDefaultButton(buttons, defaultButton, defaultBatchButton);
146 }
147 
about(QWidget * parent,const QString & title,const QString & text)148 void ScMessageBox::about(QWidget *parent, const QString &title, const QString &text)
149 {
150 	if (ScCore->usingGUI())
151 	{
152 		QMessageBox::about(parent, title, text);
153 		return;
154 	}
155 	std::cerr << title.toLocal8Bit().data() << " " << text.toLocal8Bit().data() << std::endl;
156 }
157 
aboutQt(QWidget * parent,const QString & title)158 void ScMessageBox::aboutQt(QWidget *parent, const QString &title)
159 {
160 	if (ScCore->usingGUI())
161 	{
162 		QMessageBox::aboutQt(parent, title);
163 		return;
164 	}
165 	std::cerr << title.toLocal8Bit().data() << std::endl;
166 }
167