[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfpobj.h
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfpobj.h
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfpobj.h 991 2017-01-04 09:15:43Z phfaist $ */
23 
24 
25 #ifndef KLFPOBJ_H
26 #define KLFPOBJ_H
27 
28 #include <QDebug>
29 #include <QVariant>
30 #include <QByteArray>
31 #include <QDataStream>
32 #include <QTextStream>
33 #include <QVector>
34 #include <QList>
35 #include <QMap>
36 #include <QStringList>
37 
38 #include <klfdefs.h>
39 
40 
41 
42 
59 {
60 public:
63 
71  virtual QString objectKind() const = 0;
72 
77  virtual QVariant property(const QString& propName) const = 0;
78 
84  virtual QStringList propertyNameList() const = 0;
85 
97  virtual bool setProperty(const QString& pname, const QVariant& value) = 0;
98 
99 
108  virtual QMap<QString,QVariant> allProperties() const;
109 
124  virtual bool setAllProperties(const QMap<QString,QVariant>& data);
125 
129  virtual bool hasFixedTypes() const { return false; }
130 
135  virtual QByteArray typeNameFor(const QString& property) const { Q_UNUSED(property); return QByteArray(); }
136 
144  virtual QByteArray typeSpecificationFor(const QString& property) const { Q_UNUSED(property); return QByteArray(); }
145 };
146 
147 
149 {
150 public:
152  virtual ~KLFSpecifyableType();
153 
154  virtual QByteArray specification() const = 0;
155  virtual bool setSpecification(const QByteArray& data) = 0;
156 };
157 
158 
160 {
161  int val;
162  QStringList enumVals;
163 public:
164  KLFEnumType(int initvalue = 0) : val(initvalue), enumVals()
165  {
166  }
167  KLFEnumType(const KLFEnumType& copy) : KLFSpecifyableType(), val(copy.val), enumVals(copy.enumVals)
168  {
169  }
170  virtual ~KLFEnumType() { }
171 
172  inline int value() const
173  {
174  return val;
175  }
176  inline void setValue(int v)
177  {
178  val = v;
179  }
180 
181  inline QString enumValue() const
182  {
183  if (val < 0 || val >= enumVals.size()) {
184  klfWarning("Invalid value: "<<val<<" for enum values "<<enumVals) ;
185  return QString();
186  }
187  return enumVals[val];
188  }
189 
190 
191  inline QStringList enumValues() const
192  {
193  return enumVals;
194  }
195  inline void setEnumValues(const QStringList& list)
196  {
197  enumVals = list;
198  }
199 
201  {
202  return enumVals.join(":").toUtf8();
203  }
204 
205  bool setSpecification(const QByteArray& data)
206  {
207  // parse enum values
208  setEnumValues(QString::fromUtf8(data).split(QRegExp(":")));
209  return true;
210  }
211 
212 };
213 
214 
216 
219 // exact matches
220 inline bool operator==(const KLFEnumType& a, const KLFEnumType& b)
221 { return a.value() == b.value(); }
222 
223 
224 
225 
227 
228 
278 {
279 public:
283  explicit KLFPropertizedObject(const QString& propertyNameSpace);
284  virtual ~KLFPropertizedObject();
285 
294  virtual QVariant property(const QString& propName) const;
304  virtual QVariant property(int propId) const;
315  virtual QVariant property(const QString& propName, const QVariant& defaultValue) const;
323  virtual bool hasPropertyValue(const QString& propName) const;
331  virtual bool hasPropertyValue(int propId) const;
332 
341  QList<int> propertyIdList() const;
342 
350 
356 
369  virtual bool setProperty(const QString& propname, const QVariant& value);
370 
376  virtual bool setProperty(int propId, const QVariant& value);
377 
387  virtual bool setAllProperties(const QMap<QString, QVariant>& propValues);
388 
389 
399  QDataStream& streamInto(QDataStream& stream) const;
400 
407  QDataStream& streamFrom(QDataStream& stream);
408 
409 
416  QByteArray allPropertiesToByteArray() const;
417 
424  void setAllPropertiesFromByteArray(const QByteArray& data);
425 
426  /*
427  / ** \brief Parses a string representation of a property
428  *
429  * Subclasses should reimplement this function to parse the actual string into a value. Note
430  * that this function should NOT set the property, it should just parse the string in a way
431  * depending on what kind of information the property \c propId is supposed to store.
432  *
433  * The formatting is left to the implementation. It could be user input, for example.
434  *
435  * The default implementation calls \ref parsePropertyValue(const QString&, const QString&) .
436  * It is thus enough for subclasses to reimplement the other method.
437  * /
438  virtual QVariant parsePropertyValue(int propId, const QString& strvalue);
439 
440  / ** \brief Parses a string representation of a property
441  *
442  * Subclasses should reimplement this function to parse the actual string into a value. Note
443  * that this function should NOT set the property, it should just parse the string in a way
444  * depending on what kind of information the property \c propName is supposed to store.
445  *
446  * The formatting is left to the implementation. It could be user input, for example.
447  *
448  * Since the base implementation has no idea what kind of information \c propId is supposed
449  * to store, the default implementation returns a null \c QVariant().
450  * /
451  virtual QVariant parsePropertyValue(const QString& propName, const QString& strvalue);
452  */
453 
454 
458  ToStringUseHtml = 0x0001,
459  ToStringUseHtmlDiv = 0x0002,
460  ToStringQuoteValues = 0x0004,
461  ToStringAllProperties = 0x0008
462  };
467  virtual QString toString(uint toStringFlags = 0) const;
468 
470 
472  int propertyMaxId() const;
474 
477  bool propertyIdRegistered(int propId) const;
479 
482  bool propertyNameRegistered(const QString& propertyName) const;
484 
487  int propertyIdForName(const QString& propertyName) const;
489 
492  QString propertyNameForId(int propId) const;
494 
497  QList<int> registeredPropertyIdList() const;
499 
502  QStringList registeredPropertyNameList() const;
504 
507  QMap<QString, int> registeredProperties() const;
508 
509  virtual QString objectKind() const { return pPropNameSpace; }
510 
511 protected:
512 
522  virtual void propertyValueChanged(int propId, const QVariant& oldValue,
523  const QVariant& newValue);
524 
525 
530  virtual bool doSetProperty(const QString& propname, const QVariant& value);
531 
536  virtual bool doSetProperty(int propId, const QVariant& value);
537 
543  virtual int doLoadProperty(const QString& propname, const QVariant& value);
544 
548  void registerBuiltInProperty(int propId, const QString& propName) const;
549 
554  int registerProperty(const QString& propertyName) const;
555 
605  static void registerBuiltInProperty(const QString& propNameSpace, int propId,
606  const QString& name);
607 
621  static int registerProperty(const QString& propNameSpace, const QString& propertyName);
622 
630  static int propertyMaxId(const QString& propNameSpace);
631 
639  static bool propertyIdRegistered(const QString& propNameSpace, int propId);
640 
648  static bool propertyNameRegistered(const QString& propNameSpace, const QString& propertyName);
649 
662  static int propertyIdForName(const QString& propNameSpace, const QString& propertyName);
663 
671  static QString propertyNameForId(const QString& propNameSpace, int propId);
672 
679  static QList<int> registeredPropertyIdList(const QString& propNameSpace);
680 
687  static QStringList registeredPropertyNameList(const QString& propNameSpace);
688 
695  static QMap<QString, int> registeredProperties(const QString& propNameSpace);
696 
697  QString propertyNameSpace() const { return pPropNameSpace; }
698 
699  QVector<QVariant> propertyVector() const { return pProperties; }
700 
701 
702  friend class KLFPObjPropRefHelper;
703 
704 private:
707 
708  QString pPropNameSpace;
709 
710  QVector<QVariant> pProperties;
711 
715  static int internalRegisterProperty(const QString& propNameSpace, const QString& name,
716  int propId = -1);
717  static QMap<QString, QMap<QString, int> > pRegisteredProperties;
718  static QMap<QString, int> pRegisteredPropertiesMaxId;
719 
720  friend bool operator==(const KLFPropertizedObject& a, const KLFPropertizedObject& b);
721 };
722 
726 bool operator==(const KLFPropertizedObject& a, const KLFPropertizedObject& b);
727 
736 
738 
740 
741 
747 {
748 protected:
749  void registerbuiltinprop(KLFPropertizedObject *obj, int propid, const QString& pname)
750  {
751  obj->registerBuiltInProperty(propid, pname);
752  }
754 };
755 
756 template<class T>
758 {
759  KLFPropertizedObject *pPObj;
760  int pPropId;
761 public:
762  typedef T Type;
763 
765  : pPObj(pobj), pPropId(propId)
766  {
767  }
769  : pPObj(other.pPObj), pPropId(other.pPropId)
770  {
771  }
778  KLFPObjPropRef(KLFPropertizedObject *pobj, int builtInPropId, const QString& pname)
779  : pPObj(pobj), pPropId(builtInPropId)
780  {
781  init(pname);
782  }
789  KLFPObjPropRef(KLFPropertizedObject *pobj, int builtInPropId, const QString& pname, const T& value)
790  : pPObj(pobj), pPropId(builtInPropId)
791  {
792  init(pname);
793  set(value);
794  }
795 
796  operator QVariant() const
797  {
798  return variantValue();
799  }
800  operator T() const
801  {
802  return value<T>();
803  }
804  const T operator ()() const
805  {
806  return value<T>();
807  }
809  {
810  pPObj->setProperty(pPropId, v);
811  return *this;
812  }
813  const KLFPObjPropRef& operator=(const T& value)
814  {
815  pPObj->setProperty(pPropId, QVariant::fromValue<T>(value));
816  return *this;
817  }
820  {
821  return this->operator=(value.value());
822  }
823 
825  {
826  return pPObj->property(pPropId);
827  }
828 
829  const T value() const
830  {
831  return value<T>();
832  }
833 
834  template<class VariantType>
835  const T value() const
836  {
837  QVariant v = pPObj->property(pPropId);
838  return T(v.value<VariantType>());
839  }
840 
841  template<class VariantType>
842  void set(const T& value)
843  {
844  pPObj->setProperty(pPropId, QVariant::fromValue<VariantType>(value));
845  }
846  void set(const T& value)
847  {
848  set<T>(value);
849  }
850 
851  template<class VariantType>
852  bool equals(const KLFPObjPropRef& other) const
853  {
854  return (value<VariantType>() == other.value<VariantType>());
855  }
856  bool equals(const KLFPObjPropRef& other) const
857  {
858  return equals<T>(other);
859  }
860 
861  bool operator==(const T& val) const
862  {
863  return (value() == val);
864  }
865  bool operator==(const KLFPObjPropRef& other) const
866  {
867  return (value() == other.value());
868  }
869 
870 private:
871  void init(const QString& pname)
872  {
873  if (!pPObj->propertyIdRegistered(pPropId)) {
874  // from our helper base class
875  registerbuiltinprop(pPObj, pPropId, pname);
876  } else {
877  // make sure the correct name was registered
878  KLF_ASSERT_CONDITION(pPObj->propertyNameForId(pPropId) == pname,
879  qPrintable(propertyNameSpace(pPObj))<<": Built-In property ID "<<pPropId
880  <<" does not have name "<<pname<<" !",
881  ; ) ;
882  }
883  }
884 
885 };
886 
887 template<typename T>
888 inline QDebug & operator<<(QDebug & str, const KLFPObjPropRef<T> & p)
889 {
890  return str << p();
891 }
892 
893 
894 
895 // ----
896 
899 public:
900  KLFPObjRegisteredType(const char *name)
901  {
902  doregister(Register, name);
903  }
904 
905  static bool isRegistered(const char *name)
906  {
907  return doregister(Query, name);
908  }
909 
910 private:
911  enum Action { Query, Register };
912  static int doregister(Action action, const char * name)
913  {
914  static QList<QByteArray> registered_types;
915  bool x;
916  switch (action) {
917  case Query: // is querying the existance of a registered type
918  x = registered_types.contains(QByteArray(name));
919  return x;
920  case Register: // want to register the given type
921  registered_types.append(QByteArray(name));
922  return 0;
923  default:
924  fprintf(stderr, "ERRORORROOERROR: %s: what is your action?? `%d' for name `%s'\n",
925  KLF_FUNC_NAME, (int)action, name);
926  }
927  return -1;
928  }
929 };
930 
936 #define KLF_DECLARE_POBJ_TYPE(TYPE) \
937  static KLFPObjRegisteredType __klf_pobj_regtype_##TYPE = KLFPObjRegisteredType(#TYPE) ;
938 
941 public:
943  {
944  doregister(Register, name);
945  }
946 
947  static bool isRegistered(const char *name)
948  {
949  return doregister(Query, name);
950  }
951 
952 private:
953  enum Action { Query, Register };
954  static int doregister(Action action, const char * name)
955  {
956  static QList<QByteArray> registered_types;
957  bool x;
958  switch (action) {
959  case Query: // is querying the existance of a registered type
960  x = registered_types.contains(QByteArray(name));
961  return x;
962  case Register: // want to register the given type
963  registered_types.append(QByteArray(name));
964  return 0;
965  default:
966  fprintf(stderr, "ERRORORROORORR: %s: what is your action?? `%d' for name `%s'\n",
967  KLF_FUNC_NAME, (int)action, name);
968  }
969  return -1;
970  }
971 };
972 
974 #define KLF_DECLARE_SPECIFYABLE_TYPE(TYPE) \
975  static KLFSpecifyableRegisteredType __klf_specifyable_regtype_##TYPE = KLFSpecifyableRegisteredType(#TYPE) ;
976 
977 
978 
979 #endif
const KLFPObjPropRef & operator=(const KLFPObjPropRef &value)
Definition: klfpobj.h:819
QString propertyNameSpace() const
Definition: klfpobj.h:697
bool operator==(const T &val) const
Definition: klfpobj.h:861
static bool isRegistered(const char *name)
Definition: klfpobj.h:905
KLF_EXPORT QDataStream & operator<<(QDataStream &stream, const KLFEnumType &e)
Definition: klfpobj.cpp:79
static bool isRegistered(const char *name)
Definition: klfpobj.h:947
virtual ~KLFEnumType()
Definition: klfpobj.h:170
KLFPObjPropRef(KLFPropertizedObject *pobj, int builtInPropId, const QString &pname, const T &value)
Definition: klfpobj.h:789
Base declarations for klatexformula and some utilities.
const KLFPObjPropRef & operator=(const T &value)
Definition: klfpobj.h:813
void setEnumValues(const QStringList &list)
Definition: klfpobj.h:195
const KLFPObjPropRef & operator=(const QVariant &v)
Definition: klfpobj.h:808
T value() const
void setValue(int v)
Definition: klfpobj.h:176
int value() const
Definition: klfpobj.h:172
QString join(const QString &separator) const
virtual bool setProperty(const QString &pname, const QVariant &value)=0
Assign a value to a property.
void registerBuiltInProperty(int propId, const QString &propName) const
Definition: klfpobj.cpp:443
QString enumValue() const
Definition: klfpobj.h:181
A class that holds properties.
Definition: klfpobj.h:277
virtual QMap< QString, QVariant > allProperties() const
Convenience function to retrieve all properties.
Definition: klfpobj.cpp:51
KLF_EXPORT QDataStream & operator>>(QDataStream &stream, KLFEnumType &e)
Definition: klfpobj.cpp:83
int size() const
KLFPObjPropRef(KLFPropertizedObject *pobj, int builtInPropId, const QString &pname)
Definition: klfpobj.h:778
An abstract object characterized by properties.
Definition: klfpobj.h:58
virtual QByteArray typeNameFor(const QString &property) const
Corresonding type for the given property.
Definition: klfpobj.h:135
void append(const T &value)
QString fromUtf8(const char *str, int size)
#define KLF_EXPORT
Definition: klfdefs.h:41
virtual QStringList propertyNameList() const =0
Queries what property are (or can be) set.
virtual QVariant property(const QString &propName) const =0
get a property&#39;s value
QStringList enumValues() const
Definition: klfpobj.h:191
#define klfWarning(streamableItems)
const T value() const
Definition: klfpobj.h:829
bool equals(const KLFPObjPropRef &other) const
Definition: klfpobj.h:856
QVariant variantValue() const
Definition: klfpobj.h:824
void registerbuiltinprop(KLFPropertizedObject *obj, int propid, const QString &pname)
Definition: klfpobj.h:749
bool operator==(const KLFEnumType &a, const KLFEnumType &b)
Definition: klfpobj.h:220
Q_DECLARE_METATYPE(KLFEnumType)
bool equals(const KLFPObjPropRef &other) const
Definition: klfpobj.h:852
#define KLF_FUNC_NAME
ToStringFlag
Flags for tuning the toString() method.
Definition: klfpobj.h:457
bool contains(const T &value) const
QString propertyNameSpace(KLFPropertizedObject *obj) const
Definition: klfpobj.h:753
KLFEnumType(const KLFEnumType &copy)
Definition: klfpobj.h:167
KLFPObjPropRef(const KLFPObjPropRef &other)
Definition: klfpobj.h:768
KLFPObjRegisteredType(const char *name)
Definition: klfpobj.h:900
const T value() const
Definition: klfpobj.h:835
virtual QString objectKind() const
Definition: klfpobj.h:509
QVector< QVariant > propertyVector() const
Definition: klfpobj.h:699
bool setSpecification(const QByteArray &data)
Definition: klfpobj.h:205
virtual bool setAllProperties(const QMap< QString, QVariant > &data)
Convenience function to load a set of property values.
Definition: klfpobj.cpp:61
virtual bool hasFixedTypes() const
Definition: klfpobj.h:129
#define KLF_ASSERT_CONDITION(expr, msg, failaction)
Asserting Conditions (NON-FATAL)
bool operator==(const KLFPObjPropRef &other) const
Definition: klfpobj.h:865
KLFSpecifyableRegisteredType(const char *name)
Definition: klfpobj.h:942
KLFPObjPropRef(KLFPropertizedObject *pobj, int propId)
Definition: klfpobj.h:764
int propId
Definition: klfdatautil.cpp:96
virtual QByteArray typeSpecificationFor(const QString &property) const
A type specification for the given property.
Definition: klfpobj.h:144
QByteArray specification() const
Definition: klfpobj.h:200
KLFEnumType(int initvalue=0)
Definition: klfpobj.h:164
QByteArray toUtf8() const

Generated by doxygen 1.8.13