app/resources/qml/Pages/WidgetPage.qml

405 lines
12 KiB
QML
Raw Permalink Normal View History

/*
blueROCK - for digital rock
Copyright (C) 2019 Dorian Zedler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.9
import QtQuick.Controls 2.4
//import QtGraphicalEffects 1.0
//import QtQuick.Templates 2.04 as T
//import QtQuick.Controls.impl 2.04
import QtQuick.Controls.Material 2.3
import "../Components"
Page {
id: root
enum WidgetType {
Competitions,
Profile,
Registration,
Startlist,
Result,
Ranking,
Aggregated // not yet implemented
}
title: widgetLd.item !== null && widgetLd.item.hasOwnProperty('title') ? widgetLd.item['title']:""
property string subTitle: widgetLd.item !== null && widgetLd.item.hasOwnProperty('subTitle') ? widgetLd.item['subTitle']:""
property bool titleIsPageTitle: widgetLd.item !== null && widgetLd.item.hasOwnProperty('titleIsPageTitle') ? widgetLd.item['titleIsPageTitle']:false
property Component headerComponent: widgetLd.item !== null && widgetLd.item.hasOwnProperty('headerComponent') ? widgetLd.item['headerComponent']:null
property var params
property int status: -1
property bool ready: false
property var widgetData
property int widgetType
Component.onCompleted: {
loadData(params)
}
function loadData(params) {
// params is an object and can contain: {
// comp: competitionId,
// person: personId,
// cat: categoryId,
// nation: nationString ('', 'GER', 'SUI')
// route: (int) round
// type: ('','starters', 'nat_team_ranking', 'sektionenwertung', 'regionalzentren'),
//}
var ret = serverConn.getWidgetData(params)
2019-05-29 20:59:01 +02:00
root.status = ret["status"]
if(ret["status"] === 200){
root.widgetData = ret["data"]
root.widgetType = checkWidgetType(params, root.widgetData)
2019-05-29 20:59:01 +02:00
if(widgetLd.load()){
root.ready = true
}
else {
//root.status = 901
2019-05-29 20:59:01 +02:00
root.ready = false
}
}
else if(ret["status"] === 404 && [WidgetPage.WidgetType.Registration, WidgetPage.WidgetType.Startlist, WidgetPage.WidgetType.Result].includes(root.widgetType) && root.params["route"] !== "") {
// if we get a 404 and have startlist, results or registration, the route was not found -> remove round and try again
root.params["route"] = ""
loadData(root.params)
return
}
2019-05-29 20:59:01 +02:00
else {
root.ready = false
}
2019-05-29 20:59:01 +02:00
app.errorCode = root.status
}
function updateData(params, openLoadingDl) {
if(openLoadingDl)
loadingDl.open()
2019-05-29 20:59:01 +02:00
// update all the given values
Object.assign(root.params, params)
loadData(root.params)
console.log("ready: " + root.ready + ": " + root.status)
if(openLoadingDl)
loadingDl.close()
}
function checkWidgetType(params, widgetData){
var widgetType
function hasParam(object, key, value){
if(object[key] !== undefined){
if(value !== undefined){
return object[key] === value
}
return true
}
return false
}
// check the type of the requested widget
if(hasParam(params, 'person')){
// person profile
widgetType = WidgetPage.WidgetType.Profile
}
else if(hasParam(params, 'nation')){
// competition calendar
widgetType = WidgetPage.WidgetType.Competitions
}
else if(hasParam(params, 'comp') && hasParam(params, 'type', 'starters')){
// registration
widgetType = WidgetPage.WidgetType.Registration
}
else if(hasParam(params, 'type', 'startlist') || (widgetData.participants !== undefined && widgetData.participants[0] && !widgetData.participants[0].result_rank && widgetData.discipline !== 'ranking')){
// startlist
widgetType = WidgetPage.WidgetType.Startlist
}
else if(hasParam(params, 'comp') && hasParam(params, 'cat')){
// results
widgetType = WidgetPage.WidgetType.Result
}
else if( hasParam(params, 'cat') && hasParam(params, 'cup') && !hasParam(params, 'comp')){
// ranking data
widgetType = WidgetPage.WidgetType.Ranking
}
else if(hasParam(params, 'type', 'nat_team_ranking') || hasParam(params, 'type', 'sektionenwertung') || hasParam(params, 'type', 'regionalzentren')){
// aggregated
widgetType = WidgetPage.WidgetType.Aggregated
}
return widgetType
}
Loader {
id: widgetLd
property alias selector: selectorPu
property var updateData: root.updateData
property alias params: root.params
2019-05-29 20:59:01 +02:00
property alias currentWidgetData: root.widgetData
property bool isTopElement: mainStack.currentItem === root
2019-05-29 20:59:01 +02:00
property var oldWidgetType: NaN
anchors.fill: parent
source: ""
function load() {
2019-05-29 20:59:01 +02:00
if(root.widgetType !== oldWidgetType){
oldWidgetType = root.widgetType
var calComp = Qt.createComponent(getFile(root.widgetType))//.createObject(null, {widgetData: root.widgetData, parent: widgetLd})
widgetLd.sourceComponent = calComp
//widgetLd.item.widgetData = root.widgetData
}
root.status = widgetLd.item.status
2019-05-29 20:59:01 +02:00
if(widgetLd.item.ready){
return true
}
else {
root.status = widgetLd.item.status === undefined ? 900:widgetLd.item.status
delete(widgetLd.sourceComponent)
return false
}
2019-05-29 20:59:01 +02:00
//
}
function getFile(widgetType) {
var path = "qrc:/Widgets/"
switch(widgetType){
case WidgetPage.WidgetType.Competitions:
path += "CalendarWidget"
break
case WidgetPage.WidgetType.Profile:
path += "ProfileWidget"
break
case WidgetPage.WidgetType.Registration:
path += "RegistrationWidget"
break
case WidgetPage.WidgetType.Startlist:
path += "StartlistWidget"
break
case WidgetPage.WidgetType.Result:
path += "ResultWidget"
break
case WidgetPage.WidgetType.Ranking:
path += "RankingWidget"
break
}
path += ".qml"
return path
}
function getItemProperty(key, defaultValue) {
if(widgetLd.item !== null && widgetLd.item.hasOwnProperty(key)) {
return key
}
else {
return defaultValue
}
}
}
Dialog {
id: selectorPu
property var dataObj
property string subTitle: ""
signal selectionFinished(int index, var data)
x: 0 //root.width / 2 - width / 2
y: root.height - selectorPu.height * 0.7//root.height - height //root.height / 2 - height / 2
opacity: 0
width: root.width
height: selectorLv.implicitHeight
modal: true
focus: true
title: ""
header: Column {
id: selectorPuHeaderCol
width: parent.width
Label {
id: headerLa
visible: selectorPu.title
width: parent.width
elide: "ElideRight"
padding: 24
bottomPadding: 0
font.bold: true
font.pixelSize: 16
background: Rectangle {
radius: 2
color: selectorPu.Material.dialogColor
clip: true
}
text: selectorPu.title
onLinkActivated: {
Qt.openUrlExternally(link)
}
}
Label {
id: headerSubLa
visible: selectorPu.subTitle
width: parent.width
elide: "ElideRight"
padding: 24
topPadding: 5
bottomPadding: 0
font.bold: true
font.pixelSize: 16
background: Rectangle {
radius: 2
color: selectorPu.Material.dialogColor
clip: true
}
text: selectorPu.subTitle
onLinkActivated: {
Qt.openUrlExternally(link)
}
}
}
function appear(dataObj, title, subTitle) {
2019-05-29 20:59:01 +02:00
if(dataObj.length > 0){
selectorPu.dataObj = dataObj
}
else {
selectorPu.dataObj = undefined
}
2019-05-29 20:59:01 +02:00
selectorPu.title = title
selectorPu.subTitle = subTitle === undefined ? "":subTitle
2019-05-29 20:59:01 +02:00
selectorPu.open()
}
ListView {
id: selectorLv
property int delegateHeight: 50
anchors.fill: parent
implicitWidth: parent.width
implicitHeight: root.height * 0.7 < ( (delegateHeight + spacing) * model ) ? root.height * 0.7 : (delegateHeight + spacing) * model + 100
model: selectorPu.dataObj !== undefined ? selectorPu.dataObj.length:0
ScrollIndicator.vertical: ScrollIndicator {
parent: selectorLv.parent
anchors {
top: selectorLv.top
left: selectorLv.right
margins: 10
leftMargin: 3
bottom: selectorLv.bottom
}
}
delegate: Button {
id: catBt
width: parent.width
height: text !== "" ? selectorLv.delegateHeight:0
flat: true
text: selectorPu.dataObj[index].text
onClicked: {
selectorPu.close()
selectorPu.selectionFinished(index, selectorPu.dataObj[index].data)
}
}
}
enter: Transition {
NumberAnimation {
property: "opacity";
//from: 0.0;
to: 1.0
}
NumberAnimation {
property: "y"
//from: root.height - selectorPu.height * 0.7
to: root.height - selectorPu.height
}
}
exit: Transition {
NumberAnimation {
property: "opacity";
//from: 1.0;
to: 0.0
}
NumberAnimation {
property: "y"
//from: root.height - selectorPu.height
to: root.height - selectorPu.height * 0.7
}
}
}
}