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 "basewindow.h"
22 #include "code/point.h"
23 #include "code/size.h"
24 #include "code/image.h"
25 
26 #include <QScriptValueIterator>
27 #include <QWidget>
28 #include <QIcon>
29 
30 namespace Code
31 {
BaseWindow()32 	BaseWindow::BaseWindow()
33 		: CodeClass()
34 
35 	{
36 	}
37 
title() const38 	QString BaseWindow::title() const
39 	{
40 		Q_ASSERT(mWindow);
41 
42 		return mWindow->windowTitle();
43 	}
44 
position() const45 	QScriptValue BaseWindow::position() const
46 	{
47 		Q_ASSERT(mWindow);
48 
49 		return Point::constructor(mWindow->pos(), engine());
50 	}
51 
opacity() const52 	float BaseWindow::opacity() const
53 	{
54 		Q_ASSERT(mWindow);
55 
56 		return mWindow->windowOpacity();
57 	}
58 
enabled() const59 	bool BaseWindow::enabled() const
60 	{
61 		Q_ASSERT(mWindow);
62 
63 		return mWindow->isEnabled();
64 	}
65 
visible() const66 	bool BaseWindow::visible() const
67 	{
68 		Q_ASSERT(mWindow);
69 
70 		return mWindow->isVisible();
71 	}
72 
setWidget(QWidget * widget)73 	void BaseWindow::setWidget(QWidget *widget)
74 	{
75 		mWindow = widget;
76 	}
77 
setupConstructorParameters(QScriptContext * context,QScriptEngine * engine,const QScriptValue & parameters)78 	void BaseWindow::setupConstructorParameters(QScriptContext *context, QScriptEngine *engine, const QScriptValue &parameters)
79 	{
80 		Q_ASSERT(mWindow);
81 
82 		mWindow->setWindowTitle(tr("Window"));
83 
84 		QScriptValueIterator it(parameters);
85 
86 		while(it.hasNext())
87 		{
88 			it.next();
89 
90 			if(it.name() == QLatin1String("title"))
91 				mWindow->setWindowTitle(it.value().toString());
92 			else if(it.name() == QLatin1String("position"))
93 			{
94 				QObject *object = it.value().toQObject();
95 				if(auto codePoint = qobject_cast<Point*>(object))
96 					mWindow->move(codePoint->point());
97 				else
98 					throwError(context, engine, QStringLiteral("ParameterTypeError"), tr("Incorrect parameter type"));
99 			}
100 			else if(it.name() == QLatin1String("opacity"))
101 				mWindow->setWindowOpacity(it.value().toNumber());
102 			else if(it.name() == QLatin1String("enabled"))
103 				mWindow->setEnabled(it.value().toBool());
104 			else if(it.name() == QLatin1String("visible"))
105 				mWindow->setVisible(it.value().toBool());
106 			else if(it.name() == QLatin1String("windowIcon"))
107 			{
108 				if(auto icon = qobject_cast<Image*>(it.value().toQObject()))
109 					mWindow->setWindowIcon(QIcon(QPixmap::fromImage(icon->image())));
110 				else
111 					throwError(context, engine, QStringLiteral("ParameterTypeError"), tr("Incorrect parameter type"));
112 			}
113 		}
114 	}
115 
setTitle(const QString & title)116 	QScriptValue BaseWindow::setTitle(const QString &title)
117 	{
118 		Q_ASSERT(mWindow);
119 
120 		mWindow->setWindowTitle(title);
121 
122 		return thisObject();
123 	}
124 
setPosition(const QScriptValue &)125 	QScriptValue BaseWindow::setPosition(const QScriptValue &)
126 	{
127 		Q_ASSERT(mWindow);
128 
129 		mWindow->move(Point::parameter(context(), engine()));
130 
131 		return thisObject();
132 	}
133 
setOpacity(float opacity)134 	QScriptValue BaseWindow::setOpacity(float opacity)
135 	{
136 		Q_ASSERT(mWindow);
137 
138 		mWindow->setWindowOpacity(opacity);
139 
140 		return thisObject();
141 	}
142 
setEnabled(bool enabled)143 	QScriptValue BaseWindow::setEnabled(bool enabled)
144 	{
145 		Q_ASSERT(mWindow);
146 
147 		mWindow->setEnabled(enabled);
148 
149 		return thisObject();
150 	}
151 
setVisible(bool visible)152 	QScriptValue BaseWindow::setVisible(bool visible)
153 	{
154 		Q_ASSERT(mWindow);
155 
156 		mWindow->setVisible(visible);
157 
158 		return thisObject();
159 	}
160 
setWindowIcon(const QScriptValue & windowIcon)161 	QScriptValue BaseWindow::setWindowIcon(const QScriptValue &windowIcon)
162 	{
163 		Q_ASSERT(mWindow);
164 
165 		if(windowIcon.isUndefined() || windowIcon.isNull())
166 		{
167 			mWindow->setWindowIcon(QIcon());
168 
169 			return thisObject();
170 		}
171 
172 		if(auto icon = qobject_cast<Image*>(windowIcon.toQObject()))
173 			mWindow->setWindowIcon(QIcon(QPixmap::fromImage(icon->image())));
174 		else
175 		{
176 			throwError(QStringLiteral("SetWindowIcon"), tr("Invalid image"));
177 			return thisObject();
178 		}
179 
180 		return thisObject();
181 	}
182 
close()183 	QScriptValue BaseWindow::close()
184 	{
185 		Q_ASSERT(mWindow);
186 
187 		mWindow->close();
188 
189         return thisObject();
190     }
191 
equals(const QScriptValue & other) const192     bool BaseWindow::equals(const QScriptValue &other) const
193     {
194         if(other.isUndefined() || other.isNull())
195             return false;
196 
197         QObject *object = other.toQObject();
198         if(auto otherBaseWindow = qobject_cast<BaseWindow*>(object))
199             return (otherBaseWindow == this || otherBaseWindow->mWindow == mWindow);
200 
201         return false;
202     }
203 }
204