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