Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
grid.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant reason:default
4
5#include "grid_p.h"
6#include "iconloader_p.h"
7
8#include <QtCore/qlist.h>
9#include <QtCore/qstring.h>
10#include <QtCore/qvariant.h>
11#include <QtCore/qmap.h>
12
13#include <QtGui/qevent.h>
14#include <QtGui/qpainter.h>
15
16#include <QtWidgets/qwidget.h>
17
19
20static const bool defaultSnap = true;
21static const bool defaultVisible = true;
22static const int DEFAULT_GRID = 10;
23static const char* KEY_VISIBLE = "gridVisible";
24static const char* KEY_SNAPX = "gridSnapX";
25static const char* KEY_SNAPY = "gridSnapY";
26static const char* KEY_DELTAX = "gridDeltaX";
27static const char* KEY_DELTAY = "gridDeltaY";
28
29// Insert a value into the serialization map unless default
30template <class T>
31 static inline void valueToVariantMap(T value, T defaultValue, const QString &key, QVariantMap &v, bool forceKey) {
32 if (forceKey || value != defaultValue)
33 v.insert(key, QVariant(value));
34 }
35
36// Obtain a value form QVariantMap
37template <class T>
38 static inline bool valueFromVariantMap(const QVariantMap &v, const QString &key, T &value) {
39 const auto it = v.constFind(key);
40 const bool found = it != v.constEnd();
41 if (found)
42 value = qvariant_cast<T>(it.value());
43 return found;
44 }
45
46namespace qdesigner_internal
47{
48
57
59{
60 Grid grid;
66 if (!anyData)
67 return false;
68 if (grid.m_deltaX == 0 || grid.m_deltaY == 0) {
69 qWarning("Attempt to set invalid grid with a spacing of 0.");
70 return false;
71 }
72 *this = grid;
73 return true;
74}
75
82
91
93{
95 paint(p, widget, e);
96}
97
98void Grid::paint(QPainter &p, const QWidget *widget, QPaintEvent *e) const
99{
100 const auto &palette = widget->palette();
102
103 if (m_visible) {
104 const int xstart = (e->rect().x() / m_deltaX) * m_deltaX;
105 const int ystart = (e->rect().y() / m_deltaY) * m_deltaY;
106
107 const int xend = e->rect().right();
108 const int yend = e->rect().bottom();
109
110 using Points = QList<QPointF>;
111 static Points points;
112 points.clear();
113
114 for (int x = xstart; x <= xend; x += m_deltaX) {
115 points.reserve((yend - ystart) / m_deltaY + 1);
116 for (int y = ystart; y <= yend; y += m_deltaY)
118 p.drawPoints( &(*points.begin()), points.size());
119 points.clear();
120 }
121 }
122}
123
124int Grid::snapValue(int value, int grid) const
125{
126 const int rest = value % grid;
127 const int absRest = (rest < 0) ? -rest : rest;
128 int offset = 0;
129 if (2 * absRest > grid)
130 offset = 1;
131 if (rest < 0)
132 offset *= -1;
133 return (value / grid + offset) * grid;
134}
135
137{
138 const int sx = m_snapX ? snapValue(p.x(), m_deltaX) : p.x();
139 const int sy = m_snapY ? snapValue(p.y(), m_deltaY) : p.y();
140 return QPoint(sx, sy);
141}
142
144{
145 return m_snapX ? (x / m_deltaX) * m_deltaX + 1 : x;
146}
147
149{
150 return m_snapY ? (y / m_deltaY) * m_deltaY + 1 : y;
151}
152
153}
154
155QT_END_NAMESPACE
static const bool defaultVisible
Definition grid.cpp:21
static const char * KEY_DELTAY
Definition grid.cpp:27
static const int DEFAULT_GRID
Definition grid.cpp:22
static const char * KEY_SNAPX
Definition grid.cpp:24
static void valueToVariantMap(T value, T defaultValue, const QString &key, QVariantMap &v, bool forceKey)
Definition grid.cpp:31
static const char * KEY_DELTAX
Definition grid.cpp:26
static QT_BEGIN_NAMESPACE const bool defaultSnap
Definition grid.cpp:20
static const char * KEY_SNAPY
Definition grid.cpp:25
static const char * KEY_VISIBLE
Definition grid.cpp:23
static bool valueFromVariantMap(const QVariantMap &v, const QString &key, T &value)
Definition grid.cpp:38
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.