Incidenceeditor

incidenceresource.cpp
1/*
2 * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
5 */
6
7#include "incidenceresource.h"
8#include "attendeecomboboxdelegate.h"
9#include "attendeelineeditdelegate.h"
10#include "incidencedatetime.h"
11#include "resourcemanagement.h"
12#include "resourcemodel.h"
13
14#include "ui_dialogdesktop.h"
15
16#include <KDescendantsProxyModel>
17#include <KEmailAddress>
18#include <QCompleter>
19
20using namespace IncidenceEditorNG;
21
22class SwitchRoleProxy : public QSortFilterProxyModel
23{
24public:
25 explicit SwitchRoleProxy(QObject *parent = nullptr)
27 {
28 }
29
30 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override
31 {
32 QVariant d;
33 if (role == Qt::DisplayRole || role == Qt::EditRole) {
34 d = QSortFilterProxyModel::data(index, ResourceModel::FullName);
35 return d;
36 }
38 return d;
39 }
40};
41
42IncidenceResource::IncidenceResource(IncidenceAttendee *ieAttendee, IncidenceDateTime *dateTime, Ui::EventOrTodoDesktop *ui)
43 : IncidenceEditor(nullptr)
44 , mUi(ui)
45 , dataModel(ieAttendee->dataModel())
46 , mDateTime(dateTime)
47 , resourceDialog(new ResourceManagement())
48{
49 setObjectName(QLatin1StringView("IncidenceResource"));
50 connect(resourceDialog, &ResourceManagement::accepted, this, &IncidenceResource::dialogOkPressed);
51
52 connect(mDateTime, &IncidenceDateTime::startDateChanged, this, &IncidenceResource::slotDateChanged);
53 connect(mDateTime, &IncidenceDateTime::endDateChanged, this, &IncidenceResource::slotDateChanged);
54
55 QStringList attrs;
56 attrs << QStringLiteral("cn") << QStringLiteral("mail");
57
58 completer = new QCompleter(this);
59 auto model = new ResourceModel(attrs, this);
60
61 auto proxyModel = new KDescendantsProxyModel(this);
62 proxyModel->setSourceModel(model);
63 auto proxyModel2 = new SwitchRoleProxy(this);
64 proxyModel2->setSourceModel(proxyModel);
65
66 completer->setModel(proxyModel2);
67 completer->setCompletionRole(ResourceModel::FullName);
68 completer->setWrapAround(false);
69 mUi->mNewResource->setCompleter(completer);
70
71 auto attendeeDelegate = new AttendeeLineEditDelegate(this);
72
73 auto filterProxyModel = new ResourceFilterProxyModel(this);
74 filterProxyModel->setDynamicSortFilter(true);
75 filterProxyModel->setSourceModel(dataModel);
76
77 mUi->mResourcesTable->setModel(filterProxyModel);
78 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Role, ieAttendee->roleDelegate());
79 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::FullName, attendeeDelegate);
80 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Status, ieAttendee->stateDelegate());
81 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Response, ieAttendee->responseDelegate());
82
83 connect(mUi->mFindResourcesButton, &QPushButton::clicked, this, &IncidenceResource::findResources);
84 connect(mUi->mBookResourceButton, &QPushButton::clicked, this, &IncidenceResource::bookResource);
85 connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this, &IncidenceResource::layoutChanged);
86 connect(filterProxyModel, &ResourceFilterProxyModel::modelReset, this, &IncidenceResource::layoutChanged);
87 connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this, &IncidenceResource::updateCount);
88 connect(filterProxyModel, &ResourceFilterProxyModel::rowsInserted, this, &IncidenceResource::updateCount);
89 connect(filterProxyModel, &ResourceFilterProxyModel::rowsRemoved, this, &IncidenceResource::updateCount);
90 connect(filterProxyModel, &ResourceFilterProxyModel::modelReset, this, &IncidenceResource::updateCount);
91 // only update when FullName is changed
92 connect(filterProxyModel, &ResourceFilterProxyModel::dataChanged, this, &IncidenceResource::updateCount);
93}
94
95IncidenceResource::~IncidenceResource()
96{
97 delete resourceDialog;
98}
99
100void IncidenceResource::load(const KCalendarCore::Incidence::Ptr &incidence)
101{
103 slotDateChanged();
104}
105
106void IncidenceResource::slotDateChanged()
107{
108 resourceDialog->slotDateChanged(mDateTime->startDate(), mDateTime->endDate());
109}
110
111void IncidenceResource::save(const KCalendarCore::Incidence::Ptr &incidence)
112{
114 // all logic inside IncidenceAtendee (using same model)
115}
116
117bool IncidenceResource::isDirty() const
118{
119 // all logic inside IncidenceAtendee (using same model)
120 return false;
121}
122
123void IncidenceResource::bookResource()
124{
125 if (mUi->mNewResource->text().trimmed().isEmpty()) {
126 return;
127 }
129 QString email;
130 KEmailAddress::extractEmailAddressAndName(mUi->mNewResource->text(), email, name);
131 KCalendarCore::Attendee attendee(name, email);
132 attendee.setCuType(KCalendarCore::Attendee::Resource);
133 dataModel->insertAttendee(dataModel->rowCount(), attendee);
134}
135
136void IncidenceResource::findResources()
137{
138 resourceDialog->show();
139}
140
141void IncidenceResource::dialogOkPressed()
142{
143 ResourceItem::Ptr item = resourceDialog->selectedItem();
144 if (item) {
145 const QString name = QString::fromLatin1(item->ldapObject().value(QStringLiteral("cn")));
146 const QString email = QString::fromLatin1(item->ldapObject().value(QStringLiteral("mail")));
147 KCalendarCore::Attendee attendee(name, email);
148 attendee.setCuType(KCalendarCore::Attendee::Resource);
149 dataModel->insertAttendee(dataModel->rowCount(), attendee);
150 }
151}
152
153void IncidenceResource::layoutChanged()
154{
155 QHeaderView *headerView = mUi->mResourcesTable->horizontalHeader();
156 headerView->setSectionHidden(AttendeeTableModel::CuType, true);
157 headerView->setSectionHidden(AttendeeTableModel::Name, true);
158 headerView->setSectionHidden(AttendeeTableModel::Email, true);
159 headerView->setSectionResizeMode(AttendeeTableModel::Role, QHeaderView::ResizeToContents);
160 headerView->setSectionResizeMode(AttendeeTableModel::FullName, QHeaderView::Stretch);
161 headerView->setSectionResizeMode(AttendeeTableModel::Available, QHeaderView::ResizeToContents);
162 headerView->setSectionResizeMode(AttendeeTableModel::Status, QHeaderView::ResizeToContents);
163 headerView->setSectionResizeMode(AttendeeTableModel::Response, QHeaderView::ResizeToContents);
164}
165
166void IncidenceResource::updateCount()
167{
168 Q_EMIT resourceCountChanged(resourceCount());
169}
170
171int IncidenceResource::resourceCount() const
172{
173 int c = 0;
174 QModelIndex index;
175 QAbstractItemModel *model = mUi->mResourcesTable->model();
176 if (!model) {
177 return 0;
178 }
179 const int nbRow = model->rowCount(QModelIndex());
180 for (int i = 0; i < nbRow; ++i) {
181 index = model->index(i, AttendeeTableModel::FullName);
182 if (!model->data(index).toString().isEmpty()) {
183 ++c;
184 }
185 }
186 return c;
187}
188
189#include "moc_incidenceresource.cpp"
KCal Incidences are complicated objects.
QSharedPointer< IncidenceT > incidence() const
Convenience method to get a pointer for a specific const Incidence Type.
The ResourceManagement class.
KCODECS_EXPORT bool extractEmailAddressAndName(const QString &aStr, QString &mail, QString &name)
QString name(StandardShortcut id)
void clicked(bool checked)
virtual QVariant data(const QModelIndex &index, int role) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual int rowCount(const QModelIndex &parent) const const=0
void setSectionHidden(int logicalIndex, bool hide)
void setSectionResizeMode(ResizeMode mode)
Q_EMITQ_EMIT
QObject * parent() const const
T qobject_cast(QObject *object)
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
DisplayRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:37 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.