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 "inputdialog.h"
22 
23 #include <QScriptValueIterator>
24 #include <QInputDialog>
25 
26 namespace Code
27 {
constructor(QScriptContext * context,QScriptEngine * engine)28 	QScriptValue InputDialog::constructor(QScriptContext *context, QScriptEngine *engine)
29 	{
30 		auto inputDialog = new InputDialog;
31 		inputDialog->setupConstructorParameters(context, engine, context->argument(0));
32 
33 		QScriptValueIterator it(context->argument(0));
34 
35 		while(it.hasNext())
36 		{
37 			it.next();
38 
39 			if(it.name() == QLatin1String("labelText"))
40 				inputDialog->mInputDialog->setLabelText(it.value().toString());
41 			else if(it.name() == QLatin1String("okButtonText"))
42 				inputDialog->mInputDialog->setOkButtonText(it.value().toString());
43 			else if(it.name() == QLatin1String("cancelButtonText"))
44 				inputDialog->mInputDialog->setCancelButtonText(it.value().toString());
45 			else if(it.name() == QLatin1String("textEchoMode"))
46 				inputDialog->mInputDialog->setTextEchoMode(static_cast<QLineEdit::EchoMode>(it.value().toInt32()));
47 			else if(it.name() == QLatin1String("floatDecimals"))
48 				inputDialog->mInputDialog->setDoubleDecimals(it.value().toInt32());
49 			else if(it.name() == QLatin1String("integerStep"))
50 				inputDialog->mInputDialog->setIntStep(it.value().toInt32());
51 			else if(it.name() == QLatin1String("minimum"))
52 				inputDialog->mMinimum = it.value();
53 			else if(it.name() == QLatin1String("maximum"))
54 				inputDialog->mMaximum = it.value();
55 			else if(it.name() == QLatin1String("range"))
56 			{
57 				inputDialog->mMinimum = it.value().property(QStringLiteral("minimum"));
58 				inputDialog->mMaximum = it.value().property(QStringLiteral("maximum"));
59 			}
60 			else if(it.name() == QLatin1String("inputType"))
61 				inputDialog->mInputType = static_cast<InputType>(it.value().toInt32());
62 			else if(it.name() == QLatin1String("value"))
63 				inputDialog->mValue = it.value();
64 			else if(it.name() == QLatin1String("items"))
65 				inputDialog->mItems = it.value();
66 			else if(it.name() == QLatin1String("itemsEditable"))
67 				inputDialog->mInputDialog->setComboBoxEditable(it.value().toBool());
68 			else if(it.name() == QLatin1String("onClosed"))
69 				inputDialog->mOnClosed = it.value();
70 			else if(it.name() == QLatin1String("onValueChanged"))
71 				inputDialog->mOnValueChanged = it.value();
72 		}
73 
74 		return CodeClass::constructor(inputDialog, context, engine);
75 	}
76 
InputDialog()77 	InputDialog::InputDialog()
78 		: BaseWindow(),
79 
80 		mInputDialog(new QInputDialog)
81 	{
82         mInputDialog->setWindowFlags(mInputDialog->windowFlags() | Qt::WindowContextHelpButtonHint);
83 
84 		setWidget(mInputDialog);
85 
86         connect(mInputDialog, &QInputDialog::finished, this, &InputDialog::finished);
87         connect(mInputDialog, &QInputDialog::doubleValueChanged, this, &InputDialog::doubleValueChanged);
88         connect(mInputDialog, &QInputDialog::intValueChanged, this, &InputDialog::intValueChanged);
89         connect(mInputDialog, &QInputDialog::textValueChanged, this, &InputDialog::textValueChanged);
90 	}
91 
~InputDialog()92 	InputDialog::~InputDialog()
93 	{
94 		delete mInputDialog;
95 	}
96 
value() const97 	QScriptValue InputDialog::value() const
98 	{
99 		switch(mInputType)
100 		{
101 		case Integer:
102 			return engine()->newVariant(mInputDialog->intValue());
103 		case Float:
104 			return engine()->newVariant(mInputDialog->doubleValue());
105 		case Items:
106 		case Text:
107         case MultilineText:
108 		default:
109 			return engine()->newVariant(mInputDialog->textValue());
110 			break;
111 		}
112 	}
113 
setLabelText(const QString & labelText)114 	QScriptValue InputDialog::setLabelText(const QString &labelText)
115 	{
116 		mInputDialog->setLabelText(labelText);
117 
118 		return thisObject();
119 	}
120 
setOkButtonText(const QString & okButtonText)121 	QScriptValue InputDialog::setOkButtonText(const QString &okButtonText)
122 	{
123 		mInputDialog->setOkButtonText(okButtonText);
124 
125 		return thisObject();
126 	}
127 
setCancelButtonText(const QString & cancelButtonText)128 	QScriptValue InputDialog::setCancelButtonText(const QString &cancelButtonText)
129 	{
130 		mInputDialog->setCancelButtonText(cancelButtonText);
131 
132 		return thisObject();
133 	}
134 
setTextEchoMode(TextEchoMode textEchoMode)135 	QScriptValue InputDialog::setTextEchoMode(TextEchoMode textEchoMode)
136 	{
137 		mInputDialog->setTextEchoMode(static_cast<QLineEdit::EchoMode>(textEchoMode));
138 
139 		return thisObject();
140 	}
141 
setFloatDecimals(int decimals)142 	QScriptValue InputDialog::setFloatDecimals(int decimals)
143 	{
144 		mInputDialog->setDoubleDecimals(decimals);
145 
146 		return thisObject();
147 	}
148 
setIntegerStep(int step)149 	QScriptValue InputDialog::setIntegerStep(int step)
150 	{
151 		mInputDialog->setIntStep(step);
152 
153 		return thisObject();
154 	}
155 
setMaximum(const QScriptValue & maximum)156 	QScriptValue InputDialog::setMaximum(const QScriptValue &maximum)
157 	{
158 		mMaximum = maximum;
159 
160 		return thisObject();
161 	}
162 
setMinimum(const QScriptValue & minimum)163 	QScriptValue InputDialog::setMinimum(const QScriptValue &minimum)
164 	{
165 		mMinimum = minimum;
166 
167 		return thisObject();
168 	}
169 
setRange(const QScriptValue & minimum,const QScriptValue & maximum)170 	QScriptValue InputDialog::setRange(const QScriptValue &minimum, const QScriptValue &maximum)
171 	{
172 		mMinimum = minimum;
173 		mMaximum = maximum;
174 
175 		return thisObject();
176 	}
177 
setInputType(InputType inputType)178 	QScriptValue InputDialog::setInputType(InputType inputType)
179 	{
180 		mInputType = inputType;
181 
182 		return thisObject();
183 	}
184 
setValue(const QScriptValue & value)185 	QScriptValue InputDialog::setValue(const QScriptValue &value)
186 	{
187 		mValue = value;
188 
189 		return thisObject();
190 	}
191 
setItems(const QScriptValue & items)192 	QScriptValue InputDialog::setItems(const QScriptValue &items)
193 	{
194 		mItems = items;
195 
196 		return thisObject();
197 	}
198 
setItemsEditable(bool itemsEditable)199 	QScriptValue InputDialog::setItemsEditable(bool itemsEditable)
200 	{
201 		mInputDialog->setComboBoxEditable(itemsEditable);
202 
203 		return thisObject();
204 	}
205 
show()206 	QScriptValue InputDialog::show()
207 	{
208 		setup();
209 
210 		mInputDialog->open();
211 
212 		return thisObject();
213 	}
214 
showModal()215 	int InputDialog::showModal()
216 	{
217 		setup();
218 
219 		return mInputDialog->exec();
220 	}
221 
finished(int result)222 	void InputDialog::finished(int result)
223 	{
224 		if(mOnClosed.isValid())
225 			mOnClosed.call(thisObject(), QScriptValueList() << result);
226 	}
227 
doubleValueChanged(double value)228 	void InputDialog::doubleValueChanged(double value)
229 	{
230 		if(mOnValueChanged.isValid())
231 			mOnValueChanged.call(thisObject(), QScriptValueList() << value);
232 	}
233 
intValueChanged(int value)234 	void InputDialog::intValueChanged(int value)
235 	{
236 		if(mOnValueChanged.isValid())
237 			mOnValueChanged.call(thisObject(), QScriptValueList() << value);
238 	}
239 
textValueChanged(const QString & value)240 	void InputDialog::textValueChanged(const QString &value)
241 	{
242 		if(mOnValueChanged.isValid())
243 			mOnValueChanged.call(thisObject(), QScriptValueList() << value);
244 	}
245 
setup()246 	void InputDialog::setup()
247 	{
248         mInputDialog->setOption(QInputDialog::UsePlainTextEditForTextInput, mInputType == MultilineText);
249 
250 		switch(mInputType)
251 		{
252 		case Integer:
253 			mInputDialog->setInputMode(QInputDialog::IntInput);
254 			mInputDialog->setIntValue(mValue.toInt32());
255 			if(mMinimum.isValid())
256 				mInputDialog->setIntMinimum(mMinimum.toInt32());
257 			if(mMaximum.isValid())
258 				mInputDialog->setIntMaximum(mMaximum.toInt32());
259 			break;
260 		case Float:
261 			mInputDialog->setInputMode(QInputDialog::DoubleInput);
262 			mInputDialog->setDoubleValue(mValue.toNumber());
263 			if(mMinimum.isValid())
264 				mInputDialog->setDoubleMinimum(mMinimum.toNumber());
265 			if(mMaximum.isValid())
266 				mInputDialog->setDoubleMaximum(mMaximum.toNumber());
267 			break;
268 		case Items:
269 			{
270 				QScriptValueIterator it(mItems);
271 				QStringList values;
272 				while(it.hasNext())
273 				{
274 					it.next();
275 
276 					if (it.flags() & QScriptValue::SkipInEnumeration)
277 							 continue;
278 
279 					values << it.value().toString();
280 				}
281 				mInputDialog->setComboBoxItems(values);
282 
283 				mInputDialog->setTextValue(mValue.toString());
284 			}
285 			break;
286         case MultilineText:
287 		case Text:
288 		default:
289 			mInputDialog->setInputMode(QInputDialog::TextInput);
290 			mInputDialog->setTextValue(mValue.toString());
291 			break;
292 		}
293 	}
294 }
295