From 4e22109113611aa1b03c36bbd07fba28a93b8d56 Mon Sep 17 00:00:00 2001 From: orbital-mango <106028940+orbital-mango@users.noreply.github.com> Date: Tue, 6 Sep 2022 16:17:47 +0100 Subject: [PATCH] Qt: Allow piece size selection on torrent creation (#3768) --- qt/MakeDialog.cc | 46 ++++++++++++++++++++++++++++++++++------------ qt/MakeDialog.h | 3 ++- qt/MakeDialog.ui | 26 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 13 deletions(-) diff --git a/qt/MakeDialog.cc b/qt/MakeDialog.cc index 72fc879cd..dccfe561f 100644 --- a/qt/MakeDialog.cc +++ b/qt/MakeDialog.cc @@ -220,24 +220,14 @@ void MakeDialog::onSourceChanged() builder_.emplace(filename.toStdString()); } - QString text; - if (!builder_) { - text = tr("No source selected"); + updatePiecesLabel(); } else { - auto const files = tr("%Ln File(s)", nullptr, builder_->fileCount()); - auto const pieces = tr("%Ln Piece(s)", nullptr, builder_->pieceCount()); - text = tr("%1 in %2; %3 @ %4") - .arg(Formatter::get().sizeToString(builder_->totalSize())) - .arg(files) - .arg(pieces) - .arg(Formatter::get().sizeToString(static_cast(builder_->pieceSize()))); + ui_.pieceSizeSlider->setValue(log2(builder_->pieceSize())); } - - ui_.sourceSizeLabel->setText(text); } MakeDialog::MakeDialog(Session& session, QWidget* parent) @@ -266,6 +256,7 @@ MakeDialog::MakeDialog(Session& session, QWidget* parent) connect(ui_.dialogButtons, &QDialogButtonBox::accepted, this, &MakeDialog::makeTorrent); connect(ui_.dialogButtons, &QDialogButtonBox::rejected, this, &MakeDialog::close); + connect(ui_.pieceSizeSlider, &QSlider::valueChanged, this, &MakeDialog::onPieceSizeUpdated); onSourceChanged(); } @@ -303,3 +294,34 @@ void MakeDialog::dropEvent(QDropEvent* event) } } } + +void MakeDialog::updatePiecesLabel() +{ + QString text; + + if (!builder_) + { + text = tr("No source selected"); + ui_.pieceSizeSlider->setEnabled(false); + } + else + { + auto const files = tr("%Ln File(s)", nullptr, builder_->fileCount()); + auto const pieces = tr("%Ln Piece(s)", nullptr, builder_->pieceCount()); + text = tr("%1 in %2; %3 @ %4") + .arg(Formatter::get().sizeToString(builder_->totalSize())) + .arg(files) + .arg(pieces) + .arg(Formatter::get().memToString(static_cast(builder_->pieceSize()))); + ui_.pieceSizeSlider->setEnabled(true); + } + + ui_.sourceSizeLabel->setText(text); +} + +void MakeDialog::onPieceSizeUpdated(int value) +{ + auto new_size = static_cast(pow(2, value)); + builder_->setPieceSize(new_size); + updatePiecesLabel(); +} diff --git a/qt/MakeDialog.h b/qt/MakeDialog.h index 7414a2bd9..ff32f018b 100644 --- a/qt/MakeDialog.h +++ b/qt/MakeDialog.h @@ -32,10 +32,11 @@ protected: private slots: void onSourceChanged(); void makeTorrent(); + void onPieceSizeUpdated(int); private: QString getSource() const; - + void updatePiecesLabel(); Session& session_; Ui::MakeDialog ui_ = {}; diff --git a/qt/MakeDialog.ui b/qt/MakeDialog.ui index a5f233fe1..c7c5c1210 100644 --- a/qt/MakeDialog.ui +++ b/qt/MakeDialog.ui @@ -79,6 +79,32 @@ + + + + 10 + + + 26 + + + 1 + + + Qt::Horizontal + + + + + + + Piece s&ize: + + + pieceSizeSlider + + +