-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
toolbarmodel.cpp
249 lines (219 loc) · 6.14 KB
/
toolbarmodel.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "toolbarmodel.h"
#include "toolbaraction.h"
#include <QPalette>
#include <QMimeData>
#define NOTE_TOOLBAR_ITEM_MIME "application/znotes.toolbar_item"
#define NOTE_TOOLBAR_ITEM_USED_MIME "application/znotes.toolbar_item_used"
//------------------------------------------------------------------------------
ToolbarItems::ToolbarItems()
{
elems.resize(itemMax);
}
void ToolbarItems::setVector(const QVector<int>& nv)
{
for(int i=0; i<elems.size(); ++i) elems[i]=false;
toolbar_elems=nv;
for(int i=0; i<toolbar_elems.size(); ++i)
{
int id = toolbar_elems[i];
if(id!=itemSeparator) elems[id] = true;
}
}
void ToolbarItems::swap(int pos0, int pos1)
{
qSwap(toolbar_elems[pos0], toolbar_elems[pos1]);
}
void ToolbarItems::move(int pos0, int pos1)
{
if(pos0<0 || pos0>=toolbar_elems.size()) return;
if(pos1<0 || pos1>toolbar_elems.size()) return;
int id = toolbar_elems.at(pos0);
toolbar_elems.remove(pos0);
if(pos1<pos0) toolbar_elems.insert(pos1, id);
else toolbar_elems.insert(pos1-1, id);
}
void ToolbarItems::insert(int id, int pos)
{
if(pos!=-1) toolbar_elems.insert(pos, id);
else toolbar_elems.append(id);
if(id!=itemSeparator) elems[id] = true;
emit reset();
}
void ToolbarItems::remove(int pos)
{
int id = toolbar_elems.at(pos);
toolbar_elems.remove(pos);
elems[id] = false;
emit reset();
}
/*
This model contains all existing items
*/
ItemModel::ItemModel(ToolbarItems& t_items)
: items(t_items), v(t_items.getAllElems())
{
connect(&t_items, SIGNAL(reset()), this, SIGNAL(modelReset()));
// TODO: connect to settings
}
int ItemModel::rowCount(const QModelIndex &) const
{
return v.size();
}
QVariant ItemModel::data(const QModelIndex &index, int role) const
{
auto use_system_icons_theme = false; // TODO: take from settings/ui (how?)
const int id = index.row();
switch(role)
{
case Qt::DisplayRole: return ToolbarAction(item_enum(id)).text();
case Qt::DecorationRole: return ToolbarAction(item_enum(id), use_system_icons_theme).icon();
default: return QVariant();
}
}
Qt::ItemFlags ItemModel::flags(const QModelIndex &index) const
{
if(!index.isValid()) return Qt::ItemIsDropEnabled;
if(items.isUsed(index.row())) return Qt::ItemIsDropEnabled;
return Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled|Qt::ItemIsSelectable|Qt::ItemIsEnabled;
}
Qt::DropActions ItemModel::supportedDropActions() const
{
return Qt::MoveAction;
}
QStringList ItemModel::mimeTypes() const
{
QStringList types;
types << NOTE_TOOLBAR_ITEM_USED_MIME;
return types;
}
QMimeData* ItemModel::mimeData(const QModelIndexList& indexes) const
{
QMimeData* mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach(QModelIndex index, indexes)
{
stream << index.row();
}
mimeData->setData(NOTE_TOOLBAR_ITEM_MIME, encodedData);
return mimeData;
}
bool ItemModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
{
Q_UNUSED(row);
Q_UNUSED(column);
Q_UNUSED(parent);
if(action==Qt::IgnoreAction) return true;
if(!data->hasFormat(NOTE_TOOLBAR_ITEM_USED_MIME)) return false;
if(action!=Qt::MoveAction) return true;
//
QByteArray encodedData = data->data(NOTE_TOOLBAR_ITEM_USED_MIME);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int pos;
stream >> pos;
items.remove(pos);
return true;
}
//------------------------------------------------------------------------------
/*
This model contains items, added on toolbar
*/
ItemToolbarModel::ItemToolbarModel(ToolbarItems& t_items)
: items(t_items), v(t_items.getToolbarElems())
{
connect(&t_items, SIGNAL(reset()), this, SIGNAL(modelReset()));
}
int ItemToolbarModel::rowCount(const QModelIndex&) const
{
return v.size();
}
QVariant ItemToolbarModel::data(const QModelIndex &index, int role) const
{
item_enum item = item_enum(v[index.row()]);
switch(role)
{
case Qt::DisplayRole: return ToolbarAction(item).text();
case Qt::DecorationRole : return ToolbarAction(item).icon();
default: return QVariant();
}
}
Qt::DropActions ItemToolbarModel::supportedDropActions() const
{
return Qt::MoveAction;
}
Qt::ItemFlags ItemToolbarModel::flags(const QModelIndex &index) const
{
if(!index.isValid()) return Qt::ItemIsDropEnabled;
return Qt::ItemIsDragEnabled|Qt::ItemIsSelectable|Qt::ItemIsEnabled;
}
QStringList ItemToolbarModel::mimeTypes() const
{
QStringList types;
types << NOTE_TOOLBAR_ITEM_MIME << NOTE_TOOLBAR_ITEM_USED_MIME;
return types;
}
QMimeData* ItemToolbarModel::mimeData(const QModelIndexList& indexes) const
{
QMimeData* mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach(QModelIndex index, indexes)
{
stream << index.row();
}
mimeData->setData(NOTE_TOOLBAR_ITEM_USED_MIME, encodedData);
return mimeData;
}
bool ItemToolbarModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
{
Q_UNUSED(column);
Q_UNUSED(parent);
if(row==-1) return false;
if(action!=Qt::MoveAction) return false;
if(data->hasFormat(NOTE_TOOLBAR_ITEM_USED_MIME))
{
QByteArray encodedData = data->data(NOTE_TOOLBAR_ITEM_USED_MIME);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int pos;
stream >> pos;
items.move(pos, row);
return true;
}
if(data->hasFormat(NOTE_TOOLBAR_ITEM_MIME))
{
QByteArray encodedData = data->data(NOTE_TOOLBAR_ITEM_MIME);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int id;
stream >> id;
items.insert(id, row);
return true;
}
return false;
}
QModelIndex ItemToolbarModel::up(const QModelIndex &index)
{
int row = index.row();
if(row==0) return index; //if this item first
#if QT_VERSION >= 0x050000
beginResetModel();
items.swap(row, row-1);
endResetModel();
#endif
#if QT_VERSION < 0x050000
items.swap(row, row-1);
emit reset();
#endif
QModelIndex new_index(this->index(row-1, index.column()));
return new_index;
}
QModelIndex ItemToolbarModel::down(const QModelIndex &index)
{
int row = index.row();
if(row==v.size()-1) return index; //if this item last
items.swap(row, row+1);
#if QT_VERSION < 0x050000
emit reset();
#endif
QModelIndex new_index(this->index(row+1, index.column()));
return new_index;
}