Merge pull request #13156 from jagannatharjun/search-style

Use default delegate for SearchModel
This commit is contained in:
Mike Tzou 2020-07-26 20:20:40 +08:00 committed by GitHub
commit dcef3a68f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 138 deletions

View file

@ -52,7 +52,6 @@ add_library(qbt_gui STATIC
search/pluginselectdialog.h
search/pluginsourcedialog.h
search/searchjobwidget.h
search/searchlistdelegate.h
search/searchsortmodel.h
search/searchwidget.h
shutdownconfirmdialog.h
@ -132,7 +131,6 @@ add_library(qbt_gui STATIC
search/pluginselectdialog.cpp
search/pluginsourcedialog.cpp
search/searchjobwidget.cpp
search/searchlistdelegate.cpp
search/searchsortmodel.cpp
search/searchwidget.cpp
shutdownconfirmdialog.cpp

View file

@ -50,7 +50,6 @@ HEADERS += \
$$PWD/search/pluginselectdialog.h \
$$PWD/search/pluginsourcedialog.h \
$$PWD/search/searchjobwidget.h \
$$PWD/search/searchlistdelegate.h \
$$PWD/search/searchsortmodel.h \
$$PWD/search/searchwidget.h \
$$PWD/shutdownconfirmdialog.h \
@ -130,7 +129,6 @@ SOURCES += \
$$PWD/search/pluginselectdialog.cpp \
$$PWD/search/pluginsourcedialog.cpp \
$$PWD/search/searchjobwidget.cpp \
$$PWD/search/searchlistdelegate.cpp \
$$PWD/search/searchsortmodel.cpp \
$$PWD/search/searchwidget.cpp \
$$PWD/shutdownconfirmdialog.cpp \

View file

@ -51,7 +51,6 @@
#include "gui/lineedit.h"
#include "gui/uithememanager.h"
#include "gui/utils.h"
#include "searchlistdelegate.h"
#include "searchsortmodel.h"
#include "ui_searchjobwidget.h"
@ -91,9 +90,6 @@ SearchJobWidget::SearchJobWidget(SearchHandler *searchHandler, QWidget *parent)
m_proxyModel->setNameFilter(searchHandler->pattern());
m_ui->resultsBrowser->setModel(m_proxyModel);
m_searchDelegate = new SearchListDelegate(this);
m_ui->resultsBrowser->setItemDelegate(m_searchDelegate);
m_ui->resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column
m_ui->resultsBrowser->hideColumn(SearchSortModel::DESC_LINK);
@ -513,13 +509,25 @@ void SearchJobWidget::appendSearchResults(const QVector<SearchResult> &results)
int row = m_searchListModel->rowCount();
m_searchListModel->insertRow(row);
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::NAME), result.fileName); // Name
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::DL_LINK), result.fileUrl); // download URL
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::SIZE), result.fileSize); // Size
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::SEEDS), result.nbSeeders); // Seeders
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::LEECHES), result.nbLeechers); // Leechers
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::ENGINE_URL), result.siteUrl); // Search site URL
m_searchListModel->setData(m_searchListModel->index(row, SearchSortModel::DESC_LINK), result.descrLink); // Description Link
const auto setModelData = [this, row] (const int column, const QString &displayData
, const QVariant &underlyingData, const Qt::Alignment textAlignmentData = {})
{
const QMap<int, QVariant> data =
{
{Qt::DisplayRole, displayData},
{SearchSortModel::UnderlyingDataRole, underlyingData},
{Qt::TextAlignmentRole, QVariant {textAlignmentData}}
};
m_searchListModel->setItemData(m_searchListModel->index(row, column), data);
};
setModelData(SearchSortModel::NAME, result.fileName, result.fileName);
setModelData(SearchSortModel::DL_LINK, result.fileUrl, result.fileUrl);
setModelData(SearchSortModel::ENGINE_URL, result.siteUrl, result.siteUrl);
setModelData(SearchSortModel::DESC_LINK, result.descrLink, result.descrLink);
setModelData(SearchSortModel::SIZE, Utils::Misc::friendlyUnit(result.fileSize), result.fileSize, (Qt::AlignRight | Qt::AlignVCenter));
setModelData(SearchSortModel::SEEDS, QString::number(result.nbSeeders), result.nbSeeders, (Qt::AlignRight | Qt::AlignVCenter));
setModelData(SearchSortModel::LEECHES, QString::number(result.nbLeechers), result.nbLeechers, (Qt::AlignRight | Qt::AlignVCenter));
}
updateResultsCount();

View file

