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