1 /*
2  * Copyright (C) 2012 Stellarium Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program 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 this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
17  */
18 
19 #ifndef PROPERTYBASEDTABLEMODEL_H
20 #define PROPERTYBASEDTABLEMODEL_H
21 
22 #include <QAbstractTableModel>
23 
24 //! This class provides a table model for just about any QObject.  It it nice, as a table model implementation per
25 //! class is not required.  It does this by using the Qt meta object system.
26 //!
27 //! To use this class, your domain objects basically just need to use properties (for any properties you want to make
28 //! available to the model), and have a Q_INVOKABLE copy constructor.  Then, when you instantiate an
29 //! instance, you must call the init methd.  The init method takes the data to model, as well as an instance of your
30 //! model class (to use as a model for creating new instances), and a map to know the ordering  of the properties to
31 //! their position (as you want them displayed).
32 //!
33 //! @author Timothy Reaves <treaves@silverfieldstech.com>
34 //! @ref http://doc.qt.nokia.com/latest/properties.html
35 //! @ingroup oculars
36 class PropertyBasedTableModel : public QAbstractTableModel
37 {
38 	Q_OBJECT
39 public:
40 	PropertyBasedTableModel(QObject *parent = Q_NULLPTR);
41 	virtual ~PropertyBasedTableModel();
42 
43 	//! Initializes this instance for use.  If you do not call this method, and use this class, your app will crash.
44 	//! @param content the domain objects you want to model.  They should all be the same type.  This isnstance does not
45 	//!			take ownership of content, or the elements in it.
46 	//! @param model an instance of the same type as in content, this instance is used to create new instances of your
47 	//!			domain objects by calling the model objects copy constructor.  This instance takes ownership of model.
48 	//! @param mappings mas an integer positional index to the property.
49 	void init(QList<QObject *>* content, QObject *model, QMap<int, QString> mappings);
50 
51 	//Over-rides from QAbstractTableModel
52 	virtual QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const;
53 
54 	virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
55 	virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
56 
57 	virtual Qt::ItemFlags flags(const QModelIndex &index) const;
58 	virtual bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex());
59 	virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
60 	virtual bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex());
61 
62 	void moveRowUp(int position);
63 	void moveRowDown(int position);
64 
65 private:
66 	QList<QObject *>* content;
67 	QMap<int, QString> mappings;
68 	QObject* modelObject;
69 };
70 
71 #endif // PROPERTYBASEDTABLEMODEL_H
72