@ -40,7 +40,6 @@ class QStandardItemModel;
class LineEdit;
class SearchHandler;
class SearchListDelegate;
class SearchSortModel;
struct SearchResult;
@ -124,7 +123,6 @@ private:
SearchHandler *m_searchHandler;
QStandardItemModel *m_searchListModel;
SearchSortModel *m_proxyModel;
SearchListDelegate *m_searchDelegate;
LineEdit *m_lineEditSearchResultsFilter;
Status m_status = Status::Ongoing;
bool m_noSearchResults = true;

View file

@ -1,72 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "searchlistdelegate.h"
#include <QModelIndex>
#include <QPainter>
#include <QStyleOptionViewItem>
#include "base/utils/misc.h"
#include "searchsortmodel.h"
SearchListDelegate::SearchListDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
void SearchListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->save();
QStyleOptionViewItem opt = QItemDelegate::setOptions(index, option);
QItemDelegate::drawBackground(painter, opt, index);
switch (index.column()) {
case SearchSortModel::SIZE:
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, option.rect, Utils::Misc::friendlyUnit(index.data().toLongLong()));
break;
case SearchSortModel::SEEDS:
case SearchSortModel::LEECHES:
opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
QItemDelegate::drawDisplay(painter, opt, option.rect
, (index.data().toLongLong() >= 0) ? index.data().toString() : tr("Unknown"));
break;
default:
QItemDelegate::paint(painter, option, index);
}
painter->restore();
}
QWidget *SearchListDelegate::createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const
{
// No editor here
return nullptr;
}

View file

@ -1,45 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#ifndef SEARCHLISTDELEGATE_H
#define SEARCHLISTDELEGATE_H
#include <QItemDelegate>
class SearchListDelegate final : public QItemDelegate
{
Q_OBJECT
public:
explicit SearchListDelegate(QObject *parent);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const override;
};
#endif // SEARCHLISTDELEGATE_H

View file

@ -41,6 +41,8 @@ SearchSortModel::SearchSortModel(QObject *parent)
, m_minSize(0)
, m_maxSize(-1)
{
setSortRole(UnderlyingDataRole);
setFilterRole(UnderlyingDataRole);
}
void SearchSortModel::enableNameFilter(const bool enabled)
@ -129,7 +131,7 @@ bool SearchSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &s
const QAbstractItemModel *const sourceModel = this->sourceModel();
if (m_isNameFilterEnabled && !m_searchTerm.isEmpty()) {
const QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent)).toString();
const QString name = sourceModel->data(sourceModel->index(sourceRow, NAME, sourceParent), UnderlyingDataRole).toString();
for (const QString &word : asConst(m_searchTermWords)) {
if (!name.contains(word, Qt::CaseInsensitive))
return false;
@ -137,21 +139,21 @@ bool SearchSortModel::filterAcceptsRow(const int sourceRow, const QModelIndex &s
}
if ((m_minSize > 0) || (m_maxSize >= 0)) {
const qlonglong size = sourceModel->data(sourceModel->index(sourceRow, SIZE, sourceParent)).toLongLong();
const qlonglong size = sourceModel->data(sourceModel->index(sourceRow, SIZE, sourceParent), UnderlyingDataRole).toLongLong();
if (((m_minSize > 0) && (size < m_minSize))
|| ((m_maxSize > 0) && (size > m_maxSize)))
return false;
}
if ((m_minSeeds > 0) || (m_maxSeeds >= 0)) {
const int seeds = sourceModel->data(sourceModel->index(sourceRow, SEEDS, sourceParent)).toInt();
const int seeds = sourceModel->data(sourceModel->index(sourceRow, SEEDS, sourceParent), UnderlyingDataRole).toInt();
if (((m_minSeeds > 0) && (seeds < m_minSeeds))
|| ((m_maxSeeds > 0) && (seeds > m_maxSeeds)))
return false;
}
if ((m_minLeeches > 0) || (m_maxLeeches >= 0)) {
const int leeches = sourceModel->data(sourceModel->index(sourceRow, LEECHES, sourceParent)).toInt();
const int leeches = sourceModel->data(sourceModel->index(sourceRow, LEECHES, sourceParent), UnderlyingDataRole).toInt();
if (((m_minLeeches > 0) && (leeches < m_minLeeches))
|| ((m_maxLeeches > 0) && (leeches > m_maxLeeches)))
return false;

View file

@ -49,6 +49,11 @@ public:
NB_SEARCH_COLUMNS
};
enum SearchDataRole
{
UnderlyingDataRole = Qt::UserRole
};
explicit SearchSortModel(QObject *parent = nullptr);
void enableNameFilter(bool enabled);