1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-03-13 05:41:17 +00:00

- Implemented files prioritizing in torrent addition dialog

This commit is contained in:
Christophe Dumez 2007-04-12 20:22:42 +00:00
parent ff3575c7ac
commit 00afd6b635
3 changed files with 56 additions and 129 deletions

View File

@ -80,31 +80,10 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QTreeWidget" name="torrentContentList" > <widget class="QTreeView" name="torrentContentList" >
<property name="contextMenuPolicy" > <property name="editTriggers" >
<enum>Qt::CustomContextMenu</enum> <set>QAbstractItemView::AllEditTriggers</set>
</property> </property>
<property name="selectionMode" >
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="indentation" >
<number>2</number>
</property>
<column>
<property name="text" >
<string>File name</string>
</property>
</column>
<column>
<property name="text" >
<string>File size</string>
</property>
</column>
<column>
<property name="text" >
<string>Selected</string>
</property>
</column>
</widget> </widget>
</item> </item>
<item> <item>
@ -172,22 +151,6 @@
</layout> </layout>
</item> </item>
</layout> </layout>
<action name="actionSelect" >
<property name="text" >
<string>Select</string>
</property>
<property name="iconText" >
<string>Select</string>
</property>
<property name="toolTip" >
<string>Select</string>
</property>
</action>
<action name="actionUnselect" >
<property name="text" >
<string>Unselect</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>

View File

