窗口基类(二)

在这里插入图片描述


The recalculateResult() function is used to calculate amd display the result of combining the two images together with the user’s choice of composition mode.

 void ImageComposer::recalculateResult()
 {
     QPainter::CompositionMode mode = currentMode();

     QPainter painter(&resultImage);
     painter.setCompositionMode(QPainter::CompositionMode_Source);
     painter.fillRect(resultImage.rect(), Qt::transparent);
     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
     painter.drawImage(0, 0, destinationImage);
     painter.setCompositionMode(mode);
     painter.drawImage(0, 0, sourceImage);
     painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
     painter.fillRect(resultImage.rect(), Qt::white);
     painter.end();

     resultLabel->setPixmap(QPixmap::fromImage(resultImage));
 }

The addOp() function adds an item to the operatorComboBox using QComboBox’s addItem function. This function accepts a QPainter::CompositionMode, mode, and a QString, name. The rectangle is filled with Qt::Transparent and both the sourceImage and destinationImage are painted, before displaying it on resultLabel.


#pragma once

#include <QDialog>
#include"titlebar.h"

class BasicWindow : public QDialog
{
	Q_OBJECT

public:
	BasicWindow(QWidget *parent = nullptr);
	virtual ~BasicWindow();

public:
	//加载样式表
	void loadStyleSheet(const QString &sheetName);
	

	//获取圆头像
	QPixmap getRoundImage(const QPixmap &src,QPixmap &mask,QSize masksize = QSize(0,0));

private:
	void initBackGroundColor();//初始化背景

protected:
	void paintEvent(QPaintEvent*);		//绘制事件
	void mousePressEvent(QMouseEvent* event); //鼠标事件
	void mouseMoveEvent(QMouseEvent* event);	//鼠标移动事件
	void mouseReleasEvent(QMouseEvent*);	//鼠标松开事件

protected:
	void initTitleBar(ButtonType buttontype = MIN_BUTTON);
	void setTitleBarTitle(const QString& title,const QString& icon = "");

public slots:
	void onShowClose(bool);
	void onShowMin(bool);
	void onShowHide(bool);
	void onShowNormal(bool);
	void onShowQuit(bool);
	void onSignalSkinChanged(const QColor& color);

	void onButtonMinClicked();
	void onButtonRestoreClicked();
	void onButtonMaxClicked();
	void onButtonCloseClicked();

protected:
	QPoint m_mousePoint;	//鼠标位置
	bool m_mousePressed;	//鼠标是否按下
	QColor m_colorBackGround;	//背景色
	QString m_styleName;	//样式文件名称
	TitleBar* _titleBar;	//标题栏
};

#include "basicwindow.h"

#include<QFile>
#include<QStyleOption>
#include<QPainter>
#include<QDesktopWidget>
#include<QRect>
#include<QApplication>
#include<QMouseEvent>

BasicWindow::BasicWindow(QWidget *parent)
	: QDialog(parent)
{
	setWindowFlags(Qt::FramelessWindowHint);
	setAttribute(Qt::WA_TranslucentBackground, true);
}

BasicWindow::~BasicWindow()
{
}

void BasicWindow::loadStyleSheet(const QString& sheetName)
{
	m_styleName = sheetName;
	QFile file(":/Resouces/QSS/"+sheetName+".css");
	file.open(QFile::ReadOnly);

	if (file.isOpen())
	{
		setStyleSheet("");
		QString qsstyleSheet = QLatin1String(file.readAll());


		//获取用户当前的皮肤RGB值
		QString r = QString::number(m_colorBackGround.red());
		QString g = QString::number(m_colorBackGround.green());
		QString b = QString::number(m_colorBackGround.blue());

		qsstyleSheet += QString("QWidget[titleskin=true]\
					{background-color:rgb(%1,%2,%3);\
					 border-top-left-radius:4px;}\
					 QWidget[bottomskin=true]\
					 {border-top:1px solid rgba(%1,%2,%3,100);\
					 background-color:rgba(%1,%2,%3,50);\
				     border-bottom-left-radius:4px;\
					 border-bottom-right-radius:4px;}")
					 .arg(r).arg(g).arg(b);
		setStyleSheet(qsstyleSheet);
	}

	file.close();
}

