]> git.sven.stormbind.net Git - sven/vym.git/blob - src/slidemodel.cpp
New upstream version 2.9.22
[sven/vym.git] / src / slidemodel.cpp
1 #include "slidemodel.h"
2
3 #include "slideitem.h"
4 #include "vymmodel.h"
5
6 #include <QDebug>
7 #include <QItemSelectionModel>
8
9 SlideModel::SlideModel(VymModel *vm) : QAbstractItemModel(NULL)
10 {
11     QVector<QVariant> rootData;
12     rootData << "Slide";
13     rootItem = new SlideItem(rootData, NULL, this);
14     vymModel = vm;
15 }
16
17 SlideModel::~SlideModel() { delete rootItem; }
18
19 void SlideModel::clear()
20 {
21     if (rootItem->childCount() > 0)
22         removeRows(0, rowCount(QModelIndex()));
23 }
24
25 VymModel *SlideModel::getVymModel() { return vymModel; }
26
27 int SlideModel::columnCount(const QModelIndex & /* parent */) const
28 {
29     return rootItem->columnCount();
30 }
31
32 QVariant SlideModel::data(const QModelIndex &index, int role) const
33 {
34     if (!index.isValid())
35         return QVariant();
36
37     if (role != Qt::DisplayRole && role != Qt::EditRole)
38         return QVariant();
39
40     SlideItem *item = getItem(index);
41
42     return item->data(index.column());
43 }
44
45 Qt::ItemFlags SlideModel::flags(const QModelIndex &index) const
46 {
47     if (!index.isValid())
48         return 0;
49
50     return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
51 }
52
53 QVariant SlideModel::headerData(int section, Qt::Orientation orientation,
54                                 int role) const
55 {
56     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
57         return rootItem->data(section);
58
59     return QVariant();
60 }
61
62 QModelIndex SlideModel::index(SlideItem *fri)
63 {
64     if (!fri || !fri->parent())
65         return QModelIndex();
66     else
67         return createIndex(fri->row(), 0, fri);
68 }
69
70 QModelIndex SlideModel::index(int row, int column,
71                               const QModelIndex &parent) const
72 {
73     if (parent.isValid() && parent.column() != 0)
74         return QModelIndex();
75
76     SlideItem *parentItem = getItem(parent);
77
78     SlideItem *childItem = parentItem->child(row);
79     if (childItem)
80         return createIndex(row, column, childItem);
81     else
82         return QModelIndex();
83 }
84
85 bool SlideModel::insertColumns(int position, int columns,
86                                const QModelIndex &parent)
87 {
88     bool success;
89
90     beginInsertColumns(parent, position, position + columns - 1);
91     success = rootItem->insertColumns(position, columns);
92     endInsertColumns();
93
94     return success;
95 }
96
97 bool SlideModel::insertRows(int position, int rows, const QModelIndex &parent)
98 {
99     SlideItem *parentItem = getItem(parent);
100     bool success;
101
102     beginInsertRows(parent, position, position + rows - 1);
103     success =
104         parentItem->insertChildren(position, rows, rootItem->columnCount());
105     endInsertRows();
106
107     return success;
108 }
109
110 QModelIndex SlideModel::parent(const QModelIndex &index) const
111 {
112     if (!index.isValid())
113         return QModelIndex();
114
115     SlideItem *childItem = getItem(index);
116     SlideItem *parentItem = childItem->parent();
117
118     if (parentItem == rootItem)
119         return QModelIndex();
120
121     return createIndex(parentItem->childNumber(), 0, parentItem);
122 }
123
124 bool SlideModel::removeColumns(int position, int columns,
125                                const QModelIndex &parent)
126 {
127     bool success;
128
129     beginRemoveColumns(parent, position, position + columns - 1);
130     success = rootItem->removeColumns(position, columns);
131     endRemoveColumns();
132
133     if (rootItem->columnCount() == 0)
134         removeRows(0, rowCount());
135
136     return success;
137 }
138
139 bool SlideModel::removeRows(int position, int rows, const QModelIndex &parent)
140 {
141     SlideItem *parentItem = getItem(parent);
142     bool success = true;
143
144     beginRemoveRows(parent, position, position + rows - 1);
145     success = parentItem->removeChildren(position, rows);
146     endRemoveRows();
147
148     return success;
149 }
150
151 int SlideModel::count() { return rootItem->childCount(); }
152
153 int SlideModel::rowCount(const QModelIndex &parent) const
154 {
155     SlideItem *parentItem = getItem(parent);
156
157     return parentItem->childCount();
158 }
159
160 bool SlideModel::setData(const QModelIndex &index, const QVariant &value,
161                          int role)
162 {
163     if (role != Qt::EditRole)
164         return false;
165
166     SlideItem *item = getItem(index);
167     bool result = item->setData(index.column(), value);
168
169     if (result)
170         emit dataChanged(index, index);
171
172     return result;
173 }
174
175 bool SlideModel::setHeaderData(int section, Qt::Orientation orientation,
176                                const QVariant &value, int role)
177 {
178     if (role != Qt::EditRole || orientation != Qt::Horizontal)
179         return false;
180
181     bool result = rootItem->setData(section, value);
182
183     if (result)
184         emit headerDataChanged(orientation, section, section);
185
186     return result;
187 }
188
189 SlideItem *SlideModel::addSlide(SlideItem *dst, int n)
190 {
191     SlideItem *si = NULL;
192     if (!dst)
193         dst = rootItem;
194
195     emit(layoutAboutToBeChanged());
196
197     QModelIndex parix = index(dst);
198     if (n < 0)
199         n = dst->childCount();
200     beginInsertRows(parix, n, n);
201     if (rootItem->insertChildren(n, 1, 0)) {
202         QModelIndex ix = index(n, 0, QModelIndex());
203         si = getItem(ix);
204     }
205     endInsertRows();
206     emit(layoutChanged());
207
208     return si;
209 }
210
211 void SlideModel::deleteSlide(SlideItem *si)
212 {
213     QModelIndex ix = index(si);
214     if (ix.isValid()) {
215         QModelIndex px = ix.parent();
216         int n = si->childNumber();
217         removeRows(n, 1, px);
218     }
219 }
220
221 bool SlideModel::relinkSlide(SlideItem *si, SlideItem *dst, int pos)
222 {
223     if (si && dst) {
224         emit(layoutAboutToBeChanged());
225         SlideItem *pi = si->parent();
226
227         // Remove at current position
228         int n = si->childNumber();
229
230         beginRemoveRows(index(pi), n, n);
231         pi->removeItem(n);
232         endRemoveRows();
233
234         if (pos < 0 || pos > dst->childCount())
235             pos = dst->childCount();
236
237         // Insert at new position
238         beginInsertRows(index(dst), pos, pos);
239         dst->insertItem(pos, si);
240         endInsertRows();
241
242         emit(layoutChanged());
243
244         selModel->select(index(si), QItemSelectionModel::ClearAndSelect);
245
246         return true;
247     }
248     return false;
249 }
250
251 SlideItem *SlideModel::getItem(const QModelIndex &index) const
252 {
253     if (index.isValid()) {
254         SlideItem *item = static_cast<SlideItem *>(index.internalPointer());
255         if (item)
256             return item;
257     }
258     return rootItem;
259 }
260
261 SlideItem *SlideModel::getSlide(int n)
262 {
263     if (n >= count() || n < 0)
264         return NULL;
265     return getItem(index(n, 0, QModelIndex()));
266 }
267
268 SlideItem *SlideModel::findSlideID(uint n)
269 {
270     for (int i = 0; i < rootItem->childCount(); i++)
271         if (rootItem->child(i)->getID() == n)
272             return rootItem->child(i);
273     return NULL;
274 }
275
276 QString SlideModel::saveToDir()
277 {
278     QString s;
279     for (int i = 0; i < rootItem->childCount(); i++)
280         s += rootItem->child(i)->saveToDir();
281     return s;
282 }
283
284 void SlideModel::setSearchString(const QString &s) { searchString = s; }
285
286 QString SlideModel::getSearchString() { return searchString; }
287
288 void SlideModel::setSearchFlags(QTextDocument::FindFlags f) { searchFlags = f; }
289
290 QTextDocument::FindFlags SlideModel::getSearchFlags() { return searchFlags; }
291
292 void SlideModel::setSelectionModel(QItemSelectionModel *sm) { selModel = sm; }
293
294 QItemSelectionModel *SlideModel::getSelectionModel() { return selModel; }
295
296 QModelIndex SlideModel::getSelectedIndex()
297 {
298     if (!selModel) {
299         qDebug() << "SlideModel: No selection model!";
300         return QModelIndex();
301     }
302     QModelIndexList list = selModel->selectedIndexes();
303     if (!list.isEmpty())
304         return list.first();
305     return QModelIndex();
306 }
307
308 SlideItem *SlideModel::getSelectedItem()
309 {
310     QModelIndex ix = getSelectedIndex();
311     if (ix.isValid())
312         return getItem(ix);
313     return NULL;
314 }