Mercurial > minori
annotate src/gui/pages/search.cc @ 260:dd211ff68b36
pages/seasons: add initial functionality
the menu doesn't work yet, but it's a good start
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Wed, 03 Apr 2024 19:48:38 -0400 |
parents | 862d0d8619f6 |
children | f31305b9f60a |
rev | line source |
---|---|
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
1 #include "gui/pages/search.h" |
250 | 2 #include "core/anime.h" |
3 #include "core/anime_db.h" | |
258 | 4 #include "core/filesystem.h" |
250 | 5 #include "core/http.h" |
6 #include "core/session.h" | |
258 | 7 #include "core/strings.h" |
250 | 8 #include "gui/dialog/information.h" |
9 #include "gui/translate/anime.h" | |
258 | 10 #include "gui/widgets/text.h" |
250 | 11 #include "services/services.h" |
258 | 12 #include "track/media.h" |
250 | 13 |
258 | 14 #include <QDate> |
250 | 15 #include <QHeaderView> |
258 | 16 #include <QMenu> |
250 | 17 #include <QToolBar> |
18 #include <QTreeView> | |
258 | 19 #include <QVBoxLayout> |
250 | 20 |
258 | 21 #include <algorithm> |
22 #include <fstream> | |
250 | 23 #include <iostream> |
24 #include <sstream> | |
25 | |
258 | 26 #include "anitomy/anitomy.h" |
250 | 27 #include "pugixml.hpp" |
28 | |
29 SearchPageListSortFilter::SearchPageListSortFilter(QObject* parent) : QSortFilterProxyModel(parent) { | |
30 } | |
31 | |
32 bool SearchPageListSortFilter::lessThan(const QModelIndex& l, const QModelIndex& r) const { | |
33 QVariant left = sourceModel()->data(l, sortRole()); | |
34 QVariant right = sourceModel()->data(r, sortRole()); | |
35 | |
36 switch (left.userType()) { | |
37 case QMetaType::Int: | |
38 case QMetaType::UInt: | |
39 case QMetaType::LongLong: | |
258 | 40 case QMetaType::ULongLong: return left.toInt() < right.toInt(); |
41 case QMetaType::QDate: return left.toDate() < right.toDate(); | |
250 | 42 case QMetaType::QString: |
43 default: // meh | |
44 return QString::compare(left.toString(), right.toString(), Qt::CaseInsensitive) < 0; | |
45 } | |
46 } | |
47 | |
48 /* -------------------------------------------- */ | |
49 | |
50 SearchPageListModel::SearchPageListModel(QObject* parent) : QAbstractListModel(parent) { | |
51 } | |
52 | |
53 void SearchPageListModel::ParseSearch(const std::vector<int>& ids) { | |
54 /* hack!!! */ | |
55 if (!rowCount(index(0))) { | |
56 beginInsertRows(QModelIndex(), 0, 0); | |
57 endInsertRows(); | |
58 } | |
59 | |
60 beginResetModel(); | |
61 | |
62 this->ids = ids; | |
63 | |
64 endResetModel(); | |
65 } | |
66 | |
67 int SearchPageListModel::rowCount(const QModelIndex& parent) const { | |
68 return ids.size(); | |
69 (void)(parent); | |
70 } | |
71 | |
72 int SearchPageListModel::columnCount(const QModelIndex& parent) const { | |
73 return NB_COLUMNS; | |
74 (void)(parent); | |
75 } | |
76 | |
77 QVariant SearchPageListModel::headerData(const int section, const Qt::Orientation orientation, const int role) const { | |
78 switch (role) { | |
79 case Qt::DisplayRole: { | |
80 switch (section) { | |
81 case SR_TITLE: return tr("Anime title"); | |
82 case SR_EPISODES: return tr("Episode"); | |
83 case SR_TYPE: return tr("Type"); | |
84 case SR_SCORE: return tr("Score"); | |
85 case SR_SEASON: return tr("Season"); | |
86 default: return {}; | |
87 } | |
88 break; | |
89 } | |
90 case Qt::TextAlignmentRole: { | |
91 switch (section) { | |
92 case SR_TITLE: return QVariant(Qt::AlignLeft | Qt::AlignVCenter); | |
93 case SR_TYPE: return QVariant(Qt::AlignHCenter | Qt::AlignVCenter); | |
94 case SR_EPISODES: | |
95 case SR_SCORE: | |
96 case SR_SEASON: return QVariant(Qt::AlignRight | Qt::AlignVCenter); | |
97 default: return {}; | |
98 } | |
99 break; | |
100 } | |
101 } | |
102 return QAbstractListModel::headerData(section, orientation, role); | |
103 } | |
104 | |
105 QVariant SearchPageListModel::data(const QModelIndex& index, int role) const { | |
106 if (!index.isValid()) | |
107 return QVariant(); | |
108 | |
109 const Anime::Anime& anime = Anime::db.items[ids[index.row()]]; | |
110 | |
111 switch (role) { | |
112 case Qt::DisplayRole: | |
113 switch (index.column()) { | |
114 case SR_TITLE: return Strings::ToQString(anime.GetUserPreferredTitle()); | |
115 case SR_TYPE: return Strings::ToQString(Translate::ToLocalString(anime.GetFormat())); | |
116 case SR_EPISODES: return anime.GetEpisodes(); | |
117 case SR_SCORE: return QString::number(anime.GetAudienceScore()) + "%"; | |
258 | 118 case SR_SEASON: |
119 return Strings::ToQString(Translate::ToLocalString(anime.GetSeason())) + " " + | |
120 QString::number(anime.GetAirDate().GetYear().value_or(2000)); | |
250 | 121 default: return {}; |
122 } | |
123 break; | |
124 case Qt::UserRole: | |
125 switch (index.column()) { | |
126 case SR_SCORE: return anime.GetAudienceScore(); | |
127 case SR_EPISODES: return anime.GetEpisodes(); | |
128 case SR_SEASON: return anime.GetAirDate().GetAsQDate(); | |
129 /* We have to use this to work around some stupid | |
130 * "conversion ambiguous" error on Linux | |
258 | 131 */ |
250 | 132 default: return data(index, Qt::DisplayRole); |
133 } | |
134 break; | |
135 case Qt::SizeHintRole: { | |
136 switch (index.column()) { | |
137 default: { | |
138 /* max horizontal size of 100, height size = size of current font */ | |
139 const QString d = data(index, Qt::DisplayRole).toString(); | |
140 const QFontMetrics metric = QFontMetrics(QFont()); | |
141 | |
142 return QSize(std::max(metric.horizontalAdvance(d), 100), metric.height()); | |
143 } | |
144 } | |
145 break; | |
146 } | |
258 | 147 case Qt::TextAlignmentRole: return headerData(index.column(), Qt::Horizontal, Qt::TextAlignmentRole); |
250 | 148 } |
149 return QVariant(); | |
150 } | |
151 | |
152 Qt::ItemFlags SearchPageListModel::flags(const QModelIndex& index) const { | |
153 if (!index.isValid()) | |
154 return Qt::NoItemFlags; | |
155 | |
156 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; | |
157 } | |
158 | |
159 Anime::Anime* SearchPageListModel::GetAnimeFromIndex(const QModelIndex& index) const { | |
160 return &Anime::db.items[ids[index.row()]]; | |
161 } | |
162 | |
163 void SearchPage::DisplayListMenu() { | |
164 QMenu* menu = new QMenu(this); | |
165 menu->setAttribute(Qt::WA_DeleteOnClose); | |
166 menu->setToolTipsVisible(true); | |
167 | |
168 const QItemSelection selection = sort_model->mapSelectionToSource(treeview->selectionModel()->selection()); | |
169 | |
170 bool add_to_list_enable = true; | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
171 |
250 | 172 std::set<Anime::Anime*> animes; |
173 for (const auto& index : selection.indexes()) { | |
174 if (!index.isValid()) | |
175 continue; | |
176 | |
177 Anime::Anime* anime = model->GetAnimeFromIndex(index); | |
178 if (anime) { | |
179 animes.insert(anime); | |
180 if (anime->IsInUserList()) | |
181 add_to_list_enable = false; | |
182 } | |
183 } | |
184 | |
185 menu->addAction(tr("Information"), [this, animes] { | |
186 for (auto& anime : animes) { | |
258 | 187 InformationDialog* dialog = new InformationDialog( |
188 *anime, | |
189 [this, anime] { | |
190 // UpdateAnime(anime->GetId()); | |
191 }, | |
192 InformationDialog::PAGE_MAIN_INFO, this); | |
250 | 193 |
194 dialog->show(); | |
195 dialog->raise(); | |
196 dialog->activateWindow(); | |
197 } | |
198 }); | |
199 menu->addSeparator(); | |
200 { | |
201 QMenu* submenu = menu->addMenu(tr("Add to list...")); | |
258 | 202 submenu->addAction(tr("Currently watching"), [animes] { |
250 | 203 for (auto& anime : animes) { |
204 if (!anime->IsInUserList()) | |
205 anime->AddToUserList(); | |
206 anime->SetUserStatus(Anime::ListStatus::CURRENT); | |
207 Services::UpdateAnimeEntry(anime->GetId()); | |
208 } | |
209 }); | |
258 | 210 submenu->addAction(tr("Completed"), [animes] { |
250 | 211 for (auto& anime : animes) { |
212 if (!anime->IsInUserList()) | |
213 anime->AddToUserList(); | |
214 anime->SetUserStatus(Anime::ListStatus::COMPLETED); | |
215 Services::UpdateAnimeEntry(anime->GetId()); | |
216 } | |
217 }); | |
258 | 218 submenu->addAction(tr("On hold"), [animes] { |
250 | 219 for (auto& anime : animes) { |
220 if (!anime->IsInUserList()) | |
221 anime->AddToUserList(); | |
222 anime->SetUserStatus(Anime::ListStatus::PAUSED); | |
223 Services::UpdateAnimeEntry(anime->GetId()); | |
224 } | |
225 }); | |
258 | 226 submenu->addAction(tr("Dropped"), [animes] { |
250 | 227 for (auto& anime : animes) { |
228 if (!anime->IsInUserList()) | |
229 anime->AddToUserList(); | |
230 anime->SetUserStatus(Anime::ListStatus::DROPPED); | |
231 Services::UpdateAnimeEntry(anime->GetId()); | |
232 } | |
233 }); | |
258 | 234 submenu->addAction(tr("Plan to watch"), [animes] { |
250 | 235 for (auto& anime : animes) { |
236 if (!anime->IsInUserList()) | |
237 anime->AddToUserList(); | |
238 anime->SetUserStatus(Anime::ListStatus::PLANNING); | |
239 Services::UpdateAnimeEntry(anime->GetId()); | |
240 } | |
241 }); | |
242 submenu->setEnabled(add_to_list_enable); | |
243 } | |
244 menu->popup(QCursor::pos()); | |
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
245 } |
250 | 246 |
247 void SearchPage::ItemDoubleClicked() { | |
248 /* throw out any other garbage */ | |
249 const QItemSelection selection = sort_model->mapSelectionToSource(treeview->selectionModel()->selection()); | |
250 if (!selection.indexes().first().isValid()) | |
251 return; | |
252 | |
253 const QModelIndex index = model->index(selection.indexes().first().row()); | |
254 Anime::Anime* anime = model->GetAnimeFromIndex(index); | |
255 | |
258 | 256 InformationDialog* dialog = new InformationDialog( |
257 *anime, | |
258 [this, anime] { | |
259 // UpdateAnime(anime->GetId()); | |
260 }, | |
261 InformationDialog::PAGE_MAIN_INFO, this); | |
250 | 262 |
263 dialog->show(); | |
264 dialog->raise(); | |
265 dialog->activateWindow(); | |
266 } | |
267 | |
268 SearchPage::SearchPage(QWidget* parent) : QFrame(parent) { | |
269 setFrameShape(QFrame::Box); | |
270 setFrameShadow(QFrame::Sunken); | |
271 | |
272 QVBoxLayout* layout = new QVBoxLayout(this); | |
273 layout->setContentsMargins(0, 0, 0, 0); | |
274 layout->setSpacing(0); | |
275 | |
276 { | |
277 /* Toolbar */ | |
278 QToolBar* toolbar = new QToolBar(this); | |
279 toolbar->setMovable(false); | |
280 | |
281 { | |
282 QLineEdit* line_edit = new QLineEdit("", toolbar); | |
258 | 283 connect(line_edit, &QLineEdit::returnPressed, this, [this, line_edit] { |
250 | 284 /* static thread here. */ |
285 static QThread* thread = nullptr; | |
286 | |
287 if (thread) | |
288 return; | |
289 | |
258 | 290 thread = QThread::create([this, line_edit] { |
250 | 291 model->ParseSearch(Services::Search(Strings::ToUtf8String(line_edit->text()))); |
292 }); | |
293 | |
258 | 294 connect(thread, &QThread::finished, this, [] { |
250 | 295 thread->deleteLater(); |
296 thread = nullptr; | |
297 }); | |
298 | |
299 thread->start(); | |
300 }); | |
301 toolbar->addWidget(line_edit); | |
302 } | |
303 | |
304 layout->addWidget(toolbar); | |
305 } | |
306 | |
307 { | |
308 QFrame* line = new QFrame(this); | |
309 line->setFrameShape(QFrame::HLine); | |
310 line->setFrameShadow(QFrame::Sunken); | |
311 line->setLineWidth(1); | |
312 layout->addWidget(line); | |
313 } | |
314 | |
315 { | |
316 treeview = new QTreeView(this); | |
317 treeview->setUniformRowHeights(true); | |
318 treeview->setAllColumnsShowFocus(false); | |
319 treeview->setAlternatingRowColors(true); | |
320 treeview->setSortingEnabled(true); | |
321 treeview->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
322 treeview->setItemsExpandable(false); | |
323 treeview->setRootIsDecorated(false); | |
324 treeview->setContextMenuPolicy(Qt::CustomContextMenu); | |
325 treeview->setFrameShape(QFrame::NoFrame); | |
326 | |
327 { | |
328 sort_model = new SearchPageListSortFilter(treeview); | |
329 model = new SearchPageListModel(treeview); | |
330 sort_model->setSourceModel(model); | |
331 sort_model->setSortRole(Qt::UserRole); | |
332 sort_model->setSortCaseSensitivity(Qt::CaseInsensitive); | |
333 treeview->setModel(sort_model); | |
334 } | |
335 | |
336 // set column sizes | |
258 | 337 treeview->setColumnWidth(SearchPageListModel::SR_TITLE, 400); |
338 treeview->setColumnWidth(SearchPageListModel::SR_TYPE, 60); | |
250 | 339 treeview->setColumnWidth(SearchPageListModel::SR_EPISODES, 60); |
258 | 340 treeview->setColumnWidth(SearchPageListModel::SR_SCORE, 60); |
341 treeview->setColumnWidth(SearchPageListModel::SR_SEASON, 100); | |
250 | 342 |
343 treeview->header()->setStretchLastSection(false); | |
344 | |
345 /* Double click stuff */ | |
346 connect(treeview, &QAbstractItemView::doubleClicked, this, &SearchPage::ItemDoubleClicked); | |
347 connect(treeview, &QWidget::customContextMenuRequested, this, &SearchPage::DisplayListMenu); | |
348 | |
349 layout->addWidget(treeview); | |
350 } | |
351 } |