From f6e4ff1ab963c49144eee45d8430518097bf87c3 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Tue, 9 Jul 2013 19:49:25 +0300 Subject: [PATCH] Use naturalSort in search. --- src/searchengine/searchengine.cpp | 24 +++++++++--------- src/searchengine/searchengine.h | 2 -- src/searchengine/searchengine.pri | 3 ++- src/searchengine/searchlistdelegate.h | 2 +- src/searchengine/searchsortmodel.h | 35 +++++++++++++++++++++++++++ src/searchengine/searchtab.cpp | 26 ++++++++++---------- src/searchengine/searchtab.h | 4 +-- 7 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 src/searchengine/searchsortmodel.h diff --git a/src/searchengine/searchengine.cpp b/src/searchengine/searchengine.cpp index 7a2228b33..0e6eb90b1 100644 --- a/src/searchengine/searchengine.cpp +++ b/src/searchengine/searchengine.cpp @@ -539,26 +539,26 @@ void SearchEngine::appendSearchResult(const QString &line) { int row = cur_model->rowCount(); cur_model->insertRow(row); - cur_model->setData(cur_model->index(row, DL_LINK), parts.at(PL_DL_LINK).trimmed()); // download URL - cur_model->setData(cur_model->index(row, NAME), parts.at(PL_NAME).trimmed()); // Name - cur_model->setData(cur_model->index(row, SIZE), parts.at(PL_SIZE).trimmed().toLongLong()); // Size + cur_model->setData(cur_model->index(row, SearchSortModel::DL_LINK), parts.at(PL_DL_LINK).trimmed()); // download URL + cur_model->setData(cur_model->index(row, SearchSortModel::NAME), parts.at(PL_NAME).trimmed()); // Name + cur_model->setData(cur_model->index(row, SearchSortModel::SIZE), parts.at(PL_SIZE).trimmed().toLongLong()); // Size bool ok = false; qlonglong nb_seeders = parts.at(PL_SEEDS).trimmed().toLongLong(&ok); if (!ok || nb_seeders < 0) { - cur_model->setData(cur_model->index(row, SEEDS), tr("Unknown")); // Seeders + cur_model->setData(cur_model->index(row, SearchSortModel::SEEDS), tr("Unknown")); // Seeders } else { - cur_model->setData(cur_model->index(row, SEEDS), nb_seeders); // Seeders + cur_model->setData(cur_model->index(row, SearchSortModel::SEEDS), nb_seeders); // Seeders } qlonglong nb_leechers = parts.at(PL_LEECHS).trimmed().toLongLong(&ok); if (!ok || nb_leechers < 0) { - cur_model->setData(cur_model->index(row, LEECHS), tr("Unknown")); // Leechers + cur_model->setData(cur_model->index(row, SearchSortModel::LEECHS), tr("Unknown")); // Leechers } else { - cur_model->setData(cur_model->index(row, LEECHS), nb_leechers); // Leechers + cur_model->setData(cur_model->index(row, SearchSortModel::LEECHS), nb_leechers); // Leechers } - cur_model->setData(cur_model->index(row, ENGINE_URL), parts.at(PL_ENGINE_URL).trimmed()); // Engine URL + cur_model->setData(cur_model->index(row, SearchSortModel::ENGINE_URL), parts.at(PL_ENGINE_URL).trimmed()); // Engine URL // Description Link if (nb_fields == NB_PLUGIN_COLUMNS) - cur_model->setData(cur_model->index(row, DESC_LINK), parts.at(PL_DESC_LINK).trimmed()); + cur_model->setData(cur_model->index(row, SearchSortModel::DESC_LINK), parts.at(PL_DESC_LINK).trimmed()); no_search_results = false; ++nb_search_results; @@ -591,7 +591,7 @@ void SearchEngine::on_download_button_clicked() { //QModelIndexList selectedIndexes = currentSearchTab->getCurrentTreeView()->selectionModel()->selectedIndexes(); QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); foreach (const QModelIndex &index, selectedIndexes) { - if (index.column() == NAME) { + if (index.column() == SearchSortModel::NAME) { // Get Item url QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); QString torrent_url = model->data(model->index(index.row(), URL_COLUMN)).toString(); @@ -606,9 +606,9 @@ void SearchEngine::on_goToDescBtn_clicked() { QModelIndexList selectedIndexes = all_tab.at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); foreach (const QModelIndex &index, selectedIndexes) { - if (index.column() == NAME) { + if (index.column() == SearchSortModel::NAME) { QSortFilterProxyModel* model = all_tab.at(tabWidget->currentIndex())->getCurrentSearchListProxy(); - const QString desc_url = model->data(model->index(index.row(), DESC_LINK)).toString(); + const QString desc_url = model->data(model->index(index.row(), SearchSortModel::DESC_LINK)).toString(); if (!desc_url.isEmpty()) QDesktopServices::openUrl(QUrl::fromEncoded(desc_url.toUtf8())); } diff --git a/src/searchengine/searchengine.h b/src/searchengine/searchengine.h index e33c5870c..c10c04338 100644 --- a/src/searchengine/searchengine.h +++ b/src/searchengine/searchengine.h @@ -54,8 +54,6 @@ class SearchEngine : public QWidget, public Ui::search_engine{ Q_OBJECT Q_DISABLE_COPY(SearchEngine) -public: - enum SearchColumn { NAME, SIZE, SEEDS, LEECHS, ENGINE_URL, DL_LINK, DESC_LINK, NB_SEARCH_COLUMNS }; private: enum PluginColumn { PL_DL_LINK, PL_NAME, PL_SIZE, PL_SEEDS, PL_LEECHS, PL_ENGINE_URL, PL_DESC_LINK, NB_PLUGIN_COLUMNS }; diff --git a/src/searchengine/searchengine.pri b/src/searchengine/searchengine.pri index 8a4ac7c46..9a514a4b4 100644 --- a/src/searchengine/searchengine.pri +++ b/src/searchengine/searchengine.pri @@ -9,7 +9,8 @@ HEADERS += $$PWD/searchengine.h \ $$PWD/engineselectdlg.h \ $$PWD/pluginsource.h \ $$PWD/searchlistdelegate.h \ - $$PWD/supportedengines.h + $$PWD/supportedengines.h \ + $$PWD/searchsortmodel.h SOURCES += $$PWD/searchengine.cpp \ $$PWD/searchtab.cpp \ diff --git a/src/searchengine/searchlistdelegate.h b/src/searchengine/searchlistdelegate.h index da63da57a..ca3cd14f6 100644 --- a/src/searchengine/searchlistdelegate.h +++ b/src/searchengine/searchlistdelegate.h @@ -52,7 +52,7 @@ class SearchListDelegate: public QItemDelegate { painter->save(); QStyleOptionViewItemV2 opt = QItemDelegate::setOptions(index, option); switch(index.column()) { - case SearchEngine::SIZE: + case SearchSortModel::SIZE: QItemDelegate::drawBackground(painter, opt, index); QItemDelegate::drawDisplay(painter, opt, option.rect, misc::friendlyUnit(index.data().toLongLong())); break; diff --git a/src/searchengine/searchsortmodel.h b/src/searchengine/searchsortmodel.h new file mode 100644 index 000000000..7692128f3 --- /dev/null +++ b/src/searchengine/searchsortmodel.h @@ -0,0 +1,35 @@ +#ifndef SEARCHSORTMODEL_H +#define SEARCHSORTMODEL_H + +#include +#include "misc.h" + +class SearchSortModel : public QSortFilterProxyModel { + Q_OBJECT + +public: + enum SearchColumn { NAME, SIZE, SEEDS, LEECHS, ENGINE_URL, DL_LINK, DESC_LINK, NB_SEARCH_COLUMNS }; + + SearchSortModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {} + +protected: + virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const { + if (sortColumn() == NAME || sortColumn() == ENGINE_URL) { + QVariant vL = sourceModel()->data(left); + QVariant vR = sourceModel()->data(right); + if (!(vL.isValid() && vR.isValid())) + return QSortFilterProxyModel::lessThan(left, right); + Q_ASSERT(vL.isValid()); + Q_ASSERT(vR.isValid()); + + bool res = false; + if (misc::naturalSort(vL.toString(), vR.toString(), res)) + return res; + + return QSortFilterProxyModel::lessThan(left, right); + } + return QSortFilterProxyModel::lessThan(left, right); + } +}; + +#endif // SEARCHSORTMODEL_H diff --git a/src/searchengine/searchtab.cpp b/src/searchengine/searchtab.cpp index cc6a96d97..ce9cae3f8 100644 --- a/src/searchengine/searchtab.cpp +++ b/src/searchengine/searchtab.cpp @@ -51,14 +51,14 @@ SearchTab::SearchTab(SearchEngine *parent) : QWidget(), parent(parent) setLayout(box); // Set Search results list model - SearchListModel = new QStandardItemModel(0, SearchEngine::NB_SEARCH_COLUMNS); - SearchListModel->setHeaderData(SearchEngine::NAME, Qt::Horizontal, tr("Name", "i.e: file name")); - SearchListModel->setHeaderData(SearchEngine::SIZE, Qt::Horizontal, tr("Size", "i.e: file size")); - SearchListModel->setHeaderData(SearchEngine::SEEDS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources")); - SearchListModel->setHeaderData(SearchEngine::LEECHS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources")); - SearchListModel->setHeaderData(SearchEngine::ENGINE_URL, Qt::Horizontal, tr("Search engine")); - - proxyModel = new QSortFilterProxyModel(); + SearchListModel = new QStandardItemModel(0, SearchSortModel::NB_SEARCH_COLUMNS); + SearchListModel->setHeaderData(SearchSortModel::NAME, Qt::Horizontal, tr("Name", "i.e: file name")); + SearchListModel->setHeaderData(SearchSortModel::SIZE, Qt::Horizontal, tr("Size", "i.e: file size")); + SearchListModel->setHeaderData(SearchSortModel::SEEDS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources")); + SearchListModel->setHeaderData(SearchSortModel::LEECHS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources")); + SearchListModel->setHeaderData(SearchSortModel::ENGINE_URL, Qt::Horizontal, tr("Search engine")); + + proxyModel = new SearchSortModel(); proxyModel->setDynamicSortFilter(true); proxyModel->setSourceModel(SearchListModel); resultsBrowser->setModel(proxyModel); @@ -66,8 +66,8 @@ SearchTab::SearchTab(SearchEngine *parent) : QWidget(), parent(parent) SearchDelegate = new SearchListDelegate(); resultsBrowser->setItemDelegate(SearchDelegate); - resultsBrowser->hideColumn(SearchEngine::DL_LINK); // Hide url column - resultsBrowser->hideColumn(SearchEngine::DESC_LINK); + resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column + resultsBrowser->hideColumn(SearchSortModel::DESC_LINK); resultsBrowser->setRootIsDecorated(false); resultsBrowser->setAllColumnsShowFocus(true); @@ -82,12 +82,12 @@ SearchTab::SearchTab(SearchEngine *parent) : QWidget(), parent(parent) } // Sort by Seeds - resultsBrowser->sortByColumn(SearchEngine::SEEDS, Qt::DescendingOrder); + resultsBrowser->sortByColumn(SearchSortModel::SEEDS, Qt::DescendingOrder); } void SearchTab::downloadSelectedItem(const QModelIndex& index) { - QString engine_url = proxyModel->data(proxyModel->index(index.row(), SearchEngine::ENGINE_URL)).toString(); - QString torrent_url = proxyModel->data(proxyModel->index(index.row(), SearchEngine::DL_LINK)).toString(); + QString engine_url = proxyModel->data(proxyModel->index(index.row(), SearchSortModel::ENGINE_URL)).toString(); + QString torrent_url = proxyModel->data(proxyModel->index(index.row(), SearchSortModel::DL_LINK)).toString(); setRowColor(index.row(), "red"); parent->downloadTorrent(engine_url, torrent_url); } diff --git a/src/searchengine/searchtab.h b/src/searchengine/searchtab.h index dfc77c2e2..3477258bb 100644 --- a/src/searchengine/searchtab.h +++ b/src/searchengine/searchtab.h @@ -32,6 +32,7 @@ #define SEARCH_TAB_H #include "ui_search.h" +#include "searchsortmodel.h" #define ENGINE_URL_COLUMN 4 #define URL_COLUMN 5 @@ -43,7 +44,6 @@ QT_BEGIN_NAMESPACE class QTreeView; class QHeaderView; class QStandardItemModel; -class QSortFilterProxyModel; QT_END_NAMESPACE class SearchTab: public QWidget, public Ui::search_engine { @@ -54,7 +54,7 @@ private: QLabel *results_lbl; QTreeView *resultsBrowser; QStandardItemModel *SearchListModel; - QSortFilterProxyModel *proxyModel; + SearchSortModel *proxyModel; SearchListDelegate *SearchDelegate; SearchEngine *parent;