-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.qml
535 lines (413 loc) · 16.5 KB
/
main.qml
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import QtQuick 2.6
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.0
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Universal 2.0
import Qt.labs.settings 1.0
import QtWebView 1.1
import QtQuick.Controls.Styles 1.4
import en.wtl.model 1.0
ApplicationWindow {
id: window
width: 520
height: 320
visible: true
title: "WikiToLearn"
property string searchterm: "";
property alias searchwebview: webview;
property string current_title : "";
// function parsing is used to request and parse the JSON returned by the API
function parsing()
{
var http = new XMLHttpRequest();
var json , parse , html , search;
http.onreadystatechange = function(){
/*
checks if the
request finished and response is ready ( readystate 4)
200: "OK"
*/
if(http.readyState == 4 && http.status == 200)
{
loading.visible=false
json = http.responseText;
parse = JSON.parse(json);
search = parse.query.search;
html = "<!doctype html>";
html = html + "<html> <head> </head> <body>";
html = html + "<h5> Search results </h5> <br />";
for(var i in search){
var title = search[i].title; // get titles of search
var snippet = search[i].snippet; // small description of each title
title = "<a href = \"http://en.wikitolearn.org/api.php?action=parse&page="+title+ "&format=json\">"+title+"</a>";
html = html + title + "<br />";
html = html + snippet + "<br /> <br />" ;
}
html = html + "<br />";
html = html+"</body> </html>";
//console.log(html);
}
webview.loadHtml(html);
};
loading.visible=true
http.open('GET',"http://en.wikitolearn.org/api.php?action=query&list=search&srsearch="+searchterm+"&format=json");
http.send();
}
/* extract_json takes the requried info from the JSON
that was passed by the API .
*/
function extract_json()
{
var p_url = searchwebview.url
console.log(p_url);
var http = new XMLHttpRequest();
var json , parse , text , title;
var styling = " <head>
<link rel =\"stylesheet\" href=\"http://en.wikitolearn.org/load.php?debug=false&lang=en&modules=ext.math.styles%7Cext.visualEditor.desktopArticleTarget.noscript%7Cmediawiki.legacy.commonPrint%2Cshared%7Cmediawiki.sectionAnchor%7Cskin.wikitolearn&only=styles&skin=wikitolearnskin\"/>
<link rel=\"stylesheet\" href=\"https://en.wikitolearn.org/load.php?debug=false&lang=en&modules=ext.echo.badgeicons%7Cext.echo.styles.badge%7Cext.visualEditor.desktopArticleTarget.noscript%7Cmediawiki.legacy.commonPrint%2Cshared%7Cmediawiki.sectionAnchor%7Cskin.wikitolearn&only=styles&skin=wikitolearnskin\"/>
<script async=\"\" src=\"https://en.wikitolearn.org/load.php?debug=false&lang=en&modules=startup&only=scripts&skin=wikitolearnskin\"></script>
<link rel=\"stylesheet\" href=\"https://en.wikitolearn.org/skins/WikiToLearnSkin/bower_components/font-awesome/css/font-awesome.min.css?314\"/>
<meta name=\"ResourceLoaderDynamicStyles\" content=\"\"/>
<link rel=\"stylesheet\" href=\http://en.wikitolearn.org/load.php?debug=false&lang=en&modules=site&only=styles&skin=neverland\"/>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
<link rel=\"shortcut icon\" href=\"http://en.wikitolearn.org/favicon.ico\"/>
<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"/https://en.wikitolearn.org/opensearch_desc.php\" title=\"WikiToLearn - collaborative textbooks (en)\"/>
<link rel=\"EditURI\" type=\"application/rsd+xml\" href=\"http://en.wikitolearn.org/api.php?action=rsd\"/>
<link rel=\"copyright\" href=\"https://en.wikitolearn.org/Project:Copyright\"/>
<link rel=\"alternate\" type=\"application/atom+xml\" title=\"WikiToLearn - collaborative textbooks Atom feed\" href=\"http://en.wikitolearn.org/index.php?title=Special:RecentChanges&feed=atom\"/>
</head>";
http.onreadystatechange = function(){
if(http.readyState == 4 && http.status == 200)
{ json = http.responseText;
parse = JSON.parse(json);
text = parse.parse.text["*"];
title = parse.parse.title;
current_title = title;
console.log(text);
// <-- STRIP ME (o.O) Basically construct the url
while(text.match(/\"\/\/restbase\.wikitolearn\.org/)){
text = text.replace(/\"\/\/restbase\.wikitolearn\.org/, "\"http://restbase.wikitolearn.org");
}
while(text.match(/src=\"\/\/pool.wikitolearn.org/))
{
text = text.replace(/src=\"\/\/pool.wikitolearn.org/ , "src=\"http://pool.wikitolearn.org");
}
while(text.match(/<a href=\"\//))
{
text = text.replace(/<a href=\"\//, "<a href=\"http://en.wikitolearn.org\/")
}
text = styling + text;
console.log(text); // after strip :p .
webview.loadHtml(text);
}
};
http.open('GET',p_url);
http.send();
}
/* Material Settings as per the google guidelines
settings are set to default .
can be changed from settings in the option
*/
Settings {
id: settings
property string style: "Default"
}
header: ToolBar {
Material.foreground: "white"
RowLayout {
spacing: 20
anchors.fill: parent
ToolButton {
contentItem: Image {
fillMode: Image.Pad
horizontalAlignment: Image.AlignHCenter
verticalAlignment: Image.AlignVCenter
source: "qrc:/images/drawer.png"
}
onClicked: drawer.open()
}
Label {
id: titleLabel
text: "WikiToLearn"
font.pixelSize: 20
elide: Label.ElideRight
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
Layout.fillWidth: true
}
ToolButton {
contentItem: Image {
fillMode: Image.Pad
horizontalAlignment: Image.AlignHCenter
verticalAlignment: Image.AlignVCenter
source: "qrc:/images/menu.png"
}
onClicked: optionsMenu.open()
Menu {
id: optionsMenu
x: parent.width - width
transformOrigin: Menu.TopRight
MenuItem {
text: "Settings"
onTriggered: settingsPopup.open()
}
MenuItem {
text: "About"
onTriggered: aboutDialog.open()
}
}
}
}
}
Drawer {
id: drawer
width: Math.min(window.width, window.height) / 3 * 2
height: window.height
ColumnLayout {
anchors.fill: parent
Rectangle {
id: search_field
width: drawer.width
height: 50
TextField{
id: search_text
placeholderText: " Search WikiToLearn"
width: drawer.width
onAccepted: {
listView.currentIndex = -1
searchterm=text
if(stackView.currentItem!==searchview)
{ stackView.push(searchview)
webview.visible=true
}
else{webview.visible=true}
drawer.close();
parsing();
}
Image {
id: search_button
anchors.right: search_text.right
anchors.verticalCenter: search_text.verticalCenter
source: "qrc:/images/search.png"
MouseArea{
anchors.fill: search_button
onClicked: {
searchterm=search_text.text
if(stackView.currentItem!==searchview)
{ stackView.push(searchview)
webview.visible=true
}
else{webview.visible=true}
drawer.close();
listView.currentIndex = -1
parsing();
}
}
}
}
}
Item{
visible: false
Text{
visible: false
id: responsetext
text:""
}
}
Button{
width: drawer.width
height: 40
text:"save offline"
onClicked: {
dbman.clear_list();
responsetext.text = dbman.add(current_title);
mod.new_page(current_title);
drawer.close();
}
}
ListView {
id: listView
currentIndex: -1
Layout.fillWidth: true
Layout.fillHeight: true
delegate: ItemDelegate {
width: parent.width
text: model.title
highlighted: ListView.isCurrentItem
onClicked: {
if (listView.currentIndex != index) {
listView.currentIndex = index
titleLabel.text = model.title
stackView.replace(model.source)
}
drawer.close()
}
}
model: ListModel {
ListElement { title: "Home"; source:""}
ListElement { title: "Manage Pages"; source: "qrc:/pages/manage_pages.qml" }
ListElement { title: "Saved pages"; source: "qrc:/pages/offline_pages.qml"}
ListElement { title: "Quit"; source: "qrc:/pages/quit.qml" }
}
ScrollIndicator.vertical: ScrollIndicator { }
onCurrentIndexChanged: {
if(currentIndex===0)stackView.push(pane)
}
}
}
}
StackView {
id: stackView
anchors.fill: parent
initialItem: Pane {
id: pane
// anchors.fill: parent
Image {
id: logo
width: pane.availableWidth / 2
height: pane.availableHeight / 2
anchors.centerIn: parent
anchors.verticalCenterOffset: -50
fillMode: Image.PreserveAspectFit
source: "qrc:/images/wtl-logo.png"
}
Label {
text: "WikiToLearn wants to provide free, collaborative and accessible text books to the whole world \“knowledge only grows if shared\""
anchors.margins: 20
anchors.top: logo.bottom
anchors.left: parent.left
anchors.right: parent.right
horizontalAlignment: Label.AlignHCenter
verticalAlignment: Label.AlignVCenter
wrapMode: Label.Wrap
}
}
}
Item{
id:searchview
WebView{
id: webview
visible: false
anchors.fill: searchview
onUrlChanged: {
// console.log(searchwebview.url);
extract_json();
}
onLoadingChanged: {
if(webview.loadProgress==="100" && url.toString().match("about:blank")){
loadHtml("<html><head><title></title></head><body><center>Page not Found</center></body></html>");
}
}
}
AnimatedImage {
id: loading
anchors.centerIn: webview
source: "qrc:/images/loading.gif"
visible: false
}
}
Popup {
id: settingsPopup
x: (window.width - width) / 2
y: window.height / 6
width: Math.min(window.width, window.height) / 3 * 2
height: settingsColumn.implicitHeight + topPadding + bottomPadding
modal: true
focus: true
contentItem: ColumnLayout {
id: settingsColumn
spacing: 20
Label {
text: "Settings"
font.bold: true
}
RowLayout {
spacing: 10
Label {
text: "Style:"
}
ComboBox {
id: styleBox
property int styleIndex: -1
model: ["Default", "Material", "Universal"]
Component.onCompleted: {
styleIndex = find(settings.style, Qt.MatchFixedString)
if (styleIndex !== -1)
currentIndex = styleIndex
}
Layout.fillWidth: true
}
}
Label {
text: "Restart required"
color: "#e41e25"
opacity: styleBox.currentIndex !== styleBox.styleIndex ? 1.0 : 0.0
horizontalAlignment: Label.AlignHCenter
verticalAlignment: Label.AlignVCenter
Layout.fillWidth: true
Layout.fillHeight: true
}
RowLayout {
spacing: 10
Button {
id: okButton
text: "Ok"
onClicked: {
settings.style = styleBox.displayText
settingsPopup.close()
}
Material.foreground: Material.primary
Material.background: "transparent"
Material.elevation: 0
Layout.preferredWidth: 0
Layout.fillWidth: true
}
Button {
id: cancelButton
text: "Cancel"
onClicked: {
styleBox.currentIndex = styleBox.styleIndex
settingsPopup.close()
}
Material.background: "transparent"
Material.elevation: 0
Layout.preferredWidth: 0
Layout.fillWidth: true
}
}
}
}
Popup {
id: aboutDialog
modal: true
focus: true
x: (window.width - width) / 2
y: window.height / 6
width: Math.min(window.width, window.height) / 3 * 2
contentHeight: aboutColumn.height
Column {
id: aboutColumn
spacing: 20
Label {
text: "About"
font.bold: true
}
Label {
width: aboutDialog.availableWidth
text: "WikiToLearn wants to provide free, collaborative and accessible text books to the whole world.
Our philosophy is synthetized in the sentence: “knowledge only grows if shared”"
wrapMode: Label.Wrap
font.pixelSize: 12
}
Label {
width: aboutDialog.availableWidth
text: "We provide a platform where learners and teachers can together complete,"
+ "refine and re-assemble notes, lecture notes in order to create text books, "
+ "tailored precisely to their needs, so that you can “stand on the shoulders of giants”"
wrapMode: Label.Wrap
font.pixelSize: 12
}
}
}
}