QXRD  0.11.16
qcepdataobject.cpp
Go to the documentation of this file.
1 #include "qcepdataobject.h"
2 #include <QScriptEngine>
3 #include "qcepdatagroup.h"
4 #include <stdio.h>
5 #include "qcepapplication.h"
6 #include <QAtomicInteger>
7 #include <QFileInfo>
8 #include <QDir>
9 
10 static QAtomicInt s_ObjectAllocateCount(0);
11 static QAtomicInt s_ObjectDeleteCount(0);
12 
14  QString name,
15  int byteSize,
16  QcepObject *parent) :
17  QcepObject(name, parent),
18  m_Saver(saver),
19  m_Type(saver, this, "type", "object", "Data object type"),
20  m_ByteSize(QcepSettingsSaverWPtr(), this, "size", byteSize, "Object Size"),
21  m_Creator(saver, this, "creator", "Unknown", "QXRD Version Number"),
22  m_Version(saver, this, "version", "Unknown", "QXRD Version Number"),
23  m_QtVersion(saver, this, "qtVersion", QT_VERSION_STR, "QT Version Number"),
24  m_Description(saver, this, "description", "", "Object Description"),
25  m_FileName(saver, this, "fileName", "", "File Name of Image"),
26  m_ObjectSaved(saver, this, "objectSaved",0, "Object is Saved?")
27 {
28  s_ObjectAllocateCount.fetchAndAddOrdered(1);
29 
30  set_Type("object");
31 
32  if (name.contains("/")) {
33  printMessage(tr("object %1 name contains \"/\"").arg(name));
34  }
35 
36  if (g_Application) {
38  }
39 }
40 
42 {
43  s_ObjectDeleteCount.fetchAndAddOrdered(1);
44 }
45 
47 {
48  return s_ObjectAllocateCount.load();
49 }
50 
52 {
53  return s_ObjectDeleteCount.load();
54 }
55 
56 QString QcepDataObject::description() const
57 {
58  return "";
59 }
60 
61 QString QcepDataObject::pathName() const
62 {
63  QString suffix="";
64 
65  if (qobject_cast<const QcepDataGroup*>(this)) {
66  suffix = "/";
67  }
68 
69  if (parentItem()) {
70  return parentItem()->pathName()+get_Name()+suffix;
71  } else {
72  return "/";
73  }
74 }
75 
76 void QcepDataObject::mkPath(QString filePath)
77 {
78  QFileInfo f(filePath);
79  QDir dir = f.dir();
80 
81  if (!dir.exists()) {
82  dir.mkpath(dir.absolutePath());
83  }
84 }
85 
87 {
88  QFileInfo f(name);
89 
90  if (f.exists()) {
91  QDir dir = f.dir();
92  QString base = f.baseName();
93  QString suff = f.completeSuffix();
94 
95 // QxrdAcquisitionPtr acq(acquisition());
96 
97  int width = 5;
98 
99 // if (acq) {
100 // width = acq->get_FileOverflowWidth();
101 // }
102 
103  for (int i=1; ; i++) {
104  QString newname = dir.filePath(base+QString().sprintf("-%0*d.",width,i)+suff);
105  QFileInfo f(newname);
106 
107  if (!f.exists()) {
108  return newname;
109  }
110  }
111  } else {
112  return name;
113  }
114 }
115 
116 void QcepDataObject::saveData(QString &name, QString filter, Overwrite canOverwrite)
117 {
118 }
119 
121 {
122  QcepDataObjectPtr res(new QcepDataObject(saver, name, 0, parent));
123 
124  return res;
125 }
126 
128 {
129  return m_Saver;
130 }
131 
132 QScriptValue QcepDataObject::toScriptValue(QScriptEngine *engine, const QcepDataObjectPtr &data)
133 {
134  return engine->newQObject(data.data());
135 }
136 
137 void QcepDataObject::fromScriptValue(const QScriptValue &obj, QcepDataObjectPtr &data)
138 {
139  QObject *qobj = obj.toQObject();
140 
141  if (qobj) {
142  QcepDataObject *qdobj = qobject_cast<QcepDataObject*>(qobj);
143 
144  if (qdobj) {
145  QcepDataObjectPtr p = qdobj->sharedFromThis();
146 
147  if (p) {
148  data = p;
149  } else {
150  printf("QcepDataObject::fromScriptValue returns NULL\n");
151  }
152  }
153  }
154 }
155 
157 {
158  return 0;
159 }
160 
162 {
163  return 0;
164 }
165 
167 {
168  return 0;
169 }
170 
172 {
173  return QcepDataObjectPtr();
174 }
175 
177 {
178  return QcepDataObjectPtr();
179 }
180 
182 {
183  return m_Parent;
184 }
185 
187 {
188  QcepDataGroupPtr parent(m_Parent);
189 
190  if (parent) {
191  return parent->rootItem();
192  } else {
193  QcepDataObjectPtr obj = sharedFromThis();
194 
195  return qSharedPointerDynamicCast<QcepDataGroup>(obj);
196  }
197 }
198 
200 {
201  m_Parent = parent;
202 }
203 
205 {
206  QcepDataObjectPtr parent = m_Parent;
207 
208  if (parent) {
209  QcepDataGroup *parentGroup =
210  qobject_cast<QcepDataGroup*>(parent.data());
211 
212  if (parentGroup) {
213  for (int i=0; i<parentGroup->childCount(); i++) {
214  if (parentGroup->item(i).data() == this) {
215  return i;
216  }
217  }
218  }
219  }
220 
221  return 0;
222 }
223 
224 QVariant QcepDataObject::columnData(int col) const
225 {
226  if (col == 0) {
227  return get_Name();
228  } else if (col == 1) {
229  return get_Type();
230  } else {
231  return QVariant(description());
232  }
233 }
234 
235 QString QcepDataObject::metaTypeName(int id) const
236 {
237  return QMetaType::typeName(id);
238 }
239 
241 {
242  return "Text (*.txt)";
243 }
QcepDataObjectPtr item(QString nm)
virtual void saveData(QString &name, QString filter, Overwrite canOverwrite=NoOverwrite)
static void fromScriptValue(const QScriptValue &obj, QcepDataObjectPtr &data)
QcepSettingsSaverWPtr m_Saver
static QScriptValue toScriptValue(QScriptEngine *engine, const QcepDataObjectPtr &data)
QSharedPointer< QcepDataGroup > QcepDataGroupPtr
QString uniqueFileName(QString name)
virtual int columnCount() const
void mkPath(QString filePath)
QcepDataObject(QcepSettingsSaverWPtr saver, QString name, int byteSize, QcepObject *parent)
virtual QcepDataGroupPtr parentItem() const
virtual ~QcepDataObject()
int childCount() const
static int deletedObjects()
virtual int childCount() const
QString get_Name() const
Definition: qcepobject.cpp:72
virtual QString fileFormatFilterString()
QString metaTypeName(int id) const
static int allocatedObjects()
virtual void setDefaultObjectData(QcepDataObject *obj)=0
virtual int rowCount() const
virtual void setParentItem(QcepDataGroupWPtr parent)
QcepDataGroupWPtr m_Parent
virtual void printMessage(QString msg, QDateTime dt=QDateTime::currentDateTime()) const
Definition: qcepobject.cpp:84
virtual int indexInParent() const
QcepDataGroupPtr rootItem()
QString pathName() const
QSharedPointer< QcepDataObject > QcepDataObjectPtr
virtual QString description() const
QcepApplication * g_Application
static QAtomicInt s_ObjectDeleteCount(0)
QString name
Definition: qcepobject.h:49
virtual QcepDataObjectPtr item(int n)
static QcepDataObjectPtr newDataObject(QcepSettingsSaverWPtr saver, QString name, QcepObject *parent)
static QAtomicInt s_ObjectAllocateCount(0)
virtual QVariant columnData(int col) const
QWeakPointer< QcepSettingsSaver > QcepSettingsSaverWPtr
QWeakPointer< QcepDataGroup > QcepDataGroupWPtr
QcepSettingsSaverWPtr saver()