From 49bc0a1d3e8c4a2e208ce6a7614bc34adb6f8a7a Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 1 Nov 2020 21:48:01 +0200 Subject: Scale bar now rescales image --- src/mainwindow.cpp | 35 +++++++++++++++++++++++++---------- src/mainwindow.h | 4 +++- src/mainwindow.ui | 21 +++++++++++++++++---- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 839b2c3..447cf0c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -13,26 +13,41 @@ MainWindow::MainWindow(const QStringList &filePaths, QWidget *parent) if (!filePaths.isEmpty()) { current.load(filePaths.first()); setWindowTitle(title_format.arg(filePaths.first())); - resizePixmap(); + fit(); } - connect(ui->original_toolButton, &QToolButton::clicked, this, - [this]() { ui->label->setPixmap(current); }); + connect(ui->ori_toolButton, &QToolButton::clicked, [this]() { scale(1.0); }); + connect(ui->fit_toolButton, &QToolButton::clicked, [this]() { fit(); }); - connect(ui->fit_toolButton, &QToolButton::clicked, this, - [this]() { resizePixmap(); }); + connect(ui->scale_slider, &QSlider::sliderMoved, + [this](int value) { scale(static_cast(value) / 100.0f); }); } MainWindow::~MainWindow() { delete ui; } void MainWindow::resizeEvent(QResizeEvent *event) { - resizePixmap(); + fit(); QMainWindow::resizeEvent(event); } -void MainWindow::resizePixmap() { - const auto size = - ui->scrollArea->viewport()->size().boundedTo(current.size()); +void MainWindow::fit() { + const auto pixmpSize = current.size(); + const auto scaleSize = + pixmpSize.scaled(ui->scrollArea->viewport()->size(), Qt::KeepAspectRatio); + + const int scale = std::min(scaleSize.width() * 100 / pixmpSize.width(), + scaleSize.height() * 100 / pixmpSize.height()); + + ui->label->setPixmap( + current.scaled(scaleSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + ui->scale_slider->setValue(scale); + ui->scale_label->setText(scale_format.arg(scale)); +} + +void MainWindow::scale(double scale) { + const auto scaleSize = current.size() * scale; ui->label->setPixmap( - current.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + current.scaled(scaleSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + ui->scale_slider->setValue(scale * 100); + ui->scale_label->setText(scale_format.arg(scale * 100)); } diff --git a/src/mainwindow.h b/src/mainwindow.h index dfd704b..f6b8c32 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -18,9 +18,11 @@ protected: void resizeEvent(QResizeEvent *event) override; private: - void resizePixmap(); + void fit(); + void scale(double scale); Ui::MainWindow *ui; QPixmap current; const QString title_format{"qimv [%1]"}; + const QString scale_format{"%1%"}; }; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 733b06e..2676638 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -15,6 +15,9 @@ + + 2 + 0 @@ -38,7 +41,7 @@ 0 0 798 - 548 + 552 @@ -87,9 +90,9 @@ - + - orig + original @@ -101,12 +104,22 @@ - + + + 100 + Qt::Horizontal + + + + 0% + + + -- cgit v1.2.1