Browse Source

Use default delegate for SearchModel

Introduce and use data role - SearchFilterModel::UnderlyingDataRole as a filter role in SearchFilterModel

Removes the usage of QItemDelegate
adaptive-webui-19844
jagannatharjun 4 years ago
parent
commit
00413b0dd1
  1. 2
      src/gui/CMakeLists.txt
  2. 2
      src/gui/gui.pri
  3. 30
      src/gui/search/searchjobwidget.cpp
  4. 2
      src/gui/search/searchjobwidget.h
  5. 72
      src/gui/search/searchlistdelegate.cpp
  6. 45
      src/gui/search/searchlistdelegate.h
  7. 10
      src/gui/search/searchsortmodel.cpp
  8. 5
      src/gui/search/searchsortmodel.h

2
src/gui/CMakeLists.txt

@ -53,7 +53,6 @@ add_library(qbt_gui STATIC
search/pluginselectdialog.h search/pluginselectdialog.h
search/pluginsourcedialog.h search/pluginsourcedialog.h
search/searchjobwidget.h search/searchjobwidget.h
search/searchlistdelegate.h
search/searchsortmodel.h search/searchsortmodel.h
search/searchwidget.h search/searchwidget.h
shutdownconfirmdialog.h shutdownconfirmdialog.h
@ -134,7 +133,6 @@ add_library(qbt_gui STATIC
search/pluginselectdialog.cpp search/pluginselectdialog.cpp
search/pluginsourcedialog.cpp search/pluginsourcedialog.cpp
search/searchjobwidget.cpp search/searchjobwidget.cpp
search/searchlistdelegate.cpp
search/searchsortmodel.cpp search/searchsortmodel.cpp
search/searchwidget.cpp search/searchwidget.cpp
shutdownconfirmdialog.cpp shutdownconfirmdialog.cpp

2
src/gui/gui.pri

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

30
src/gui/search/searchjobwidget.cpp

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

2
src/gui/search/searchjobwidget.h

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

72
src/gui/search/searchlistdelegate.cpp

@ -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;
}

45
src/gui/search/searchlistdelegate.h

@ -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

10
src/gui/search/searchsortmodel.cpp

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

5
src/gui/search/searchsortmodel.h

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

Loading…
Cancel
Save