step/stepcore
types.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00023 #ifndef STEPCORE_TYPES_H
00024 #define STEPCORE_TYPES_H
00025
00026 #include "object.h"
00027 #include "vector.h"
00028 #include <vector>
00029 #include <QByteArray>
00030 #include <gmm/gmm_vector.h>
00031 #include <gmm/gmm_matrix.h>
00032 #include <gmm/gmm_interface.h>
00033
00034 namespace StepCore {
00035
00036 typedef std::vector<double> GmmStdVector;
00037 typedef gmm::array1D_reference<double*> GmmArrayVector;
00038 typedef gmm::rsvector<double> GmmSparseVector;
00039 typedef gmm::row_matrix<GmmSparseVector> GmmSparseRowMatrix;
00040 typedef gmm::col_matrix<GmmSparseVector> GmmSparseColMatrix;
00041
00043 struct Color
00044 {
00045 Color() {}
00046 Color(unsigned int v): value(v) {}
00047 operator unsigned int() const { return value; }
00048 unsigned int value;
00049 };
00050
00051 template<> inline QString typeToString(const Color& v)
00052 {
00053 return QString("#%1").arg(v, 8, 16, QLatin1Char('0'));
00054 }
00055
00056 template<> inline Color stringToType(const QString& s, bool *ok)
00057 {
00058 if(ok) *ok = false;
00059 QString s1 = s.trimmed();
00060 if(!s1.startsWith('#')) return Color(0);
00061 s1 = s1.mid(1);
00062 return Color(s1.toUInt(ok, 16));
00063 }
00064
00066 template<> inline QString typeToString(const QByteArray& v)
00067 {
00068 return QString::fromAscii(v.toBase64());
00069 }
00070
00071 template<> inline QByteArray stringToType(const QString& s, bool *ok)
00072 {
00073 if(ok) *ok = true;
00074 return QByteArray::fromBase64(s.toAscii());
00075 }
00076
00078 template<> inline QString typeToString(const Vector2d& v)
00079 {
00080 return QString("(%1,%2)").arg(v[0]).arg(v[1]);
00081 }
00082
00083 template<> inline Vector2d stringToType(const QString& s, bool *ok)
00084 {
00085
00086 if(ok) *ok = false;
00087 QString s1 = s.trimmed();
00088 if(!s1.startsWith('(') || !s1.endsWith(')')) return Vector2d();
00089 s1 = s1.mid(1, s1.size()-2);
00090 bool ok1, ok2;
00091 StepCore::Vector2d v(s1.section(',',0,0).toDouble(&ok1), s1.section(',',1,1).toDouble(&ok2));
00092 if(!ok1 || !ok2) return v;
00093 if(ok) *ok = true;
00094 return v;
00095 }
00096
00098 template<> inline QString typeToString(const Vector2i& v)
00099 {
00100 return QString("(%1,%2)").arg(v[0]).arg(v[1]);
00101 }
00102
00103 template<> inline Vector2i stringToType(const QString& s, bool *ok)
00104 {
00105
00106 if(ok) *ok = false;
00107 QString s1 = s.trimmed();
00108 if(!s1.startsWith('(') || !s1.endsWith(')')) return Vector2i();
00109 s1 = s1.mid(1, s1.size()-2);
00110 bool ok1, ok2;
00111 StepCore::Vector2i v(s1.section(',',0,0).toInt(&ok1), s1.section(',',1,1).toInt(&ok2));
00112 if(!ok1 || !ok2) return v;
00113 if(ok) *ok = true;
00114 return v;
00115 }
00116
00118 typedef std::vector<Vector2d> Vector2dList;
00119
00120 template<> inline QString typeToString(const Vector2dList& v)
00121 {
00122 QString ret;
00123 for(Vector2dList::const_iterator it = v.begin(); it != v.end(); ++it) {
00124 if(!ret.isEmpty()) ret += ",";
00125 ret += QString("(%1,%2)").arg((*it)[0]).arg((*it)[1]);
00126 }
00127 return ret;
00128 }
00129
00130 template<> inline Vector2dList stringToType(const QString& s, bool *ok)
00131 {
00132
00133 Vector2dList ret;
00134 if(ok) *ok = false;
00135 QString s1 = s.trimmed();
00136
00137
00138 while(s1[0] == '(' && s1.contains(')')) {
00139 bool ok; double d1, d2;
00140 s1 = s1.mid(1);
00141 d1 = s1.section(',',0,0).toDouble(&ok);
00142 if(!ok) return Vector2dList();
00143 s1 = s1.section(',',1);
00144 d2 = s1.section(')',0,0).toDouble(&ok);
00145 if(!ok) return Vector2dList();
00146 s1 = s1.section(')',1).trimmed();
00147 ret.push_back(Vector2d(d1, d2));
00148 if(s1.isEmpty()) break;
00149 if(s1[0] != ',') return Vector2dList();
00150 s1 = s1.mid(1).trimmed();
00151 }
00152 if(!s1.isEmpty()) return Vector2dList();
00153 if(ok) *ok = true;
00154 return ret;
00155 }
00156
00157 }
00158
00159 #ifdef STEPCORE_WITH_QT
00160 Q_DECLARE_METATYPE(StepCore::Vector2dList)
00161 Q_DECLARE_METATYPE(StepCore::Object*)
00162 Q_DECLARE_METATYPE(StepCore::Color)
00163 #endif
00164
00165
00166 #endif
00167