summaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
blob: eacd4861a947e6579132638a6e851ed7a347bb95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "mainwindow.h"
#include "scaleform.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QResizeEvent>

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);

  if (!filePaths.isEmpty()) {
    current.load(filePaths.first());
    setWindowTitle(title_format.arg(filePaths.first()));
    fit();
  }

}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::resizeEvent(QResizeEvent *event) {
  fit();
  QMainWindow::resizeEvent(event);
}

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));
  scaleForm->setScale(scale);
}

void MainWindow::scale(double scale) {
  const auto scaleSize = current.size() * scale;
  ui->label->setPixmap(
      current.scaled(scaleSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
  scaleForm->setScale(scale * 100);
}