]> rtime.felk.cvut.cz Git - coffee/qtwebbrowser.git/blob - src/utils.h
Improve home screen
[coffee/qtwebbrowser.git] / src / utils.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtBrowser project.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 2 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.GPLv2 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU General Public License version 2 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3.0 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 3.0 requirements will be
31 ** met: http://www.gnu.org/copyleft/gpl.html.
32 **
33 **
34 ** $QT_END_LICENSE$
35 **
36 ****************************************************************************/
37
38 #ifndef UTILS_H
39 #define UTILS_H
40
41 #include <QtCore/QEvent>
42 #include <QtCore/QFileInfo>
43 #include <QtCore/QUrl>
44 #include <QtGui/QColor>
45 #include <QtQuick/QQuickItemGrabResult>
46
47 namespace utils {
48 inline bool isTouchEvent(const QEvent* event)
49 {
50     switch (event->type()) {
51     case QEvent::TouchBegin:
52     case QEvent::TouchUpdate:
53     case QEvent::TouchEnd:
54         return true;
55     default:
56         return false;
57     }
58 }
59
60 inline bool isMouseEvent(const QEvent* event)
61 {
62     switch (event->type()) {
63     case QEvent::MouseButtonPress:
64     case QEvent::MouseMove:
65     case QEvent::MouseButtonRelease:
66     case QEvent::MouseButtonDblClick:
67         return true;
68     default:
69         return false;
70     }
71 }
72
73 inline int randomColor()
74 {
75     return qrand() % 255;
76 }
77
78 }
79
80 class Utils : public QObject {
81     Q_OBJECT
82
83     Q_PROPERTY(QObject * rootWindow READ rootWindow FINAL CONSTANT)
84
85 public:
86     Utils(QObject *parent)
87         : QObject(parent)
88     {
89         qsrand(255);
90     }
91     QObject *rootWindow()
92     {
93         return parent();
94     }
95
96     Q_INVOKABLE static QUrl fromUserInput(const QString& userInput);
97     Q_INVOKABLE static QString domainFromString(const QString& urlString);
98     Q_INVOKABLE static QString randomColor();
99     Q_INVOKABLE static QString colorForIcon(QQuickItemGrabResult *result);
100     Q_INVOKABLE static QString oppositeColor(const QString & color);
101 };
102
103 inline QUrl Utils::fromUserInput(const QString& userInput)
104 {
105     QFileInfo fileInfo(userInput);
106     if (fileInfo.exists())
107         return QUrl::fromLocalFile(fileInfo.absoluteFilePath());
108     return QUrl::fromUserInput(userInput);
109 }
110
111 inline QString Utils::domainFromString(const QString& urlString)
112 {
113     return QUrl::fromUserInput(urlString).host();
114 }
115
116 inline QString Utils::randomColor()
117 {
118     QColor color(utils::randomColor(), utils::randomColor(), utils::randomColor());
119     return color.name();
120 }
121
122 inline QString Utils::colorForIcon(QQuickItemGrabResult *result)
123 {
124     QImage image = result->image();
125     int hue = 0;
126     int saturation = 0;
127     int value = 0;
128     for (int i = 0, width = image.width(); i < width; ++i) {
129         int skip = 0;
130         int h = 0;
131         int s = 0;
132         int v = 0;
133         for (int j = 0, height = image.height(); j < height; ++j) {
134             const QColor color(QColor(image.pixel(i, j)).toHsv());
135             if (color.alpha() < 127) {
136                 ++skip;
137                 continue;
138             }
139
140             h += color.hsvHue();
141             s += color.hsvSaturation();
142             v += color.value();
143         }
144         int count = image.height() - skip + 1;
145         hue = h / count;
146         saturation = s / count;
147         value = v / count;
148     }
149     return QColor::fromHsv(hue, saturation, value).name();
150 }
151
152 inline QString Utils::oppositeColor(const QString &color)
153 {
154     const QColor c(QColor(color).toHsv());
155     return QColor::fromHsv(c.hue(), c.saturation(), c.value() < 127 ? 255 : c.value() - 100).name();
156 }
157
158 #endif // UTILS_H