~chris.gagnon/+junk/qtpim-coverage

« back to all changes in this revision

Viewing changes to examples/organizer/calendardemo/src/daypage.cpp

  • Committer: chris.gagnon
  • Date: 2013-12-10 23:09:37 UTC
  • Revision ID: chris.gagnon@canonical.com-20131210230937-2akf1ft1edcttk87
first post

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
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 Digia Plc and its Subsidiary(-ies) nor the names
 
21
**     of its 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 <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(backButton,SIGNAL(clicked()),this,SLOT(viewMonthClicked()));
 
68
    dateLayout->addWidget(backButton);
 
69
    mainLayout->addLayout(dateLayout);
 
70
 
 
71
    m_itemList = new QListWidget(this);
 
72
    mainLayout->addWidget(m_itemList);
 
73
    connect(m_itemList, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(itemDoubleClicked(QListWidgetItem *)));
 
74
 
 
75
    QHBoxLayout* hbLayout = new QHBoxLayout();
 
76
    QPushButton* editEventButton = new QPushButton("Edit",this);
 
77
    connect(editEventButton,SIGNAL(clicked()),this,SLOT(editItem()));
 
78
    hbLayout->addWidget(editEventButton);
 
79
    QPushButton* removeEventButton = new QPushButton("Remove",this);
 
80
    connect(removeEventButton,SIGNAL(clicked()),this,SLOT(removeItem()));
 
81
    hbLayout->addWidget(removeEventButton);
 
82
    mainLayout->addLayout(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(QDateTime(m_day), QDateTime(m_day, QTime(23, 59, 59)));
 
98
 
 
99
    foreach (const QOrganizerItem &item, items)
 
100
    {
 
101
        QOrganizerEventTime eventTime = item.detail(QOrganizerItemDetail::TypeEventTime);
 
102
        if (!eventTime.isEmpty()) {
 
103
            QString time = eventTime.startDateTime().time().toString("hh:mm");
 
104
            QListWidgetItem* listItem = new QListWidgetItem();
 
105
            if (item.type() == QOrganizerItemType::TypeEventOccurrence)
 
106
                listItem->setText(QString("Event occurrence:%1-%2").arg(time).arg(item.displayLabel()));
 
107
            else
 
108
                listItem->setText(QString("Event:%1-%2").arg(time).arg(item.displayLabel()));
 
109
            QVariant data = QVariant::fromValue<QOrganizerItem>(item);
 
110
            listItem->setData(ORGANIZER_ITEM_ROLE, data);
 
111
            m_itemList->addItem(listItem);
 
112
        }
 
113
        
 
114
        QOrganizerTodoTime todoTime = item.detail(QOrganizerItemDetail::TypeTodoTime);
 
115
        if (!todoTime.isEmpty()) {
 
116
            QString time = todoTime.startDateTime().time().toString("hh:mm");
 
117
            QListWidgetItem* listItem = new QListWidgetItem();
 
118
            listItem->setText(QString("Todo:%1-%2").arg(time).arg(item.displayLabel()));
 
119
            QVariant data = QVariant::fromValue<QOrganizerItem>(item);
 
120
            listItem->setData(ORGANIZER_ITEM_ROLE, data);
 
121
            m_itemList->addItem(listItem);
 
122
        }
 
123
 
 
124
        QOrganizerJournalTime journalTime = item.detail(QOrganizerItemDetail::TypeJournalTime);
 
125
        if (!journalTime.isEmpty()) {
 
126
            QString time = journalTime.entryDateTime().time().toString("hh:mm");
 
127
            QListWidgetItem* listItem = new QListWidgetItem();
 
128
            listItem->setText(QString("Journal:%1-%2").arg(time).arg(item.displayLabel()));
 
129
            QVariant data = QVariant::fromValue<QOrganizerItem>(item);
 
130
            listItem->setData(ORGANIZER_ITEM_ROLE, data);
 
131
            m_itemList->addItem(listItem);
 
132
        }
 
133
        
 
134
        // TODO: other item types
 
135
    }
 
136
 
 
137
    if (m_itemList->count() == 0)
 
138
        m_itemList->addItem("(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(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(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(organizerItem.detail(QOrganizerItemDetail::TypeParent).value<QOrganizerItemId>(QOrganizerItemParent::FieldParentId));
 
193
    } else {
 
194
        m_manager->removeItem(organizerItem.id());
 
195
    }
 
196
 
 
197
    if (m_manager->error())
 
198
        QMessageBox::information(this, "Failed!", QString("Failed to remove item!\n(error code %1)").arg(m_manager->error()));
 
199
    else
 
200
        delete m_itemList->takeItem(m_itemList->currentRow());
 
201
 
 
202
    if (m_itemList->count() == 0)
 
203
        m_itemList->addItem("(no entries)");
 
204
}
 
205
 
 
206
void DayPage::showEvent(QShowEvent *event)
 
207
{
 
208
    window()->setWindowTitle(m_day.toString("dddd"));
 
209
    QWidget::showEvent(event);
 
210
}
 
211