@ -84,8 +84,9 @@ properties::properties(QWidget *parent, torrent_handle &h, QStringList trackerEr
std::vector<float> fp; std::vector<float> fp;
h.file_progress(fp); h.file_progress(fp);
// List files in torrent // List files in torrent
for(int i=0; i<torrentInfo.num_files(); ++i){ unsigned int nbFiles = torrentInfo.num_files();
int row = PropListModel->rowCount(); for(unsigned int i=0; i<nbFiles; ++i){
unsigned int row = PropListModel->rowCount();
PropListModel->insertRow(row); PropListModel->insertRow(row);
PropListModel->setData(PropListModel->index(row, NAME), QVariant(torrentInfo.file_at(i).path.leaf().c_str())); PropListModel->setData(PropListModel->index(row, NAME), QVariant(torrentInfo.file_at(i).path.leaf().c_str()));
PropListModel->setData(PropListModel->index(row, SIZE), QVariant((qlonglong)torrentInfo.file_at(i).size)); PropListModel->setData(PropListModel->index(row, SIZE), QVariant((qlonglong)torrentInfo.file_at(i).size));

View File

@ -29,16 +29,14 @@
#include <QMessageBox> #include <QMessageBox>
#include <QMenu> #include <QMenu>
#include <QSettings> #include <QSettings>
#include <QStandardItemModel>
#include <libtorrent/session.hpp> #include <libtorrent/session.hpp>
#include <libtorrent/bencode.hpp> #include <libtorrent/bencode.hpp>
#include "misc.h" #include "misc.h"
#include "PropListDelegate.h"
#include "ui_addTorrentDialog.h" #include "ui_addTorrentDialog.h"
#define NAME 0
#define SIZE 1
#define SELECTED 2
using namespace libtorrent; using namespace libtorrent;
class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
@ -52,18 +50,26 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
QString fileName; QString fileName;
QString fileHash; QString fileHash;
QString filePath; QString filePath;
QList<bool> selection;
bool fromScanDir; bool fromScanDir;
QString from_url; QString from_url;
QStandardItemModel *PropListModel;
PropListDelegate *PropDelegate;
public: public:
torrentAdditionDialog(QWidget *parent) : QDialog(parent) { torrentAdditionDialog(QWidget *parent) : QDialog(parent) {
setupUi(this); setupUi(this);
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
actionSelect->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/add.png"))); // Set Properties list model
actionUnselect->setIcon(QIcon(QString::fromUtf8(":/Icons/skin/remove.png"))); PropListModel = new QStandardItemModel(0,4);
connect(actionSelect, SIGNAL(triggered()), this, SLOT(selectItems())); PropListModel->setHeaderData(NAME, Qt::Horizontal, tr("File name"));
connect(actionUnselect, SIGNAL(triggered()), this, SLOT(unselectItems())); PropListModel->setHeaderData(SIZE, Qt::Horizontal, tr("Size"));
PropListModel->setHeaderData(PROGRESS, Qt::Horizontal, tr("Progress"));
PropListModel->setHeaderData(PRIORITY, Qt::Horizontal, tr("Priority"));
torrentContentList->setModel(PropListModel);
torrentContentList->hideColumn(PROGRESS);
PropDelegate = new PropListDelegate();
torrentContentList->setItemDelegate(PropDelegate);
connect(torrentContentList, SIGNAL(clicked(const QModelIndex&)), torrentContentList, SLOT(edit(const QModelIndex&)));
QString home = QDir::homePath(); QString home = QDir::homePath();
if(home[home.length()-1] != QDir::separator()){ if(home[home.length()-1] != QDir::separator()){
home += QDir::separator(); home += QDir::separator();
@ -95,14 +101,14 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
} }
fileNameLbl->setText("<center><b>"+newFileName+"</b></center>"); fileNameLbl->setText("<center><b>"+newFileName+"</b></center>");
// List files in torrent // List files in torrent
for(int i=0; i<t.num_files(); ++i){ unsigned int nbFiles = t.num_files();
QStringList line; for(unsigned int i=0; i<nbFiles; ++i){
line << QString(t.file_at(i).path.leaf().c_str()); unsigned int row = PropListModel->rowCount();
line << misc::friendlyUnit((qlonglong)t.file_at(i).size); PropListModel->insertRow(row);
selection << true; PropListModel->setData(PropListModel->index(row, NAME), QVariant(t.file_at(i).path.leaf().c_str()));
line << tr("True"); PropListModel->setData(PropListModel->index(row, SIZE), QVariant((qlonglong)t.file_at(i).size));
torrentContentList->addTopLevelItem(new QTreeWidgetItem(line)); PropListModel->setData(PropListModel->index(i, PRIORITY), QVariant(NORMAL));
setLineColor(torrentContentList->topLevelItemCount()-1, "green"); setRowColor(i, "green");
} }
}catch (invalid_torrent_file&){ // Raised by torrent_info constructor }catch (invalid_torrent_file&){ // Raised by torrent_info constructor
// Display warning to tell user we can't decode the torrent file // Display warning to tell user we can't decode the torrent file
@ -137,9 +143,6 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
} }
close(); close();
} }
//Connects
connect(torrentContentList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(toggleSelection(QTreeWidgetItem*, int)));
connect(torrentContentList, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displaySelectionMenu(const QPoint&)));
show(); show();
} }
@ -161,77 +164,32 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
close(); close();
} }
void selectItems(){ // Set the color of a row in data model
QList<QTreeWidgetItem *> selectedItems = torrentContentList->selectedItems(); void setRowColor(int row, QString color){
QTreeWidgetItem *item; for(int i=0; i<PropListModel->columnCount(); ++i){
foreach(item, selectedItems){ PropListModel->setData(PropListModel->index(row, i), QVariant(QColor(color)), Qt::TextColorRole);
int row = torrentContentList->indexOfTopLevelItem(item);
setLineColor(row, "green");
item->setText(SELECTED, tr("True"));
selection.replace(row, true);
} }
} }
void unselectItems(){ void savePiecesPriorities(){
QList<QTreeWidgetItem *> selectedItems = torrentContentList->selectedItems(); qDebug("Saving pieces priorities");
QTreeWidgetItem *item; QFile pieces_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".priorities");
foreach(item, selectedItems){
int row = torrentContentList->indexOfTopLevelItem(item);
setLineColor(row, "red");
item->setText(SELECTED, tr("False"));
selection.replace(row, false);
}
}
void toggleSelection(QTreeWidgetItem *item, int){
int row = torrentContentList->indexOfTopLevelItem(item);
if(row == -1){
return;
}
if(selection.at(row)){
setLineColor(row, "red");
item->setText(SELECTED, tr("False"));
selection.replace(row, false);
}else{
setLineColor(row, "green");
item->setText(SELECTED, tr("True"));
selection.replace(row, true);
}
}
void displaySelectionMenu(const QPoint& pos){
QMenu mySelectionMenu(this);
mySelectionMenu.addAction(actionSelect);
mySelectionMenu.addAction(actionUnselect);
mySelectionMenu.exec(mapToGlobal(pos)+QPoint(10, 150));
}
void saveFilteredFiles(){
QFile pieces_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".pieces");
// First, remove old file // First, remove old file
pieces_file.remove(); pieces_file.remove();
// Write new files // Write new files
if(!pieces_file.open(QIODevice::WriteOnly | QIODevice::Text)){ if(!pieces_file.open(QIODevice::WriteOnly | QIODevice::Text)){
std::cerr << "Error: Could not save filtered pieces\n"; std::cerr << "Error: Could not save pieces priorities\n";
return; return;
} }
for(int i=0; i<torrentContentList->topLevelItemCount(); ++i){ unsigned int nbRows = PropListModel->rowCount();
if(selection.at(i)){ for(unsigned int i=0; i<nbRows; ++i){
pieces_file.write(QByteArray("0\n")); QStandardItem *item = PropListModel->item(i, PRIORITY);
}else{ unsigned short priority = item->text().toInt();
pieces_file.write(QByteArray("1\n")); pieces_file.write(QByteArray((misc::toString(priority)+"\n").c_str()));
}
} }
pieces_file.close(); pieces_file.close();
} }
void setLineColor(int row, QString color){
QTreeWidgetItem *item = torrentContentList->topLevelItem(row);
for(int i=0; i<item->columnCount(); ++i){
item->setData(i, Qt::ForegroundRole, QVariant(QColor(color)));
}
}
void on_OkButton_clicked(){ void on_OkButton_clicked(){
QDir savePath(savePathTxt->text()); QDir savePath(savePathTxt->text());
if(savePathTxt->text().trimmed().isEmpty()){ if(savePathTxt->text().trimmed().isEmpty()){
@ -270,23 +228,28 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".paused"); QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".paused");
} }
// Check if there is at least one selected file // Check if there is at least one selected file
bool selected_file = false; if(!hasSelectedFiles()){
for(int i=0; i<torrentContentList->topLevelItemCount(); ++i){
if(selection.at(i)){
selected_file = true;
break;
}
}
if(!selected_file){
QMessageBox::critical(0, tr("Invalid file selection"), tr("You must select at least one file in the torrent")); QMessageBox::critical(0, tr("Invalid file selection"), tr("You must select at least one file in the torrent"));
return; return;
} }
// save filtered files // save filtered files
saveFilteredFiles(); savePiecesPriorities();
// Add to download list // Add to download list
emit torrentAddition(filePath, fromScanDir, from_url); emit torrentAddition(filePath, fromScanDir, from_url);
close(); close();
} }
bool hasSelectedFiles(){
unsigned int nbRows = PropListModel->rowCount();
for(unsigned int i=0; i<nbRows; ++i){
QStandardItem *item = PropListModel->item(i, PRIORITY);
unsigned short priority = item->text().toInt();
if(priority) {
return true;
}
}
return false;
}
}; };
#endif #endif