-
Notifications
You must be signed in to change notification settings - Fork 1
/
BikeEditingDelegate.h
166 lines (154 loc) · 6.3 KB
/
BikeEditingDelegate.h
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
#ifndef BIKEEDITINGDELEGATE_H
#define BIKEEDITINGDELEGATE_H
#include <QTreeWidgetItem>
#include <QItemDelegate>
#include <QDropEvent>
#include <QDebug>
#include <QMimeData>
class BikeEditingDelegate : public QItemDelegate
{
Q_OBJECT
public:
BikeEditingDelegate(QObject *parent = nullptr) : QItemDelegate(parent) {}
virtual void setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
{
QItemDelegate::setModelData(editor, model, index);
if (index.column() == 0) {
emit editingFinished(index);
}
}
signals:
void editingFinished(const QModelIndex &) const;
};
class BikeTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
BikeTreeWidget(QWidget *parent = nullptr) : QTreeWidget(parent)
{
auto rooItem = invisibleRootItem();
rooItem->setFlags( rooItem->flags() ^ Qt::ItemIsDropEnabled );
}
QTreeWidgetItem *getItemFromIndex(const QModelIndex &index) const
{
return itemFromIndex(index);
}
QTreeWidgetItem * getDropTarget(QPoint pos)
{
QTreeWidgetItem *pItem = this->itemAt(pos);
if (pItem)
{
// Prüfen, ob auf oder neben das Item gedropt wurde
QRect rc = this->visualItemRect(pItem);
QRect rcNew = QRect(rc.left(), rc.top() + 1, rc.width(), rc.height() - 2);
if (rcNew.contains(pos, true))
{
//Wird unter das Item gedroppt? Dann sende parent.
if( pItem->parent() ) pItem = pItem->parent();
}
else
pItem = pItem->parent(); // Es wird NEBEN das Item gedropt -> Target ist das Parent-Item
}
return pItem;
}
void dragMoveEvent(QDragMoveEvent *event)
{
if (!getDropTarget(event->pos()))
{
// getDropTarget hat NULL zurückgeliefert -> Hier darf nicht gedropt werden
event->ignore();
return;
}
QTreeWidget::dragMoveEvent(event);
}
void dropEvent(QDropEvent *event)
{
QTreeWidgetItem *pItem = getDropTarget(event->pos());
if (pItem)
emit itemsDropped(this->selectedItems(), pItem);
QTreeWidget::dropEvent(event);
}
void setFilter( QString filter )
{
for( int i = 0; i < this->topLevelItemCount(); i++ )
{
//bool childFound = false;
for( int j = 0; j < this->topLevelItem(i)->childCount(); j++ )
{
if( filter == "" ) this->topLevelItem(i)->child(j)->setHidden( false );
else
{
if( filter.startsWith( "<=" ) )
{
QString filterEdit = filter;
QString entry1 = this->topLevelItem(i)->child(j)->text( 2 );
entry1.remove( " km" );
filterEdit.remove( " " );
filterEdit.remove( "km" );
filterEdit.remove( "<=" );
int filterNum = filterEdit.toInt();
int entryNum = entry1.toInt();
this->topLevelItem(i)->child(j)->setHidden( filterNum < entryNum );
}
else if( filter.startsWith( ">=" ) )
{
QString filterEdit = filter;
QString entry1 = this->topLevelItem(i)->child(j)->text( 2 );
entry1.remove( " km" );
filterEdit.remove( " " );
filterEdit.remove( "km" );
filterEdit.remove( ">=" );
int filterNum = filterEdit.toInt();
int entryNum = entry1.toInt();
this->topLevelItem(i)->child(j)->setHidden( filterNum > entryNum );
}
else if( filter.startsWith( '<' ) )
{
QString filterEdit = filter;
QString entry1 = this->topLevelItem(i)->child(j)->text( 2 );
entry1.remove( " km" );
filterEdit.remove( " " );
filterEdit.remove( "km" );
filterEdit.remove( "<" );
int filterNum = filterEdit.toInt();
int entryNum = entry1.toInt();
this->topLevelItem(i)->child(j)->setHidden( filterNum <= entryNum );
}
else if( filter.startsWith( '>' ) )
{
QString filterEdit = filter;
QString entry1 = this->topLevelItem(i)->child(j)->text( 2 );
entry1.remove( " km" );
filterEdit.remove( " " );
filterEdit.remove( "km" );
filterEdit.remove( ">" );
int filterNum = filterEdit.toInt();
int entryNum = entry1.toInt();
this->topLevelItem(i)->child(j)->setHidden( filterNum >= entryNum );
}
else if( filter.startsWith( "radius" ) )
{
QString filterEdit = filter;
filterEdit.remove( " " );
filterEdit.remove( "radius" );
filterEdit.remove( "km" );
double radius = filterEdit.toDouble();
this->topLevelItem(i)->child(j)->setHidden( this->topLevelItem(i)->child(j)->text( 7 ).toDouble() > radius );
}
else //Name contains
{
QString entry0 = this->topLevelItem(i)->child(j)->text( 0 );
QString entry1 = this->topLevelItem(i)->child(j)->text( 2 );
this->topLevelItem(i)->child(j)->setHidden( !entry0.contains( filter, Qt::CaseInsensitive )
&& !entry1.contains( filter, Qt::CaseInsensitive ) );
}
}
}
}
}
signals:
void itemsDropped(QList<QTreeWidgetItem *> pSource, QTreeWidgetItem* pTarget);
};
#endif // BIKEEDITINGDELEGATE_H