/
AssigViewMdl.cpp
224 lines (203 loc) · 5.92 KB
/
AssigViewMdl.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
/*
* Copyright 2012-2018 Rochus Keller <mailto:me@rochus-keller.info>
*
* This file is part of the WorkTree application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to me@rochus-keller.info.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "AssigViewMdl.h"
#include "WtTypeDefs.h"
#include <Udb/Idx.h>
#include <Udb/Extent.h>
#include <Udb/Database.h>
#include <Udb/Transaction.h>
#include <Oln2/OutlineUdbMdl.h>
using namespace Wt;
AssigViewMdl::AssigViewMdl(QObject *parent) :
QAbstractItemModel(parent)
{
}
AssigViewMdl::~AssigViewMdl()
{
foreach( Slot* s, d_slots )
delete s;
}
void AssigViewMdl::setObject( const Udb::Obj& r )
{
//if( !d_target.isNull() )
// d_target.getDb()->removeObserver( this, SLOT( onDbUpdate( Udb::UpdateInfo ) ) );
d_object= r;
refill();
//if( !d_target.isNull() )
// d_target.getDb()->addObserver( this, SLOT( onDbUpdate( Udb::UpdateInfo ) ), false );
}
void AssigViewMdl::refill()
{
foreach( Slot* s, d_slots )
delete s;
d_slots.clear();
reset();
if( d_object.isNull() )
return;
Udb::Idx idx( d_object.getTxn(), IndexDefs::IdxAssigObject );
if( idx.seek( d_object ) ) do
{
Udb::Obj assig = d_object.getObject( idx.getOid() );
Udb::Obj subject = assig.getValueAsObj( AttrAssigPrincipal );
if( !subject.isNull() )
{
const int to = findLowerBound( d_slots, subject.getString( AttrText ) );
Slot* s = new Slot();
s->d_assig = assig;
s->d_principal = subject;
d_slots.insert( to, s );
}// TODO: else sollte assig gleich geoelscht werden!
}while( idx.nextKey() );
reset();
// TODO getTree()->resizeColumnToContents(0);
}
QModelIndex AssigViewMdl::parent ( const QModelIndex & index ) const
{
return QModelIndex();
}
int AssigViewMdl::rowCount ( const QModelIndex & parent ) const
{
if( !parent.isValid() )
return d_slots.size();
else
return 0;
}
QModelIndex AssigViewMdl::index ( int row, int column, const QModelIndex & parent ) const
{
if( !parent.isValid() && !d_slots.isEmpty() )
{
Q_ASSERT( row < d_slots.size() );
return createIndex( row, column, d_slots[row] );
}else
return QModelIndex();
}
QVariant AssigViewMdl::data ( const QModelIndex & index, int role ) const
{
if( !index.isValid() || d_object.isNull() )
return QVariant();
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
switch( role )
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
switch( index.column() )
{
case 0:
if( role == Qt::ToolTipRole )
return WtTypeDefs::prettyName( s->d_assig.getType() );
else
switch( s->d_assig.getType() )
{
case TypeRasciAssig:
return WtTypeDefs::formatRasciRole( s->d_assig.getValue( AttrRasciRole ).getUInt8() );
// case TypeWorkAssig:
// return QString("%1%").arg(s->d_assig.getValue( AttrPercentage ).getUInt8() );
}
break;
case 1:
return s->d_principal.getString( AttrText );
}
break;
case Qt::DecorationRole:
switch( index.column() )
{
case 0:
return Oln::OutlineUdbMdl::getPixmap( s->d_assig.getType() );
case 1:
return Oln::OutlineUdbMdl::getPixmap( s->d_principal.getType() );
}
break;
}
return QVariant();
}
QModelIndex AssigViewMdl::getIndex( Slot* s ) const
{
if( s == 0 )
return QModelIndex();
else
return createIndex( d_slots.indexOf( s ), 0, s );
}
Udb::Obj AssigViewMdl::getObject( const QModelIndex& i, bool useCol ) const
{
if( i.isValid() )
{
Slot* s = static_cast<Slot*>( i.internalPointer() );
Q_ASSERT( s != 0 );
if( useCol && i.column() > 0 )
return s->d_principal;
else
return s->d_assig;
}else
return d_object;
}
int AssigViewMdl::findLowerBound(const QList<Slot*>& l, const QString& value )
{
int middle;
int n = l.size();
int begin = 0;
int half;
while (n > 0) {
half = n >> 1;
middle = begin + half;
if ( l[middle]->d_principal.getString( AttrText ).compare( value, Qt::CaseInsensitive ) < 0 )
{
begin = middle + 1;
n -= half + 1;
} else {
n = half;
}
}
return begin;
}
QModelIndex AssigViewMdl::getIndex( const Udb::Obj& o ) const
{
for( int i = 0; i < d_slots.size(); i++ )
if( d_slots[i]->d_assig.equals( o ) || d_slots[i]->d_principal.equals( o ) )
return index( i, 0 );
return QModelIndex();
}
Qt::DropActions AssigViewMdl::supportedDropActions() const
{
return Qt::DropActions( Qt::MoveAction | Qt::CopyAction );
}
QStringList AssigViewMdl::mimeTypes () const
{
return QStringList() << QLatin1String( Udb::Obj::s_mimeObjectRefs );
}
bool AssigViewMdl::dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parentIndex )
{
if( action == Qt::MoveAction || action == Qt::CopyAction )
{
QList<Udb::Obj> objs = Udb::Obj::readObjectRefs( data, d_object.getTxn() );
if( objs.isEmpty() )
return false;
emit signalLinkTo( objs, parentIndex, row );
return true;
}
return false;
}
Qt::ItemFlags AssigViewMdl::flags( const QModelIndex & index ) const
{
if( !index.isValid() )
return Qt::ItemIsDropEnabled;
Slot* s = static_cast<Slot*>( index.internalPointer() );
Q_ASSERT( s != 0 );
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
}