1 /* This file is part of StepCore library.
2    Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
3 
4    StepCore library is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8 
9    StepCore library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with StepCore; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18 
19 #include "object.h"
20 #include <cstring>
21 #include <QCoreApplication>
22 
23 namespace StepCore {
24 
25 STEPCORE_META_OBJECT(Object, QT_TRANSLATE_NOOP("ObjectClass", "Object"), QT_TRANSLATE_NOOP("ObjectDescription", "Object"), MetaObject::ABSTRACT,,
26         STEPCORE_PROPERTY_RW(QString, name, QT_TRANSLATE_NOOP("PropertyName", "name"), STEPCORE_UNITS_NULL, QT_TRANSLATE_NOOP("PropertyDescription", "Object name"), name, setName))
27 
28 int MetaObject::s_classIdCount = 0;
29 
copyProperties(const MetaProperty ** dest) const30 void MetaObject::copyProperties(const MetaProperty** dest) const
31 {
32     int c = 0;
33     for(int i=0; i<superClassCount(); ++i) {
34         superClass(i)->copyProperties(dest + c);
35         c += superClass(i)->propertyCount();
36     }
37     for(int i=0; i<_classPropertyCount; ++i) {
38         dest[c+i] = _classProperties + i;
39     }
40 }
41 
init() const42 void MetaObject::init() const
43 {
44     if(_initialized) return;
45 
46     // properties
47     _allPropertyCount = classPropertyCount();
48     for(int i=0; i<superClassCount(); ++i) {
49         _allPropertyCount += superClass(i)->propertyCount();
50     }
51 
52     _allProperties = new const MetaProperty*[_allPropertyCount];
53     copyProperties(_allProperties);
54 
55     // classId and super classes
56     _classId = s_classIdCount++; // all super classes is already registered
57     _allSuperClassIds.fill(false, s_classIdCount);
58     _allSuperClassIds.setBit(_classId);
59     for(int i=0; i<superClassCount(); ++i) {
60         _allSuperClassIds |= superClass(i)->_allSuperClassIds;
61     }
62 
63     // strings
64     _classNameTr = QCoreApplication::translate("ObjectClass", _className.toUtf8().constData());
65     _descriptionTr = QCoreApplication::translate(NULL, _description.toUtf8().constData());
66 
67     _initialized = true;
68 }
69 
inherits(const MetaObject * obj) const70 bool MetaObject::inherits(const MetaObject* obj) const
71 {
72     if(!_initialized) init();
73     int objClassId = obj->classId();
74     if(objClassId == _classId) return true;
75     else if(objClassId > _classId) return false;
76     return _allSuperClassIds.testBit(objClassId);
77 }
78 
inherits(const char * name) const79 bool MetaObject::inherits(const char* name) const
80 {
81     //if(std::strcmp(className(), name) == 0) return true;
82     if(className() == name) return true;
83     for(int i=superClassCount()-1; i>=0; --i) {
84         if(superClass(i)->inherits(name)) return true;
85     }
86     return false;
87 }
88 
89 /*
90 int MetaObject::propertyCount() const
91 {
92     if(_allPropertyCount >= 0) return _allPropertyCount;
93 
94     _allPropertyCount = 0;
95     for(int i=0; i<superClassCount(); ++i)
96         _allPropertyCount += superClass(i)->propertyCount();
97     _allPropertyCount += classPropertyCount();
98     return _allPropertyCount;
99 }*/
100 
101 /*
102 const MetaProperty* MetaObject::property(int n) const
103 {
104     for(int i=0; i<superClassCount(); ++i) {
105         const MetaProperty* pr = superClass(i)->property(n);
106         if(pr) return pr;
107         n -= superClass(i)->propertyCount();
108     }
109     if(n < classPropertyCount()) return &_classProperties[n];
110     else return NULL;
111 }*/
112 
init() const113 void MetaProperty::init() const
114 {
115     _nameTr = QCoreApplication::translate("PropertyName", _name.toUtf8().constData());
116     _unitsTr = QCoreApplication::translate("Units", _units.toUtf8().constData());
117     _descriptionTr = QCoreApplication::translate(NULL, _description.toUtf8().constData());
118 
119     _initialized = true;
120 }
121 
property(const QString & name) const122 const MetaProperty* MetaObject::property(const QString& name) const
123 {
124     if(!_initialized) init();
125     for(int i=0; i<_allPropertyCount; ++i) {
126         //if(std::strcmp(_allProperties[i]->name(), name) == 0)
127         if(_allProperties[i]->name() == name)
128             return _allProperties[i];
129     }
130     /*
131     for(int i=0; i<classPropertyCount(); ++i) {
132         if(std::strcmp(classProperty(i)->name(), name) == 0)
133             return classProperty(i);
134     }*/
135     /*
136     for(int i=superClassCount()-1; i>=0; --i) {
137         const MetaProperty* pr = superClass(i)->property(name);
138         if(pr) return pr;
139     }*/
140     return NULL;
141 }
142 
143 } // namespace StepCore
144 
145