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 #include "PropertyBasedTableModel.hpp"
20 #include <QDebug>
21 
22 /* ********************************************************************* */
23 #if 0
24 #pragma mark -
25 #pragma mark instance Methods
26 #endif
27 /* ********************************************************************* */
PropertyBasedTableModel(QObject * parent)28 PropertyBasedTableModel::PropertyBasedTableModel(QObject *parent)
29 	: QAbstractTableModel(parent)
30 	, content(Q_NULLPTR)
31 	, modelObject(Q_NULLPTR)
32 {
33 }
34 
35 
~PropertyBasedTableModel()36 PropertyBasedTableModel::~PropertyBasedTableModel()
37 {
38 	delete modelObject;
39 	modelObject = Q_NULLPTR;
40 }
41 
init(QList<QObject * > * content,QObject * model,QMap<int,QString> mappings)42 void PropertyBasedTableModel::init(QList<QObject *>* content, QObject *model, QMap<int,QString> mappings)
43 {
44 	beginResetModel();
45 	this->content = content;
46 	this->modelObject = model;
47 	this->mappings = mappings;
48 	endResetModel();
49 }
50 
51 /* ********************************************************************* */
52 #if 0
53 #pragma mark -
54 #pragma mark Model Methods
55 #endif
56 /* ********************************************************************* */
57 
rowCount(const QModelIndex & parent) const58 int PropertyBasedTableModel::rowCount(const QModelIndex &parent) const
59 {
60 	Q_UNUSED(parent);
61 	return content->size();
62 }
63 
columnCount(const QModelIndex & parent) const64 int PropertyBasedTableModel::columnCount(const QModelIndex &parent) const
65 {
66 	Q_UNUSED(parent);
67 	return mappings.size();
68 }
69 
data(const QModelIndex & index,int role) const70 QVariant PropertyBasedTableModel::data(const QModelIndex &index, int role) const
71 {
72 	QVariant data;
73 	if ((role == Qt::DisplayRole || role == Qt::EditRole)
74 		 && index.isValid()
75 		 && index.row() < content->size()
76 		 && index.row() >= 0
77 		 && index.column() < mappings.size()
78 		 && index.column() >= 0){
79 			QObject *object = content->at(index.row());
80 			data = object->property(mappings[index.column()].toStdString().c_str());
81   }
82 	return data;
83 }
84 
insertRows(int position,int rows,const QModelIndex & index)85 bool PropertyBasedTableModel::insertRows(int position, int rows, const QModelIndex &index)
86 {
87 	Q_UNUSED(index);
88 	beginInsertRows(QModelIndex(), position, position + rows - 1);
89 
90 	for (int row=0; row < rows; row++) {
91 		QObject* newInstance = modelObject->metaObject()->newInstance(Q_ARG(QObject, *modelObject));
92 		Q_ASSERT(newInstance != 0);
93 		content->insert(position, newInstance);
94 	}
95 
96 	endInsertRows();
97 	return true;
98 }
99 
removeRows(int position,int rows,const QModelIndex & index)100 bool PropertyBasedTableModel::removeRows(int position, int rows, const QModelIndex &index)
101 {
102 	Q_UNUSED(index);
103 	beginRemoveRows(QModelIndex(), position, position + rows - 1);
104 
105 	for (int row=0; row < rows; ++row) {
106 		content->removeAt(position);
107 	}
108 
109 	endRemoveRows();
110 	return true;
111 }
112 
setData(const QModelIndex & index,const QVariant & value,int role)113 bool PropertyBasedTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
114 {
115 	bool changeMade = false;
116 	if (index.isValid() && role == Qt::EditRole && index.column() < mappings.size()) {
117 		QObject* object = content->at(index.row());
118 		object->setProperty(mappings[index.column()].toStdString().c_str(), value);
119 		emit(QAbstractItemModel::dataChanged(index, index));
120 
121 		changeMade = true;
122 	}
123 
124 	return changeMade;
125 }
126 
flags(const QModelIndex & index) const127 Qt::ItemFlags PropertyBasedTableModel::flags(const QModelIndex &index) const
128 {
129 	if (!index.isValid()) {
130 		return Qt::ItemIsEnabled;
131 	}
132 
133 	return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
134 }
135 
moveRowUp(int position)136 void PropertyBasedTableModel::moveRowUp(int position)
137 {
138 	int count = content->count();
139 	if (count < 2 || position < 1 || position >= count)
140 		return;
141 
142 	beginMoveRows(QModelIndex(), position, position, QModelIndex(), position-1);
143 
144 	content->move(position, position - 1);
145 
146 	endMoveRows();
147 }
148 
moveRowDown(int position)149 void PropertyBasedTableModel::moveRowDown(int position)
150 {
151 	int count = content->count();
152 	if (count < 2 || position < 0 || position > (count - 2))
153 		return;
154 
155 	beginMoveRows(QModelIndex(), position, position, QModelIndex(), position+2);
156 
157 	content->move(position, position + 1);
158 
159 	endMoveRows();
160 }
161 
162