Itasca C++ Interface
Loading...
Searching...
No Matches
basetoqt.h
Go to the documentation of this file.
1#pragma once
8#include "avect.h"
9#include "base.h"
10#include "property.h"
11#include "basei.h"
12#ifdef _WIN32
13#include <codeanalysis\warnings.h>
14#endif
15
16PUSHWARNING
17VSWARNING(ALL_CODE_ANALYSIS_WARNINGS)
18VSWARNING(4251)
19VSWARNING(4244)
20VSWARNING(4127)
21CLANGWARNING("-Wlanguage-extension-token")
22CLANGWARNING("-Wmicrosoft-enum-value")
23//INTELLLVMWARNING("-Wmicrosoft-unqualified-friend")
24INTELWARNING(1744) // DLL Interface
25INTELWARNING(3280) // Declaration hides member
26namespace Qt {
27 class weak_ordering;
28 class strong_ordering;
29}
30#include <QtCore>
31POPWARNING
32
33#undef rad1
34#undef rad2
35#undef small
36#undef near
37#undef far
38#undef grp1
39#undef grp2
40
41#if QT_VERSION > 0x060000
42# define QT6
43#elif QT_VERSION > 0x050000
44# define QT5
45#endif
46
47#ifndef DOXYGEN
48// The following are commented since they should not be used with Qt anyway
49//Q_DECLARE_METATYPE(quint64);
50Q_DECLARE_METATYPE(string)
51//Q_DECLARE_METATYPE(String);
52Q_DECLARE_METATYPE(Variant)
53Q_DECLARE_METATYPE(IVect2)
54Q_DECLARE_METATYPE(FVect2)
55Q_DECLARE_METATYPE(DVect2)
56Q_DECLARE_METATYPE(UVect2)
57Q_DECLARE_METATYPE(DAVect2)
58Q_DECLARE_METATYPE(FAVect2)
59Q_DECLARE_METATYPE(IAVect2)
60Q_DECLARE_METATYPE(UAVect2)
61Q_DECLARE_METATYPE(IVect3)
62Q_DECLARE_METATYPE(FVect3)
63Q_DECLARE_METATYPE(DVect3)
64Q_DECLARE_METATYPE(UVect3)
65Q_DECLARE_METATYPE(DAVect3)
66Q_DECLARE_METATYPE(FAVect3)
67Q_DECLARE_METATYPE(IAVect3)
68Q_DECLARE_METATYPE(UAVect3)
69Q_DECLARE_METATYPE(Orientation2)
70Q_DECLARE_METATYPE(Orientation3)
71Q_DECLARE_METATYPE(Axes2D)
72Q_DECLARE_METATYPE(Axes3D)
73Q_DECLARE_METATYPE(Quat2)
74Q_DECLARE_METATYPE(Quat3)
75Q_DECLARE_METATYPE(SymTensor)
76Q_DECLARE_METATYPE(itasca::Mat)
77#endif
78//#ifndef __GNUC__
79//#pragma warning(pop)
80//#endif
81
82// Ensure always utf-8
83class ItascaTextStream : public QTextStream {
84public:
85 BASE_EXPORT ItascaTextStream() : QTextStream() {
86 setEncoding(QStringConverter::Encoding::Utf8);
87 }
88
89 BASE_EXPORT ItascaTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly) : QTextStream(array,openMode) {
90 setEncoding(QStringConverter::Encoding::Utf8);
91 }
92
93 BASE_EXPORT ItascaTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite) : QTextStream(array,openMode) {
94 setEncoding(QStringConverter::Encoding::Utf8);
95 }
96
97 BASE_EXPORT ItascaTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite) : QTextStream(string,openMode) {
98 setEncoding(QStringConverter::Encoding::Utf8);
99 }
100
101 BASE_EXPORT ItascaTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite) : QTextStream(fileHandle,openMode) {
102 setEncoding(QStringConverter::Encoding::Utf8);
103 }
104
105 BASE_EXPORT ItascaTextStream(QIODevice *device) : QTextStream(device) {
106 setEncoding(QStringConverter::Encoding::Utf8);
107 }
108};
109
114
115// QDataStream functions
116//
117// NOTE: This is left for compatibility, but will be removed in the future.
118// you should avoid using QDataStream even in Qt code!
119//
121template <class T> inline QDataStream &operator<<(QDataStream &ds,const Vector2<T> &v) {
122 ds << v.x() << v.y();
123 return ds;
124}
126template <class T> inline QDataStream &operator>>(QDataStream &ds,Vector2<T> &v) {
127 ds >> v.rx() >> v.ry();
128 return ds;
129}
131template <class T> inline QDataStream &operator<<(QDataStream &ds,const Vector3<T> &v) {
132 ds << v.x() << v.y() << v.z();
133 return ds;
134}
136template <class T> inline QDataStream &operator>>(QDataStream &ds,Vector3<T> &v) {
137 ds >> v.rx() >> v.ry() >> v.rz();
138 return ds;
139}
141template <class T> inline QDataStream &operator<<(QDataStream &ds,const AVector2<T> &av) {
142 ds << av.z();
143 return ds;
144}
146template <class T> inline QDataStream &operator>>(QDataStream &ds,AVector2<T> &av) {
147 ds >> av.rz();
148 return ds;
149}
150
152template <class T> inline QDataStream &operator<<(QDataStream &ds,const AVector3<T> &av) {
153 ds << av.x() << av.y() << av.z();
154 return ds;
155}
157template <class T> inline QDataStream &operator>>(QDataStream &ds,AVector3<T> &av) {
158 ds >> av.rx() >> av.ry() >> av.rz();
159 return ds;
160}
161
162// Streaming Extent2 and Extent3
164template <class T> inline QDataStream &operator<<(QDataStream &ds,const Extent2<T> &v) {
165 ds << v.x1() << v.x2() << v.y1() << v.y2();
166 return ds;
167}
169template <class T> inline QDataStream &operator>>(QDataStream &ds,Extent2<T> &v) {
170 ds >> v.rx1() >> v.rx2() >> v.ry1() >> v.ry2();
171 return ds;
172}
174template <class T> inline QDataStream &operator<<(QDataStream &ds,const Extent3<T> &v) {
175 ds << v.x1() << v.x2() << v.y1() << v.y2() << v.z1() << v.z2();
176 return ds;
177}
179template <class T> inline QDataStream &operator>>(QDataStream &ds,Extent3<T> &v) {
180 ds >> v.rx1() >> v.rx2() >> v.ry1() >> v.ry2() >> v.rz1() >> v.rz2();
181 return ds;
182}
183
184// Data Streaming Orientation, Axes, Variant, string, and SymTensor classes.
186BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Orientation2 &o);
187BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Orientation2 &o);
188BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Orientation3 &o);
189BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Orientation3 &o);
190BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Axes2D &v);
191BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Axes2D &v);
192BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Axes3D &v);
193BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Axes3D &v);
194BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const CAxes2D &v);
195BASE_EXPORT QDataStream &operator>>(QDataStream &ds,CAxes2D &v);
196BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const CAxes3D &v);
197BASE_EXPORT QDataStream &operator>>(QDataStream &ds,CAxes3D &v);
198BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Variant &v);
199BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Variant &v);
200BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const std::wstring &v);
201BASE_EXPORT QDataStream &operator>>(QDataStream &ds,std::wstring &v);
202BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const string &v);
203BASE_EXPORT QDataStream &operator>>(QDataStream &ds,string &v);
204BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const SymTensor &s);
205BASE_EXPORT QDataStream &operator>>(QDataStream &ds,SymTensor &v);
206BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Quat2 &o);
207BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Quat2 &o);
208BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Quat3 &o);
209BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Quat3 &o);
210BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const itasca::Mat &o);
211BASE_EXPORT QDataStream &operator>>(QDataStream &ds,itasca::Mat &o);
212
213// QTextStream functions
216template <class T> inline QTextStream &operator<<(QTextStream &ds,const Vector2<T> &v) {
217 int w = ds.fieldWidth();
218 w = std::max(0,w-3);
219 w /= 2;
220 ds << qSetFieldWidth(0) << "(" << qSetFieldWidth(w) << v.x();
221 ds << qSetFieldWidth(0) << "," << qSetFieldWidth(w) << v.y();
222 ds << qSetFieldWidth(0) << ")";
223 return ds;
224}
227template <class T> inline QTextStream &operator<<(QTextStream &ds,const Vector3<T> &v) {
228 int w = ds.fieldWidth();
229 w = std::max(0,w-4);
230 w /= 3;
231 ds << qSetFieldWidth(0) << "(" << qSetFieldWidth(w) << v.x();
232 ds << qSetFieldWidth(0) << "," << qSetFieldWidth(w) << v.y();
233 ds << qSetFieldWidth(0) << "," << qSetFieldWidth(w) << v.z();
234 ds << qSetFieldWidth(0) << ")";
235 return ds;
236}
238template <class T> inline QTextStream &operator<<(QTextStream &ds,const AVector2<T> &v) {
239 ds << v.z();
240 return ds;
241}
242
243// Conversion to Qt Core 2D vector types
245template <class T> inline QPoint toQPoint(const Vector2<T> &v) {
246 QPoint ret(to<int>(v.x()),to<int>(v.y()));
247 return ret;
248}
250template <class T> inline QPointF toQPointF(const Vector2<T> &v) {
251 QPointF ret(to<qreal>(v.x()),to<qreal>(v.y()));
252 return ret;
253}
255inline DVect2 toDVect2(const QPoint &p) {
256 DVect2 dv2(to<double>(p.x()),to<double>(p.y()));
257 return dv2;
258}
260inline DVect2 toDVect2(const QSize &p) {
261 DVect2 dv2(to<double>(p.width()),to<double>(p.height()));
262 return dv2;
263}
265inline FVect2 toFVect2(const QPoint &p) {
266 FVect2 fv2(to<float>(p.x()),to<float>(p.y()));
267 return fv2;
268}
270inline IVect2 toIVect2(const QPoint &p) {
271 IVect2 iv2(to<int32>(p.x()),to<int32>(p.y()));
272 return iv2;
273}
274
275inline I64Vect2 toI64Vect2(const QPoint &p) {
276 I64Vect2 iv2(to<int64>(p.x()),to<int64>(p.y()));
277 return iv2;
278}
279
281inline UVect2 toUVect2(const QPoint &p) {
282 UVect2 uv2(to<uint32>(p.x()),to<uint32>(p.y()));
283 return uv2;
284}
286inline DVect2 toDVect2(const QPointF &p) {
287 DVect2 dv2(to<double>(p.x()),to<double>(p.y()));
288 return dv2;
289}
291inline FVect2 toFVect2(const QPointF &p) {
292 FVect2 fv2(to<float>(p.x()),to<float>(p.y()));
293 return fv2;
294}
296inline IVect2 toIVect2(const QPointF &p) {
297 IVect2 iv2(to<int32>(p.x()),to<int32>(p.y()));
298 return iv2;
299}
301inline UVect2 toUVect2(const QPointF &p) {
302 UVect2 uv2(to<uint32>(p.x()),to<uint32>(p.y()));
303 return uv2;
304}
305
307inline bool checkQVariantTypes(const QVariant &v1, const QVariant &v2)
308{
309 int type1 = static_cast<int>(v1.typeId());
310 int type2 = static_cast<int>(v2.typeId());
311#ifdef QT5
312 if (type1 == QMetaType::User)
313 {
314 type1 = v1.userType();
315 if (type2 == QMetaType::User)
316 type2 = v2.userType();
317 else
318 return false;
319 }
320#endif
321 return type1 == type2;
322}
323
324// Converts from QVariant to Variant and vice versa.
327template <typename T>
328QVariant toQVariant(const T &t) { QVariant v; v.setValue(t); return v; }
329template <> BASE_EXPORT QVariant toQVariant<Variant>(const Variant &v);
330template <> BASE_EXPORT QVariant toQVariant<base::Property>(const base::Property &prop);
331
335BASE_EXPORT Variant toVariant(const QVariant &v);
336// Convert a QVariant to a Property
337BASE_EXPORT base::Property toProperty(const QVariant &v);
338
347BASE_EXPORT bool isDirectoryProtected(const QDir &dir,const QString &registryString);
348
355BASE_EXPORT QString getInstallDirectory(const QString &registryString);
356
364BASE_EXPORT QString convertFilePath(const QString &filePath,const QDir &previousDir,const QDir &newDir);
365
369
373
378
379// This allows you to send QVariant to a fmt::format
380template <>
381struct fmt::formatter<QVariant> : public fmt::formatter<string> {
382 template <typename ParseContext>
383 constexpr auto parse(ParseContext &ctx) {
384 return fmt::formatter<string>::parse(ctx);
385 }
386
387 template <typename FormatContext> auto format(QVariant const& val, FormatContext& ctx) {
388 string out;
389 auto type = val.typeId();
390 if (type == QMetaType::Bool)
391 out = base::ts(val.toBool());
392 else if (type == QMetaType::Int)
393 out = base::ts(val.toInt());
394 else if (type == QMetaType::UInt)
395 out = base::ts(val.toUInt());
396 else if (type == QMetaType::LongLong)
397 out = base::ts(val.toLongLong());
398 else if (type == QMetaType::ULongLong)
399 out = base::ts(val.toULongLong());
400 else if (type == QMetaType::Double)
401 out = base::ts(val.toDouble());
402 else if (type == QMetaType::QString)
403 out = base::ts(val.toString());
404 else if (type == QMetaType::QVariantList) {
405 out = "(";
406 auto list = val.toList();
407 for (int i=0;i<list.size();++i) {
408 if (i) out += ",";
409 out += base::ts(list[i].toDouble());
410 }
411 out += ")";
412 } else if (type == qMetaTypeId<IVect2>())
413 out = base::ts(val.value<IVect2 >());
414 else if (type == qMetaTypeId<DVect2>())
415 out = base::ts(val.value<DVect2 >());
416 else if (type == qMetaTypeId<IVect3>())
417 out = base::ts(val.value<IVect3 >());
418 else if (type == qMetaTypeId<DVect3>())
419 out = base::ts(val.value<DVect3 >());
420 else if (type == qMetaTypeId<DAVect2>())
421 out = base::ts(val.value<DAVect2>());
422 else if (type == qMetaTypeId<DAVect3>())
423 out = base::ts(val.value<DAVect3>());
424 else
425 throw Exception("Unable to format QVariant type {}.", val.typeName());
426 return fmt::formatter<string>::format(out, ctx);
427 }
428};
429// EOF
Angular Vectors.
One stop include for all objects defined as part of base interface.
2D Angular vector class.
Definition avect.h:43
T & rz()
Returns a reference to the z component, cannot reference access x or y.
Definition avect.h:81
const T & z() const
Member access - returns the z component of the 2D angular vector.
Definition avect.h:70
3D Angular vector class.
Definition avect.h:176
Class for specifying a particular 2D cartesian axes system, and converting to and from it.
Definition axes.h:21
Class for specifying a particular 3D cartesian axes system, and converting to and from it.
Definition axes.h:121
Class for specifying a particular 2D cartesian axes system, and converting to and from it.
Definition caxes.h:25
Class for specifying a particular 3D cartesian axes system, and converting to and from it.
Definition caxes.h:103
Base exception class for all Itasca code.
Definition baseexception.h:10
2D cartesian region in space.
Definition extent2.h:12
T & ry1()
Access reference to the minimum y extent.
Definition extent2.h:129
T & ry2()
Access reference to the maximum y extent.
Definition extent2.h:131
T & rx1()
Access reference to the minimum x extent.
Definition extent2.h:125
const T & y1() const
Returns the lower y-bound.
Definition extent2.h:37
const T & y2() const
Returns the upper y-bound.
Definition extent2.h:39
const T & x2() const
Returns the upper x-bound.
Definition extent2.h:35
T & rx2()
Access reference to the maximum x extent.
Definition extent2.h:127
const T & x1() const
Returns the lower x-bound.
Definition extent2.h:33
A Class representing a cartesian extent in 3D.
Definition extent3.h:16
const T & z1() const
Returns the lower bound of the extent in the y-direction.
Definition extent3.h:50
const T & x1() const
Returns the lower bound of the extent in the x-direction.
Definition extent3.h:42
const T & y2() const
Returns the upper bound of the extent in the y-direction.
Definition extent3.h:48
T & rz1()
Reference access to the minimum z extent.
Definition extent3.h:174
T & ry1()
Reference access to the minimum y extent.
Definition extent3.h:172
T & ry2()
Reference access to the maximum y extent.
Definition extent3.h:173
T & rx2()
Reference access to the maximum x extent.
Definition extent3.h:171
T & rz2()
Definition extent3.h:175
T & rx1()
Reference access to the minimum x extent.
Definition extent3.h:170
const T & z2() const
Returns the upper bound of the extent in the y-direction.
Definition extent3.h:52
const T & y1() const
Returns the lower bound of the extent in the y-direction.
Definition extent3.h:46
const T & x2() const
Returns the upper bound of the extent in the x-direction.
Definition extent3.h:44
Definition istring.h:14
Definition basetoqt.h:83
Class for storing an "orientation", or a direction in 2D or 3D space.
Definition orientation.h:50
Class for storing an "orientation", or a direction in 2D or 3D space.
Definition orientation.h:99
2D quaternion-like utility class. In this case only the angle (in radians) is stored as opposed to th...
Definition quat.h:20
3D quaternion utility class.
Definition quat.h:108
A symmetric 2nd order tensor.
Definition symtensor.h:22
A simpler (and slightly faster) version of a QVariant, added for interfaces that do not use Qt.
Definition variant.h:32
2D vector utility class.
Definition vect.h:34
constexpr const T & x() const
X component access.
Definition vect.h:58
T & rx()
Reference access to x-component.
Definition vect.h:64
constexpr const T & y() const
Y component access.
Definition vect.h:60
T & ry()
Reference access to y-component.
Definition vect.h:66
3D vector utility class.
Definition vect.h:163
constexpr const T & y() const
The y-component of the vector.
Definition vect.h:186
T & rx()
Reference access to the x-component of the vector.
Definition vect.h:192
T & ry()
Reference access to the y-component of the vector.
Definition vect.h:194
constexpr const T & x() const
The x-component of the vector.
Definition vect.h:184
constexpr const T & z() const
The z-component of the vector.
Definition vect.h:188
T & rz()
Reference access to the z-component of the vector.
Definition vect.h:196
Definition property.h:25
Definition mat.h:28
QDataStream & operator>>(QDataStream &ds, Vector2< T > &v)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition basetoqt.h:126
QPointF toQPointF(const Vector2< T > &v)
Convert a Vector2 to a QPointF.
Definition basetoqt.h:250
BASE_EXPORT bool isDirectoryProtected(const QDir &dir, const QString &registryString)
Definition basetoqt.cpp:487
BASE_EXPORT QString getInstallDirectory(const QString &registryString)
Definition basetoqt.cpp:525
BASE_EXPORT bool setCurrentDirectory(const IString &dir)
Definition basetoqt.cpp:568
QPoint toQPoint(const Vector2< T > &v)
Convert a Vector2 to a QPoint.
Definition basetoqt.h:245
QVariant toQVariant(const T &t)
Definition basetoqt.h:328
IVect2 toIVect2(const QPoint &p)
Convert a QPoint to an IVect2.
Definition basetoqt.h:270
UVect2 toUVect2(const QPoint &p)
Convert a QPoint to a UVect2.
Definition basetoqt.h:281
BASE_EXPORT Variant toVariant(const QVariant &v)
Definition basetoqt.cpp:368
BASE_EXPORT QString convertFilePath(const QString &filePath, const QDir &previousDir, const QDir &newDir)
Definition basetoqt.cpp:536
QDataStream & operator<<(QDataStream &ds, const Vector2< T > &v)
Allows a Base type to be written to, or read from, a QDataStream.
Definition basetoqt.h:121
FVect2 toFVect2(const QPoint &p)
Convert a QPoint to an FVect2.
Definition basetoqt.h:265
#define BASE_EXPORT
Definition basedef.h:24
BASE_EXPORT QString getCurrentDirectory()
Definition basetoqt.cpp:573
BASE_EXPORT void initializeQtToBase()
Definition basetoqt.cpp:11
BASE_EXPORT QDir getCurrentDirectoryObject()
Definition basetoqt.cpp:578
bool checkQVariantTypes(const QVariant &v1, const QVariant &v2)
returns true if Variants have the same type
Definition basetoqt.h:307
DVect2 toDVect2(const QPoint &p)
Convert a QPoint to a DVect2.
Definition basetoqt.h:255