MauiKit Calendar

KalendarUiUtils.qml
1// SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@gmail.com>
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4pragma Singleton
5
6import QtQuick 2.15
8import org.mauikit.controls as Maui
9
10import "dateutils.js" as DateUtils
11import "labelutils.js" as LabelUtils
12import org.mauikit.calendar
14QtObject {
15 id: utilsObject
16 property var appMain
17
18 readonly property bool darkMode: LabelUtils.isDarkColor(Maui.Theme.backgroundColor)
19
20 function switchView(newViewComponent, viewSettings) {
21 if(appMain.pageStack.layers.depth > 1) {
22 appMain.pageStack.layers.pop(appMain.pageStack.layers.initialItem);
23 }
24 if (appMain.pageStack.depth > 1) {
25 appMain.pageStack.pop();
26 }
27 appMain.pageStack.replace(newViewComponent);
28
29 if (appMain.filterHeaderBarLoaderItem.active && appMain.pageStack.currentItem.mode !== KalendarApplication.Contact) {
30 appMain.pageStack.currentItem.header = appMain.filterHeaderBarLoaderItem.item;
31 }
32
33 if(viewSettings) {
34 for(const [key, value] of Object.entries(viewSettings)) {
35 appMain.pageStack.currentItem[key] = value;
36 }
37 }
38
39 if (appMain.pageStack.currentItem.mode === KalendarApplication.Event) {
40 appMain.pageStack.currentItem.setToDate(appMain.selectedDate, true);
41 }
42 }
43
44 function editorToUse() {
45 if (!Maui.Handy.isMobile) {
46 appMain.editorWindowedLoaderItem.active = true
47 return appMain.editorWindowedLoaderItem.item.incidenceEditorPage
48 } else {
49 appMain.pageStack.layers.push(incidenceEditorPage);
50 return incidenceEditorPage;
51 }
52 }
53
54 function setUpAdd(type, addDate, collectionId, includeTime) {
55 let editorToUse = utilsObject.editorToUse();
56 if (editorToUse.editMode || !editorToUse.incidenceWrapper) {
57 editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
58 editorToUse, "incidence");
59 }
60 editorToUse.editMode = false;
61
62 if(type === IncidenceWrapper.TypeEvent) {
63 editorToUse.incidenceWrapper.setNewEvent();
64 } else if (type === IncidenceWrapper.TypeTodo) {
65 editorToUse.incidenceWrapper.setNewTodo();
66 }
67
68 if(addDate !== undefined && !isNaN(addDate.getTime())) {
69 let existingStart = editorToUse.incidenceWrapper.incidenceStart;
70 let existingEnd = editorToUse.incidenceWrapper.incidenceEnd;
71
72 let newStart = addDate;
73 let newEnd = new Date(newStart.getFullYear(), newStart.getMonth(), newStart.getDate(), newStart.getHours() + 1, newStart.getMinutes());
74
75 if(!includeTime) {
76 newStart = new Date(addDate.setHours(existingStart.getHours(), existingStart.getMinutes()));
77 newEnd = new Date(addDate.setHours(existingStart.getHours() + 1, existingStart.getMinutes()));
78 }
79
80 if(type === IncidenceWrapper.TypeEvent) {
81 editorToUse.incidenceWrapper.incidenceStart = newStart;
82 editorToUse.incidenceWrapper.incidenceEnd = newEnd;
83 } else if (type === IncidenceWrapper.TypeTodo) {
84 editorToUse.incidenceWrapper.incidenceEnd = newStart;
85 }
86 }
87
88 if(collectionId && collectionId >= 0) {
89 editorToUse.incidenceWrapper.collectionId = collectionId;
90 } else if(type === IncidenceWrapper.TypeEvent && Config.lastUsedEventCollection > -1) {
91 editorToUse.incidenceWrapper.collectionId = Config.lastUsedEventCollection;
92 } else if (type === IncidenceWrapper.TypeTodo && Config.lastUsedTodoCollection > -1) {
93 editorToUse.incidenceWrapper.collectionId = Config.lastUsedTodoCollection;
94 } else {
95 editorToUse.incidenceWrapper.collectionId = CalendarManager.defaultCalendarId(editorToUse.incidenceWrapper);
96 }
97 }
98
99 function setUpAddSubTodo(parentWrapper) {
100 let editorToUse = utilsObject.editorToUse();
101 if (editorToUse.editMode || !editorToUse.incidenceWrapper) {
102 editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
103 editorToUse, "incidence");
104 }
105 editorToUse.editMode = false;
106 editorToUse.incidenceWrapper.setNewTodo();
107 editorToUse.incidenceWrapper.parent = parentWrapper.uid;
108 editorToUse.incidenceWrapper.collectionId = parentWrapper.collectionId;
109 editorToUse.incidenceWrapper.incidenceStart = parentWrapper.incidenceStart;
110 editorToUse.incidenceWrapper.incidenceEnd = parentWrapper.incidenceEnd;
111 }
112
113 function setUpView(modelData) {
114 appMain.incidenceInfoDrawer.incidenceData = modelData;
115 appMain.incidenceInfoDrawer.open();
116 }
117
118 function fakeModelDataFromIncidenceWrapper(incidenceWrapper) {
119 // Spoof what a modelData would look like from the model
120 const collectionDetails = CalendarManager.getCollectionDetails(incidenceWrapper.collectionId)
121 const fakeModelData = {
122 "text": incidenceWrapper.summary,
123 "description": incidenceWrapper.description,
124 "location": incidenceWrapper.location,
125 "startTime": incidenceWrapper.incidenceStart,
126 "endTime": incidenceWrapper.incidenceEnd,
127 "allDay": incidenceWrapper.allDay,
128 "todoCompleted": incidenceWrapper.todoCompleted,
129 "priority": incidenceWrapper.priority,
130 // These next two are mainly used in the hourly and day grid views, and we don't use this for
131 // anything but the incidence info drawer -- for now. Remember that they are different to
132 // the incidence's actual startTime and duration time -- these are just for positioning!
133 //"starts":
134 //"duration":
135 "durationString": incidenceWrapper.durationDisplayString,
136 "recurs": incidenceWrapper.recurrenceData.type !== 0,
137 "hasReminders": incidenceWrapper.remindersModel.rowCount() > 0,
138 "isOverdue": incidenceWrapper.incidenceType === IncidenceWrapper.TypeTodo &&
139 !isNaN(incidenceWrapper.incidenceEnd.getTime()) &&
140 incidenceWrapper.incidenceEnd < appMain.currentDate,
141 "isReadOnly": collectionDetails.readOnly,
142 "color": collectionDetails.color,
143 "collectionId": incidenceWrapper.collectionId,
144 "incidenceId": incidenceWrapper.uid,
145 "incidenceType": incidenceWrapper.incidenceType,
146 "incidenceTypeStr": incidenceWrapper.incidenceTypeStr,
147 "incidenceTypeIcon": incidenceWrapper.incidenceIconName,
148 "incidencePtr": incidenceWrapper.incidencePtr,
149 //"incidenceOccurrence":
150 };
151
152 return fakeModelData;
153 }
154
155 function setUpEdit(incidencePtr) {
156 let editorToUse = utilsObject.editorToUse();
157 editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
158 editorToUse, "incidence");
159 editorToUse.incidenceWrapper.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
160 editorToUse.incidenceWrapper.triggerEditMode();
161 editorToUse.editMode = true;
162 }
163
164 function setUpDelete(incidencePtr, deleteDate) {
165 let incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}', utilsObject, "incidence");
166 incidenceWrapper.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
167
168 const openDialogWindow = appMain.pageStack.pushDialogLayer(appMain.deleteIncidencePageComponent, {
169 incidenceWrapper: incidenceWrapper,
170 deleteDate: deleteDate
171 }, {
172 width: Maui.Style.units.gridUnit * 32,
173 height: Maui.Style.units.gridUnit * 6
174 });
175
176 openDialogWindow.Keys.escapePressed.connect(function() { openDialogWindow.closeDialog() });
177 }
178
179 function completeTodo(incidencePtr) {
180 let todo = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
181 utilsObject, "incidence");
182
183 todo.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
184
185 if(todo.incidenceType === IncidenceWrapper.TypeTodo) {
186 todo.todoCompleted = !todo.todoCompleted;
187 CalendarManager.editIncidence(todo);
188 }
189 }
190
191 function setUpIncidenceDateChange(incidenceWrapper, startOffset, endOffset, occurrenceDate, caughtDelegate, allDay=null) {
192 appMain.pageStack.currentItem.dragDropEnabled = false;
193
194 if(appMain.pageStack.layers.currentItem && appMain.pageStack.layers.currentItem.dragDropEnabled) {
195 appMain.pageStack.layers.currentItem.dragDropEnabled = false;
196 }
197
198 if(incidenceWrapper.recurrenceData.type === 0) {
199 if (allDay !== null) {
200 incidenceWrapper.allDay = allDay;
201 }
202 CalendarManager.updateIncidenceDates(incidenceWrapper, startOffset, endOffset);
203 } else {
204 const onClosingHandler = () => { caughtDelegate.caught = false; utilsObject.reenableDragOnCurrentView(); };
205 const openDialogWindow = appMain.pageStack.pushDialogLayer(appMain.recurringIncidenceChangePageComponent, {
206 incidenceWrapper: incidenceWrapper,
207 startOffset: startOffset,
208 endOffset: endOffset,
209 occurrenceDate: occurrenceDate,
210 caughtDelegate: caughtDelegate,
211 allDay: allDay
212 }, {
213 width: Maui.Style.units.gridUnit * 34,
214 height: Maui.Style.units.gridUnit * 6,
215 onClosing: onClosingHandler()
216 });
217
218 openDialogWindow.Keys.escapePressed.connect(function() { openDialogWindow.closeDialog() });
219 }
220 }
221
222 function reenableDragOnCurrentView() {
223 appMain.pageStack.currentItem.dragDropEnabled = true;
224
225 if(appMain.pageStack.layers.currentItem && appMain.pageStack.layers.currentItem.dragDropEnabled) {
226 appMain.pageStack.layers.currentItem.dragDropEnabled = true;
227 }
228 }
229
230 function openDayLayer(selectedDate) {
231 appMain.dayScaleModelLoaderItem.active = true;
232
233 if(!isNaN(selectedDate.getTime())) {
234 appMain.selectedDate = selectedDate;
235
236 appMain.dayViewAction.trigger();
237 }
238 }
239}
This class is a wrapper for a KCalendarCore::Incidence::Ptr object.
AKONADI_CALENDAR_EXPORT KCalendarCore::Todo::Ptr todo(const Akonadi::Item &item)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:50:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.