HTYPaint/mainwindow.cpp

1104 lines
43 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ui_mainwindow.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
settings(QApplication::organizationName(), QApplication::applicationName())
{
ui->setupUi(this);
QActionGroup *AG = new QActionGroup(this);
AG->addAction(ui->actionPencil);
AG->addAction(ui->actionLine);
AG->addAction(ui->actionArrow);
AG->addAction(ui->actionRect);
AG->addAction(ui->actionEllipse);
AG->addAction(ui->actionText);
AG->addAction(ui->actionColorPicker);
AG->addAction(ui->actionFill);
AG->addAction(ui->actionErase);
AG->addAction(ui->actionMove);
AG->addAction(ui->actionRectSelect);
setStyleSheet("QScrollArea{ background-color:#cccccc; }");
//状态栏
LSB1 = new QLabel("欢迎使用海天鹰画图!");
LSB1->setMinimumSize(500,20);
LSB1->setStyleSheet("padding:0px 3px;");
//LSB1->setFrameShape(QFrame::WinPanel);
//LSB1->setFrameShadow(QFrame::Sunken);
LSB2 = new QLabel("");
LSB2->setMinimumSize(150,20);
LSB2->setStyleSheet("padding:0px 3px;");
LSB2->setAlignment(Qt::AlignCenter);
//LSB2->setFrameShape(QFrame::WinPanel);
//LSB2->setFrameShadow(QFrame::Sunken);
ui->statusBar->addWidget(LSB1);
ui->statusBar->addWidget(LSB2);
text = "文字内容";
path = "";
//move((QApplication::desktop()->width() - width())/2, (QApplication::desktop()->height() - height())/2);
imageWidget = new ImageWidget;
connect(imageWidget, SIGNAL(statusBar1Message(QString)), LSB1, SLOT(setText(QString)));
connect(imageWidget, SIGNAL(statusBar2Message(QString)), LSB2, SLOT(setText(QString)));
//connect(imageWidget, SIGNAL(pick(QColor)), this, SLOT(setPicker(QColor)));
scrollArea = new QScrollArea;
scrollArea->setWidget(imageWidget);
scrollArea->widget()->setMinimumSize(600,500);
setCentralWidget(scrollArea);
toolButton_color_border = new QToolButton(this);
toolButton_color_border->setText("");
toolButton_color_border->setToolTip("边框");
ui->mainToolBar->addWidget(toolButton_color_border);
checkBox_color_border = new QCheckBox(this);
ui->mainToolBar->addWidget(checkBox_color_border);
toolButton_color_fill = new QToolButton(this);
toolButton_color_fill->setText("");
toolButton_color_fill->setToolTip("填充");
ui->mainToolBar->addWidget(toolButton_color_fill);
checkBox_color_fill = new QCheckBox(this);
ui->mainToolBar->addWidget(checkBox_color_fill);
spinbox_penWidth = new QSpinBox(this);
spinbox_penWidth->setSingleStep(1);
spinbox_penWidth->setRange(1,100);
//spinbox_penWidth->setValue(1);
ui->mainToolBar->addWidget(spinbox_penWidth);
font = qApp->font();
ui->actionFont->setText(font.family() + "," + QString::number(font.pointSize()));
lineEdit = new QLineEdit(text,this);
lineEdit->setFixedWidth(60);
ui->mainToolBar->addWidget(lineEdit);
imageWidget->text = text;
connect(ui->action_new, SIGNAL(triggered(bool)), imageWidget, SLOT(newfile()));
connect(ui->action_quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(ui->action_selectAll, SIGNAL(triggered(bool)), imageWidget, SLOT(selectAll()));
connect(ui->action_delete, SIGNAL(triggered(bool)), imageWidget, SLOT(delSelect()));
connect(ui->actionPencil, SIGNAL(triggered(bool)), imageWidget, SLOT(drawPoint()));
connect(ui->actionLine, SIGNAL(triggered(bool)), imageWidget, SLOT(drawLine()));
connect(ui->actionArrow, SIGNAL(triggered(bool)), imageWidget, SLOT(drawArrow()));
connect(ui->actionRect, SIGNAL(triggered(bool)), imageWidget, SLOT(drawRect()));
connect(ui->actionEllipse, SIGNAL(triggered(bool)), imageWidget, SLOT(drawEllipse()));
connect(ui->actionText, SIGNAL(triggered(bool)), imageWidget, SLOT(drawText()));
connect(ui->actionFill, SIGNAL(triggered(bool)), imageWidget, SLOT(drawFill()));
connect(ui->actionLine, SIGNAL(triggered(bool)), imageWidget, SLOT(drawLine()));
connect(ui->actionErase, SIGNAL(triggered(bool)), imageWidget, SLOT(drawErase()));
connect(ui->actionMove, SIGNAL(triggered(bool)), imageWidget, SLOT(drawMove()));
connect(ui->actionRectSelect, SIGNAL(triggered(bool)), imageWidget, SLOT(drawRectSelect()));
connect(ui->actionCutSelect, SIGNAL(triggered(bool)), this, SLOT(cutSelect()));
connect(ui->actionColorPicker, SIGNAL(triggered(bool)), imageWidget, SLOT(colorPicker()));
connect(ui->actionZoomin, SIGNAL(triggered(bool)), imageWidget, SLOT(zoomin()));
connect(ui->actionZoomout, SIGNAL(triggered(bool)), imageWidget, SLOT(zoomout()));
connect(ui->actionZoom1, SIGNAL(triggered(bool)), imageWidget, SLOT(zoom1()));
connect(ui->action_matting, SIGNAL(triggered(bool)), imageWidget, SLOT(matting()));
connect(toolButton_color_border, SIGNAL(clicked()), this, SLOT(setColorBorder()));
connect(toolButton_color_fill, SIGNAL(clicked()), this, SLOT(setColorFill()));
connect(spinbox_penWidth, SIGNAL(valueChanged(int)), this, SLOT(spinValueChange(int)));
connect(lineEdit, SIGNAL(textChanged(QString)), this, SLOT(textChange(QString)));
connect(checkBox_color_border, SIGNAL(stateChanged(int)), this, SLOT(checkBox_color_border_Changed(int)));
connect(checkBox_color_fill, SIGNAL(stateChanged(int)), this, SLOT(checkBox_color_fill_Changed(int)));
connect(new QShortcut(QKeySequence(Qt::Key_Plus), this), SIGNAL(activated()), this, SLOT(addPenWidth()));
connect(new QShortcut(QKeySequence(Qt::Key_Equal), this), SIGNAL(activated()), this, SLOT(addPenWidth()));
connect(new QShortcut(QKeySequence(Qt::Key_Minus), this), SIGNAL(activated()), this, SLOT(reducePenWidth()));
QStringList SL_args = QApplication::arguments();
qDebug() << SL_args;
if (SL_args.length() > 1) {
QString filepath = SL_args.at(1);
if (filepath.startsWith("file://")) {
QUrl url(filepath);
open(url.toLocalFile());
} else {
open(filepath);
}
}
readSettings();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
//QRect childRect = scrollArea->childrenRect();
//imageWidget->resize(childRect.size());
}
void MainWindow::closeEvent(QCloseEvent *event)
{
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
settings.setValue("DrawType", imageWidget->drawType);
settings.setValue("BoolBorder", checkBox_color_border->isChecked());
settings.setValue("BoolFill", checkBox_color_fill->isChecked());
settings.setValue("ColorBorder", imageWidget->pen.color().blue() << 16 | imageWidget->pen.color().green() << 8 | imageWidget->pen.color().red()); // color to int
settings.setValue("ColorFill", imageWidget->brush.color().blue() << 16 | imageWidget->brush.color().green() << 8 | imageWidget->brush.color().red());
settings.setValue("PenWidth", spinbox_penWidth->value());
QMainWindow::closeEvent(event);
}
void MainWindow::readSettings()
{
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("windowState").toByteArray());
imageWidget->drawType = static_cast<ImageWidget::DrawType>(settings.value("DrawType").toInt());
switch (imageWidget->drawType) {
case ImageWidget::POINT_DRAW:
ui->actionPencil->trigger();
break;
case ImageWidget::LINE_DRAW:
ui->actionLine->trigger();
break;
case ImageWidget::ARROW_DRAW:
ui->actionArrow->trigger();
break;
case ImageWidget::RECT_DRAW:
ui->actionRect->trigger();
break;
case ImageWidget::ELLIPSE_DRAW:
ui->actionEllipse->trigger();
break;
case ImageWidget::TEXT_DRAW:
ui->actionText->trigger();
break;
case ImageWidget::COLORPICKER_DRAW:
ui->actionColorPicker->trigger();
break;
case ImageWidget::FILL_DRAW:
ui->actionFill->trigger();
break;
case ImageWidget::ERASE_DRAW:
ui->actionErase->trigger();
break;
case ImageWidget::MOVE_DRAW:
ui->actionMove->trigger();
break;
case ImageWidget::SELECT_DRAW:
ui->actionRectSelect->trigger();
break;
}
bool b = settings.value("BoolBorder", true).toBool();
checkBox_color_border->setChecked(b);
imageWidget->boolBorder = b;
b = settings.value("BoolFill", false).toBool();
checkBox_color_fill->setChecked(b);
imageWidget->boolFill = b;
QColor color = intToColor(settings.value("ColorBorder", 0).toInt());
imageWidget->pen.setColor(color);
QPalette plt = toolButton_color_border->palette();
plt.setColor(QPalette::ButtonText, color);
toolButton_color_border->setPalette(plt);
color = intToColor(settings.value("ColorFill", 0).toInt());
imageWidget->brush.setColor(color);
plt = toolButton_color_fill->palette();
plt.setColor(QPalette::ButtonText, color);
toolButton_color_fill->setPalette(plt);
int w = settings.value("PenWidth", 1).toInt();
spinbox_penWidth->setValue(w);
imageWidget->pen.setWidth(w);
}
QColor MainWindow::intToColor(int intColor)
{
int red = intColor & 255;
int green = intColor >> 8 & 255;
int blue = intColor >> 16 & 255;
return QColor(red, green, blue);
}
void MainWindow::on_action_changelog_triggered()
{
QString s = "1.22\n(2022-11)\n边框和填充可以分开取色。\n\n1.21\n(2022-10)\n增加对比度调整。\n\n1.20\n(2022-05)\n关闭时保存设置,打开时读取设置。\n\n1.19\n(2021-04)\n修复缩放锯齿问题。\n\n1.18\n(2020-12)\n如果粘贴的图片比当前图片大,询问是否扩张,然后处理。\n\n1.17\n(2020-04)\n修复:上个选区残留问题。\n增加:第一次全选可以填充颜色(因路径不能清空只能填充一次,再次填充需要关闭再打开)。\n\n1.16\n(2020-03)\n增加:圆形路径裁剪。\n背景文字改成了描边文字。\n\n1.15\n(2019-11)\n增加:文字工具可以绘制矩形背景。\n\n1.14\n(2019-08)\n增加:颜色调节。\n修复:选择颜色取消时颜色变黑色。\n\n1.13\n(2019-06)\n修复:选区裁剪大小微差。删除选区操作后没有设置回到框选工具。\n优化信息内容、选区裁剪。群组工具栏Action实现互斥Checked。\n增加画线长度信息画框长、宽信息微调显示信息裁剪后滚动条回到0。\n\n1.12\n(2019-05)\n修复PNG转灰度图透明度丢失。\n\n1.11\n(2019-04)\n修改:删除选取填充,白色改成透明色。\n增加:棋盘背景。\n增加:鼠标移动定位文字。\n增加Ctrl+鼠标滚轮 改变线粗或字体大小。\n\n1.10\n(2018-12)\n增加:选区马赛克。\n\n1.9\n(2018-11)\n增加:画笔粗细快捷键,画图更加方便。\n\n1.8\n(2018-05)\n修复:删除选区有虚线框,从右键打开方式无法打开文件。\n\n1.7\n(2017-11)\n颜色透明工具,取色后在边框色、填充色显示,超出边界清空鼠标位置信息。\n增加灰色背景,凸显绘图区域。\n优化代码。\n(2017-10)\n简化工具信号简化setCursor()。\n(2017-09)\n增加箭头工具。\n增加抗锯齿。\n\n1.6\n(2017-07)\n更新日志消息窗口写不下了,改成带滚动条的文本框。\n自定义信号结合事件过滤器,把鼠标移动位置发送到主窗体信息栏。\n增加拖放打开文件。\n(2017-06)\n使用自定义信号解决子类发信息给主窗体状态栏问题致谢rekols。\n(2017-05)\n右键打开文件记忆文件路径。\n\n1.5 (2017-04)\n透明色反色不改变。\n增加取色工具,橡皮擦颜色不再固定为白色,而是填充色。\n\n1.4 (2017-03)\n支持命令行打开文件和打开方式打开文件。\n修复鼠标指针引用本地文件没引用资源文件引起启动path参数改变不能加载图标的问题。\n菜单的SIGNAL-SLOT改为on_action_triggered()\n修复PNG图片裁剪丢失透明度问题。\n新建图像为透明图像。\n\n1.3 (2017-03)\n实现选区模糊。\n加入模糊滤镜。\n\n1.2 (2017-02)\n文件名显示在窗口标题栏。\n区别保存和另存为。\n增加导入图片。\n\n1.1 (2017-01)\n新增灰度、反色。\n\n1.0 (2017-01)\n解决删除选区后画不出选框的问题。\n恢复撤销。\n增加全选。\n实现选区或剪贴板移动!\n保存时自动获取打开文件的路径。\n增加按像素、比例缩放。\n实现在属性窗口设置画布大小。\n2016-12\n增加快捷键控制选框及其边框移动。\n绘图代码从MainWindow向imageWidget迁移。\n实现水平镜像、垂直镜像。\n实现放大、缩小、原始大小。\n为了增加滚动条增加自定义imageWidget。\n状态栏显示绘图详情。\n复制选区到系统剪贴板,从系统剪贴板获取图像粘贴。\n优化颜色选择交互。\n增加撤销、重做功能有BUG。\n设为壁纸。\n画选区,剪裁选区。\n新建图片,打开图片,保存图片。\n实现画点、线、框、圆、字。";
QDialog *dialog = new QDialog;
dialog->setWindowTitle("更新历史");
dialog->setFixedSize(400,300);
QVBoxLayout *vbox = new QVBoxLayout;
QTextBrowser *textBrowser = new QTextBrowser;
textBrowser->setText(s);
textBrowser->zoomIn();
vbox->addWidget(textBrowser);
QHBoxLayout *hbox = new QHBoxLayout;
QPushButton *pushButton_confirm = new QPushButton("确定");
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
dialog->show();
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
if (dialog->exec() == QDialog::Accepted) {
dialog->close();
}
}
void MainWindow::on_action_aboutQt_triggered()
{
QMessageBox::aboutQt(nullptr, "关于 Qt");
}
void MainWindow::on_action_about_triggered()
{
QMessageBox aboutMB(QMessageBox::NoIcon, "关于", "海天鹰画图 1.22\n一款基于 Qt 的画图程序。\n作者:海天鹰\nE-mail: sonichy@163.com\n主页https://gitlink.org.cn/sonichy/HTYPaint\n参考文献:\n绘图:\nhttp://tzc.is-programmer.com/posts/534.html\nhttp://lizhigg.love.blog.163.com/blog/static/62611781201222105550184/\n滚动条http://down.51cto.com/data/233634\n灰度图http://www.cnblogs.com/xianglan/archive/2010/12/24/1915905.html\n拖放打开文件http://blog.csdn.net/rl529014/article/details/53057577\nColor与Int类型相互转换https://blog.csdn.net/snowdream86/article/details/5273757");
aboutMB.setIconPixmap(QPixmap(":/icon/HTYPaint.png"));
aboutMB.exec();
}
void MainWindow::setColorBorder()
{
QPalette plt = toolButton_color_border->palette();
QBrush brushb = plt.color(QPalette::ButtonText);
QColor color = QColorDialog::getColor(brushb.color(), this);
if (color.isValid()) {
imageWidget->pen.setColor(color);
plt.setColor(QPalette::ButtonText, color);
toolButton_color_border->setPalette(plt);
}
}
void MainWindow::setColorFill()
{
QPalette plt = toolButton_color_fill->palette();
QBrush brush = plt.color(QPalette::ButtonText);
QColor color = QColorDialog::getColor(brush.color(), this);
if (color.isValid()) {
imageWidget->brush.setColor(color);
plt.setColor(QPalette::ButtonText, color);
toolButton_color_fill->setPalette(plt);
}
}
void MainWindow::spinValueChange(int i)
{
imageWidget->pen.setWidth(i);
}
void MainWindow::textChange(QString s)
{
imageWidget->text=s;
}
void MainWindow::on_action_open_triggered()
{
if (path == "")
path = ".";
path = QFileDialog::getOpenFileName(this, "打开图片", path, "图片文件(*.jpg *.jpeg *.png *.bmp)");
if (path.length() != 0) {
open(path);
}
}
void MainWindow::open(QString filepath)
{
imageWidget->load(filepath);
filename = QFileInfo(filepath).fileName();
setWindowTitle(filename + " - 海天鹰画图");
LSB1->setText("打开 " + filepath);
path = filepath;
qDebug() << path;
qDebug() << imageWidget->size();
}
void MainWindow::on_action_import_triggered(){
QString filenamei = "";
if(path == ""){
filenamei = QFileDialog::getOpenFileName(this,"导入图片", ".", "图片文件(*.jpg *.png *.bmp)");
}else{
filenamei = QFileDialog::getOpenFileName(this,"导入图片", path, "图片文件(*.jpg *.png *.bmp)");
}
LSB1->setText("导入 " + filenamei);
if (path.length() != 0) {
imageWidget->image = imageWidget->imgtemp;
QImage imgImport(filenamei);
QApplication::clipboard()->setImage(imgImport, QClipboard::Clipboard);
imageWidget->paste();
imageWidget->imgmove = imgImport;
//imageWidget->imgtemp=imgImport;
//imageWidget->imgbuf[0]=imgImport;
}
}
void MainWindow::on_action_save_triggered(){
qDebug() << "save path=" << path;
if(path.length() != 0){
LSB1->setText("保存 " + path);
imageWidget->image = imageWidget->imgtemp;
imageWidget->save(path);
}else{
on_action_saveas_triggered();
}
}
void MainWindow::on_action_saveas_triggered()
{
if (path == "")
path = "./未命名.jpg";
path = QFileDialog::getSaveFileName(this, "保存图片", path, "图片文件(*.jpg *.png *.bmp)");
if (path.length() != 0) {
LSB1->setText("保存 " + path);
imageWidget->image = imageWidget->imgtemp;
imageWidget->save(path);
filename = QFileInfo(path).fileName();
setWindowTitle(filename + " - 海天鹰画图");
}
}
void MainWindow::on_actionFont_triggered()
{
bool ok;
font = QFontDialog::getFont(&ok, font, this, "选择字体");
if (ok) {
imageWidget->font = font;
ui->actionFont->setText(font.family() + "," + QString::number(font.pointSize()));
}
}
void MainWindow::on_action_undo_triggered()
{
imageWidget->undo();
LSB1->setText("撤销" + QString::number(imageWidget->cundo));
}
void MainWindow::on_action_redo_triggered()
{
imageWidget->redo();
LSB1->setText("重做" + QString::number(imageWidget->cundo));
}
void MainWindow::on_action_setWallpaper_triggered()
{
QString s = "gsettings set org.gnome.desktop.background picture-uri file://" + path;
QByteArray BA = s.toLocal8Bit();
qDebug() << BA.data();
system(BA.data());
}
void MainWindow::on_action_property_triggered()
{
//qDebug() << image.width() << "X" << image.height();
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("属性");
dialog->setFixedSize(150,100);
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
QLabel *label = new QLabel("宽度:");
QSpinBox *spinw = new QSpinBox;
spinw->setRange(0, 20000);
spinw->setValue(imageWidget->imgtemp.width());
hbox->addWidget(label, 0, Qt::AlignCenter);
hbox->addWidget(spinw);
vbox->addLayout(hbox);
hbox = new QHBoxLayout;
label = new QLabel("高度:");
hbox->addWidget(label, 0, Qt::AlignCenter);
QSpinBox *spinh = new QSpinBox;
spinh->setRange(0, 20000);
spinh->setValue(imageWidget->imgtemp.height());
hbox->addWidget(spinh);
vbox->addLayout(hbox);
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
vbox->addLayout(hbox);
hbox->addStretch();
dialog->setLayout(vbox);
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
if(dialog->exec() == QDialog::Accepted){
imageWidget->newSize(spinw->value(), spinh->value());
}
dialog->close();
}
void MainWindow::on_action_excude_triggered()
{
int wo = imageWidget->imgtemp.width();
int ho = imageWidget->imgtemp.height();
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("缩放");
dialog->setFixedSize(250,150);
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
QLabel *label = new QLabel("宽度:");
spinw = new QSpinBox;
spinw->setRange(1,10000);
spinw->setValue(wo);
hbox->addWidget(label,0,Qt::AlignCenter);
hbox->addWidget(spinw);
label = new QLabel("px");
hbox->addWidget(label);
spinwr = new QSpinBox;
spinwr->setRange(10,1000);
spinwr->setValue(100);
hbox->addWidget(spinwr);
label = new QLabel("%");
hbox->addWidget(label);
vbox->addLayout(hbox);
hbox = new QHBoxLayout;
label = new QLabel("高度:");
hbox->addWidget(label,0,Qt::AlignCenter);
spinh = new QSpinBox;
spinh->setRange(1,10000);
spinh->setValue(ho);
hbox->addWidget(spinh);
label = new QLabel("px");
hbox->addWidget(label);
spinhr = new QSpinBox;
spinhr->setRange(10,1000);
spinhr->setValue(100);
hbox->addWidget(spinhr);
label = new QLabel("%");
hbox->addWidget(label);
vbox->addLayout(hbox);
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
connect(spinw, SIGNAL(valueChanged(int)), this, SLOT(onSpinwChanged(int)));
connect(spinh, SIGNAL(valueChanged(int)), this, SLOT(onSpinhChanged(int)));
connect(spinwr, SIGNAL(valueChanged(int)), this, SLOT(onSpinwrChanged(int)));
connect(spinhr, SIGNAL(valueChanged(int)), this, SLOT(onSpinhrChanged(int)));
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
if(dialog->exec() == QDialog::Accepted){
imageWidget->scale(spinw->value(),spinh->value());
}
dialog->close();
}
void MainWindow::onSpinwChanged(int i)
{
disconnect(spinwr, SIGNAL(valueChanged(int)), this, SLOT(onSpinwrChanged(int)));
spinwr->setValue(i*100/imageWidget->image.width());
connect(spinwr, SIGNAL(valueChanged(int)), this, SLOT(onSpinwrChanged(int)));
}
void MainWindow::onSpinhChanged(int i)
{
disconnect(spinhr, SIGNAL(valueChanged(int)), this, SLOT(onSpinhrChanged(int)));
spinhr->setValue(i*100/imageWidget->image.height());
connect(spinhr, SIGNAL(valueChanged(int)), this, SLOT(onSpinhrChanged(int)));
}
void MainWindow::onSpinwrChanged(int i)
{
disconnect(spinw, SIGNAL(valueChanged(int)), this, SLOT(onSpinwChanged(int)));
spinw->setValue(imageWidget->image.width()*i/100);
connect(spinw, SIGNAL(valueChanged(int)), this, SLOT(onSpinwChanged(int)));
}
void MainWindow::onSpinhrChanged(int i)
{
disconnect(spinh, SIGNAL(valueChanged(int)), this, SLOT(onSpinhChanged(int)));
spinh->setValue(imageWidget->image.height()*i/100);
connect(spinh, SIGNAL(valueChanged(int)), this, SLOT(onSpinhChanged(int)));
}
void MainWindow::on_action_copy_triggered()
{
if (imageWidget->drawType == imageWidget->SELECT_DRAW) {
imageWidget->copy();
LSB1->setText("选区已复制到剪贴板");
}
}
void MainWindow::on_action_paste_triggered()
{
LSB1->setText("粘贴剪贴板...");
imageWidget->paste();
}
void MainWindow::on_actionRotateLeft_triggered()
{
imageWidget->rotate(-90);
}
void MainWindow::on_actionRotateRight_triggered()
{
imageWidget->rotate(90);
}
void MainWindow::on_actionMirrorHorizontal_triggered()
{
imageWidget->mirror(true,false);
}
void MainWindow::on_actionMirrorVertical_triggered()
{
imageWidget->mirror(false,true);
}
void MainWindow::on_action_help_triggered()
{
QString s = "<h3 align='center'>快捷键</h3>"
"<table align='center'>"
"<tr><td>Alt + ↑</td><td>选区上移</td></tr>"
"<tr><td>Alt + ↓</td><td>选区下移</td></tr>"
"<tr><td>Alt + ←</td><td>选区左移</td></tr>"
"<tr><td>Alt + →</td><td>选区右移</td></tr>"
"<tr><td>Ctrl + ↑</td><td>选区上边上移</td></tr>"
"<tr><td>Ctrl + ↓</td><td>选区上边下移</td></tr>"
"<tr><td>Ctrl + ←</td><td>选区左边左移</td></tr>"
"<tr><td>Ctrl + →</td><td>选区左边右移</td></tr>"
"<tr><td>Shift + ↑</td><td>选区下边上移</td></tr>"
"<tr><td>Shift + ↓</td><td>选区下边下移</td></tr>"
"<tr><td>Shift + ←</td><td>选区右边左移</td></tr>"
"<tr><td>Shift + →</td><td>选区右边右移</td></tr>"
"<tr><td>Ctrl + 鼠标滚轮</td><td>改变线粗或字体大小</td></tr>"
"<table>";
QDialog *dialog = new QDialog;
dialog->setWindowTitle("帮助");
dialog->setFixedSize(400,300);
QVBoxLayout *vbox = new QVBoxLayout;
QTextBrowser *textBrowser = new QTextBrowser;
textBrowser->setHtml(s);
textBrowser->zoomIn();
vbox->addWidget(textBrowser);
QHBoxLayout *hbox = new QHBoxLayout;
QPushButton *pushButton_confirm = new QPushButton("确定");
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
dialog->show();
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
if (dialog->exec() == QDialog::Accepted) {
dialog->close();
}
}
void MainWindow::checkBox_color_border_Changed(int state)
{
if (state == Qt::Checked) {
imageWidget->boolBorder = true;
} else if (state == Qt::Unchecked) {
imageWidget->boolBorder = false;
}
}
void MainWindow::checkBox_color_fill_Changed(int state)
{
if (state == Qt::Checked) {
imageWidget->boolFill = true;
} else if (state == Qt::Unchecked) {
imageWidget->boolFill = false;
}
}
void MainWindow::on_action_gray_triggered()
{
imageWidget->gray();
}
void MainWindow::on_action_invert_triggered()
{
imageWidget->invert();
}
void MainWindow::on_action_transparent_triggered()
{
imageWidget->transparent();
}
void MainWindow::on_action_blur_triggered()
{
imageWidget->blur(spinbox_penWidth->value());
}
void MainWindow::on_action_mosaic_triggered()
{
imageWidget->mosaic(spinbox_penWidth->value());
}
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
//if(e->mimeData()->hasFormat("text/uri-list")) //只能打开文本文件
e->acceptProposedAction(); //可以在这个窗口部件上拖放对象
}
void MainWindow::dropEvent(QDropEvent *e) //释放对方时,执行的操作
{
QList<QUrl> urls = e->mimeData()->urls();
if(urls.isEmpty())
return ;
QString fileName = urls.first().toLocalFile();
// foreach (QUrl u, urls) {
// qDebug() << u.toString();
// }
// qDebug() << urls.size();
if(fileName.isEmpty())
return;
open(fileName);
}
void MainWindow::setPicker(QColor color)
{
QPalette plt = toolButton_color_border->palette();
plt.setColor(QPalette::ButtonText, color);
toolButton_color_border->setPalette(plt);
plt = toolButton_color_fill->palette();
plt.setColor(QPalette::ButtonText, color);
toolButton_color_fill->setPalette(plt);
}
void MainWindow::addPenWidth()
{
spinbox_penWidth->setValue(spinbox_penWidth->value() + 1);
}
void MainWindow::reducePenWidth()
{
spinbox_penWidth->setValue(spinbox_penWidth->value() - 1);
}
void MainWindow::wheelEvent(QWheelEvent *e)
{
if (QApplication::keyboardModifiers() == Qt::ControlModifier) {
if (e->delta() > 0) {
if (imageWidget->drawType == imageWidget->TEXT_DRAW) {
font.setPointSize(font.pointSize()+1);
imageWidget->font = font;
ui->actionFont->setText(font.family() + "," + QString::number(font.pointSize()));
} else {
addPenWidth();
}
} else {
if (imageWidget->drawType == imageWidget->TEXT_DRAW) {
font.setPointSize(font.pointSize()-1);
imageWidget->font = font;
ui->actionFont->setText(font.family() + "," + QString::number(font.pointSize()));
} else {
reducePenWidth();
}
}
} else {
QWidget::wheelEvent(e);
}
}
void MainWindow::cutSelect()
{
imageWidget->cutSelect();
scrollArea->horizontalScrollBar()->setSliderPosition(0);
scrollArea->verticalScrollBar()->setSliderPosition(0);
}
void MainWindow::on_action_adjustRGB_triggered()
{
int ov = 0;
QDialog *dialog = new QDialog;
dialog->setWindowTitle("调色");
dialog->setFixedSize(400, 200);
QVBoxLayout *vbox = new QVBoxLayout;
QGridLayout *gridLayout = new QGridLayout;
// RGB
QLabel *label = new QLabel("RGB");
gridLayout->addWidget(label, 0, 0);
QLabel *label_value_RGB = new QLabel("0");
gridLayout->addWidget(label_value_RGB, 0, 1);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(-50, 50);
gridLayout->addWidget(slider, 0, 2);
QPushButton *pushButton_reset_RGB = new QPushButton;
pushButton_reset_RGB->setIcon(QIcon::fromTheme("view-refresh"));
gridLayout->addWidget(pushButton_reset_RGB, 0, 3);
// Red
label = new QLabel("R");
gridLayout->addWidget(label, 1, 0, Qt::AlignCenter);
QLabel *label_value_R = new QLabel("0");
gridLayout->addWidget(label_value_R, 1, 1);
QSlider *sliderR = new QSlider(Qt::Horizontal);
sliderR->setRange(-50, 50);
gridLayout->addWidget(sliderR, 1, 2);
QPushButton *pushButton_reset_R = new QPushButton;
pushButton_reset_R->setIcon(QIcon::fromTheme("view-refresh"));
connect(pushButton_reset_R, &QPushButton::clicked, [&](){
sliderR->setValue(0);
});
gridLayout->addWidget(pushButton_reset_R, 1, 3);
// Green
label = new QLabel("G");
gridLayout->addWidget(label, 2, 0, Qt::AlignCenter);
QLabel *label_value_G = new QLabel("0");
gridLayout->addWidget(label_value_G, 2, 1);
QSlider *sliderG = new QSlider(Qt::Horizontal);
sliderG->setRange(-50, 50);
gridLayout->addWidget(sliderG, 2, 2);
QPushButton *pushButton_reset_G = new QPushButton;
pushButton_reset_G->setIcon(QIcon::fromTheme("view-refresh"));
connect(pushButton_reset_G, &QPushButton::clicked, [&](){
sliderG->setValue(0);
});
gridLayout->addWidget(pushButton_reset_G, 2, 3);
// Blue
label = new QLabel("B");
gridLayout->addWidget(label, 3, 0, Qt::AlignCenter);
QLabel *label_value_B = new QLabel("0");
gridLayout->addWidget(label_value_B, 3, 1);
QSlider *sliderB = new QSlider(Qt::Horizontal);
sliderB->setRange(-50, 50);
gridLayout->addWidget(sliderB, 3, 2);
QPushButton *pushButton_reset_B = new QPushButton;
pushButton_reset_B->setIcon(QIcon::fromTheme("view-refresh"));
connect(pushButton_reset_B, &QPushButton::clicked, [&](){
sliderB->setValue(0);
});
gridLayout->addWidget(pushButton_reset_B, 3, 3);
vbox->addLayout(gridLayout);
connect(slider, &QSlider::actionTriggered, [&](){
ov = slider->value();
});
connect(slider, &QSlider::valueChanged, [&](int v){
label_value_RGB->setText(QString::number(v));
sliderR->setValue(sliderR->value() + v - ov);
sliderG->setValue(sliderG->value() + v - ov);
sliderB->setValue(sliderB->value() + v - ov);
});
connect(pushButton_reset_RGB, &QPushButton::clicked, [&](){
slider->setValue(0);
sliderR->setValue(0);
sliderG->setValue(0);
sliderB->setValue(0);
});
connect(sliderR, &QSlider::valueChanged, [&](int v){
label_value_R->setText(QString::number(v));
imageWidget->adjustRGB(v, sliderG->value(), sliderB->value(), false);
});
connect(sliderG, &QSlider::valueChanged, [&](int v){
label_value_G->setText(QString::number(v));
imageWidget->adjustRGB(sliderR->value(), v, sliderB->value(), false);
});
connect(sliderB, &QSlider::valueChanged, [&](int v){
label_value_B->setText(QString::number(v));
imageWidget->adjustRGB(sliderR->value(), sliderG->value(), v, false);
});
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
if (dialog->exec() == QDialog::Accepted) {
imageWidget->adjustRGB(sliderR->value(), sliderG->value(), sliderB->value(), true);
}
}
void MainWindow::on_action_brightness_triggered()
{
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("亮度");
dialog->setFixedWidth(400);
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
QLabel *label = new QLabel("0");
hbox->addWidget(label);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(-50, 50);
connect(slider, &QSlider::valueChanged, [&](int v){
label->setText(QString::number(v));
});
connect(slider, &QSlider::sliderReleased, [&](){
imageWidget->adjustBrightness(slider->value(), false);
});
hbox->addWidget(slider);
QPushButton *pushButton_reset = new QPushButton;
pushButton_reset->setIcon(QIcon::fromTheme("view-refresh"));
connect(pushButton_reset, &QPushButton::clicked, [&](){
slider->setValue(0);
label->setText("0");
imageWidget->adjustBrightness(0, false);
});
hbox->addWidget(pushButton_reset);
vbox->addLayout(hbox);
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
if (dialog->exec() == QDialog::Accepted) {
imageWidget->adjustBrightness(slider->value(), true);
} else {
imageWidget->imgtemp = imageWidget->image;
imageWidget->update();
}
}
void MainWindow::on_action_contrast_triggered()
{
QDialog *dialog = new QDialog(this);
dialog->setWindowTitle("对比度");
dialog->setFixedWidth(400);
QVBoxLayout *vbox = new QVBoxLayout;
QHBoxLayout *hbox = new QHBoxLayout;
QLabel *label = new QLabel("0");
hbox->addWidget(label);
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(-100, 100);
connect(slider, &QSlider::valueChanged, [&](int v){
label->setText(QString::number(v));
});
connect(slider, &QSlider::sliderReleased, [&](){
imageWidget->adjustContrast(slider->value(), false);
});
hbox->addWidget(slider);
QPushButton *pushButton_reset = new QPushButton;
pushButton_reset->setIcon(QIcon::fromTheme("view-refresh"));
connect(pushButton_reset, &QPushButton::clicked, [&](){
slider->setValue(0);
label->setText("0");
imageWidget->adjustContrast(0, false);
});
hbox->addWidget(pushButton_reset);
vbox->addLayout(hbox);
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
if (dialog->exec() == QDialog::Accepted) {
imageWidget->adjustContrast(slider->value(), true);
} else {
imageWidget->imgtemp = imageWidget->image;
imageWidget->update();
}
}
void MainWindow::on_action_clip_triggered()
{
QDialog *dialog = new QDialog;
dialog->setWindowTitle("裁剪");
dialog->setFixedSize(200, 200);
QVBoxLayout *vbox = new QVBoxLayout;
QGridLayout *gridLayout = new QGridLayout;
QLabel *label = new QLabel("X1");
gridLayout->addWidget(label, 0, 0);
QSpinBox *spinBox_x1 = new QSpinBox;
spinBox_x1->setSingleStep(1);
spinBox_x1->setRange(0, imageWidget->imgtemp.width());
spinBox_x1->setValue(0);
gridLayout->addWidget(spinBox_x1, 0, 1);
label = new QLabel("Y1");
gridLayout->addWidget(label, 1, 0);
QSpinBox *spinBox_y1 = new QSpinBox;
spinBox_y1->setSingleStep(1);
spinBox_y1->setRange(0, imageWidget->imgtemp.width());
spinBox_y1->setValue(0);
gridLayout->addWidget(spinBox_y1, 1, 1);
label = new QLabel("X2");
gridLayout->addWidget(label, 2, 0);
QSpinBox *spinBox_x2 = new QSpinBox;
spinBox_x2->setSingleStep(1);
spinBox_x2->setRange(0, imageWidget->imgtemp.width());
spinBox_x2->setValue(imageWidget->imgtemp.width());
gridLayout->addWidget(spinBox_x2, 2, 1);
label = new QLabel("Y2");
gridLayout->addWidget(label, 3, 0);
QSpinBox *spinBox_y2 = new QSpinBox;
spinBox_y2->setSingleStep(1);
spinBox_y2->setRange(0, imageWidget->imgtemp.height());
spinBox_y2->setValue(imageWidget->imgtemp.height());
gridLayout->addWidget(spinBox_y2, 3, 1);
vbox->addLayout(gridLayout);
connect(spinBox_x1, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clip(spinBox_x1->value(), spinBox_y1->value(), spinBox_x2->value(), spinBox_y2->value(), false);
});
connect(spinBox_y1, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clip(spinBox_x1->value(), spinBox_y1->value(), spinBox_x2->value(), spinBox_y2->value(), false);
});
connect(spinBox_x2, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clip(spinBox_x1->value(), spinBox_y1->value(), spinBox_x2->value(), spinBox_y2->value(), false);
});
connect(spinBox_y2, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clip(spinBox_x1->value(), spinBox_y1->value(), spinBox_x2->value(), spinBox_y2->value(), false);
});
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
if (dialog->exec() == QDialog::Accepted) {
imageWidget->clip(spinBox_x1->value(), spinBox_y1->value(), spinBox_x2->value(), spinBox_y2->value(), true);
} else {
imageWidget->imgtemp = imageWidget->image;
imageWidget->update();
}
}
void MainWindow::on_action_clipPath_triggered()
{
QDialog *dialog = new QDialog;
dialog->setWindowTitle("路径裁剪");
dialog->setFixedSize(200, 200);
QVBoxLayout *vbox = new QVBoxLayout;
QGridLayout *gridLayout = new QGridLayout;
QLabel *label = new QLabel("X");
gridLayout->addWidget(label, 0, 0);
QSpinBox *spinBox_x = new QSpinBox;
spinBox_x->setSingleStep(1);
spinBox_x->setRange(0, imageWidget->imgtemp.width());
spinBox_x->setValue(0);
gridLayout->addWidget(spinBox_x, 0, 1);
label = new QLabel("Y");
gridLayout->addWidget(label, 1, 0);
QSpinBox *spinBox_y = new QSpinBox;
spinBox_y->setSingleStep(1);
spinBox_y->setRange(0, imageWidget->imgtemp.width());
spinBox_y->setValue(0);
gridLayout->addWidget(spinBox_y, 1, 1);
label = new QLabel("");
gridLayout->addWidget(label, 2, 0);
QSpinBox *spinBox_width = new QSpinBox;
spinBox_width->setSingleStep(1);
spinBox_width->setRange(0, imageWidget->imgtemp.width());
spinBox_width->setValue(imageWidget->imgtemp.width());
gridLayout->addWidget(spinBox_width, 2, 1);
label = new QLabel("");
gridLayout->addWidget(label, 3, 0);
QSpinBox *spinBox_height = new QSpinBox;
spinBox_height->setSingleStep(1);
spinBox_height->setRange(0, imageWidget->imgtemp.height());
spinBox_height->setValue(imageWidget->imgtemp.height());
gridLayout->addWidget(spinBox_height, 3, 1);
vbox->addLayout(gridLayout);
connect(spinBox_x, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clipPath(spinBox_x->value(), spinBox_y->value(), spinBox_width->value(), spinBox_height->value(), false);
});
connect(spinBox_y, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clipPath(spinBox_x->value(), spinBox_y->value(), spinBox_width->value(), spinBox_height->value(), false);
});
connect(spinBox_width, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clipPath(spinBox_x->value(), spinBox_y->value(), spinBox_width->value(), spinBox_height->value(), false);
});
connect(spinBox_height, QOverload<int>::of(&QSpinBox::valueChanged), [=](){
imageWidget->clipPath(spinBox_x->value(), spinBox_y->value(), spinBox_width->value(), spinBox_height->value(), false);
});
QPushButton *pushButton_confirm = new QPushButton("确定");
QPushButton *pushButton_cancel = new QPushButton("取消");
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addStretch();
hbox->addWidget(pushButton_confirm);
hbox->addWidget(pushButton_cancel);
hbox->addStretch();
vbox->addLayout(hbox);
dialog->setLayout(vbox);
connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
if (dialog->exec() == QDialog::Accepted) {
imageWidget->clipPath(spinBox_x->value(), spinBox_y->value(), spinBox_width->value(), spinBox_height->value(), true);
} else {
imageWidget->imgtemp = imageWidget->image;
imageWidget->update();
}
}
void MainWindow::on_action_deleteBlackBorder_triggered()
{
QDialog *dialog = new QDialog;
dialog->setWindowTitle("去黑边");
dialog->setFixedSize(200, 200);
QGridLayout *gridLayout = new QGridLayout;
QSpinBox *spinBox_d = new QSpinBox;
spinBox_d->setValue(0);
spinBox_d->setRange(0, 255);
gridLayout->addWidget(spinBox_d, 0, 1);
QPushButton *pushButton_top = new QPushButton("上边");
gridLayout->addWidget(pushButton_top, 1, 1);
connect(pushButton_top, &QPushButton::clicked, [=](){
imageWidget->deleteBlackBorder(imageWidget->BorderType::BORDER_TOP, spinBox_d->value());
});
QPushButton *pushButton_left = new QPushButton("左边");
gridLayout->addWidget(pushButton_left, 2, 0);
connect(pushButton_left, &QPushButton::clicked, [=](){
imageWidget->deleteBlackBorder(imageWidget->BorderType::BORDER_LEFT, spinBox_d->value());
});
QPushButton *pushButton_right = new QPushButton("右边");
gridLayout->addWidget(pushButton_right, 2, 2);
connect(pushButton_right, &QPushButton::clicked, [=](){
imageWidget->deleteBlackBorder(imageWidget->BorderType::BORDER_RIGHT, spinBox_d->value());
});
QPushButton *pushButton_bottom = new QPushButton("下边");
gridLayout->addWidget(pushButton_bottom, 3, 1);
connect(pushButton_bottom, &QPushButton::clicked, [=](){
imageWidget->deleteBlackBorder(imageWidget->BorderType::BORDER_BOTTOM, spinBox_d->value());
});
dialog->setLayout(gridLayout);
dialog->show();
}