//头像转变为圆头像
QPixmap BasicWindow::getRoundImage(const QPixmap& src, QPixmap& mask, QSize masksize)
{
	//图像为零 
	if (masksize == QSize(0, 0))
	{//重置masksize
		masksize = mask.size();
	}
	else
	{  //通过给定的masksize进行缩放
		mask.scaled(masksize,Qt::KeepAspectRatio,Qt::SmoothTransformation);
	}

	//保存转换后的图像
	QImage resultImage(masksize,QImage::Format_ARGB32_Premultiplied);
	QPainter painter(&resultImage);
	painter.setCompositionMode(QPainter::CompositionMode_Source);
	painter.fillRect(resultImage.rect(),Qt::transparent);
	painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
	painter.drawPixmap(0,0,mask);
	painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
	painter.drawPixmap(0,0,src.scaled(Qt::KeepAspectRatio,Qt::SmoothTransformation));
	painter.end();

	return QPixmap::fromImage(resultImage);
}

void BasicWindow::onShowClose(bool)
{
	close();
}

void BasicWindow::onShowMin(bool)
{
	showMinimized();
}

void BasicWindow::onShowHide(bool)
{
	hide();
}

void BasicWindow::onShowNormal(bool)
{
	show();
	activateWindow();
}

void BasicWindow::onShowQuit(bool)
{
	QApplication::quit();
}

void BasicWindow::paintEvent(QPaintEvent*)
{
}

//鼠标按下事件
void BasicWindow::mousePressEvent(QMouseEvent* e)
{
	if (e->button() == Qt::LeftButton)
	{
		m_mousePressed = true;
		m_mousePoint = e->globalPos() - pos();
		accept();
	}
}

//鼠标移动事件  
void BasicWindow::mouseMoveEvent(QMouseEvent* e)
{
	if (m_mousePressed && (e->buttons() && Qt::LeftButton))
	{
		move(e->globalPos() - m_mousePoint);
		e->accept();
	}
}

//鼠标释放事件
void BasicWindow::mouseReleasEvent(QMouseEvent*)
{
	m_mousePressed = false;
}

//背景图
void BasicWindow::initBackGroundColor()
{
	QStyleOption opt;
	opt.init(this);

	QPainter  p(this);
	style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);
}

//子类化部件时,需要重写绘图事件设置背景图
void BasicWindow::onSignalSkinChanged(const QColor& color)
{
	m_colorBackGround = color;
	loadStyleSheet(m_styleName);
}

void BasicWindow::onButtonMinClicked()
{
	if (Qt::Tool == (windowFlags() & Qt::Tool))
	{
		hide();
	}
	else
	{
		showMinimized();
	}
}

void BasicWindow::onButtonRestoreClicked()
{
	QPoint windowPos;
	QSize windowSize;
	//获取窗体的位置 大小信息
	_titleBar->getRestoreInfo(windowPos,windowSize);
	setGeometry(QRect(windowPos,windowSize));
}

void BasicWindow::onButtonMaxClicked()
{
	_titleBar->saveRestoreInfo(pos(), QSize(width(), height()));
	QRect desktopRect = QApplication::desktop()->availableGeometry();
	QRect factRect = QRect(desktopRect.x() - 3,desktopRect.y() - 3,
						desktopRect.width() + 6,desktopRect.height() + 6);
	setGeometry(factRect);
}

void BasicWindow::onButtonCloseClicked()
{
	close();
}

void BasicWindow::initTitleBar(ButtonType buttontype)
{
	_titleBar = new TitleBar(this);
	_titleBar->setButtonType(buttontype);
	_titleBar->move(0, 0);

	connect(_titleBar, SIGNAL(signalButtonMinClicked()), this, SLOT(onButtonMinClicked()));
	connect(_titleBar, SIGNAL(signalButtonRestoreClicked()), this, SLOT(onButtonRestoreClicked()));
	connect(_titleBar,SIGNAL(signalButtonMaxClicked()),this,SLOT(onButtonMaxClicked()));
	connect(_titleBar,SIGNAL(signalButtonCloseClicked()),this,SLOT(onButtonCloseClicked()));
}

void BasicWindow::setTitleBarTitle(const QString& title, const QString& icon)
{
	_titleBar->setTitleIcon(icon);
	_titleBar->setTitleContent(title);
}

注意 const


	void setTitleIcon(const QString &filePath);	//设置标题栏图标
	void setTitleContent(const QString &titleContent);	 //设置标题栏内容

头文件

#include<QDesktopWidget>

void BasicWindow::onButtonMaxClicked()
{
	_titleBar->saveRestoreInfo(pos(), QSize(width(), height()));
	QRect desktopRect = QApplication::desktop()->availableGeometry();
	QRect factRect = QRect(desktopRect.x() - 3,desktopRect.y() - 3,
						desktopRect.width() + 6,desktopRect.height() + 6);
	setGeometry(factRect);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值