Mercurial > minori
annotate src/gui/pages/search.cc @ 301:b1f625b0227c
*: convert all files CRLF -> LF
some files were in DOS format, others were in unix. now everything
(that at least is under our control) should all be the same format
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Mon, 13 May 2024 15:04:51 -0400 |
| parents | 657fda1b9cac |
| children | 91ac90a34003 |
| 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...")); | |
| 279 | 213 for (const auto& status : Anime::ListStatuses) { |
| 214 submenu->addAction(Strings::ToQString(Translate::ToLocalString(status)), [animes, status] { | |
| 215 for (auto& anime : animes) { | |
| 216 if (!anime->IsInUserList()) | |
| 217 anime->AddToUserList(); | |
| 218 anime->SetUserStatus(status); | |
| 219 Services::UpdateAnimeEntry(anime->GetId()); | |
| 220 } | |
| 221 }); | |
| 222 } | |
| 250 | 223 submenu->setEnabled(add_to_list_enable); |
| 224 } | |
| 225 menu->popup(QCursor::pos()); | |
|
54
466ac9870df9
add stub pages (to be implemented)
Paper <mrpapersonic@gmail.com>
parents:
diff
changeset
|
226 } |
| 250 | 227 |
| 228 void SearchPage::ItemDoubleClicked() { | |
| 229 /* throw out any other garbage */ | |
| 230 const QItemSelection selection = sort_model->mapSelectionToSource(treeview->selectionModel()->selection()); | |
| 231 if (!selection.indexes().first().isValid()) | |
| 232 return; | |
| 233 | |
| 234 const QModelIndex index = model->index(selection.indexes().first().row()); | |
| 235 Anime::Anime* anime = model->GetAnimeFromIndex(index); | |
| 236 | |
| 258 | 237 InformationDialog* dialog = new InformationDialog( |
| 238 *anime, | |
| 239 [this, anime] { | |
| 240 // UpdateAnime(anime->GetId()); | |
| 241 }, | |
| 242 InformationDialog::PAGE_MAIN_INFO, this); | |
| 250 | 243 |
| 244 dialog->show(); | |
| 245 dialog->raise(); | |
| 246 dialog->activateWindow(); | |
| 247 } | |
| 248 | |
| 249 SearchPage::SearchPage(QWidget* parent) : QFrame(parent) { | |
| 250 setFrameShape(QFrame::Box); | |
| 251 setFrameShadow(QFrame::Sunken); | |
| 252 | |
| 253 QVBoxLayout* layout = new QVBoxLayout(this); | |
| 254 layout->setContentsMargins(0, 0, 0, 0); | |
| 255 layout->setSpacing(0); | |
| 256 | |
| 257 { | |
| 258 /* Toolbar */ | |
| 259 QToolBar* toolbar = new QToolBar(this); | |
| 260 toolbar->setMovable(false); | |
| 261 | |
| 262 { | |
| 263 QLineEdit* line_edit = new QLineEdit("", toolbar); | |
| 258 | 264 connect(line_edit, &QLineEdit::returnPressed, this, [this, line_edit] { |
| 250 | 265 /* static thread here. */ |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
266 if (thread_.isRunning()) |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
267 thread_.exit(1); /* fail */ |
| 250 | 268 |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
269 thread_.SetSearch(Strings::ToUtf8String(line_edit->text())); |
| 250 | 270 |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
271 thread_.start(); |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
272 }); |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
273 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
|
274 model->ParseSearch(search); |
| 250 | 275 }); |
| 276 toolbar->addWidget(line_edit); | |
| 277 } | |
| 278 | |
| 279 layout->addWidget(toolbar); | |
| 280 } | |
| 281 | |
| 282 { | |
| 283 QFrame* line = new QFrame(this); | |
| 284 line->setFrameShape(QFrame::HLine); | |
| 285 line->setFrameShadow(QFrame::Sunken); | |
| 286 line->setLineWidth(1); | |
| 287 layout->addWidget(line); | |
| 288 } | |
| 289 | |
| 290 { | |
| 291 treeview = new QTreeView(this); | |
| 292 treeview->setUniformRowHeights(true); | |
| 293 treeview->setAllColumnsShowFocus(false); | |
| 294 treeview->setAlternatingRowColors(true); | |
| 295 treeview->setSortingEnabled(true); | |
| 296 treeview->setSelectionMode(QAbstractItemView::ExtendedSelection); | |
| 297 treeview->setItemsExpandable(false); | |
| 298 treeview->setRootIsDecorated(false); | |
| 299 treeview->setContextMenuPolicy(Qt::CustomContextMenu); | |
| 300 treeview->setFrameShape(QFrame::NoFrame); | |
| 301 | |
| 302 { | |
| 303 sort_model = new SearchPageListSortFilter(treeview); | |
| 304 model = new SearchPageListModel(treeview); | |
| 305 sort_model->setSourceModel(model); | |
| 306 sort_model->setSortRole(Qt::UserRole); | |
| 307 sort_model->setSortCaseSensitivity(Qt::CaseInsensitive); | |
| 308 treeview->setModel(sort_model); | |
| 309 } | |
| 310 | |
| 311 // set column sizes | |
| 258 | 312 treeview->setColumnWidth(SearchPageListModel::SR_TITLE, 400); |
| 313 treeview->setColumnWidth(SearchPageListModel::SR_TYPE, 60); | |
| 250 | 314 treeview->setColumnWidth(SearchPageListModel::SR_EPISODES, 60); |
| 258 | 315 treeview->setColumnWidth(SearchPageListModel::SR_SCORE, 60); |
| 316 treeview->setColumnWidth(SearchPageListModel::SR_SEASON, 100); | |
| 250 | 317 |
| 318 treeview->header()->setStretchLastSection(false); | |
| 319 | |
| 320 /* Double click stuff */ | |
| 321 connect(treeview, &QAbstractItemView::doubleClicked, this, &SearchPage::ItemDoubleClicked); | |
| 322 connect(treeview, &QWidget::customContextMenuRequested, this, &SearchPage::DisplayListMenu); | |
| 323 | |
| 324 layout->addWidget(treeview); | |
| 325 } | |
| 326 } |
