1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** You may use this file under the terms of the BSD license as follows: |
10 | ** |
11 | ** "Redistribution and use in source and binary forms, with or without |
12 | ** modification, are permitted provided that the following conditions are |
13 | ** met: |
14 | ** * Redistributions of source code must retain the above copyright |
15 | ** notice, this list of conditions and the following disclaimer. |
16 | ** * Redistributions in binary form must reproduce the above copyright |
17 | ** notice, this list of conditions and the following disclaimer in |
18 | ** the documentation and/or other materials provided with the |
19 | ** distribution. |
20 | ** * Neither the name of The Qt Company Ltd nor the names of its |
21 | ** contributors may be used to endorse or promote products derived |
22 | ** from this software without specific prior written permission. |
23 | ** |
24 | ** |
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | #include "todoeditpage.h" |
41 | |
42 | #include <QtWidgets> |
43 | #include <QComboBox> |
44 | #include <QtOrganizer/qorganizer.h> |
45 | |
46 | QTORGANIZER_USE_NAMESPACE |
47 | |
48 | TodoEditPage::TodoEditPage(QWidget *parent) |
49 | :QWidget(parent), |
50 | m_manager(0), |
51 | m_subjectEdit(0), |
52 | m_startTimeEdit(0), |
53 | m_dueTimeEdit(0), |
54 | m_priorityEdit(0), |
55 | m_statusEdit(0), |
56 | m_alarmComboBox(0) |
57 | { |
58 | // Create widgets |
59 | QLabel *subjectLabel = new QLabel("Subject:" , this); |
60 | m_subjectEdit = new QLineEdit(this); |
61 | QLabel *startTimeLabel = new QLabel("Start time:" , this); |
62 | m_startTimeEdit = new QDateTimeEdit(this); |
63 | m_startTimeEdit->setDisplayFormat(QString("yyyy-MM-dd hh:mm:ss AP" )); |
64 | QLabel *dueTimeLabel = new QLabel("Due time:" , this); |
65 | m_dueTimeEdit = new QDateTimeEdit(this); |
66 | m_dueTimeEdit->setDisplayFormat(QString("yyyy-MM-dd hh:mm:ss AP" )); |
67 | QLabel *priorityLabel = new QLabel("Priority:" , this); |
68 | m_priorityEdit = new QComboBox(this); |
69 | QLabel *statusLabel = new QLabel("Status:" , this); |
70 | m_statusEdit = new QComboBox(this); |
71 | QLabel *alarmLabel = new QLabel("Alarm:" , this); |
72 | m_alarmComboBox = new QComboBox(this); |
73 | QLabel *calendarLabel = new QLabel("Calendar:" , this); |
74 | |
75 | QStringList alarmList; |
76 | alarmList << "None" |
77 | << "0 minutes before" |
78 | << "5 minutes before" |
79 | << "15 minutes before" |
80 | << "30 minutes before" |
81 | << "1 hour before" ; |
82 | m_alarmComboBox->addItems(texts: alarmList); |
83 | connect(sender: m_alarmComboBox, SIGNAL(currentIndexChanged(const QString)), receiver: this, |
84 | SLOT(handleAlarmIndexChanged(const QString))); |
85 | |
86 | m_calendarComboBox = new QComboBox(this); |
87 | // the calendar names are not know here, fill the combo box later... |
88 | |
89 | // Add push buttons |
90 | QHBoxLayout* hbLayout = new QHBoxLayout(); |
91 | QPushButton *okButton = new QPushButton("Save" , this); |
92 | connect(sender: okButton,SIGNAL(clicked()),receiver: this,SLOT(saveClicked())); |
93 | hbLayout->addWidget(okButton); |
94 | QPushButton *cancelButton = new QPushButton("Cancel" , this); |
95 | connect(sender: cancelButton,SIGNAL(clicked()),receiver: this,SLOT(cancelClicked())); |
96 | hbLayout->addWidget(cancelButton); |
97 | |
98 | // check to see whether we support alarms. |
99 | QOrganizerManager defaultManager; |
100 | QList<QOrganizerItemDetail::DetailType> supportedDetails = defaultManager.supportedItemDetails(itemType: QOrganizerItemType::TypeTodo); |
101 | |
102 | QVBoxLayout *scrollAreaLayout = new QVBoxLayout(); |
103 | scrollAreaLayout->addWidget(subjectLabel); |
104 | scrollAreaLayout->addWidget(m_subjectEdit); |
105 | scrollAreaLayout->addWidget(startTimeLabel); |
106 | scrollAreaLayout->addWidget(m_startTimeEdit); |
107 | scrollAreaLayout->addWidget(dueTimeLabel); |
108 | scrollAreaLayout->addWidget(m_dueTimeEdit); |
109 | scrollAreaLayout->addWidget(priorityLabel); |
110 | scrollAreaLayout->addWidget(m_priorityEdit); |
111 | scrollAreaLayout->addWidget(statusLabel); |
112 | scrollAreaLayout->addWidget(m_statusEdit); |
113 | if (supportedDetails.contains(t: QOrganizerItemDetail::TypeVisualReminder)) { |
114 | scrollAreaLayout->addWidget(alarmLabel); |
115 | scrollAreaLayout->addWidget(m_alarmComboBox); |
116 | } |
117 | scrollAreaLayout->addWidget(calendarLabel); |
118 | scrollAreaLayout->addWidget(m_calendarComboBox); |
119 | scrollAreaLayout->addStretch(); |
120 | scrollAreaLayout->addLayout(layout: hbLayout); |
121 | |
122 | QScrollArea *scrollArea = new QScrollArea(this); |
123 | scrollArea->setWidgetResizable(true); |
124 | QWidget *formContainer = new QWidget(scrollArea); |
125 | formContainer->setLayout(scrollAreaLayout); |
126 | scrollArea->setWidget(formContainer); |
127 | |
128 | QVBoxLayout *mainLayout = new QVBoxLayout(); |
129 | mainLayout->addWidget(scrollArea); |
130 | setLayout(mainLayout); |
131 | |
132 | // Fill priority combo |
133 | m_priorityEdit->addItem(atext: "Unknown" , auserData: QVariant(QOrganizerItemPriority::UnknownPriority)); |
134 | m_priorityEdit->addItem(atext: "Highest" , auserData: QVariant(QOrganizerItemPriority::HighestPriority)); |
135 | m_priorityEdit->addItem(atext: "Extremely high" , auserData: QVariant(QOrganizerItemPriority::ExtremelyHighPriority)); |
136 | m_priorityEdit->addItem(atext: "Very high" , auserData: QVariant(QOrganizerItemPriority::VeryHighPriority)); |
137 | m_priorityEdit->addItem(atext: "High" , auserData: QVariant(QOrganizerItemPriority::HighPriority)); |
138 | m_priorityEdit->addItem(atext: "Medium" , auserData: QVariant(QOrganizerItemPriority::MediumPriority)); |
139 | m_priorityEdit->addItem(atext: "Low" , auserData: QVariant(QOrganizerItemPriority::LowPriority)); |
140 | m_priorityEdit->addItem(atext: "Very low" , auserData: QVariant(QOrganizerItemPriority::VeryLowPriority)); |
141 | m_priorityEdit->addItem(atext: "Extremely low" , auserData: QVariant(QOrganizerItemPriority::ExtremelyLowPriority)); |
142 | m_priorityEdit->addItem(atext: "Lowest" , auserData: QVariant(QOrganizerItemPriority::LowestPriority)); |
143 | |
144 | // Fill status combo |
145 | m_statusEdit->addItem(atext: "Not started" , auserData: QVariant(QOrganizerTodoProgress::StatusNotStarted)); |
146 | m_statusEdit->addItem(atext: "In progress" , auserData: QVariant(QOrganizerTodoProgress::StatusInProgress)); |
147 | m_statusEdit->addItem(atext: "Complete" , auserData: QVariant(QOrganizerTodoProgress::StatusComplete)); |
148 | } |
149 | |
150 | TodoEditPage::~TodoEditPage() |
151 | { |
152 | |
153 | } |
154 | |
155 | void TodoEditPage::todoChanged(QOrganizerManager *manager, const QOrganizerTodo &todo) |
156 | { |
157 | m_manager = manager; |
158 | m_organizerTodo = todo; |
159 | m_subjectEdit->setText(todo.displayLabel()); |
160 | m_startTimeEdit->setDateTime(todo.startDateTime()); |
161 | m_dueTimeEdit->setDateTime(todo.dueDateTime()); |
162 | int index = m_priorityEdit->findData(data: QVariant(todo.priority())); |
163 | m_priorityEdit->setCurrentIndex(index); |
164 | index = m_priorityEdit->findData(data: QVariant(todo.status())); |
165 | m_statusEdit->setCurrentIndex(index); |
166 | |
167 | // set calendar selection |
168 | m_calendarComboBox->clear(); |
169 | |
170 | // resolve metadata field that contains calendar name (if any) |
171 | m_collections = m_manager->collections(); |
172 | int counter = 0; |
173 | int todoCalendarIndex = -1; |
174 | foreach(QOrganizerCollection collection, m_collections) { |
175 | // We currently have no way of stringifying ids |
176 | // QString visibleName = "Calendar id = " + QString::number(collection.id().localId()); |
177 | QString visibleName = collection.metaData(key: QOrganizerCollection::KeyName).toString(); |
178 | if (visibleName.isEmpty()) |
179 | visibleName = "Calendar " + QString::number(index); |
180 | |
181 | m_calendarComboBox->addItem(atext: visibleName); |
182 | if (collection.id() == todo.collectionId()) |
183 | todoCalendarIndex = counter; |
184 | ++counter; |
185 | } |
186 | |
187 | if (todoCalendarIndex > -1) { |
188 | m_calendarComboBox->setCurrentIndex(todoCalendarIndex); |
189 | m_calendarComboBox->setEnabled(false); // when modifying existing todos, the calendar can't be changed anymore |
190 | } |
191 | else { |
192 | m_calendarComboBox->setEnabled(true); |
193 | } |
194 | } |
195 | |
196 | |
197 | void TodoEditPage::cancelClicked() |
198 | { |
199 | emit showDayPage(); |
200 | } |
201 | |
202 | void TodoEditPage::saveClicked() |
203 | { |
204 | // Read data from page |
205 | QDateTime start(m_startTimeEdit->dateTime()); |
206 | QDateTime due(m_dueTimeEdit->dateTime()); |
207 | if (start > due) { |
208 | QMessageBox::warning(parent: this, title: "Failed!" , text: "Start date is not before due date" ); |
209 | return; |
210 | } |
211 | |
212 | m_organizerTodo.setDisplayLabel(m_subjectEdit->text()); |
213 | m_organizerTodo.setStartDateTime(start); |
214 | m_organizerTodo.setDueDateTime(due); |
215 | int index = m_priorityEdit->currentIndex(); |
216 | m_organizerTodo.setPriority((QOrganizerItemPriority::Priority) m_priorityEdit->itemData(index).toInt()); |
217 | |
218 | index = m_statusEdit->currentIndex(); |
219 | QOrganizerTodoProgress::Status currentStatus = (QOrganizerTodoProgress::Status) m_statusEdit->itemData(index).toInt(); |
220 | QOrganizerTodoProgress oldStatus = m_organizerTodo.detail(detailType: QOrganizerItemDetail::TypeTodoProgress); |
221 | m_organizerTodo.removeDetail(detail: &oldStatus); |
222 | if (currentStatus == QOrganizerTodoProgress::StatusComplete && oldStatus.status() != QOrganizerTodoProgress::StatusComplete) |
223 | m_organizerTodo.setFinishedDateTime(QDateTime::currentDateTime()); |
224 | m_organizerTodo.setStatus(currentStatus); |
225 | |
226 | // Save |
227 | if (m_calendarComboBox->currentIndex() > -1) { |
228 | m_organizerTodo.setCollectionId(m_collections[m_calendarComboBox->currentIndex()].id()); |
229 | } |
230 | |
231 | m_manager->saveItem(item: &m_organizerTodo); |
232 | if (m_manager->error()) |
233 | QMessageBox::warning(parent: this, title: "Failed!" , text: QString("Failed to save todo!\n(error code %1)" ).arg(a: m_manager->error())); |
234 | else |
235 | emit showDayPage(); |
236 | } |
237 | |
238 | void TodoEditPage::showEvent(QShowEvent *event) |
239 | { |
240 | window()->setWindowTitle("Edit todo" ); |
241 | QWidget::showEvent(event); |
242 | } |
243 | |
244 | void TodoEditPage::handleAlarmIndexChanged(const QString time) |
245 | { |
246 | bool noVisualReminders = !m_manager->supportedItemDetails(itemType: QOrganizerItemType::TypeEvent).contains(t: QOrganizerItemDetail::TypeVisualReminder); |
247 | |
248 | QScopedPointer<QOrganizerItemReminder> reminder; |
249 | if (noVisualReminders) { |
250 | reminder.reset(other: new QOrganizerItemReminder()); |
251 | } else { |
252 | reminder.reset(other: new QOrganizerItemVisualReminder()); |
253 | static_cast<QOrganizerItemVisualReminder *>(reminder.data())->setMessage(m_subjectEdit->text()); |
254 | } |
255 | |
256 | if (time == "None" ) { |
257 | QOrganizerItemVisualReminder fetchedReminder = m_organizerTodo.detail(detailType: QOrganizerItemDetail::TypeVisualReminder); |
258 | m_organizerTodo.removeDetail(detail: &fetchedReminder); |
259 | return; |
260 | } else if (time == "0 minutes before" ) { |
261 | reminder->setSecondsBeforeStart(0); |
262 | } else if (time == "5 minutes before" ) { |
263 | reminder->setSecondsBeforeStart(5*60); |
264 | } else if (time == "15 minutes before" ) { |
265 | reminder->setSecondsBeforeStart(15*60); |
266 | } else if (time == "30 minutes before" ) { |
267 | reminder->setSecondsBeforeStart(30*60); |
268 | } else if (time == "1 hour before" ) { |
269 | reminder->setSecondsBeforeStart(60*60); |
270 | } |
271 | |
272 | m_organizerTodo.saveDetail(detail: reminder.data()); |
273 | } |
274 | |
275 | |