Itasca C++ Interface
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 
16 PUSHWARNING
17 VSWARNING(ALL_CODE_ANALYSIS_WARNINGS)
18 VSWARNING(4251)
19 VSWARNING(4244)
20 VSWARNING(4127)
21 CLANGWARNING("-Wlanguage-extension-token")
22 CLANGWARNING("-Wmicrosoft-enum-value")
23 INTELWARNING(1744) // DLL Interface
24 INTELWARNING(3280) // Declaration hides member
25 #include <QtCore>
26 POPWARNING
27 
28 #undef rad1
29 #undef rad2
30 #undef small
31 #undef near
32 #undef far
33 #undef grp1
34 #undef grp2
35 
36 #if QT_VERSION > 0x050000
37 # define QT5
38 #endif
39 
40 #ifndef DOXYGEN
41 // The following are commented since they should not be used with Qt anyway
42 //Q_DECLARE_METATYPE(quint64);
43 Q_DECLARE_METATYPE(string)
44 //Q_DECLARE_METATYPE(String);
45 Q_DECLARE_METATYPE(Variant)
46 Q_DECLARE_METATYPE(IVect2)
47 Q_DECLARE_METATYPE(FVect2)
48 Q_DECLARE_METATYPE(DVect2)
49 Q_DECLARE_METATYPE(UVect2)
50 Q_DECLARE_METATYPE(DAVect2)
51 Q_DECLARE_METATYPE(FAVect2)
52 Q_DECLARE_METATYPE(IAVect2)
53 Q_DECLARE_METATYPE(UAVect2)
54 Q_DECLARE_METATYPE(IVect3)
55 Q_DECLARE_METATYPE(FVect3)
56 Q_DECLARE_METATYPE(DVect3)
57 Q_DECLARE_METATYPE(UVect3)
58 Q_DECLARE_METATYPE(DAVect3)
59 Q_DECLARE_METATYPE(FAVect3)
60 Q_DECLARE_METATYPE(IAVect3)
61 Q_DECLARE_METATYPE(UAVect3)
62 Q_DECLARE_METATYPE(Orientation2)
63 Q_DECLARE_METATYPE(Orientation3)
64 Q_DECLARE_METATYPE(Axes2D)
65 Q_DECLARE_METATYPE(Axes3D)
66 Q_DECLARE_METATYPE(Quat2)
67 Q_DECLARE_METATYPE(Quat3)
68 Q_DECLARE_METATYPE(SymTensor)
69 Q_DECLARE_METATYPE(itasca::Mat)
70 #endif
71 //#ifndef __GNUC__
72 //#pragma warning(pop)
73 //#endif
74 
75 // Ensure always utf-8
76 class ItascaTextStream : public QTextStream {
77 public:
78  BASE_EXPORT ItascaTextStream() : QTextStream() {
79  setCodec("UTF-8");
80  }
81 
82  BASE_EXPORT ItascaTextStream(const QByteArray &array, QIODevice::OpenMode openMode = QIODevice::ReadOnly) : QTextStream(array,openMode) {
83  setCodec("UTF-8");
84  }
85 
86  BASE_EXPORT ItascaTextStream(QByteArray *array, QIODevice::OpenMode openMode = QIODevice::ReadWrite) : QTextStream(array,openMode) {
87  setCodec("UTF-8");
88  }
89 
90  BASE_EXPORT ItascaTextStream(QString *string, QIODevice::OpenMode openMode = QIODevice::ReadWrite) : QTextStream(string,openMode) {
91  setCodec("UTF-8");
92  }
93 
94  BASE_EXPORT ItascaTextStream(FILE *fileHandle, QIODevice::OpenMode openMode = QIODevice::ReadWrite) : QTextStream(fileHandle,openMode) {
95  setCodec("UTF-8");
96  }
97 
98  BASE_EXPORT ItascaTextStream(QIODevice *device) : QTextStream(device) {
99  setCodec("UTF-8");
100  }
101 };
102 
107 
108 // QDataStream functions
109 //
110 // NOTE: This is left for compatibility, but will be removed in the future.
111 // you should avoid using QDataStream even in Qt code!
112 //
114 template <class T> inline QDataStream &operator<<(QDataStream &ds,const Vector2<T> &v) {
115  ds << v.x() << v.y();
116  return ds;
117 }
119 template <class T> inline QDataStream &operator>>(QDataStream &ds,Vector2<T> &v) {
120  ds >> v.rx() >> v.ry();
121  return ds;
122 }
124 template <class T> inline QDataStream &operator<<(QDataStream &ds,const Vector3<T> &v) {
125  ds << v.x() << v.y() << v.z();
126  return ds;
127 }
129 template <class T> inline QDataStream &operator>>(QDataStream &ds,Vector3<T> &v) {
130  ds >> v.rx() >> v.ry() >> v.rz();
131  return ds;
132 }
134 template <class T> inline QDataStream &operator<<(QDataStream &ds,const AVector2<T> &av) {
135  ds << av.z();
136  return ds;
137 }
139 template <class T> inline QDataStream &operator>>(QDataStream &ds,AVector2<T> &av) {
140  ds >> av.rz();
141  return ds;
142 }
143 
145 template <class T> inline QDataStream &operator<<(QDataStream &ds,const AVector3<T> &av) {
146  ds << av.x() << av.y() << av.z();
147  return ds;
148 }
150 template <class T> inline QDataStream &operator>>(QDataStream &ds,AVector3<T> &av) {
151  ds >> av.rx() >> av.ry() >> av.rz();
152  return ds;
153 }
154 
155 // Streaming Extent2 and Extent3
157 template <class T> inline QDataStream &operator<<(QDataStream &ds,const Extent2<T> &v) {
158  ds << v.x1() << v.x2() << v.y1() << v.y2();
159  return ds;
160 }
162 template <class T> inline QDataStream &operator>>(QDataStream &ds,Extent2<T> &v) {
163  ds >> v.rx1() >> v.rx2() >> v.ry1() >> v.ry2();
164  return ds;
165 }
167 template <class T> inline QDataStream &operator<<(QDataStream &ds,const Extent3<T> &v) {
168  ds << v.x1() << v.x2() << v.y1() << v.y2() << v.z1() << v.z2();
169  return ds;
170 }
172 template <class T> inline QDataStream &operator>>(QDataStream &ds,Extent3<T> &v) {
173  ds >> v.rx1() >> v.rx2() >> v.ry1() >> v.ry2() >> v.rz1() >> v.rz2();
174  return ds;
175 }
176 
177 // Data Streaming Orientation, Axes, Variant, string, and SymTensor classes.
179 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Orientation2 &o);
180 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Orientation2 &o);
181 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Orientation3 &o);
182 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Orientation3 &o);
183 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Axes2D &v);
184 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Axes2D &v);
185 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Axes3D &v);
186 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Axes3D &v);
187 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const CAxes2D &v);
188 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,CAxes2D &v);
189 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const CAxes3D &v);
190 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,CAxes3D &v);
191 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Variant &v);
192 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Variant &v);
193 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const std::wstring &v);
194 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,std::wstring &v);
195 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const string &v);
196 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,string &v);
197 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const SymTensor &s);
198 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,SymTensor &v);
199 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Quat2 &o);
200 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Quat2 &o);
201 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const Quat3 &o);
202 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,Quat3 &o);
203 BASE_EXPORT QDataStream &operator<<(QDataStream &ds,const itasca::Mat &o);
204 BASE_EXPORT QDataStream &operator>>(QDataStream &ds,itasca::Mat &o);
205 
206 // QTextStream functions
209 template <class T> inline QTextStream &operator<<(QTextStream &ds,const Vector2<T> &v) {
210  int w = ds.fieldWidth();
211  w = std::max(0,w-3);
212  w /= 2;
213  ds << qSetFieldWidth(0) << "(" << qSetFieldWidth(w) << v.x();
214  ds << qSetFieldWidth(0) << "," << qSetFieldWidth(w) << v.y();
215  ds << qSetFieldWidth(0) << ")";
216  return ds;
217 }
220 template <class T> inline QTextStream &operator<<(QTextStream &ds,const Vector3<T> &v) {
221  int w = ds.fieldWidth();
222  w = std::max(0,w-4);
223  w /= 3;
224  ds << qSetFieldWidth(0) << "(" << qSetFieldWidth(w) << v.x();
225  ds << qSetFieldWidth(0) << "," << qSetFieldWidth(w) << v.y();
226  ds << qSetFieldWidth(0) << "," << qSetFieldWidth(w) << v.z();
227  ds << qSetFieldWidth(0) << ")";
228  return ds;
229 }
231 template <class T> inline QTextStream &operator<<(QTextStream &ds,const AVector2<T> &v) {
232  ds << v.z();
233  return ds;
234 }
235 
236 // Conversion to Qt Core 2D vector types
238 template <class T> inline QPoint toQPoint(const Vector2<T> &v) {
239  QPoint ret(to<int>(v.x()),to<int>(v.y()));
240  return ret;
241 }
243 template <class T> inline QPointF toQPointF(const Vector2<T> &v) {
244  QPointF ret(to<qreal>(v.x()),to<qreal>(v.y()));
245  return ret;
246 }
248 inline DVect2 toDVect2(const QPoint &p) {
249  DVect2 dv2(to<double>(p.x()),to<double>(p.y()));
250  return dv2;
251 }
253 inline DVect2 toDVect2(const QSize &p) {
254  DVect2 dv2(to<double>(p.width()),to<double>(p.height()));
255  return dv2;
256 }
258 inline FVect2 toFVect2(const QPoint &p) {
259  FVect2 fv2(to<float>(p.x()),to<float>(p.y()));
260  return fv2;
261 }
263 inline IVect2 toIVect2(const QPoint &p) {
264  IVect2 iv2(to<int32>(p.x()),to<int32>(p.y()));
265  return iv2;
266 }
267 
268 inline I64Vect2 toI64Vect2(const QPoint &p) {
269  I64Vect2 iv2(to<int64>(p.x()),to<int64>(p.y()));
270  return iv2;
271 }
272 
274 inline UVect2 toUVect2(const QPoint &p) {
275  UVect2 uv2(to<uint32>(p.x()),to<uint32>(p.y()));
276  return uv2;
277 }
279 inline DVect2 toDVect2(const QPointF &p) {
280  DVect2 dv2(to<double>(p.x()),to<double>(p.y()));
281  return dv2;
282 }
284 inline FVect2 toFVect2(const QPointF &p) {
285  FVect2 fv2(to<float>(p.x()),to<float>(p.y()));
286  return fv2;
287 }
289 inline IVect2 toIVect2(const QPointF &p) {
290  IVect2 iv2(to<int32>(p.x()),to<int32>(p.y()));
291  return iv2;
292 }
294 inline UVect2 toUVect2(const QPointF &p) {
295  UVect2 uv2(to<uint32>(p.x()),to<uint32>(p.y()));
296  return uv2;
297 }
298 
300 inline bool checkQVariantTypes(const QVariant &v1, const QVariant &v2)
301 {
302  int type1 = static_cast<int>(v1.type());
303  int type2 = static_cast<int>(v2.type());
304  if (type1==QVariant::UserType)
305  {
306  type1 = v1.userType();
307  if (type2==QVariant::UserType)
308  type2 = v2.userType();
309  else
310  return false;
311  }
312  return type1 == type2;
313 }
314 
315 // Converts from QVariant to Variant and vice versa.
318 template <typename T>
319 QVariant toQVariant(const T &t) { QVariant v; v.setValue(t); return v; }
320 template <> BASE_EXPORT QVariant toQVariant<Variant>(const Variant &v);
321 template <> BASE_EXPORT QVariant toQVariant<base::Property>(const base::Property &prop);
322 
326 BASE_EXPORT Variant toVariant(const QVariant &v);
327 // Convert a QVariant to a Property
328 BASE_EXPORT base::Property toProperty(const QVariant &v);
329 
338 BASE_EXPORT bool isDirectoryProtected(const QDir &dir,const QString &registryString);
339 
346 BASE_EXPORT QString getInstallDirectory(const QString &registryString);
347 
355 BASE_EXPORT QString convertFilePath(const QString &filePath,const QDir &previousDir,const QDir &newDir);
356 
359 BASE_EXPORT bool setCurrentDirectory(const QString &dir);
360 
364 
369 
370 // This allows you to send QVariant to a fmt::format
371 template <>
372 struct fmt::formatter<QVariant> : public fmt::formatter<string> {
373  template <typename ParseContext>
374  constexpr auto parse(ParseContext &ctx) {
375  return fmt::formatter<string>::parse(ctx);
376  }
377 
378  template <typename FormatContext>
379  auto format(QVariant const &val, FormatContext &ctx) {
380  string out;
381  switch (val.type()) {
382  case QVariant::Bool: out = base::ts(val.toBool()); break;
383  case QVariant::Int: out = base::ts(val.toInt()); break;
384  case QVariant::UInt: out = base::ts(val.toUInt()); break;
385  case QVariant::LongLong: out = base::ts(val.toLongLong()); break;
386  case QVariant::ULongLong: out = base::ts(val.toULongLong()); break;
387  case QVariant::Double: out = base::ts(val.toDouble()); break;
388  case QVariant::String: out = base::ts(val.toString()); break;
389  case QVariant::List: {
390  out = "(";
391  auto list = val.toList();
392  for (int i=0;i<list.size();++i) {
393  if (i) out += ",";
394  out += base::ts(list[i].toDouble());
395  }
396  out += ")";
397  break;
398  }
399  case QVariant::UserType: {
400  auto v = val.userType();
401  if (v==qMetaTypeId<IVect2 >()) out = base::ts(val.value<IVect2 >());
402  else if (v==qMetaTypeId<DVect2 >()) out = base::ts(val.value<DVect2 >());
403  else if (v==qMetaTypeId<IVect3 >()) out = base::ts(val.value<IVect3 >());
404  else if (v==qMetaTypeId<DVect3 >()) out = base::ts(val.value<DVect3 >());
405  else if (v==qMetaTypeId<DAVect2>()) out = base::ts(val.value<DAVect2>());
406  else if (v==qMetaTypeId<DAVect3>()) out = base::ts(val.value<DAVect3>());
407  break;
408  }
409  default:
410  throw Exception("Unable to format QVariant type {}.", val.typeName());
411  }
412  return fmt::formatter<string>::format(out, ctx);
413  }
414 };
415 
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
const T & y1() const
Returns the lower y-bound.
Definition: extent2.h:37
T & rx1()
Access reference to the minimum x extent.
Definition: extent2.h:125
T & ry2()
Access reference to the maximum y extent.
Definition: extent2.h:131
const T & x1() const
Returns the lower x-bound.
Definition: extent2.h:33
const T & y2() const
Returns the upper y-bound.
Definition: extent2.h:39
T & rx2()
Access reference to the maximum x extent.
Definition: extent2.h:127
T & ry1()
Access reference to the minimum y extent.
Definition: extent2.h:129
const T & x2() const
Returns the upper x-bound.
Definition: extent2.h:35
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
T & ry1()
Reference access to the minimum y extent.
Definition: extent3.h:172
const T & y2() const
Returns the upper bound of the extent in the y-direction.
Definition: extent3.h:48
const T & x2() const
Returns the upper bound of the extent in the x-direction.
Definition: extent3.h:44
T & rx1()
Reference access to the minimum x extent.
Definition: extent3.h:170
T & rz1()
Reference access to the minimum z extent.
Definition: extent3.h:174
T & ry2()
Reference access to the maximum y extent.
Definition: extent3.h:173
T & rz2()
Definition: extent3.h:175
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
T & rx2()
Reference access to the maximum x extent.
Definition: extent3.h:171
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:109
A simpler (and slightly faster) version of a QVariant, added for interfaces that do not use Qt.
Definition: variant.h:32
Definition: property.h:20
Definition: mat.h:28
QPointF toQPointF(const Vector2< T > &v)
Convert a Vector2 to a QPointF.
Definition: basetoqt.h:243
BASE_EXPORT bool isDirectoryProtected(const QDir &dir, const QString &registryString)
Definition: basetoqt.cpp:453
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:119
BASE_EXPORT QString getInstallDirectory(const QString &registryString)
Definition: basetoqt.cpp:491
PUSHWARNING INTELWARNING(1744) INTELWARNING(3280) POPWARNING class ItascaTextStream BASE_EXPORT void initializeQtToBase()
QPoint toQPoint(const Vector2< T > &v)
Convert a Vector2 to a QPoint.
Definition: basetoqt.h:238
QVariant toQVariant(const T &t)
Definition: basetoqt.h:319
IVect2 toIVect2(const QPoint &p)
Convert a QPoint to an IVect2.
Definition: basetoqt.h:263
UVect2 toUVect2(const QPoint &p)
Convert a QPoint to a UVect2.
Definition: basetoqt.h:274
BASE_EXPORT Variant toVariant(const QVariant &v)
Definition: basetoqt.cpp:368
QDataStream & operator<<(QDataStream &ds, const Vector2< T > &v)
Allows a Base type to be written to, or read from, a QDataStream.
Definition: basetoqt.h:114
BASE_EXPORT QString convertFilePath(const QString &filePath, const QDir &previousDir, const QDir &newDir)
Definition: basetoqt.cpp:502
FVect2 toFVect2(const QPoint &p)
Convert a QPoint to an FVect2.
Definition: basetoqt.h:258
BASE_EXPORT bool setCurrentDirectory(const QString &dir)
Definition: basetoqt.cpp:534
#define BASE_EXPORT
Definition: basedef.h:24
BASE_EXPORT QString getCurrentDirectory()
Definition: basetoqt.cpp:539
BASE_EXPORT QDir getCurrentDirectoryObject()
Definition: basetoqt.cpp:544
bool checkQVariantTypes(const QVariant &v1, const QVariant &v2)
returns true if Variants have the same type
Definition: basetoqt.h:300
DVect2 toDVect2(const QPoint &p)
Convert a QPoint to a DVect2.
Definition: basetoqt.h:248