#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
statusBar = new QStatusBar(this);
setStatusBar(statusBar);
statusBar->showMessage("");
setWindowTitle(tr("Babaika"));
widthDialog = new QDialog(this);
connect(ui->widthAction, SIGNAL(triggered()), this, SLOT(tuneWidth()));
widthLineEdit = new QLineEdit(widthDialog);
QVBoxLayout *layout = new QVBoxLayout(widthDialog);
QPushButton *okButton = new QPushButton(tr("Ок"), widthDialog);
connect(okButton, SIGNAL(clicked()), this, SLOT(okButton_clicked()));
connect(okButton, SIGNAL(clicked()), widthDialog, SLOT(close()));
widthDialog->setWindowTitle("Толщина");
layout->addWidget(widthLineEdit);
layout->addWidget(okButton);
connect(ui->lineColorAction, &QAction::triggered, this, &This::colorDialog);
connect(ui->fillColorAction, &QAction::triggered, this, &This::colorDialog);
connect(ui->lineAction, &QAction::triggered, this, &This::createLine);
connect(ui->ellipseAction, &QAction::triggered, this, &This::createEllipse);
connect(ui->rectangleAction, &QAction::triggered, this, &This::createRectangle);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::paintEvent(QPaintEvent *){
QPainter painter(this);
QPen pen(Qt::black, penWidth, Qt::SolidLine, Qt::RoundCap);
pen.setColor(color);
painter.setPen(pen);
if (shapeToDraw == Shape::LINE){
createLine();
painter.drawLine(firstX, firstY, secondX, secondY);
}
else if (shapeToDraw == Shape::ELLIPSE){
createEllipse();
painter.drawEllipse(firstX, firstY, secondX - firstY, secondY - firstY);
}
else if(shapeToDraw == Shape::RECTANGLE){
createRectangle();
painter.drawRect(firstX, firstY, secondX - firstX, secondY - firstY);
}
painter.end();
}
void MainWindow::createRectangle(){
shapeToDraw = Shape::RECTANGLE;
update();
}
void MainWindow::createEllipse(){
shapeToDraw = Shape::ELLIPSE;
update();
}
void MainWindow::createLine(){
shapeToDraw = Shape::LINE;
update();
}
void MainWindow::colorDialog(){
color = QColorDialog::getColor(Qt::white, this, "Выберите цвет");
}
void MainWindow::okButton_clicked(){
bool ok;
QString strPenWidth = widthLineEdit->text();
penWidth = strPenWidth.toInt(&ok, 10);
}
void MainWindow::tuneWidth(){
widthDialog->show();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
int x = event->x();
int y = event->y();
secondX = event->x();
secondY = event->y();
QString coords = QString("x: %1, y: %2").arg(x).arg(y);
statusBar->showMessage(coords);
}
void MainWindow::mousePressEvent(QMouseEvent *event){
firstX = event->x();
secondX = firstX;
firstY = event->y();
secondY = firstY;
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event){
secondX = event->x();
secondY = event->y();
} When the paint event occurs, the update region has normally been erased, so you are painting on the widget's background