-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinfinitestashtreeview.cpp
More file actions
157 lines (133 loc) · 4.16 KB
/
Copy pathinfinitestashtreeview.cpp
File metadata and controls
157 lines (133 loc) · 4.16 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
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
#include "infinitestashtreeview.h"
#include <QDragMoveEvent>
#include <QStandardItemModel>
#include <QMimeData>
#include <QDebug>
#include <infinitestashstandarditemmodel.h>
InfiniteStashTreeView::InfiniteStashTreeView(QWidget *parent) :
QTreeView(parent)
{
}
void InfiniteStashTreeView::rowsInserted(const QModelIndex &parent, int start, int end)
{
QTreeView::rowsInserted(parent, start, end);
// QStandardItemModel* model = (QStandardItemModel*)this->model();
// QStandardItem* test = model->itemFromIndex(parent);
// QStandardItem* child = test->child(start);
// QStandardItem* item = model->item(start);
// if (item != NULL)
// item->setDropEnabled(false);
}
void InfiniteStashTreeView::setModel(QAbstractItemModel *model)
{
QTreeView::setModel(model);
}
void InfiniteStashTreeView::dropEvent(QDropEvent *event)
{
QTreeView::dropEvent(event);
itemDropped(event);
}
bool InfiniteStashTreeView::wasDropIndicatorOnItem()
{
return dropIndicatorPosition() == QTreeView::OnItem;
}
qint8 InfiniteStashTreeView::whereWasDropIndicator()
{
qint8 dropPos = dropIndicatorPosition();
// qint8 dropPos = dropIndicatorPosition();
// QTreeView::DropIndicatorPosition pos = (QTreeView::DropIndicatorPosition)dropIndicatorPosition();
qint8 result = 0;
switch (dropPos)
{
case QTreeView::AboveItem:
{
result = -1;
break;
}
case QTreeView::BelowItem:
{
result = 0;
break;
}
case QTreeView::OnItem:
{
result = 1;
break;
}
}
return result;
}
/*
* If the drag was started from the infinite stash tree view,
* we need to check to see if the user is trying to drag a group.
* If that is the case, we need to reject the drag event, as groups
* should not be dragged on this control.
*/
void InfiniteStashTreeView::dragEnterEvent(QDragEnterEvent *event)
{
QTreeView::dragEnterEvent(event);
if (event->source() == this)
{
QByteArray modelList = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
QDataStream inputStream(&modelList, QIODevice::ReadOnly);
while (!inputStream.atEnd())
{
int row;
int col;
QMap<int, QVariant> roleDataMap;
inputStream >> row >> col >> roleDataMap;
QVariant itemType = roleDataMap[Qt::UserRole + 1];
if (itemType.toInt() == InfiniteStashStandardItemModel::GroupItemType)
{
event->ignore();
event->setDropAction(Qt::IgnoreAction);
}
}
}
}
/*
* We have to check all dragging to make sure we don't drop items
* somewhere we can't allow them to be. Items must meet the following
* rules:
*
* 1. All items must be in a group.
* 2. A stash item cannot have anything set as its child.
*
*
*/
void InfiniteStashTreeView::dragMoveEvent(QDragMoveEvent *event)
{
QTreeView::dragMoveEvent(event);
QStandardItem* itemDraggedOver = ((QStandardItemModel*)model())->itemFromIndex(indexAt(event->pos()));
bool acceptEvent = true;
if (itemDraggedOver == NULL)
{
//dragging over viewport, reject
acceptEvent = false;
}
else
{
QVariant itemType = itemDraggedOver->data(Qt::UserRole + 1);
if (itemType.toInt() == InfiniteStashStandardItemModel::StashItemType)
{
//dragging over another stash item and drop would
//drop on the stash item, reject
if (wasDropIndicatorOnItem())
acceptEvent = false;
}
else if(itemDraggedOver->parent() == NULL)
{
if (!wasDropIndicatorOnItem())
{
//dragging over element without parent and drop would drop
//outside of this element, reject
acceptEvent = false;
}
}
}
if (!acceptEvent)
{
event->ignore();
event->setDropAction(Qt::IgnoreAction);
}
}