1 #include "slidemodel.h"
7 #include <QItemSelectionModel>
9 SlideModel::SlideModel(VymModel *vm) : QAbstractItemModel(NULL)
11 QVector<QVariant> rootData;
13 rootItem = new SlideItem(rootData, NULL, this);
17 SlideModel::~SlideModel() { delete rootItem; }
19 void SlideModel::clear()
21 if (rootItem->childCount() > 0)
22 removeRows(0, rowCount(QModelIndex()));
25 VymModel *SlideModel::getVymModel() { return vymModel; }
27 int SlideModel::columnCount(const QModelIndex & /* parent */) const
29 return rootItem->columnCount();
32 QVariant SlideModel::data(const QModelIndex &index, int role) const
37 if (role != Qt::DisplayRole && role != Qt::EditRole)
40 SlideItem *item = getItem(index);
42 return item->data(index.column());
45 Qt::ItemFlags SlideModel::flags(const QModelIndex &index) const
50 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
53 QVariant SlideModel::headerData(int section, Qt::Orientation orientation,
56 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
57 return rootItem->data(section);
62 QModelIndex SlideModel::index(SlideItem *fri)
64 if (!fri || !fri->parent())
67 return createIndex(fri->row(), 0, fri);
70 QModelIndex SlideModel::index(int row, int column,
71 const QModelIndex &parent) const
73 if (parent.isValid() && parent.column() != 0)
76 SlideItem *parentItem = getItem(parent);
78 SlideItem *childItem = parentItem->child(row);
80 return createIndex(row, column, childItem);
85 bool SlideModel::insertColumns(int position, int columns,
86 const QModelIndex &parent)
90 beginInsertColumns(parent, position, position + columns - 1);
91 success = rootItem->insertColumns(position, columns);
97 bool SlideModel::insertRows(int position, int rows, const QModelIndex &parent)
99 SlideItem *parentItem = getItem(parent);
102 beginInsertRows(parent, position, position + rows - 1);
104 parentItem->insertChildren(position, rows, rootItem->columnCount());
110 QModelIndex SlideModel::parent(const QModelIndex &index) const
112 if (!index.isValid())
113 return QModelIndex();
115 SlideItem *childItem = getItem(index);
116 SlideItem *parentItem = childItem->parent();
118 if (parentItem == rootItem)
119 return QModelIndex();
121 return createIndex(parentItem->childNumber(), 0, parentItem);
124 bool SlideModel::removeColumns(int position, int columns,
125 const QModelIndex &parent)
129 beginRemoveColumns(parent, position, position + columns - 1);
130 success = rootItem->removeColumns(position, columns);
133 if (rootItem->columnCount() == 0)
134 removeRows(0, rowCount());
139 bool SlideModel::removeRows(int position, int rows, const QModelIndex &parent)
141 SlideItem *parentItem = getItem(parent);
144 beginRemoveRows(parent, position, position + rows - 1);
145 success = parentItem->removeChildren(position, rows);
151 int SlideModel::count() { return rootItem->childCount(); }
153 int SlideModel::rowCount(const QModelIndex &parent) const
155 SlideItem *parentItem = getItem(parent);
157 return parentItem->childCount();
160 bool SlideModel::setData(const QModelIndex &index, const QVariant &value,
163 if (role != Qt::EditRole)
166 SlideItem *item = getItem(index);
167 bool result = item->setData(index.column(), value);
170 emit dataChanged(index, index);
175 bool SlideModel::setHeaderData(int section, Qt::Orientation orientation,
176 const QVariant &value, int role)
178 if (role != Qt::EditRole || orientation != Qt::Horizontal)
181 bool result = rootItem->setData(section, value);
184 emit headerDataChanged(orientation, section, section);
189 SlideItem *SlideModel::addSlide(SlideItem *dst, int n)
191 SlideItem *si = NULL;
195 emit(layoutAboutToBeChanged());
197 QModelIndex parix = index(dst);
199 n = dst->childCount();
200 beginInsertRows(parix, n, n);
201 if (rootItem->insertChildren(n, 1, 0)) {
202 QModelIndex ix = index(n, 0, QModelIndex());
206 emit(layoutChanged());
211 void SlideModel::deleteSlide(SlideItem *si)
213 QModelIndex ix = index(si);
215 QModelIndex px = ix.parent();
216 int n = si->childNumber();
217 removeRows(n, 1, px);
221 bool SlideModel::relinkSlide(SlideItem *si, SlideItem *dst, int pos)
224 emit(layoutAboutToBeChanged());
225 SlideItem *pi = si->parent();
227 // Remove at current position
228 int n = si->childNumber();
230 beginRemoveRows(index(pi), n, n);
234 if (pos < 0 || pos > dst->childCount())
235 pos = dst->childCount();
237 // Insert at new position
238 beginInsertRows(index(dst), pos, pos);
239 dst->insertItem(pos, si);
242 emit(layoutChanged());
244 selModel->select(index(si), QItemSelectionModel::ClearAndSelect);
251 SlideItem *SlideModel::getItem(const QModelIndex &index) const
253 if (index.isValid()) {
254 SlideItem *item = static_cast<SlideItem *>(index.internalPointer());
261 SlideItem *SlideModel::getSlide(int n)
263 if (n >= count() || n < 0)
265 return getItem(index(n, 0, QModelIndex()));
268 SlideItem *SlideModel::findSlideID(uint n)
270 for (int i = 0; i < rootItem->childCount(); i++)
271 if (rootItem->child(i)->getID() == n)
272 return rootItem->child(i);
276 QString SlideModel::saveToDir()
279 for (int i = 0; i < rootItem->childCount(); i++)
280 s += rootItem->child(i)->saveToDir();
284 void SlideModel::setSearchString(const QString &s) { searchString = s; }
286 QString SlideModel::getSearchString() { return searchString; }
288 void SlideModel::setSearchFlags(QTextDocument::FindFlags f) { searchFlags = f; }
290 QTextDocument::FindFlags SlideModel::getSearchFlags() { return searchFlags; }
292 void SlideModel::setSelectionModel(QItemSelectionModel *sm) { selModel = sm; }
294 QItemSelectionModel *SlideModel::getSelectionModel() { return selModel; }
296 QModelIndex SlideModel::getSelectedIndex()
299 qDebug() << "SlideModel: No selection model!";
300 return QModelIndex();
302 QModelIndexList list = selModel->selectedIndexes();
305 return QModelIndex();
308 SlideItem *SlideModel::getSelectedItem()
310 QModelIndex ix = getSelectedIndex();