cctw  0.2.1
cctwobject.cpp
Go to the documentation of this file.
1 #include "cctwobject.h"
2 #include "qcepproperty.h"
3 #include "cctwapplication.h"
4 #include <QMetaProperty>
5 #include "cctwunitcell.h"
6 
7 CctwObject::CctwObject(QString name, QObject *parent) :
8  QObject(parent),
9  m_ObjectNamer(this, name),
10  m_Name(QcepSettingsSaverWPtr(), this, "name", name, "Object Name")
11 {
12 }
13 
14 void CctwObject::printLine(QString line)
15 {
16 #ifdef NO_GUI
17  printf("%s\n", qPrintable(line));
18 #else
19  if (parent()) {
20  QMetaObject::invokeMethod( parent(), "printLine", Q_ARG(QString, line));
21  }
22 #endif
23 }
24 
25 void CctwObject::printMessage(QString msg, QDateTime dt)
26 {
27 #ifdef NO_GUI
28  printf("%s\n", qPrintable(msg));
29 #else
30  if (parent()) {
31  QMetaObject::invokeMethod( parent(), "printMessage", Q_ARG(QString, msg), Q_ARG(QDateTime, dt));
32  } else {
33  printf("MESSAGE: %s %s\n",
34  qPrintable(dt.toString("hh:mm:ss")), qPrintable(msg));
35  }
36 #endif
37 }
38 
39 void CctwObject::writeSettings(QSettings *set, QString section)
40 {
41  const QMetaObject *meta = metaObject();
42 
43  while (meta) {
44  QcepProperty::writeSettings(this, meta, section, set);
45 
46  meta = meta->superClass();
47 
48  if (meta == &QObject::staticMetaObject) break;
49  }
50 }
51 
52 void CctwObject::readSettings(QSettings *set, QString section)
53 {
54  QcepProperty::readSettings(this, metaObject(), section, set);
55 }
56 
57 QString CctwObject::scriptValueLiteral(QVariant v)
58 {
59  if (v.canConvert<CctwIntVector3D>()) {
60  CctwIntVector3D dv = v.value<CctwIntVector3D>();
61  return tr("[%1,%2,%3]").arg(dv.x()).arg(dv.y()).arg(dv.z());
62  } else if (v.canConvert<CctwDoubleVector3D>()) {
63  CctwDoubleVector3D dv = v.value<CctwDoubleVector3D>();
64  return tr("[%1,%2,%3]").arg(dv.x()).arg(dv.y()).arg(dv.z());
65  } else if (v.canConvert<CctwDoubleMatrix3x3>()) {
67  return tr("[ [%1,%2,%3], [%4,%5,%6], [%7,%8,%9] ]")
68  .arg(m(0,0)).arg(m(0,1)).arg(m(0,2))
69  .arg(m(1,0)).arg(m(1,1)).arg(m(1,2))
70  .arg(m(2,0)).arg(m(2,1)).arg(m(2,2));
71  } else if (v.canConvert<CctwUnitCell>()) {
72  CctwUnitCell u = v.value<CctwUnitCell>();
73  return tr("[%1,%2,%3, %4,%5,%6]")
74  .arg(u.a()).arg(u.b()).arg(u.c())
75  .arg(u.alpha()).arg(u.beta()).arg(u.gamma());
76  } else if (v.type() == QMetaType::QString) {
77  return "\"" + CctwApplication::addSlashes(v.toString()) + "\"";
78  } else {
79  return v.toString();
80  }
81 }
82 
84 {
85  QString res = "";
86  const QMetaObject* metaObject = this->metaObject();
87 
88  for (int i=1; i < metaObject->propertyCount(); i++) {
89  QMetaProperty prop = metaObject->property(i);
90 
91  if (prop.isStored()) {
92  res += tr("%1.%2 = %3;\n").arg(this->get_Name())
93  .arg(prop.name())
94  .arg(scriptValueLiteral(this->property(prop.name())));
95  }
96  }
97 
98  return res;
99 }
QString scriptValueLiteral(QVariant v)
Definition: cctwobject.cpp:57
virtual QString settingsScript()
Definition: cctwobject.cpp:83
static QString addSlashes(QString str)
double beta() const
double b() const
double a() const
virtual void printMessage(QString msg, QDateTime dt=QDateTime::currentDateTime())
Definition: cctwobject.cpp:25
double alpha() const
T x() const
Definition: cctwvector3d.h:17
virtual void readSettings(QSettings *set, QString section)
Definition: cctwobject.cpp:52
T z() const
Definition: cctwvector3d.h:19
virtual void printLine(QString line)
Definition: cctwobject.cpp:14
double gamma() const
virtual void writeSettings(QSettings *set, QString section)
Definition: cctwobject.cpp:39
CctwObject(QString name, QObject *parent=0)
Definition: cctwobject.cpp:7
double c() const
T y() const
Definition: cctwvector3d.h:18