-
Notifications
You must be signed in to change notification settings - Fork 0
/
vqbschemaconstruct.cpp
228 lines (185 loc) · 6.13 KB
/
vqbschemaconstruct.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
225
226
227
228
#include "vqbschemaconstruct.h"
#include "vqbmainwindow.h"
#include "ui_vqbschemaconstruct.h"
#include "sparqlhighlighter.h"
#include "subjecttree.h"
#include "constraint.h"
#include "vqbglobal.h"
#include <QMainWindow>
#include <QLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QComboBox>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QListWidget>
#include <QListWidgetItem>
#include <QRegExp>
#include <QProcess>
#include <kdebug.h>
#include <kstandardaction.h>
#include <kaction.h>
#include <KPushButton>
#include <KIcon>
#include <KIconLoader>
#include <KStandardGuiItem>
class VqbSchemaConstruct::Private
{
public:
QPushButton *btnAdd;
QVBoxLayout *topLayout;//holds the query trees
QStringList queryTreeStrings;
QList<SubjectTree*> queryTrees;
QStringList outputs;
QString query;
Ui::VqbSchemaConstruct *ui;
};
/************* CONSTR, DESTR, INIT ****************/
VqbSchemaConstruct::VqbSchemaConstruct(VqbMainWindow *parent)
: VqbForm(parent), d(new Private)
{
d->ui = new Ui::VqbSchemaConstruct;
d->ui->setupUi(this);
init();
}
void VqbSchemaConstruct::init()
{
/* Layouts and visual elements */
d->btnAdd = new QPushButton("New Query Tree"); // add button
d->btnAdd->setStatusTip("Adds a new query tree");
d->btnAdd->setBaseSize(100, 50);
connect(d->btnAdd, SIGNAL(clicked()), this, SLOT(addQueryTree()));
QHBoxLayout *qhbl = new QHBoxLayout; //button's layout
qhbl->setDirection(QBoxLayout::RightToLeft);
qhbl->addWidget(d->btnAdd, 1);
qhbl->addStretch(10);
QVBoxLayout* layout = new QVBoxLayout();
d->ui->scrollAreaWidgetContents->setLayout(layout);
d->topLayout = new QVBoxLayout; //the top QueryTree stack
layout->addLayout(d->topLayout, 1);
layout->addLayout(qhbl, 1); //the bottom layout (holding the button)
layout->addStretch(5);
addQueryTree();
connect(d->ui->listSubject, SIGNAL(itemSelectionChanged()), this, SLOT(updateTriple()));
connect(d->ui->listPredicate, SIGNAL(itemSelectionChanged()), this, SLOT(updateTriple()));
connect(d->ui->listObject, SIGNAL(itemSelectionChanged()), this, SLOT(updateTriple()));
connect(d->ui->listBoxOutputs, SIGNAL(changed()), this, SLOT(emitQueryChanged()));
}
VqbSchemaConstruct::~VqbSchemaConstruct()
{
delete d;
delete d->ui;
}
/************* PUBLIC SLOTS ****************/
void VqbSchemaConstruct::queryTreeChanged(int index, QString queryTreeString)
{
//kDebug() << "Subject tree " << index << " has changed:" << queryTreeString;
if (index < d->queryTreeStrings.count()) {
d->queryTreeStrings[index] = queryTreeString;
}
emitQueryChanged();
populateOutputLists();
}
void VqbSchemaConstruct::queryTreeDeleted(int treeNumber)
{
int i;
for(i=treeNumber; i<d->queryTrees.size()-1; i++) {
d->queryTrees[i] = d->queryTrees[i+1];
d->queryTrees[i]->setTreeNumber(i);
d->queryTreeStrings[i] = d->queryTreeStrings[i+1];
}
//kDebug() << i << ", " << d->queryTrees.size();
if (i < d->queryTrees.size()) {
d->queryTrees.removeAt(i);
d->queryTreeStrings.removeAt(i);
}
emitQueryChanged();
}
void VqbSchemaConstruct::addQueryTree()
{
if (d->topLayout) {
SubjectTree *qt = new SubjectTree(d->queryTrees.count(), this);
d->topLayout->addWidget(qt);
d->queryTrees.append(qt);
d->queryTreeStrings.append("");
connect(qt, SIGNAL(queryTreeChanged(int,QString)), this, SLOT(queryTreeChanged(int, QString)));
connect(qt, SIGNAL(queryTreeDeleted(int)), this, SLOT(queryTreeDeleted(int)));
}
}
void VqbSchemaConstruct::emitQueryChanged()
{
QString query = "CONSTRUCT { ";
foreach(QString s, d->ui->listBoxOutputs->items()) {
query.append(s + " ");
}
query.append(" } \n WHERE { \n");
foreach(QString s, d->queryTreeStrings) {
query.append(s);
}
query.append("}\n");
d->query = query;
emit queryChanged(query);
}
void VqbSchemaConstruct::populateOutputLists()
{
//kDebug() << d->query;
d->ui->listObject->clear();
d->ui->listSubject->clear();
d->ui->listPredicate->clear();;
//FIXME? use word boundaries?
QRegExp rx(VqbGlobal::typeRegExp("var"));
QStringList itemList;
QString item;
int pos = 0;
while ((pos = rx.indexIn( d->query, pos )) != -1) {
item = rx.cap(0);
if(!itemList.contains(item) && !item.isEmpty()) {
itemList.append(item);
}
pos += rx.matchedLength();
}
d->ui->listObject->addItems(itemList);
d->ui->listSubject->addItems(itemList);
d->ui->listPredicate->addItems(itemList);
itemList.clear();
rx.setPattern(VqbGlobal::typeRegExp("URI"));//non-literal URIs
pos = 0;
while ((pos = rx.indexIn( d->query, pos )) != -1) {
item = rx.cap(0);
if( !itemList.contains(item) && !item.isEmpty() ) {
itemList.append(item);
}
pos += rx.matchedLength();
}
d->ui->listObject->addItems(itemList);
d->ui->listPredicate->addItems(itemList << "a"); //a (rdfs:type) predicate always needed
itemList.clear();
rx.setPattern(VqbGlobal::typeRegExp("Literal"));//literal URIs
pos = 0;
while ((pos = rx.indexIn( d->query, pos )) != -1) {
item = rx.cap(0);
if(!itemList.contains(item) ) {
itemList.append(item);
}
pos += rx.matchedLength();
}
d->ui->listObject->addItems(itemList);
}
void VqbSchemaConstruct::updateTriple()
{
QString triple = (d->ui->listSubject->currentItem() ? d->ui->listSubject->currentItem()->text() : QString()) + " " +
(d->ui->listPredicate->currentItem() ? d->ui->listPredicate->currentItem()->text() : QString()) + " " +
(d->ui->listObject->currentItem() ? d->ui->listObject->currentItem()->text() : QString());
d->ui->listBoxOutputs->lineEdit()->setText(triple);
}
void VqbSchemaConstruct::addVarToOutput(QString var)
{
Q_UNUSED(var);
}
void VqbSchemaConstruct::removeVarFromOutput(QString var)
{
Q_UNUSED(var);
}
#include "vqbschemaconstruct.moc"