| 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 "daypage.h" |
| 42 | |
| 43 | #include "calendardemo.h" |
| 44 | #include <QtWidgets> |
| 45 | #include <QtOrganizer/qorganizer.h> |
| 46 | |
| 47 | QTORGANIZER_USE_NAMESPACE |
| 48 | |
| 49 | Q_DECLARE_METATYPE(QOrganizerItem) |
| 50 | |
| 51 | DayPage::DayPage(QWidget *parent) |
| 52 | :QWidget(parent), |
| 53 | m_manager(0), |
| 54 | m_dateLabel(0), |
| 55 | m_itemList(0), |
| 56 | m_menuBar(0) |
| 57 | { |
| 58 | QVBoxLayout *mainLayout = new QVBoxLayout(); |
| 59 | |
| 60 | QHBoxLayout *dateLayout = new QHBoxLayout(); |
| 61 | m_dateLabel = new QLabel(this); |
| 62 | m_dateLabel->setAlignment(Qt::AlignCenter); |
| 63 | dateLayout->addWidget(m_dateLabel); |
| 64 | dateLayout->addStretch(); |
| 65 | |
| 66 | QPushButton* backButton = new QPushButton("View Month" ,this); |
| 67 | connect(sender: backButton,SIGNAL(clicked()),receiver: this,SLOT(viewMonthClicked())); |
| 68 | dateLayout->addWidget(backButton); |
| 69 | mainLayout->addLayout(layout: dateLayout); |
| 70 | |
| 71 | m_itemList = new QListWidget(this); |
| 72 | mainLayout->addWidget(m_itemList); |
| 73 | connect(sender: m_itemList, SIGNAL(itemDoubleClicked(QListWidgetItem *)), receiver: this, SLOT(itemDoubleClicked(QListWidgetItem *))); |
| 74 | |
| 75 | QHBoxLayout* hbLayout = new QHBoxLayout(); |
| 76 | QPushButton* editEventButton = new QPushButton("Edit" ,this); |
| 77 | connect(sender: editEventButton,SIGNAL(clicked()),receiver: this,SLOT(editItem())); |
| 78 | hbLayout->addWidget(editEventButton); |
| 79 | QPushButton* removeEventButton = new QPushButton("Remove" ,this); |
| 80 | connect(sender: removeEventButton,SIGNAL(clicked()),receiver: this,SLOT(removeItem())); |
| 81 | hbLayout->addWidget(removeEventButton); |
| 82 | mainLayout->addLayout(layout: hbLayout); |
| 83 | setLayout(mainLayout); |
| 84 | } |
| 85 | |
| 86 | DayPage::~DayPage() |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void DayPage::refresh() |
| 91 | { |
| 92 | |
| 93 | m_dateLabel->setText(m_day.toString()); |
| 94 | m_itemList->clear(); |
| 95 | |
| 96 | // Today's item |
| 97 | QList<QOrganizerItem> items = m_manager->items(startDateTime: QDateTime(m_day), endDateTime: QDateTime(m_day, QTime(23, 59, 59))); |
| 98 | |
| 99 | foreach (const QOrganizerItem &item, items) |
| 100 | { |
| 101 | QOrganizerEventTime eventTime = item.detail(detailType: QOrganizerItemDetail::TypeEventTime); |
| 102 | if (!eventTime.isEmpty()) { |
| 103 | QString time = eventTime.startDateTime().time().toString(format: "hh:mm" ); |
| 104 | QListWidgetItem* listItem = new QListWidgetItem(); |
| 105 | if (item.type() == QOrganizerItemType::TypeEventOccurrence) |
| 106 | listItem->setText(QString("Event occurrence:%1-%2" ).arg(a: time).arg(a: item.displayLabel())); |
| 107 | else |
| 108 | listItem->setText(QString("Event:%1-%2" ).arg(a: time).arg(a: item.displayLabel())); |
| 109 | QVariant data = QVariant::fromValue<QOrganizerItem>(value: item); |
| 110 | listItem->setData(ORGANIZER_ITEM_ROLE, value: data); |
| 111 | m_itemList->addItem(aitem: listItem); |
| 112 | } |
| 113 | |
| 114 | QOrganizerTodoTime todoTime = item.detail(detailType: QOrganizerItemDetail::TypeTodoTime); |
| 115 | if (!todoTime.isEmpty()) { |
| 116 | QString time = todoTime.startDateTime().time().toString(format: "hh:mm" ); |
| 117 | QListWidgetItem* listItem = new QListWidgetItem(); |
| 118 | listItem->setText(QString("Todo:%1-%2" ).arg(a: time).arg(a: item.displayLabel())); |
| 119 | QVariant data = QVariant::fromValue<QOrganizerItem>(value: item); |
| 120 | listItem->setData(ORGANIZER_ITEM_ROLE, value: data); |
| 121 | m_itemList->addItem(aitem: listItem); |
| 122 | } |
| 123 | |
| 124 | QOrganizerJournalTime journalTime = item.detail(detailType: QOrganizerItemDetail::TypeJournalTime); |
| 125 | if (!journalTime.isEmpty()) { |
| 126 | QString time = journalTime.entryDateTime().time().toString(format: "hh:mm" ); |
| 127 | QListWidgetItem* listItem = new QListWidgetItem(); |
| 128 | listItem->setText(QString("Journal:%1-%2" ).arg(a: time).arg(a: item.displayLabel())); |
| 129 | QVariant data = QVariant::fromValue<QOrganizerItem>(value: item); |
| 130 | listItem->setData(ORGANIZER_ITEM_ROLE, value: data); |
| 131 | m_itemList->addItem(aitem: listItem); |
| 132 | } |
| 133 | |
| 134 | // TODO: other item types |
| 135 | } |
| 136 | |
| 137 | if (m_itemList->count() == 0) |
| 138 | m_itemList->addItem(label: "(no entries)" ); |
| 139 | } |
| 140 | |
| 141 | void DayPage::changeManager(QOrganizerManager *manager) |
| 142 | { |
| 143 | m_manager = manager; |
| 144 | } |
| 145 | |
| 146 | void DayPage::dayChanged(QDate date) |
| 147 | { |
| 148 | m_day = date; |
| 149 | } |
| 150 | |
| 151 | void DayPage::itemDoubleClicked(QListWidgetItem *listItem) |
| 152 | { |
| 153 | if (!listItem) |
| 154 | return; |
| 155 | |
| 156 | QOrganizerItem organizerItem = listItem->data(ORGANIZER_ITEM_ROLE).value<QOrganizerItem>(); |
| 157 | if (!organizerItem.isEmpty()) |
| 158 | emit showEditPage(item: organizerItem); |
| 159 | } |
| 160 | |
| 161 | void DayPage::viewMonthClicked() |
| 162 | { |
| 163 | emit showMonthPage(); |
| 164 | } |
| 165 | |
| 166 | void DayPage::editItem() |
| 167 | { |
| 168 | QListWidgetItem *listItem = m_itemList->currentItem(); |
| 169 | if (!listItem) |
| 170 | return; |
| 171 | |
| 172 | QOrganizerItem organizerItem = listItem->data(ORGANIZER_ITEM_ROLE).value<QOrganizerItem>(); |
| 173 | if (!organizerItem.isEmpty()) |
| 174 | emit showEditPage(item: organizerItem); |
| 175 | } |
| 176 | |
| 177 | void DayPage::removeItem() |
| 178 | { |
| 179 | QListWidgetItem *listItem = m_itemList->currentItem(); |
| 180 | if (!listItem) |
| 181 | return; |
| 182 | |
| 183 | QOrganizerItem organizerItem = listItem->data(ORGANIZER_ITEM_ROLE).value<QOrganizerItem>(); |
| 184 | if (organizerItem.isEmpty()) |
| 185 | return; |
| 186 | |
| 187 | if (organizerItem.type() == QOrganizerItemType::TypeEventOccurrence |
| 188 | || organizerItem.type() == QOrganizerItemType::TypeTodoOccurrence) { |
| 189 | // Here we could ask if the user wishes to remove only the occurrence (meaning we would |
| 190 | // add an exception date to the parent item), or the parent item. The current |
| 191 | // implementation is to remove the parent (including all the occurrences). |
| 192 | m_manager->removeItem(itemId: organizerItem.detail(detailType: QOrganizerItemDetail::TypeParent).value<QOrganizerItemId>(field: QOrganizerItemParent::FieldParentId)); |
| 193 | } else { |
| 194 | m_manager->removeItem(itemId: organizerItem.id()); |
| 195 | } |
| 196 | |
| 197 | if (m_manager->error()) |
| 198 | QMessageBox::information(parent: this, title: "Failed!" , text: QString("Failed to remove item!\n(error code %1)" ).arg(a: m_manager->error())); |
| 199 | else |
| 200 | delete m_itemList->takeItem(row: m_itemList->currentRow()); |
| 201 | |
| 202 | if (m_itemList->count() == 0) |
| 203 | m_itemList->addItem(label: "(no entries)" ); |
| 204 | } |
| 205 | |
| 206 | void DayPage::showEvent(QShowEvent *event) |
| 207 | { |
| 208 | window()->setWindowTitle(m_day.toString(format: "dddd" )); |
| 209 | QWidget::showEvent(event); |
| 210 | } |
| 211 | |
| 212 | |