2 Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
3 Copyright (C) 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "schedulingdialog.h"
22 #include "conflictresolver.h"
23 #include "freebusymodel/freeperiodmodel.h"
24 #ifndef KDEPIM_MOBILE_UI
25 #include "visualfreebusywidget.h"
27 #include <KCalUtils/Stringify>
29 #include <KCalendarSystem>
31 #include <KIconLoader>
32 #include <KLocalizedString>
33 #include <KConfigGroup>
34 #include <QDialogButtonBox>
35 #include <QPushButton>
36 #include <QVBoxLayout>
38 using namespace IncidenceEditorNG
;
40 SchedulingDialog::SchedulingDialog(const QDate
&startDate
, const QTime
&startTime
, int duration
,
41 ConflictResolver
*resolver
, QWidget
*parent
)
42 : QDialog(parent
), mResolver(resolver
), mPeriodModel(new KPIM::FreePeriodModel(this))
44 QVBoxLayout
*mainLayout
= new QVBoxLayout
;
45 setLayout(mainLayout
);
46 QWidget
*w
= new QWidget(this);
48 QDialogButtonBox
*buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel
);
49 QPushButton
*okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
50 okButton
->setDefault(true);
51 okButton
->setShortcut(Qt::CTRL
| Qt::Key_Return
);
52 connect(buttonBox
, &QDialogButtonBox::accepted
, this, &SchedulingDialog::accept
);
53 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &SchedulingDialog::reject
);
55 mainLayout
->addWidget(buttonBox
);
56 mainLayout
->addWidget(w
);
59 Q_ASSERT(duration
> 0);
62 #ifndef KDEPIM_MOBILE_UI
63 mVisualWidget
= new VisualFreeBusyWidget(resolver
->model(), 8);
64 QVBoxLayout
*ganttlayout
= new QVBoxLayout(mGanttTab
);
66 mGanttTab
->setLayout(ganttlayout
);
67 ganttlayout
->addWidget(mVisualWidget
);
70 connect(mStartDate
, &KDateComboBox::dateEdited
, mResolver
, &ConflictResolver::setEarliestDate
);
71 connect(mStartTime
, &KTimeComboBox::timeEdited
, mResolver
, &ConflictResolver::setEarliestTime
);
72 connect(mEndDate
, &KDateComboBox::dateEdited
, mResolver
, &ConflictResolver::setLatestDate
);
73 connect(mEndTime
, &KTimeComboBox::timeEdited
, mResolver
, &ConflictResolver::setLatestTime
);
75 connect(mStartDate
, &KDateComboBox::dateEdited
, this, &SchedulingDialog::slotStartDateChanged
);
77 connect(mWeekdayCombo
, &KPIM::KWeekdayCheckCombo::checkedItemsChanged
, this, &SchedulingDialog::slotWeekdaysChanged
);
78 connect(mWeekdayCombo
, &KPIM::KWeekdayCheckCombo::checkedItemsChanged
, this, &SchedulingDialog::slotMandatoryRolesChanged
);
80 connect(mResolver
, &ConflictResolver::freeSlotsAvailable
, mPeriodModel
, &KPIM::FreePeriodModel::slotNewFreePeriods
);
81 connect(mMoveBeginTimeEdit
, &KTimeComboBox::timeEdited
, this, &SchedulingDialog::slotSetEndTimeLabel
);
83 mTableView
->setModel(mPeriodModel
);
84 connect(mTableView
->selectionModel(), SIGNAL(currentRowChanged(QModelIndex
,QModelIndex
)), this, SLOT(slotRowSelectionChanged(QModelIndex
,QModelIndex
)));
86 mStartDate
->setDate(startDate
);
87 mEndDate
->setDate(mStartDate
->date().addDays(7));
88 mStartTime
->setTime(startTime
);
89 mEndTime
->setTime(startTime
);
91 mResolver
->setEarliestDate(mStartDate
->date());
92 mResolver
->setEarliestTime(mStartTime
->time());
93 mResolver
->setLatestDate(mEndDate
->date());
94 mResolver
->setLatestTime(mEndTime
->time());
96 mMoveApptGroupBox
->hide();
99 SchedulingDialog::~SchedulingDialog()
103 void SchedulingDialog::slotUpdateIncidenceStartEnd(const KDateTime
&startDateTime
,
104 const KDateTime
&endDateTime
)
106 #ifdef KDEPIM_MOBILE_UI
107 Q_UNUSED(startDateTime
);
108 Q_UNUSED(endDateTime
);
110 mVisualWidget
->slotUpdateIncidenceStartEnd(startDateTime
, endDateTime
);
114 void SchedulingDialog::fillCombos()
116 // Note: we depend on the following order
117 #ifdef KDEPIM_MOBILE_UI
118 mRolesCombo
->addItem(DesktopIcon("meeting-participant", 48),
119 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::ReqParticipant
));
120 mRolesCombo
->addItem(DesktopIcon("meeting-participant-optional", 48),
121 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::OptParticipant
));
122 mRolesCombo
->addItem(DesktopIcon("meeting-observer", 48),
123 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::NonParticipant
));
124 mRolesCombo
->addItem(DesktopIcon("meeting-chair", 48),
125 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::Chair
));
128 mRolesCombo
->addItem(SmallIcon("meeting-participant"),
129 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::ReqParticipant
));
130 mRolesCombo
->addItem(SmallIcon("meeting-participant-optional"),
131 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::OptParticipant
));
132 mRolesCombo
->addItem(SmallIcon("meeting-observer"),
133 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::NonParticipant
));
134 mRolesCombo
->addItem(SmallIcon("meeting-chair"),
135 KCalUtils::Stringify::attendeeRole(KCalCore::Attendee::Chair
));
138 mRolesCombo
->setWhatsThis(i18nc("@info:whatsthis",
139 "Edits the role of the attendee."));
141 mRolesCombo
->setItemCheckState(0, Qt::Checked
);
142 mRolesCombo
->setItemCheckState(1, Qt::Checked
);
143 mRolesCombo
->setItemCheckState(2, Qt::Checked
);
144 mRolesCombo
->setItemCheckState(3, Qt::Checked
);
147 days
.setBit(0); //Monday
148 days
.setBit(1); //Tuesday
149 days
.setBit(2); //Wednesday
150 days
.setBit(3); //Thursday
151 days
.setBit(4); //Friday.. surprise!
153 mWeekdayCombo
->setDays(days
);
154 mResolver
->setAllowedWeekdays(days
);
157 void SchedulingDialog::slotStartDateChanged(const QDate
&newDate
)
159 QDate oldDate
= mStDate
;
161 if (newDate
.isValid() && oldDate
.isValid()) {
162 updateWeekDays(oldDate
);
166 void SchedulingDialog::updateWeekDays(const QDate
&oldDate
)
168 const int oldStartDayIndex
= mWeekdayCombo
->weekdayIndex(oldDate
);
169 const int newStartDayIndex
= mWeekdayCombo
->weekdayIndex(mStDate
);
171 mWeekdayCombo
->setItemCheckState(oldStartDayIndex
, Qt::Unchecked
);
172 mWeekdayCombo
->setItemEnabled(oldStartDayIndex
, true);
173 mWeekdayCombo
->setItemCheckState(newStartDayIndex
, Qt::Checked
);
174 mWeekdayCombo
->setItemEnabled(newStartDayIndex
, false);
177 void SchedulingDialog::slotWeekdaysChanged()
179 // notify the resolver
180 mResolver
->setAllowedWeekdays(mWeekdayCombo
->days());
183 void SchedulingDialog::slotMandatoryRolesChanged()
185 QSet
<KCalCore::Attendee::Role
> roles
;
186 for (int i
= 0; i
< mRolesCombo
->count(); ++i
) {
187 if (mRolesCombo
->itemCheckState(i
) == Qt::Checked
) {
188 roles
<< KCalCore::Attendee::Role(i
);
191 mResolver
->setMandatoryRoles(roles
);
194 void SchedulingDialog::slotRowSelectionChanged(const QModelIndex
¤t
,
195 const QModelIndex
&previous
)
198 if (!current
.isValid()) {
199 mMoveApptGroupBox
->hide();
202 KCalCore::Period period
= current
.data(KPIM::FreePeriodModel::PeriodRole
).value
<KCalCore::Period
>();
203 const QDate startDate
= period
.start().date();
205 const KCalendarSystem
*calSys
= KLocale::global()->calendar();
206 const int dayOfWeek
= calSys
->dayOfWeek(startDate
);
207 const QString dayLabel
=
208 ki18nc("@label Day of week followed by day of the month, then the month. "
209 "Example: Monday, 12 June",
211 subs(calSys
->weekDayName(dayOfWeek
, KCalendarSystem::LongDayName
)).
212 subs(startDate
.day()).
213 subs(calSys
->monthName(startDate
)).toString();
215 mMoveDayLabel
->setText(dayLabel
);
216 mMoveBeginTimeEdit
->setTimeRange(period
.start().time(),
217 period
.end().addSecs(-mDuration
).time());
218 mMoveBeginTimeEdit
->setTime(period
.start().time());
219 slotSetEndTimeLabel(period
.start().time());
220 mMoveApptGroupBox
->show();
222 mSelectedDate
= startDate
;
225 void SchedulingDialog::slotSetEndTimeLabel(const QTime
&startTime
)
227 const QTime endTime
= startTime
.addSecs(mDuration
);
228 const QString endTimeLabel
=
229 ki18nc("@label This is a suffix following a time selecting widget. "
230 "Example: [timeedit] to 10:00am",
231 "to %1").subs(KLocale::global()->formatTime(endTime
)).toString();
233 mMoveEndTimeLabel
->setText(endTimeLabel
);
234 mSelectedTime
= startTime
;
237 QDate
SchedulingDialog::selectedStartDate() const
239 return mSelectedDate
;
242 QTime
SchedulingDialog::selectedStartTime() const
244 return mSelectedTime
;