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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/smolbote.hg
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "bookmarkmodel.h"
#include "bookmarkitem.h"
BookmarkModel::BookmarkModel(QObject *parent)
: QAbstractItemModel(parent)
{
rootItem = new BookmarkItem({ "Title", "Address" }, BookmarkItem::Folder, nullptr);
}
BookmarkModel::~BookmarkModel()
{
delete rootItem;
}
QVariant BookmarkModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
QVariant BookmarkModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DecorationRole && index.column() == 0)
return static_cast<BookmarkItem *>(index.internalPointer())->icon();
else if(role == Qt::ToolTipRole)
return static_cast<BookmarkItem *>(index.internalPointer())->tooltip();
else if(role != Qt::DisplayRole)
return QVariant();
return static_cast<BookmarkItem *>(index.internalPointer())->data(index.column());
}
QVariant BookmarkModel::data(const QModelIndex &index, int column, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
return static_cast<BookmarkItem *>(index.internalPointer())->data(column);
return QVariant();
}
bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!index.isValid() || role != Qt::DisplayRole)
return false;
bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(static_cast<BookmarkItem::Fields>(index.column()), value);
if(success)
emit dataChanged(index, index, { role });
return success;
}
bool BookmarkModel::setData(const QModelIndex &index, const QVariant &value, BookmarkItem::Fields column, int role)
{
if(!index.isValid() || role != Qt::DisplayRole)
return false;
bool success = static_cast<BookmarkItem *>(index.internalPointer())->setData(column, value);
if(success)
emit dataChanged(index, index, { role });
return success;
}
Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const
{
if(getItem(index)->type() == BookmarkItem::Folder)
return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
else
return QAbstractItemModel::flags(index) | Qt::ItemIsDragEnabled;
}
bool BookmarkModel::isItemExpanded(const QModelIndex &index) const
{
if(!index.isValid())
return false;
return static_cast<BookmarkItem *>(index.internalPointer())->isExpanded();
}
int BookmarkModel::rowCount(const QModelIndex &index) const
{
if(index.column() > 0)
return 0;
return getItem(index)->childCount();
}
bool BookmarkModel::insertRows(int position, int rows, const QModelIndex &parent)
{
auto *parentItem = getItem(parent);
// if position is negative, or past the number of children in the item
if(position < 0 || position > parentItem->childCount())
return false;
beginInsertRows(parent, position, position + rows - 1);
for(int i = 0; i < rows; ++i) {
parentItem->insertChild(position + i, new BookmarkItem({}, BookmarkItem::Bookmark, parentItem));
}
endInsertRows();
return true;
}
bool BookmarkModel::removeRows(int position, int rows, const QModelIndex &parent)
{
auto *parentItem = getItem(parent);
beginRemoveRows(parent, position, position + rows - 1);
bool success = parentItem->removeChildAt(position, rows);
endRemoveRows();
return success;
}
bool BookmarkModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
{
auto *sourceParentItem = getItem(sourceParent);
auto *destinationParentItem = getItem(destinationParent);
bool success = true;
beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild);
for(int i = 0; i < count; ++i) {
auto *child = sourceParentItem->takeChild(sourceRow, destinationParentItem);
if(!destinationParentItem->insertChild(destinationChild, child)) {
success = false;
break;
}
}
endMoveRows();
return success;
}
int BookmarkModel::columnCount(const QModelIndex &index) const
{
return 2;
// if(!index.isValid())
// return rootItem->columnCount();
// else
// return static_cast<BookmarkItem *>(index.internalPointer())->columnCount();
}
Qt::DropActions BookmarkModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QModelIndex BookmarkModel::index(int row, int column, const QModelIndex &parent) const
{
if(!this->hasIndex(row, column, parent))
return QModelIndex();
BookmarkItem *parentItem = getItem(parent);
BookmarkItem *childItem = parentItem->child(row);
if(childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex BookmarkModel::parent(const QModelIndex &index) const
{
if(!index.isValid())
return QModelIndex();
auto *childItem = static_cast<BookmarkItem *>(index.internalPointer());
auto *parentItem = childItem->parent();
if(parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
BookmarkItem *BookmarkModel::getItem(const QModelIndex &index) const
{
if(!index.isValid())
return rootItem;
else
return static_cast<BookmarkItem *>(index.internalPointer());
}
|