Updated changelog
[grr] / src / entrieswindow.cpp
1 #include <QMenuBar>
2 #include <QDebug>
3 #include <QPainter>
4 #include "entrieswindow.h"
5 #include "contentwindow.h"
6
7 EntriesWindow::EntriesWindow(QWidget *parent, Feed *f) : QMainWindow(parent) {
8         setAttribute(Qt::WA_Maemo5StackedWindow);
9
10         feed = f;
11
12         menuBar()->addAction(tr("Fetch more"), this, SLOT(sync()));
13         menuBar()->addAction(tr("Mark all as read"), this, SLOT(markRead()));
14
15         QActionGroup *filter_group = new QActionGroup(this);
16         show_all = new QAction(tr("Show All"), filter_group);
17         show_all->setCheckable(true);
18         show_all->setChecked(true);
19         show_updated = new QAction(tr("Show Updated"), filter_group);
20         show_updated->setCheckable(true);
21         menuBar()->addActions(filter_group->actions());
22
23         setWindowTitle(f->title);
24
25         list = new QListView();
26         setCentralWidget(list);
27         list->setItemDelegate(new EntryListDelegate(this));
28
29         connect(list, SIGNAL(activated(const QModelIndex &)),
30                 SLOT(entrySelected(const QModelIndex &)));
31
32         connect(feed, SIGNAL(updateFeedComplete()),
33                 SLOT(entriesUpdated()));
34
35         if(feed->getEntriesSize() == 0 || feed->lastUpdated < feed->reader->lastUpdated)
36                 sync();
37         else
38                 entriesUpdated();
39 }
40
41 EntriesWindow::~EntriesWindow() {
42 }
43
44 void EntriesWindow::sync() {
45         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
46         feed->fetch(feed->lastUpdated > feed->reader->lastUpdated);
47 }
48
49 void EntriesWindow::markRead() {
50         feed->markRead();
51 }
52
53 void EntriesWindow::entriesUpdated() {
54         QList<Entry *>entries = feed->getEntries();
55         QAbstractItemModel *old_model = list->model();
56         EntryListModel *new_model = new EntryListModel(this, entries, show_updated->isChecked());
57         list->setModel(new_model);
58         connect(show_updated, SIGNAL(toggled(bool)), new_model, SLOT(showUpdated(bool)));
59         delete(old_model);
60         setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
61 }
62
63 void EntriesWindow::entrySelected(const QModelIndex &index) {
64         Entry *e = qVariantValue<Entry *>(index.data());
65
66         current_row = index.row();
67
68         content = new ContentWindow(this, e);
69
70         connect(content, SIGNAL(showNextEntry()), SLOT(showNextEntry()));
71         connect(content, SIGNAL(showPrevEntry()), SLOT(showPrevEntry()));
72
73         content->show();
74 }
75
76 void EntriesWindow::showNextEntry() {
77         QAbstractListModel *model = static_cast<QAbstractListModel *>(list->model());
78         if(current_row + 1 < model->rowCount()) {
79                 current_row++;
80                 content->showEntry(qVariantValue<Entry *>(model->index(current_row).data()));
81         }
82 }
83
84 void EntriesWindow::showPrevEntry() {
85         QAbstractListModel *model = static_cast<QAbstractListModel *>(list->model());
86         if(current_row > 0) {
87                 current_row--;
88                 content->showEntry(qVariantValue<Entry *>(model->index(current_row).data()));
89         }
90 }
91
92 int EntryListModel::rowCount(const QModelIndex &) const {
93         int size = 0;
94         
95         if(show_updated) {
96                 for(int i = 0; i < entry_list.size(); i++) {
97                         if((entry_list.at(i)->flags & ENTRY_FLAG_READ) == 0)
98                                 size++;
99                 }
100         }
101         else
102                 size = entry_list.size();
103
104         return size;;
105 }
106
107 QVariant EntryListModel::data(const QModelIndex &index, int role) const {
108         if(!index.isValid())
109                 return QVariant();
110
111         if(index.row() >= rowCount() || index.row() < 0) {
112                 return QVariant();
113         }
114
115         if(role == Qt::DisplayRole) {
116                 if(show_updated) {
117                         int i, j;
118                         for(i = 0, j = 0; i < entry_list.size(); i++) {
119                                 if((entry_list.at(i)->flags & ENTRY_FLAG_READ) == 0) j++;
120                                 if(j > index.row())
121                                         return qVariantFromValue(entry_list.at(i));
122                         }
123                 }
124                 else
125                         return qVariantFromValue(entry_list.at(index.row()));
126         }
127
128         return QVariant();
129 }
130
131 void EntryListModel::showUpdated(bool updated) {
132         beginResetModel();
133         show_updated = updated;
134         endResetModel();
135 }
136
137 void EntryListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
138         const QModelIndex &index) const {
139     
140         QStyledItemDelegate::paint(painter, option, index);
141
142         Entry *e = qVariantValue<Entry *>(index.data());
143
144         QFont font = option.font;
145         QRect rect = option.rect;
146         rect.adjust(20, 8, -20, -8);
147         QPoint topleft = rect.topLeft();
148         QPoint bottomleft = rect.bottomLeft();
149         rect.adjust(36, 0, 0, 0);
150
151         painter->save();
152
153         if(((e->flags & ENTRY_FLAG_READ) == 0) &&
154            !(option.state & QStyle::State_Selected)) {
155                 painter->setPen(option.palette.highlight().color());
156         }
157
158         painter->drawText(rect, Qt::AlignTop | Qt::AlignLeft, e->title);
159
160         painter->setPen(option.palette.mid().color());
161         font.setPointSizeF(font.pointSizeF() * 0.70);
162         painter->setFont(font);
163
164         painter->drawText(rect, Qt::AlignBottom | Qt::AlignLeft, e->author);
165
166         QString date;
167         if(e->published.date() == QDateTime::currentDateTime().date())
168                 date = e->published.time().toString(Qt::DefaultLocaleShortDate);
169         else
170                 date = e->published.date().toString();
171
172         painter->drawText(rect, Qt::AlignBottom | Qt::AlignRight, date);
173
174         if(e->flags & ENTRY_FLAG_STARRED) {
175                 QImage img = QImage(QLatin1String(":/images/star-1"));
176                 painter->drawImage(topleft, img);
177         }
178
179         if(e->flags & ENTRY_FLAG_SHARED) {
180                 QImage img = QImage(QLatin1String(":/images/shared-1"));
181                 bottomleft.ry() -= img.height();
182                 painter->drawImage(bottomleft, img);
183         }
184
185         painter->restore();
186 }
187