QmlMirror.qml: refactoring

TaskWarrior support added
Moved all windows to using MirrorWindow
This commit is contained in:
Oscar Andreasson 2015-11-17 08:14:05 +01:00
parent 25f889bef8
commit 5b041df310
9 changed files with 182 additions and 40 deletions

View file

@ -4,13 +4,11 @@ import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.2
Rectangle {
MirrorWindow {
id: root
// width: 360
// height: 700
color: "black"
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
windowHeight: 3
windowWidth: 1
ListView {
anchors.fill: parent

52
JSONListModel.qml Normal file
View file

@ -0,0 +1,52 @@
/* JSONListModel - a QML ListModel with JSON and JSONPath support
*
* Copyright (c) 2012 Romain Pokrzywka (KDAB) (romain@kdab.com)
* Licensed under the MIT licence (http://opensource.org/licenses/mit-license.php)
*/
import QtQuick 2.2
import "jsonpath.js" as JSONPath
Item {
property string source: ""
property string json: ""
property string query: ""
property ListModel model : ListModel { id: jsonModel }
property alias count: jsonModel.count
onSourceChanged: {
var xhr = new XMLHttpRequest;
xhr.open("GET", source);
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE)
json = xhr.responseText;
}
xhr.send();
}
onJsonChanged: updateJSONModel()
onQueryChanged: updateJSONModel()
function updateJSONModel() {
jsonModel.clear();
if ( json === "" )
return;
var objectArray = parseJSONString(json, query);
for ( var key in objectArray ) {
var jo = objectArray[key];
console.log(jo);
jsonModel.append( jo );
}
}
function parseJSONString(jsonString, jsonPathQuery) {
var objectArray = JSON.parse(jsonString);
if ( jsonPathQuery !== "" )
objectArray = JSONPath.jsonPath(objectArray, jsonPathQuery);
return objectArray;
}
}

View file

@ -24,4 +24,6 @@ DISTFILES += \
Calendar.qml \
lala.qml \
MirrorText.qml \
MirrorWindow.qml
MirrorWindow.qml \
JSONListModel.qml \
jsonpath.js

View file

@ -8,6 +8,7 @@
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
import QtQuick.Window 2.0
Rectangle {
@ -22,38 +23,30 @@ Rectangle {
height: 100
}
Clock {
id: clockId
anchors.top: positionId.bottom
anchors.left: parent.left
width: 360
}
Calendar {
anchors.top: clockId.bottom
anchors.left: parent.left
width: 360
height: 480
}
Rectangle {
id: weatherId
width: 360
height: 400
anchors.top: positionId.bottom
anchors.right: parent.right
Weather {
width: 360
height: 400
width: 1080
Row {
Column {
Clock {
}
TaskWarrior {
}
}
Column {
MirrorWindow {}
}
Column {
Weather {
}
Transport {
id: transportId
anchors.top: weatherId.bottom
anchors.right: parent.right
width: 360
height: 400
}
}
}
}
}

View file

@ -4,8 +4,7 @@ import QtQuick 2.2
Rectangle {
id: root
anchors.fill: parent
color: "white"
color: "black"
property string fileName: "jsondata.txt"
@ -23,6 +22,7 @@ Rectangle {
delegate: Text {
text: model.description
color: "white"
Component.onCompleted: {
console.log("Text: " + text)
}

View file

@ -11,8 +11,9 @@ import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.2
Item {
MirrorWindow {
id: root
windowHeight: 3
ListModel {
id: lineModel

View file

@ -10,8 +10,10 @@ import QtQuick 2.0
import net.frozentux.weatherinfo 1.0
Rectangle {
MirrorWindow {
color: "black"
windowHeight: 3
WeatherInfo {
id: weatherdata

88
jsonpath.js Normal file
View file

@ -0,0 +1,88 @@
/* JSONPath 0.8.5 - XPath for JSON
*
* Copyright (c) 2007 Stefan Goessner (goessner.net)
* Licensed under the MIT (MIT-LICENSE.txt) licence.
*
*/
function jsonPath(obj, expr, arg) {
var P = {
resultType: arg && arg.resultType || "VALUE",
result: [],
normalize: function(expr) {
var subx = [];
return expr.replace(/[\['](\??\(.*?\))[\]']|\['(.*?)'\]/g, function($0,$1,$2){return "[#"+(subx.push($1||$2)-1)+"]";}) /* http://code.google.com/p/jsonpath/issues/detail?id=4 */
.replace(/'?\.'?|\['?/g, ";")
.replace(/;;;|;;/g, ";..;")
.replace(/;$|'?\]|'$/g, "")
.replace(/#([0-9]+)/g, function($0,$1){return subx[$1];});
},
asPath: function(path) {
var x = path.split(";"), p = "$";
for (var i=1,n=x.length; i<n; i++)
p += /^[0-9*]+$/.test(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']");
return p;
},
store: function(p, v) {
if (p) P.result[P.result.length] = P.resultType == "PATH" ? P.asPath(p) : v;
return !!p;
},
trace: function(expr, val, path) {
if (expr !== "") {
var x = expr.split(";"), loc = x.shift();
x = x.join(";");
if (val && val.hasOwnProperty(loc))
P.trace(x, val[loc], path + ";" + loc);
else if (loc === "*")
P.walk(loc, x, val, path, function(m,l,x,v,p) { P.trace(m+";"+x,v,p); });
else if (loc === "..") {
P.trace(x, val, path);
P.walk(loc, x, val, path, function(m,l,x,v,p) { typeof v[m] === "object" && P.trace("..;"+x,v[m],p+";"+m); });
}
else if (/^\(.*?\)$/.test(loc)) // [(expr)]
P.trace(P.eval(loc, val, path.substr(path.lastIndexOf(";")+1))+";"+x, val, path);
else if (/^\?\(.*?\)$/.test(loc)) // [?(expr)]
P.walk(loc, x, val, path, function(m,l,x,v,p) { if (P.eval(l.replace(/^\?\((.*?)\)$/,"$1"), v instanceof Array ? v[m] : v, m)) P.trace(m+";"+x,v,p); }); // issue 5 resolved
else if (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.test(loc)) // [start:end:step] phyton slice syntax
P.slice(loc, x, val, path);
else if (/,/.test(loc)) { // [name1,name2,...]
for (var s=loc.split(/'?,'?/),i=0,n=s.length; i<n; i++)
P.trace(s[i]+";"+x, val, path);
}
}
else
P.store(path, val);
},
walk: function(loc, expr, val, path, f) {
if (val instanceof Array) {
for (var i=0,n=val.length; i<n; i++)
if (i in val)
f(i,loc,expr,val,path);
}
else if (typeof val === "object") {
for (var m in val)
if (val.hasOwnProperty(m))
f(m,loc,expr,val,path);
}
},
slice: function(loc, expr, val, path) {
if (val instanceof Array) {
var len=val.length, start=0, end=len, step=1;
loc.replace(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/g, function($0,$1,$2,$3){start=parseInt($1||start);end=parseInt($2||end);step=parseInt($3||step);});
start = (start < 0) ? Math.max(0,start+len) : Math.min(len,start);
end = (end < 0) ? Math.max(0,end+len) : Math.min(len,end);
for (var i=start; i<end; i+=step)
P.trace(i+";"+expr, val, path);
}
},
eval: function(x, _v, _vname) {
try { return $ && _v && eval(x.replace(/(^|[^\\])@/g, "$1_v").replace(/\\@/g, "@")); } // issue 7 : resolved ..
catch(e) { throw new SyntaxError("jsonPath: " + e.message + ": " + x.replace(/(^|[^\\])@/g, "$1_v").replace(/\\@/g, "@")); } // issue 7 : resolved ..
}
};
var $ = obj;
if (expr && obj && (P.resultType == "VALUE" || P.resultType == "PATH")) {
P.trace(P.normalize(expr).replace(/^\$;?/,""), obj, "$"); // issue 6 resolved
return P.result.length ? P.result : false;
}
}

View file

@ -297,6 +297,9 @@ void AppModel::queryCity()
query.addQueryItem("lat", latitude);
query.addQueryItem("lon", longitude);
query.addQueryItem("mode", "json");
query.addQueryItem("APPID", "42c2f93b39eed8f38f08e8ddc321d98f");
url.setQuery(query);
qCDebug(requestsLog) << "submitting request";
@ -324,7 +327,7 @@ void AppModel::positionError(QGeoPositionInfoSource::Error e)
void AppModel::hadError(bool tryAgain)
{
qCDebug(requestsLog) << "hadError, will " << (tryAgain ? "" : "not ") << "rety";
qCDebug(requestsLog) << "hadError, will " << (tryAgain ? "" : "not ") << "retry";
d->throttle.start();
if (d->nErrors < 10)
++d->nErrors;
@ -377,6 +380,8 @@ void AppModel::refreshWeather()
query.addQueryItem("q", d->city);
query.addQueryItem("mode", "json");
query.addQueryItem("APPID", "42c2f93b39eed8f38f08e8ddc321d98f");
url.setQuery(query);
QNetworkReply *rep = d->nam->get(QNetworkRequest(url));
@ -435,6 +440,7 @@ void AppModel::handleWeatherNetworkData(QObject *replyObj)
query.addQueryItem("q", d->city);
query.addQueryItem("mode", "json");
query.addQueryItem("cnt", "5");
query.addQueryItem("APPID", "42c2f93b39eed8f38f08e8ddc321d98f");
url.setQuery(query);
QNetworkReply *rep = d->nam->get(QNetworkRequest(url));