Eventviews

mainwindow.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
3 SPDX-FileContributor: Kevin Krammer <krake@kdab.com>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "mainwindow.h"
9using namespace Qt::Literals::StringLiterals;
10
11#include "settings.h"
12
13#include "agenda/agenda.h"
14#include "agenda/agendaview.h"
15#include "calendarview_debug.h"
16#include "month/monthview.h"
17#include "multiagenda/multiagendaview.h"
18#include "prefs.h"
19#include "timeline/timelineview.h"
20
21#include <Akonadi/Collection>
22#include <Akonadi/CollectionFilterProxyModel>
23#include <Akonadi/ControlGui>
24#include <Akonadi/EntityTreeModel>
25#include <Akonadi/IncidenceChanger>
26#include <Akonadi/ItemFetchScope>
27#include <Akonadi/Monitor>
28
29#include <CalendarSupport/CollectionSelection>
30
31#include <KCalendarCore/Event>
32
33#include <KCheckableProxyModel>
34
35using namespace Akonadi;
36using namespace CalendarSupport;
37using namespace EventViews;
38
39MainWindow::MainWindow(const QStringList &viewNames)
40 : QMainWindow()
41 , mViewNames(viewNames)
42 , mIncidenceChanger(nullptr)
43 , mSettings(nullptr)
44 , mViewPreferences(nullptr)
45{
46 mUi.setupUi(this);
47 mUi.tabWidget->clear();
48
49 connect(mUi.addViewMenu, &QMenu::triggered, this, &MainWindow::addViewTriggered);
50
52
53 setGeometry(0, 0, 800, 600);
55}
56
57MainWindow::~MainWindow()
58{
59 delete mViewPreferences;
60 delete mSettings;
61}
62
63void MainWindow::addView(const QString &viewName)
64{
65 EventView *eventView = nullptr;
66
67 const auto start = QDateTime::currentDateTime().addDays(-1);
68 const auto end = QDateTime::currentDateTime().addDays(1);
69
70 if (viewName == "agenda"_L1) {
71 eventView = new AgendaView(start.date(), end.date(), true, false, this);
72 } else if (viewName == "multiagenda"_L1) {
73 eventView = new MultiAgendaView(this);
74 } else if (viewName == "month"_L1) {
75 eventView = new MonthView(MonthView::Visible, this);
76 } else if (viewName == "timeline"_L1) {
77 eventView = new TimelineView(this);
78 }
79
80 if (eventView) {
81 eventView->setPreferences(*mViewPreferences);
82 eventView->setIncidenceChanger(mIncidenceChanger);
83 eventView->updateConfig();
84
85 for (const auto &calendar : mCalendars) {
86 eventView->addCalendar(calendar);
87 }
88
89 eventView->setDateRange(start, end);
90
91 mUi.tabWidget->addTab(eventView, viewName);
92 mEventViews.push_back(eventView);
93 } else {
94 qCCritical(CALENDARVIEW_LOG) << "Cannot create view" << viewName;
95 }
96}
97
98void MainWindow::delayedInit()
99{
100 // create our application settings
101 mSettings = new Settings;
102
103 // create view preferences so that matching values are retrieved from
104 // application settings
105 mViewPreferences = new PrefsPtr(new Prefs(mSettings));
106
107 mMonitor = new Akonadi::Monitor(this);
108 for (const auto &mt : KCalendarCore::Incidence::mimeTypes()) {
109 mMonitor->setMimeTypeMonitored(mt);
110 }
111 mMonitor->itemFetchScope().fetchFullPayload();
113 mEtm = new Akonadi::EntityTreeModel(mMonitor, this);
114
115 auto collectionProxy = new Akonadi::CollectionFilterProxyModel(mEtm);
116 collectionProxy->addMimeTypeFilters(KCalendarCore::Incidence::mimeTypes());
117 collectionProxy->setSourceModel(mEtm);
118
119 auto selectionModel = new QItemSelectionModel(collectionProxy, mEtm);
120
121 auto checkableProxy = new KCheckableProxyModel(mEtm);
122 checkableProxy->setSourceModel(collectionProxy);
123 checkableProxy->setSelectionModel(selectionModel);
124
125 mUi.calendarView->setModel(checkableProxy);
126
127 CalendarSupport::CollectionSelection *collectionSelection = new CalendarSupport::CollectionSelection(selectionModel, this);
128 EventViews::EventView::setGlobalCollectionSelection(collectionSelection);
129
130 connect(collectionSelection, &CalendarSupport::CollectionSelection::collectionSelected, this, &MainWindow::collectionSelected);
131 connect(collectionSelection, &CalendarSupport::CollectionSelection::collectionDeselected, this, &MainWindow::collectionDeselected);
132
133 for (const auto &collection : collectionSelection->selectedCollections()) {
134 collectionSelected(collection);
135 }
136
137 mIncidenceChanger = new IncidenceChanger(this);
138
139 for (const QString &viewName : std::as_const(mViewNames)) {
140 addView(viewName);
141 }
142}
143
144void MainWindow::addViewTriggered(QAction *action)
145{
146 QString viewName = action->text().toLower();
147 viewName.remove(QLatin1Char('&'));
148 addView(viewName);
149}
150
151void MainWindow::collectionSelected(const Akonadi::Collection &col)
152{
153 qDebug() << "Collection selected id=" << col.id() << "name=" << col.displayName();
154 auto calendar = Akonadi::CollectionCalendar::Ptr::create(mEtm, col);
155 mCalendars.push_back(calendar);
156
157 for (auto view : mEventViews) {
158 view->addCalendar(calendar);
159 view->updateView();
160 }
161}
162
163void MainWindow::collectionDeselected(const Akonadi::Collection &col)
164{
165 qDebug() << "Collection deselected id=" << col.id() << "name=" << col.displayName();
166 auto calendar = std::find_if(mCalendars.begin(), mCalendars.end(), [col](const auto &cal) {
167 return cal->collection() == col;
168 });
169 if (calendar == mCalendars.cend()) {
170 return;
171 }
172
173 const auto start = QDateTime::currentDateTime().addDays(-1);
174 const auto end = QDateTime::currentDateTime().addDays(1);
175
176 for (auto view : mEventViews) {
177 view->removeCalendar(*calendar);
178 view->updateView();
179 }
180
181 mCalendars.erase(calendar);
182}
183#include "moc_mainwindow.cpp"
QString displayName() const
static void widgetNeedsAkonadi(QWidget *widget)
void setAncestorRetrieval(AncestorRetrieval ancestorDepth)
void fetchFullPayload(bool fetch=true)
void setMimeTypeMonitored(const QString &mimetype, bool monitored=true)
ItemFetchScope & itemFetchScope()
AgendaView is the agenda-like view that displays events in a single or multi-day view.
Definition agendaview.h:70
EventView is the abstract base class from which all other calendar views for event data are derived.
Definition eventview.h:69
virtual void setDateRange(const QDateTime &start, const QDateTime &end, const QDate &preferredMonth=QDate())
virtual void updateConfig()
Re-reads the configuration and picks up relevant changes which are applicable to the view.
virtual void setIncidenceChanger(Akonadi::IncidenceChanger *changer)
Assign a new incidence change helper object.
Shows one agenda for every resource side-by-side.
This class provides a view showing which blocks of time are occupied by events in the user's calendar...
static QStringList mimeTypes()
Q_SCRIPTABLE Q_NOREPLY void start()
Namespace EventViews provides facilities for displaying incidences, including events,...
Definition agenda.h:33
const QList< QKeySequence > & end()
QDateTime addDays(qint64 ndays) const const
QDateTime currentDateTime()
void push_back(parameter_type value)
void triggered(QAction *action)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QSharedPointer< T > create(Args &&... args)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QueuedConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setupUi(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.