mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-05 07:02:31 -06:00
- new torrent content selection (as a tree). Merge from the new-torrent-selection branch
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
#include "misc.h"
|
||||
#include "PropListDelegate.h"
|
||||
#include "ui_addTorrentDialog.h"
|
||||
#include "arborescence.h"
|
||||
|
||||
using namespace libtorrent;
|
||||
|
||||
@@ -55,19 +56,22 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
||||
QString from_url;
|
||||
QStandardItemModel *PropListModel;
|
||||
PropListDelegate *PropDelegate;
|
||||
unsigned int nbFiles;
|
||||
bool editParentsOnly;
|
||||
|
||||
public:
|
||||
torrentAdditionDialog(QWidget *parent) : QDialog(parent) {
|
||||
torrentAdditionDialog(QWidget *parent) : QDialog(parent), editParentsOnly(false){
|
||||
setupUi(this);
|
||||
setAttribute(Qt::WA_DeleteOnClose);
|
||||
// Set Properties list model
|
||||
PropListModel = new QStandardItemModel(0,4);
|
||||
PropListModel = new QStandardItemModel(0,5);
|
||||
PropListModel->setHeaderData(NAME, Qt::Horizontal, tr("File name"));
|
||||
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);
|
||||
torrentContentList->hideColumn(INDEX);
|
||||
PropDelegate = new PropListDelegate();
|
||||
torrentContentList->setItemDelegate(PropDelegate);
|
||||
connect(torrentContentList, SIGNAL(clicked(const QModelIndex&)), torrentContentList, SLOT(edit(const QModelIndex&)));
|
||||
@@ -100,6 +104,7 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
||||
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
|
||||
// Getting torrent file informations
|
||||
torrent_info t(e);
|
||||
nbFiles = t.num_files();
|
||||
// Setting file name
|
||||
fileName = misc::toQString(t.name());
|
||||
hash = misc::toQString(t.info_hash());
|
||||
@@ -112,15 +117,11 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
||||
}
|
||||
fileNameLbl->setText(QString::fromUtf8("<center><b>")+newFileName+QString::fromUtf8("</b></center>"));
|
||||
// List files in torrent
|
||||
unsigned int nbFiles = t.num_files();
|
||||
for(unsigned int i=0; i<nbFiles; ++i){
|
||||
unsigned int row = PropListModel->rowCount();
|
||||
PropListModel->insertRow(row);
|
||||
PropListModel->setData(PropListModel->index(row, NAME), QVariant(misc::toQString(t.file_at(i).path.leaf())));
|
||||
PropListModel->setData(PropListModel->index(row, SIZE), QVariant((qlonglong)t.file_at(i).size));
|
||||
PropListModel->setData(PropListModel->index(i, PRIORITY), QVariant(NORMAL));
|
||||
setRowColor(i, QString::fromUtf8("green"));
|
||||
}
|
||||
arborescence *arb = new arborescence(t);
|
||||
addFilesToTree(arb->getRoot(), PropListModel->invisibleRootItem());
|
||||
delete arb;
|
||||
connect(PropListModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateChildrenPriority(QStandardItem*)));
|
||||
torrentContentList->expandAll();
|
||||
}catch (invalid_torrent_file&){ // Raised by torrent_info constructor
|
||||
// Display warning to tell user we can't decode the torrent file
|
||||
if(!from_url.isNull()){
|
||||
@@ -157,7 +158,93 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
||||
show();
|
||||
}
|
||||
|
||||
void addFilesToTree(file *root, QStandardItem *parent) {
|
||||
QList<QStandardItem*> child;
|
||||
// Name
|
||||
QStandardItem *first;
|
||||
if(root->isDir()) {
|
||||
first = new QStandardItem(QIcon(":/Icons/folder.png"), root->name());
|
||||
} else {
|
||||
first = new QStandardItem(QIcon(":/Icons/file.png"), root->name());
|
||||
}
|
||||
child << first;
|
||||
// Size
|
||||
child << new QStandardItem(misc::toQString(root->getSize()));
|
||||
// Hidden progress
|
||||
child << new QStandardItem("");
|
||||
// Prio
|
||||
child << new QStandardItem(misc::toQString(NORMAL));
|
||||
// INDEX
|
||||
child << new QStandardItem(misc::toQString(root->getIndex()));
|
||||
// TODO: row Color?
|
||||
// Add the child to the tree
|
||||
parent->appendRow(child);
|
||||
// Add childs
|
||||
file *childFile;
|
||||
foreach(childFile, root->getChildren()) {
|
||||
addFilesToTree(childFile, first);
|
||||
}
|
||||
}
|
||||
|
||||
public slots:
|
||||
|
||||
void updateChildrenPriority(QStandardItem *item) {
|
||||
qDebug("Priority changed");
|
||||
QStandardItem *parent = item->parent();
|
||||
int row = item->row();
|
||||
if(!parent) {
|
||||
parent = PropListModel->invisibleRootItem();
|
||||
}
|
||||
bool is_dir = (parent->child(row, INDEX)->text().toInt() == -1);
|
||||
int priority = parent->child(row, PRIORITY)->text().toInt();
|
||||
// Update parent priority
|
||||
if(item->parent()) {
|
||||
bool parentUpdate = true;
|
||||
unsigned int rowCount = parent->rowCount();
|
||||
for(unsigned int i=0; i<rowCount; ++i) {
|
||||
if(parent->child(i, PRIORITY)->text().toInt() != priority) {
|
||||
// Check if parent priority is NORMAL
|
||||
QStandardItem *grandFather = parent->parent();
|
||||
if(!grandFather) {
|
||||
grandFather = PropListModel->invisibleRootItem();
|
||||
}
|
||||
QStandardItem *parentPrio = grandFather->child(parent->row(), PRIORITY);
|
||||
editParentsOnly = true;
|
||||
parentPrio->setText(misc::toQString(NORMAL));
|
||||
editParentsOnly = false;
|
||||
parentUpdate = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(parentUpdate) {
|
||||
QStandardItem *grandFather = parent->parent();
|
||||
if(!grandFather) {
|
||||
grandFather = PropListModel->invisibleRootItem();
|
||||
}
|
||||
QStandardItem *parentPrio = grandFather->child(parent->row(), PRIORITY);
|
||||
editParentsOnly = true;
|
||||
parentPrio->setText(misc::toQString(priority));
|
||||
editParentsOnly = false;
|
||||
}
|
||||
}
|
||||
if(editParentsOnly) return;
|
||||
if(!is_dir) return;
|
||||
// Updating children
|
||||
qDebug("Priority changed for a folder to %d", priority);
|
||||
parent = parent->child(row);
|
||||
unsigned int rowCount = parent->rowCount();
|
||||
qDebug("The folder has %d children", rowCount);
|
||||
for(unsigned int i=0; i<rowCount; ++i) {
|
||||
// get child priority
|
||||
QStandardItem *child = parent->child(i, PRIORITY);
|
||||
int child_prio = child->text().toInt();
|
||||
qDebug("Child priority is %d", child_prio);
|
||||
if(child_prio != priority) {
|
||||
child->setText(misc::toQString(priority));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void on_browseButton_clicked(){
|
||||
QString dir;
|
||||
QDir saveDir(savePathTxt->text());
|
||||
@@ -192,8 +279,7 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
||||
}
|
||||
|
||||
void displayFilesListMenu(const QPoint& pos){
|
||||
unsigned int nbRows = PropListModel->rowCount();
|
||||
if(nbRows == 1) return;
|
||||
if(nbFiles == 1) return;
|
||||
QMenu myFilesLlistMenu(this);
|
||||
QModelIndex index;
|
||||
// Enable/disable pause/start action given the DL state
|
||||
@@ -260,23 +346,37 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{
|
||||
}
|
||||
}
|
||||
|
||||
void getPriorities(QStandardItem *parent, int *priorities) {
|
||||
unsigned int nbRows = parent->rowCount();
|
||||
for(unsigned int i=0; i<nbRows; ++i){
|
||||
QStandardItem *item = parent->child(i, INDEX);
|
||||
int index = item->text().toInt();
|
||||
if(index < 0) {
|
||||
getPriorities(parent->child(i, NAME), priorities);
|
||||
} else {
|
||||
item = parent->child(i, PRIORITY);
|
||||
priorities[index] = item->text().toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void savePiecesPriorities(){
|
||||
qDebug("Saving pieces priorities");
|
||||
QFile pieces_file(misc::qBittorrentPath()+QString::fromUtf8("BT_backup")+QDir::separator()+hash+QString::fromUtf8(".priorities"));
|
||||
// First, remove old file
|
||||
pieces_file.remove();
|
||||
// Write new files
|
||||
int *priorities = new int[nbFiles];
|
||||
getPriorities(PropListModel->invisibleRootItem(), priorities);
|
||||
// Ok, we have priorities, save them
|
||||
if(!pieces_file.open(QIODevice::WriteOnly | QIODevice::Text)){
|
||||
std::cerr << "Error: Could not save pieces priorities\n";
|
||||
return;
|
||||
}
|
||||
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();
|
||||
pieces_file.write((misc::toQByteArray(priority)+misc::toQByteArray("\n")));
|
||||
for(unsigned int i=0; i<nbFiles; ++i) {
|
||||
pieces_file.write(misc::toQByteArray(priorities[i])+misc::toQByteArray("\n"));
|
||||
}
|
||||
pieces_file.close();
|
||||
delete[] priorities;
|
||||
}
|
||||
|
||||
void on_OkButton_clicked(){
|
||||
|
||||
Reference in New Issue
Block a user