加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ehome_img_btn.cpp 7.29 KB
一键复制 编辑 原始数据 按行查看 历史
编号001 提交于 2024-11-03 21:21 . init
#include <QPainter>
#include <QFileInfo>
#include <QDebug>
#include "ehome_resimg.h"
#include "ehome_img_btn.h"
ehome_img_btn::ehome_img_btn(QWidget *parent) : QPushButton(parent)
{
setupUi(this);
m_type = eit_circle;
m_mtasktype = emt_wideedge;
m_resimg = new ehome_resimg();
m_needreq = true;
m_needtimer = true;
// m_menubtn = new ehome_basemenu(this);
// m_actiondelete = new QAction(m_menubtn);
// m_actionreedit = new QAction(m_menubtn);
// m_actiondelete->setText("删除");
// m_actionreedit->setText("重修");
// m_actiondelete->setIcon(QIcon(":/images/error.png"));
// m_actionreedit->setIcon(QIcon(":/images/setting.png"));
// m_menubtn->addAction(m_actiondelete);
// m_menubtn->addAction(m_actionreedit);
// m_actiondelete->setVisible(false);
// m_actionreedit->setVisible(false);
connect(m_resimg,SIGNAL(sign_flesh()),this,SLOT(slot_fleshicon()));
}
void ehome_img_btn::setResimg(ehome_resimg *resimg)
{
m_resimg = resimg;
}
void ehome_img_btn::setnobuffer()
{
m_resimg->setIsbuffer(false);
}
ehome_img_btn::~ehome_img_btn()
{
// Q_ASSERT(m_headericon != Q_NULLPTR);
}
void ehome_img_btn::setMtasktype(const ehome_mtasktype &mtasktype)
{
m_mtasktype = mtasktype;
}
void ehome_img_btn::setType(const ehome_icontype &type)
{
//
m_type = type;
}
void ehome_img_btn::setPath(const QString &path)
{
m_path = path;
m_resimg->setProperty("id",this->property("id").toInt());
m_resimg->setProperty("imageName",this->property("imageName").toString());
m_resimg->setProperty("imagedir",this->property("imagedir").toString());
m_resimg->getimage(m_path);
}
void ehome_img_btn::setPath(const QString &path, const QString uid)
{
m_path = path;
m_resimg->getimage(m_path,uid);
}
void ehome_img_btn::setUid(const QString &uid)
{
}
void ehome_img_btn::setlocalPath(const QString &path)
{
m_resimg->setlocalpath(path);
m_path = m_resimg->m_imagepath;
}
QImage ehome_img_btn::getRoundQImage(const QImage& img,QSize size)
{
if (img.isNull())
{
return QImage();
}
QImage resultImage(size,QImage::Format_ARGB32_Premultiplied);
QImage mask;
switch (m_mtasktype)
{
case emt_inner:
mask.load(":/ehome_oimage/ehome_inmtask.png");
break;
case emt_wideedge:
mask.load(":/ehome_oimage/ehome_mtask.png");
break;
case emt_thinedge:
mask.load(":/ehome_oimage/ehome_mtask_big.png");
break;
}
QPainter painter(&resultImage);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(resultImage.rect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.drawImage(0, 0, mask.scaled(size,Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
painter.setCompositionMode(QPainter::CompositionMode_SourceOut);
painter.drawImage(0, 0, img.scaled(size,Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
painter.end();
return resultImage;
}
void ehome_img_btn::paintEvent(QPaintEvent *ev)
{
Q_UNUSED(ev);
if(this->isChecked())
{
m_mtasktype = emt_wideedge;
}
else
{
m_mtasktype = emt_inner;
}
QPainter painter;
painter.begin(this);
QPen pen;
pen.setColor(Qt::red);
pen.setWidthF(1);
//绘制圆角
painter.setPen(pen);
QRect crect = QRect(0,0,this->width(),this->height());//设置第一个长方形的位置
painter.setRenderHint(QPainter::Antialiasing, true);
if (QFileInfo::exists(m_resimg->m_imagepath))
{
// QPixmap pixmap = QPixmap(m_resimg->m_imagepath);
// m_fitpixmap = QPixmap(m_resimg->m_imagepath);
if (m_fitpixmap.isNull())
{
m_fitpixmap.load(":/ehome_oimage/log_null.png");
}
QPixmap fitpixmap = m_fitpixmap.scaled(this->width(),this->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
switch (m_type)
{
case eit_circle:
{
QImage myimage = getRoundQImage(QImage(m_resimg->m_imagepath),QSize(this->width(),this->height()));
if (myimage.isNull())
{
return;
}
switch (m_mtasktype)
{
case emt_inner:
painter.drawImage(0,0,myimage);
break;
case emt_wideedge:
painter.drawImage(0,0,QImage(":/ehome_oimage/ehome_circle.png")
.scaled(this->width(),this->height(),Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
painter.drawImage(0,0,myimage);
break;
case emt_thinedge:
painter.drawImage(0,0,QImage(":/ehome_oimage/ehome_mcircle.png")
.scaled(this->width(),this->height(),Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
painter.drawImage(0,0,myimage);
break;
}
break;
}
case eit_rect:
painter.drawPixmap(crect,fitpixmap);
break;
}
}
else
{
QString filepath= ":/ehome_oimage/log_null.png";
QPixmap pixmap = QPixmap(filepath);
QPixmap fitpixmap = pixmap.scaled(this->width(),this->height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
switch (m_type)
{
case eit_circle:
{
QImage myimage = getRoundQImage(QImage(filepath),QSize(this->width(),this->height()));
if (myimage.isNull())
{
return;
}
switch (m_mtasktype)
{
case emt_inner:
painter.drawImage(0,0,myimage);
break;
case emt_wideedge:
painter.drawImage(0,0,QImage(":/ehome_oimage/ehome_circle.png")
.scaled(this->width(),this->height(),Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
painter.drawImage(0,0,myimage);
break;
case emt_thinedge:
painter.drawImage(0,0,QImage(":/ehome_oimage/ehome_mcircle.png")
.scaled(this->width(),this->height(),Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
painter.drawImage(0,0,myimage);
break;
}
break;
}
case eit_rect:
painter.drawPixmap(crect,fitpixmap);
break;
}
}
painter.end();
}
bool ehome_img_btn::event(QEvent *event)
{
// if(event->type()==QEvent::ContextMenu)
// {
// m_menubtn->popup(cursor().pos());
// }
return QWidget::event(event);
}
void ehome_img_btn::enterEvent(QEvent *enter)
{
m_mtasktype = emt_wideedge;
}
void ehome_img_btn::slot_fleshicon()
{
m_fitpixmap = QPixmap(m_resimg->m_imagepath);
this->update();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化