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"
8using namespace Qt::Literals::StringLiterals;
9
10#include "attendeecomboboxdelegate.h"
11#include "attendeelineeditdelegate.h"
12#include "incidencedatetime.h"
13#include "resourcemanagement.h"
14#include "resourcemodel.h"
15
16#include "ui_dialogdesktop.h"
17
18#include <KDescendantsProxyModel>
19#include <KEmailAddress>
20#include <QCompleter>
21
22using namespace IncidenceEditorNG;
23
24class SwitchRoleProxy : public QSortFilterProxyModel
25{
26public:
27 explicit SwitchRoleProxy(QObject *parent = nullptr)
29 {
30 }
31
32 [[nodiscard]] QVariant data(const QModelIndex &index, int role) const override
33 {
34 QVariant d;
35 if (role == Qt::DisplayRole || role == Qt::EditRole) {
36 d = QSortFilterProxyModel::data(index, ResourceModel::FullName);
37 return d;
38 }
40 return d;
41 }
42};
43
44IncidenceResource::IncidenceResource(IncidenceAttendee *ieAttendee, IncidenceDateTime *dateTime, Ui::EventOrTodoDesktop *ui)
45 : IncidenceEditor(nullptr)
46 , mUi(ui)
47 , dataModel(ieAttendee->dataModel())
48 , mDateTime(dateTime)
49 , resourceDialog(new ResourceManagement())
50{
51 setObjectName("IncidenceResource"_L1);
52 connect(resourceDialog, &ResourceManagement::accepted, this, &IncidenceResource::dialogOkPressed);
53
54 connect(mDateTime, &IncidenceDateTime::startDateChanged, this, &IncidenceResource::slotDateChanged);
55 connect(mDateTime, &IncidenceDateTime::endDateChanged, this, &IncidenceResource::slotDateChanged);
56
57 QStringList attrs;
58 attrs << QStringLiteral("cn") << QStringLiteral("mail");
59
60 completer = new QCompleter(this);
61 auto model = new ResourceModel(attrs, this);
62
63 auto proxyModel = new KDescendantsProxyModel(this);
64 proxyModel->setSourceModel(model);
65 auto proxyModel2 = new SwitchRoleProxy(this);
66 proxyModel2->setSourceModel(proxyModel);
67
68 completer->setModel(proxyModel2);
69 completer->setCompletionRole(ResourceModel::FullName);
70 completer->setWrapAround(false);
71 mUi->mNewResource->setCompleter(completer);
72
73 auto attendeeDelegate = new AttendeeLineEditDelegate(this);
74
75 auto filterProxyModel = new ResourceFilterProxyModel(this);
76 filterProxyModel->setSourceModel(dataModel);
77
78 mUi->mResourcesTable->setModel(filterProxyModel);
79 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Role, ieAttendee->roleDelegate());
80 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::FullName, attendeeDelegate);
81 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Status, ieAttendee->stateDelegate());
82 mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Response, ieAttendee->responseDelegate());
83
84 connect(mUi->mFindResourcesButton, &QPushButton::clicked, this, &IncidenceResource::findResources);
85 connect(mUi->mBookResourceButton, &QPushButton::clicked, this, &IncidenceResource::bookResource);
86 connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this, &IncidenceResource::layoutChanged);
87 connect(filterProxyModel, &ResourceFilterProxyModel::modelReset, this, &IncidenceResource::layoutChanged);
88 connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this, &IncidenceResource::updateCount);
89 connect(filterProxyModel, &ResourceFilterProxyModel::rowsInserted, this, &IncidenceResource::updateCount);
90 connect(filterProxyModel, &ResourceFilterProxyModel::rowsRemoved, this, &IncidenceResource::updateCount);
91 connect(filterProxyModel, &ResourceFilterProxyModel::modelReset, this, &IncidenceResource::updateCount);
92 // only update when FullName is changed
93 connect(filterProxyModel, &ResourceFilterProxyModel::dataChanged, this, &IncidenceResource::updateCount);
94}
95
96IncidenceResource::~IncidenceResource()
97{
98 delete resourceDialog;
99}
100
101void IncidenceResource::load(const KCalendarCore::Incidence::Ptr &incidence)
102{
103 Q_UNUSED(incidence)
104 slotDateChanged();
105}
106
107void IncidenceResource::slotDateChanged()
108{
109 resourceDialog->slotDateChanged(mDateTime->startDate(), mDateTime->endDate());
110}
111
112void IncidenceResource::save(const KCalendarCore::Incidence::Ptr &incidence)
113{
114 Q_UNUSED(incidence)
115 // all logic inside IncidenceAtendee (using same model)
116}
117
118bool IncidenceResource::isDirty() const
119{
120 // all logic inside IncidenceAtendee (using same model)
121 return false;
122}
123
124void IncidenceResource::bookResource()
125{
126 if (mUi->mNewResource->text().trimmed().isEmpty()) {
127 return;
128 }
130 QString email;
131 KEmailAddress::extractEmailAddressAndName(mUi->mNewResource->text(), email, name);
132 KCalendarCore::Attendee attendee(name, email);
133 attendee.setCuType(KCalendarCore::Attendee::Resource);
134 dataModel->insertAttendee(dataModel->rowCount(), attendee);
135}
136
137void IncidenceResource::findResources()
138{
139 resourceDialog->show();
140}
141
142void IncidenceResource::dialogOkPressed()
143{
144 ResourceItem::Ptr item = resourceDialog->selectedItem();
145 if (item) {
146 const QString name = QString::fromLatin1(item->ldapObject().value(QStringLiteral("cn")));
147 const QString email = QString::fromLatin1(item->ldapObject().value(QStringLiteral("mail")));
148 KCalendarCore::Attendee attendee(name, email);
149 attendee.setCuType(KCalendarCore::Attendee::Resource);
150 dataModel->insertAttendee(dataModel->rowCount(), attendee);
151 }
152}
153
154void IncidenceResource::layoutChanged()
155{
156 QHeaderView *headerView = mUi->mResourcesTable->horizontalHeader();
157 headerView->setSectionHidden(AttendeeTableModel::CuType, true);
158 headerView->setSectionHidden(AttendeeTableModel::Name, true);
159 headerView->setSectionHidden(AttendeeTableModel::Email, true);
160 headerView->setSectionResizeMode(AttendeeTableModel::Role, QHeaderView::ResizeToContents);
161 headerView->setSectionResizeMode(AttendeeTableModel::FullName, QHeaderView::Stretch);
162 headerView->setSectionResizeMode(AttendeeTableModel::Available, QHeaderView::ResizeToContents);
163 headerView->setSectionResizeMode(AttendeeTableModel::Status, QHeaderView::ResizeToContents);
164 headerView->setSectionResizeMode(AttendeeTableModel::Response, QHeaderView::ResizeToContents);
165}
166
167void IncidenceResource::updateCount()
168{
169 Q_EMIT resourceCountChanged(resourceCount());
170}
171
172int IncidenceResource::resourceCount() const
173{
174 int c = 0;
175 QModelIndex index;
176 QAbstractItemModel *model = mUi->mResourcesTable->model();
177 if (!model) {
178 return 0;
179 }
180 const int nbRow = model->rowCount(QModelIndex());
181 for (int i = 0; i < nbRow; ++i) {
182 index = model->index(i, AttendeeTableModel::FullName);
183 if (!model->data(index).toString().isEmpty()) {
184 ++c;
185 }
186 }
187 return c;
188}
189
190#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(StandardAction 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
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 Fri Oct 11 2024 12:16:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.