From 13cad7b1e472e8eba3b951700979aff6c58b4d41 Mon Sep 17 00:00:00 2001 From: leichaojian Date: Thu, 13 Nov 2014 22:36:54 +0800 Subject: [PATCH] add iconeditor --- IconEditor/IconEditor.pro | 20 +++ IconEditor/IconEditor.pro.user | 255 +++++++++++++++++++++++++++++++++ IconEditor/iconeditor.cpp | 150 +++++++++++++++++++ IconEditor/iconeditor.h | 50 +++++++ IconEditor/iconeditor.ui | 20 +++ IconEditor/main.cpp | 11 ++ 6 files changed, 506 insertions(+) create mode 100644 IconEditor/IconEditor.pro create mode 100644 IconEditor/IconEditor.pro.user create mode 100644 IconEditor/iconeditor.cpp create mode 100644 IconEditor/iconeditor.h create mode 100644 IconEditor/iconeditor.ui create mode 100644 IconEditor/main.cpp diff --git a/IconEditor/IconEditor.pro b/IconEditor/IconEditor.pro new file mode 100644 index 0000000..fe0e618 --- /dev/null +++ b/IconEditor/IconEditor.pro @@ -0,0 +1,20 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2014-11-13T20:10:26 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = IconEditor +TEMPLATE = app + + +SOURCES += main.cpp\ + iconeditor.cpp + +HEADERS += iconeditor.h + +FORMS += iconeditor.ui diff --git a/IconEditor/IconEditor.pro.user b/IconEditor/IconEditor.pro.user new file mode 100644 index 0000000..99593e7 --- /dev/null +++ b/IconEditor/IconEditor.pro.user @@ -0,0 +1,255 @@ + + + + + + EnvironmentId + {a8c18b7e-8020-49b9-a0ef-0165c784344f} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.3 MinGW 32bit + Desktop Qt 5.3 MinGW 32bit + qt.53.win32_mingw482_kit + 0 + 0 + 0 + + C:/Users/fzyz_sb/Desktop/build-IconEditor-Desktop_Qt_5_3_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + C:/Users/fzyz_sb/Desktop/build-IconEditor-Desktop_Qt_5_3_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + true + + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + 构建 + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + 清理 + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 2 + + + 0 + 部署 + + ProjectExplorer.BuildSteps.Deploy + + 1 + 在本地部署 + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + IconEditor + + Qt4ProjectManager.Qt4RunConfiguration:C:/Users/fzyz_sb/Desktop/IconEditor/IconEditor.pro + + IconEditor.pro + false + false + + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 16 + + + Version + 16 + + diff --git a/IconEditor/iconeditor.cpp b/IconEditor/iconeditor.cpp new file mode 100644 index 0000000..97496c2 --- /dev/null +++ b/IconEditor/iconeditor.cpp @@ -0,0 +1,150 @@ +#include "iconeditor.h" +#include "ui_iconeditor.h" + +IconEditor::IconEditor(QWidget *parent) : + QWidget(parent), + ui(new Ui::IconEditor) +{ + ui->setupUi(this); + //保留原窗口内容,防止重绘 + setAttribute(Qt::WA_StaticContents); + //已经设置了窗口的最小大小(窗口可拉伸) + setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); + + //画笔的颜色设置为黑色 + curColor = Qt::black; + //缩放因子设置为8,即每个像素会显示成一个8*8的正方形 + zoom = 8; + + //图标数据初始化为16*16的像素大小和32位的ARGB颜色格式 + image = QImage(16, 16, QImage::Format_ARGB32); + //类似于RGB(0,0,0)的数据格式 + image.fill(qRgba(0, 0, 0, 0)); +} + +IconEditor::~IconEditor() +{ + delete ui; +} + +QSize IconEditor::sizeHint() const +{ + //缩放因子*像素大小 = 窗口部件的理想大小 + QSize size = zoom * image.size(); + //如果缩放因子大于3,则增加一个额外的像素,用来容纳网格线 + if (zoom >= 3) { + size += QSize(1, 1); + } + return size; +} + +void IconEditor::setpenColor(const QColor &newColor) +{ + curColor = newColor; +} + +//编辑图像 +void IconEditor::seticonImage(const QImage &newImage) +{ + if (newImage != image) { + image = newImage.convertToFormat(QImage::Format_ARGB32); + //update会强制使用新的图像重绘窗口部件 + update(); + //通知窗口布局,窗口部件的大小已经改变了 + updateGeometry(); + } +} + +//设置图像的缩放因子 +void IconEditor::setzoomFactor(int newZoom) +{ + if (newZoom < 1) { + newZoom = 1; + } + if (newZoom != zoom) { + zoom = newZoom; + update(); + updateGeometry(); + } +} + +void IconEditor::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + //缩放因子大于3的情况下,重新画网格 + if (zoom >= 3) { + //窗口部件的调色板palette + painter.setPen(palette().foreground().color()); + for (int i = 0; i <= image.width(); ++i) { + painter.drawLine(zoom * i, 0, + zoom * i, zoom * image.height()); + } + for (int j = 0; j <= image.height(); ++j) { + painter.drawLine(0, zoom * j, + zoom * image.width(), zoom * j); + } + } + + for (int i = 0; i < image.width(); ++i) { + for (int j = 0; j < image.height(); ++j) { + //返回像素坐标 + QRect rect = pixelRect(i, j); + //绘制窗口颜色为白色 + if (!event->region().intersected(rect).isEmpty()) { + QColor color = QColor::fromRgba(image.pixel(i, j)); + if (color.alpha() < 255) { + painter.fillRect(rect, Qt::white); + } + painter.fillRect(rect, color); + } + } + } +} + +QRect IconEditor::pixelRect(int i, int j) const +{ + if (zoom >= 3) { + return QRect(zoom * i + 1, zoom * j + 1, zoom - 1, zoom - 1); + } else { + return QRect(zoom * i, zoom * j, zoom, zoom); + } +} +void IconEditor::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + //把鼠标选中的像素设置成当前画笔的颜色 + setImagePixel(event->pos(), true); + } else if (event->button() == Qt::RightButton) { + //通过传递false,来清空这个像素 + setImagePixel(event->pos(), false); + } +} + +void IconEditor::mouseMoveEvent(QMouseEvent *event) +{ + if (event->button() & Qt::LeftButton) { + //鼠标选中后连续的设置像素 + setImagePixel(event->pos(), true); + } else if (event->button() & Qt::RightButton) { + //鼠标选中后连续的清空像素 + setImagePixel(event->pos(), false); + } +} + +void IconEditor::setImagePixel(const QPoint &pos, bool opaque) +{ + //这里必须除以zoom,因为image.rect()是返回格子范围16*16,而pos则为像素的范围,存在缩放因子,所以扩大了zoom倍 + int i = pos.x() / zoom; + int j = pos.y() / zoom; + + if (image.rect().contains(i, j)) { + if (opaque) { + //设定一个像素 + image.setPixel(i, j, penColor().rgba()); + } else { + //清除一个像素 + image.setPixel(i, j, qRgba(0, 0, 0, 0)); + } + update(pixelRect(i, j)); + } +} diff --git a/IconEditor/iconeditor.h b/IconEditor/iconeditor.h new file mode 100644 index 0000000..9a44671 --- /dev/null +++ b/IconEditor/iconeditor.h @@ -0,0 +1,50 @@ +#ifndef ICONEDITOR_H +#define ICONEDITOR_H + +#include +#include +#include +#include +#include +#include + +namespace Ui { +class IconEditor; +} + +class IconEditor : public QWidget +{ + Q_OBJECT + //使用Q_PROPERTY声明三个自定义属性,而每个属性均有一个数据类型QColor,一个读函数penColor和一个写函数setpenColor + Q_PROPERTY(QColor penColor READ penColor WRITE setpenColor) + Q_PROPERTY(QImage iconImage READ iconImage WRITE seticonImage) + Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setzoomFactor) + +public: + explicit IconEditor(QWidget *parent = 0); + ~IconEditor(); + + void setpenColor(const QColor &newColor); + QColor penColor() const { return curColor; } + void setzoomFactor(int newZoom); + int zoomFactor() const { return zoom; } + void seticonImage(const QImage &newImage); + QImage iconImage() const { return image; } + QSize sizeHint() const; + +protected: + void mousePressEvent(QMouseEvent *event); + void mouseMoveEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event); +private: + void setImagePixel(const QPoint &pos, bool opaque); + QRect pixelRect(int i, int j) const; + + Ui::IconEditor *ui; + //三个私有变量保存着属性的值 + QColor curColor; + QImage image; + int zoom; +}; + +#endif // ICONEDITOR_H diff --git a/IconEditor/iconeditor.ui b/IconEditor/iconeditor.ui new file mode 100644 index 0000000..ebfdf74 --- /dev/null +++ b/IconEditor/iconeditor.ui @@ -0,0 +1,20 @@ + + IconEditor + + + + 0 + 0 + 400 + 300 + + + + IconEditor + + + + + + + diff --git a/IconEditor/main.cpp b/IconEditor/main.cpp new file mode 100644 index 0000000..23024c9 --- /dev/null +++ b/IconEditor/main.cpp @@ -0,0 +1,11 @@ +#include "iconeditor.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + IconEditor w; + w.show(); + + return a.exec(); +}