-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfattachmentmodel.cpp
More file actions
118 lines (100 loc) · 2.84 KB
/
pdfattachmentmodel.cpp
File metadata and controls
118 lines (100 loc) · 2.84 KB
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
/*
* This program 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
*/
#include "pdfattachmentmodel.h"
/*
* PdfAttachmentModel QT Model for QML ListView
*
* When adding/removing items QT has to being notified via:
* beginRemoveRows and endRemoveRows for removals
* beginInsertRows and endInsertRows for insertions
* beginResetModel and endResetModel for resetting the whole data;
* emit dataChanged for existing data updates
*
*/
PdfAttachmentModel::PdfAttachmentModel(QObject *parent)
: QAbstractListModel{parent}
{
}
PdfAttachmentModel::~PdfAttachmentModel()
{
}
QList<QString> PdfAttachmentModel::getData()
{
return m_data;
}
int PdfAttachmentModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
// return our data count
return m_data.count();
}
QVariant PdfAttachmentModel::data(const QModelIndex &index, int role) const
{
// the index returns the requested row and column information.
// we ignore the column and only use the row information
int row = index.row();
// boundary check for the row
if(row < 0 || row >= m_data.count())
{
return QVariant();
}
// A model can return data for different roles.
switch(role)
{
case Qt::DisplayRole: // ("model.display" in QML)
return m_data.value(row);
}
// The view asked for other data, just return an empty QVariant
return QVariant();
}
void PdfAttachmentModel::clear()
{
beginResetModel();
m_data.clear();
endResetModel();
}
void PdfAttachmentModel::append(const QString &item)
{
beginInsertRows(QModelIndex(), m_data.count(), m_data.count());
m_data.append(item);
endInsertRows();
}
void PdfAttachmentModel::appendList(const QList<QString> &items)
{
beginInsertRows(QModelIndex(), m_data.count(), m_data.count() + items.size() - 1);
m_data.append(items);
endInsertRows();
}
void PdfAttachmentModel::insert(const QString &item, int pos)
{
beginInsertRows(QModelIndex(), pos, pos);
m_data.insert(pos, item);
endInsertRows();
}
void PdfAttachmentModel::remove(int pos)
{
if (pos >= 0 && pos < m_data.count())
{
beginRemoveRows(QModelIndex(), pos, pos);
m_data.removeAt(pos);
endRemoveRows ();
}
}
void PdfAttachmentModel::removeByKey(const QString key)
{
int pos;
pos = m_data.indexOf(key);
if (pos >= 0) remove(pos);
}
void PdfAttachmentModel::replace (const QString &item, int pos)
{
if (pos >= 0 && pos < m_data.count())
{
m_data.replace(pos, item);
QModelIndex item_index = index(pos, 0);
emit dataChanged (item_index, item_index);
}
}