forked from Brewtarget/brewtarget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeerXMLElement.h
191 lines (162 loc) · 6.28 KB
/
BeerXMLElement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* BeerXMLElement.h is part of Brewtarget, and is Copyright the following
* authors 2009-2014
* - Jeff Bailey <[email protected]>
* - Mik Firestone <[email protected]>
* - Philip Greggory Lee <[email protected]>
* - Samuel Östling <[email protected]>
*
* Brewtarget is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Brewtarget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _BEERXMLELEMENT_H
#define _BEERXMLELEMENT_H
#include <QDomText>
#include <QDomNode>
#include <QDomDocument>
#include <QString>
#include <QObject>
#include <QMetaProperty>
#include <QVariant>
#include <QDateTime>
#include "brewtarget.h"
// For uintptr_t.
#if HAVE_STDINT_H
# include <stdint.h>
#else
# include "pstdint.h"
#endif
// Make uintptr_t available in QVariant.
Q_DECLARE_METATYPE( uintptr_t )
class BeerXMLElement;
/*!
* \class BeerXMLElement
* \author Philip G. Lee
*
* \brief The base class for our database items.
*/
class BeerXMLElement : public QObject
{
Q_OBJECT
Q_CLASSINFO("version","1")
friend class Database;
public:
BeerXMLElement(Brewtarget::DBTable table, int key);
BeerXMLElement( BeerXMLElement const& other );
// Everything that inherits from BeerXML has a name, delete, display and a folder
Q_PROPERTY( QString name READ name WRITE setName )
Q_PROPERTY( bool deleted READ deleted WRITE setDeleted )
Q_PROPERTY( bool display READ display WRITE setDisplay )
Q_PROPERTY( QString folder READ folder WRITE setFolder )
Q_PROPERTY( int key READ key )
Q_PROPERTY( Brewtarget::DBTable table READ table )
//! Convenience method to determine if we are deleted or displayed
bool deleted() const;
bool display() const;
//! Access to the folder attribute.
QString folder() const;
//! Access to the name attribute.
QString name() const;
//! And ways to set those flags
void setDeleted(const bool var);
void setDisplay(const bool var);
//! and a way to set the folder
virtual void setFolder(const QString var, bool signal=true);
//!
void setName(const QString var);
//! \returns our key in the table we are stored in.
int key() const;
//! \returns the table we are stored in.
Brewtarget::DBTable table() const;
//! \returns the BeerXML version of this element.
int version() const;
//! Convenience method to get a meta property by name.
QMetaProperty metaProperty(const char* name) const;
//! Convenience method to get a meta property by name.
QMetaProperty metaProperty(QString const& name) const;
// Some static helpers to convert to/from text.
static double getDouble( const QDomText& textNode );
static bool getBool( const QDomText& textNode );
static int getInt( const QDomText& textNode );
static QString getString( QDomText const& textNode );
static QDateTime getDateTime( QDomText const& textNode );
static QDate getDate( QDomText const& textNode );
//! Convert the string to a QDateTime according to Qt::ISODate.
static QDateTime getDateTime(QString const& str = "");
static QDate getDate(QString const& str = "");
static QString text(bool val);
static QString text(double val);
static QString text(int val);
//! Convert the date to string in Qt::ISODate format for storage NOT display.
static QString text(QDate const& val);
//! Use this to pass pointers around in QVariants.
static inline QVariant qVariantFromPtr( BeerXMLElement* ptr )
{
uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
return QVariant::fromValue<uintptr_t>(addr);
}
static inline BeerXMLElement* extractPtr( QVariant ptrVal )
{
uintptr_t addr = ptrVal.value<uintptr_t>();
return reinterpret_cast<BeerXMLElement*>(addr);
}
bool isValid();
void invalidate();
signals:
/*!
* Passes the meta property that has changed about this object.
* NOTE: when subclassing, be \em extra careful not to create a method with
* the same signature. Otherwise, everything will silently break.
*/
void changed(QMetaProperty, QVariant value = QVariant());
void changedFolder(QString);
void changedName(QString);
protected:
//! The key of this ingredient in its table.
int _key;
//! The table where this ingredient is stored.
Brewtarget::DBTable _table;
/*!
* \param prop_name A meta-property name
* \param col_name The appropriate column in the table.
* \param value the new value
* \param notify true to call NOTIFY method associated with \c prop_name
* Should do the following:
* 1) Set the appropriate value in the appropriate table row.
* 2) Call the NOTIFY method associated with \c prop_name if \c notify == true.
*/
void set( const char* prop_name, const char* col_name, QVariant const& value, bool notify = true );
void set( const QString& prop_name, const QString& col_name, const QVariant& value, bool notify = true );
/*!
* \param col_name - The database column of the attribute we want to get.
* Returns the value of the attribute specified by key/table/col_name.
*/
QVariant get( const char* col_name ) const;
QVariant get( const QString& col_name ) const;
void setInventory( const char* prop_name, const char* col_name, QVariant const& value, bool notify = true );
void setInventory( const QString& prop_name, const QString& col_name, QVariant const& value, bool notify = true );
QVariant getInventory( const char* col_name ) const;
QVariant getInventory( const QString& col_name ) const;
QVariantMap getColumnValueMap() const;
private:
/*!
* \param valid - Indicates if the beerXML element was valid. There is a problem with importing invalid
* XML. I'm hoping this helps fix it
*/
bool _valid;
mutable QString _folder;
mutable QString _name;
mutable QVariant _display;
mutable QVariant _deleted;
};
#endif /* _BEERXMLELEMENT_H */