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