/
MainWindow.cpp
92 lines (67 loc) · 2.27 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef MAIN_WINDOW_CPP
#define MAIN_WINDOW_CPP
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QPrintDialog>
#include <QPainter>
#include "MainWindow.h"
#include "MazeWidget.h"
#include "MazePainter.h"
#include <QLabel>
#include <math.h>
MainWindow::MainWindow(QWidget *parent) : QWidget(parent){
initComponents();
}
MainWindow::~MainWindow() {
if (pMazeModel)
delete pMazeModel;
}
void MainWindow::initComponents() {
QVBoxLayout *pMainLayout = new QVBoxLayout();
QGridLayout *pControlsLayout = new QGridLayout();
QGridLayout *pSizeLayout = new QGridLayout();
pSizePicker = new QSizePicker(250, sqrt(2.));
pSizePicker->setValue(QSize(90, 75));
pSizeLayout->addWidget(pSizePicker, 0, 0, 2, 1);
pControlsLayout->addLayout(pSizeLayout, 0, 0, 2, 1);
pbtnGenerate = new QPushButton("&Generate");
pControlsLayout->addWidget(pbtnGenerate, 0, 1);
pbtnPrint = new QPushButton("&Print");
pbtnPrint->setEnabled(false);
pControlsLayout->addWidget(pbtnPrint, 1, 1);
pbtnQuit = new QPushButton("&Quit");
pControlsLayout->addWidget(pbtnQuit, 0, 2, 2, 1);
pMainLayout->addLayout(pControlsLayout);
pMazeWidget = new MazeWidget(this);
pMainLayout->addWidget(pMazeWidget);
this->setLayout(pMainLayout);
pMazeModel = new MazeModel();
pMazeWidget->setModel(pMazeModel);
connect(pbtnQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(pbtnGenerate, SIGNAL(clicked()), this, SLOT(generateMaze()));
connect(pbtnPrint, SIGNAL(clicked()), this, SLOT(showPrintDialog()));
}
void MainWindow::generateMaze() {
QSize size = pSizePicker->value();
pMazeModel->regenerate(size.width(), size.height());
pMazeWidget->repaint();
pbtnPrint->setEnabled(true);
}
void MainWindow::showPrintDialog() {
QPrinter printer;
printer.setDocName("Maze");
QPrintDialog dialog(&printer);
if (dialog.exec() == QDialog::Accepted)
print(printer);
}
void MainWindow::print(QPrinter &printer) {
printer.setOrientation(QPrinter::Landscape);
QPainter painter;
painter.begin(&printer);
painter.setPen(QPen(QBrush(QColor(0, 0, 0)), 1.5, Qt::SolidLine, Qt::FlatCap));
MazePainter::paint(pMazeModel, painter);
painter.end();
}
#endif