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
4#include "grid_p.h"
5#include "iconloader_p.h"
6
7#include <QtCore/qlist.h>
8#include <QtCore/qstring.h>
9#include <QtCore/qvariant.h>
10#include <QtCore/qmap.h>
11
12#include <QtGui/qevent.h>
13#include <QtGui/qpainter.h>
14
15#include <QtWidgets/qwidget.h>
16
18
19static const bool defaultSnap = true;
20static const bool defaultVisible = true;
21static const int DEFAULT_GRID = 10;
22static const char* KEY_VISIBLE = "gridVisible";
23static const char* KEY_SNAPX = "gridSnapX";
24static const char* KEY_SNAPY = "gridSnapY";
25static const char* KEY_DELTAX = "gridDeltaX";
26static const char* KEY_DELTAY = "gridDeltaY";
27
28// Insert a value into the serialization map unless default
29template <class T>
30 static inline void valueToVariantMap(T value, T defaultValue, const QString &key, QVariantMap &v, bool forceKey) {
31 if (forceKey || value != defaultValue)
32 v.insert(key, QVariant(value));
33 }
34
35// Obtain a value form QVariantMap
36template <class T>
37 static inline bool valueFromVariantMap(const QVariantMap &v, const QString &key, T &value) {
38 const auto it = v.constFind(key);
39 const bool found = it != v.constEnd();
40 if (found)
41 value = qvariant_cast<T>(it.value());
42 return found;
43 }
44
45namespace qdesigner_internal
46{
47
56
58{
59 Grid grid;
65 if (!anyData)
66 return false;
67 if (grid.m_deltaX == 0 || grid.m_deltaY == 0) {
68 qWarning("Attempt to set invalid grid with a spacing of 0.");
69 return false;
70 }
71 *this = grid;
72 return true;
73}
74
81
90
92{
94 paint(p, widget, e);
95}
96
97void Grid::paint(QPainter &p, const QWidget *widget, QPaintEvent *e) const
98{
99 const auto &palette = widget->palette();
101
102 if (m_visible) {
103 const int xstart = (e->rect().x() / m_deltaX) * m_deltaX;
104 const int ystart = (e->rect().y() / m_deltaY) * m_deltaY;
105
106 const int xend = e->rect().right();
107 const int yend = e->rect().bottom();
108
109 using Points = QList<QPointF>;
110 static Points points;
111 points.clear();
112
113 for (int x = xstart; x <= xend; x += m_deltaX) {
114 points.reserve((yend - ystart) / m_deltaY + 1);
115 for (int y = ystart; y <= yend; y += m_deltaY)
117 p.drawPoints( &(*points.begin()), points.size());
118 points.clear();
119 }
120 }
121}
122
123int Grid::snapValue(int value, int grid) const
124{
125 const int rest = value % grid;
126 const int absRest = (rest < 0) ? -rest : rest;
127 int offset = 0;
128 if (2 * absRest > grid)
129 offset = 1;
130 if (rest < 0)
131 offset *= -1;
132 return (value / grid + offset) * grid;
133}
134
136{
137 const int sx = m_snapX ? snapValue(p.x(), m_deltaX) : p.x();
138 const int sy = m_snapY ? snapValue(p.y(), m_deltaY) : p.y();
139 return QPoint(sx, sy);
140}
141
143{
144 return m_snapX ? (x / m_deltaX) * m_deltaX + 1 : x;
145}
146
148{
149 return m_snapY ? (y / m_deltaY) * m_deltaY + 1 : y;
150}
151
152}
153
154QT_END_NAMESPACE
static const bool defaultVisible
Definition grid.cpp:20
static const char * KEY_DELTAY
Definition grid.cpp:26
static const int DEFAULT_GRID
Definition grid.cpp:21
static const char * KEY_SNAPX
Definition grid.cpp:23
static void valueToVariantMap(T value, T defaultValue, const QString &key, QVariantMap &v, bool forceKey)
Definition grid.cpp:30
static const char * KEY_DELTAX
Definition grid.cpp:25
static QT_BEGIN_NAMESPACE const bool defaultSnap
Definition grid.cpp:19
static const char * KEY_SNAPY
Definition grid.cpp:24
static const char * KEY_VISIBLE
Definition grid.cpp:22
static bool valueFromVariantMap(const QVariantMap &v, const QString &key, T &value)
Definition grid.cpp:37
Auxiliary methods to store/retrieve settings.