PropertyStandard.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <boost/version.hpp>
00028 # include <boost/filesystem/path.hpp>
00029 #endif
00030 
00032 
00033 #include <Base/Exception.h>
00034 #include <Base/Reader.h>
00035 #include <Base/Writer.h>
00036 #include <Base/Stream.h>
00037 
00038 #include "PropertyStandard.h"
00039 #include "MaterialPy.h"
00040 #define new DEBUG_CLIENTBLOCK
00041 using namespace App;
00042 using namespace Base;
00043 using namespace std;
00044 
00045 
00046 
00047 
00048 //**************************************************************************
00049 //**************************************************************************
00050 // PropertyInteger
00051 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00052 
00053 TYPESYSTEM_SOURCE(App::PropertyInteger , App::Property);
00054 
00055 //**************************************************************************
00056 // Construction/Destruction
00057 
00058 
00059 PropertyInteger::PropertyInteger()
00060 {
00061     _lValue = 0;
00062 }
00063 
00064 
00065 PropertyInteger::~PropertyInteger()
00066 {
00067 
00068 }
00069 
00070 //**************************************************************************
00071 // Base class implementer
00072 
00073 
00074 void PropertyInteger::setValue(long lValue)
00075 {
00076     aboutToSetValue();
00077     _lValue=lValue;
00078     hasSetValue();
00079 }
00080 
00081 long PropertyInteger::getValue(void) const
00082 {
00083     return _lValue;
00084 }
00085 
00086 PyObject *PropertyInteger::getPyObject(void)
00087 {
00088     return Py_BuildValue("l", _lValue);
00089 }
00090 
00091 void PropertyInteger::setPyObject(PyObject *value)
00092 { 
00093     if (PyInt_Check(value)) {
00094         aboutToSetValue();
00095         _lValue = PyInt_AsLong(value);
00096         hasSetValue();
00097     } 
00098     else {
00099         std::string error = std::string("type must be int, not ");
00100         error += value->ob_type->tp_name;
00101         throw Py::TypeError(error);
00102     }
00103 }
00104 
00105 void PropertyInteger::Save (Base::Writer &writer) const
00106 {
00107     writer.Stream() << writer.ind() << "<Integer value=\"" <<  _lValue <<"\"/>" << std::endl;
00108 }
00109 
00110 void PropertyInteger::Restore(Base::XMLReader &reader)
00111 {
00112     // read my Element
00113     reader.readElement("Integer");
00114     // get the value of my Attribute
00115     setValue(reader.getAttributeAsInteger("value"));
00116 }
00117 
00118 Property *PropertyInteger::Copy(void) const
00119 {
00120     PropertyInteger *p= new PropertyInteger();
00121     p->_lValue = _lValue;
00122     return p;
00123 }
00124 
00125 void PropertyInteger::Paste(const Property &from)
00126 {
00127     aboutToSetValue();
00128     _lValue = dynamic_cast<const PropertyInteger&>(from)._lValue;
00129     hasSetValue();
00130 }
00131 
00132 
00133 //**************************************************************************
00134 //**************************************************************************
00135 // PropertyPath
00136 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00137 
00138 TYPESYSTEM_SOURCE(App::PropertyPath , App::Property);
00139 
00140 //**************************************************************************
00141 // Construction/Destruction
00142 
00143 PropertyPath::PropertyPath()
00144 {
00145 
00146 }
00147 
00148 PropertyPath::~PropertyPath()
00149 {
00150 
00151 }
00152 
00153 
00154 //**************************************************************************
00155 // Base class implementer
00156 
00157 
00158 //**************************************************************************
00159 // Setter/getter for the property
00160 
00161 void PropertyPath::setValue(const boost::filesystem::path &Path)
00162 {
00163     aboutToSetValue();
00164     _cValue = Path;
00165     hasSetValue();
00166 }
00167 
00168 void PropertyPath::setValue(const char * Path)
00169 {
00170     aboutToSetValue();
00171 #if (BOOST_VERSION < 104600) || (BOOST_FILESYSTEM_VERSION == 2)
00172     _cValue = boost::filesystem::path(Path,boost::filesystem::no_check );
00173     //_cValue = boost::filesystem::path(Path,boost::filesystem::native );
00174     //_cValue = boost::filesystem::path(Path,boost::filesystem::windows_name );
00175 #else
00176     _cValue = boost::filesystem::path(Path);
00177 #endif
00178     hasSetValue();
00179 }
00180 
00181 boost::filesystem::path PropertyPath::getValue(void) const
00182 {
00183     return _cValue;
00184 }
00185 
00186 PyObject *PropertyPath::getPyObject(void)
00187 {
00188 #if (BOOST_VERSION < 104600) || (BOOST_FILESYSTEM_VERSION == 2)
00189     std::string str = _cValue.native_file_string();
00190 #else
00191     std::string str = _cValue.string();
00192 #endif
00193 
00194     // Returns a new reference, don't increment it!
00195     PyObject *p = PyUnicode_DecodeUTF8(str.c_str(),str.size(),0);
00196     if (!p) throw Base::Exception("UTF8 conversion failure at PropertyPath::getPyObject()");
00197     return p;
00198 }
00199 
00200 void PropertyPath::setPyObject(PyObject *value)
00201 {
00202     std::string path;
00203     if (PyUnicode_Check(value)) {
00204         PyObject* unicode = PyUnicode_AsUTF8String(value);
00205         path = PyString_AsString(unicode);
00206         Py_DECREF(unicode);
00207     }
00208     else if (PyString_Check(value)) {
00209         path = PyString_AsString(value);
00210     }
00211     else {
00212         std::string error = std::string("type must be str or unicode, not ");
00213         error += value->ob_type->tp_name;
00214         throw Py::TypeError(error);
00215     }
00216 
00217     // assign the path
00218     setValue(path.c_str());
00219 }
00220 
00221 
00222 void PropertyPath::Save (Base::Writer &writer) const
00223 {
00224     std::string val = encodeAttribute(_cValue.string());
00225     writer.Stream() << writer.ind() << "<Path value=\"" <<  val <<"\"/>" << std::endl;
00226 }
00227 
00228 void PropertyPath::Restore(Base::XMLReader &reader)
00229 {
00230     // read my Element
00231     reader.readElement("Path");
00232     // get the value of my Attribute
00233     setValue(reader.getAttribute("value"));
00234 }
00235 
00236 Property *PropertyPath::Copy(void) const
00237 {
00238     PropertyPath *p= new PropertyPath();
00239     p->_cValue = _cValue;
00240     return p;
00241 }
00242 
00243 void PropertyPath::Paste(const Property &from)
00244 {
00245     aboutToSetValue();
00246     _cValue = dynamic_cast<const PropertyPath&>(from)._cValue;
00247     hasSetValue();
00248 }
00249 
00250 unsigned int PropertyPath::getMemSize (void) const
00251 {
00252     return static_cast<unsigned int>(_cValue.string().size());
00253 }
00254 
00255 //**************************************************************************
00256 //**************************************************************************
00257 // PropertyEnumeration
00258 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00259 
00260 TYPESYSTEM_SOURCE(App::PropertyEnumeration, App::PropertyInteger);
00261 
00262 //**************************************************************************
00263 // Construction/Destruction
00264 
00265 
00266 PropertyEnumeration::PropertyEnumeration()
00267   : _CustomEnum(false), _EnumArray(0)
00268 {
00269 
00270 }
00271 
00272 PropertyEnumeration::~PropertyEnumeration()
00273 {
00274 
00275 }
00276 
00277 void PropertyEnumeration::setEnums(const char** plEnums)
00278 {
00279     _EnumArray = plEnums;
00280 # ifdef FC_DEBUG
00281     if (_EnumArray) {
00282         // check for NULL termination
00283         const char* p = *_EnumArray;
00284         unsigned int i=0;
00285         while(*(p++) != NULL)i++;
00286             // very unlikely to have enums with more then 5000 entries!
00287             assert(i<5000);
00288     }
00289 # endif
00290 }
00291 
00292 void PropertyEnumeration::setValue(const char* value)
00293 {
00294     // using string methods without set, use setEnums(const char** plEnums) first!
00295     assert(_EnumArray);
00296 
00297     // set zero if there is no enum array
00298     if(!_EnumArray){
00299         PropertyInteger::setValue(0);
00300         return;
00301     }
00302 
00303     unsigned int i=0;
00304     const char** plEnums = _EnumArray;
00305 
00306     // search for the right entry
00307     while(1){
00308         // end of list? set zero
00309         if(*plEnums==NULL){
00310             PropertyInteger::setValue(0);
00311             break;
00312         }
00313         if(strcmp(*plEnums,value)==0){
00314             PropertyInteger::setValue(i);
00315             break;
00316         }
00317         plEnums++;
00318         i++;
00319     }
00320 }
00321 
00322 void PropertyEnumeration::setValue(long value)
00323 {
00324 # ifdef FC_DEBUG
00325     assert(value>=0 && value<5000);
00326     if(_EnumArray){
00327         const char** plEnums = _EnumArray;
00328         long i=0;
00329         while(*(plEnums++) != NULL)i++;
00330         // very unlikely to have enums with more then 5000 entries!
00331         // Note: Do NOT call assert() because this code might be executed from Python console!
00332         if ( value < 0 || i <= value )
00333             throw Base::Exception("Out of range");
00334     }
00335 # endif
00336     PropertyInteger::setValue(value);
00337 }
00338 
00340 bool PropertyEnumeration::isValue(const char* value) const
00341 {
00342     // using string methods without set, use setEnums(const char** plEnums) first!
00343     assert(_EnumArray);
00344     return strcmp(_EnumArray[getValue()],value)==0;
00345 }
00346 
00348 bool PropertyEnumeration::isPartOf(const char* value) const
00349 {
00350     // using string methods without set, use setEnums(const char** plEnums) first!
00351     assert(_EnumArray);
00352 
00353     const char** plEnums = _EnumArray;
00354 
00355     // search for the right entry
00356     while(1){
00357         // end of list?
00358         if(*plEnums==NULL) 
00359             return false;
00360         if(strcmp(*plEnums,value)==0) 
00361             return true;
00362         plEnums++;
00363     }
00364 }
00365 
00367 const char* PropertyEnumeration::getValueAsString(void) const
00368 {
00369     // using string methods without set, use setEnums(const char** plEnums) first!
00370     assert(_EnumArray);
00371     return _EnumArray[getValue()];
00372 }
00373 
00374 std::vector<std::string> PropertyEnumeration::getEnumVector(void) const
00375 {
00376     // using string methods without set, use setEnums(const char** plEnums) first!
00377     assert(_EnumArray);
00378 
00379     std::vector<std::string> result;
00380     const char** plEnums = _EnumArray;
00381 
00382     // end of list?
00383     while(*plEnums!=NULL){ 
00384         result.push_back(*plEnums);
00385         plEnums++;
00386     }
00387 
00388     return result;
00389 }
00390 
00391 void PropertyEnumeration::setEnumVector(const std::vector<std::string>& values)
00392 {
00393     delete [] _EnumArray;
00394     _EnumArray = new const char*[values.size()+1];
00395     int i=0;
00396     for (std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) {
00397 #if defined (_MSC_VER)
00398         _EnumArray[i++] = _strdup(it->c_str());
00399 #else
00400         _EnumArray[i++] = strdup(it->c_str());
00401 #endif
00402     }
00403 
00404     _EnumArray[i] = 0; // null termination
00405 }
00406 
00407 const char** PropertyEnumeration::getEnums(void) const
00408 {
00409     return _EnumArray;
00410 }
00411 
00412 void PropertyEnumeration::Save(Base::Writer &writer) const
00413 {
00414     writer.Stream() << writer.ind() << "<Integer value=\"" <<  _lValue <<"\"";
00415     if (_CustomEnum)
00416         writer.Stream() << " CustomEnum=\"true\"";
00417     writer.Stream() << "/>" << std::endl;
00418     if (_CustomEnum) {
00419         std::vector<std::string> items = getEnumVector();
00420         writer.Stream() << writer.ind() << "<CustomEnumList count=\"" <<  items.size() <<"\">" << endl;
00421         writer.incInd();
00422         for(std::vector<std::string>::iterator it = items.begin(); it != items.end(); ++it) {
00423             std::string val = encodeAttribute(*it);
00424             writer.Stream() << writer.ind() << "<Enum value=\"" <<  val <<"\"/>" << endl;
00425         }
00426         writer.decInd();
00427         writer.Stream() << writer.ind() << "</CustomEnumList>" << endl;
00428     }
00429 }
00430 
00431 void PropertyEnumeration::Restore(Base::XMLReader &reader)
00432 {
00433     // read my Element
00434     reader.readElement("Integer");
00435     // get the value of my Attribute
00436     long val = reader.getAttributeAsInteger("value");
00437 
00438     if (reader.hasAttribute("CustomEnum")) {
00439         reader.readElement("CustomEnumList");
00440         int count = reader.getAttributeAsInteger("count");
00441         std::vector<std::string> values(count);
00442         for(int i = 0; i < count; i++) {
00443             reader.readElement("Enum");
00444             values[i] = reader.getAttribute("value");
00445         }
00446 
00447         reader.readEndElement("CustomEnumList");
00448 
00449         _CustomEnum = true;
00450         setEnumVector(values);
00451     }
00452 
00453     setValue(val);
00454 }
00455 
00456 PyObject *PropertyEnumeration::getPyObject(void)
00457 {
00458     if (!_EnumArray) {
00459         PyErr_SetString(PyExc_AssertionError, "The enum is empty");
00460         return 0;
00461     }
00462 
00463     return Py_BuildValue("s", getValueAsString());
00464 }
00465 
00466 void PropertyEnumeration::setPyObject(PyObject *value)
00467 { 
00468     if (PyInt_Check(value)) {
00469         long val = PyInt_AsLong(value);
00470         if(_EnumArray){
00471             const char** plEnums = _EnumArray;
00472             long i=0;
00473             while(*(plEnums++) != NULL)i++;
00474             if (val < 0 || i <= val)
00475                 throw Py::ValueError("Out of range");
00476             PropertyInteger::setValue(val);
00477         }
00478     }
00479     else if (PyString_Check(value)) {
00480         const char* str = PyString_AsString (value);
00481         if (isPartOf(str))
00482             setValue(PyString_AsString (value));
00483         else
00484             throw Py::ValueError("not a member of the enum");
00485     }
00486     else if (PyList_Check(value)) {
00487         Py_ssize_t nSize = PyList_Size(value);
00488         std::vector<std::string> values;
00489         values.resize(nSize);
00490 
00491         for (Py_ssize_t i=0; i<nSize;++i) {
00492             PyObject* item = PyList_GetItem(value, i);
00493             if (!PyString_Check(item)) {
00494                 std::string error = std::string("type in list must be str, not ");
00495                 error += item->ob_type->tp_name;
00496                 throw Py::TypeError(error);
00497             }
00498             values[i] = PyString_AsString(item);
00499         }
00500 
00501         _CustomEnum = true;
00502         setEnumVector(values);
00503         setValue((long)0);
00504     }
00505     else {
00506         std::string error = std::string("type must be int or str, not ");
00507         error += value->ob_type->tp_name;
00508         throw Py::TypeError(error);
00509     }
00510 }
00511 
00512 //**************************************************************************
00513 //**************************************************************************
00514 // PropertyIntegerConstraint
00515 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00516 
00517 TYPESYSTEM_SOURCE(App::PropertyIntegerConstraint, App::PropertyInteger);
00518 
00519 //**************************************************************************
00520 // Construction/Destruction
00521 
00522 
00523 PropertyIntegerConstraint::PropertyIntegerConstraint()
00524   : _ConstStruct(0)
00525 {
00526 
00527 }
00528 
00529 
00530 PropertyIntegerConstraint::~PropertyIntegerConstraint()
00531 {
00532 
00533 }
00534 
00535 void PropertyIntegerConstraint::setConstraints(const Constraints* sConstrain)
00536 {
00537     _ConstStruct = sConstrain;
00538 }
00539 
00540 const PropertyIntegerConstraint::Constraints*  PropertyIntegerConstraint::getConstraints(void) const
00541 {
00542     return _ConstStruct;
00543 }
00544 
00545 void PropertyIntegerConstraint::setPyObject(PyObject *value)
00546 { 
00547     if (PyInt_Check(value)) {
00548         long temp = PyInt_AsLong(value);
00549         if (_ConstStruct) {
00550             if (temp > _ConstStruct->UpperBound)
00551                 temp = _ConstStruct->UpperBound;
00552             else if(temp < _ConstStruct->LowerBound)
00553                 temp = _ConstStruct->LowerBound;
00554         }
00555         aboutToSetValue();
00556         _lValue = temp;
00557         hasSetValue();
00558     } 
00559     else {
00560         std::string error = std::string("type must be int, not ");
00561         error += value->ob_type->tp_name;
00562         throw Py::TypeError(error);
00563     }
00564 }
00565 
00566 //**************************************************************************
00567 //**************************************************************************
00568 // PropertyPercent
00569 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00570 
00571 TYPESYSTEM_SOURCE(App::PropertyPercent , App::PropertyIntegerConstraint);
00572 
00573 const PropertyIntegerConstraint::Constraints percent = {0,100,1};
00574 
00575 //**************************************************************************
00576 // Construction/Destruction
00577 
00578 
00579 PropertyPercent::PropertyPercent()
00580 {
00581     _ConstStruct = &percent;
00582 }
00583 
00584 PropertyPercent::~PropertyPercent()
00585 {
00586 }
00587 
00588 //**************************************************************************
00589 //**************************************************************************
00590 // PropertyIntegerList
00591 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00592 
00593 TYPESYSTEM_SOURCE(App::PropertyIntegerList , App::PropertyLists);
00594 
00595 //**************************************************************************
00596 // Construction/Destruction
00597 
00598 
00599 PropertyIntegerList::PropertyIntegerList()
00600 {
00601 
00602 }
00603 
00604 PropertyIntegerList::~PropertyIntegerList()
00605 {
00606 
00607 }
00608 
00609 void PropertyIntegerList::setSize(int newSize)
00610 {
00611     _lValueList.resize(newSize);
00612 }
00613 
00614 int PropertyIntegerList::getSize(void) const
00615 {
00616     return static_cast<int>(_lValueList.size());
00617 }
00618 
00619 //**************************************************************************
00620 // Base class implementer
00621 
00622 void PropertyIntegerList::setValue(long lValue)
00623 {
00624     aboutToSetValue();
00625     _lValueList.resize(1);
00626     _lValueList[0]=lValue;
00627     hasSetValue();
00628 }
00629 
00630 void PropertyIntegerList::setValues(const std::vector<long>& values)
00631 {
00632     aboutToSetValue();
00633     _lValueList = values;
00634     hasSetValue();
00635 }
00636 
00637 PyObject *PropertyIntegerList::getPyObject(void)
00638 {
00639     PyObject* list = PyList_New(getSize());
00640     for(int i = 0;i<getSize(); i++)
00641         PyList_SetItem( list, i, PyInt_FromLong(_lValueList[i]));
00642     return list;
00643 }
00644 
00645 void PropertyIntegerList::setPyObject(PyObject *value)
00646 { 
00647     if (PyList_Check(value)) {
00648         Py_ssize_t nSize = PyList_Size(value);
00649         std::vector<long> values;
00650         values.resize(nSize);
00651 
00652         for (Py_ssize_t i=0; i<nSize;++i) {
00653             PyObject* item = PyList_GetItem(value, i);
00654             if (!PyInt_Check(item)) {
00655                 std::string error = std::string("type in list must be int, not ");
00656                 error += item->ob_type->tp_name;
00657                 throw Py::TypeError(error);
00658             }
00659             values[i] = PyInt_AsLong(item);
00660         }
00661 
00662         setValues(values);
00663     }
00664     else if (PyInt_Check(value)) {
00665         setValue(PyInt_AsLong(value));
00666     }
00667     else {
00668         std::string error = std::string("type must be int or list of int, not ");
00669         error += value->ob_type->tp_name;
00670         throw Py::TypeError(error);
00671     }
00672 }
00673 
00674 void PropertyIntegerList::Save (Base::Writer &writer) const
00675 {
00676     writer.Stream() << writer.ind() << "<IntegerList count=\"" <<  getSize() <<"\">" << endl;
00677     writer.incInd();
00678     for(int i = 0;i<getSize(); i++)
00679         writer.Stream() << writer.ind() << "<I v=\"" <<  _lValueList[i] <<"\"/>" << endl; ;
00680     writer.decInd();
00681     writer.Stream() << writer.ind() << "</IntegerList>" << endl ;
00682 }
00683 
00684 void PropertyIntegerList::Restore(Base::XMLReader &reader)
00685 {
00686     // read my Element
00687     reader.readElement("IntegerList");
00688     // get the value of my Attribute
00689     int count = reader.getAttributeAsInteger("count");
00690     
00691     std::vector<long> values(count);
00692     for(int i = 0; i < count; i++) {
00693         reader.readElement("I");
00694         values[i] = reader.getAttributeAsInteger("v");
00695     }
00696     
00697     reader.readEndElement("IntegerList");
00698 
00699     //assignment
00700     setValues(values);
00701 }
00702 
00703 Property *PropertyIntegerList::Copy(void) const
00704 {
00705     PropertyIntegerList *p= new PropertyIntegerList();
00706     p->_lValueList = _lValueList;
00707     return p;
00708 }
00709 
00710 void PropertyIntegerList::Paste(const Property &from)
00711 {
00712     aboutToSetValue();
00713     _lValueList = dynamic_cast<const PropertyIntegerList&>(from)._lValueList;
00714     hasSetValue();
00715 }
00716 
00717 unsigned int PropertyIntegerList::getMemSize (void) const
00718 {
00719     return static_cast<unsigned int>(_lValueList.size() * sizeof(long));
00720 }
00721 
00722 //**************************************************************************
00723 //**************************************************************************
00724 // PropertyFloat
00725 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00726 
00727 TYPESYSTEM_SOURCE(App::PropertyFloat , App::Property);
00728 
00729 //**************************************************************************
00730 // Construction/Destruction
00731 
00732 
00733 PropertyFloat::PropertyFloat()
00734 {
00735     _dValue = 0.0;
00736 }
00737 
00738 PropertyFloat::~PropertyFloat()
00739 {
00740 
00741 }
00742 
00743 //**************************************************************************
00744 // Base class implementer
00745 
00746 void PropertyFloat::setValue(float lValue)
00747 {
00748     aboutToSetValue();
00749     _dValue=lValue;
00750     hasSetValue();
00751 }
00752 
00753 float PropertyFloat::getValue(void) const
00754 {
00755     return _dValue;
00756 }
00757 
00758 PyObject *PropertyFloat::getPyObject(void)
00759 {
00760     return Py_BuildValue("d", _dValue);
00761 }
00762 
00763 void PropertyFloat::setPyObject(PyObject *value)
00764 {
00765     if (PyFloat_Check(value)) {
00766         aboutToSetValue();
00767         _dValue = (float) PyFloat_AsDouble(value);
00768         hasSetValue();
00769     }
00770     else if(PyInt_Check(value)) {
00771         aboutToSetValue();
00772         _dValue = (float) PyInt_AsLong(value);
00773         hasSetValue();
00774     }
00775     else {
00776         std::string error = std::string("type must be float or int, not ");
00777         error += value->ob_type->tp_name;
00778         throw Py::TypeError(error);
00779     }
00780 }
00781 
00782 void PropertyFloat::Save (Base::Writer &writer) const
00783 {
00784     writer.Stream() << writer.ind() << "<Float value=\"" <<  _dValue <<"\"/>" << std::endl;
00785 }
00786 
00787 void PropertyFloat::Restore(Base::XMLReader &reader)
00788 {
00789     // read my Element
00790     reader.readElement("Float");
00791     // get the value of my Attribute
00792     setValue((float)reader.getAttributeAsFloat("value"));
00793 }
00794 
00795 Property *PropertyFloat::Copy(void) const
00796 {
00797     PropertyFloat *p= new PropertyFloat();
00798     p->_dValue = _dValue;
00799     return p;
00800 }
00801 
00802 void PropertyFloat::Paste(const Property &from)
00803 {
00804     aboutToSetValue();
00805     _dValue = dynamic_cast<const PropertyFloat&>(from)._dValue;
00806     hasSetValue();
00807 }
00808 
00809 //**************************************************************************
00810 //**************************************************************************
00811 // PropertyFloatConstraint
00812 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00813 
00814 TYPESYSTEM_SOURCE(App::PropertyFloatConstraint, App::PropertyFloat);
00815 
00816 //**************************************************************************
00817 // Construction/Destruction
00818 
00819 
00820 PropertyFloatConstraint::PropertyFloatConstraint()
00821   : _ConstStruct(0)
00822 {
00823 
00824 }
00825 
00826 PropertyFloatConstraint::~PropertyFloatConstraint()
00827 {
00828 
00829 }
00830 
00831 void PropertyFloatConstraint::setConstraints(const Constraints* sConstrain)
00832 {
00833     _ConstStruct = sConstrain;
00834 }
00835 
00836 const PropertyFloatConstraint::Constraints*  PropertyFloatConstraint::getConstraints(void) const
00837 {
00838     return _ConstStruct;
00839 }
00840 
00841 void PropertyFloatConstraint::setPyObject(PyObject *value)
00842 { 
00843     if (PyFloat_Check(value)) {
00844         float temp = (float)PyFloat_AsDouble(value);
00845         if (_ConstStruct) {
00846             if (temp > _ConstStruct->UpperBound)
00847                 temp = _ConstStruct->UpperBound;
00848             else if (temp < _ConstStruct->LowerBound)
00849                 temp = _ConstStruct->LowerBound;
00850         }
00851     
00852         aboutToSetValue();
00853         _dValue = temp;
00854         hasSetValue();
00855     } 
00856     else if (PyInt_Check(value)) {
00857         float temp = (float)PyInt_AsLong(value);
00858         if (_ConstStruct) {
00859             if (temp > _ConstStruct->UpperBound)
00860                 temp = _ConstStruct->UpperBound;
00861             else if (temp < _ConstStruct->LowerBound)
00862                 temp = _ConstStruct->LowerBound;
00863         }
00864     
00865         aboutToSetValue();
00866         _dValue = temp;
00867         hasSetValue();
00868     } 
00869     else {
00870         std::string error = std::string("type must be float, not ");
00871         error += value->ob_type->tp_name;
00872         throw Py::TypeError(error);
00873     }
00874 }
00875 
00876 
00877 //**************************************************************************
00878 // PropertyFloatList
00879 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00880 
00881 TYPESYSTEM_SOURCE(App::PropertyFloatList , App::PropertyLists);
00882 
00883 //**************************************************************************
00884 // Construction/Destruction
00885 
00886 
00887 PropertyFloatList::PropertyFloatList()
00888 {
00889 
00890 }
00891 
00892 PropertyFloatList::~PropertyFloatList()
00893 {
00894 
00895 }
00896 
00897 //**************************************************************************
00898 // Base class implementer
00899 
00900 void PropertyFloatList::setSize(int newSize)
00901 {
00902     _lValueList.resize(newSize);
00903 }
00904 
00905 int PropertyFloatList::getSize(void) const
00906 {
00907     return static_cast<int>(_lValueList.size());
00908 }
00909 
00910 void PropertyFloatList::setValue(float lValue)
00911 {
00912     aboutToSetValue();
00913     _lValueList.resize(1);
00914     _lValueList[0]=lValue;
00915     hasSetValue();
00916 }
00917 
00918 void PropertyFloatList::setValues(const std::vector<float>& values)
00919 {
00920     aboutToSetValue();
00921     _lValueList = values;
00922     hasSetValue();
00923 }
00924 
00925 PyObject *PropertyFloatList::getPyObject(void)
00926 {
00927     PyObject* list = PyList_New(getSize());
00928     for (int i = 0;i<getSize(); i++)
00929          PyList_SetItem( list, i, PyFloat_FromDouble(_lValueList[i]));
00930     return list;
00931 }
00932 
00933 void PropertyFloatList::setPyObject(PyObject *value)
00934 { 
00935     if (PyList_Check(value)) {
00936         Py_ssize_t nSize = PyList_Size(value);
00937         std::vector<float> values;
00938         values.resize(nSize);
00939 
00940         for (Py_ssize_t i=0; i<nSize;++i) {
00941             PyObject* item = PyList_GetItem(value, i);
00942             if (!PyFloat_Check(item)) {
00943                 std::string error = std::string("type in list must be float, not ");
00944                 error += item->ob_type->tp_name;
00945                 throw Py::TypeError(error);
00946             }
00947             
00948             values[i] = (float) PyFloat_AsDouble(item);
00949         }
00950 
00951         setValues(values);
00952     }
00953     else if (PyFloat_Check(value)) {
00954         setValue((float) PyFloat_AsDouble(value));
00955     } 
00956     else {
00957         std::string error = std::string("type must be float or list of float, not ");
00958         error += value->ob_type->tp_name;
00959         throw Py::TypeError(error);
00960     }
00961 }
00962 
00963 void PropertyFloatList::Save (Base::Writer &writer) const
00964 {
00965     if (writer.isForceXML()) {
00966         writer.Stream() << writer.ind() << "<FloatList count=\"" <<  getSize() <<"\">" << endl;
00967         writer.incInd();
00968         for(int i = 0;i<getSize(); i++)
00969             writer.Stream() << writer.ind() << "<F v=\"" <<  _lValueList[i] <<"\"/>" << endl; ;
00970         writer.decInd();
00971         writer.Stream() << writer.ind() <<"</FloatList>" << endl ;
00972     }
00973     else {
00974         writer.Stream() << writer.ind() << "<FloatList file=\"" << 
00975         writer.addFile(getName(), this) << "\"/>" << std::endl;
00976     }
00977 }
00978 
00979 void PropertyFloatList::Restore(Base::XMLReader &reader)
00980 {
00981     reader.readElement("FloatList");
00982     string file (reader.getAttribute("file") );
00983 
00984     if (!file.empty()) {
00985         // initate a file read
00986         reader.addFile(file.c_str(),this);
00987     }
00988 }
00989 
00990 void PropertyFloatList::SaveDocFile (Base::Writer &writer) const
00991 {
00992     Base::OutputStream str(writer.Stream());
00993     uint32_t uCt = (uint32_t)getSize();
00994     str << uCt;
00995     for (std::vector<float>::const_iterator it = _lValueList.begin(); it != _lValueList.end(); ++it) {
00996         str << *it;
00997     }
00998 }
00999 
01000 void PropertyFloatList::RestoreDocFile(Base::Reader &reader)
01001 {
01002     Base::InputStream str(reader);
01003     uint32_t uCt=0;
01004     str >> uCt;
01005     std::vector<float> values(uCt);
01006     for (std::vector<float>::iterator it = values.begin(); it != values.end(); ++it) {
01007         str >> *it;
01008     }
01009     setValues(values);
01010 }
01011 
01012 Property *PropertyFloatList::Copy(void) const
01013 {
01014     PropertyFloatList *p= new PropertyFloatList();
01015     p->_lValueList = _lValueList;
01016     return p;
01017 }
01018 
01019 void PropertyFloatList::Paste(const Property &from)
01020 {
01021     aboutToSetValue();
01022     _lValueList = dynamic_cast<const PropertyFloatList&>(from)._lValueList;
01023     hasSetValue();
01024 }
01025 
01026 unsigned int PropertyFloatList::getMemSize (void) const
01027 {
01028     return static_cast<unsigned int>(_lValueList.size() * sizeof(float));
01029 }
01030 
01031 //**************************************************************************
01032 //**************************************************************************
01033 // PropertyString
01034 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01035 
01036 TYPESYSTEM_SOURCE(App::PropertyString , App::Property);
01037 
01038 PropertyString::PropertyString()
01039 {
01040 
01041 }
01042 
01043 PropertyString::~PropertyString()
01044 {
01045 
01046 }
01047 
01048 void PropertyString::setValue(const char* sString)
01049 {
01050     if (sString) {
01051         aboutToSetValue();
01052         _cValue = sString;
01053         hasSetValue();
01054     }
01055 }
01056 
01057 void PropertyString::setValue(const std::string &sString)
01058 {
01059     aboutToSetValue();
01060     _cValue = sString;
01061     hasSetValue();
01062 }
01063 
01064 const char* PropertyString::getValue(void) const
01065 {
01066     return _cValue.c_str();
01067 }
01068 
01069 PyObject *PropertyString::getPyObject(void)
01070 {
01071     PyObject *p = PyUnicode_DecodeUTF8(_cValue.c_str(),_cValue.size(),0);
01072     if (!p) throw Base::Exception("UTF8 conversion failure at PropertyString::getPyObject()");
01073     return p;
01074 }
01075 
01076 void PropertyString::setPyObject(PyObject *value)
01077 {
01078     std::string string;
01079     if (PyUnicode_Check(value)) {
01080         PyObject* unicode = PyUnicode_AsUTF8String(value);
01081         string = PyString_AsString(unicode);
01082         Py_DECREF(unicode);
01083     }
01084     else if (PyString_Check(value)) {
01085         string = PyString_AsString(value);
01086     }
01087     else {
01088         std::string error = std::string("type must be str or unicode, not ");
01089         error += value->ob_type->tp_name;
01090         throw Py::TypeError(error);
01091     }
01092 
01093     // assign the string
01094     setValue(string);
01095 }
01096 
01097 void PropertyString::Save (Base::Writer &writer) const
01098 {
01099     std::string val = encodeAttribute(_cValue);
01100     writer.Stream() << writer.ind() << "<String value=\"" <<  val <<"\"/>" << std::endl;
01101 }
01102 
01103 void PropertyString::Restore(Base::XMLReader &reader)
01104 {
01105     // read my Element
01106     reader.readElement("String");
01107     // get the value of my Attribute
01108     setValue(reader.getAttribute("value"));
01109 }
01110 
01111 Property *PropertyString::Copy(void) const
01112 {
01113     PropertyString *p= new PropertyString();
01114     p->_cValue = _cValue;
01115     return p;
01116 }
01117 
01118 void PropertyString::Paste(const Property &from)
01119 {
01120     aboutToSetValue();
01121     _cValue = dynamic_cast<const PropertyString&>(from)._cValue;
01122     hasSetValue();
01123 }
01124 
01125 unsigned int PropertyString::getMemSize (void) const
01126 {
01127     return static_cast<unsigned int>(_cValue.size());
01128 }
01129 
01130 //**************************************************************************
01131 // PropertyFont
01132 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01133 
01134 TYPESYSTEM_SOURCE(App::PropertyFont , App::PropertyString);
01135 
01136 PropertyFont::PropertyFont()
01137 {
01138 
01139 }
01140 
01141 PropertyFont::~PropertyFont()
01142 {
01143 
01144 }
01145 
01146 //**************************************************************************
01147 // PropertyStringList
01148 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01149 
01150 TYPESYSTEM_SOURCE(App::PropertyStringList , App::PropertyLists);
01151 
01152 PropertyStringList::PropertyStringList()
01153 {
01154 
01155 }
01156 
01157 PropertyStringList::~PropertyStringList()
01158 {
01159 
01160 }
01161 
01162 //**************************************************************************
01163 // Base class implementer
01164 
01165 void PropertyStringList::setSize(int newSize)
01166 {
01167     _lValueList.resize(newSize);
01168 }
01169 
01170 int PropertyStringList::getSize(void) const
01171 {
01172     return static_cast<int>(_lValueList.size());
01173 }
01174 
01175 void PropertyStringList::setValue(const std::string& lValue)
01176 {
01177     aboutToSetValue();
01178     _lValueList.resize(1);
01179     _lValueList[0]=lValue;
01180     hasSetValue();
01181 }
01182 
01183 void PropertyStringList::setValues(const std::vector<std::string>& lValue)
01184 {
01185     aboutToSetValue();
01186     _lValueList=lValue;
01187     hasSetValue();
01188 }
01189 
01190 void PropertyStringList::setValues(const std::list<std::string>& lValue)
01191 {
01192     aboutToSetValue();
01193     _lValueList.resize(lValue.size());
01194     std::copy(lValue.begin(), lValue.end(), _lValueList.begin());
01195     hasSetValue();
01196 }
01197 
01198 PyObject *PropertyStringList::getPyObject(void)
01199 {
01200     PyObject* list = PyList_New(getSize());
01201 
01202     for (int i = 0;i<getSize(); i++) {
01203         PyObject* item = PyUnicode_DecodeUTF8(_lValueList[i].c_str(), _lValueList[i].size(), 0);
01204         if (!item) {
01205             Py_DECREF(list);
01206             throw Base::Exception("UTF8 conversion failure at PropertyStringList::getPyObject()");
01207         }
01208         PyList_SetItem(list, i, item);
01209     }
01210 
01211     return list;
01212 }
01213 
01214 void PropertyStringList::setPyObject(PyObject *value)
01215 {
01216     if (PyList_Check(value)) {
01217         Py_ssize_t nSize = PyList_Size(value);
01218         std::vector<std::string> values;
01219         values.resize(nSize);
01220 
01221         for (Py_ssize_t i=0; i<nSize;++i) {
01222             PyObject* item = PyList_GetItem(value, i);
01223             if (PyUnicode_Check(item)) {
01224                 PyObject* unicode = PyUnicode_AsUTF8String(item);
01225                 values[i] = PyString_AsString(unicode);
01226                 Py_DECREF(unicode);
01227             }
01228             else if (PyString_Check(item)) {
01229                 values[i] = PyString_AsString(item);
01230             }
01231             else {
01232                 std::string error = std::string("type in list must be str or unicode, not ");
01233                 error += item->ob_type->tp_name;
01234                 throw Py::TypeError(error);
01235             }
01236         }
01237         
01238         setValues(values);
01239     }
01240     else if (PyString_Check(value)) {
01241         setValue(PyString_AsString(value));
01242     }
01243     else {
01244         std::string error = std::string("type must be str or list of str, not ");
01245         error += value->ob_type->tp_name;
01246         throw Py::TypeError(error);
01247     }
01248 }
01249 
01250 unsigned int PropertyStringList::getMemSize (void) const
01251 {
01252     size_t size=0;
01253     for(int i = 0;i<getSize(); i++) 
01254         size += _lValueList[i].size();
01255     return static_cast<unsigned int>(size);
01256 }
01257 
01258 void PropertyStringList::Save (Base::Writer &writer) const
01259 {
01260     writer.Stream() << writer.ind() << "<StringList count=\"" <<  getSize() <<"\">" << endl;
01261     writer.incInd();
01262     for(int i = 0;i<getSize(); i++) {
01263         std::string val = encodeAttribute(_lValueList[i]);
01264         writer.Stream() << writer.ind() << "<String value=\"" <<  val <<"\"/>" << endl;
01265     }
01266     writer.decInd();
01267     writer.Stream() << writer.ind() << "</StringList>" << endl ;
01268 }
01269 
01270 void PropertyStringList::Restore(Base::XMLReader &reader)
01271 {
01272     // read my Element
01273     reader.readElement("StringList");
01274     // get the value of my Attribute
01275     int count = reader.getAttributeAsInteger("count");
01276 
01277     std::vector<std::string> values(count);
01278     for(int i = 0; i < count; i++) {
01279         reader.readElement("String");
01280         values[i] = reader.getAttribute("value");
01281     }
01282     
01283     reader.readEndElement("StringList");
01284 
01285     // assignment
01286     setValues(values);
01287 }
01288 
01289 Property *PropertyStringList::Copy(void) const
01290 {
01291     PropertyStringList *p= new PropertyStringList();
01292     p->_lValueList = _lValueList;
01293     return p;
01294 }
01295 
01296 void PropertyStringList::Paste(const Property &from)
01297 {
01298     aboutToSetValue();
01299     _lValueList = dynamic_cast<const PropertyStringList&>(from)._lValueList;
01300     hasSetValue();
01301 }
01302 
01303 //**************************************************************************
01304 //**************************************************************************
01305 // PropertyBool
01306 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01307 
01308 TYPESYSTEM_SOURCE(App::PropertyBool , App::Property);
01309 
01310 //**************************************************************************
01311 // Construction/Destruction
01312 
01313 PropertyBool::PropertyBool()
01314 {
01315     _lValue = false;
01316 }
01317 
01318 PropertyBool::~PropertyBool()
01319 {
01320 
01321 }
01322 
01323 //**************************************************************************
01324 // Setter/getter for the property
01325 
01326 void PropertyBool::setValue(bool lValue)
01327 {
01328     aboutToSetValue();
01329     _lValue=lValue;
01330     hasSetValue();
01331 }
01332 
01333 bool PropertyBool::getValue(void) const
01334 {
01335     return _lValue;
01336 }
01337 
01338 PyObject *PropertyBool::getPyObject(void)
01339 {
01340     if (_lValue)
01341         {Py_INCREF(Py_True); return Py_True;}
01342     else
01343         {Py_INCREF(Py_False); return Py_False;}
01344 
01345 }
01346 
01347 void PropertyBool::setPyObject(PyObject *value)
01348 {
01349     if (PyBool_Check(value))
01350         setValue(value == Py_True);
01351     else if(PyInt_Check(value))
01352         setValue(PyInt_AsLong(value)!=0);
01353     else {
01354         std::string error = std::string("type must be bool, not ");
01355         error += value->ob_type->tp_name;
01356         throw Py::TypeError(error);
01357     }
01358 }
01359 
01360 void PropertyBool::Save (Base::Writer &writer) const
01361 {
01362     writer.Stream() << writer.ind() << "<Bool value=\"" ;
01363     if (_lValue)
01364         writer.Stream() << "true" <<"\"/>" ;
01365     else
01366         writer.Stream() << "false" <<"\"/>" ;
01367     writer.Stream() << std::endl;
01368 }
01369 
01370 void PropertyBool::Restore(Base::XMLReader &reader)
01371 {
01372     // read my Element
01373     reader.readElement("Bool");
01374     // get the value of my Attribute
01375     string b = reader.getAttribute("value");
01376     (b == "true") ? setValue(true) : setValue(false);
01377 }
01378 
01379 
01380 Property *PropertyBool::Copy(void) const
01381 {
01382     PropertyBool *p= new PropertyBool();
01383     p->_lValue = _lValue;
01384     return p;
01385 }
01386 
01387 void PropertyBool::Paste(const Property &from)
01388 {
01389     aboutToSetValue();
01390     _lValue = dynamic_cast<const PropertyBool&>(from)._lValue;
01391     hasSetValue();
01392 }
01393 
01394 //**************************************************************************
01395 //**************************************************************************
01396 // PropertyColor
01397 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01398 
01399 TYPESYSTEM_SOURCE(App::PropertyColor , App::Property);
01400 
01401 //**************************************************************************
01402 // Construction/Destruction
01403 
01404 PropertyColor::PropertyColor()
01405 {
01406 
01407 }
01408 
01409 PropertyColor::~PropertyColor()
01410 {
01411 
01412 }
01413 
01414 //**************************************************************************
01415 // Base class implementer
01416 
01417 void PropertyColor::setValue(const Color &col)
01418 {
01419     aboutToSetValue();
01420     _cCol=col;
01421     hasSetValue();
01422 }
01423 
01424 void PropertyColor::setValue(uint32_t rgba)
01425 {
01426     aboutToSetValue();
01427     _cCol.setPackedValue(rgba);
01428     hasSetValue();
01429 }
01430 
01431 void PropertyColor::setValue(float r, float g, float b, float a)
01432 {
01433     aboutToSetValue();
01434     _cCol.set(r,g,b,a);
01435     hasSetValue();
01436 }
01437 
01438 const Color& PropertyColor::getValue(void) const 
01439 {
01440     return _cCol;
01441 }
01442 
01443 PyObject *PropertyColor::getPyObject(void)
01444 {
01445     PyObject* rgba = PyTuple_New(4);
01446     PyObject* r = PyFloat_FromDouble(_cCol.r);
01447     PyObject* g = PyFloat_FromDouble(_cCol.g);
01448     PyObject* b = PyFloat_FromDouble(_cCol.b);
01449     PyObject* a = PyFloat_FromDouble(_cCol.a);
01450 
01451     PyTuple_SetItem(rgba, 0, r);
01452     PyTuple_SetItem(rgba, 1, g);
01453     PyTuple_SetItem(rgba, 2, b);
01454     PyTuple_SetItem(rgba, 3, a);
01455 
01456     return rgba;
01457 }
01458 
01459 void PropertyColor::setPyObject(PyObject *value)
01460 {
01461     App::Color cCol;
01462     if (PyTuple_Check(value) && PyTuple_Size(value) == 3) {
01463         PyObject* item;
01464         item = PyTuple_GetItem(value,0);
01465         if (PyFloat_Check(item))
01466             cCol.r = (float)PyFloat_AsDouble(item);
01467         else
01468             throw Base::Exception("Type in tuple must be float");
01469         item = PyTuple_GetItem(value,1);
01470         if (PyFloat_Check(item))
01471             cCol.g = (float)PyFloat_AsDouble(item);
01472         else
01473             throw Base::Exception("Type in tuple must be float");
01474         item = PyTuple_GetItem(value,2);
01475         if (PyFloat_Check(item))
01476             cCol.b = (float)PyFloat_AsDouble(item);
01477         else
01478             throw Base::Exception("Type in tuple must be float");
01479     }
01480     else if (PyTuple_Check(value) && PyTuple_Size(value) == 4) {
01481         PyObject* item;
01482         item = PyTuple_GetItem(value,0);
01483         if (PyFloat_Check(item))
01484             cCol.r = (float)PyFloat_AsDouble(item);
01485         else
01486             throw Base::Exception("Type in tuple must be float");
01487         item = PyTuple_GetItem(value,1);
01488         if (PyFloat_Check(item))
01489             cCol.g = (float)PyFloat_AsDouble(item);
01490         else
01491             throw Base::Exception("Type in tuple must be float");
01492         item = PyTuple_GetItem(value,2);
01493         if (PyFloat_Check(item))
01494             cCol.b = (float)PyFloat_AsDouble(item);
01495         else
01496             throw Base::Exception("Type in tuple must be float");
01497         item = PyTuple_GetItem(value,3);
01498         if (PyFloat_Check(item))
01499             cCol.a = (float)PyFloat_AsDouble(item);
01500         else
01501             throw Base::Exception("Type in tuple must be float");
01502     }
01503     else if (PyLong_Check(value)) {
01504         cCol.setPackedValue(PyLong_AsUnsignedLong(value));
01505     }
01506     else {
01507         std::string error = std::string("type must be int or tuple of float, not ");
01508         error += value->ob_type->tp_name;
01509         throw Py::TypeError(error);
01510     }
01511 
01512     setValue( cCol );
01513 }
01514 
01515 void PropertyColor::Save (Base::Writer &writer) const
01516 {
01517     writer.Stream() << writer.ind() << "<PropertyColor value=\"" 
01518     <<  _cCol.getPackedValue() <<"\"/>" << endl;
01519 }
01520 
01521 void PropertyColor::Restore(Base::XMLReader &reader)
01522 {
01523     // read my Element
01524     reader.readElement("PropertyColor");
01525     // get the value of my Attribute
01526     unsigned long rgba = reader.getAttributeAsUnsigned("value");
01527     setValue(rgba);
01528 }
01529 
01530 Property *PropertyColor::Copy(void) const
01531 {
01532     PropertyColor *p= new PropertyColor();
01533     p->_cCol = _cCol;
01534     return p;
01535 }
01536 
01537 void PropertyColor::Paste(const Property &from)
01538 {
01539     aboutToSetValue();
01540     _cCol = dynamic_cast<const PropertyColor&>(from)._cCol;
01541     hasSetValue();
01542 }
01543 
01544 //**************************************************************************
01545 // PropertyColorList
01546 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01547 
01548 TYPESYSTEM_SOURCE(App::PropertyColorList , App::PropertyLists);
01549 
01550 //**************************************************************************
01551 // Construction/Destruction
01552 
01553 PropertyColorList::PropertyColorList()
01554 {
01555 
01556 }
01557 
01558 PropertyColorList::~PropertyColorList()
01559 {
01560 
01561 }
01562 
01563 //**************************************************************************
01564 // Base class implementer
01565 
01566 void PropertyColorList::setSize(int newSize)
01567 {
01568     _lValueList.resize(newSize);
01569 }
01570 
01571 int PropertyColorList::getSize(void) const
01572 {
01573     return static_cast<int>(_lValueList.size());
01574 }
01575 
01576 void PropertyColorList::setValue(const Color& lValue)
01577 {
01578     aboutToSetValue();
01579     _lValueList.resize(1);
01580     _lValueList[0]=lValue;
01581     hasSetValue();
01582 }
01583 
01584 void PropertyColorList::setValues (const std::vector<Color>& values)
01585 {
01586     aboutToSetValue();
01587     _lValueList=values;
01588     hasSetValue();
01589 }
01590 
01591 PyObject *PropertyColorList::getPyObject(void)
01592 {
01593     PyObject* list = PyList_New(getSize());
01594 
01595     for(int i = 0;i<getSize(); i++) {
01596         PyObject* rgba = PyTuple_New(4);
01597         PyObject* r = PyFloat_FromDouble(_lValueList[i].r);
01598         PyObject* g = PyFloat_FromDouble(_lValueList[i].g);
01599         PyObject* b = PyFloat_FromDouble(_lValueList[i].b);
01600         PyObject* a = PyFloat_FromDouble(_lValueList[i].a);
01601 
01602         PyTuple_SetItem(rgba, 0, r);
01603         PyTuple_SetItem(rgba, 1, g);
01604         PyTuple_SetItem(rgba, 2, b);
01605         PyTuple_SetItem(rgba, 3, a);
01606 
01607         PyList_SetItem( list, i, rgba );
01608     }
01609 
01610     return list;
01611 }
01612 
01613 void PropertyColorList::setPyObject(PyObject *value)
01614 {
01615     if (PyList_Check(value)) {
01616         Py_ssize_t nSize = PyList_Size(value);
01617         std::vector<Color> values;
01618         values.resize(nSize);
01619 
01620         for (Py_ssize_t i=0; i<nSize;++i) {
01621             PyObject* item = PyList_GetItem(value, i);
01622             PropertyColor col;
01623             col.setPyObject(item);
01624             values[i] = col.getValue();
01625         }
01626 
01627         setValues(values);
01628     }
01629     else if (PyTuple_Check(value) && PyTuple_Size(value) == 3) {
01630         PropertyColor col;
01631         col.setPyObject( value );
01632         setValue( col.getValue() );
01633     }
01634     else if (PyTuple_Check(value) && PyTuple_Size(value) == 4) {
01635         PropertyColor col;
01636         col.setPyObject( value );
01637         setValue( col.getValue() );
01638     }
01639     else {
01640         std::string error = std::string("not allowed type, ");
01641         error += value->ob_type->tp_name;
01642         throw Py::TypeError(error);
01643     }
01644 }
01645 
01646 void PropertyColorList::Save (Base::Writer &writer) const
01647 {
01648     if (!writer.isForceXML()) {
01649         writer.Stream() << writer.ind() << "<ColorList file=\"" << writer.addFile(getName(), this) << "\"/>" << std::endl;
01650     }
01651 }
01652 
01653 void PropertyColorList::Restore(Base::XMLReader &reader)
01654 {
01655     reader.readElement("ColorList");
01656     if (reader.hasAttribute("file")) {
01657         std::string file (reader.getAttribute("file"));
01658 
01659         if (!file.empty()) {
01660             // initate a file read
01661             reader.addFile(file.c_str(),this);
01662         }
01663     }
01664 }
01665 
01666 void PropertyColorList::SaveDocFile (Base::Writer &writer) const
01667 {
01668     Base::OutputStream str(writer.Stream());
01669     uint32_t uCt = (uint32_t)getSize();
01670     str << uCt;
01671     for (std::vector<App::Color>::const_iterator it = _lValueList.begin(); it != _lValueList.end(); ++it) {
01672         str << it->getPackedValue();
01673     }
01674 }
01675 
01676 void PropertyColorList::RestoreDocFile(Base::Reader &reader)
01677 {
01678     Base::InputStream str(reader);
01679     uint32_t uCt=0;
01680     str >> uCt;
01681     std::vector<Color> values(uCt);
01682     uint32_t value; // must be 32 bit long
01683     for (std::vector<App::Color>::iterator it = values.begin(); it != values.end(); ++it) {
01684         str >> value;
01685         it->setPackedValue(value);
01686     }
01687     setValues(values);
01688 }
01689 
01690 Property *PropertyColorList::Copy(void) const
01691 {
01692     PropertyColorList *p= new PropertyColorList();
01693     p->_lValueList = _lValueList;
01694     return p;
01695 }
01696 
01697 void PropertyColorList::Paste(const Property &from)
01698 {
01699     aboutToSetValue();
01700     _lValueList = dynamic_cast<const PropertyColorList&>(from)._lValueList;
01701     hasSetValue();
01702 }
01703 
01704 unsigned int PropertyColorList::getMemSize (void) const
01705 {
01706     return static_cast<unsigned int>(_lValueList.size() * sizeof(Color));
01707 }
01708 
01709 //**************************************************************************
01710 //**************************************************************************
01711 // PropertyMaterial
01712 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
01713 
01714 TYPESYSTEM_SOURCE(App::PropertyMaterial , App::Property);
01715 
01716 PropertyMaterial::PropertyMaterial()
01717 {
01718 
01719 }
01720 
01721 PropertyMaterial::~PropertyMaterial()
01722 {
01723 
01724 }
01725 
01726 void PropertyMaterial::setValue(const Material &mat)
01727 {
01728     aboutToSetValue();
01729     _cMat=mat;
01730     hasSetValue();
01731 }
01732 
01733 const Material& PropertyMaterial::getValue(void) const 
01734 {
01735     return _cMat;
01736 }
01737 
01738 void PropertyMaterial::setAmbientColor(const Color& col)
01739 {
01740     aboutToSetValue();
01741     _cMat.ambientColor = col;
01742     hasSetValue();
01743 }
01744 
01745 void PropertyMaterial::setDiffuseColor(const Color& col)
01746 {
01747     aboutToSetValue();
01748     _cMat.diffuseColor = col;
01749     hasSetValue();
01750 }
01751 
01752 void PropertyMaterial::setSpecularColor(const Color& col)
01753 {
01754     aboutToSetValue();
01755     _cMat.specularColor = col;
01756     hasSetValue();
01757 }
01758 
01759 void PropertyMaterial::setEmmisiveColor(const Color& col)
01760 {
01761     aboutToSetValue();
01762     _cMat.emissiveColor = col;
01763     hasSetValue();
01764 }
01765 
01766 void PropertyMaterial::setShininess(float val)
01767 {
01768     aboutToSetValue();
01769     _cMat.shininess = val;
01770     hasSetValue();
01771 }
01772 
01773 void PropertyMaterial::setTransparency(float val)
01774 {
01775     aboutToSetValue();
01776     _cMat.transparency = val;
01777     hasSetValue();
01778 }
01779 
01780 PyObject *PropertyMaterial::getPyObject(void)
01781 {
01782     return new MaterialPy(new Material(_cMat));
01783 }
01784 
01785 void PropertyMaterial::setPyObject(PyObject *value)
01786 {
01787     if (PyObject_TypeCheck(value, &(MaterialPy::Type))) {
01788         setValue(*static_cast<MaterialPy*>(value)->getMaterialPtr());
01789     }
01790     else {
01791         std::string error = std::string("type must be 'Material', not ");
01792         error += value->ob_type->tp_name;
01793         throw Py::TypeError(error);
01794     }
01795 }
01796 
01797 void PropertyMaterial::Save (Base::Writer &writer) const
01798 {
01799     writer.Stream() << writer.ind() << "<PropertyMaterial ambientColor=\"" 
01800         <<  _cMat.ambientColor.getPackedValue() 
01801         << "\" diffuseColor=\"" <<  _cMat.diffuseColor.getPackedValue() 
01802         << "\" specularColor=\"" <<  _cMat.specularColor.getPackedValue()
01803         << "\" emissiveColor=\"" <<  _cMat.emissiveColor.getPackedValue()
01804         << "\" shininess=\"" <<  _cMat.shininess << "\" transparency=\"" 
01805         <<  _cMat.transparency << "\"/>" << endl;
01806 }
01807 
01808 void PropertyMaterial::Restore(Base::XMLReader &reader)
01809 {
01810     // read my Element
01811     reader.readElement("PropertyMaterial");
01812     // get the value of my Attribute
01813     aboutToSetValue();
01814     _cMat.ambientColor.setPackedValue(reader.getAttributeAsUnsigned("ambientColor"));
01815     _cMat.diffuseColor.setPackedValue(reader.getAttributeAsUnsigned("diffuseColor"));
01816     _cMat.specularColor.setPackedValue(reader.getAttributeAsUnsigned("specularColor"));
01817     _cMat.emissiveColor.setPackedValue(reader.getAttributeAsUnsigned("emissiveColor"));
01818     _cMat.shininess = (float)reader.getAttributeAsFloat("shininess");
01819     _cMat.transparency = (float)reader.getAttributeAsFloat("transparency");
01820     hasSetValue();
01821 }
01822 
01823 Property *PropertyMaterial::Copy(void) const
01824 {
01825     PropertyMaterial *p= new PropertyMaterial();
01826     p->_cMat = _cMat;
01827     return p;
01828 }
01829 
01830 void PropertyMaterial::Paste(const Property &from)
01831 {
01832     aboutToSetValue();
01833     _cMat = dynamic_cast<const PropertyMaterial&>(from)._cMat;
01834     hasSetValue();
01835 }
01836 
01837 

Generated on Wed Nov 23 19:00:31 2011 for FreeCAD by  doxygen 1.6.1