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