| 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 "journaleditpage.h" |
| 41 | |
| 42 | #include <QtWidgets> |
| 43 | #include <QComboBox> |
| 44 | #include <QtOrganizer/qorganizer.h> |
| 45 | |
| 46 | QTORGANIZER_USE_NAMESPACE |
| 47 | |
| 48 | JournalEditPage::JournalEditPage(QWidget *parent) |
| 49 | :QWidget(parent), |
| 50 | m_manager(0), |
| 51 | m_subjectEdit(0), |
| 52 | m_timeEdit(0), |
| 53 | m_alarmComboBox(0) |
| 54 | { |
| 55 | // Create widgets |
| 56 | QLabel *subjectLabel = new QLabel("Subject:" , this); |
| 57 | m_subjectEdit = new QLineEdit(this); |
| 58 | QLabel *startTimeLabel = new QLabel("Time:" , this); |
| 59 | m_timeEdit = new QDateTimeEdit(this); |
| 60 | m_timeEdit->setDisplayFormat(QString("yyyy-MM-dd hh:mm:ss AP" )); |
| 61 | QLabel *alarmLabel = new QLabel("Alarm:" , this); |
| 62 | m_alarmComboBox = new QComboBox(this); |
| 63 | QLabel *calendarLabel = new QLabel("Calendar:" , this); |
| 64 | |
| 65 | QStringList alarmList; |
| 66 | alarmList << "None" |
| 67 | << "0 minutes before" |
| 68 | << "5 minutes before" |
| 69 | << "15 minutes before" |
| 70 | << "30 minutes before" |
| 71 | << "1 hour before" ; |
| 72 | m_alarmComboBox->addItems(texts: alarmList); |
| 73 | connect(sender: m_alarmComboBox, SIGNAL(currentIndexChanged(const QString)), receiver: this, |
| 74 | SLOT(handleAlarmIndexChanged(const QString))); |
| 75 | |
| 76 | m_calendarComboBox = new QComboBox(this); |
| 77 | // the calendar names are not know here, fill the combo box later... |
| 78 | |
| 79 | // Add push buttons |
| 80 | QHBoxLayout* hbLayout = new QHBoxLayout(); |
| 81 | QPushButton *okButton = new QPushButton("Ok" , this); |
| 82 | connect(sender: okButton,SIGNAL(clicked()),receiver: this,SLOT(saveClicked())); |
| 83 | hbLayout->addWidget(okButton); |
| 84 | QPushButton *cancelButton = new QPushButton("Cancel" , this); |
| 85 | connect(sender: cancelButton,SIGNAL(clicked()),receiver: this,SLOT(cancelClicked())); |
| 86 | hbLayout->addWidget(cancelButton); |
| 87 | |
| 88 | // check to see whether we support alarms. |
| 89 | QOrganizerManager defaultManager; |
| 90 | QList<QOrganizerItemDetail::DetailType> supportedDetails = defaultManager.supportedItemDetails(itemType: QOrganizerItemType::TypeJournal); |
| 91 | |
| 92 | QVBoxLayout *scrollAreaLayout = new QVBoxLayout(); |
| 93 | scrollAreaLayout->addWidget(subjectLabel); |
| 94 | scrollAreaLayout->addWidget(m_subjectEdit); |
| 95 | scrollAreaLayout->addWidget(startTimeLabel); |
| 96 | scrollAreaLayout->addWidget(m_timeEdit); |
| 97 | if (supportedDetails.contains(t: QOrganizerItemDetail::TypeVisualReminder)) { |
| 98 | scrollAreaLayout->addWidget(alarmLabel); |
| 99 | scrollAreaLayout->addWidget(m_alarmComboBox); |
| 100 | } |
| 101 | scrollAreaLayout->addWidget(calendarLabel); |
| 102 | scrollAreaLayout->addWidget(m_calendarComboBox); |
| 103 | scrollAreaLayout->addStretch(); |
| 104 | scrollAreaLayout->addLayout(layout: hbLayout); |
| 105 | |
| 106 | QScrollArea *scrollArea = new QScrollArea(this); |
| 107 | scrollArea->setWidgetResizable(true); |
| 108 | QWidget *formContainer = new QWidget(scrollArea); |
| 109 | formContainer->setLayout(scrollAreaLayout); |
| 110 | scrollArea->setWidget(formContainer); |
| 111 | |
| 112 | QVBoxLayout *mainLayout = new QVBoxLayout(); |
| 113 | mainLayout->addWidget(scrollArea); |
| 114 | setLayout(mainLayout); |
| 115 | } |
| 116 | |
| 117 | JournalEditPage::~JournalEditPage() |
| 118 | { |
| 119 | |
| 120 | } |
| 121 | |
| 122 | void JournalEditPage::journalChanged(QOrganizerManager *manager, const QOrganizerJournal &journal) |
| 123 | { |
| 124 | m_manager = manager; |
| 125 | m_organizerJournal = journal; |
| 126 | m_subjectEdit->setText(journal.displayLabel()); |
| 127 | m_timeEdit->setDateTime(journal.dateTime()); |
| 128 | |
| 129 | // set calendar selection |
| 130 | m_calendarComboBox->clear(); |
| 131 | |
| 132 | // resolve metadata field that contains calendar name (if any) |
| 133 | m_collections = m_manager->collections(); |
| 134 | int index = 0; |
| 135 | int journalCalendarIndex = -1; |
| 136 | foreach(QOrganizerCollection collection, m_collections) { |
| 137 | // We currently have no way of stringifying ids |
| 138 | // QString visibleName = "Calendar id = " + QString::number(collection.id().localId()); |
| 139 | QString visibleName = collection.metaData(key: QOrganizerCollection::KeyName).toString(); |
| 140 | if (visibleName.isEmpty()) |
| 141 | visibleName = "Calendar " + QString::number(index); |
| 142 | |
| 143 | m_calendarComboBox->addItem(atext: visibleName); |
| 144 | if (collection.id() == journal.collectionId()) |
| 145 | journalCalendarIndex = index; |
| 146 | ++index; |
| 147 | } |
| 148 | |
| 149 | if (journalCalendarIndex > -1) { |
| 150 | m_calendarComboBox->setCurrentIndex(journalCalendarIndex); |
| 151 | m_calendarComboBox->setEnabled(false); // when modifying existing events, the calendar can't be changed anymore |
| 152 | } |
| 153 | else { |
| 154 | m_calendarComboBox->setEnabled(true); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | void JournalEditPage::cancelClicked() |
| 160 | { |
| 161 | emit showDayPage(); |
| 162 | } |
| 163 | |
| 164 | void JournalEditPage::saveClicked() |
| 165 | { |
| 166 | // Read data from page |
| 167 | m_organizerJournal.setDisplayLabel(m_subjectEdit->text()); |
| 168 | m_organizerJournal.setDateTime(m_timeEdit->dateTime()); |
| 169 | |
| 170 | // Save |
| 171 | if (m_calendarComboBox->currentIndex() > -1) { |
| 172 | m_organizerJournal.setCollectionId(m_collections[m_calendarComboBox->currentIndex()].id()); |
| 173 | } |
| 174 | m_manager->saveItem(item: &m_organizerJournal); |
| 175 | if (m_manager->error()) |
| 176 | QMessageBox::warning(parent: this, title: "Failed!" , text: QString("Failed to save journal!\n(error code %1)" ).arg(a: m_manager->error())); |
| 177 | else |
| 178 | emit showDayPage(); |
| 179 | } |
| 180 | |
| 181 | void JournalEditPage::showEvent(QShowEvent *event) |
| 182 | { |
| 183 | window()->setWindowTitle("Edit journal" ); |
| 184 | QWidget::showEvent(event); |
| 185 | } |
| 186 | |
| 187 | void JournalEditPage::handleAlarmIndexChanged(const QString time) |
| 188 | { |
| 189 | QOrganizerItemVisualReminder reminder; |
| 190 | reminder.setMessage(m_subjectEdit->text()); |
| 191 | |
| 192 | if (time == "None" ) { |
| 193 | QOrganizerItemVisualReminder fetchedReminder = m_organizerJournal.detail(detailType: QOrganizerItemDetail::TypeVisualReminder); |
| 194 | m_organizerJournal.removeDetail(detail: &fetchedReminder); |
| 195 | return; |
| 196 | } else if (time == "0 minutes before" ) { |
| 197 | reminder.setSecondsBeforeStart(0); |
| 198 | } else if (time == "5 minutes before" ) { |
| 199 | reminder.setSecondsBeforeStart(5*60); |
| 200 | } else if (time == "15 minutes before" ) { |
| 201 | reminder.setSecondsBeforeStart(15*60); |
| 202 | } else if (time == "30 minutes before" ) { |
| 203 | reminder.setSecondsBeforeStart(30*60); |
| 204 | } else if (time == "1 hour before" ) { |
| 205 | reminder.setSecondsBeforeStart(60*60); |
| 206 | } |
| 207 | |
| 208 | m_organizerJournal.saveDetail(detail: &reminder); |
| 209 | } |
| 210 | |
| 211 | |