1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the Qt Designer of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "grid_p.h"
|
---|
43 |
|
---|
44 | #include <QtCore/QString>
|
---|
45 | #include <QtCore/QVector>
|
---|
46 | #include <QtGui/QPainter>
|
---|
47 | #include <QtGui/QWidget>
|
---|
48 | #include <QtGui/qevent.h>
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | static const bool defaultSnap = true;
|
---|
53 | static const bool defaultVisible = true;
|
---|
54 | static const int DEFAULT_GRID = 10;
|
---|
55 | static const char* KEY_VISIBLE = "gridVisible";
|
---|
56 | static const char* KEY_SNAPX = "gridSnapX";
|
---|
57 | static const char* KEY_SNAPY = "gridSnapY";
|
---|
58 | static const char* KEY_DELTAX = "gridDeltaX";
|
---|
59 | static const char* KEY_DELTAY = "gridDeltaY";
|
---|
60 |
|
---|
61 | // Insert a value into the serialization map unless default
|
---|
62 | template <class T>
|
---|
63 | static inline void valueToVariantMap(T value, T defaultValue, const QString &key, QVariantMap &v, bool forceKey) {
|
---|
64 | if (forceKey || value != defaultValue)
|
---|
65 | v.insert(key, QVariant(value));
|
---|
66 | }
|
---|
67 |
|
---|
68 | // Obtain a value form QVariantMap
|
---|
69 | template <class T>
|
---|
70 | static inline bool valueFromVariantMap(const QVariantMap &v, const QString &key, T &value) {
|
---|
71 | const QVariantMap::const_iterator it = v.constFind(key);
|
---|
72 | const bool found = it != v.constEnd();
|
---|
73 | if (found)
|
---|
74 | value = qVariantValue<T>(it.value());
|
---|
75 | return found;
|
---|
76 | }
|
---|
77 |
|
---|
78 | namespace qdesigner_internal
|
---|
79 | {
|
---|
80 |
|
---|
81 | Grid::Grid() :
|
---|
82 | m_visible(defaultVisible),
|
---|
83 | m_snapX(defaultSnap),
|
---|
84 | m_snapY(defaultSnap),
|
---|
85 | m_deltaX(DEFAULT_GRID),
|
---|
86 | m_deltaY(DEFAULT_GRID)
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | bool Grid::fromVariantMap(const QVariantMap& vm)
|
---|
91 | {
|
---|
92 | *this = Grid();
|
---|
93 | valueFromVariantMap(vm, QLatin1String(KEY_VISIBLE), m_visible);
|
---|
94 | valueFromVariantMap(vm, QLatin1String(KEY_SNAPX), m_snapX);
|
---|
95 | valueFromVariantMap(vm, QLatin1String(KEY_SNAPY), m_snapY);
|
---|
96 | valueFromVariantMap(vm, QLatin1String(KEY_DELTAX), m_deltaX);
|
---|
97 | return valueFromVariantMap(vm, QLatin1String(KEY_DELTAY), m_deltaY);
|
---|
98 | }
|
---|
99 |
|
---|
100 | QVariantMap Grid::toVariantMap(bool forceKeys) const
|
---|
101 | {
|
---|
102 | QVariantMap rc;
|
---|
103 | addToVariantMap(rc, forceKeys);
|
---|
104 | return rc;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void Grid::addToVariantMap(QVariantMap& vm, bool forceKeys) const
|
---|
108 | {
|
---|
109 | valueToVariantMap(m_visible, defaultVisible, QLatin1String(KEY_VISIBLE), vm, forceKeys);
|
---|
110 | valueToVariantMap(m_snapX, defaultSnap, QLatin1String(KEY_SNAPX), vm, forceKeys);
|
---|
111 | valueToVariantMap(m_snapY, defaultSnap, QLatin1String(KEY_SNAPY), vm, forceKeys);
|
---|
112 | valueToVariantMap(m_deltaX, DEFAULT_GRID, QLatin1String(KEY_DELTAX), vm, forceKeys);
|
---|
113 | valueToVariantMap(m_deltaY, DEFAULT_GRID, QLatin1String(KEY_DELTAY), vm, forceKeys);
|
---|
114 | }
|
---|
115 |
|
---|
116 | void Grid::paint(QWidget *widget, QPaintEvent *e) const
|
---|
117 | {
|
---|
118 | QPainter p(widget);
|
---|
119 | paint(p, widget, e);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void Grid::paint(QPainter &p, const QWidget *widget, QPaintEvent *e) const
|
---|
123 | {
|
---|
124 | p.setPen(widget->palette().dark().color());
|
---|
125 |
|
---|
126 | if (m_visible) {
|
---|
127 | const int xstart = (e->rect().x() / m_deltaX) * m_deltaX;
|
---|
128 | const int ystart = (e->rect().y() / m_deltaY) * m_deltaY;
|
---|
129 |
|
---|
130 | const int xend = e->rect().right();
|
---|
131 | const int yend = e->rect().bottom();
|
---|
132 |
|
---|
133 | typedef QVector<QPointF> Points;
|
---|
134 | static Points points;
|
---|
135 | points.clear();
|
---|
136 |
|
---|
137 | for (int x = xstart; x <= xend; x += m_deltaX) {
|
---|
138 | points.reserve((yend - ystart) / m_deltaY + 1);
|
---|
139 | for (int y = ystart; y <= yend; y += m_deltaY)
|
---|
140 | points.push_back(QPointF(x, y));
|
---|
141 | p.drawPoints( &(*points.begin()), points.count());
|
---|
142 | points.clear();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | int Grid::snapValue(int value, int grid) const
|
---|
148 | {
|
---|
149 | const int rest = value % grid;
|
---|
150 | const int absRest = (rest < 0) ? -rest : rest;
|
---|
151 | int offset = 0;
|
---|
152 | if (2 * absRest > grid)
|
---|
153 | offset = 1;
|
---|
154 | if (rest < 0)
|
---|
155 | offset *= -1;
|
---|
156 | return (value / grid + offset) * grid;
|
---|
157 | }
|
---|
158 |
|
---|
159 | QPoint Grid::snapPoint(const QPoint &p) const
|
---|
160 | {
|
---|
161 | const int sx = m_snapX ? snapValue(p.x(), m_deltaX) : p.x();
|
---|
162 | const int sy = m_snapY ? snapValue(p.y(), m_deltaY) : p.y();
|
---|
163 | return QPoint(sx, sy);
|
---|
164 | }
|
---|
165 |
|
---|
166 | int Grid::widgetHandleAdjustX(int x) const
|
---|
167 | {
|
---|
168 | return m_snapX ? (x / m_deltaX) * m_deltaX + 1 : x;
|
---|
169 | }
|
---|
170 |
|
---|
171 | int Grid::widgetHandleAdjustY(int y) const
|
---|
172 | {
|
---|
173 | return m_snapY ? (y / m_deltaY) * m_deltaY + 1 : y;
|
---|
174 | }
|
---|
175 |
|
---|
176 | bool Grid::equals(const Grid &rhs) const
|
---|
177 | {
|
---|
178 | return m_visible == rhs.m_visible &&
|
---|
179 | m_snapX == rhs.m_snapX &&
|
---|
180 | m_snapY == rhs.m_snapY &&
|
---|
181 | m_deltaX == rhs.m_deltaX &&
|
---|
182 | m_deltaY == rhs.m_deltaY;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | QT_END_NAMESPACE
|
---|