-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
77 lines (66 loc) · 1.64 KB
/
MainWindow.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <QFileDialog>
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
_step(0)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
paint(&painter, width(), height(), _step);
}
void MainWindow::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save..."), _settings.value("lastDocument", QDir::homePath()).toString(), "*.png");
if (!fileName.isEmpty()) {
int width = 256;
int height = 256;
QFileInfo info(fileName);
for (int i = 0; i < 8; i++) {
QPixmap pixmap(width, height);
QPainter painter(&pixmap);
paint(&painter, width, height, i);
QImage image = pixmap.toImage();
QString imageFileName = QDir(info.absolutePath()).filePath(info.completeBaseName() + QString("%0.png").arg(i, 3, 10, QChar('0')));
image.save(imageFileName);
}
_settings.setValue("lastDocument", fileName);
}
}
void MainWindow::on_actionNext_triggered()
{
_step++;
this->update();
}
void MainWindow::on_actionPrevious_triggered()
{
_step--;
this->update();
}
void MainWindow::paint(QPainter *painter, int width, int height, int step)
{
painter->fillRect(this->rect(), QBrush(Qt::black));
if ( step % 2 == 0) {
painter->setBrush(QBrush(Qt::yellow));
painter->drawEllipse(QRect(0, 0, width, height));
}
QRect rect;
if ((step / 2) % 2) {
rect.setX(width / 2);
}
if ((step / 4) % 2) {
rect.setY(height / 2);
}
rect.setWidth(width / 2);
rect.setHeight(height / 2);
painter->setBrush(QBrush(Qt::red));
painter->drawRect(rect);
}