Qt:创建自定义窗口部件(3)--自定义IconEditor

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39568531/article/details/80070061


#ifndef ICONEDITOR_H
#define ICONEDITOR_H

#include <QWidget>
#include"QColor"
#include"QImage"

class IconEditor : public QWidget
{
    Q_OBJECT
    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:
    IconEditor(QWidget *parent = 0);
    ~IconEditor();


public:
    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;
    QColor curColor;
    QImage image;
    int zoom;
};

#endif // ICONEDITOR_H


#include"QtGui"
#include "iconeditor.h"

IconEditor::IconEditor(QWidget *parent)
    : QWidget(parent)
{

    //设置改变窗口大小时,窗口部件的内容不发生改变。避免Qt对已显示区域重新绘制
    setAttribute(Qt::WA_StaticContents);
    setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum); //设置水平和垂直策略

    curColor =Qt::black;
    zoom = 32; //图标中的每个像素都会编程8*8的正方形
    image = QImage(16,16,QImage::Format_ARGB32);
    image.fill(qRgba(0,0,0,0));
}

IconEditor::~IconEditor()
{

}

QSize IconEditor::sizeHint()const
{
    QSize size = zoom*image.size();
    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();
        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);
    if(zoom > 3)
    {
        painter.setPen(palette().foreground().color());

        //绘制栅格画布
        for(int i =0;i <= image.height();++i)
        {
            painter.drawLine(zoom*i,0,zoom*i,zoom*image.height());
        }
        for(int j = 0;j< image.width();++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().intersect(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)
    {//鼠标右键
        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)
{
    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));
}


猜你喜欢

转载自blog.csdn.net/weixin_39568531/article/details/80070061