From c90d8b4458966e9908b82e8d786ff699089176c7 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 1 Nov 2020 22:57:22 +0200 Subject: Add ScaleForm widget --- meson.build | 6 ++-- src/mainwindow.cpp | 16 +++++------ src/mainwindow.h | 8 +++--- src/mainwindow.ui | 50 +------------------------------- src/scaleform.cpp | 22 ++++++++++++++ src/scaleform.h | 23 +++++++++++++++ src/scaleform.ui | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 144 insertions(+), 65 deletions(-) create mode 100644 src/scaleform.cpp create mode 100644 src/scaleform.h create mode 100644 src/scaleform.ui diff --git a/meson.build b/meson.build index 86acf5b..fde2360 100644 --- a/meson.build +++ b/meson.build @@ -22,13 +22,13 @@ mod_qt5 = import('qt5') dep_qt5 = dependency('qt5', modules: [ 'Core', 'Widgets' ], include_type: 'system') moc = mod_qt5.preprocess( - moc_headers: [ 'src/mainwindow.h' ], - ui_files: [ 'src/mainwindow.ui' ], + moc_headers: [ 'src/mainwindow.h', 'src/scaleform.h' ], + ui_files: [ 'src/mainwindow.ui', 'src/scaleform.ui' ], dependencies: dep_qt5 ) exe = executable('qimv', - sources: [ 'src/main.cpp', 'src/mainwindow.cpp', moc ], + sources: [ 'src/main.cpp', 'src/mainwindow.cpp', 'src/scaleform.cpp', moc ], dependencies: dep_qt5, install: true) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 447cf0c..eacd486 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,12 +1,17 @@ #include "mainwindow.h" +#include "scaleform.h" #include "ui_mainwindow.h" #include #include #include +ScaleForm *scaleForm = nullptr; + MainWindow::MainWindow(const QStringList &filePaths, QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { + scaleForm = new ScaleForm(this); ui->setupUi(this); + ui->menubar->setCornerWidget(scaleForm); connect(ui->actionQuit, &QAction::triggered, qApp, &QApplication::quit); @@ -16,11 +21,6 @@ MainWindow::MainWindow(const QStringList &filePaths, QWidget *parent) fit(); } - connect(ui->ori_toolButton, &QToolButton::clicked, [this]() { scale(1.0); }); - connect(ui->fit_toolButton, &QToolButton::clicked, [this]() { fit(); }); - - connect(ui->scale_slider, &QSlider::sliderMoved, - [this](int value) { scale(static_cast(value) / 100.0f); }); } MainWindow::~MainWindow() { delete ui; } @@ -40,14 +40,12 @@ void MainWindow::fit() { ui->label->setPixmap( current.scaled(scaleSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); - ui->scale_slider->setValue(scale); - ui->scale_label->setText(scale_format.arg(scale)); + scaleForm->setScale(scale); } void MainWindow::scale(double scale) { const auto scaleSize = current.size() * scale; ui->label->setPixmap( current.scaled(scaleSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); - ui->scale_slider->setValue(scale * 100); - ui->scale_label->setText(scale_format.arg(scale * 100)); + scaleForm->setScale(scale * 100); } diff --git a/src/mainwindow.h b/src/mainwindow.h index f6b8c32..5ef586b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -14,15 +14,15 @@ public: explicit MainWindow(const QStringList &filePaths, QWidget *parent = nullptr); ~MainWindow() override; +public slots: + void fit(); + void scale(double scale); + protected: void resizeEvent(QResizeEvent *event) override; private: - 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 2676638..56105ca 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -41,7 +41,7 @@ 0 0 798 - 552 + 578 @@ -74,54 +74,6 @@ - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - original - - - - - - - fit - - - - - - - 100 - - - Qt::Horizontal - - - - - - - 0% - - - - - diff --git a/src/scaleform.cpp b/src/scaleform.cpp new file mode 100644 index 0000000..e9bf740 --- /dev/null +++ b/src/scaleform.cpp @@ -0,0 +1,22 @@ +#include "scaleform.h" +#include "mainwindow.h" +#include "ui_scaleform.h" + +ScaleForm::ScaleForm(MainWindow *parent) + : QWidget(parent), ui(new Ui::ScaleForm) { + ui->setupUi(this); + + connect(ui->ori_toolButton, &QToolButton::clicked, parent, + [parent]() { parent->scale(1.0); }); + connect(ui->fit_toolButton, &QToolButton::clicked, parent, &MainWindow::fit); + connect(ui->scale_slider, &QSlider::sliderMoved, parent, [parent](int value) { + parent->scale(static_cast(value) / 100.0); + }); +} + +ScaleForm::~ScaleForm() { delete ui; } + +void ScaleForm::setScale(int scale) { + ui->scale_slider->setValue(scale); + ui->scale_label->setText(scale_format.arg(scale)); +} diff --git a/src/scaleform.h b/src/scaleform.h new file mode 100644 index 0000000..8ba8c8c --- /dev/null +++ b/src/scaleform.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +namespace Ui { +class ScaleForm; +} + +class MainWindow; +class ScaleForm final : public QWidget { + Q_OBJECT + +public: + explicit ScaleForm(MainWindow *parent = nullptr); + ~ScaleForm() override; + +public slots: + void setScale(int scale); + +private: + Ui::ScaleForm *ui; + const QString scale_format{"%1%"}; +}; diff --git a/src/scaleform.ui b/src/scaleform.ui new file mode 100644 index 0000000..886aa61 --- /dev/null +++ b/src/scaleform.ui @@ -0,0 +1,84 @@ + + + ScaleForm + + + + 0 + 0 + 345 + 22 + + + + Form + + + + 2 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + original + + + + + + + fit + + + + + + + false + + + width + + + + + + + false + + + height + + + + + + + 0% + + + + + + + Qt::Horizontal + + + + + + + + -- cgit v1.2.1