1 /*
2 	Actiona
3 	Copyright (C) 2005 Jonathan Mercier-Ganady
4 
5 	Actiona is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 3 of the License, or
8 	(at your option) any later version.
9 
10 	Actiona is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 	Contact : jmgr@jmgr.info
19 */
20 
21 #include "messagebox.h"
22 #include "code/image.h"
23 
24 #include <QScriptValueIterator>
25 #include <QPushButton>
26 
27 namespace Code
28 {
constructor(QScriptContext * context,QScriptEngine * engine)29 	QScriptValue MessageBox::constructor(QScriptContext *context, QScriptEngine *engine)
30 	{
31         auto messageBox = new MessageBox;
32 		messageBox->setupConstructorParameters(context, engine, context->argument(0));
33 
34 		QScriptValueIterator it(context->argument(0));
35 
36 		while(it.hasNext())
37 		{
38 			it.next();
39 
40 			if(it.name() == QLatin1String("text"))
41 				messageBox->mMessageBox->setText(it.value().toString());
42 			else if(it.name() == QLatin1String("detailedText"))
43 				messageBox->mMessageBox->setDetailedText(it.value().toString());
44 			else if(it.name() == QLatin1String("informativeText"))
45 				messageBox->mMessageBox->setInformativeText(it.value().toString());
46 			else if(it.name() == QLatin1String("buttons"))
47 				messageBox->mMessageBox->setStandardButtons(static_cast<QMessageBox::StandardButton>(it.value().toInt32()));
48 			else if(it.name() == QLatin1String("icon"))
49 				messageBox->mMessageBox->setIcon(static_cast<QMessageBox::Icon>(it.value().toInt32()));
50 			else if(it.name() == QLatin1String("defaultButton"))
51 				messageBox->mMessageBox->setDefaultButton(static_cast<QMessageBox::StandardButton>(it.value().toInt32()));
52 			else if(it.name() == QLatin1String("escapeButton"))
53 				messageBox->mMessageBox->setEscapeButton(static_cast<QMessageBox::StandardButton>(it.value().toInt32()));
54 			else if(it.name() == QLatin1String("onClosed"))
55 				messageBox->mOnClosed = it.value();
56 		}
57 
58 		return CodeClass::constructor(messageBox, context, engine);
59 	}
60 
MessageBox()61 	MessageBox::MessageBox()
62 		: BaseWindow(),
63 		mMessageBox(new QMessageBox)
64 	{
65         mMessageBox->setWindowFlags(mMessageBox->windowFlags() | Qt::WindowContextHelpButtonHint);
66 
67 		setWidget(mMessageBox);
68 
69         connect(mMessageBox, &QMessageBox::finished, this, &MessageBox::finished);
70 	}
71 
~MessageBox()72 	MessageBox::~MessageBox()
73 	{
74 		delete mMessageBox;
75 	}
76 
setText(const QString & text)77 	QScriptValue MessageBox::setText(const QString &text)
78 	{
79 		mMessageBox->setText(text);
80 
81 		return thisObject();
82 	}
83 
setDetailedText(const QString & detailedText)84 	QScriptValue MessageBox::setDetailedText(const QString &detailedText)
85 	{
86 		mMessageBox->setDetailedText(detailedText);
87 
88 		return thisObject();
89 	}
90 
setInformativeText(const QString & informativeText)91 	QScriptValue MessageBox::setInformativeText(const QString &informativeText)
92 	{
93 		mMessageBox->setInformativeText(informativeText);
94 
95 		return thisObject();
96 	}
97 
setButtons(StandardButton buttons)98 	QScriptValue MessageBox::setButtons(StandardButton buttons)
99 	{
100 		mMessageBox->setStandardButtons(static_cast<QMessageBox::StandardButton>(buttons));
101 
102 		return thisObject();
103 	}
104 
setIcon(Icon icon)105 	QScriptValue MessageBox::setIcon(Icon icon)
106 	{
107 		mMessageBox->setIcon(static_cast<QMessageBox::Icon>(icon));
108 
109 		return thisObject();
110 	}
111 
setIconPixmap(const QScriptValue & image)112 	QScriptValue MessageBox::setIconPixmap(const QScriptValue &image)
113 	{
114 		if(image.isUndefined() || image.isNull())
115 		{
116 			mMessageBox->setIconPixmap(QPixmap());
117 
118 			return thisObject();
119 		}
120 
121 		QObject *object = image.toQObject();
122 		if(auto otherImage = qobject_cast<Image*>(object))
123 		{
124 			mMessageBox->setIconPixmap(QPixmap::fromImage(otherImage->image()));
125 		}
126 		else
127 		{
128 			throwError(QStringLiteral("SetIconPixmapError"), tr("Invalid image"));
129 			return thisObject();
130 		}
131 
132 		return thisObject();
133 	}
134 
setDefaultButton(StandardButton button)135 	QScriptValue MessageBox::setDefaultButton(StandardButton button)
136 	{
137 		mMessageBox->setDefaultButton(static_cast<QMessageBox::StandardButton>(button));
138 
139 		return thisObject();
140 	}
141 
setEscapeButton(StandardButton button)142 	QScriptValue MessageBox::setEscapeButton(StandardButton button)
143 	{
144 		mMessageBox->setEscapeButton(static_cast<QMessageBox::StandardButton>(button));
145 
146 		return thisObject();
147 	}
148 
addCustomButton(StandardButton button,const QString & text)149 	QScriptValue MessageBox::addCustomButton(StandardButton button, const QString &text)
150 	{
151 		QPushButton *addedButton = mMessageBox->addButton(static_cast<QMessageBox::StandardButton>(button));
152 		if(!addedButton)
153 		{
154 			throwError(QStringLiteral("AddCustomButtonError"), tr("Add custom button failed"));
155 			return thisObject();
156 		}
157 
158 		addedButton->setText(text);
159 
160 		return thisObject();
161 	}
162 
show()163 	QScriptValue MessageBox::show()
164 	{
165 		mMessageBox->open();
166 
167 		return thisObject();
168 	}
169 
showModal()170 	int MessageBox::showModal()
171 	{
172 		return mMessageBox->exec();
173 	}
174 
finished(int result)175 	void MessageBox::finished(int result)
176 	{
177 		if(mOnClosed.isValid())
178 			mOnClosed.call(thisObject(), QScriptValueList() << result);
179 	}
180 }
181