#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
this->initUi();
this->loadSettings();
}
MainWindow::~MainWindow()
{
this->saveSettings();
}
void MainWindow::initUi()
{
this
->setCentralWidget
(new QWidget(this));
this
->setMenuBar
(new QMenuBar(this
->centralWidget
()));
auto toolBar
= new QToolBar(this
->centralWidget
());
this->addToolBar(toolBar);
this
->setStatusBar
(new QStatusBar(this
->centralWidget
()));
_paintScene = new PaintScene(this);
layout->addWidget(_paintScene);
auto figMenu = this->menuBar()->addMenu(tr("Figures"));
auto aboutMenu = this->menuBar()->addMenu(tr("About"));
//figures
auto lineAction
= figMenu
->addAction
(QIcon(":/img/Resources/Img/line_stroked"), tr
("Draw line"));
auto rectAction
= figMenu
->addAction
(QIcon(":/img/Resources/Img/rectangle_stroked"), tr
("Draw rect"));
auto ellipseAction
= figMenu
->addAction
(QIcon(":/img/Resources/Img/ellipse_stroked"), tr
("Draw ellipse"));
auto polyAction
= figMenu
->addAction
(QIcon(":/img/Resources/Img/polyline"), tr
("Draw polyline"));
auto brushAction
= figMenu
->addAction
(QIcon(":/img/Resources/Img/paint_brush"), tr
("Free draw"));
figMenu->addSeparator();
auto undoAction
= figMenu
->addAction
(QIcon(":/img/Resources/Img/undo"), tr
("Undo"));
//about
auto aboutAction = aboutMenu->addAction(tr("About"));
lineAction->setCheckable (true);
rectAction->setCheckable (true);
ellipseAction->setCheckable (true);
polyAction->setCheckable (true);
brushAction->setCheckable (true);
figGroup->addAction(lineAction);
figGroup->addAction(rectAction);
figGroup->addAction(ellipseAction);
figGroup->addAction(polyAction);
figGroup->addAction(brushAction);
//shortcuts
lineAction->setShortcut (Qt::CTRL + Qt::Key_L);
rectAction->setShortcut (Qt::CTRL + Qt::Key_R);
ellipseAction->setShortcut (Qt::CTRL + Qt::Key_E);
polyAction->setShortcut (Qt::CTRL + Qt::Key_M);
brushAction->setShortcut (Qt::CTRL + Qt::Key_B);
undoAction->setShortcut (Qt::CTRL + Qt::Key_Z);
//status tips
lineAction->setStatusTip (tr("Draw line (Ctrl + L)"));
rectAction->setStatusTip (tr("Draw rectangle (Ctrl + R)"));
ellipseAction->setStatusTip (tr("Draw ellipse (Ctrl + E)"));
polyAction->setStatusTip (tr("Draw polyline (Ctrl + M)"));
brushAction->setStatusTip (tr("Free draw (Ctrl + B)"));
undoAction->setStatusTip (tr("Undo last action (Ctrl + Z)"));
aboutAction->setStatusTip (tr("About this program"));
//tool tips
lineAction->setToolTip (tr("Draw line (Ctrl + L)"));
rectAction->setToolTip (tr("Draw rectangle (Ctrl + R)"));
ellipseAction->setToolTip (tr("Draw ellipse (Ctrl + E)"));
polyAction->setToolTip (tr("Draw polyline (Ctrl + P)"));
brushAction->setToolTip (tr("Free draw (Ctrl + B)"));
undoAction->setToolTip (tr("Undo last action (Ctrl + Z)"));
//add actions to tool bar
toolBar->addActions(figGroup->actions());
toolBar->addSeparator();
toolBar->addAction(undoAction);
//add functions to actions
//figures
connect(lineAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::line);
});
connect(rectAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::rect);
});
connect(ellipseAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::ellipse);
});
connect(polyAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::poly);
});
connect(brushAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::brush);
});
lineAction->setChecked(true);
//for shure
_paintScene->setTool(PaintScene::Tools::line);
//undo
connect(undoAction, &QAction::triggered, [&]() mutable {
_paintScene->undo();
});
connect(aboutAction, &QAction::triggered, [&](){
auto w
= new QWidget(this, Qt
::Window);
w->setWindowModality(Qt::WindowModal);
w->setWindowIcon(this->windowIcon());
w->setWindowTitle(tr("About"));
image
->setPixmap
(QPixmap(":/img/qt"));
lay
->addWidget
(new QLabel(tr
("Made with Qt 5.7"), w
),
0, Qt
::AlignHCenter);
lay->addWidget(image);
w->show();
});
}
void MainWindow::saveSettings()
{
s.beginGroup("window");
s.setValue("geometry", this->geometry());
s.endGroup();
s.sync();
}
void MainWindow::loadSettings()
{
s.beginGroup("window");
this
->setGeometry
(s.
value("geometry",
QRect(100,
100,
500,
500)).
toRect());
s.endGroup();
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
this->initUi();
this->loadSettings();
}
MainWindow::~MainWindow()
{
this->saveSettings();
}
void MainWindow::initUi()
{
this->setCentralWidget(new QWidget(this));
this->setMenuBar(new QMenuBar(this->centralWidget()));
auto toolBar = new QToolBar(this->centralWidget());
this->addToolBar(toolBar);
this->setStatusBar(new QStatusBar(this->centralWidget()));
_paintScene = new PaintScene(this);
auto *layout = new QVBoxLayout(this->centralWidget());
layout->addWidget(_paintScene);
auto figMenu = this->menuBar()->addMenu(tr("Figures"));
auto aboutMenu = this->menuBar()->addMenu(tr("About"));
//figures
auto lineAction = figMenu->addAction(QIcon(":/img/Resources/Img/line_stroked"), tr("Draw line"));
auto rectAction = figMenu->addAction(QIcon(":/img/Resources/Img/rectangle_stroked"), tr("Draw rect"));
auto ellipseAction = figMenu->addAction(QIcon(":/img/Resources/Img/ellipse_stroked"), tr("Draw ellipse"));
auto polyAction = figMenu->addAction(QIcon(":/img/Resources/Img/polyline"), tr("Draw polyline"));
auto brushAction = figMenu->addAction(QIcon(":/img/Resources/Img/paint_brush"), tr("Free draw"));
figMenu->addSeparator();
auto undoAction = figMenu->addAction(QIcon(":/img/Resources/Img/undo"), tr("Undo"));
//about
auto aboutAction = aboutMenu->addAction(tr("About"));
lineAction->setCheckable (true);
rectAction->setCheckable (true);
ellipseAction->setCheckable (true);
polyAction->setCheckable (true);
brushAction->setCheckable (true);
auto figGroup = new QActionGroup(figMenu);
figGroup->addAction(lineAction);
figGroup->addAction(rectAction);
figGroup->addAction(ellipseAction);
figGroup->addAction(polyAction);
figGroup->addAction(brushAction);
//shortcuts
lineAction->setShortcut (Qt::CTRL + Qt::Key_L);
rectAction->setShortcut (Qt::CTRL + Qt::Key_R);
ellipseAction->setShortcut (Qt::CTRL + Qt::Key_E);
polyAction->setShortcut (Qt::CTRL + Qt::Key_M);
brushAction->setShortcut (Qt::CTRL + Qt::Key_B);
undoAction->setShortcut (Qt::CTRL + Qt::Key_Z);
//status tips
lineAction->setStatusTip (tr("Draw line (Ctrl + L)"));
rectAction->setStatusTip (tr("Draw rectangle (Ctrl + R)"));
ellipseAction->setStatusTip (tr("Draw ellipse (Ctrl + E)"));
polyAction->setStatusTip (tr("Draw polyline (Ctrl + M)"));
brushAction->setStatusTip (tr("Free draw (Ctrl + B)"));
undoAction->setStatusTip (tr("Undo last action (Ctrl + Z)"));
aboutAction->setStatusTip (tr("About this program"));
//tool tips
lineAction->setToolTip (tr("Draw line (Ctrl + L)"));
rectAction->setToolTip (tr("Draw rectangle (Ctrl + R)"));
ellipseAction->setToolTip (tr("Draw ellipse (Ctrl + E)"));
polyAction->setToolTip (tr("Draw polyline (Ctrl + P)"));
brushAction->setToolTip (tr("Free draw (Ctrl + B)"));
undoAction->setToolTip (tr("Undo last action (Ctrl + Z)"));
//add actions to tool bar
toolBar->addActions(figGroup->actions());
toolBar->addSeparator();
toolBar->addAction(undoAction);
//add functions to actions
//figures
connect(lineAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::line);
});
connect(rectAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::rect);
});
connect(ellipseAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::ellipse);
});
connect(polyAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::poly);
});
connect(brushAction, &QAction::triggered, [&](bool checked) mutable {
if(checked)
_paintScene->setTool(PaintScene::Tools::brush);
});
lineAction->setChecked(true);
//for shure
_paintScene->setTool(PaintScene::Tools::line);
//undo
connect(undoAction, &QAction::triggered, [&]() mutable {
_paintScene->undo();
});
connect(aboutAction, &QAction::triggered, [&](){
auto w = new QWidget(this, Qt::Window);
w->setWindowModality(Qt::WindowModal);
w->setWindowIcon(this->windowIcon());
w->setWindowTitle(tr("About"));
auto *lay = new QVBoxLayout(w);
auto image = new QLabel(w);
image->setPixmap(QPixmap(":/img/qt"));
lay->addWidget(new QLabel(tr("Made with Qt 5.7"), w), 0, Qt::AlignHCenter);
lay->addWidget(image);
w->show();
});
}
void MainWindow::saveSettings()
{
QSettings s(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
s.beginGroup("window");
s.setValue("geometry", this->geometry());
s.endGroup();
s.sync();
}
void MainWindow::loadSettings()
{
QSettings s(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat);
s.beginGroup("window");
this->setGeometry(s.value("geometry", QRect(100, 100, 500, 500)).toRect());
s.endGroup();
}
To copy to clipboard, switch view to plain text mode
Bookmarks