Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
testobject.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <QtCore/QObject>
8#include <QtCore/QPoint>
9#include <QtCore/QRect>
10#include <QtCore/QSize>
11#include <QtCore/QUrl>
12#include <QtCore/QVariant>
13
14namespace SondeTest {
15
17struct Opaque
18{
19 int value = 0;
20};
21
24{
25 Q_GADGET
26 Q_PROPERTY(double latitude MEMBER latitude)
27 Q_PROPERTY(double longitude MEMBER longitude)
28 Q_PROPERTY(bool isValid READ isValid)
29
30public:
31 double latitude = 0;
32 double longitude = 0;
33
35 bool isValid() const { return latitude >= -90 && latitude <= 90; }
36
38 friend bool operator==(const Coordinate &a, const Coordinate &b)
39 {
40 return a.latitude == b.latitude && a.longitude == b.longitude;
41 }
42
43 friend bool operator!=(const Coordinate &a, const Coordinate &b) { return !(a == b); }
44};
45
47struct Box
48{
49 Q_GADGET
50 Q_PROPERTY(SondeTest::Coordinate corner MEMBER corner)
51 Q_PROPERTY(double size MEMBER size)
52
53public:
55 double size = 0;
56};
57
59struct Chain
60{
61 Q_GADGET
62 Q_PROPERTY(QVariant next READ next)
63
64public:
66 QVariant next() const { return QVariant::fromValue(Chain{ }); }
67};
68
70class TestObject : public QObject
71{
72 Q_OBJECT
73 Q_PROPERTY(QString text MEMBER m_text)
74 Q_PROPERTY(bool visible MEMBER m_visible)
75 Q_PROPERTY(bool enabled MEMBER m_enabled)
76 Q_PROPERTY(int number MEMBER m_number)
77 Q_PROPERTY(double real MEMBER m_real)
78 Q_PROPERTY(QPoint point MEMBER m_point)
79 Q_PROPERTY(QPointF pointF MEMBER m_pointF)
80 Q_PROPERTY(QSize size MEMBER m_size)
81 Q_PROPERTY(QSizeF sizeF MEMBER m_sizeF)
82 Q_PROPERTY(QRect rect MEMBER m_rect)
83 Q_PROPERTY(QRectF rectF MEMBER m_rectF)
84 Q_PROPERTY(QUrl url MEMBER m_url)
85 Q_PROPERTY(QByteArray bytes MEMBER m_bytes)
86 Q_PROPERTY(Mode mode MEMBER m_mode)
87 Q_PROPERTY(Options options READ options WRITE setOptions)
88 Q_PROPERTY(QObject *buddy MEMBER m_buddy)
89 Q_PROPERTY(TestObject *peer MEMBER m_peer)
90 Q_PROPERTY(QVariant anything MEMBER m_anything)
91 Q_PROPERTY(QStringList list MEMBER m_list)
92 Q_PROPERTY(int readOnly READ readOnly CONSTANT)
93 Q_PROPERTY(int rejecting READ rejecting WRITE setRejecting)
94
95public:
97 enum Mode { Idle, Busy, Broken };
98 Q_ENUM(Mode)
99
100
101 enum Option { None = 0, Fast = 1, Safe = 2 };
102 Q_DECLARE_FLAGS(Options, Option)
103 Q_FLAG(Options)
104
105
106 explicit TestObject(QObject *parent = nullptr, const QString &name = { }) : QObject(parent)
107 {
108 setObjectName(name);
109 }
110
112 Options options() const { return m_options; }
114 void setOptions(Options o) { m_options = o; }
115
117 int readOnly() const { return 42; }
119 int rejecting() const { return 0; }
121 void setRejecting(int /*unused*/) { }
122
124 Q_INVOKABLE int add(int a, int b) { return a + b; }
126 Q_INVOKABLE QString describe(const QString &s) { return QStringLiteral("string:") + s; }
128 Q_INVOKABLE void takeOpaque(SondeTest::Opaque /*unused*/) { }
130 Q_INVOKABLE QPoint origin() const { return { 1, 2 }; }
132 Q_INVOKABLE double sum(const SondeTest::Coordinate &c) const
133 {
134 return c.latitude + c.longitude;
135 }
136
137 Q_INVOKABLE QVariantList places() const
138 {
139 return { QVariantMap{
140 { QStringLiteral("name"), QStringLiteral("a") },
141 { QStringLiteral("at"), QVariant::fromValue(Coordinate{ 64, -22 }) } } };
142 }
143
144public Q_SLOTS:
146 void poke()
147 {
148 ++pokes;
149 Q_EMIT poked();
150 }
151
153
154Q_SIGNALS:
156 void poked();
158 void pinged(int count, const QString &note);
160 void pinged();
164 void nudged(int amount = 1);
165
166public:
167 int pokes = 0;
168 QString m_text;
169 bool m_visible = true;
170 bool m_enabled = true;
171 int m_number = 0;
172 double m_real = 0;
173 QPoint m_point;
174 QPointF m_pointF;
175 QSize m_size;
176 QSizeF m_sizeF;
177 QRect m_rect;
178 QRectF m_rectF;
179 QUrl m_url;
180 QByteArray m_bytes;
181 Mode m_mode = Idle;
182 Options m_options;
183 QObject *m_buddy = nullptr;
184 TestObject *m_peer = nullptr;
185 QVariant m_anything;
186 QStringList m_list;
187};
188
189} // namespace SondeTest
190
191Q_DECLARE_METATYPE(SondeTest::Opaque)
192Q_DECLARE_OPERATORS_FOR_FLAGS(SondeTest::TestObject::Options)
TestObject * m_peer
Backing field.
Definition testobject.h:184
Option
Flag enumeration exposed to the meta-object system.
Definition testobject.h:101
void setMode(SondeTest::TestObject::Mode mode)
Slot taking an enum.
Definition testobject.h:152
void takeOpaque(SondeTest::Opaque)
Invokable with an argument that JSON cannot express.
Definition testobject.h:128
QRectF m_rectF
Backing field.
Definition testobject.h:178
QSize m_size
Backing field.
Definition testobject.h:175
QPoint m_point
Backing field.
Definition testobject.h:173
QStringList m_list
Backing field.
Definition testobject.h:186
QPoint origin() const
Invokable that returns a point.
Definition testobject.h:130
QVariant m_anything
Backing field.
Definition testobject.h:185
double sum(const SondeTest::Coordinate &c) const
Invokable taking a gadget: the sum of its latitude and longitude.
Definition testobject.h:132
void poke()
Slot with no arguments that counts calls.
Definition testobject.h:146
int m_number
Backing field.
Definition testobject.h:171
QByteArray m_bytes
Backing field.
Definition testobject.h:180
TestObject(QObject *parent=nullptr, const QString &name={ })
Creates the object with an optional parent and objectName.
Definition testobject.h:106
void sentOpaque(SondeTest::Opaque value)
Emitted by tests, with an argument JSON can't express.
bool m_visible
Backing field.
Definition testobject.h:169
double m_real
Backing field.
Definition testobject.h:172
QObject * m_buddy
Backing field.
Definition testobject.h:183
int add(int a, int b)
Invokable that adds two numbers.
Definition testobject.h:124
QUrl m_url
Backing field.
Definition testobject.h:179
QString describe(const QString &s)
Invokable overloaded on argument type.
Definition testobject.h:126
void setOptions(Options o)
Setter for the flags property.
Definition testobject.h:114
bool m_enabled
Backing field.
Definition testobject.h:170
void pinged()
Overload of pinged() without arguments.
void setRejecting(int)
Setter that ignores its argument.
Definition testobject.h:121
void pinged(int count, const QString &note)
Emitted by tests, with two arguments.
int rejecting() const
Getter of a property whose setter is a no-op.
Definition testobject.h:119
QString m_text
Backing field.
Definition testobject.h:168
QVariantList places() const
Invokable returning a list of maps that hold gadgets, as the viewer's model data does.
Definition testobject.h:137
Mode m_mode
Backing field.
Definition testobject.h:181
QRect m_rect
Backing field.
Definition testobject.h:177
Options m_options
Backing field.
Definition testobject.h:182
Mode
Enumeration exposed to the meta-object system.
Definition testobject.h:97
int pokes
Number of poke() calls.
Definition testobject.h:167
Options options() const
Getter for the flags property.
Definition testobject.h:112
QPointF m_pointF
Backing field.
Definition testobject.h:174
QSizeF m_sizeF
Backing field.
Definition testobject.h:176
void nudged(int amount=1)
Emitted by tests; the default argument makes moc add a clone, nudged().
int readOnly() const
A constant property.
Definition testobject.h:117
void poked()
Emitted by poke().
A gadget holding another gadget, like QGeoRectangle.
Definition testobject.h:48
double size
Length of a side.
Definition testobject.h:55
Coordinate corner
One corner.
Definition testobject.h:54
A gadget whose property holds another of its kind, without end.
Definition testobject.h:60
QVariant next() const
Another chain.
Definition testobject.h:66
A gadget like QGeoCoordinate: two writable properties and a read-only one.
Definition testobject.h:24
friend bool operator==(const Coordinate &a, const Coordinate &b)
Equal when both properties are (MEMBER properties of a gadget need it).
Definition testobject.h:38
friend bool operator!=(const Coordinate &a, const Coordinate &b)
Not equal.
Definition testobject.h:43
double longitude
Degrees east.
Definition testobject.h:32
bool isValid() const
Whether the coordinate is on the earth (a read-only property).
Definition testobject.h:35
double latitude
Degrees north.
Definition testobject.h:31
Opaque value type with no JSON or string conversion.
Definition testobject.h:18
int value
Payload.
Definition testobject.h:19