Commit 69f3aaa9 authored by Claudio Cambra's avatar Claudio Cambra
Browse files

Remove functions from main.qml and move to new KalendarUiUtils singleton

This should help us clean up our code throughout other parts of Kalendar (i.e. easier to split up further parts of `main.qml`, avoid the excessive signal passing we employ at the moment, etc.)
parent 944fa8ab
Pipeline #177726 failed with stage
in 6 minutes and 5 seconds
// SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@gmail.com>
// SPDX-License-Identifier: GPL-3.0-or-later
pragma Singleton
import QtQuick 2.15
import org.kde.kirigami 2.14 as Kirigami
import "dateutils.js" as DateUtils
import "labelutils.js" as LabelUtils
import org.kde.kalendar 1.0
import org.kde.kalendar.contact 1.0
QtObject {
id: utilsObject
property var appMain
function switchView(newViewComponent, viewSettings) {
if(appMain.pageStack.layers.depth > 1) {
appMain.pageStack.layers.pop(appMain.pageStack.layers.initialItem);
}
if (appMain.pageStack.depth > 1) {
appMain.pageStack.pop();
}
appMain.pageStack.replace(newViewComponent);
if (appMain.filterHeaderLoaderItem.active && appMain.pageStack.currentItem.mode !== KalendarApplication.Contact) {
appMain.pageStack.currentItem.header = appMain.filterHeaderLoaderItem.item;
}
if(viewSettings) {
for(const [key, value] of Object.entries(viewSettings)) {
appMain.pageStack.currentItem[key] = value;
}
}
if (appMain.pageStack.currentItem.mode === KalendarApplication.Event) {
appMain.pageStack.currentItem.setToDate(appMain.selectedDate, true);
}
}
function editorToUse() {
if (!Kirigami.Settings.isMobile) {
appMain.editorWindowedLoaderItem.active = true
return appMain.editorWindowedLoaderItem.item.incidenceEditor
} else {
appMain.pageStack.layers.push(incidenceEditor);
return incidenceEditor;
}
}
function setUpAdd(type, addDate, collectionId, includeTime) {
let editorToUse = utilsObject.editorToUse();
if (editorToUse.editMode || !editorToUse.incidenceWrapper) {
editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
editorToUse, "incidence");
}
editorToUse.editMode = false;
if(type === IncidenceWrapper.TypeEvent) {
editorToUse.incidenceWrapper.setNewEvent();
} else if (type === IncidenceWrapper.TypeTodo) {
editorToUse.incidenceWrapper.setNewTodo();
}
if(addDate !== undefined && !isNaN(addDate.getTime())) {
let existingStart = editorToUse.incidenceWrapper.incidenceStart;
let existingEnd = editorToUse.incidenceWrapper.incidenceEnd;
let newStart = addDate;
let newEnd = new Date(newStart.getFullYear(), newStart.getMonth(), newStart.getDate(), newStart.getHours() + 1, newStart.getMinutes());
if(!includeTime) {
newStart = new Date(addDate.setHours(existingStart.getHours(), existingStart.getMinutes()));
newEnd = new Date(addDate.setHours(existingStart.getHours() + 1, existingStart.getMinutes()));
}
if(type === IncidenceWrapper.TypeEvent) {
editorToUse.incidenceWrapper.incidenceStart = newStart;
editorToUse.incidenceWrapper.incidenceEnd = newEnd;
} else if (type === IncidenceWrapper.TypeTodo) {
editorToUse.incidenceWrapper.incidenceEnd = newStart;
}
}
if(collectionId && collectionId >= 0) {
editorToUse.incidenceWrapper.collectionId = collectionId;
} else if(type === IncidenceWrapper.TypeEvent && Config.lastUsedEventCollection > -1) {
editorToUse.incidenceWrapper.collectionId = Config.lastUsedEventCollection;
} else if (type === IncidenceWrapper.TypeTodo && Config.lastUsedTodoCollection > -1) {
editorToUse.incidenceWrapper.collectionId = Config.lastUsedTodoCollection;
} else {
editorToUse.incidenceWrapper.collectionId = CalendarManager.defaultCalendarId(editorToUse.incidenceWrapper);
}
}
function setUpAddSubTodo(parentWrapper) {
let editorToUse = utilsObject.editorToUse();
if (editorToUse.editMode || !editorToUse.incidenceWrapper) {
editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
editorToUse, "incidence");
}
editorToUse.editMode = false;
editorToUse.incidenceWrapper.setNewTodo();
editorToUse.incidenceWrapper.parent = parentWrapper.uid;
editorToUse.incidenceWrapper.collectionId = parentWrapper.collectionId;
editorToUse.incidenceWrapper.incidenceStart = parentWrapper.incidenceStart;
editorToUse.incidenceWrapper.incidenceEnd = parentWrapper.incidenceEnd;
}
function setUpView(modelData) {
appMain.contextDrawer.incidenceData = modelData;
appMain.contextDrawer.open();
}
function setUpEdit(incidencePtr) {
let editorToUse = utilsObject.editorToUse();
editorToUse.incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
editorToUse, "incidence");
editorToUse.incidenceWrapper.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
editorToUse.incidenceWrapper.triggerEditMode();
editorToUse.editMode = true;
}
function setUpDelete(incidencePtr, deleteDate) {
let incidenceWrapper = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}', utilsObject, "incidence");
incidenceWrapper.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
const openDialogWindow = appMain.pageStack.pushDialogLayer(appMain.deleteIncidenceSheetComponent, {
incidenceWrapper: incidenceWrapper,
deleteDate: deleteDate
}, {
width: Kirigami.Units.gridUnit * 32,
height: Kirigami.Units.gridUnit * 6
});
openDialogWindow.Keys.escapePressed.connect(function() { openDialogWindow.closeDialog() });
}
function completeTodo(incidencePtr) {
let todo = Qt.createQmlObject('import org.kde.kalendar 1.0; IncidenceWrapper {id: incidence}',
utilsObject, "incidence");
todo.incidenceItem = CalendarManager.incidenceItem(incidencePtr);
if(todo.incidenceType === IncidenceWrapper.TypeTodo) {
todo.todoCompleted = !todo.todoCompleted;
CalendarManager.editIncidence(todo);
}
}
function setUpIncidenceDateChange(incidenceWrapper, startOffset, endOffset, occurrenceDate, caughtDelegate, allDay=null) {
appMain.pageStack.currentItem.dragDropEnabled = false;
if(appMain.pageStack.layers.currentItem && appMain.pageStack.layers.currentItem.dragDropEnabled) {
appMain.pageStack.layers.currentItem.dragDropEnabled = false;
}
if(incidenceWrapper.recurrenceData.type === 0) {
if (allDay !== null) {
incidenceWrapper.allDay = allDay;
}
CalendarManager.updateIncidenceDates(incidenceWrapper, startOffset, endOffset);
} else {
const onClosingHandler = () => { caughtDelegate.caught = false; utilsObject.reenableDragOnCurrentView(); };
const openDialogWindow = appMain.pageStack.pushDialogLayer(appMain.recurringIncidenceChangeSheetComponent, {
incidenceWrapper: incidenceWrapper,
startOffset: startOffset,
endOffset: endOffset,
occurrenceDate: occurrenceDate,
caughtDelegate: caughtDelegate,
allDay: allDay
}, {
width: Kirigami.Units.gridUnit * 34,
height: Kirigami.Units.gridUnit * 6,
onClosing: onClosingHandler()
});
openDialogWindow.Keys.escapePressed.connect(function() { openDialogWindow.closeDialog() });
}
}
function reenableDragOnCurrentView() {
appMain.pageStack.currentItem.dragDropEnabled = true;
if(appMain.pageStack.layers.currentItem && appMain.pageStack.layers.currentItem.dragDropEnabled) {
appMain.pageStack.layers.currentItem.dragDropEnabled = true;
}
}
function openDayLayer(selectedDate) {
appMain.dayScaleModelLoaderItem.active = true;
if(!isNaN(selectedDate.getTime())) {
appMain.selectedDate = selectedDate;
appMain.dayViewAction.trigger();
}
}
}
This diff is collapsed.
......@@ -138,6 +138,8 @@ int main(int argc, char *argv[])
qmlRegisterType<MonthModel>("org.kde.kalendar", 1, 0, "MonthModel");
qmlRegisterType<InfiniteCalendarViewModel>("org.kde.kalendar", 1, 0, "InfiniteCalendarViewModel");
qmlRegisterSingletonType(QUrl(QStringLiteral("qrc:/KalendarUiUtils.qml")), "org.kde.kalendar.utils", 1, 0, "KalendarUiUtils");
qRegisterMetaType<Akonadi::ETMCalendar::Ptr>();
qRegisterMetaType<QAbstractProxyModel *>("QAbstractProxyModel*");
qRegisterMetaType<Akonadi::AgentFilterProxyModel *>();
......
......@@ -12,14 +12,14 @@ SPDX-License-Identifier: CC0-1.0
<file alias="MultiDayView.qml">contents/ui/MultiDayView.qml</file>
<file alias="MonthView.qml">contents/ui/MonthView.qml</file>
<file alias="DayLabels.qml">contents/ui/DayLabels.qml</file>
<file alias="dateutils.js">contents/ui/dateutils.js</file>
<file alias="dateutils.js">contents/ui/KalendarUtils/dateutils.js</file>
<file alias="IncidenceEditor.qml">contents/ui/IncidenceEditor.qml</file>
<file alias="DeleteIncidenceSheet.qml">contents/ui/DeleteIncidenceSheet.qml</file>
<file alias="DeleteCalendarSheet.qml">contents/ui/DeleteCalendarSheet.qml</file>
<file alias="IncidenceInfo.qml">contents/ui/IncidenceInfo.qml</file>
<file alias="DatePicker.qml">contents/ui/DatePicker.qml</file>
<file alias="TimePicker.qml">contents/ui/TimePicker.qml</file>
<file alias="labelutils.js">contents/ui/labelutils.js</file>
<file alias="labelutils.js">contents/ui/KalendarUtils/labelutils.js</file>
<file alias="TitleDateButton.qml">contents/ui/TitleDateButton.qml</file>
<file alias="ViewTitleDelegate.qml">contents/ui/ViewTitleDelegate.qml</file>
<file alias="DateChanger.qml">contents/ui/DateChanger.qml</file>
......@@ -60,5 +60,6 @@ SPDX-License-Identifier: CC0-1.0
<file alias="RecurringIncidenceChangeSheet.qml">contents/ui/RecurringIncidenceChangeSheet.qml</file>
<file alias="ResizerSeparator.qml">contents/ui/ResizerSeparator.qml</file>
<file alias="LazyContactView.qml">contents/ui/LazyContactView.qml</file>
<file alias="KalendarUiUtils.qml">contents/ui/KalendarUtils/KalendarUiUtils.qml</file>
</qresource>
</RCC>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment