From cecd1e11b1a0022d0be8faafb7e616bd51c74968 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Wed, 25 Jun 2008 19:29:29 +0000 Subject: [PATCH] - Tabbed search engine code cleanup (moved SearchTab class to different file) --- src/SearchTab.cpp | 191 +++++++++++++++++++++++++++++++++++++++++++ src/SearchTab.h | 59 +++++++++++++ src/searchEngine.cpp | 174 ++------------------------------------- src/searchEngine.h | 32 +------- src/src.pro | 5 +- 5 files changed, 261 insertions(+), 200 deletions(-) create mode 100644 src/SearchTab.cpp create mode 100644 src/SearchTab.h diff --git a/src/SearchTab.cpp b/src/SearchTab.cpp new file mode 100644 index 000000000..cd7c484df --- /dev/null +++ b/src/SearchTab.cpp @@ -0,0 +1,191 @@ +/* + * Bittorrent Client using Qt4 and libtorrent. + * Copyright (C) 2006 Christophe Dumez + * + * 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. + * + * Contact : chris@qbittorrent.org + */ + +#include +#include +#include +#include +#include + +#include "SearchTab.h" +#include "SearchListDelegate.h" +#include "misc.h" +#include "searchEngine.h" + +#define SEARCH_NAME 0 +#define SEARCH_SIZE 1 +#define SEARCH_SEEDERS 2 +#define SEARCH_LEECHERS 3 +#define SEARCH_ENGINE 4 + +QList* SearchTab::all_tab = new QList(); +SearchTab::SearchTab(QString &title,QTabWidget *tab_barWidget,SearchEngine *searchEngi) : QWidget() +{ + box=new QVBoxLayout(); + results_lbl=new QLabel(); + resultsBrowser = new QTreeView(); + box->addWidget(results_lbl); + box->addWidget(resultsBrowser); + + setLayout(box); + // Set Search results list model + SearchListModel = new QStandardItemModel(0,5); + SearchListModel->setHeaderData(SEARCH_NAME, Qt::Horizontal, tr("Name", "i.e: file name")); + SearchListModel->setHeaderData(SEARCH_SIZE, Qt::Horizontal, tr("Size", "i.e: file size")); + SearchListModel->setHeaderData(SEARCH_SEEDERS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources")); + SearchListModel->setHeaderData(SEARCH_LEECHERS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources")); + SearchListModel->setHeaderData(SEARCH_ENGINE, Qt::Horizontal, tr("Search engine")); + resultsBrowser->setModel(SearchListModel); + SearchDelegate = new SearchListDelegate(); + resultsBrowser->setItemDelegate(SearchDelegate); + // Make search list header clickable for sorting + resultsBrowser->header()->setClickable(true); + resultsBrowser->header()->setSortIndicatorShown(true); + + // Connect signals to slots (search part) + connect(resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), searchEngi, SLOT(downloadSelectedItem(const QModelIndex&))); + connect(resultsBrowser->header(), SIGNAL(sectionPressed(int)), this, SLOT(sortSearchList(int))); + + // Load last columns width for search results list + if(!loadColWidthSearchList()){ + resultsBrowser->header()->resizeSection(0, 275); + } + tab_barWidget->addTab(this, title); + all_tab->append(this); +} + +SearchTab::~SearchTab() +{ + saveColWidthSearchList(); + delete resultsBrowser; + delete SearchListModel; + delete SearchDelegate; +} + +QLabel* SearchTab::getCurrentLabel() +{ + return results_lbl; +} + +QTreeView* SearchTab::getCurrentTreeView() +{ + return resultsBrowser; +} + +QStandardItemModel* SearchTab::getCurrentSearchListModel() +{ + return SearchListModel; +} + +// Set the color of a row in data model +void SearchTab::setRowColor(int row, QString color){ + for(int i=0; icolumnCount(); ++i){ + SearchListModel->setData(SearchListModel->index(row, i), QVariant(QColor(color)), Qt::ForegroundRole); + } +} + +void SearchTab::sortSearchList(int index){ + static Qt::SortOrder sortOrder = Qt::AscendingOrder; + if(resultsBrowser->header()->sortIndicatorSection() == index){ + sortOrder = (sortOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder; ; + } + resultsBrowser->header()->setSortIndicator(index, sortOrder); + switch(index){ + case SEEDERS: + case LEECHERS: + case SIZE: + sortSearchListInt(index, sortOrder); + break; + default: + sortSearchListString(index, sortOrder); + } +} + +void SearchTab::sortSearchListInt(int index, Qt::SortOrder sortOrder){ + QList > lines; + // Insertion sorting + for(int i=0; irowCount(); ++i){ + misc::insertSort(lines, QPair(i, SearchListModel->data(SearchListModel->index(i, index)).toLongLong()), sortOrder); + } + // Insert items in new model, in correct order + int nbRows_old = lines.size(); + for(int row=0; rowinsertRow(SearchListModel->rowCount()); + int sourceRow = lines[row].first; + for(int col=0; col<5; ++col){ + SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col))); + SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col), Qt::ForegroundRole), Qt::ForegroundRole); + } + } + // Remove old rows + SearchListModel->removeRows(0, nbRows_old); +} + +void SearchTab::sortSearchListString(int index, Qt::SortOrder sortOrder){ + QList > lines; + // Insetion sorting + for(int i=0; irowCount(); ++i){ + misc::insertSortString(lines, QPair(i, SearchListModel->data(SearchListModel->index(i, index)).toString()), sortOrder); + } + // Insert items in new model, in correct order + int nbRows_old = lines.size(); + for(int row=0; rowinsertRow(SearchListModel->rowCount()); + int sourceRow = lines[row].first; + for(int col=0; col<5; ++col){ + SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col))); + SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col), Qt::ForegroundRole), Qt::ForegroundRole); + } + } + // Remove old rows + SearchListModel->removeRows(0, nbRows_old); +} + +// Save columns width in a file to remember them +// (download list) +void SearchTab::saveColWidthSearchList() const{ + qDebug("Saving columns width in search list"); + QSettings settings("qBittorrent", "qBittorrent"); + QStringList width_list; + for(int i=0; icolumnCount(); ++i){ + width_list << misc::toQString(resultsBrowser->columnWidth(i)); + } + settings.setValue("SearchListColsWidth", width_list.join(" ")); + qDebug("Search list columns width saved"); +} + +// Load columns width in a file that were saved previously +// (search list) +bool SearchTab::loadColWidthSearchList(){ + qDebug("Loading columns width for search list"); + QSettings settings("qBittorrent", "qBittorrent"); + QString line = settings.value("SearchListColsWidth", QString()).toString(); + if(line.isEmpty()) + return false; + QStringList width_list = line.split(' '); + if(width_list.size() != SearchListModel->columnCount()) + return false; + for(int i=0; iheader()->resizeSection(i, width_list.at(i).toInt()); + } + qDebug("Search list columns width loaded"); + return true; +} \ No newline at end of file diff --git a/src/SearchTab.h b/src/SearchTab.h new file mode 100644 index 000000000..6ffd977fd --- /dev/null +++ b/src/SearchTab.h @@ -0,0 +1,59 @@ +/* + * Bittorrent Client using Qt4 and libtorrent. + * Copyright (C) 2006 Christophe Dumez + * + * 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. + * + * Contact : chris@qbittorrent.org + */ + +#ifndef SEARCH_TAB_H +#define SEARCH_TAB_H + +#include "ui_search.h" + +class SearchListDelegate; +class SearchEngine; +class QTreeView; +class QStandardItemModel; + +class SearchTab : public QWidget, public Ui::search_engine +{ + Q_OBJECT + + private: + QVBoxLayout *box; + QLabel *results_lbl; + QTreeView *resultsBrowser; + QStandardItemModel *SearchListModel; + SearchListDelegate *SearchDelegate; + public: + static QList *all_tab; // To store all tabs + SearchTab(QString &title,QTabWidget *tab_barWidget, SearchEngine *searchEngi); + ~SearchTab(); + bool loadColWidthSearchList(); + QLabel * getCurrentLabel(); + QStandardItemModel * getCurrentSearchListModel(); + QTreeView * getCurrentTreeView(); + void setRowColor(int row, QString color); + protected slots: + void sortSearchList(int index); + void sortSearchListInt(int index, Qt::SortOrder sortOrder); + void sortSearchListString(int index, Qt::SortOrder sortOrder); + void saveColWidthSearchList() const; + +}; + +#endif \ No newline at end of file diff --git a/src/searchEngine.cpp b/src/searchEngine.cpp index 86c8f8152..8adc19cae 100644 --- a/src/searchEngine.cpp +++ b/src/searchEngine.cpp @@ -29,178 +29,16 @@ #include #include #include +#include -#include "SearchListDelegate.h" #include "searchEngine.h" #include "bittorrent.h" #include "downloadThread.h" - -#define SEARCH_NAME 0 -#define SEARCH_SIZE 1 -#define SEARCH_SEEDERS 2 -#define SEARCH_LEECHERS 3 -#define SEARCH_ENGINE 4 +#include "misc.h" +#include "SearchListDelegate.h" #define SEARCHHISTORY_MAXSIZE 50 -/*TAB SYSTEM TO MOVE IN ANOTHER FILE*/ -QList* TabbedSearch::all_tab = new QList(); -TabbedSearch::TabbedSearch(QString &title,QTabWidget *tab_barWidget,SearchEngine *searchEngi) : QWidget() -{ - box=new QVBoxLayout(); - results_lbl=new QLabel(); - resultsBrowser = new QTreeView(); - box->addWidget(results_lbl); - box->addWidget(resultsBrowser); - - setLayout(box); - // Set Search results list model - SearchListModel = new QStandardItemModel(0,5); - SearchListModel->setHeaderData(SEARCH_NAME, Qt::Horizontal, tr("Name", "i.e: file name")); - SearchListModel->setHeaderData(SEARCH_SIZE, Qt::Horizontal, tr("Size", "i.e: file size")); - SearchListModel->setHeaderData(SEARCH_SEEDERS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources")); - SearchListModel->setHeaderData(SEARCH_LEECHERS, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources")); - SearchListModel->setHeaderData(SEARCH_ENGINE, Qt::Horizontal, tr("Search engine")); - resultsBrowser->setModel(SearchListModel); - SearchDelegate = new SearchListDelegate(); - resultsBrowser->setItemDelegate(SearchDelegate); - // Make search list header clickable for sorting - resultsBrowser->header()->setClickable(true); - resultsBrowser->header()->setSortIndicatorShown(true); - - // Connect signals to slots (search part) - connect(resultsBrowser, SIGNAL(doubleClicked(const QModelIndex&)), searchEngi, SLOT(downloadSelectedItem(const QModelIndex&))); - connect(resultsBrowser->header(), SIGNAL(sectionPressed(int)), this, SLOT(sortSearchList(int))); - - // Load last columns width for search results list - if(!loadColWidthSearchList()){ - resultsBrowser->header()->resizeSection(0, 275); - } - tab_barWidget->addTab(this, title); - all_tab->append(this); -} - -TabbedSearch::~TabbedSearch() -{ - saveColWidthSearchList(); - delete resultsBrowser; - delete SearchListModel; - delete SearchDelegate; -} - -/*LES FONCTIONS POUR GET LA BETE*/ -QLabel* TabbedSearch::getCurrentLabel() -{ - return results_lbl; -} - -QTreeView* TabbedSearch::getCurrentTreeView() -{ - return resultsBrowser; -} - -QStandardItemModel* TabbedSearch::getCurrentSearchListModel() -{ - return SearchListModel; -} - -// Set the color of a row in data model -void TabbedSearch::setRowColor(int row, QString color){ - for(int i=0; icolumnCount(); ++i){ - SearchListModel->setData(SearchListModel->index(row, i), QVariant(QColor(color)), Qt::ForegroundRole); - } -} - -void TabbedSearch::sortSearchList(int index){ - static Qt::SortOrder sortOrder = Qt::AscendingOrder; - if(resultsBrowser->header()->sortIndicatorSection() == index){ - sortOrder = (sortOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder; ; - } - resultsBrowser->header()->setSortIndicator(index, sortOrder); - switch(index){ - //case SIZE: - case SEEDERS: - case LEECHERS: - case SIZE: - sortSearchListInt(index, sortOrder); - break; - default: - sortSearchListString(index, sortOrder); - } -} - -void TabbedSearch::sortSearchListInt(int index, Qt::SortOrder sortOrder){ - QList > lines; - // Insertion sorting - for(int i=0; irowCount(); ++i){ - misc::insertSort(lines, QPair(i, SearchListModel->data(SearchListModel->index(i, index)).toLongLong()), sortOrder); - } - // Insert items in new model, in correct order - int nbRows_old = lines.size(); - for(int row=0; rowinsertRow(SearchListModel->rowCount()); - int sourceRow = lines[row].first; - for(int col=0; col<5; ++col){ - SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col))); - SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col), Qt::ForegroundRole), Qt::ForegroundRole); - } - } - // Remove old rows - SearchListModel->removeRows(0, nbRows_old); -} - -void TabbedSearch::sortSearchListString(int index, Qt::SortOrder sortOrder){ - QList > lines; - // Insetion sorting - for(int i=0; irowCount(); ++i){ - misc::insertSortString(lines, QPair(i, SearchListModel->data(SearchListModel->index(i, index)).toString()), sortOrder); - } - // Insert items in new model, in correct order - int nbRows_old = lines.size(); - for(int row=0; rowinsertRow(SearchListModel->rowCount()); - int sourceRow = lines[row].first; - for(int col=0; col<5; ++col){ - SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col))); - SearchListModel->setData(SearchListModel->index(nbRows_old+row, col), SearchListModel->data(SearchListModel->index(sourceRow, col), Qt::ForegroundRole), Qt::ForegroundRole); - } - } - // Remove old rows - SearchListModel->removeRows(0, nbRows_old); -} - -// Save columns width in a file to remember them -// (download list) -void TabbedSearch::saveColWidthSearchList() const{ - qDebug("Saving columns width in search list"); - QSettings settings("qBittorrent", "qBittorrent"); - QStringList width_list; - for(int i=0; icolumnCount(); ++i){ - width_list << misc::toQString(resultsBrowser->columnWidth(i)); - } - settings.setValue("SearchListColsWidth", width_list.join(" ")); - qDebug("Search list columns width saved"); -} - -// Load columns width in a file that were saved previously -// (search list) -bool TabbedSearch::loadColWidthSearchList(){ - qDebug("Loading columns width for search list"); - QSettings settings("qBittorrent", "qBittorrent"); - QString line = settings.value("SearchListColsWidth", QString()).toString(); - if(line.isEmpty()) - return false; - QStringList width_list = line.split(' '); - if(width_list.size() != SearchListModel->columnCount()) - return false; - for(int i=0; iheader()->resizeSection(i, width_list.at(i).toInt()); - } - qDebug("Search list columns width loaded"); - return true; -} -/*END TAB SYSTME*/ - /*SEARCH ENGINE START*/ SearchEngine::SearchEngine(bittorrent *BTSession, QSystemTrayIcon *myTrayIcon, bool systrayIntegration) : QWidget(), BTSession(BTSession), myTrayIcon(myTrayIcon), systrayIntegration(systrayIntegration){ setupUi(this); @@ -291,7 +129,7 @@ void SearchEngine::on_search_button_clicked(){ } QString pattern = search_pattern->text().trimmed(); //AJOUT TAB obligé de passé le widget en param sinon crash - tab_search=new TabbedSearch(pattern,tabWidget,this); + tab_search=new SearchTab(pattern,tabWidget,this); // No search pattern entered if(pattern.isEmpty()){ QMessageBox::critical(0, tr("Empty search pattern"), tr("Please type a search pattern first")); @@ -345,7 +183,7 @@ void SearchEngine::searchStarted(){ void SearchEngine::downloadSelectedItem(const QModelIndex& index){ int row = index.row(); // Get Item url - QString url = searchResultsUrls.value(TabbedSearch::all_tab->at(tabWidget->currentIndex())->getCurrentSearchListModel()->data(TabbedSearch::all_tab->at(tabWidget->currentIndex())->getCurrentSearchListModel()->index(row, NAME)).toString()); + QString url = searchResultsUrls.value(SearchTab::all_tab->at(tabWidget->currentIndex())->getCurrentSearchListModel()->data(SearchTab::all_tab->at(tabWidget->currentIndex())->getCurrentSearchListModel()->index(row, NAME)).toString()); // Download from url BTSession->downloadFromUrl(url); // Set item color to RED @@ -526,7 +364,7 @@ void SearchEngine::on_clearPatternButton_clicked() { // Download selected items in search results list void SearchEngine::on_download_button_clicked(){ //QModelIndexList selectedIndexes = tab_search->getCurrentTreeView()->selectionModel()->selectedIndexes(); - QModelIndexList selectedIndexes = TabbedSearch::all_tab->at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); + QModelIndexList selectedIndexes = SearchTab::all_tab->at(tabWidget->currentIndex())->getCurrentTreeView()->selectionModel()->selectedIndexes(); QModelIndex index; foreach(index, selectedIndexes){ if(index.column() == NAME){ diff --git a/src/searchEngine.h b/src/searchEngine.h index 333a69d52..3e30e158f 100644 --- a/src/searchEngine.h +++ b/src/searchEngine.h @@ -26,43 +26,15 @@ #include #include -#include #include "ui_search.h" #include "engineSelectDlg.h" +#include "SearchTab.h" -class QStandardItemModel; -class SearchListDelegate; class bittorrent; class QSystemTrayIcon; class downloadThread; class QTimer; class SearchEngine; -class TabbedSearch : public QWidget, public Ui::search_engine -{ - Q_OBJECT - - private: - QVBoxLayout *box; - QLabel *results_lbl; - QTreeView *resultsBrowser; - QStandardItemModel *SearchListModel; - SearchListDelegate *SearchDelegate; - public: - static QList *all_tab; // To store all tabs - TabbedSearch(QString &title,QTabWidget *tab_barWidget,SearchEngine *searchEngi); - ~TabbedSearch(); - bool loadColWidthSearchList(); - QLabel * getCurrentLabel(); - QStandardItemModel * getCurrentSearchListModel(); - QTreeView * getCurrentTreeView(); - void setRowColor(int row, QString color); - protected slots: - void sortSearchList(int index); - void sortSearchListInt(int index, Qt::SortOrder sortOrder); - void sortSearchListString(int index, Qt::SortOrder sortOrder); - void saveColWidthSearchList() const; - -}; class SearchEngine : public QWidget, public Ui::search_engine{ Q_OBJECT @@ -83,7 +55,7 @@ class SearchEngine : public QWidget, public Ui::search_engine{ downloadThread *downloader; QStringList enabled_engines; QTimer *searchTimeout; - TabbedSearch *tab_search; + SearchTab *tab_search; public: SearchEngine(bittorrent *BTSession, QSystemTrayIcon *myTrayIcon, bool systrayIntegration); ~SearchEngine(); diff --git a/src/src.pro b/src/src.pro index af9433c4c..f28426f02 100644 --- a/src/src.pro +++ b/src/src.pro @@ -148,7 +148,7 @@ HEADERS += GUI.h misc.h options_imp.h about_imp.h \ httpserver.h httpconnection.h \ httprequestparser.h httpresponsegenerator.h \ json.h eventmanager.h filterParserThread.h \ - TrackersAdditionDlg.h + TrackersAdditionDlg.h searchTab.h FORMS += MainWindow.ui options.ui about.ui \ properties.ui createtorrent.ui preview.ui \ login.ui downloadFromURL.ui addTorrentDialog.ui \ @@ -176,7 +176,8 @@ SOURCES += GUI.cpp \ httprequestparser.cpp \ httpresponsegenerator.cpp \ json.cpp \ - eventmanager.cpp + eventmanager.cpp \ + SearchTab.cpp DESTDIR = .