Incidenceeditor

alarmdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
3 SPDX-FileCopyrightText: 2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "alarmdialog.h"
9#include "editorconfig.h"
10#include "ui_alarmdialog.h"
11
12#include <KLocalizedString>
13#include <QDialogButtonBox>
14#include <QPushButton>
15#include <QVBoxLayout>
16
17using namespace IncidenceEditorNG;
18
19AlarmDialog::AlarmDialog(KCalendarCore::Incidence::IncidenceType incidenceType, QWidget *parent)
20 : QDialog(parent)
21 , mUi(new Ui::AlarmDialog)
22 , mIncidenceType(incidenceType)
23{
24 setWindowTitle(i18nc("@title:window", "Create a new reminder"));
25 auto mainLayout = new QVBoxLayout(this);
27 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
28 okButton->setDefault(true);
30 connect(buttonBox, &QDialogButtonBox::accepted, this, &AlarmDialog::accept);
31 connect(buttonBox, &QDialogButtonBox::rejected, this, &AlarmDialog::reject);
32
33 auto mainWidget = new QWidget(this);
34 mUi->setupUi(mainWidget);
35 mainLayout->addWidget(mainWidget);
36 mainLayout->addWidget(buttonBox);
37
38 const int defaultReminderTime = IncidenceEditorNG::EditorConfig::instance()->reminderTime();
39 mUi->mAlarmOffset->setValue(defaultReminderTime);
40
41 int defaultReminderUnits = IncidenceEditorNG::EditorConfig::instance()->reminderTimeUnits();
42 if (defaultReminderUnits < 0 || defaultReminderUnits > 2) {
43 defaultReminderUnits = 0; // minutes
44 }
45
46 mUi->mOffsetUnit->setCurrentIndex(defaultReminderUnits);
47 mUi->mSoundFile->setMimeTypeFilters({QStringLiteral("audio/x-wav"), QStringLiteral("audio/x-mp3"), QStringLiteral("application/ogg")});
48
49 if (IncidenceEditorNG::EditorConfig::instance()->defaultAudioFileReminders()) {
50 mUi->mSoundFile->setUrl(IncidenceEditorNG::EditorConfig::instance()->audioFilePath());
51 }
52
53 fillCombo();
54}
55
56AlarmDialog::~AlarmDialog()
57{
58 delete mUi;
59}
60
61void AlarmDialog::load(const KCalendarCore::Alarm::Ptr &alarm)
62{
63 if (!alarm) {
64 return;
65 }
66
67 setWindowTitle(i18nc("@title:window", "Edit existing reminder"));
68
69 // Offsets
70 int offset;
71 int beforeafterpos = 0;
72
73 if (alarm->hasEndOffset()) {
74 beforeafterpos = 2;
75 offset = alarm->endOffset().asSeconds();
76 } else {
77 // TODO: Also allow alarms at fixed times, not relative to start/end
78 offset = alarm->startOffset().asSeconds();
79 }
80 // Negative offset means before the start/end…
81 if (offset < 0) {
82 offset = -offset;
83 } else {
84 ++beforeafterpos;
85 }
86 mUi->mBeforeAfter->setCurrentIndex(beforeafterpos);
87
88 offset = offset / 60; // make minutes
89 int useoffset = offset;
90
91 if (offset % (24 * 60) == 0 && offset > 0) { // divides evenly into days?
92 useoffset = offset / (24 * 60);
93 mUi->mOffsetUnit->setCurrentIndex(2);
94 } else if (offset % 60 == 0 && offset > 0) { // divides evenly into hours?
95 useoffset = offset / 60;
96 mUi->mOffsetUnit->setCurrentIndex(1);
97 } else {
98 useoffset = offset;
99 mUi->mOffsetUnit->setCurrentIndex(0);
100 }
101 mUi->mAlarmOffset->setValue(useoffset);
102
103 // Repeating
104 mUi->mRepeats->setChecked(alarm->repeatCount() > 0);
105 if (alarm->repeatCount() > 0) {
106 mUi->mRepeatCount->setValue(alarm->repeatCount());
107 mUi->mRepeatInterval->setValue(alarm->snoozeTime().asSeconds() / 60); // show as minutes
108 }
109 int id = 0;
110
111 switch (alarm->type()) {
113 mUi->mTypeCombo->setCurrentIndex(1);
114 mUi->mSoundFile->setUrl(QUrl::fromLocalFile(alarm->audioFile()));
115 id = 1;
116 break;
121 default:
122 mUi->mTypeCombo->setCurrentIndex(0);
123 mUi->mDisplayText->setPlainText(alarm->text());
124 break;
125 }
126
127 mUi->mTypeStack->setCurrentIndex(id);
128 if (alarm->audioFile().isEmpty() && IncidenceEditorNG::EditorConfig::instance()->defaultAudioFileReminders()) {
129 mUi->mSoundFile->setUrl(IncidenceEditorNG::EditorConfig::instance()->audioFilePath());
130 }
131}
132
133void AlarmDialog::save(const KCalendarCore::Alarm::Ptr &alarm) const
134{
135 // Offsets
136 int offset = mUi->mAlarmOffset->value() * 60; // minutes
137 int offsetunit = mUi->mOffsetUnit->currentIndex();
138 if (offsetunit >= 1) {
139 offset *= 60; // hours
140 }
141 if (offsetunit >= 2) {
142 offset *= 24; // days
143 }
144 if (offsetunit >= 3) {
145 offset *= 7; // weeks
146 }
147
148 const int beforeafterpos = mUi->mBeforeAfter->currentIndex();
149 if (beforeafterpos % 2 == 0) { // before -> negative
150 offset = -offset;
151 }
152
153 // Note: if this triggers, fix the logic at the place causing it. It really makes
154 // no sense to have both disabled.
155 Q_ASSERT(mAllowBeginReminders || mAllowEndReminders);
156
157 // TODO: Add possibility to specify a given time for the reminder
158
159 // We assume that if mAllowBeginReminders is not set, that mAllowBeginReminders
160 // is set.
161 if (!mAllowBeginReminders) { // before or after DTDUE
162 alarm->setEndOffset(KCalendarCore::Duration(offset));
163 } else if (beforeafterpos == 0 || beforeafterpos == 1) { // before or after DTSTART
164 alarm->setStartOffset(KCalendarCore::Duration(offset));
165 } else if (beforeafterpos == 2 || beforeafterpos == 3) { // before or after DTEND/DTDUE
166 alarm->setEndOffset(KCalendarCore::Duration(offset));
167 }
168
169 // Repeating
170 if (mUi->mRepeats->isChecked()) {
171 alarm->setRepeatCount(mUi->mRepeatCount->value());
172 alarm->setSnoozeTime(mUi->mRepeatInterval->value() * 60); // convert back to seconds
173 } else {
174 alarm->setRepeatCount(0);
175 }
176
177 if (mUi->mTypeCombo->currentIndex() == 1) { // Audio
178 alarm->setAudioAlarm(mUi->mSoundFile->url().toLocalFile());
179 } else { // Display
180 alarm->setDisplayAlarm(mUi->mDisplayText->toPlainText());
181 }
182}
183
184void AlarmDialog::fillCombo()
185{
186 QStringList items;
187
188 if (mIncidenceType == KCalendarCore::Incidence::TypeTodo) {
189 mUi->mBeforeAfter->clear();
190
191 if (mAllowBeginReminders) {
192 items << i18n("Before the to-do starts") << i18n("After the to-do starts");
193 }
194
195 if (mAllowEndReminders) {
196 items << i18n("Before the to-do is due") << i18n("After the to-do is due");
197 }
198 } else {
199 if (mAllowBeginReminders) {
200 items << i18n("Before the event starts") << i18n("After the event starts");
201 }
202 if (mAllowEndReminders) {
203 items << i18n("Before the event ends") << i18n("After the event ends");
204 }
205 }
206
207 mUi->mBeforeAfter->clear();
208 mUi->mBeforeAfter->addItems(items);
209}
210
211void AlarmDialog::setAllowBeginReminders(bool allow)
212{
213 mAllowBeginReminders = allow;
214 fillCombo();
215}
216
217void AlarmDialog::setAllowEndReminders(bool allow)
218{
219 mAllowEndReminders = allow;
220 fillCombo();
221}
222
223void AlarmDialog::setOffset(int offset)
224{
225 Q_ASSERT(offset > 0);
226 mUi->mAlarmOffset->setValue(offset);
227}
228
229void AlarmDialog::setUnit(Unit unit)
230{
231 mUi->mOffsetUnit->setCurrentIndex(unit);
232}
233
234void AlarmDialog::setWhen(When when)
235{
236 Q_ASSERT(when <= mUi->mBeforeAfter->count());
237 mUi->mBeforeAfter->setCurrentIndex(when);
238}
239
240#include "moc_alarmdialog.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void setShortcut(const QKeySequence &key)
void setDefault(bool)
Key_Return
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.