-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStandardItemModel.cpp
More file actions
98 lines (78 loc) · 3.06 KB
/
MyStandardItemModel.cpp
File metadata and controls
98 lines (78 loc) · 3.06 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
#include "MyStandardItemModel.h"
QMimeData* MyStandardItemModel::mimeData(const QModelIndexList &indexes) const
{
QMimeData* mimeData = QStandardItemModel::mimeData(indexes);
// The raw data that will be placed in the mimeData.
QByteArray mimeBytes;
// Scope the data stream.
{
QDataStream ds(&mimeBytes, QIODevice::WriteOnly);
// The first item encoded will be the number of pointers to expect.
ds << quint32(indexes.size());
// for each index gets a pointer to the standardItem, and write
// into the datastream.
for (int i = 0; i < indexes.size(); i++)
{
QStandardItem* ptrItem = itemFromIndex(indexes[i]);
ds.writeRawData((const char*)&ptrItem, sizeof(QStandardItem*));
}
}
// Add the encoded standard item pointers into the mimeData.
mimeData->setData("Qt/QStandardItemArray", mimeBytes);
return mimeData;
}
bool MyStandardItemModel::dropMimeData(const QMimeData *data, Qt::DropAction /*action*/,
int row, int /*column*/, const QModelIndex &parent)
{
// Get the QStandardItem target of the drop.
QStandardItem* target = itemFromIndex(parent);
// If the target is valid, accepts drops and the mimedata has QStandardItem pointers
// go ahead with decode and insertion. (Checking drop enabled pobably already
// done by the framework before calling this function.)
if ( NULL != target && target->isDropEnabled() && data->hasFormat("Qt/QStandardItemArray") )
{
// Fetch the encoded bytes, create a data stream for decoding,
// and variables to store the output.
QByteArray indexListBytes = data->data("Qt/QStandardItemArray");
QDataStream ds(&indexListBytes, QIODevice::ReadOnly);
quint32 numItems = 0;
// Get the number of items, allocate memory to store pointers to
// them and read the pointer data into that memory.
ds >> numItems;
int byteLen = numItems*sizeof(QStandardItem*);
QStandardItem** stdItems = (QStandardItem**)malloc(byteLen);
ds.readRawData((char*)stdItems, byteLen);
// Add items to the target at a specific child index if requested,
// using thier clone() function to create the items.
for (unsigned i = 0; i < numItems; i++)
{
if ( 0 <= row )
target->insertRow(row, stdItems[i]->clone());
else
target->appendRow(stdItems[i]->clone());
}
// Free memory allocated to store item pointers.
free(stdItems);
return true;
}
return false;
}
void MyStandardItemModel::setStandalone()
{
m_standalone = true;
}
void MyStandardItemModel::addRow(QStandardItem *item)
{
if(!m_standalone)
appendRow(item);
else
{
auto child = item->takeChild(0); // if standalone ignore grouping.
appendRow(child);
}
}
void MyStandardItemModel::clear()
{
for(int i = this->rowCount() - 1; i >= 0 ; --i)
this->removeRow(i);
}