#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"donewdialog.h"
#include <QColorDialog>
#include <QMessageBox>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->graphicsView->setFixedSize(2000,768);
// resize(700,500);
// area = new PaintArea;
// scrollArea = new QScrollArea;
//// scrollArea->setBackgroundRole(QPalette::Dark); //scrollArea对象的背景色设为Dark
// scrollArea->setWidget(area); //将画布添加到scrollArea中
// scrollArea->widget()->setMinimumSize(800,600);
// scrollArea->setMouseTracking(true);
//// this->setMouseTracking(true);
// setCentralWidget(scrollArea);
// isSaved = false;
// curFile = QStringLiteral("未命名.png");
QPalette palette = ui->textBrowser_Pen->palette(); //显示这个颜色
palette.setColor(QPalette::Base,QColor(Qt::black));
ui->textBrowser_Pen->setPalette(palette);
// QPalette palette = ui->textBrowser_Brush->palette(); //显示这个颜色
palette.setColor(QPalette::Base,QColor(Qt::black));
ui->textBrowser_Brush->setPalette(palette);
}
void MainWindow::doOpen() {
if (maybeSave()){
QString fileName = QFileDialog::getOpenFileName(
this, QStringLiteral("打开文件"), QDir::currentPath());
if (!fileName.isEmpty()) {
area->openImage(fileName);
scrollArea->widget()->resize(area->getImageSize()); //获得图片的大小,然后更改scrollArea的大小
isSaved = true;
curFile = fileName;
}
}
}
bool MainWindow::maybeSave() {
if(area->isModified()) {
QMessageBox::StandardButton box;
box = QMessageBox::warning(this,
QStringLiteral("保存文件"),QStringLiteral("图片已经改变,是否保存?"),
QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
if(box == QMessageBox::Yes) {
return doFileSave();
}
else if(box == QMessageBox::Cancel) {
return false;
}
}
return true;
}
bool MainWindow::doFileSave() {
if(isSaved)
{
return saveFile(curFile);
}
else
return doFileSaveAs();
}
bool MainWindow::saveFile(QString fileName)
{
if(area->saveImage(fileName,"png")){
isSaved = true;
return true;
}
else return false;
}
bool MainWindow::doFileSaveAs(){
QString fileName = QFileDialog::getSaveFileName(
this,QStringLiteral("另存为"),curFile);
if(fileName.isEmpty()){
return false;
}
else
return saveFile(fileName);
}
void MainWindow::on_action_New_triggered() //新建菜单
{
DoNewDialog dlg;
if(dlg.exec() == QDialog::Accepted)
{
int width = dlg.getWidth();
int height = dlg.getHeight();
area->setImageSize(width,height); //更改画布大小
scrollArea->widget()->resize(width,height); //更改ScrollArea的大小
area->setImageColor(dlg.getBackColor());
}
}
void MainWindow::on_action_Save_triggered()
{
doFileSave();
}
void MainWindow::on_action_SaveAs_triggered()
{
doFileSaveAs();
}
void MainWindow::on_action_Open_triggered()
{
doOpen();
}
void MainWindow::on_action_Exit_triggered()
{
if(maybeSave())
qApp->quit();
}
void MainWindow::on_action_ZoomIn_triggered()
{
area->zoomIn();
}
void MainWindow::on_action_ZoomOut_triggered()
{
area->zoomOut();
}
void MainWindow::on_action_OverView_triggered()
{
area->overView();
}
void MainWindow::on_action_Clear_triggered()
{
QImage image= QImage(600,500,QImage::Format_RGB32);
// backColor = qRgb(255,255,255);
image.fill(area->getBackColor());
area->setImage(image);
area->repaint();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_penStyleComboBox_currentIndexChanged(const QString penStyle)
{
if(penStyle == QStringLiteral("实线"))
{
area->setPenStyle(Qt::SolidLine); //利用PaintArea类中的枚举变量
}
else if(penStyle == QStringLiteral("虚线"))
{
area->setPenStyle(Qt::DotLine);
}
}
void MainWindow::on_shapeComboBox_currentIndexChanged(const QString shape)
{
if(shape == QStringLiteral("点"))
{
area->setShape(PaintArea::Point);
}
else if(shape == QStringLiteral("线"))
{
area->setShape(PaintArea::Line);
}
else if(shape == QStringLiteral("面"))
{
area->setShape(PaintArea::Polygon);
}
}
void MainWindow::on_penWidthSpinBox_valueChanged(int penWidth)
{
area->setPenWidth(penWidth);
}
void MainWindow::on_penColorToolButton_clicked()
{
QColor newColor = QColorDialog::getColor(); //从颜色对话框获得颜色
if (newColor.isValid()) //如果得到的是可用的颜色
{
area->setPenColor(newColor);
QPalette palette = ui->textBrowser_Pen->palette(); //显示这个颜色
palette.setColor(QPalette::Base,newColor);
ui->textBrowser_Pen->setPalette(palette);
}
}
void MainWindow::on_brushColorToolButton_clicked()
{
QColor newColor = QColorDialog::getColor(); //从颜色对话框获得颜色
if (newColor.isValid()) //如果得到的是可用的颜色
{
area->setBrushColor(newColor);
QPalette palette = ui->textBrowser_Brush->palette(); //显示这个颜色
palette.setColor(QPalette::Base,newColor);
ui->textBrowser_Brush->setPalette(palette);
}
}
评论2
最新资源