incidenceresource.cpp 6.55 KB
Newer Older
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
8
9
10
 */

#include "incidenceresource.h"
#include "attendeecomboboxdelegate.h"
#include "attendeelineeditdelegate.h"
#include "incidencedatetime.h"
Laurent Montel's avatar
Laurent Montel committed
11
12
#include "resourcemanagement.h"
#include "resourcemodel.h"
13
14
15
16
17

#include "ui_dialogdesktop.h"

#include <KDescendantsProxyModel>
#include <KEmailAddress>
18
#include <QCompleter>
19
20
21

using namespace IncidenceEditorNG;

Laurent Montel's avatar
Laurent Montel committed
22
class SwitchRoleProxy : public QSortFilterProxyModel
23
24
{
public:
Laurent Montel's avatar
Laurent Montel committed
25
    explicit SwitchRoleProxy(QObject *parent = nullptr)
26
27
28
29
        : QSortFilterProxyModel(parent)
    {
    }

30
    QVariant data(const QModelIndex &index, int role) const override
31
32
33
34
35
36
37
38
39
40
41
    {
        QVariant d;
        if (role == Qt::DisplayRole || role == Qt::EditRole) {
            d = QSortFilterProxyModel::data(index, ResourceModel::FullName);
            return d;
        }
        d = QSortFilterProxyModel::data(index, role);
        return d;
    }
};

Laurent Montel's avatar
Laurent Montel committed
42
IncidenceResource::IncidenceResource(IncidenceAttendee *ieAttendee, IncidenceDateTime *dateTime, Ui::EventOrTodoDesktop *ui)
Laurent Montel's avatar
Laurent Montel committed
43
    : IncidenceEditor(nullptr)
44
45
46
47
48
49
    , mUi(ui)
    , dataModel(ieAttendee->dataModel())
    , mDateTime(dateTime)
    , resourceDialog(new ResourceManagement())
{
    setObjectName(QStringLiteral("IncidenceResource"));
Laurent Montel's avatar
Laurent Montel committed
50
    connect(resourceDialog, &ResourceManagement::accepted, this, &IncidenceResource::dialogOkPressed);
51

Laurent Montel's avatar
Laurent Montel committed
52
53
    connect(mDateTime, &IncidenceDateTime::startDateChanged, this, &IncidenceResource::slotDateChanged);
    connect(mDateTime, &IncidenceDateTime::endDateChanged, this, &IncidenceResource::slotDateChanged);
54
55

    QStringList attrs;
Laurent Montel's avatar
Laurent Montel committed
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
65
66
67
68
69
70
    proxyModel2->setSourceModel(proxyModel);

    completer->setModel(proxyModel2);
    completer->setCompletionRole(ResourceModel::FullName);
    completer->setWrapAround(false);
    mUi->mNewResource->setCompleter(completer);

71
    auto attendeeDelegate = new AttendeeLineEditDelegate(this);
72

73
    auto filterProxyModel = new ResourceFilterProxyModel(this);
74
75
76
77
    filterProxyModel->setDynamicSortFilter(true);
    filterProxyModel->setSourceModel(dataModel);

    mUi->mResourcesTable->setModel(filterProxyModel);
Laurent Montel's avatar
Laurent Montel committed
78
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Role, ieAttendee->roleDelegate());
79
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::FullName, attendeeDelegate);
Laurent Montel's avatar
Laurent Montel committed
80
81
82
83
84
85
86
87
88
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Status, ieAttendee->stateDelegate());
    mUi->mResourcesTable->setItemDelegateForColumn(AttendeeTableModel::Response, ieAttendee->responseDelegate());

    connect(mUi->mFindResourcesButton, &QPushButton::clicked, this, &IncidenceResource::findResources);
    connect(mUi->mBookResourceButton, &QPushButton::clicked, this, &IncidenceResource::bookResource);
    connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this, &IncidenceResource::layoutChanged);
    connect(filterProxyModel, &ResourceFilterProxyModel::layoutChanged, this, &IncidenceResource::updateCount);
    connect(filterProxyModel, &ResourceFilterProxyModel::rowsInserted, this, &IncidenceResource::updateCount);
    connect(filterProxyModel, &ResourceFilterProxyModel::rowsRemoved, this, &IncidenceResource::updateCount);
