Warning, /maui/mauikit-calendar/src/controls.5/KalendarUiUtils.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@gmail.com>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 pragma Singleton
0005 
0006 import QtQuick 2.15
0007 import org.kde.kirigami 2.14 as Kirigami
0008 
0009 import "dateutils.js" as DateUtils
0010 import "labelutils.js" as LabelUtils
0011 import org.mauikit.calendar 1.0
0012 //import org.kde.kalendar.contact 1.0
0013 
0014 QtObject {
0015     id: utilsObject
0016     property var appMain
0017 
0018     readonly property bool darkMode: LabelUtils.isDarkColor(Kirigami.Theme.backgroundColor)
0019 
0020     function switchView(newViewComponent, viewSettings) {
0021         if(appMain.pageStack.layers.depth > 1) {
0022             appMain.pageStack.layers.pop(appMain.pageStack.layers.initialItem);
0023         }
0024         if (appMain.pageStack.depth > 1) {
0025             appMain.pageStack.pop();
0026         }
0027         appMain.pageStack.replace(newViewComponent);
0028 
0029         if (appMain.filterHeaderBarLoaderItem.active && appMain.pageStack.currentItem.mode !== KalendarApplication.Contact) {
0030             appMain.pageStack.currentItem.header = appMain.filterHeaderBarLoaderItem.item;
0031         }
0032 
0033         if(viewSettings) {
0034             for(const [key, value] of Object.entries(viewSettings)) {
0035                 appMain.pageStack.currentItem[key] = value;
0036             }
0037         }
0038 
0039         if (appMain.pageStack.currentItem.mode === KalendarApplication.Event) {
0040             appMain.pageStack.currentItem.setToDate(appMain.selectedDate, true);
0041         }
0042     }
0043 
0044     function editorToUse() {
0045         if (!Kirigami.Settings.isMobile) {
0046             appMain.editorWindowedLoaderItem.active = true
0047             return appMain.editorWindowedLoaderItem.item.incidenceEditorPage
0048         } else {
0049             appMain.pageStack.layers.push(incidenceEditorPage);
0050             return incidenceEditorPage;
0051         }
0052     }
0053 
0054     function setUpAdd(type, addDate, collectionId, includeTime) {
0055         let editorToUse = utilsObject.editorToUse();
0056         if (editorToUse.editMode || !editorToUse.incidenceWrapper) {
0057             editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
0058                 editorToUse, "incidence");
0059         }
0060         editorToUse.editMode = false;
0061 
0062         if(type === IncidenceWrapper.TypeEvent) {
0063             editorToUse.incidenceWrapper.setNewEvent();
0064         } else if (type === IncidenceWrapper.TypeTodo) {
0065             editorToUse.incidenceWrapper.setNewTodo();
0066         }
0067 
0068         if(addDate !== undefined && !isNaN(addDate.getTime())) {
0069             let existingStart = editorToUse.incidenceWrapper.incidenceStart;
0070             let existingEnd = editorToUse.incidenceWrapper.incidenceEnd;
0071 
0072             let newStart = addDate;
0073             let newEnd = new Date(newStart.getFullYear(), newStart.getMonth(), newStart.getDate(), newStart.getHours() + 1, newStart.getMinutes());
0074 
0075             if(!includeTime) {
0076                 newStart = new Date(addDate.setHours(existingStart.getHours(), existingStart.getMinutes()));
0077                 newEnd = new Date(addDate.setHours(existingStart.getHours() + 1, existingStart.getMinutes()));
0078             }
0079 
0080             if(type === IncidenceWrapper.TypeEvent) {
0081                 editorToUse.incidenceWrapper.incidenceStart = newStart;
0082                 editorToUse.incidenceWrapper.incidenceEnd = newEnd;
0083             } else if (type === IncidenceWrapper.TypeTodo) {
0084                 editorToUse.incidenceWrapper.incidenceEnd = newStart;
0085             }
0086         }
0087 
0088         if(collectionId && collectionId >= 0) {
0089             editorToUse.incidenceWrapper.collectionId = collectionId;
0090         } else if(type === IncidenceWrapper.TypeEvent && Config.lastUsedEventCollection > -1) {
0091             editorToUse.incidenceWrapper.collectionId = Config.lastUsedEventCollection;
0092         } else if (type === IncidenceWrapper.TypeTodo && Config.lastUsedTodoCollection > -1) {
0093             editorToUse.incidenceWrapper.collectionId = Config.lastUsedTodoCollection;
0094         } else {
0095             editorToUse.incidenceWrapper.collectionId = CalendarManager.defaultCalendarId(editorToUse.incidenceWrapper);
0096         }
0097     }
0098 
0099     function setUpAddSubTodo(parentWrapper) {
0100         let editorToUse = utilsObject.editorToUse();
0101         if (editorToUse.editMode || !editorToUse.incidenceWrapper) {
0102             editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
0103                 editorToUse, "incidence");
0104         }
0105         editorToUse.editMode = false;
0106         editorToUse.incidenceWrapper.setNewTodo();
0107         editorToUse.incidenceWrapper.parent = parentWrapper.uid;
0108         editorToUse.incidenceWrapper.collectionId = parentWrapper.collectionId;
0109         editorToUse.incidenceWrapper.incidenceStart = parentWrapper.incidenceStart;
0110         editorToUse.incidenceWrapper.incidenceEnd = parentWrapper.incidenceEnd;
0111     }
0112 
0113     function setUpView(modelData) {
0114         appMain.incidenceInfoDrawer.incidenceData = modelData;
0115         appMain.incidenceInfoDrawer.open();
0116     }
0117 
0118     function fakeModelDataFromIncidenceWrapper(incidenceWrapper) {
0119         // Spoof what a modelData would look like from the model
0120         const collectionDetails = CalendarManager.getCollectionDetails(incidenceWrapper.collectionId)
0121         const fakeModelData = {
0122             "text": incidenceWrapper.summary,
0123             "description": incidenceWrapper.description,
0124             "location": incidenceWrapper.location,
0125             "startTime": incidenceWrapper.incidenceStart,
0126             "endTime": incidenceWrapper.incidenceEnd,
0127             "allDay": incidenceWrapper.allDay,
0128             "todoCompleted": incidenceWrapper.todoCompleted,
0129             "priority": incidenceWrapper.priority,
0130             // These next two are mainly used in the hourly and day grid views, and we don't use this for
0131             // anything but the incidence info drawer -- for now. Remember that they are different to
0132             // the incidence's actual startTime and duration time -- these are just for positioning!
0133             //"starts":
0134             //"duration":
0135             "durationString": incidenceWrapper.durationDisplayString,
0136             "recurs": incidenceWrapper.recurrenceData.type !== 0,
0137             "hasReminders": incidenceWrapper.remindersModel.rowCount() > 0,
0138             "isOverdue": incidenceWrapper.incidenceType === IncidenceWrapper.TypeTodo &&
0139                          !isNaN(incidenceWrapper.incidenceEnd.getTime()) &&
0140                          incidenceWrapper.incidenceEnd < appMain.currentDate,
0141             "isReadOnly": collectionDetails.readOnly,
0142             "color": collectionDetails.color,
0143             "collectionId": incidenceWrapper.collectionId,
0144             "incidenceId": incidenceWrapper.uid,
0145             "incidenceType": incidenceWrapper.incidenceType,
0146             "incidenceTypeStr": incidenceWrapper.incidenceTypeStr,
0147             "incidenceTypeIcon": incidenceWrapper.incidenceIconName,
0148             "incidencePtr": incidenceWrapper.incidencePtr,
0149             //"incidenceOccurrence":
0150         };
0151 
0152         return fakeModelData;
0153     }
0154 
0155     function setUpEdit(incidencePtr) {
0156         let editorToUse = utilsObject.editorToUse();
0157         editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
0158             editorToUse, "incidence");
0159         editorToUse.incidenceWrapper.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
0160         editorToUse.incidenceWrapper.triggerEditMode();
0161         editorToUse.editMode = true;
0162     }
0163 
0164     function setUpDelete(incidencePtr, deleteDate) {
0165         let incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}', utilsObject, "incidence");
0166         incidenceWrapper.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
0167 
0168         const openDialogWindow = appMain.pageStack.pushDialogLayer(appMain.deleteIncidencePageComponent, {
0169             incidenceWrapper: incidenceWrapper,
0170             deleteDate: deleteDate
0171         }, {
0172             width: Kirigami.Units.gridUnit * 32,
0173             height: Kirigami.Units.gridUnit * 6
0174         });
0175 
0176         openDialogWindow.Keys.escapePressed.connect(function() { openDialogWindow.closeDialog() });
0177     }
0178 
0179     function completeTodo(incidencePtr) {
0180         let todo = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
0181             utilsObject, "incidence");
0182 
0183         todo.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
0184 
0185         if(todo.incidenceType === IncidenceWrapper.TypeTodo) {
0186             todo.todoCompleted = !todo.todoCompleted;
0187             CalendarManager.editIncidence(todo);
0188         }
0189     }
0190 
0191     function setUpIncidenceDateChange(incidenceWrapper, startOffset, endOffset, occurrenceDate, caughtDelegate, allDay=null) {
0192         appMain.pageStack.currentItem.dragDropEnabled = false;
0193 
0194         if(appMain.pageStack.layers.currentItem && appMain.pageStack.layers.currentItem.dragDropEnabled) {
0195             appMain.pageStack.layers.currentItem.dragDropEnabled = false;
0196         }
0197 
0198         if(incidenceWrapper.recurrenceData.type === 0) {
0199             if (allDay !== null) {
0200                 incidenceWrapper.allDay = allDay;
0201             }
0202             CalendarManager.updateIncidenceDates(incidenceWrapper, startOffset, endOffset);
0203         } else {
0204             const onClosingHandler = () => { caughtDelegate.caught = false; utilsObject.reenableDragOnCurrentView(); };
0205             const openDialogWindow = appMain.pageStack.pushDialogLayer(appMain.recurringIncidenceChangePageComponent, {
0206                 incidenceWrapper: incidenceWrapper,
0207                 startOffset: startOffset,
0208                 endOffset: endOffset,
0209                 occurrenceDate: occurrenceDate,
0210                 caughtDelegate: caughtDelegate,
0211                 allDay: allDay
0212             }, {
0213                 width: Kirigami.Units.gridUnit * 34,
0214                 height: Kirigami.Units.gridUnit * 6,
0215                 onClosing: onClosingHandler()
0216             });
0217 
0218             openDialogWindow.Keys.escapePressed.connect(function() { openDialogWindow.closeDialog() });
0219         }
0220     }
0221 
0222     function reenableDragOnCurrentView() {
0223         appMain.pageStack.currentItem.dragDropEnabled = true;
0224 
0225         if(appMain.pageStack.layers.currentItem && appMain.pageStack.layers.currentItem.dragDropEnabled) {
0226             appMain.pageStack.layers.currentItem.dragDropEnabled = true;
0227         }
0228     }
0229 
0230     function openDayLayer(selectedDate) {
0231         appMain.dayScaleModelLoaderItem.active = true;
0232 
0233         if(!isNaN(selectedDate.getTime())) {
0234             appMain.selectedDate = selectedDate;
0235 
0236             appMain.dayViewAction.trigger();
0237         }
0238     }
0239 }