#include "MainWindow.hpp"
#include "ui_mainwindow.h"
#include "Canvas.hpp"
#include "Group.hpp"
#include "DrawDialogFactory.hpp"
#include "Circle.hpp"
#include "Rectangle.hpp"
#include "Line.hpp"
#include "SelectionTool.hpp"
#include "DrawCircleTool.hpp"
#include "DrawRectangleTool.hpp"
#include "DrawLineTool.hpp"
#include "PropertyColorButton.hpp"
#include "PropertySpinBox.hpp"
#include "GlobalDrawProperties.hpp"
#include "MainCommandStack.hpp"
#include "ActiveSelection.hpp"
#include "DrawCommand.hpp"
#include "DeleteSelectedCommand.hpp"
#include "SendBackwardCommand.hpp"
#include "BringForwardCommand.hpp"
#include "BringToFrontCommand.hpp"
#include "SendToBackCommand.hpp"
#include "FileReaderFactory.hpp"
#include "FileWriterFactory.hpp"
#include <QMessageBox>
#include <QFileDialog>
#include <QCloseEvent>
#include <QFileInfo>
#include <cassert>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_appName("Drawing App"),
m_canvasFile("Untitled"),
m_appStackIdx(-1),
m_isFileSet(false),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_canvas = new Canvas(this);
m_gp = &GlobalDrawProperties::getInstance();
m_mcs = &MainCommandStack::getInstance();
m_mcs->getCurrentIndexChangedSignal()
->connect([=](int){ mainCommandStackHasChanged(); });
m_selectionTool = std::unique_ptr<SelectionTool>
(new SelectionTool(m_canvas));
m_drawCircleTool = std::unique_ptr<DrawCircleTool>
(new DrawCircleTool(m_canvas));
m_drawRectangleTool = std::unique_ptr<DrawRectangleTool>
(new DrawRectangleTool(m_canvas));
m_drawLineTool = std::unique_ptr<DrawLineTool>
(new DrawLineTool(m_canvas));
m_canvas->setActiveTool(m_selectionTool.get());
PropertyColorButton *fillColorBtn =
new PropertyColorButton(this, getCanvas(), QColor(200, 200, 200));
PropertyColorButton *lineColorBtn =
new PropertyColorButton(this, getCanvas(), QColor(0, 0, 0));
PropertySpinBox *thicknessSpinBox =
new PropertySpinBox(this, getCanvas(), 2);
m_gp->setup(fillColorBtn, lineColorBtn, thicknessSpinBox);
ui->VEProp->addRow("Fill Color", fillColorBtn);
ui->VEProp->addRow("Line Color", lineColorBtn);
ui->VEProp->addRow("Line Thickness", thicknessSpinBox);
setCentralWidget(m_canvas);
this->setWindowTitle(getTitle());
}
Canvas *MainWindow::getCanvas()
{
return m_canvas;
}
void MainWindow::setActiveTool(Tool *tool)
{
m_activeTool = tool;
m_canvas->setActiveTool(tool);
}
bool MainWindow::isDirty() const
{
return (m_mcs->getCurrentIdx() != m_appStackIdx);
}
bool MainWindow::promptUnsavedWork()
{
if (isDirty()) {
auto answer =
QMessageBox::question(this,
"Save your current work?",
"Looks like you left a masterpiece behind. Do you want to save it?",
QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel));
if (answer == QMessageBox::Yes) {
on_actionSave_triggered();
if (isDirty()) return false;
} else if (answer == QMessageBox::Cancel) {
return false;
}
}
return true;
}
void MainWindow::saveFile()
{
QString ext = QFileInfo(m_canvasFile).suffix();
IFileWriter *writer = FileWriterFactory::create(ext.toStdString());
assert(writer != nullptr);
writer->setup(m_canvas->getMainGroup());
if (writer->write(m_canvasFile.toStdString())) {
ui->statusBar->showMessage(QString("Saved file: \"%1\"").arg(m_canvasFile));
} else {
QMessageBox::critical(this,
"File Write Error",
"I can't write to this file. Does it exist, and if so do you give me access to write to it?");
delete writer;
return;
}
setCommandStackIdx(m_mcs->getCurrentIdx());
mainCommandStackHasChanged();
delete writer;
}
void MainWindow::setCommandStackIdx(int val)
{
m_appStackIdx = val;
}
void MainWindow::mainCommandStackHasChanged()
{
QString title = getTitle();
if (isDirty()) {
title += "*";
}
this->setWindowTitle(title);
}
QString MainWindow::getTitle() const
{
return QString("%1 - %2").arg(m_appName, m_canvasFile);
}
MainWindow::~MainWindow()
{
delete ui;
delete m_canvas;
}
void MainWindow::on_actionCircle_triggered()
{
Circle *c = new Circle();
m_canvas->addVisualEntity(c);
DrawDialog *d = DrawDialogFactory::CreateDrawDialog(this, c);
d->exec();
delete d;
ActiveSelection::getInstance().deselectAll();
c->setSelected(true);
DrawCommand *comm = new DrawCommand(c);
comm->addtoCommandStack();
}
void MainWindow::on_actionRectangle_triggered()
{
Rectangle *r = new Rectangle();
m_canvas->addVisualEntity(r);
DrawDialog *d = DrawDialogFactory::CreateDrawDialog(this, r);
d->exec();
delete d;
ActiveSelection::getInstance().deselectAll();
r->setSelected(true);
DrawCommand *comm = new DrawCommand(r);
comm->addtoCommandStack();
}
void MainWindow::on_actionLine_triggered()
{
Line *l = new Line();
m_canvas->addVisualEntity(l);
DrawDialog *d = DrawDialogFactory::CreateDrawDialog(this, l);
d->exec();
delete d;
ActiveSelection::getInstance().deselectAll();
l->setSelected(true);
DrawCommand *comm = new DrawCommand(l);
comm->addtoCommandStack();
}
void MainWindow::on_actionSelectionTool_triggered()
{
uncheckAllToolbar();
ui->actionSelectionTool->setChecked(true);
setActiveTool(m_selectionTool.get());
}
void MainWindow::uncheckAllToolbar()
{
ui->actionDrawLine->setChecked(false);
ui->actionDrawCircle->setChecked(false);
ui->actionDrawRectangle->setChecked(false);
ui->actionSelectionTool->setChecked(false);
}
void MainWindow::resetCommandStack()
{
m_mcs->clear();
setCommandStackIdx(-1);
mainCommandStackHasChanged();
}
QString MainWindow::getCanvasFile() const
{
return m_canvasFile;
}
void MainWindow::setCanvasFile(const QString &value)
{
m_canvasFile = value;
m_isFileSet = true;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (!promptUnsavedWork()) {
event->ignore();
}
}
void MainWindow::on_actionDrawCircle_triggered()
{
uncheckAllToolbar();
ui->actionDrawCircle->setChecked(true);
setActiveTool(m_drawCircleTool.get());
}
void MainWindow::on_actionDrawRectangle_triggered()
{
uncheckAllToolbar();
ui->actionDrawRectangle->setChecked(true);
setActiveTool(m_drawRectangleTool.get());
}
void MainWindow::on_actionDrawLine_triggered()
{
uncheckAllToolbar();
ui->actionDrawLine->setChecked(true);
setActiveTool(m_drawLineTool.get());
}
void MainWindow::on_actionUndo_triggered()
{
m_mcs->undo();
m_canvas->repaint();
}
void MainWindow::on_actionRedo_triggered()
{
m_mcs->redo();
m_canvas->repaint();
}
void MainWindow::on_actionDelete_triggered()
{
if (ActiveSelection::getInstance().getSize() == 0) return;
DeleteSelectedCommand *dsc = new DeleteSelectedCommand();
dsc->execute();
dsc->addtoCommandStack();
m_canvas->repaint();
}
void MainWindow::on_actionSend_Backward_triggered()
{
if (!ChangeOrderCommand::canChangeOrder()) return;
SendBackwardCommand *comm = new SendBackwardCommand();
comm->execute();
comm->addtoCommandStack();
m_canvas->repaint();
}
void MainWindow::on_actionBring_Forward_triggered()
{
if (!ChangeOrderCommand::canChangeOrder()) return;
BringForwardCommand *comm = new BringForwardCommand();
comm->execute();
comm->addtoCommandStack();
m_canvas->repaint();
}
void MainWindow::on_actionBring_To_Front_triggered()
{
if (!ChangeOrderCommand::canChangeOrder()) return;
BringToFrontCommand *comm = new BringToFrontCommand();
没有合适的资源?快使用搜索试试~ 我知道了~
QT-DrawingApp图标绘制工具
共113个文件
hpp:48个
cpp:43个
png:14个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 171 浏览量
2023-08-12
13:17:00
上传
评论 1
收藏 115KB RAR 举报
温馨提示
Qt Creator 3.3.0 Qt 5.4 C++11 compliant compiler Catch Unit Test Framework (bundled)
资源推荐
资源详情
资源评论
收起资源包目录
QT-DrawingApp图标绘制工具 (113个子文件)
MainWindow.cpp 11KB
GlobalDrawProperties.cpp 6KB
DrawDialogFactory.cpp 3KB
CommandStackTest.cpp 3KB
AbstractGroup.cpp 3KB
JsonFileReader.cpp 3KB
JsonFileWriter.cpp 3KB
PropertyColorButton.cpp 2KB
SelectionTool.cpp 2KB
Line.cpp 2KB
DrawRectangleTool.cpp 2KB
Selection.cpp 2KB
Canvas.cpp 1KB
CommandStack.cpp 1KB
DrawCircleTool.cpp 1KB
Group.cpp 1KB
Circle.cpp 1KB
Rectangle.cpp 1KB
GroupTests.cpp 1KB
VisualEntity.cpp 1KB
PropertySpinBox.cpp 1KB
ChangeOrderCommand.cpp 1KB
DrawLineTool.cpp 1KB
DrawDialog.cpp 1012B
VisualEntityTests.cpp 1001B
BulkOrderCommand.cpp 950B
DeleteSelectedCommand.cpp 892B
MoveSelectionCommand.cpp 868B
ActiveSelection.cpp 815B
Tool.cpp 798B
DrawCommand.cpp 752B
Shape.cpp 706B
SelectionTests.cpp 626B
SendBackwardCommand.cpp 503B
BringForwardCommand.cpp 500B
main.cpp 347B
FileReaderFactory.cpp 226B
FileWriterFactory.cpp 226B
BringToFrontCommand.cpp 181B
SendToBackCommand.cpp 177B
MainCommandStack.cpp 147B
Command.cpp 141B
TestMain.cpp 48B
catch.hpp 328KB
MainWindow.hpp 2KB
ChangePropertyCommand.hpp 1KB
GlobalDrawProperties.hpp 1KB
PropertyColorButton.hpp 909B
AbstractGroup.hpp 864B
Group.hpp 850B
JsonFileReader.hpp 768B
Line.hpp 767B
Canvas.hpp 704B
JsonFileWriter.hpp 701B
Signal.hpp 677B
Shape.hpp 673B
SelectionTool.hpp 664B
DrawDialog.hpp 644B
VisualEntity.hpp 641B
PropertySpinBox.hpp 619B
Selection.hpp 563B
CommandStack.hpp 521B
DrawDialogFactory.hpp 503B
DrawRectangleTool.hpp 498B
DrawCircleTool.hpp 481B
DrawLineTool.hpp 468B
ChangePropertyCommandList.hpp 467B
DeleteSelectedCommand.hpp 446B
MoveSelectionCommand.hpp 430B
ActiveSelection.hpp 422B
Rectangle.hpp 405B
Tool.hpp 393B
BulkOrderCommand.hpp 383B
ITransformable.hpp 378B
MainCommandStack.hpp 376B
ChangeOrderCommand.hpp 357B
DrawCommand.hpp 328B
Circle.hpp 322B
ILine.hpp 286B
ISelectable.hpp 239B
IFileWriter.hpp 211B
IFileReader.hpp 210B
Command.hpp 208B
IDrawable.hpp 192B
IFillable.hpp 192B
BringToFrontCommand.hpp 174B
SendToBackCommand.hpp 172B
FileReaderFactory.hpp 157B
BringForwardCommand.hpp 146B
SendBackwardCommand.hpp 146B
FileWriterFactory.hpp 139B
happy-frog-example.json 18KB
alien-example.json 16KB
bringToFront.png 14KB
sendToBack.png 14KB
sendBackward.png 14KB
bringForward.png 14KB
open.png 2KB
CircleIcon.png 2KB
CursorIcon.png 2KB
共 113 条
- 1
- 2
资源评论
- <张>2023-08-26感谢大佬分享的资源给了我灵感,果断支持!感谢分享~
进击的大海贼
- 粉丝: 1w+
- 资源: 196
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功