89
    // only update when FullName is changed
Laurent Montel's avatar
Laurent Montel committed
90
    connect(filterProxyModel, &ResourceFilterProxyModel::dataChanged, this, &IncidenceResource::updateCount);
91
92
}

Laurent Montel's avatar
Laurent Montel committed
93
94
95
96
97
IncidenceResource::~IncidenceResource()
{
    delete resourceDialog;
}

98
void IncidenceResource::load(const KCalendarCore::Incidence::Ptr &incidence)
99
{
Laurent Montel's avatar
Laurent Montel committed
100
    Q_UNUSED(incidence)
101
102
103
104
105
106
107
108
    slotDateChanged();
}

void IncidenceResource::slotDateChanged()
{
    resourceDialog->slotDateChanged(mDateTime->startDate(), mDateTime->endDate());
}

109
void IncidenceResource::save(const KCalendarCore::Incidence::Ptr &incidence)
110
{
Laurent Montel's avatar
Laurent Montel committed
111
    Q_UNUSED(incidence)
Laurent Montel's avatar
Laurent Montel committed
112
    // all logic inside IncidenceAtendee (using same model)
113
114
115
116
}

bool IncidenceResource::isDirty() const
{
Laurent Montel's avatar
Laurent Montel committed
117
    // all logic inside IncidenceAtendee (using same model)
118
119
120
121
122
123
124
125
126
127
    return false;
}

void IncidenceResource::bookResource()
{
    if (mUi->mNewResource->text().trimmed().isEmpty()) {
        return;
    }
    QString name, email;
    KEmailAddress::extractEmailAddressAndName(mUi->mNewResource->text(), email, name);
128
129
    KCalendarCore::Attendee attendee(name, email);
    attendee.setCuType(KCalendarCore::Attendee::Resource);
130
131
132
133
134
135
136
137
138
139
140
141
    dataModel->insertAttendee(dataModel->rowCount(), attendee);
}

void IncidenceResource::findResources()
{
    resourceDialog->show();
}

void IncidenceResource::dialogOkPressed()
{
    ResourceItem::Ptr item = resourceDialog->selectedItem();
    if (item) {
Laurent Montel's avatar
Laurent Montel committed
142
143
        const QString name = QString::fromLatin1(item->ldapObject().value(QStringLiteral("cn")));
        const QString email = QString::fromLatin1(item->ldapObject().value(QStringLiteral("mail")));
144
145
        KCalendarCore::Attendee attendee(name, email);
        attendee.setCuType(KCalendarCore::Attendee::Resource);
146
147
148
149
150
151
152
153
154
155
        dataModel->insertAttendee(dataModel->rowCount(), attendee);
    }
}

void IncidenceResource::layoutChanged()
{
    QHeaderView *headerView = mUi->mResourcesTable->horizontalHeader();
    headerView->setSectionHidden(AttendeeTableModel::CuType, true);
    headerView->setSectionHidden(AttendeeTableModel::Name, true);
    headerView->setSectionHidden(AttendeeTableModel::Email, true);
Laurent Montel's avatar
Laurent Montel committed
156
    headerView->setSectionResizeMode(AttendeeTableModel::Role, QHeaderView::ResizeToContents);
Laurent Montel's avatar
Laurent Montel committed
157
    headerView->setSectionResizeMode(AttendeeTableModel::FullName, QHeaderView::Stretch);
Laurent Montel's avatar
Laurent Montel committed
158
159
160
    headerView->setSectionResizeMode(AttendeeTableModel::Available, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(AttendeeTableModel::Status, QHeaderView::ResizeToContents);
    headerView->setSectionResizeMode(AttendeeTableModel::Response, QHeaderView::ResizeToContents);
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
}

void IncidenceResource::updateCount()
{
    Q_EMIT resourceCountChanged(resourceCount());
}

int IncidenceResource::resourceCount() const
{
    int c = 0;
    QModelIndex index;
    QAbstractItemModel *model = mUi->mResourcesTable->model();
    if (!model) {
        return 0;
    }
    const int nbRow = model->rowCount(QModelIndex());
    for (int i = 0; i < nbRow; ++i) {
        index = model->index(i, AttendeeTableModel::FullName);
        if (!model->data(index).toString().isEmpty()) {
            ++c;
        }
    }
    return c;
}