1 /*
2     Copyright (C) 2014 Aseman
3     http://aseman.co
4 
5     This project 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     This project 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 
19 #include "asemanqttools.h"
20 #include "asemanquickview.h"
21 #include "asemandesktoptools.h"
22 #include "asemanqtlogger.h"
23 #include "asemandevices.h"
24 #include "asemantools.h"
25 #include "asemanapplication.h"
26 #include "asemanhashobject.h"
27 #include "asemanlistobject.h"
28 #include "asemancalendarconverter.h"
29 #include "asemanimagecoloranalizor.h"
30 #include "asemanmimedata.h"
31 #include "asemandragobject.h"
32 #include "asemandownloader.h"
33 #include "asemanfilesystemmodel.h"
34 #include "asemanbackhandler.h"
35 #include "asemanquickobject.h"
36 #include "asemancountriesmodel.h"
37 #include "asemanquickitemimagegrabber.h"
38 #include "asemanfiledownloaderqueueitem.h"
39 #include "asemanfiledownloaderqueue.h"
40 #include "asemannotification.h"
41 #include "asemanautostartmanager.h"
42 #include "asemanmimeapps.h"
43 #include "asemanwebpagegrabber.h"
44 #include "asemantitlebarcolorgrabber.h"
45 #include "asemantaskbarbutton.h"
46 #include "asemanmapdownloader.h"
47 #include "asemandragarea.h"
48 #ifdef Q_OS_ANDROID
49 #include "asemanjavalayer.h"
50 #endif
51 #ifdef ASEMAN_SENSORS
52 #include "asemansensors.h"
53 #endif
54 #ifdef ASEMAN_MULTIMEDIA
55 #include "asemanaudiorecorder.h"
56 #include "asemanaudioencodersettings.h"
57 #endif
58 #if defined(Q_OS_LINUX) && defined(QT_DBUS_LIB)
59 #include "asemankdewallet.h"
60 #endif
61 
62 #include <QPointer>
63 #include <QSharedPointer>
64 #include <QtQml>
65 #include <QQmlEngine>
66 #include <QQmlContext>
67 #include <QQuickItem>
68 #include <QScreen>
69 
70 class AsemanQuickViewPrivate
71 {
72 public:
73     QPointer<QObject> root;
74     QPointer<QQuickItem> focused_text;
75 
76     bool tryClose;
77     bool fullscreen;
78     bool backController;
79     int layoutDirection;
80     bool reverseScroll;
81 
82 #ifdef ASEMAN_QML_PLUGIN
83     QQmlEngine *engine;
84 #endif
85 };
86 
87 #ifdef ASEMAN_QML_PLUGIN
AsemanQuickView(QQmlEngine * engine,QObject * parent)88 AsemanQuickView::AsemanQuickView(QQmlEngine *engine, QObject *parent ) :
89 #else
90 AsemanQuickView::AsemanQuickView(QWindow *parent) :
91 #endif
92     INHERIT_VIEW(parent)
93 {
94     p = new AsemanQuickViewPrivate;
95 #ifdef ASEMAN_QML_PLUGIN
96     p->engine = engine;
97 #endif
98     p->fullscreen = false;
99     p->backController = false;
100     p->layoutDirection = Qt::LeftToRight;
101     p->tryClose  = false;
102     p->reverseScroll = false;
103 
104 #ifndef ASEMAN_QML_PLUGIN
105     AsemanQtTools::registerTypes("AsemanTools");
106     setResizeMode(QQuickView::SizeRootObjectToView);
107     engine()->setImportPathList( QStringList()<< engine()->importPathList() << "qrc:///asemantools/qml" );
108 #endif
109 }
110 
desktopTools() const111 AsemanDesktopTools *AsemanQuickView::desktopTools() const
112 {
113     return AsemanQtTools::desktopTools();
114 }
115 
devices() const116 AsemanDevices *AsemanQuickView::devices() const
117 {
118     return AsemanQtTools::devices();
119 }
120 
qtLogger() const121 AsemanQtLogger *AsemanQuickView::qtLogger() const
122 {
123     return AsemanQtTools::qtLogger();
124 }
125 
tools() const126 AsemanTools *AsemanQuickView::tools() const
127 {
128     return AsemanQtTools::tools();
129 }
130 
131 #ifdef Q_OS_ANDROID
javaLayer() const132 AsemanJavaLayer *AsemanQuickView::javaLayer() const
133 {
134     return AsemanJavaLayer::instance();
135 }
136 #endif
137 
calendar() const138 AsemanCalendarConverter *AsemanQuickView::calendar() const
139 {
140 #ifdef ASEMAN_QML_PLUGIN
141     return AsemanQtTools::calendar(p->engine);
142 #else
143     return AsemanQtTools::calendar(engine());
144 #endif
145 }
146 
backHandler() const147 AsemanBackHandler *AsemanQuickView::backHandler() const
148 {
149 #ifdef ASEMAN_QML_PLUGIN
150     return AsemanQtTools::backHandler(p->engine);
151 #else
152     return AsemanQtTools::backHandler(engine());
153 #endif
154 }
155 
setFullscreen(bool stt)156 void AsemanQuickView::setFullscreen(bool stt)
157 {
158     if( p->fullscreen == stt )
159         return;
160 
161     p->fullscreen = stt;
162 #ifndef ASEMAN_QML_PLUGIN
163     if( p->fullscreen )
164         showFullScreen();
165     else
166         showNormal();
167 #endif
168 
169     emit fullscreenChanged();
170     emit navigationBarHeightChanged();
171     emit statusBarHeightChanged();
172 }
173 
fullscreen() const174 bool AsemanQuickView::fullscreen() const
175 {
176     return p->fullscreen;
177 }
178 
setBackController(bool stt)179 void AsemanQuickView::setBackController(bool stt)
180 {
181     if(p->backController == stt)
182         return;
183 
184     p->backController = stt;
185     emit backControllerChanged();
186 }
187 
backController() const188 bool AsemanQuickView::backController() const
189 {
190     return p->backController;
191 }
192 
setReverseScroll(bool stt)193 void AsemanQuickView::setReverseScroll(bool stt)
194 {
195     if(p->reverseScroll == stt)
196         return;
197 
198     p->reverseScroll = stt;
199     emit reverseScrollChanged();
200 }
201 
reverseScroll() const202 bool AsemanQuickView::reverseScroll() const
203 {
204     return p->reverseScroll;
205 }
206 
statusBarHeight() const207 qreal AsemanQuickView::statusBarHeight() const
208 {
209     AsemanDevices *dv = devices();
210     return dv->transparentStatusBar() && !fullscreen()? 24*dv->density() : 0;
211 }
212 
navigationBarHeight() const213 qreal AsemanQuickView::navigationBarHeight() const
214 {
215     AsemanDevices *dv = devices();
216     return dv->transparentNavigationBar() && !fullscreen()? 44*dv->density() : 0;
217 }
218 
setRoot(QObject * root)219 void AsemanQuickView::setRoot(QObject *root)
220 {
221     if( p->root == root )
222         return;
223 
224     p->root = root;
225     emit rootChanged();
226 }
227 
root() const228 QObject *AsemanQuickView::root() const
229 {
230     if( p->root )
231         return p->root;
232 
233     return p->root;
234 }
235 
setFocusedText(QQuickItem * item)236 void AsemanQuickView::setFocusedText(QQuickItem *item)
237 {
238     if( p->focused_text == item )
239         return;
240     if( p->focused_text )
241         disconnect( p->focused_text, SIGNAL(destroyed()), this, SIGNAL(focusedTextChanged()) );
242 
243     p->focused_text = item;
244     if( item )
245     {
246         connect( item, SIGNAL(destroyed()), this, SIGNAL(focusedTextChanged()) );
247         devices()->showKeyboard();
248     }
249     else
250     {
251         devices()->hideKeyboard();
252     }
253 
254     emit focusedTextChanged();
255 }
256 
focusedText() const257 QQuickItem *AsemanQuickView::focusedText() const
258 {
259     return p->focused_text;
260 }
261 
layoutDirection() const262 int AsemanQuickView::layoutDirection() const
263 {
264     return p->layoutDirection;
265 }
266 
setLayoutDirection(int l)267 void AsemanQuickView::setLayoutDirection(int l)
268 {
269     if( l == p->layoutDirection )
270         return;
271 
272     p->layoutDirection = l;
273     emit layoutDirectionChanged();
274 }
275 
flickVelocity() const276 qreal AsemanQuickView::flickVelocity() const
277 {
278 #ifdef DESKTOP_DEVICE
279     return 2500;
280 #else
281     return 25000;
282 #endif
283 }
284 
screenSize() const285 QSize AsemanQuickView::screenSize() const
286 {
287     QSize result;
288 #ifndef ASEMAN_QML_PLUGIN
289     if(screen())
290         result = screen()->size();
291 #endif
292     return result;
293 }
294 
discardFocusedText()295 void AsemanQuickView::discardFocusedText()
296 {
297     setFocusedText(0);
298 }
299 
tryClose()300 void AsemanQuickView::tryClose()
301 {
302     p->tryClose = true;
303 #ifndef ASEMAN_QML_PLUGIN
304     close();
305 #endif
306 }
307 
setMask(qreal x,qreal y,qreal width,qreal height)308 void AsemanQuickView::setMask(qreal x, qreal y, qreal width, qreal height)
309 {
310 #ifndef ASEMAN_QML_PLUGIN
311     QQuickView::setMask(QRegion(x,y,width,height));
312 #endif
313 }
314 
move(qreal x,qreal y)315 void AsemanQuickView::move(qreal x, qreal y)
316 {
317 #ifndef ASEMAN_QML_PLUGIN
318     QQuickView::setPosition(x, y);
319 #endif
320 }
321 
resize(qreal w,qreal h)322 void AsemanQuickView::resize(qreal w, qreal h)
323 {
324 #ifndef ASEMAN_QML_PLUGIN
325     QQuickView::resize(QSize(w,h));
326 #endif
327 }
328 
event(QEvent * e)329 bool AsemanQuickView::event(QEvent *e)
330 {
331     switch( static_cast<int>(e->type()) )
332     {
333     case QEvent::Close:
334         if(p->backController)
335         {
336             QCloseEvent *ce = static_cast<QCloseEvent*>(e);
337             if( p->tryClose || devices()->isDesktop() )
338                 ce->accept();
339             else
340             {
341                 ce->ignore();
342                 emit closeRequest();
343             }
344         }
345         break;
346     }
347 
348     return INHERIT_VIEW::event(e);
349 }
350 
~AsemanQuickView()351 AsemanQuickView::~AsemanQuickView()
352 {
353     delete p;
354 }
355