]> rtime.felk.cvut.cz Git - coffee/qtwebbrowser.git/blob - src/engine.cpp
Factor out utils.h to engine.h and cpp
[coffee/qtwebbrowser.git] / src / engine.cpp
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 #include "engine.h"
39
40 QUrl Engine::fromUserInput(const QString& userInput)
41 {
42     QFileInfo fileInfo(userInput);
43     if (fileInfo.exists())
44         return QUrl::fromLocalFile(fileInfo.absoluteFilePath());
45     return QUrl::fromUserInput(userInput);
46 }
47
48 QString Engine::domainFromString(const QString& urlString)
49 {
50     return QUrl::fromUserInput(urlString).host();
51 }
52
53 QString Engine::randomColor()
54 {
55     QColor color(utils::randomColor(), utils::randomColor(), utils::randomColor());
56     return color.name();
57 }
58
59 QString Engine::colorForIcon(QQuickItemGrabResult *result)
60 {
61     QImage image = result->image();
62     int hue = 0;
63     int saturation = 0;
64     int value = 0;
65     for (int i = 0, width = image.width(); i < width; ++i) {
66         int skip = 0;
67         int h = 0;
68         int s = 0;
69         int v = 0;
70         for (int j = 0, height = image.height(); j < height; ++j) {
71             const QColor color(QColor(image.pixel(i, j)).toHsv());
72             if (color.alpha() < 127) {
73                 ++skip;
74                 continue;
75             }
76
77             h += color.hsvHue();
78             s += color.hsvSaturation();
79             v += color.value();
80         }
81         int count = image.height() - skip + 1;
82         hue = h / count;
83         saturation = s / count;
84         value = v / count;
85     }
86     return QColor::fromHsv(hue, saturation, value).name();
87 }
88
89 QString Engine::oppositeColor(const QString &color)
90 {
91     const QColor c(QColor(color).toHsv());
92     return QColor::fromHsv(c.hue(), c.saturation(), c.value() < 127 ? 255 : c.value() - 100).name();
93 }