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 | |
41 | #include <QtWidgets> |
42 | |
43 | #include <QtOrganizer/qorganizer.h> |
44 | |
45 | #include "window.h" |
46 | #include "todoeditor.h" |
47 | |
48 | QTORGANIZER_USE_NAMESPACE |
49 | |
50 | //! [0] |
51 | Window::Window() |
52 | { |
53 | setupGui(); |
54 | |
55 | manager = new QOrganizerManager("memory" ); |
56 | |
57 | setWindowTitle(tr(s: "ToDo Example" )); |
58 | refreshList(); |
59 | } |
60 | //! [0] |
61 | |
62 | Window::~Window() |
63 | { |
64 | delete manager; |
65 | } |
66 | |
67 | //! [1] |
68 | void Window::editNewTodo() |
69 | { |
70 | QOrganizerTodo newTodo; |
71 | newTodo.setPriority(QOrganizerItemPriority::HighPriority); |
72 | newTodo.setStatus(QOrganizerTodoProgress::StatusNotStarted); |
73 | QDateTime currentDateTime(calendarWidget->selectedDate(), |
74 | QTime::currentTime()); |
75 | newTodo.setStartDateTime(currentDateTime); |
76 | newTodo.setDueDateTime(currentDateTime.addSecs(secs: 60*60)); |
77 | |
78 | todoEditor->editTodo(todo: newTodo); |
79 | |
80 | stackedWidget->setCurrentWidget(todoEditor); |
81 | } |
82 | //! [1] |
83 | |
84 | //! [2] |
85 | Q_DECLARE_METATYPE(QOrganizerTodo) |
86 | |
87 | void Window::editTodo(QListWidgetItem *item) |
88 | { |
89 | QVariant variant = item->data(role: Qt::UserRole); |
90 | if (!variant.canConvert<QOrganizerTodo>()) |
91 | return; |
92 | |
93 | QOrganizerTodo todo = variant.value<QOrganizerTodo>(); |
94 | todoEditor->editTodo(todo); |
95 | stackedWidget->setCurrentWidget(todoEditor); |
96 | } |
97 | //! [2] |
98 | |
99 | //! [3] |
100 | void Window::saveTodo(QOrganizerTodo &todo) |
101 | { |
102 | manager->saveItem(item: &todo); |
103 | |
104 | stackedWidget->setCurrentIndex(0); |
105 | refreshList(); |
106 | } |
107 | //! [3] |
108 | |
109 | //! [4] |
110 | void Window::deleteTodo() |
111 | { |
112 | QList<QListWidgetItem *> items = listWidget->selectedItems(); |
113 | if (!items.isEmpty()) { |
114 | QVariant variant = items.at(i: 0)->data(role: Qt::UserRole); |
115 | if (variant.canConvert<QOrganizerTodo>()) { |
116 | QOrganizerTodo theTodo = variant.value<QOrganizerTodo>(); |
117 | manager->removeItem(itemId: theTodo.id()); |
118 | refreshList(); |
119 | } |
120 | } |
121 | } |
122 | //! [4] |
123 | |
124 | //! [5] |
125 | void Window::refreshList() |
126 | { |
127 | listWidget->clear(); |
128 | |
129 | QOrganizerItemSortOrder sortOrder; |
130 | sortOrder.setDetail(detailType: QOrganizerItemDetail::TypeTodoTime, field: QOrganizerTodoTime::FieldDueDateTime); |
131 | |
132 | QList<QOrganizerItem> items = |
133 | manager->items(startDateTime: QDateTime(), endDateTime: QDateTime(), filter: QOrganizerItemFilter(), maxCount: -1, sortOrders: QList<QOrganizerItemSortOrder>() << sortOrder); |
134 | //! [5] |
135 | if (items.isEmpty()) { |
136 | new QListWidgetItem("<No Todos>" , listWidget); |
137 | } |
138 | //! [6] |
139 | foreach(QOrganizerItem item, items) { |
140 | if (item.type() == QOrganizerItemType::TypeTodo) { |
141 | QOrganizerTodo todo = static_cast<QOrganizerTodo>(item); |
142 | if (todo.startDateTime() > |
143 | QDateTime(calendarWidget->selectedDate(), QTime(23,59)) || |
144 | todo.dueDateTime() < |
145 | QDateTime(calendarWidget->selectedDate(), QTime(0, 0))) |
146 | continue; |
147 | |
148 | QString display = todo.startDateTime().toString(format: "yy/MM/dd hh:mm" ) + |
149 | "-" + todo.dueDateTime().toString(format: "yy/MM/dd hh:mm" ) + |
150 | " - " + todo.displayLabel(); |
151 | |
152 | QListWidgetItem *listItem = |
153 | new QListWidgetItem(display, listWidget); |
154 | listItem->setData(role: Qt::UserRole, value: QVariant::fromValue(value: todo)); |
155 | } |
156 | } |
157 | } |
158 | //! [6] |
159 | |
160 | void Window::setupGui() |
161 | { |
162 | todoEditor = new TodoEditor; |
163 | |
164 | listWidget = new QListWidget; |
165 | stackedWidget = new QStackedWidget; |
166 | newTodoButton = new QPushButton(tr(s: "New Todo" )); |
167 | deletTodoButton = new QPushButton(tr(s: "Delete Todo" )); |
168 | calendarWidget = new QCalendarWidget; |
169 | |
170 | connect(sender: newTodoButton, SIGNAL(clicked()), receiver: this, SLOT(editNewTodo())); |
171 | connect(sender: todoEditor, SIGNAL(editingFinished(QOrganizerTodo &)), |
172 | receiver: this, SLOT(saveTodo(QOrganizerTodo &))); |
173 | connect(sender: listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), |
174 | receiver: this, SLOT(editTodo(QListWidgetItem*))); |
175 | connect(sender: calendarWidget, SIGNAL(selectionChanged()), |
176 | receiver: this, SLOT(refreshList())); |
177 | connect(sender: deletTodoButton, SIGNAL(clicked()), receiver: this, SLOT(deleteTodo())); |
178 | |
179 | QVBoxLayout *mainLayout = new QVBoxLayout; |
180 | mainLayout->addWidget(calendarWidget); |
181 | mainLayout->addWidget(listWidget); |
182 | |
183 | QHBoxLayout *buttonLayout = new QHBoxLayout; |
184 | buttonLayout->addWidget(newTodoButton); |
185 | buttonLayout->addWidget(deletTodoButton); |
186 | mainLayout->addLayout(layout: buttonLayout); |
187 | |
188 | QWidget *frontPage = new QWidget; |
189 | frontPage->setLayout(mainLayout); |
190 | |
191 | stackedWidget->addWidget(w: frontPage); |
192 | stackedWidget->addWidget(w: todoEditor); |
193 | |
194 | // Adding a scroll area to allow proper rendering of the UI on small screens |
195 | QScrollArea *scrollArea = new QScrollArea; |
196 | scrollArea->setWidgetResizable(true); |
197 | scrollArea->setWidget(stackedWidget); |
198 | QSize suggestedSize; |
199 | suggestedSize = stackedWidget->sizeHint(); |
200 | stackedWidget->setMinimumSize(suggestedSize); |
201 | |
202 | QGridLayout *layout = new QGridLayout; |
203 | layout->addWidget(w: scrollArea); |
204 | |
205 | setLayout(layout); |
206 | } |
207 | |