From b6b819a2a18e19cc0171b9f84d32d6ae6bf4268f Mon Sep 17 00:00:00 2001 From: Eugene Shalygin Date: Sat, 27 Feb 2016 21:37:41 +0100 Subject: [PATCH] Optimize widgets inside of the search tab Since we already have searchtab.ui, let's set up all the widgets there. Additionally, save a bit of vertical space by putting results label in a row with the filter widgets. --- src/gui/search/searchtab.cpp | 114 +++++++++++++++++------------------ src/gui/search/searchtab.h | 13 ++-- src/gui/search/searchtab.ui | 10 +++ 3 files changed, 76 insertions(+), 61 deletions(-) diff --git a/src/gui/search/searchtab.cpp b/src/gui/search/searchtab.cpp index c01140041..059749af2 100644 --- a/src/gui/search/searchtab.cpp +++ b/src/gui/search/searchtab.cpp @@ -48,6 +48,7 @@ #include "searchlistdelegate.h" #include "searchwidget.h" #include "searchtab.h" +#include "ui_searchtab.h" namespace { @@ -58,25 +59,20 @@ namespace SearchTab::SearchTab(SearchWidget *parent) : QWidget(parent) + , m_ui(new Ui::SearchTab()) , m_parent(parent) { - setupUi(this); - retranslateUi(this); + m_ui->setupUi(this); - m_box = static_cast(this->layout()); - m_resultsLbl = new QLabel(this); - m_resultsBrowser = new QTreeView(this); #ifdef QBT_USES_QT5 // This hack fixes reordering of first column with Qt5. // https://github.com/qtproject/qtbase/commit/e0fc088c0c8bc61dbcaf5928b24986cd61a22777 QTableView unused; - unused.setVerticalHeader(m_resultsBrowser->header()); - m_resultsBrowser->header()->setParent(m_resultsBrowser); + unused.setVerticalHeader(m_ui->resultsBrowser->header()); + m_ui->resultsBrowser->header()->setParent(m_ui->resultsBrowser); unused.setVerticalHeader(new QHeaderView(Qt::Horizontal)); #endif - m_resultsBrowser->setSelectionMode(QAbstractItemView::ExtendedSelection); - m_box->addWidget(m_resultsLbl); - m_box->addWidget(m_resultsBrowser); + m_ui->resultsBrowser->setSelectionMode(QAbstractItemView::ExtendedSelection); // Set Search results list model m_searchListModel = new QStandardItemModel(0, SearchSortModel::NB_SEARCH_COLUMNS, this); @@ -89,43 +85,48 @@ SearchTab::SearchTab(SearchWidget *parent) m_proxyModel = new SearchSortModel(this); m_proxyModel->setDynamicSortFilter(true); m_proxyModel->setSourceModel(m_searchListModel); - m_resultsBrowser->setModel(m_proxyModel); + m_ui->resultsBrowser->setModel(m_proxyModel); m_searchDelegate = new SearchListDelegate(this); - m_resultsBrowser->setItemDelegate(m_searchDelegate); + m_ui->resultsBrowser->setItemDelegate(m_searchDelegate); - m_resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column - m_resultsBrowser->hideColumn(SearchSortModel::DESC_LINK); + m_ui->resultsBrowser->hideColumn(SearchSortModel::DL_LINK); // Hide url column + m_ui->resultsBrowser->hideColumn(SearchSortModel::DESC_LINK); - m_resultsBrowser->setRootIsDecorated(false); - m_resultsBrowser->setAllColumnsShowFocus(true); - m_resultsBrowser->setSortingEnabled(true); + m_ui->resultsBrowser->setRootIsDecorated(false); + m_ui->resultsBrowser->setAllColumnsShowFocus(true); + m_ui->resultsBrowser->setSortingEnabled(true); // Connect signals to slots (search part) - connect(m_resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(downloadSelectedItem(const QModelIndex&))); + connect(m_ui->resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(downloadSelectedItem(const QModelIndex&))); // Load last columns width for search results list if (!loadColWidthResultsList()) - m_resultsBrowser->header()->resizeSection(0, 275); + m_ui->resultsBrowser->header()->resizeSection(0, 275); // Sort by Seeds - m_resultsBrowser->sortByColumn(SearchSortModel::SEEDS, Qt::DescendingOrder); + m_ui->resultsBrowser->sortByColumn(SearchSortModel::SEEDS, Qt::DescendingOrder); fillFilterComboBoxes(); updateFilter(); - connect(filterMode, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFilter())); - connect(minSeeds, SIGNAL(editingFinished()), this, SLOT(updateFilter())); - connect(minSeeds, SIGNAL(valueChanged(int)), this, SLOT(updateFilter())); - connect(maxSeeds, SIGNAL(editingFinished()), this, SLOT(updateFilter())); - connect(maxSeeds, SIGNAL(valueChanged(int)), this, SLOT(updateFilter())); - connect(minSize, SIGNAL(editingFinished()), this, SLOT(updateFilter())); - connect(minSize, SIGNAL(valueChanged(double)), this, SLOT(updateFilter())); - connect(maxSize, SIGNAL(editingFinished()), this, SLOT(updateFilter())); - connect(maxSize, SIGNAL(valueChanged(double)), this, SLOT(updateFilter())); - connect(minSizeUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFilter())); - connect(maxSizeUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFilter())); + connect(m_ui->filterMode, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFilter())); + connect(m_ui->minSeeds, SIGNAL(editingFinished()), this, SLOT(updateFilter())); + connect(m_ui->minSeeds, SIGNAL(valueChanged(int)), this, SLOT(updateFilter())); + connect(m_ui->maxSeeds, SIGNAL(editingFinished()), this, SLOT(updateFilter())); + connect(m_ui->maxSeeds, SIGNAL(valueChanged(int)), this, SLOT(updateFilter())); + connect(m_ui->minSize, SIGNAL(editingFinished()), this, SLOT(updateFilter())); + connect(m_ui->minSize, SIGNAL(valueChanged(double)), this, SLOT(updateFilter())); + connect(m_ui->maxSize, SIGNAL(editingFinished()), this, SLOT(updateFilter())); + connect(m_ui->maxSize, SIGNAL(valueChanged(double)), this, SLOT(updateFilter())); + connect(m_ui->minSizeUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFilter())); + connect(m_ui->maxSizeUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(updateFilter())); +} + +SearchTab::~SearchTab() +{ + delete m_ui; } void SearchTab::downloadSelectedItem(const QModelIndex &index) @@ -137,7 +138,7 @@ void SearchTab::downloadSelectedItem(const QModelIndex &index) QHeaderView* SearchTab::header() const { - return m_resultsBrowser->header(); + return m_ui->resultsBrowser->header(); } bool SearchTab::loadColWidthResultsList() @@ -149,16 +150,15 @@ bool SearchTab::loadColWidthResultsList() if (widthList.size() > m_searchListModel->columnCount()) return false; - unsigned int listSize = widthList.size(); - for (unsigned int i = 0; i < listSize; ++i) - m_resultsBrowser->header()->resizeSection(i, widthList.at(i).toInt()); + for (int i = 0; i < widthList.size(); ++i) + m_ui->resultsBrowser->header()->resizeSection(i, widthList.at(i).toInt()); return true; } QTreeView* SearchTab::getCurrentTreeView() const { - return m_resultsBrowser; + return m_ui->resultsBrowser; } SearchSortModel* SearchTab::getCurrentSearchListProxy() const @@ -199,8 +199,8 @@ void SearchTab::updateResultsCount() { const int totalResults = getCurrentSearchListModel() ? getCurrentSearchListModel()->rowCount(QModelIndex()) : 0; const int filteredResults = getCurrentSearchListProxy() ? getCurrentSearchListProxy()->rowCount(QModelIndex()) : totalResults; - m_resultsLbl->setText(tr("Results (showing %1 out of %2):", "i.e: Search results") - .arg(filteredResults).arg(totalResults)); + m_ui->resultsLbl->setText(tr("Results (showing %1 out of %2):", "i.e: Search results") + .arg(filteredResults).arg(totalResults)); } void SearchTab::updateFilter() @@ -209,14 +209,14 @@ void SearchTab::updateFilter() SearchSortModel* filterModel = getCurrentSearchListProxy(); filterModel->enableNameFilter(filteringMode() == OnlyNames); // we update size and seeds filter parameters in the model even if they are disabled - // because we need to read them from the model when search tabs switch - filterModel->setSeedsFilter(minSeeds->value(), maxSeeds->value()); + filterModel->setSeedsFilter(m_ui->minSeeds->value(), m_ui->maxSeeds->value()); filterModel->setSizeFilter( - sizeInBytes(minSize->value(), static_cast(minSizeUnit->currentIndex())), - sizeInBytes(maxSize->value(), static_cast(maxSizeUnit->currentIndex()))); + sizeInBytes(m_ui->minSize->value(), static_cast(m_ui->minSizeUnit->currentIndex())), + sizeInBytes(m_ui->maxSize->value(), static_cast(m_ui->maxSizeUnit->currentIndex()))); SettingsStorage::instance()->storeValue(KEY_FILTER_MODE_SETTING_NAME, - filterMode->itemData(filterMode->currentIndex())); + m_ui->filterMode->itemData(m_ui->filterMode->currentIndex())); + filterModel->invalidate(); updateResultsCount(); } @@ -233,29 +233,29 @@ void SearchTab::fillFilterComboBoxes() unitStrings.append(unitString(SizeUnit::PebiByte)); unitStrings.append(unitString(SizeUnit::ExbiByte)); - minSizeUnit->clear(); - maxSizeUnit->clear(); - minSizeUnit->addItems(unitStrings); - maxSizeUnit->addItems(unitStrings); + m_ui->minSizeUnit->clear(); + m_ui->maxSizeUnit->clear(); + m_ui->minSizeUnit->addItems(unitStrings); + m_ui->maxSizeUnit->addItems(unitStrings); - minSize->setValue(0); - minSizeUnit->setCurrentIndex(static_cast(SizeUnit::MebiByte)); + m_ui->minSize->setValue(0); + m_ui->minSizeUnit->setCurrentIndex(static_cast(SizeUnit::MebiByte)); - maxSize->setValue(-1); - maxSizeUnit->setCurrentIndex(static_cast(SizeUnit::TebiByte)); + m_ui->maxSize->setValue(-1); + m_ui->maxSizeUnit->setCurrentIndex(static_cast(SizeUnit::TebiByte)); - filterMode->clear(); + m_ui->filterMode->clear(); QMetaEnum nameFilteringModeEnum = this->metaObject()->enumerator(this->metaObject()->indexOfEnumerator("NameFilteringMode")); - filterMode->addItem(tr("Torrent names only"), nameFilteringModeEnum.valueToKey(OnlyNames)); - filterMode->addItem(tr("Everywhere"), nameFilteringModeEnum.valueToKey(Everywhere)); + m_ui->filterMode->addItem(tr("Torrent names only"), nameFilteringModeEnum.valueToKey(OnlyNames)); + m_ui->filterMode->addItem(tr("Everywhere"), nameFilteringModeEnum.valueToKey(Everywhere)); QVariant selectedMode = SettingsStorage::instance()->loadValue( KEY_FILTER_MODE_SETTING_NAME, nameFilteringModeEnum.valueToKey(OnlyNames)); - int index = filterMode->findData(selectedMode); - filterMode->setCurrentIndex(index == -1 ? 0 : index); + int index = m_ui->filterMode->findData(selectedMode); + m_ui->filterMode->setCurrentIndex(index == -1 ? 0 : index); } QString SearchTab::statusText(SearchTab::Status st) @@ -298,5 +298,5 @@ SearchTab::NameFilteringMode SearchTab::filteringMode() const { QMetaEnum metaEnum = this->metaObject()->enumerator(this->metaObject()->indexOfEnumerator("NameFilteringMode")); - return static_cast(metaEnum.keyToValue(filterMode->itemData(filterMode->currentIndex()).toByteArray())); + return static_cast(metaEnum.keyToValue(m_ui->filterMode->itemData(m_ui->filterMode->currentIndex()).toByteArray())); } diff --git a/src/gui/search/searchtab.h b/src/gui/search/searchtab.h index cc341d90a..45ecb6d09 100644 --- a/src/gui/search/searchtab.h +++ b/src/gui/search/searchtab.h @@ -32,7 +32,7 @@ #define SEARCHTAB_H #include // I don't know why is not enought for Qt's 4.8.7 moc -#include "ui_searchtab.h" +#include #define ENGINE_URL_COLUMN 4 #define URL_COLUMN 5 @@ -48,7 +48,12 @@ class SearchSortModel; class SearchListDelegate; class SearchWidget; -class SearchTab: public QWidget, private Ui::SearchTab +namespace Ui +{ + class SearchTab; +} + +class SearchTab: public QWidget { Q_OBJECT @@ -63,6 +68,7 @@ public: Q_ENUMS(NameFilteringMode) explicit SearchTab(SearchWidget *parent); + ~SearchTab(); QStandardItemModel* getCurrentSearchListModel() const; SearchSortModel* getCurrentSearchListProxy() const; @@ -96,8 +102,7 @@ private: static QString statusText(Status st); static QString statusIconName(Status st); - QVBoxLayout *m_box; - QLabel *m_resultsLbl; + Ui::SearchTab *m_ui; QTreeView *m_resultsBrowser; QStandardItemModel *m_searchListModel; SearchSortModel *m_proxyModel; diff --git a/src/gui/search/searchtab.ui b/src/gui/search/searchtab.ui index b4e76c0b5..d2ec133d6 100644 --- a/src/gui/search/searchtab.ui +++ b/src/gui/search/searchtab.ui @@ -16,6 +16,13 @@ + + + + Results(xxx) + + + @@ -233,6 +240,9 @@ + + +