app/resources/qml/Widgets/CalendarWidget.qml

411 lines
12 KiB
QML

/*
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 "../Components"
DataListView {
id: control
property string title: (params.nation === "" ? "IFSC":params.nation === "GER" ? "DAV":"SAC") + " " + qsTr("competition calendar")
property Component headerComponent: Item {
anchors.fill: parent
Row {
anchors.fill: parent
anchors.rightMargin: 5
spacing: width * 0.05
Label {
id: yearLa
anchors.verticalCenter: parent.verticalCenter
width: parent.width * 0.4
height: parent.height * 0.6
fontSizeMode: Text.Fit
font.pixelSize: height
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
minimumPixelSize: 1
text: control.year
Behavior on text {
FadeAnimation {
target: yearLa
}
}
}
Button {
id:yearToolBt
anchors {
verticalCenter: parent.verticalCenter
}
height: parent.height * 0.5
width: parent.width * 0.25
onClicked: {
control.changeYear()
}
onPressed: yearToolBt.scale = 0.9
onReleased: yearToolBt.scale = 1.0
background: Image {
anchors.centerIn: parent
source: "qrc:/icons/calendar.png"
height: parent.height > parent.width ? parent.width : parent.height
width: height
mipmap: true
fillMode: Image.PreserveAspectFit
Behavior on scale {
PropertyAnimation {
duration: 100
}
}
}
}
Button {
id: cupToolBt
anchors {
verticalCenter: parent.verticalCenter
}
height: parent.height * 0.5
width: parent.width * 0.25
onClicked: {
control.openCup()
}
onPressed: cupToolBt.scale = 0.9
onReleased: cupToolBt.scale = 1.0
background: Image {
anchors.centerIn: parent
source: "qrc:/icons/cup.png"
height: parent.height > parent.width ? parent.width : parent.height
width: height
mipmap: true
fillMode: Image.PreserveAspectFit
Behavior on scale {
PropertyAnimation {
duration: 100
}
}
}
}
}
}
property var widgetData
property int year: new Date().getFullYear()
anchors.fill: parent
//boundsBehavior: Flickable.StopAtBounds
model: widgetData["competitions"].length
status: root.status
onRefresh: {
updateData({}, false)
}
onModelChanged: {
autoScroll()
}
onWidgetDataChanged: {
// if the IFSC Calendar is open -> add the worldranking
if(params.nation === ""){
control.widgetData['cups'].unshift({"SerId":"","rkey":"","name":"Worldranking","modified":"2018-10-24 16:11:12","modifier":"","year":"","num_comps":"","cats":["ICC-COA","ICC-HD","ICC-MED","ICC_F","ICC_FB","ICC_FS","ICC_M","ICC_MB","ICC_MS"]})
}
}
function autoScroll() {
var compList = control.widgetData["competitions"]
if(parseInt(control.year) === new Date().getFullYear()){
for(var i = 0; i < compList.length; i ++){
// get the start date pf the competition
var startDate = Date.fromLocaleString(Qt.locale(), compList[i]["date"], "yyyy-MM-dd")
// get the duration of the competition
var durationString = compList[i]["duration"] === undefined ? "1":compList[i]["duration"]
var days = parseInt(durationString.replace(/\D/g,''))
// calculate the end date of the competition
var endDate = new Date(startDate.valueOf())
endDate.setDate(endDate.getDate() + days);
//console.log(compList[i]["date"] + ": " + startDate + " to " + endDate)
if(endDate.getTime() < new Date().getTime()){
// end date is already over -> move the list view down!
control.positionViewAtIndex(i, ListView.Top)
//console.log("moving down!")
}
}
}
}
function getCompCatData(compCatId) {
var obj = app.compCats
for(var prop in obj) {
// go through the whole array and search for data keys
if (obj.hasOwnProperty(prop) && obj[prop]["cat_id"].indexOf(compCatId) >= 0) {
//console.log("found cat: " + obj[prop]['label'])
return obj[prop]
}
}
}
function openComp(compIndex){
var cats = control.widgetData["competitions"][compIndex]["cats"]
cats.sort(function(a, b) {
return parseInt(a["GrpId"]) - parseInt(b["GrpId"]);
});
var selectOptions = []
for(var prop in cats){
if (cats.hasOwnProperty(prop)) {
selectOptions.push({text: cats[prop]["name"], data:{cat: cats[prop]["GrpId"], comp: control.widgetData["competitions"][compIndex]["WetId"], status:cats[prop]["status"]}})
}
}
selector.appear(selectOptions, qsTr("select category"))
}
function changeYear(){
var years = control.widgetData["years"]
var selectOptions = []
for(var prop in years){
if (years.hasOwnProperty(prop)) {
selectOptions.push({text: years[prop], data:{year: years[prop]}})
}
}
selector.appear(selectOptions, qsTr("select Year"))
}
function openCup(state, data) {
var cups = control.widgetData["cups"]
var prop
var selectOptions = []
var selectTitle = ""
if(state === undefined){
// opened for the first time -> select cup
selectTitle = qsTr("select cup")
cups.sort(function(a, b) {
return parseInt(b["SerId"]) - parseInt(a["SerId"]);
});
for(prop in cups){
if (cups.hasOwnProperty(prop)) {
selectOptions.push({text: cups[prop]["name"], data:{cup: cups[prop]["SerId"]}})
}
}
}
else if(state === 1){
// opened for the second time -> select cat
var cup
// find the selected cup
for(prop in cups){
if (cups.hasOwnProperty(prop) && cups[prop]['SerId'] === data.cup) {
cup = cups[prop]
}
}
if(cup === undefined){
// cup was not found
return
}
selectTitle = cup['name'] + ": " + qsTr("select category")
// build a list with all cat in the cup out of the cat keys (rkey) given in the cup.cats
for(prop in cup['cats']){
if (cup['cats'].hasOwnProperty(prop)) {
// search the rkey in the cat list
for(var i = 0; i < control.widgetData["cats"].length; i ++ ){
if(control.widgetData["cats"][i]["rkey"] === cup["cats"][prop] && control.widgetData["cats"][i]["sex"] !== undefined){
// found it -> append it to the select list
var catName = control.widgetData["cats"][i]["name"] // name of cat
var catId = control.widgetData["cats"][i]["GrpId"] // id of cat
selectOptions.push({text: catName, data:{cup: data.cup, cat: catId}})
}
}
}
}
}
selector.appear(selectOptions, selectTitle)
}
Connections {
target: selector
onSelectionFinished: {
if(data.comp !== undefined){
console.log(data.status)
app.openWidget({comp: data.comp, cat: data.cat, type:data.status === 4 ? 'starters':''})
}
else if(data.year !== undefined){
updateData({year: data.year}, true)
control.year = data.year
}
else if(data.cup !== undefined && data.cat === undefined){
control.openCup(1,data)
}
else if(data.cup !== undefined && data.cat !== undefined){
app.openWidget({cup: data.cup, cat: data.cat})
}
}
}
delegate: ItemDelegate {
id: competitionDel
property bool over
property var thisData: control.widgetData["competitions"][index]
property string name: thisData["name"]
property string date: thisData["date_span"]
property var cats: thisData["cats"]
property int catId: thisData["cat_id"] === undefined ? 0:thisData["cat_id"]
width: parent.width
height: compDelCol.height + 10
enabled: thisData["cats"] !== undefined && thisData["cats"].length > 0
opacity: 0
scale: 0.9
onThisDataChanged: {
fadeInPa.start()
}
onClicked: {
control.openComp(index)
}
ParallelAnimation {
id: fadeInPa
NumberAnimation { target: competitionDel; property: "opacity"; from: 0; to: 1.0; duration: 400 }
NumberAnimation { target: competitionDel; property: "scale"; from: 0.8; to: 1.0; duration: 400 }
}
Rectangle {
id: delBackroundRect
anchors.fill: parent
opacity: 0.5
color: control.getCompCatData(catId) === undefined ? "white":control.getCompCatData(catId)["bgcolor"]
}
Column {
id: compDelCol
anchors.centerIn: parent
width: parent.width * 0.97
spacing: 10
Label {
id: nameLa
width: parent.width
font.bold: true
wrapMode: Text.WordWrap
text: name
}
/*
Label {
id: infola
width: parent.width
text: thisData.info
}*/
Label {
id: dateLa
color: "grey"
text: date
}
}
Rectangle {
id: bottomLineRa
anchors {
bottom: parent.bottom
left: parent.left
right: parent.right
}
height: 1
color: "lightgrey"
}
}
}