Mercurial > minori
annotate src/gui/pages/anime_list.cpp @ 76:3364fadc8a36
*: format source code
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 04 Oct 2023 01:46:33 -0400 |
| parents | 27a19dd6cba1 |
| children | 6f7385bd334c |
| rev | line source |
|---|---|
| 10 | 1 /** |
| 2 * anime_list.cpp: defines the anime list page | |
| 3 * and widgets. | |
| 4 * | |
| 5 * much of this file is based around | |
| 6 * Qt's original QTabWidget implementation, because | |
| 7 * I needed a somewhat native way to create a tabbed | |
| 8 * widget with only one subwidget that worked exactly | |
| 9 * like a native tabbed widget. | |
| 10 **/ | |
| 11 #include "gui/pages/anime_list.h" | |
| 12 #include "core/anime.h" | |
| 13 #include "core/anime_db.h" | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
14 #include "core/array.h" |
| 10 | 15 #include "core/session.h" |
| 76 | 16 #include "core/strings.h" |
| 10 | 17 #include "core/time.h" |
| 18 #include "gui/dialog/information.h" | |
| 19 #include "gui/translate/anime.h" | |
|
52
0c4138de2ea7
anime list: we are finally read-write
Paper <mrpapersonic@gmail.com>
parents:
51
diff
changeset
|
20 #include "services/services.h" |
| 15 | 21 #include <QDebug> |
| 10 | 22 #include <QHBoxLayout> |
| 23 #include <QHeaderView> | |
| 24 #include <QMenu> | |
| 25 #include <QProgressBar> | |
| 26 #include <QShortcut> | |
| 27 #include <QStylePainter> | |
| 28 #include <QStyledItemDelegate> | |
| 29 #include <cmath> | |
| 30 | |
| 64 | 31 AnimeListPageDelegate::AnimeListPageDelegate(QObject* parent) : QStyledItemDelegate(parent) { |
| 10 | 32 } |
| 33 | |
| 64 | 34 QWidget* AnimeListPageDelegate::createEditor(QWidget*, const QStyleOptionViewItem&, const QModelIndex&) const { |
| 10 | 35 // no edit 4 u |
| 36 return nullptr; | |
| 37 } | |
| 38 | |
| 64 | 39 void AnimeListPageDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, |
| 40 const QModelIndex& index) const { | |
| 10 | 41 switch (index.column()) { |
| 15 | 42 #if 0 |
| 64 | 43 case AnimeListPageModel::AL_PROGRESS: { |
| 10 | 44 const int progress = static_cast<int>(index.data(Qt::UserRole).toReal()); |
| 45 const int episodes = | |
| 64 | 46 static_cast<int>(index.siblingAtColumn(AnimeListPageModel::AL_EPISODES).data(Qt::UserRole).toReal()); |
| 10 | 47 |
| 48 int text_width = 59; | |
| 49 QRectF text_rect(option.rect.x() + text_width, option.rect.y(), text_width, option.decorationSize.height()); | |
| 50 painter->save(); | |
| 51 | 51 painter->drawText(text_rect, tr("/"), QTextOption(Qt::AlignCenter | Qt::AlignVCenter)); |
| 52 drawText(const QRectF &rectangle, const QString &text, const QTextOption &option = | |
| 15 | 53 QTextOption()) painter->drawText(QRectF(text_rect.x(), text_rect.y(), text_width / 2 - 2, |
| 54 text_rect.height()), QString::number(progress), QTextOption(Qt::AlignRight | Qt::AlignVCenter)); | |
| 55 painter->drawText( | |
| 56 QRectF(text_rect.x() + text_width / 2 + 2, text_rect.y(), text_width / 2 - 2, text_rect.height()), | |
| 57 QString::number(episodes), QTextOption(Qt::AlignLeft | Qt::AlignVCenter)); | |
| 58 painter->restore(); | |
| 59 QStyledItemDelegate::paint(painter, option, index); | |
| 60 break; | |
| 10 | 61 } |
| 15 | 62 #endif |
| 10 | 63 default: QStyledItemDelegate::paint(painter, option, index); break; |
| 64 } | |
| 65 } | |
| 66 | |
| 64 | 67 AnimeListPageSortFilter::AnimeListPageSortFilter(QObject* parent) : QSortFilterProxyModel(parent) { |
| 10 | 68 } |
| 69 | |
| 64 | 70 bool AnimeListPageSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const { |
| 10 | 71 QVariant left = sourceModel()->data(l, sortRole()); |
| 72 QVariant right = sourceModel()->data(r, sortRole()); | |
| 73 | |
| 74 switch (left.userType()) { | |
| 75 case QMetaType::Int: | |
| 76 case QMetaType::UInt: | |
| 77 case QMetaType::LongLong: | |
| 78 case QMetaType::ULongLong: return left.toInt() < right.toInt(); | |
| 79 case QMetaType::QDate: return left.toDate() < right.toDate(); | |
| 80 case QMetaType::QString: | |
| 81 default: return QString::compare(left.toString(), right.toString(), Qt::CaseInsensitive) < 0; | |
| 82 } | |
| 83 } | |
| 84 | |
| 64 | 85 AnimeListPageModel::AnimeListPageModel(QWidget* parent, Anime::ListStatus _status) : QAbstractListModel(parent) { |
| 10 | 86 status = _status; |
| 87 return; | |
| 88 } | |
| 89 | |
| 64 | 90 int AnimeListPageModel::rowCount(const QModelIndex& parent) const { |
| 10 | 91 return list.size(); |
| 92 (void)(parent); | |
| 93 } | |
| 94 | |
| 64 | 95 int AnimeListPageModel::columnCount(const QModelIndex& parent) const { |
| 10 | 96 return NB_COLUMNS; |
| 97 (void)(parent); | |
| 98 } | |
| 99 | |
| 64 | 100 QVariant AnimeListPageModel::headerData(const int section, const Qt::Orientation orientation, const int role) const { |
| 10 | 101 if (role == Qt::DisplayRole) { |
| 102 switch (section) { | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
103 case AL_TITLE: return tr("Anime title"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
104 case AL_PROGRESS: return tr("Progress"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
105 case AL_EPISODES: return tr("Episodes"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
106 case AL_TYPE: return tr("Type"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
107 case AL_SCORE: return tr("Score"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
108 case AL_SEASON: return tr("Season"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
109 case AL_STARTED: return tr("Date started"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
110 case AL_COMPLETED: return tr("Date completed"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
111 case AL_NOTES: return tr("Notes"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
112 case AL_AVG_SCORE: return tr("Average score"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
113 case AL_UPDATED: return tr("Last updated"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
114 default: return {}; |
| 10 | 115 } |
| 116 } else if (role == Qt::TextAlignmentRole) { | |
| 117 switch (section) { | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
118 case AL_TITLE: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
119 case AL_NOTES: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
120 case AL_PROGRESS: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
121 case AL_EPISODES: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
122 case AL_TYPE: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
123 case AL_SCORE: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
124 case AL_AVG_SCORE: return QVariant(Qt::AlignCenter | Qt::AlignVCenter); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
125 case AL_SEASON: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
126 case AL_STARTED: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
127 case AL_COMPLETED: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
128 case AL_UPDATED: return QVariant(Qt::AlignRight | Qt::AlignVCenter); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
129 default: return QAbstractListModel::headerData(section, orientation, role); |
| 10 | 130 } |
| 131 } | |
| 132 return QAbstractListModel::headerData(section, orientation, role); | |
| 133 } | |
| 134 | |
| 64 | 135 QVariant AnimeListPageModel::data(const QModelIndex& index, int role) const { |
| 10 | 136 if (!index.isValid()) |
| 137 return QVariant(); | |
| 138 switch (role) { | |
| 139 case Qt::DisplayRole: | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
140 switch (index.column()) { |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
141 case AL_TITLE: return QString::fromUtf8(list[index.row()].GetUserPreferredTitle().c_str()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
142 case AL_PROGRESS: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
143 return QString::number(list[index.row()].GetUserProgress()) + "/" + |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
144 QString::number(list[index.row()].GetEpisodes()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
145 case AL_EPISODES: return list[index.row()].GetEpisodes(); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
146 case AL_SCORE: return list[index.row()].GetUserScore(); |
|
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
147 case AL_TYPE: return Strings::ToQString(Translate::ToString(list[index.row()].GetFormat())); |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
148 case AL_SEASON: |
|
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
149 return Strings::ToQString(Translate::ToString(list[index.row()].GetSeason())) + " " + |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
150 QString::number(list[index.row()].GetAirDate().GetYear()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
151 case AL_AVG_SCORE: return QString::number(list[index.row()].GetAudienceScore()) + "%"; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
152 case AL_STARTED: return list[index.row()].GetUserDateStarted().GetAsQDate(); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
153 case AL_COMPLETED: return list[index.row()].GetUserDateCompleted().GetAsQDate(); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
154 case AL_UPDATED: { |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
155 if (list[index.row()].GetUserTimeUpdated() == 0) |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
156 return QString("-"); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
157 Time::Duration duration(Time::GetSystemTime() - list[index.row()].GetUserTimeUpdated()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
158 return QString::fromUtf8(duration.AsRelativeString().c_str()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
159 } |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
160 case AL_NOTES: return QString::fromUtf8(list[index.row()].GetUserNotes().c_str()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
161 default: return ""; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
162 } |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
163 break; |
| 10 | 164 case Qt::UserRole: |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
165 switch (index.column()) { |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
166 case AL_PROGRESS: return list[index.row()].GetUserProgress(); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
167 case AL_TYPE: return static_cast<int>(list[index.row()].GetFormat()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
168 case AL_SEASON: return list[index.row()].GetAirDate().GetAsQDate(); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
169 case AL_AVG_SCORE: return list[index.row()].GetAudienceScore(); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
170 case AL_UPDATED: return QVariant::fromValue(list[index.row()].GetUserTimeUpdated()); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
171 default: return data(index, Qt::DisplayRole); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
172 } |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
173 break; |
| 10 | 174 case Qt::TextAlignmentRole: |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
175 switch (index.column()) { |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
176 case AL_TITLE: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
177 case AL_NOTES: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
178 case AL_PROGRESS: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
179 case AL_EPISODES: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
180 case AL_TYPE: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
181 case AL_SCORE: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
182 case AL_AVG_SCORE: return QVariant(Qt::AlignCenter | Qt::AlignVCenter); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
183 case AL_SEASON: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
184 case AL_STARTED: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
185 case AL_COMPLETED: |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
186 case AL_UPDATED: return QVariant(Qt::AlignRight | Qt::AlignVCenter); |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
187 default: break; |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
188 } |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
189 break; |
| 10 | 190 } |
| 191 return QVariant(); | |
| 192 } | |
| 193 | |
| 64 | 194 void AnimeListPageModel::UpdateAnime(int id) { |
| 10 | 195 /* meh... it might be better to just reinit the entire list */ |
| 196 int i = 0; | |
| 11 | 197 for (const auto& a : Anime::db.items) { |
| 198 if (a.second.IsInUserList() && a.first == id && a.second.GetUserStatus() == status) { | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
199 emit dataChanged(index(i), index(i)); |
| 10 | 200 } |
| 201 i++; | |
| 202 } | |
| 203 } | |
| 204 | |
| 64 | 205 Anime::Anime* AnimeListPageModel::GetAnimeFromIndex(QModelIndex index) { |
| 10 | 206 return &list.at(index.row()); |
| 207 } | |
| 208 | |
| 64 | 209 void AnimeListPageModel::RefreshList() { |
| 10 | 210 bool has_children = !!rowCount(index(0)); |
| 15 | 211 if (has_children) |
| 212 beginResetModel(); | |
| 213 else { | |
| 214 int count = 0; | |
| 215 for (const auto& a : Anime::db.items) | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
216 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) |
|
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
217 count++; |
| 15 | 218 beginInsertRows(index(0), 0, count - 1); |
| 219 } | |
| 220 | |
| 10 | 221 list.clear(); |
| 222 | |
| 11 | 223 for (const auto& a : Anime::db.items) { |
| 224 if (a.second.IsInUserList() && a.second.GetUserStatus() == status) { | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
225 list.push_back(a.second); |
| 10 | 226 } |
| 227 } | |
| 15 | 228 |
| 229 if (has_children) | |
| 230 endResetModel(); | |
| 231 else | |
| 232 endInsertRows(); | |
| 10 | 233 } |
| 234 | |
| 64 | 235 int AnimeListPage::VisibleColumnsCount() const { |
| 10 | 236 int count = 0; |
| 237 | |
| 238 for (int i = 0, end = tree_view->header()->count(); i < end; i++) { | |
| 239 if (!tree_view->isColumnHidden(i)) | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
240 count++; |
| 10 | 241 } |
| 242 | |
| 243 return count; | |
| 244 } | |
| 245 | |
| 64 | 246 void AnimeListPage::SetColumnDefaults() { |
| 247 tree_view->setColumnHidden(AnimeListPageModel::AL_SEASON, false); | |
| 248 tree_view->setColumnHidden(AnimeListPageModel::AL_TYPE, false); | |
| 249 tree_view->setColumnHidden(AnimeListPageModel::AL_UPDATED, false); | |
| 250 tree_view->setColumnHidden(AnimeListPageModel::AL_PROGRESS, false); | |
| 251 tree_view->setColumnHidden(AnimeListPageModel::AL_SCORE, false); | |
| 252 tree_view->setColumnHidden(AnimeListPageModel::AL_TITLE, false); | |
| 253 tree_view->setColumnHidden(AnimeListPageModel::AL_EPISODES, true); | |
| 254 tree_view->setColumnHidden(AnimeListPageModel::AL_AVG_SCORE, true); | |
| 255 tree_view->setColumnHidden(AnimeListPageModel::AL_STARTED, true); | |
| 256 tree_view->setColumnHidden(AnimeListPageModel::AL_COMPLETED, true); | |
| 257 tree_view->setColumnHidden(AnimeListPageModel::AL_UPDATED, true); | |
| 258 tree_view->setColumnHidden(AnimeListPageModel::AL_NOTES, true); | |
| 10 | 259 } |
| 260 | |
| 64 | 261 void AnimeListPage::DisplayColumnHeaderMenu() { |
| 10 | 262 QMenu* menu = new QMenu(this); |
| 263 menu->setAttribute(Qt::WA_DeleteOnClose); | |
| 264 menu->setTitle(tr("Column visibility")); | |
| 265 menu->setToolTipsVisible(true); | |
| 266 | |
| 64 | 267 for (int i = 0; i < AnimeListPageModel::NB_COLUMNS; i++) { |
| 268 if (i == AnimeListPageModel::AL_TITLE) | |
|
47
d8eb763e6661
information.cpp: add widgets to the list tab, and add an
Paper <mrpapersonic@gmail.com>
parents:
46
diff
changeset
|
269 continue; |
| 10 | 270 const auto column_name = |
| 15 | 271 sort_models[tab_bar->currentIndex()]->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(); |
| 10 | 272 QAction* action = menu->addAction(column_name, this, [this, i](const bool checked) { |
| 273 if (!checked && (VisibleColumnsCount() <= 1)) | |
| 274 return; | |
| 275 | |
| 276 tree_view->setColumnHidden(i, !checked); | |
| 277 | |
| 278 if (checked && (tree_view->columnWidth(i) <= 5)) | |
| 279 tree_view->resizeColumnToContents(i); | |
| 280 | |
| 281 // SaveSettings(); | |
| 282 }); | |
| 283 action->setCheckable(true); | |
| 284 action->setChecked(!tree_view->isColumnHidden(i)); | |
| 285 } | |
| 286 | |
| 287 menu->addSeparator(); | |
| 288 QAction* resetAction = menu->addAction(tr("Reset to defaults"), this, [this]() { | |
| 289 for (int i = 0, count = tree_view->header()->count(); i < count; ++i) { | |
| 290 SetColumnDefaults(); | |
| 291 } | |
| 292 // SaveSettings(); | |
| 293 }); | |
| 294 menu->popup(QCursor::pos()); | |
| 295 (void)(resetAction); | |
| 296 } | |
| 297 | |
| 64 | 298 void AnimeListPage::DisplayListMenu() { |
| 10 | 299 QMenu* menu = new QMenu(this); |
| 300 menu->setAttribute(Qt::WA_DeleteOnClose); | |
| 301 menu->setTitle(tr("Column visibility")); | |
| 302 menu->setToolTipsVisible(true); | |
| 303 | |
| 15 | 304 const QItemSelection selection = |
| 305 sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection()); | |
| 10 | 306 if (!selection.indexes().first().isValid()) { |
| 307 return; | |
| 308 } | |
| 309 | |
| 51 | 310 QAction* action = menu->addAction(tr("Information"), [this, selection] { |
| 64 | 311 AnimeListPageModel* source_model = |
| 312 reinterpret_cast<AnimeListPageModel*>(sort_models[tab_bar->currentIndex()]->sourceModel()); | |
| 313 const QModelIndex index = source_model->index(selection.indexes().first().row()); | |
| 314 Anime::Anime* anime = source_model->GetAnimeFromIndex(index); | |
| 10 | 315 if (!anime) { |
| 316 return; | |
| 317 } | |
| 318 | |
| 63 | 319 InformationDialog* dialog = new InformationDialog( |
| 320 *anime, | |
| 321 [this, anime] { | |
| 322 Services::UpdateAnimeEntry(anime->GetId()); | |
| 323 Refresh(); | |
| 324 }, | |
| 325 this); | |
| 10 | 326 |
| 327 dialog->show(); | |
| 328 dialog->raise(); | |
| 329 dialog->activateWindow(); | |
| 330 }); | |
| 331 menu->popup(QCursor::pos()); | |
| 332 } | |
| 333 | |
| 64 | 334 void AnimeListPage::ItemDoubleClicked() { |
| 10 | 335 /* throw out any other garbage */ |
| 336 const QItemSelection selection = | |
| 15 | 337 sort_models[tab_bar->currentIndex()]->mapSelectionToSource(tree_view->selectionModel()->selection()); |
| 10 | 338 if (!selection.indexes().first().isValid()) { |
| 339 return; | |
| 340 } | |
| 341 | |
| 64 | 342 AnimeListPageModel* source_model = |
| 343 reinterpret_cast<AnimeListPageModel*>(sort_models[tab_bar->currentIndex()]->sourceModel()); | |
| 344 | |
| 345 const QModelIndex index = source_model->index(selection.indexes().first().row()); | |
| 346 Anime::Anime* anime = source_model->GetAnimeFromIndex(index); | |
| 10 | 347 |
| 63 | 348 InformationDialog* dialog = new InformationDialog( |
| 349 *anime, | |
| 350 [this, anime] { | |
| 351 Services::UpdateAnimeEntry(anime->GetId()); | |
| 352 Refresh(); | |
| 353 }, | |
| 354 this); | |
| 10 | 355 |
| 356 dialog->show(); | |
| 357 dialog->raise(); | |
| 358 dialog->activateWindow(); | |
| 359 } | |
| 360 | |
| 64 | 361 void AnimeListPage::paintEvent(QPaintEvent*) { |
| 10 | 362 QStylePainter p(this); |
| 363 | |
| 364 QStyleOptionTabWidgetFrame opt; | |
| 365 InitStyle(&opt); | |
| 366 opt.rect = panelRect; | |
| 367 p.drawPrimitive(QStyle::PE_FrameTabWidget, opt); | |
| 368 } | |
| 369 | |
| 64 | 370 void AnimeListPage::resizeEvent(QResizeEvent* e) { |
| 10 | 371 QWidget::resizeEvent(e); |
| 372 SetupLayout(); | |
| 373 } | |
| 374 | |
| 64 | 375 void AnimeListPage::showEvent(QShowEvent*) { |
| 10 | 376 SetupLayout(); |
| 377 } | |
| 378 | |
| 64 | 379 void AnimeListPage::InitBasicStyle(QStyleOptionTabWidgetFrame* option) const { |
| 10 | 380 if (!option) |
| 381 return; | |
| 382 | |
| 383 option->initFrom(this); | |
| 384 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this); | |
| 385 option->shape = QTabBar::RoundedNorth; | |
| 386 option->tabBarRect = tab_bar->geometry(); | |
| 387 } | |
| 388 | |
| 64 | 389 void AnimeListPage::InitStyle(QStyleOptionTabWidgetFrame* option) const { |
| 10 | 390 if (!option) |
| 391 return; | |
| 392 | |
| 393 InitBasicStyle(option); | |
| 394 | |
| 395 // int exth = style()->pixelMetric(QStyle::PM_TabBarBaseHeight, nullptr, this); | |
| 396 QSize t(0, tree_view->frameWidth()); | |
| 397 if (tab_bar->isVisibleTo(this)) { | |
| 398 t = tab_bar->sizeHint(); | |
| 399 t.setWidth(width()); | |
| 400 } | |
| 46 | 401 |
| 10 | 402 option->tabBarSize = t; |
| 403 | |
| 404 QRect selected_tab_rect = tab_bar->tabRect(tab_bar->currentIndex()); | |
| 405 selected_tab_rect.moveTopLeft(selected_tab_rect.topLeft() + option->tabBarRect.topLeft()); | |
| 406 option->selectedTabRect = selected_tab_rect; | |
| 407 | |
| 408 option->lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this); | |
| 409 } | |
| 410 | |
| 64 | 411 void AnimeListPage::SetupLayout() { |
| 10 | 412 QStyleOptionTabWidgetFrame option; |
| 413 InitStyle(&option); | |
| 414 | |
| 415 QRect tabRect = style()->subElementRect(QStyle::SE_TabWidgetTabBar, &option, this); | |
| 416 tabRect.setLeft(tabRect.left() + 1); | |
| 417 panelRect = style()->subElementRect(QStyle::SE_TabWidgetTabPane, &option, this); | |
| 418 QRect contentsRect = style()->subElementRect(QStyle::SE_TabWidgetTabContents, &option, this); | |
| 419 | |
| 420 tab_bar->setGeometry(tabRect); | |
| 421 tree_view->parentWidget()->setGeometry(contentsRect); | |
| 422 } | |
| 423 | |
| 64 | 424 AnimeListPage::AnimeListPage(QWidget* parent) : QWidget(parent) { |
| 10 | 425 /* Tab bar */ |
| 426 tab_bar = new QTabBar(this); | |
| 427 tab_bar->setExpanding(false); | |
| 428 tab_bar->setDrawBase(false); | |
| 429 | |
| 430 /* Tree view... */ | |
| 431 QWidget* tree_widget = new QWidget(this); | |
| 432 tree_view = new QTreeView(tree_widget); | |
| 64 | 433 tree_view->setItemDelegate(new AnimeListPageDelegate(tree_view)); |
| 10 | 434 tree_view->setUniformRowHeights(true); |
| 435 tree_view->setAllColumnsShowFocus(false); | |
| 436 tree_view->setAlternatingRowColors(true); | |
| 437 tree_view->setSortingEnabled(true); | |
| 438 tree_view->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
| 439 tree_view->setItemsExpandable(false); | |
| 440 tree_view->setRootIsDecorated(false); | |
| 441 tree_view->setContextMenuPolicy(Qt::CustomContextMenu); | |
| 442 tree_view->setFrameShape(QFrame::NoFrame); | |
| 443 | |
| 444 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++) { | |
|
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
445 tab_bar->addTab(Strings::ToQString(Translate::ToString(Anime::ListStatuses[i])) + " (" + |
| 15 | 446 QString::number(Anime::db.GetListsAnimeAmount(Anime::ListStatuses[i])) + ")"); |
| 64 | 447 sort_models[i] = new AnimeListPageSortFilter(tree_view); |
| 448 sort_models[i]->setSourceModel(new AnimeListPageModel(this, Anime::ListStatuses[i])); | |
| 10 | 449 sort_models[i]->setSortRole(Qt::UserRole); |
| 450 sort_models[i]->setSortCaseSensitivity(Qt::CaseInsensitive); | |
| 451 } | |
| 15 | 452 tree_view->setModel(sort_models[0]); |
| 10 | 453 |
|
68
2417121d894e
*: normalize usage of layouts
Paper <mrpapersonic@gmail.com>
parents:
65
diff
changeset
|
454 QHBoxLayout* layout = new QHBoxLayout(tree_widget); |
| 10 | 455 layout->addWidget(tree_view); |
| 62 | 456 layout->setContentsMargins(0, 0, 0, 0); |
| 10 | 457 |
| 458 /* Double click stuff */ | |
| 64 | 459 connect(tree_view, &QAbstractItemView::doubleClicked, this, &AnimeListPage::ItemDoubleClicked); |
| 460 connect(tree_view, &QWidget::customContextMenuRequested, this, &AnimeListPage::DisplayListMenu); | |
| 10 | 461 |
| 462 /* Enter & return keys */ | |
| 15 | 463 connect(new QShortcut(Qt::Key_Return, tree_view, nullptr, nullptr, Qt::WidgetShortcut), &QShortcut::activated, this, |
| 64 | 464 &AnimeListPage::ItemDoubleClicked); |
| 10 | 465 |
| 15 | 466 connect(new QShortcut(Qt::Key_Enter, tree_view, nullptr, nullptr, Qt::WidgetShortcut), &QShortcut::activated, this, |
| 64 | 467 &AnimeListPage::ItemDoubleClicked); |
| 10 | 468 |
| 469 tree_view->header()->setStretchLastSection(false); | |
| 470 tree_view->header()->setContextMenuPolicy(Qt::CustomContextMenu); | |
| 64 | 471 connect(tree_view->header(), &QWidget::customContextMenuRequested, this, &AnimeListPage::DisplayColumnHeaderMenu); |
| 10 | 472 |
| 473 connect(tab_bar, &QTabBar::currentChanged, this, [this](int index) { | |
| 474 if (sort_models[index]) | |
| 475 tree_view->setModel(sort_models[index]); | |
| 476 }); | |
| 477 | |
| 69 | 478 SetColumnDefaults(); |
| 10 | 479 setFocusPolicy(Qt::TabFocus); |
| 480 setFocusProxy(tab_bar); | |
| 481 } | |
| 482 | |
| 64 | 483 void AnimeListPage::RefreshList() { |
| 15 | 484 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++) |
| 64 | 485 reinterpret_cast<AnimeListPageModel*>(sort_models[i]->sourceModel())->RefreshList(); |
| 10 | 486 } |
| 487 | |
| 64 | 488 void AnimeListPage::RefreshTabs() { |
| 15 | 489 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++) |
|
65
26721c28bf22
*: avoid usage of (to|from)StdString
Paper <mrpapersonic@gmail.com>
parents:
64
diff
changeset
|
490 tab_bar->setTabText(i, Strings::ToQString(Translate::ToString(Anime::ListStatuses[i])) + " (" + |
| 15 | 491 QString::number(Anime::db.GetListsAnimeAmount(Anime::ListStatuses[i])) + ")"); |
| 492 } | |
| 493 | |
| 64 | 494 void AnimeListPage::Refresh() { |
| 15 | 495 RefreshList(); |
| 496 RefreshTabs(); | |
| 497 } | |
| 498 | |
| 499 /* This function, really, really should not be called. | |
| 500 Ever. Why would you ever need to clear the anime list? | |
| 501 Also, this sucks. */ | |
| 64 | 502 void AnimeListPage::Reset() { |
| 10 | 503 while (tab_bar->count()) |
| 504 tab_bar->removeTab(0); | |
| 505 for (unsigned int i = 0; i < ARRAYSIZE(sort_models); i++) | |
| 506 delete sort_models[i]; | |
| 507 } | |
| 508 | |
| 509 #include "gui/pages/moc_anime_list.cpp" |
