-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerynode.cpp
More file actions
404 lines (341 loc) · 13.9 KB
/
Copy pathquerynode.cpp
File metadata and controls
404 lines (341 loc) · 13.9 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "querynode.h"
//#include "querythread.h"
#include "combobox.h"
#include "vqbglobal.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSize>
#include <QList>
#include <QPushButton>
#include <QFrame>
#include <KPushButton>
#include <KDebug>
#include <KRandom>
static const QSize GlobalSize(120, 20);//global size for some screen elements
static const int IndentSize = 40;
static const QStringList RelationList( QStringList() << "contains" << "equals" );
class QueryNode::Private
{
public:
Private(QString _parentClass)
: predicateCB(0), relationCB(0), objectCB(0), addBtn(0), removeBtn(0), subjectLayout(0), restrictionContainer(0), restrictionLayout(0)
{
parentClass = _parentClass;
}
~Private()
{
QList<QObject*> objects;
//FIXME? does restrictionContainer delete restrictionLayout?
objects << predicateCB << relationCB << objectCB << subjectLayout << addBtn << removeBtn;
foreach(QObject *o, objects) {
if(o) {
delete o;
}
}
}
QComboBox *predicateCB;
QComboBox *relationCB;
ComboBox *objectCB;
KPushButton *addBtn;
KPushButton *removeBtn;
QHBoxLayout *subjectLayout;//layout of the current subject
QList<QueryNode*> restrictions;
QWidget *restrictionContainer;
QVBoxLayout *restrictionLayout;//layout of the restrictions
QString parentClass;//URI of parent predicate
int level;//level in the query tree
};
QueryNode::QueryNode(QString parentClass, int level)
: QVBoxLayout(), d(new Private(parentClass))
{
d->level = level;
init();
}
void QueryNode::init()
{
//Subject Layout
d->subjectLayout = new QHBoxLayout();
d->subjectLayout->setAlignment(Qt::AlignLeft);
if( d->parentClass.isEmpty() ) { //root node
addObjectToLayout();
}
else { //child node
d->predicateCB = new QComboBox();
d->predicateCB->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
d->predicateCB->resize( GlobalSize );
d->predicateCB->setMinimumSize(GlobalSize);
d->predicateCB->setStatusTip("Predicate of the RDF triple");
findPredicates();
d->subjectLayout->addWidget( d->predicateCB );
connect(d->predicateCB, SIGNAL(currentIndexChanged(int)), this, SLOT(addObjectToLayout()));
connect(d->predicateCB, SIGNAL(currentIndexChanged(int)), this, SLOT(updateQueryPart()));
}
//Restriction Layout
d->restrictionLayout = new QVBoxLayout();
d->restrictionLayout->setAlignment(Qt::AlignLeft);
d->restrictionLayout->setContentsMargins(0, 0, 0, 0);
d->restrictionContainer = new QFrame();
d->restrictionContainer->setAttribute(Qt::WA_PaintOnScreen, true);
d->restrictionContainer->setMinimumSize(0, 0);
d->restrictionContainer->resize(0, 0);
d->restrictionContainer->setContentsMargins(2, 2, 2, 2);
//d->restrictionContainer->setAutoFillBackground(true);
//((QFrame*)d->restrictionContainer)->setFrameStyle(QFrame::Box);
QPalette palette( d->restrictionContainer->palette() );
//FIXME: use theme's default Window color
palette.setColor( QPalette::Window, d->level % 2 ? QColor(232, 231, 230) : Qt::lightGray );
d->restrictionContainer->setPalette(palette);
d->restrictionContainer->setLayout(d->restrictionLayout);
QHBoxLayout *container = new QHBoxLayout();//to indent restrictionLayout
container->addSpacing(IndentSize);
container->addWidget(d->restrictionContainer);
this->addLayout(d->subjectLayout);
this->addLayout(container);
}
QueryNode::~QueryNode()
{
delete d;
}
/**** BACKEND QUERYING ******/
void QueryNode::findObjects()
{
QueryThread *qt = new QueryThread(this);
connect(qt, SIGNAL(queryDone(QList<QStringPair>)), this, SLOT(addSubjects(QList<QStringPair>)));
QString query;
if(!d->parentClass.isEmpty()) { //if not root node
//kDebug() << (int) d->predicateCB;
QString predicate = d->predicateCB->itemData( d->predicateCB->currentIndex() ).toString();
query = QString("SELECT DISTINCT ?label ?class WHERE {"
"{ %1 rdfs:range ?class .}"
"UNION { ?s %1 ?o . ?o a ?class .}"
"OPTIONAL { ?class rdfs:label ?label } } ORDER BY (?label)" )
.arg(predicate);
}
else { //root node
query = "SELECT DISTINCT ?label ?class WHERE"
"{ ?resource a ?class . ?class rdfs:label ?label } ORDER BY (?label)";
}
qt->setQuery(query, QString(), QueryThread::SingleQueryPairs);
qt->start();
}
void QueryNode::findPredicates()
{
QueryThread *qt = new QueryThread(this);
connect(qt, SIGNAL(queryDone(QList<QStringPair>)), this, SLOT(addPredicates(QList<QStringPair>)));
QString query = QString("SELECT DISTINCT ?label ?property WHERE {"
" { "
" ?property rdfs:domain %1 "
" OPTIONAL { ?property rdfs:label ?label }"
" } UNION {"
" ?instance a %1 . "
" ?instance ?property ?object "
" OPTIONAL { ?property rdfs:label ?label } "
" } } ORDER BY (?label)"
).arg(d->parentClass);
qt->setQuery( query, QString(), QueryThread::SingleQueryPairs );
qt->start();
}
/**** INTERFACE AND TREE MANIPULATIONS ******/
void QueryNode::addObjectToLayout()
{
//add the objectCB to the layout
//FIXME: sometimes one of the CBs is longer
if(d->objectCB == 0) {
d->objectCB = new ComboBox();
d->objectCB->setStatusTip("Object field of the RDF triple");
d->objectCB->resize( GlobalSize );
d->objectCB->setMinimumSize(GlobalSize);
d->objectCB->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
//d->objectCB->setEditable(true);
connect(d->objectCB, SIGNAL(currentIndexChanged(int)), this, SLOT(updateQueryPart()));
connect(d->objectCB, SIGNAL(editTextChanged(QString)), this, SLOT(updateQueryPart()));
connect(d->objectCB, SIGNAL(addVarToOutput(QString)), this, SIGNAL(addVarToOutput(QString)));
connect(d->objectCB, SIGNAL(removeVarFromOutput(QString)), this, SIGNAL(removeVarFromOutput(QString)));
connect(d->objectCB, SIGNAL(currentIndexChanged(int)), this, SLOT(removeAllRestrictions()));
findObjects();
d->subjectLayout->addWidget(d->objectCB);
}
else {
findObjects();
}
}
void QueryNode::addSubjects(QList<QStringPair> subjects)
{
//cleanup
removeAllRestrictions();
d->objectCB->clear();
//add subjects
//foreach(QStringPairsp, subjects) {
QStringPair sp;
for(int i=0; i<subjects.count(); i++) {
sp = subjects[i];
d->objectCB->addItem(sp.first, sp.second); //add s2 as the data associated to the item
d->objectCB->setItemData(d->objectCB->count()-1, sp.second, Qt::StatusTipRole);
d->objectCB->setItemData(d->objectCB->count()-1, sp.second, Qt::ToolTipRole);
//kDebug() << "++**++ Added item" << sp.first << sp.second;
}
if(d->objectCB->count() == 1) {
d->objectCB->setCurrentIndex(0);
}
//change interface
QString object = d->objectCB->itemData( d->objectCB->currentIndex() ).toString();
if (object == "xsd:string" ||
object == "rdfs:Literal" ||
object.isEmpty()) {
if(d->relationCB == 0) {
d->relationCB = new QComboBox();
d->relationCB->setStatusTip("Constraint relation");
connect(d->relationCB, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateQueryPart()));
d->relationCB->resize( GlobalSize );
d->relationCB->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
d->relationCB->insertItems(0, RelationList);
d->subjectLayout->insertWidget(1, d->relationCB );
}
d->restrictionLayout->removeWidget(d->addBtn);
d->addBtn->deleteLater();
d->addBtn = 0;
d->objectCB->clear();
d->objectCB->setEditable( true );
}
else { //not literal (root node or not)
if(d->relationCB != 0) {//relation CB
d->subjectLayout->removeWidget(d->relationCB);
d->relationCB->deleteLater();
d->relationCB = 0;
}
d->objectCB->setEditable(false);
if(d->addBtn == 0) {//add button
d->addBtn = new KPushButton(KStandardGuiItem::add().icon(), "");
d->addBtn->setToolTip("Add Restriction to " + d->objectCB->currentText());
d->addBtn->setStatusTip("Adds property restriction to element " + d->objectCB->currentText());
d->addBtn->setBaseSize(20, 20);
d->addBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(d->addBtn, SIGNAL(clicked()),this, SLOT(addRestriction()));
d->restrictionLayout->addWidget(d->addBtn);
}
}
if(d->removeBtn == 0) {//remove button
d->removeBtn = new KPushButton(KStandardGuiItem::remove().icon(), "");
d->removeBtn->setToolTip("Remove Restriction");
d->removeBtn->setStatusTip("Removes restriction");
d->removeBtn->setBaseSize(20, 20);
d->removeBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
connect(d->removeBtn, SIGNAL(clicked()),this, SLOT(emitRemove()));
d->subjectLayout->addWidget(d->removeBtn);
}
updateQueryPart();
}
void QueryNode::addPredicates(QList<QStringPair > predicates)
{
d->predicateCB->blockSignals(true); //don't trigger object population
d->predicateCB->clear();
//foreach(QStringPair sp, predicates) {
QStringPair sp;
for(int i=0; i<predicates.count(); i++) {
sp = predicates[i];
d->predicateCB->addItem(sp.first.isEmpty() ? sp.second : sp.first, sp.second); //add s2 as the data associated to the item
d->predicateCB->setItemData(d->predicateCB->count()-1, sp.second, Qt::StatusTipRole);
d->predicateCB->setItemData(d->predicateCB->count()-1, sp.second, Qt::ToolTipRole);
//kDebug() << sp;
}
d->predicateCB->setCurrentIndex(-1); //insertItem( 0, "?p" );
d->predicateCB->blockSignals(false);
}
void QueryNode::addRestriction()
{
QueryNode *qn = new QueryNode(d->objectCB->itemData(d->objectCB->currentIndex()).toString(), d->level+1);
d->restrictions.append(qn);
d->restrictionLayout->insertLayout(d->restrictionLayout->count()-1, qn);
connect(qn, SIGNAL(queryPartChanged(QString)), this, SLOT(updateQueryPart()));
connect(qn, SIGNAL(addVarToOutput(QString)), this, SIGNAL(addVarToOutput(QString)));
connect(qn, SIGNAL(removeVarFromOutput(QString)), this, SIGNAL(removeVarFromOutput(QString)));
connect(qn, SIGNAL(removeClicked(QueryNode*)), this, SLOT(removeRestriction(QueryNode*)));
}
void QueryNode::removeAllRestrictions()
{
foreach(QueryNode *qn, d->restrictions) {
d->restrictionLayout->removeItem(qn);
qn->deleteLater();
}
d->restrictions.clear();
}
void QueryNode::removeRestriction(QueryNode *qn)
{
//parse query part for vars, and remove them
QString query = qn->queryPart();
QRegExp rx(VqbGlobal::typeRegExp("var"));
QString var;
int pos = 0;
while ((pos = rx.indexIn( query, pos )) != -1) {
var = rx.cap(1);
pos += rx.matchedLength();
emit removeVarFromOutput(var);
}
//remove query node
d->restrictionLayout->removeItem(qn);
d->restrictions.removeAt(d->restrictions.indexOf(qn));
qn->deleteLater();
updateQueryPart();//refresh
}
void QueryNode::emitRemove()
{
//kDebug() << "Emitting remove";
emit removeClicked(this);
}
/****** QUERY COMPUTATION *******/
QString QueryNode::queryPart()
{
//WISH: only the changed query part returns its query string:
// use that, and don't recompute anything
//FIXME (future): use vars - based on the relation combobox (have a "variable" option).
if(d->objectCB == 0) {
return QString();
}
//variable
QString objectVar;
QRegExp rx(VqbGlobal::typeRegExp("var"));
if(rx.exactMatch(d->objectCB->currentText())) {
objectVar = d->objectCB->currentText();
}
QString var = d->objectCB->varName();
if(d->parentClass.isEmpty()) { //root node
QString classUri = d->objectCB->itemData(d->objectCB->currentIndex()).toString();
QString childrenQueryParts;
foreach(QueryNode *node, d->restrictions) {
childrenQueryParts.append(var + " " + node->queryPart());
}
return QString(var + " a " + classUri + " . \n" + childrenQueryParts);
}
if(d->objectCB->isEditable()) { //Literal node
QString relStr = QString("contains");
if(d->relationCB) {
relStr = d->relationCB->currentText();
}
QString predUri = d->predicateCB->itemData(d->predicateCB->currentIndex()).toString();
QString filterStr;
QString object = d->objectCB->currentText();
if (relStr == "equals") {
filterStr = QString(". FILTER regex(" + var + ", '^" + object + "$', 'i') . ");
} else if (relStr == "contains") {
filterStr = QString(". FILTER regex(" + var + ", '" + object + "', 'i') . ") ;
}
return QString(predUri + " " + var + filterStr + "\n");
}
else { //Resource node
QString predUri = d->predicateCB->itemData(d->predicateCB->currentIndex()).toString();
QString classUri = d->objectCB->itemData(d->objectCB->currentIndex()).toString();
QString childrenQueryParts;
foreach(QueryNode *node, d->restrictions) {
childrenQueryParts.append(var + " " + node->queryPart());
}
return QString(predUri + " " + var + " . "
+ var + " a " + classUri +
+ " . \n" + childrenQueryParts);
}
}
void QueryNode::updateQueryPart()
{
emit queryPartChanged(queryPart());
}
#include "querynode.moc"