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 "editcalendarspage.h" |
41 | |
42 | #include "calendardemo.h" |
43 | #include <QtWidgets> |
44 | #include <QtOrganizer/qorganizer.h> |
45 | |
46 | QTORGANIZER_USE_NAMESPACE |
47 | |
48 | Q_DECLARE_METATYPE(QOrganizerCollection) |
49 | |
50 | EditCalendarsPage::EditCalendarsPage(QWidget *parent) |
51 | :QWidget(parent), |
52 | m_manager(0) |
53 | { |
54 | m_calendarList = new QListWidget(this); |
55 | connect(sender: m_calendarList, SIGNAL(itemDoubleClicked(QListWidgetItem *)), receiver: this, SLOT(itemDoubleClicked(QListWidgetItem *))); |
56 | |
57 | // Add push buttons |
58 | QHBoxLayout* hbLayout = new QHBoxLayout(); |
59 | QPushButton *addButton = new QPushButton("Add new" , this); |
60 | connect(sender: addButton,SIGNAL(clicked()), receiver: this, SIGNAL(addClicked())); |
61 | hbLayout->addWidget(addButton); |
62 | QPushButton *editButton = new QPushButton("Edit" , this); |
63 | connect(sender: editButton,SIGNAL(clicked()),receiver: this,SLOT(editClicked())); |
64 | hbLayout->addWidget(editButton); |
65 | QPushButton *deleteButton = new QPushButton("Delete" , this); |
66 | connect(sender: deleteButton,SIGNAL(clicked()),receiver: this,SLOT(deleteClicked())); |
67 | hbLayout->addWidget(deleteButton); |
68 | QPushButton *backButton = new QPushButton("Back" , this); |
69 | connect(sender: backButton,SIGNAL(clicked()),receiver: this,SLOT(backClicked())); |
70 | hbLayout->addWidget(backButton); |
71 | |
72 | QVBoxLayout *scrollAreaLayout = new QVBoxLayout(); |
73 | scrollAreaLayout->addWidget(m_calendarList); |
74 | scrollAreaLayout->addLayout(layout: hbLayout); |
75 | |
76 | QScrollArea *scrollArea = new QScrollArea(this); |
77 | scrollArea->setWidgetResizable(true); |
78 | QWidget *formContainer = new QWidget(scrollArea); |
79 | formContainer->setLayout(scrollAreaLayout); |
80 | scrollArea->setWidget(formContainer); |
81 | |
82 | QVBoxLayout *mainLayout = new QVBoxLayout(); |
83 | mainLayout->addWidget(scrollArea); |
84 | setLayout(mainLayout); |
85 | } |
86 | |
87 | EditCalendarsPage::~EditCalendarsPage() |
88 | { |
89 | |
90 | } |
91 | |
92 | void EditCalendarsPage::editClicked() |
93 | { |
94 | if (m_calendarList->currentItem()) |
95 | itemDoubleClicked(listItem: m_calendarList->currentItem()); |
96 | } |
97 | |
98 | void EditCalendarsPage::deleteClicked() |
99 | { |
100 | if (!m_calendarList->currentItem()) |
101 | return; |
102 | |
103 | QMessageBox msgBox; |
104 | msgBox.setText("Are you sure you want to delete this calendar?" ); |
105 | msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); |
106 | msgBox.setDefaultButton(QMessageBox::No); |
107 | int ret = msgBox.exec(); |
108 | |
109 | if (ret == QMessageBox::Yes) { |
110 | m_collection = m_calendarList->currentItem()->data(ORGANIZER_CALENDAR_ROLE).value<QOrganizerCollection>(); |
111 | if(!m_manager->removeCollection(collectionId: m_collection.id())) |
112 | QMessageBox::information(parent: this, title: "Failed!" , text: QString("Failed to remove calendar!\n(error code %1)" ).arg(a: m_manager->error())); |
113 | showPage(manager: m_manager); |
114 | } |
115 | } |
116 | |
117 | void EditCalendarsPage::backClicked() |
118 | { |
119 | emit showPreviousPage(); |
120 | } |
121 | |
122 | void EditCalendarsPage::itemDoubleClicked(QListWidgetItem *listItem) |
123 | { |
124 | if (!listItem) |
125 | return; |
126 | |
127 | m_collection = listItem->data(ORGANIZER_CALENDAR_ROLE).value<QOrganizerCollection>(); |
128 | emit showAddCalendarPage(m_manager, &m_collection); |
129 | } |
130 | |
131 | void EditCalendarsPage::showPage(QOrganizerManager *manager) |
132 | { |
133 | m_manager = manager; |
134 | m_calendarList->clear(); |
135 | QList<QOrganizerCollection> collections = manager->collections(); |
136 | int index = 0; |
137 | foreach(QOrganizerCollection collection, collections) { |
138 | QString visibleName; |
139 | if (collection.metaData(key: QOrganizerCollection::KeyName).canConvert(targetTypeId: QVariant::String)) |
140 | visibleName = collection.metaData(key: QOrganizerCollection::KeyName).toString(); |
141 | else |
142 | // We currently have no way of stringifying ids |
143 | // visibleName = "Calendar id " + QString::number(collection.id().localId()); |
144 | visibleName = "Calendar " + QString::number(index++); |
145 | |
146 | QListWidgetItem* listItem = new QListWidgetItem(); |
147 | listItem->setText(visibleName); |
148 | QVariant data = QVariant::fromValue<QOrganizerCollection>(value: collection); |
149 | listItem->setData(ORGANIZER_CALENDAR_ROLE, value: data); |
150 | m_calendarList->addItem(aitem: listItem); |
151 | } |
152 | } |
153 | |
154 | void EditCalendarsPage::showEvent(QShowEvent *event) |
155 | { |
156 | window()->setWindowTitle("Edit calendars" ); |
157 | QWidget::showEvent(event); |
158 | } |
159 | |