qt/LedOK/wProgramManager/wEditProgram/wElement/etext.cpp
2022-01-20 10:08:17 +08:00

1385 lines
47 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 "etext.h"
#include "etextattr.h"
#include "etextinput.h"
#include <QString>
#include <QThread>
#include <QSettings>
#include <loappconfig.h>
eText::eText(QRectF rectInit,InteractiveType type, QGraphicsItem *parent) :
eObject(rectInit, type, parent),
m_wAttr(nullptr),
m_wTextInput(nullptr)
{
m_iType=eObject::Text;
// Widget
m_attr.text = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\np, li { white-space: pre-wrap; }\n</style></head><body style=\" font-family:'Arial'; font-size:14px; font-weight:448; font-style:normal;\">\n<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" color:#ffffff;\">"+tr("Hello Text !")+"</span></p></body></html>";
m_attr.lineSpacing = 0;
m_attr.wordSpacing=0;
m_attr.cBackground=Qt::transparent;
m_attr.opt.setAlignment(Qt::AlignCenter);
m_attr.opt.setWrapMode(QTextOption::WrapAnywhere);
//Play
m_attr.playStyle = Turning;
m_attr.stRolloing.headTailConnected = true;
m_attr.stRolloing.headTailSpacing = 10;
m_attr.stRolloing.rollingSpeed = 50;
m_attr.stRolloing.rollingStyle = Left2Right;
m_attr.stRolloing.playDuration = MACRO_DEFAULT_PLAYDURATION*6;
m_attr.stTurning.strEffect="no";
m_attr.stTurning.iEffectTime=MACRO_DEFAULT_PLAYDURATION*6;
m_attr.stTurning.iEffectSpeed=3;
m_attr.stTurning.iPageStopTime=10;
m_attr.stTurning.playDuration=MACRO_DEFAULT_PLAYDURATION*6;
m_attr.stStatic.playDuration=MACRO_DEFAULT_PLAYDURATION;
if(m_pRenderPixmap!=nullptr)
delete m_pRenderPixmap;
m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
m_pRenderPixmap->fill(m_attr.cBackground);
connect(this, SIGNAL(rectChanged(const QRectF &)), this, SLOT(OnRectChanged()));
RefreshBigPixmap();
}
eText::eText(const QJsonObject &json, InteractiveType type, QGraphicsItem *parent) :
eObject(json["geometry"].toObject(), type, parent),
m_wAttr(nullptr),
m_wTextInput(nullptr)
{
m_iType=eObject::Text;
setElement(json, m_attr);
if(m_pRenderPixmap!=nullptr)
delete m_pRenderPixmap;
m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
m_pRenderPixmap->fill(m_attr.cBackground);
connect(this, SIGNAL(rectChanged(const QRectF &)), this, SLOT(OnRectChanged()));
RefreshBigPixmap();
}
eText::~eText()
{
if(m_pRenderPixmap!=nullptr)
delete m_pRenderPixmap;
}
void eText::setElement(const QJsonObject &json, Data &attr)
{
auto apptool = LoAppTools::getInstance();
QJsonDocument jRoot;
jRoot.setObject(json);
// auto at = LoAppTools::getInstance();
// Widget
attr.text = jRoot["widget"]["text"].toString();
attr.lineSpacing = jRoot["widget"]["lineSpacing"].toInt();
attr.wordSpacing = jRoot["widget"]["wordSpacing"].toInt();
int iAlign=jRoot["widget"]["align"].toInt();
attr.opt.setAlignment(static_cast<Qt::Alignment>(iAlign));
attr.cBackground = apptool->int2Color(jRoot["widget"]["cBackground"].toInt());
attr.iPageCount = jRoot["widget"]["iPageCount"].toInt();
// Play
attr.playStyle = jRoot["play"]["style"].toInt();
QJsonObject objTurning;
objTurning=jRoot["play"]["turning"].toObject();
attr.stTurning.strEffect=objTurning["strEffect"].toString();
attr.stTurning.iEffectTime=objTurning["iEffectTime"].toInt();
attr.stTurning.iEffectSpeed=objTurning["iEffectSpeed"].toInt();
attr.stTurning.iPageStopTime=objTurning["iPageStopTime"].toInt();
attr.stTurning.playDuration=objTurning["playDuration"].toInt();
QJsonObject objRolling;
objRolling=jRoot["play"]["rolling"].toObject();
attr.stRolloing.rollingStyle=objRolling["rollingStyle"].toInt();
attr.stRolloing.rollingSpeed=objRolling["rollingSpeed"].toInt();
attr.stRolloing.headTailConnected=objRolling["headTailConnected"].toBool();
attr.stRolloing.headTailSpacing=objRolling["headTailSpacing"].toInt();
attr.stRolloing.playDuration=objRolling["playDuration"].toInt();
QJsonObject objStatic;
objStatic=jRoot["play"]["static"].toObject();
attr.stStatic.playDuration=objStatic["playDuration"].toInt();
}
QWidget* eText::wAttr()
{
QWidget *wObj = eObject::wAttr();//x,y,w,h widget
QWidget *w = wAttrElement();// text widget
static_cast<QBoxLayout*>(w->layout())->insertWidget(0, wObj);
return w;
}
QWidget* eText::wAttrElement()
{
eTextAttr *w = new eTextAttr(m_attr);
connect(w, SIGNAL(destroyed(QObject*)), this, SLOT(onWAttrClosed()));//alahover 销毁时调用关闭
// Widget
connect(w, SIGNAL(sTextHAlignChanged(int)), this, SLOT(onTextHAlignChanged(int)));
connect(w, SIGNAL(sTextVAlignChanged(int)), this, SLOT(onTextVAlignChanged(int)));
connect(w, SIGNAL(sBackgroundColorChanged(const QColor&)), this, SLOT(onBackgroundColorChanged(const QColor&)));
connect(w, SIGNAL(sTextLetterSpacingChanged(int)), this, SLOT(onTextLetterSpacingChanged(int)));
connect(w, SIGNAL(sTextLineSpacingChanged(int)), this, SLOT(onTextLineSpacingChanged(int)));
connect(w, SIGNAL(sTextRickChanged(const QString &)), this, SLOT(onTextRickChanged(const QString &)));
// Play
connect(w, SIGNAL(sPlayStyleChanged(int)), this, SLOT(onPlayStyleChanged(int)));
connect(w->m_pTextScrollAttr, SIGNAL(sHeadTailConnected(bool)), this, SLOT(onHeadTailConnected(bool)));
connect(w->m_pTextScrollAttr, SIGNAL(sHeadTailSpacingChanged(int)), this, SLOT(onHeadTailSpacingChanged(int)));
connect(w->m_pTextScrollAttr, SIGNAL(sRollingSpeedChanged(int)), this, SLOT(onRollingSpeedChanged(int)));
connect(w->m_pTextScrollAttr, SIGNAL(sRollingStyleChanged(int)), this, SLOT(onRollingStyleChanged(int)));
connect(w->m_pTextScrollAttr, SIGNAL(sPlayDurationChanged(QTime)), this, SLOT(onRollingPlayDurationChanged(QTime)));
connect(w->m_pTextFlipAttr, SIGNAL(sPlayDurationChanged(QTime)), this, SLOT(onTurningPlayDurationChanged(QTime)));
connect(w->m_pTextFlipAttr, SIGNAL(sEffectChanged(QString)), this, SLOT(onTurningEffectChanged(QString)));
connect(w->m_pTextFlipAttr, SIGNAL(sEveryPagePlayDurationChanged(QTime)), this, SLOT(onEveryPageDurationChanged(QTime)));
connect(w->m_pTextFlipAttr, SIGNAL(sTurningEffectSpeedChanged(int)), this, SLOT(onTurningEffectSpeedChanged(int)));
connect(w->m_pTextStaticAttr, SIGNAL(sPlayDurationChanged(QTime)), this, SLOT(onStaticPlayDurationChanged(QTime)));
connect(w,SIGNAL(sPageIndexPreview(int)),this,SLOT(OnPageIndexPreview(int)));
connect(this,SIGNAL(sPageCountChanged(int)),w,SLOT(OnPageCountChanged(int)));
m_wAttr = w;
return w;
}
bool eText::save(const QString &pRoot) {
m_pRootPath=pRoot;
QRectF r1 = geometry();
QRect r=r1.toRect();
QString strDir=QString("%1%2%3%4%5").arg(zValue()).arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
//保存图片元素到相应目录
QDir dirTemp(m_pRootPath+MACRO_FENGEFU+strDir);
if(dirTemp.exists())
{
if(dirTemp.removeRecursively())
{
int iReTryCount=0;
QDir dRoot(m_pRootPath);
while(!dRoot.mkdir(strDir))
{
QThread::sleep(1);
iReTryCount++;
if(iReTryCount>10)
break;
}
}
}
else {
int iReTryCount=0;
QDir dRoot(m_pRootPath);
while(!dRoot.mkdir(strDir))
{
QThread::sleep(1);
iReTryCount++;
if(iReTryCount>10)
break;
}
}
// QDir dRoot(m_pRootPath);
// if(dRoot.exists(strDir)) {
// QDir dirTemp(m_pRootPath+MACRO_FENGEFU+strDir);
// if(dirTemp.exists())
// {
// }
// dRoot.removeRecursively()
// if(dRoot.remove(strDir))
// {
// removeRecursively
// }
// }
// int iReTryCount=0;
// while(!dRoot.mkdir(strDir))
// {
// QThread::sleep(1);
// iReTryCount++;
// if(iReTryCount>10)
// break;
// }
QString strPath=m_pRootPath+ MACRO_FENGEFU +strDir;
if(m_attr.playStyle==eText::Turning)//生成多张图
{
for (int i=0;i<m_list.count();i++)
{
QString strFile=QString("text%1.png").arg(i);
QString strPngName=strPath+MACRO_FENGEFU+strFile;
MakeMuliPngToDir(i+1,strPngName);
}
}
else if(m_attr.playStyle==eText::Rolling)//生成一张大图
{
if(m_attr.stRolloing.rollingStyle==0||m_attr.stRolloing.rollingStyle==2)//0,1左右滚动生成一张横向长图
{
QString strFile="text";
QString strPngName=strPath+MACRO_FENGEFU+strFile;
MakeBigMoveLeftOrRightOnePngToDir(strPngName);
}
else if(m_attr.stRolloing.rollingStyle==1||m_attr.stRolloing.rollingStyle==3)//上下滚动,生成一张纵向长图
{
QString strFile="text0.png";
QString strPngName=strPath+MACRO_FENGEFU+strFile;
MakeBigMoveUpOrDownOnePngToDir(strPngName);
}
}
else if(m_attr.playStyle==eText::Static)//生成一张图
{
QString strFile="text0.png";
QString strPngName=strPath+MACRO_FENGEFU+strFile;
MakeMuliPngToDir(1,strPngName);
}
return true;
}
QJsonObject eText::elementJson() const
{
auto apptool = LoAppTools::getInstance();
QJsonObject oRoot;
QJsonObject oWidget;
QJsonObject oColor;
QJsonObject oFont;
QJsonObject oPlay;
// Interior
oRoot["elementType"] = "Text";
oRoot["elementTypeId"] = type();
oRoot["geometry"] = eObject::elementJson();
// Widget
oWidget["text"] = m_attr.text;
oWidget["align"] = static_cast<int>(m_attr.opt.alignment());
oColor["background"] = LoAppTools::getInstance()->color2Int(m_attr.cBackground);
oWidget["lineSpacing"] = m_attr.lineSpacing;
oWidget["wordSpacing"] = m_attr.wordSpacing;
oWidget["cBackground"] = apptool->color2Int(m_attr.cBackground);
QJsonObject QJOPngObject;
QJsonArray QJArrayPngItems;
QRectF r1 = geometry();
QRect r=r1.toRect();
QString strDir=QString("%1%2%3%4%5").arg(zValue()).arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height());
if(m_attr.playStyle==eText::Turning)//生成多张图
{
for (int i=0;i<m_list.count();i++)
{
QString strFile=QString("text%1.png").arg(i);
QJArrayPngItems.append(strFile);
}
}
else if(m_attr.playStyle==eText::Rolling)//生成一张大图
{
if(m_attr.stRolloing.rollingStyle==0||m_attr.stRolloing.rollingStyle==2)//上下滚动,生成一张纵向长图
{
for (int i=0;i<m_linelist.count();i++)
{
QString strFile=QString("text%1.png").arg(i);
QJArrayPngItems.append(strFile);
}
}
else if(m_attr.stRolloing.rollingStyle==1||m_attr.stRolloing.rollingStyle==3)//上下滚动,生成一张纵向长图
{
QString strFile="text0.png";
QJArrayPngItems.append(strFile);
}
}
else if(m_attr.playStyle==eText::Static)//生成一张图
{
QString strFile="text0.png";
QJArrayPngItems.append(strFile);
}
oWidget.insert("iPageCount",m_attr.iPageCount);
oWidget.insert("files",QJArrayPngItems);
oWidget.insert("idDir",strDir);
oRoot["widget"] = oWidget;
// Play
oPlay["style"] = m_attr.playStyle;
QJsonObject objTurning;
objTurning["strEffect"]=m_attr.stTurning.strEffect;
objTurning["iEffectTime"]=m_attr.stTurning.iEffectTime;
objTurning["iEffectSpeed"]=m_attr.stTurning.iEffectSpeed;
objTurning["iPageStopTime"]=m_attr.stTurning.iPageStopTime;
objTurning["playDuration"]=m_attr.stTurning.playDuration;
oPlay["turning"] = objTurning;
QJsonObject objRolling;
objRolling["rollingStyle"]=m_attr.stRolloing.rollingStyle;
objRolling["rollingSpeed"]=m_attr.stRolloing.rollingSpeed;
objRolling["headTailConnected"]=m_attr.stRolloing.headTailConnected;
objRolling["headTailSpacing"]=m_attr.stRolloing.headTailSpacing;
objRolling["playDuration"]=m_attr.stRolloing.playDuration;
oPlay["rolling"] = objRolling;
QJsonObject objStatic;
objStatic["playDuration"]=m_attr.stStatic.playDuration;
oPlay["static"] = objStatic;
oRoot["play"] = oPlay;
return oRoot;
}
void eText::setTextRichContents(QTextDocument &doc)
{
const int lineSpacing = QFontMetrics(doc.defaultFont()).lineSpacing();
m_lineH = m_attr.lineSpacing + lineSpacing;
QString ctx = QString("<style>p{"
"line-height:%1px;"
"white-space:pre-wrap;"
"}</style>"
"<p>%2</p>")
.arg(m_lineH)
.arg(m_attr.text);
doc.setHtml(ctx);
}
//void eText::setTextVAlign(int align, QTextDocument &doc)
//{//图片分页,按图片的高度计算
//}
void eText::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
MACRO_FAN_JUCHI(m_pPainter)
// QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
// if(bshBackground != Qt::NoBrush) {
// painter->fillRect(rect(), bshBackground);
// }
painter->drawPixmap(rect(), *m_pRenderPixmap,rect());
painter->restore();
eObject::paint(painter, option, widget);
}
//void eText::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
//{
//// if(Qt::LeftButton == event->button()) {
//// setInput();
//// }
//}
void eText::setInput()
{
if(nullptr == m_wTextInput && nullptr != m_wAttr) {
m_wTextInput = new eTextInput(m_attr.text, m_wAttr);
connect(m_wTextInput, SIGNAL(sTextChanged(const QString &)), this, SLOT(onTextChanged(const QString &)));
connect(m_wTextInput, SIGNAL(destroyed(QObject*)), this, SLOT(onInputClosed()));
m_wTextInput->show();
}
}
void eText::onTextHAlignChanged(int align)
{
int res = m_attr.opt.alignment();
res &= ~Qt::AlignHorizontal_Mask;
res |= align;
m_attr.opt.setAlignment(static_cast<Qt::Alignment>(res));
updateGeometry();
RefreshBigPixmap();
}
void eText::onTextVAlignChanged(int align)
{
int res = m_attr.opt.alignment();
res &= ~Qt::AlignVertical_Mask;
res |= align;
m_attr.opt.setAlignment(static_cast<Qt::Alignment>(res));
updateGeometry();
RefreshBigPixmap();
}
void eText::onTextRickChanged(const QString &strHtml)
{
m_attr.text = strHtml; updateGeometry();
RefreshBigPixmap();
}
void eText::CreateBigOnePixBmp()
{
m_pPainter=new QPainter();
if(m_pPainter!=nullptr)
{
MACRO_FAN_JUCHI(m_pPainter)
QTextDocument doc;
doc.setDefaultTextOption(m_attr.opt);
doc.setDocumentMargin(0);
doc.setTextWidth(rect().width());
QFont aa;
MACRO_FONT_FAN_JUCHI(aa)
doc.setDefaultFont(aa);
setTextRichContents(doc);
int iwidth=this->rect().toRect().width();
QRectF OutPutBmpRect = QRectF(0, 0, iwidth, doc.size().height());//实际绘制文本的矩形大小
int iheight=OutPutBmpRect.toRect().height();
QPixmap map(iwidth,iheight);
map.fill(m_attr.cBackground);
m_pPainter->begin(&map);
doc.drawContents(m_pPainter,OutPutBmpRect);
m_pPainter->end();
m_bigOnePixBmp=map.copy(OutPutBmpRect.toRect());
}
delete m_pPainter;
// QString strPngName="F:/text.png";
// m_bigOnePixBmp.save(strPngName, "PNG"); //保存
}
void eText::OnRectChanged()
{
if(m_oldWidth==rect().width()&&m_oldHeight==rect().height())//只是移动坐标,宽高没有变化
{
return;
}
m_oldWidth=rect().width();
m_oldHeight=rect().height();
RefreshBigPixmap();
}
void eText::RefreshBigPixmap()
{
if(m_attr.playStyle==eText::Turning)//生成多张图
{
CreateBigOnePixBmp();
if(m_bigOnePixBmp.height()>6)
{
QImage tempQImage=m_bigOnePixBmp.toImage();
m_ImageBkColor=tempQImage.pixelColor(0,0);
m_list.clear();
if(GetPagesPosList()<=0)
{
}
}
if(m_list.count()>0)
{
OnPageIndexPreview(1);
}
m_attr.iPageCount=m_list.count();
emit sPageCountChanged(m_list.count());
}
else if(m_attr.playStyle==eText::Rolling)//生成一张大图
{
if(m_attr.stRolloing.rollingStyle==0||m_attr.stRolloing.rollingStyle==2)//上下滚动,生成一张纵向长图
{
MakeBigMoveLeftOrRightOnePngToDir();
}
else if(m_attr.stRolloing.rollingStyle==1||m_attr.stRolloing.rollingStyle==3)//上下滚动,生成一张纵向长图
{
MakeBigMoveUpOrDownOnePngToDir();
}
m_attr.iPageCount=1;
emit sPageCountChanged(1);
}
else if(m_attr.playStyle==eText::Static)//生成一张图
{
CreateBigOnePixBmp();
if(m_bigOnePixBmp.height()>6)
{
QImage tempQImage=m_bigOnePixBmp.toImage();
m_ImageBkColor=tempQImage.pixelColor(0,0);
m_list.clear();
if(GetPagesPosList()<=0)
{
}
}
//m_attr.iPageCount=m_list.count();
m_attr.iPageCount=1;
OnPageIndexPreview(1);
emit sPageCountChanged(1);
}
}
int eText::GetPagesPosList()
{
int iIndex=0;
for (;;)
{
if(ClearPixmapTopAndBottomBlack()<=0)
return m_list.count();
txtBmpPage temp=GetFirstPagePixmap();
if(temp.index==-1)//表示到最后bigpixmap已经没有内容了
{
temp.index=iIndex;
iIndex++;
m_list.append(temp);
return m_list.count();
}
else
{
temp.index=iIndex;
m_list.append(temp);
iIndex++;
if(iIndex>MACRO_MAX_TEXT_PAGE)
{
break;
}
}
}
return m_list.count();
}
//去除上下的黑边,返回去掉黑边图像的高度
int eText::ClearPixmapTopAndBottomBlack()
{
QImage tempQImage=m_bigOnePixBmp.toImage();
int iFirst=0;
int iEnd=m_bigOnePixBmp.height();
bool bFirst=false;
bool bEnd=false;
for(int y=0;y<m_bigOnePixBmp.height();y++)
{
for (int x=0;x<m_bigOnePixBmp.width();x++)
{
if(tempQImage.pixelColor(x,y)!=m_ImageBkColor)
{
iFirst=y;
bFirst=true;
break;
}
}
if(bFirst)
break;
}
if(bFirst==false)//图全黑
{
return -1;
}
for(int m=m_bigOnePixBmp.height()-1;m>=0;m--)
{
for (int n=0;n<m_bigOnePixBmp.width();n++)
{
if(tempQImage.pixelColor(n,m)!=m_ImageBkColor)
{
iEnd=m;
bEnd=true;
break;
}
}
if(bEnd)
break;
}
if(iEnd-iFirst>1)
{
int iCopyHeight=iEnd-iFirst+1;
m_bigOnePixBmp=m_bigOnePixBmp.copy(0,iFirst,m_bigOnePixBmp.width(),iCopyHeight+1);
return iCopyHeight;
}
else {
//空的没有文字的部分
return -1;
}
}
txtBmpPage eText::GetFirstPagePixmap()
{
QImage tempQImage=m_bigOnePixBmp.toImage();
// tempQImage.save("f://a.png","PNG");
txtBmpPage tempPageClass;
int iWidth=m_bigOnePixBmp.width();
int iHeight=m_bigOnePixBmp.height();
int iRectHeight=rect().height();
if(iRectHeight>iHeight)
iRectHeight=iHeight;
int iType=0;
//判断底边是不是黑
for (int x=0;x<iWidth;x++)
{
QColor aa=tempQImage.pixelColor(x,iRectHeight-1);
if(aa!=m_ImageBkColor)
{
iType=1;
break;
}
}
if(iType==0) //矩形底边是黑色
{
//倒着推断图的下边
for (int y=iRectHeight-1;y>=0;y--)
{
bool bFinished=false;
for (int x=0;x<iWidth;x++)
{
QColor bb=tempQImage.pixelColor(x,y);
if(bb!=m_ImageBkColor)
{
bFinished=true;
break;
}
}
if(bFinished)
{
tempPageClass.bitmap=m_bigOnePixBmp.copy(0,0,iWidth,y+1);
m_bigOnePixBmp=m_bigOnePixBmp.copy(0,y+1,iWidth,iHeight-y-1);
break;
}
}
}
else if(iType==1)
{
//倒着推断图的下边,如果到顶都没有符合条件的黑边,则表示文字的大小大于图片的高度,否则找到上一个文字底部作为图片的下边
int iSubType=0;
int iSubEnd=0;
for (int y=iRectHeight-1;y>=0;y--)
{
bool bFinished=false;
for (int x=0;x<iWidth;x++)
{
QColor bb=tempQImage.pixelColor(x,y);
if(bb!=m_ImageBkColor)
{
bFinished=true;
break;
}
}
if(bFinished==false)
{
//找到了 一条黑边,则证明上面还有文字,
iSubType=1;
iSubEnd=y;
break;
}
}
if(iSubType==0)//文字的大小大于图片的高度
{
tempPageClass.bitmap=m_bigOnePixBmp.copy(0,0,iWidth,iRectHeight);
if(iHeight>iRectHeight)
m_bigOnePixBmp=m_bigOnePixBmp.copy(0,iRectHeight,iWidth,iHeight-iRectHeight);
else
{
tempPageClass.index=-1;
return tempPageClass;
}
}
else if(iSubType==1)
{
for (int y=iSubEnd;y>=0;y--)
{
bool bFinished=false;
for (int x=0;x<iWidth;x++)
{
if(tempQImage.pixelColor(x,y)!=m_ImageBkColor)
{
bFinished=true;
break;
}
}
if(bFinished==true)
{
tempPageClass.bitmap=m_bigOnePixBmp.copy(0,0,iWidth,y+1);
m_bigOnePixBmp=m_bigOnePixBmp.copy(0,y+1,iWidth,iHeight-y-1);
break;
}
}
}
}
return tempPageClass;
}
void eText::OnPageIndexPreview(int iPageIndex)
{
iPageIndex=iPageIndex-1;
if(iPageIndex<0)
iPageIndex=0;
if(iPageIndex>=0)
{
if(m_list.count()>0&&iPageIndex<m_list.count())
{
//Qt::Alignment::enum_type::AlignVCenter;
if(m_pRenderPixmap!=nullptr)
delete m_pRenderPixmap;
m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
m_pRenderPixmap->fill(m_attr.cBackground);
int res = m_attr.opt.alignment();
QPixmap tmp=m_list.at(iPageIndex).bitmap;
if((res & Qt::Alignment::enum_type::AlignTop)!=0)
{
QPainter painter;
painter.begin(m_pRenderPixmap);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
if(bshBackground != Qt::NoBrush) {
painter.fillRect(0,tmp.height(),tmp.width(),rect().height()-tmp.height(), bshBackground);
}
painter.drawPixmap(0,0,tmp.width(),tmp.height(),tmp);
painter.end();
}
else if((res & Qt::Alignment::enum_type::AlignBottom)!=0)
{
QPainter painter;
painter.begin(m_pRenderPixmap);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
if(bshBackground != Qt::NoBrush) {
painter.fillRect(0,0,tmp.width(),rect().height()-tmp.height(), bshBackground);
}
painter.drawPixmap(0,rect().height()-tmp.height(),tmp.width(),tmp.height(),tmp);
painter.end();
}
else
{
QPainter painter;
painter.begin(m_pRenderPixmap);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
int iTopOffset=(rect().height()-tmp.height())/2;
if(bshBackground != Qt::NoBrush) {
painter.fillRect(0,0,tmp.width(),iTopOffset, bshBackground);
painter.fillRect(0,iTopOffset+tmp.height(),tmp.width(),rect().height()-iTopOffset-tmp.height() ,bshBackground);
}
painter.drawPixmap(0,iTopOffset,tmp.width(),tmp.height(),tmp);
painter.end();
}
}
}
else {
}
updateGeometry();
}
void eText::MakeBigMoveUpOrDownOnePngToDir()
{
QPainter *pPainter=new QPainter();
if(pPainter!=nullptr)
{
MACRO_FAN_JUCHI(pPainter)
QTextDocument doc;
doc.setDefaultTextOption(m_attr.opt);
doc.setDocumentMargin(0);
doc.setTextWidth(rect().width());
QFont aa;
MACRO_FONT_FAN_JUCHI(aa)
doc.setDefaultFont(aa);
const int lineSpacing = QFontMetrics(doc.defaultFont()).lineSpacing();
int lineH = m_attr.lineSpacing + lineSpacing;
QString ctx = QString("<style>p{"
"line-height:%1px;"
"white-space:pre-wrap;"
"}</style>"
"<p>%2</p>")
.arg(lineH)
.arg(m_attr.text);
doc.setHtml(ctx);
int iwidth=this->rect().toRect().width();
QRectF OutPutBmpRect = QRectF(0, 0, iwidth, doc.size().height());//实际绘制文本的矩形大小
int iheight=OutPutBmpRect.toRect().height();
QPixmap map(iwidth,iheight);
map.fill(m_attr.cBackground);
pPainter->begin(&map);
doc.drawContents(pPainter,OutPutBmpRect);
pPainter->end();
if(m_pRenderPixmap!=nullptr)
delete m_pRenderPixmap;
m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
m_pRenderPixmap->fill(m_attr.cBackground);
pPainter->begin(m_pRenderPixmap);
if(rect().height()>map.height())
{
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
if(bshBackground != Qt::NoBrush) {
pPainter->fillRect(0,map.height(),rect().width(),rect().height()-map.height(), bshBackground);
}
QRectF rectDest(0,0,rect().width(),rect().height());
QRectF rectSource(0,0,rect().width(),map.height());
pPainter->drawPixmap(map.rect(),map,map.rect());
}
else {
QRectF rectSource(0,0,rect().width(),rect().height());
pPainter->drawPixmap(rect(),map,rectSource);
}
pPainter->end();
updateGeometry();
}
delete pPainter;
}
void eText::MakeBigMoveLeftOrRightOnePngToDir()
{
QPainter *pPainter=new QPainter();
if(pPainter!=nullptr)
{
MACRO_FAN_JUCHI(pPainter)
QTextDocument doc;
//m_attr.opt.setAlignment(Qt::AlignJustify);
m_attr.opt.setAlignment(Qt::AlignLeft);
doc.setDefaultTextOption(m_attr.opt);
doc.setDocumentMargin(0);
// doc.setTextWidth(rect().width());
doc.setTextWidth(3800);
QFont aa;
MACRO_FONT_FAN_JUCHI(aa)
doc.setDefaultFont(aa);
const int lineSpacing = QFontMetrics(doc.defaultFont()).lineSpacing();
int lineH = m_attr.lineSpacing + lineSpacing;
QString ctx = QString("<style>p{"
"line-height:%1px;"
"white-space:pre-wrap;"
"}</style>"
"<p>%2</p>")
.arg(lineH)
.arg(m_attr.text);
doc.setHtml(ctx);
//int iwidth=this->rect().toRect().width();
int iwidth=3800;
QRectF OutPutBmpRect = QRectF(0, 0, iwidth, doc.size().height());//实际绘制文本的矩形大小
int iheight=OutPutBmpRect.toRect().height();
QPixmap map(iwidth,iheight);
map.fill(m_attr.cBackground);
pPainter->begin(&map);
doc.drawContents(pPainter,OutPutBmpRect);
pPainter->end();
delete pPainter;
QPixmap bigOnePixmap=map.copy(OutPutBmpRect.toRect());
//获取
m_linelist.clear();
//获取到长图的每页添加到list并返回最大高度
int iMaxHeight=GetLinePng(bigOnePixmap);
//把list中的图片拼接起来
if(m_linelist.count()>0)
{
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
//把长图的开头拷贝到输出pixmap预览
QPainter *pPainter2=new QPainter();
MACRO_FAN_JUCHI(pPainter2)
if(m_pRenderPixmap!=nullptr)
delete m_pRenderPixmap;
m_pRenderPixmap=new QPixmap(rect().width(),rect().height());
m_pRenderPixmap->fill(m_attr.cBackground);
pPainter2->begin(m_pRenderPixmap);
txtBmpLine tmpLine=m_linelist.at(0);
int itempHeight=tmpLine.bitmap.height();//tmpLine.iEnd-tmpLine.iStart;
int iTopOffset=(rect().height()-itempHeight)/2;
pPainter2->fillRect(0,0,rect().width(),iTopOffset, bshBackground);
pPainter2->fillRect(0,iTopOffset+itempHeight,rect().width(),rect().height()-iTopOffset-itempHeight, bshBackground);
QRectF rectDest(0,iTopOffset,rect().width(),itempHeight);
QRectF rectSource(0,0,rect().width(),itempHeight);
pPainter2->drawPixmap(rectDest,tmpLine.bitmap,rectSource);
pPainter2->end();
delete pPainter2;
// bigOneLongPixmap.save(strPngPathAndName,"PNG");
}
}
updateGeometry();
}
int eText::GetLinePng(QPixmap bigOnePixmap)
{
QImage tempQImage=bigOnePixmap.toImage();
m_ImageBkColor=tempQImage.pixelColor(0,0);
// int iFirst=0;
// int iEnd=bigOnePixmap.height();
bool bLineFlag=false;
// bool bEnd=false;
int iIndex=0;
txtBmpLine tmp;
int iMaxHeight=0;
for(int y=0;y<bigOnePixmap.height();y++)
{
bool bOneWidthEnd=false;
int itemp=0;
for (int x=0;x<bigOnePixmap.width();x++)
{
if(bLineFlag==false)//行没有开始
{
QColor bb=tempQImage.pixelColor(x,y);
if(bb!=m_ImageBkColor )//找到行的开始
{
tmp.index=iIndex;
tmp.iStart=y;
iIndex++;
bLineFlag=true;
itemp=1;
break;
}
}
else//行开始了
{
if(tempQImage.pixelColor(x,y)!=m_ImageBkColor )
{
bOneWidthEnd=true;
}
}
}
if(itemp==0)
{
if(bLineFlag)//行开始后
{
if(bOneWidthEnd )//仍旧是行
{
if(y=bigOnePixmap.height()-1)
{
tmp.iEnd=y;
int ipicHeight=tmp.iEnd-tmp.iStart;
if(ipicHeight>2)
{
if(ipicHeight>iMaxHeight)
iMaxHeight=ipicHeight;
tmp.bitmap=bigOnePixmap.copy(0,tmp.iStart,bigOnePixmap.width(),tmp.iEnd-tmp.iStart);
m_linelist.append(tmp);
bLineFlag=false;
}
}
}
else //文字行向下判断到了一个背景色的行,扫描了一行也没有发现文字颜色,都是背景色
{
tmp.iEnd=y;
int ipicHeight=tmp.iEnd-tmp.iStart;
if(ipicHeight>2)
{
if(ipicHeight>iMaxHeight)
iMaxHeight=ipicHeight;
tmp.bitmap=bigOnePixmap.copy(0,tmp.iStart,bigOnePixmap.width(),tmp.iEnd-tmp.iStart);
m_linelist.append(tmp);
bLineFlag=false;
}
}
}
else {
}
}
}
return iMaxHeight;
}
//生成左右滚动图片素材
void eText::MakeBigMoveLeftOrRightOnePngToDir(QString strPngPathAndName)
{
QPainter *pPainter=new QPainter();
if(pPainter!=nullptr)
{
MACRO_FAN_JUCHI(pPainter)
int iAllPagesTemp=GetPagesPosList();
QTextDocument doc;
// m_attr.opt.setAlignment(Qt::AlignJustify);
m_attr.opt.setAlignment(Qt::AlignLeft);
doc.setDefaultTextOption(m_attr.opt);
doc.setDocumentMargin(0);
//doc.setTextWidth(rect().width()*iAllPagesTemp);
doc.setTextWidth(3800);
QFont aa;
MACRO_FONT_FAN_JUCHI(aa)
doc.setDefaultFont(aa);
const int lineSpacing = QFontMetrics(doc.defaultFont()).lineSpacing();
int lineH = m_attr.lineSpacing + lineSpacing;
QString ctx = QString("<style>p{"
"line-height:%1px;"
"white-space:pre-wrap;"
"}</style>"
"<p>%2</p>")
.arg(lineH)
.arg(m_attr.text);
doc.setHtml(ctx);
//int iwidth=this->rect().toRect().width()*iAllPagesTemp;
int iwidth=3800;
QRectF OutPutBmpRect = QRectF(0, 0, iwidth, doc.size().height());//实际绘制文本的矩形大小
int iheight=OutPutBmpRect.toRect().height();
QPixmap map(iwidth,iheight);
map.fill(m_attr.cBackground);
//map.fill(Qt::blue);
pPainter->begin(&map);
doc.drawContents(pPainter,OutPutBmpRect);
pPainter->end();
delete pPainter;
QPixmap bigOnePixmap=map.copy(OutPutBmpRect.toRect());
//获取
bigOnePixmap.save("f:/a.png","PNG");
m_linelist.clear();
//获取到长图的每页添加到list并返回最大高度
GetLinePng(bigOnePixmap);
//把list中的图片拼接起来
int iMaxHeight=rect().height();
if(m_linelist.count()>0)
{
QBrush bshBackground(m_attr.cBackground);// = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
QList<int> listPageWidth;
int iPicLongSize=0;
for (int j=0;j<m_linelist.count();j++)
{
txtBmpLine tmpLine=m_linelist.at(j);
iPicLongSize=tmpLine.bitmap.width();
// iPicLongSize+=tmpLine.bitmap.width();
// if(iPicLongSize>4096-rect().width())
{
listPageWidth.append(iPicLongSize);
// iPicLongSize=0;
}
}
// if(iPicLongSize!=0)
// {
// listPageWidth.append(iPicLongSize);
// }
int iIOffset=0;
for (int m=0;m<listPageWidth.count();m++)
{
int itempWidth=listPageWidth.at(m);
QPixmap bigOneLongPixmap(itempWidth,iMaxHeight);
bigOneLongPixmap.fill(m_attr.cBackground);
QPainter *pPainterTemp=new QPainter();
MACRO_FAN_JUCHI(pPainterTemp)
pPainterTemp->begin(&bigOneLongPixmap);
iPicLongSize=0;
for (int i=iIOffset;i<m_linelist.count();i++)
{
txtBmpLine tmpLine=m_linelist.at(i);
int itempHeight=tmpLine.bitmap.height();//tmpLine.iEnd-tmpLine.iStart;
iPicLongSize+=tmpLine.bitmap.width();
if(bshBackground != Qt::NoBrush)
{
int iTopOffset=(iMaxHeight-itempHeight)/2;
pPainterTemp->fillRect((i-iIOffset)*iwidth,0,rect().width(),iTopOffset, bshBackground);
pPainterTemp->fillRect((i-iIOffset)*iwidth,iTopOffset+itempHeight,rect().width(),iMaxHeight-iTopOffset-itempHeight, bshBackground);
}
QRectF rectDest((i-iIOffset)*iwidth,(iMaxHeight-itempHeight)/2,iwidth,itempHeight);
pPainterTemp->drawPixmap(rectDest,tmpLine.bitmap,tmpLine.bitmap.rect());
if(iPicLongSize==itempWidth)
{
pPainterTemp->end();
delete pPainterTemp;
if(m==listPageWidth.count()-1)
{
//如果启用了首位相接,根据间隔值处理吧文字后边的黑边流出来。
if(m_attr.stRolloing.headTailConnected)
{
int itempSpace=GetWordSpaceRealInBmp(bigOneLongPixmap);
ProcessLastStartEndLianJie(bigOneLongPixmap,strPngPathAndName+QString::number(m)+".png",itempSpace+m_attr.stRolloing.headTailSpacing);
}
else {
int itempSpace=GetWordSpaceRealInBmp(bigOneLongPixmap);
ProcessLastStartEndLianJie(bigOneLongPixmap,strPngPathAndName+QString::number(m)+".png",itempSpace);
//bigOneLongPixmap.save(strPngPathAndName+QString::number(m)+".png","PNG");
}
}
else {
int itempSpace=GetWordSpaceRealInBmp(bigOneLongPixmap);
ProcessLastStartEndLianJie(bigOneLongPixmap,strPngPathAndName+QString::number(m)+".png",itempSpace);
//bigOneLongPixmap.save(strPngPathAndName+QString::number(m)+".png","PNG");
}
iIOffset=i+1;
break;
}
else {
pPainterTemp->end();
delete pPainterTemp;
}
}
}
}
}
}
void eText::ProcessLastStartEndLianJie(QPixmap bmp,QString strPngFilePathName,int iLastRightNoWordWidth)
{
QImage tempQImage=bmp.toImage();
// m_ImageBkColor=tempQImage.pixelColor(0,0);
int iWidth= bmp.width();
int iLastX=iWidth;
for (int x=iWidth-1;x>0;x--)
{
for(int y=0;y<bmp.height();y++)
{
QColor bb=tempQImage.pixelColor(x,y);
if(bb!=m_ImageBkColor)
{
iLastX=x;
QPixmap returnBmp(iLastX+1+iLastRightNoWordWidth,bmp.height());
returnBmp.fill(m_ImageBkColor);
QPainter *pPainterTemp=new QPainter();
MACRO_FAN_JUCHI(pPainterTemp)
pPainterTemp->begin(&returnBmp);
pPainterTemp->drawPixmap(bmp.rect(),bmp,bmp.rect());
pPainterTemp->end();
if(m_attr.stRolloing.headTailConnected)
{
if(returnBmp.width()>=rect().width())//如果图片的宽大于区域的宽
{
returnBmp.save(strPngFilePathName,"PNG");
}
else {
int iBeiShu=rect().width()/returnBmp.width();
int iTempWidth=returnBmp.width();
QPixmap returnNewBmp(iTempWidth*(iBeiShu+1),bmp.height());
returnNewBmp.fill(m_ImageBkColor);
QPainter painter;
painter.begin(&returnNewBmp);
for (int ii=0;ii<iBeiShu+1;ii++) {
QRect tempRc(ii*iTempWidth,0,iTempWidth,returnBmp.height());
painter.drawPixmap(tempRc,returnBmp,returnBmp.rect());
}
returnNewBmp.save(strPngFilePathName,"PNG");
}
}
else {
if(returnBmp.width()>=rect().width())//如果图片的宽大于区域的宽
{
returnBmp.save(strPngFilePathName,"PNG");
}
else {
int iTempWidth=returnBmp.width();
QPixmap returnNewBmp(rect().width(),bmp.height());
returnNewBmp.fill(m_ImageBkColor);
QPainter painter;
painter.begin(&returnNewBmp);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
painter.fillRect(returnNewBmp.rect(),bshBackground);
QRect tempRc(0,0,iTempWidth,returnBmp.height());
painter.drawPixmap(tempRc,returnBmp,returnBmp.rect());
returnNewBmp.save(strPngFilePathName,"PNG");
}
}
delete pPainterTemp;
return;
}
}
}
}
int eText::GetWordSpaceRealInBmp(QPixmap bmp)
{
QImage tempQImage=bmp.toImage();
// m_ImageBkColor=tempQImage.pixelColor(0,0);
int iWidth= bmp.width();
int iLastX=iWidth;
int iLastX1=iWidth;
int iCount=0;
for (int x=iWidth-1;x>0;x--)
{
int iAllSameFlag=0;
for(int y=0;y<bmp.height();y++)
{
QColor bb=tempQImage.pixelColor(x,y);
if(bb!=m_ImageBkColor)
{
iAllSameFlag=1;
}
if(iCount==0)
{
if(bb!=m_ImageBkColor)
{
iCount=1;
break;
}
}
else if(iCount==2)
{
if(bb!=m_ImageBkColor)
{
iCount=3;
break;
}
}
}
if(iCount==1)
{
if(iAllSameFlag==0)
{
iLastX=x;
iCount=2;
}
}
else if(iCount==3)
{
iLastX1=x;
return iLastX-iLastX1;
}
}
return 1;
}
//生成上下滚动图片素材
void eText::MakeBigMoveUpOrDownOnePngToDir(QString strPngPathAndName)
{
QPainter *pPainter=new QPainter();
if(pPainter!=nullptr)
{
MACRO_FAN_JUCHI(pPainter)
QTextDocument doc;
doc.setDefaultTextOption(m_attr.opt);
doc.setDocumentMargin(0);
doc.setTextWidth(rect().width());
QFont aa;
MACRO_FONT_FAN_JUCHI(aa)
doc.setDefaultFont(aa);
const int lineSpacing = QFontMetrics(doc.defaultFont()).lineSpacing();
int lineH = m_attr.lineSpacing + lineSpacing;
QString ctx = QString("<style>p{"
"line-height:%1px;"
"white-space:pre-wrap;"
"}</style>"
"<p>%2</p>")
.arg(lineH)
.arg(m_attr.text);
doc.setHtml(ctx);
int iwidth=this->rect().toRect().width();
QRectF OutPutBmpRect = QRectF(0, 0, iwidth, doc.size().height());//实际绘制文本的矩形大小
int iheight=OutPutBmpRect.toRect().height();
QPixmap map(iwidth,iheight);
map.fill(m_attr.cBackground);
pPainter->begin(&map);
doc.drawContents(pPainter,OutPutBmpRect);
pPainter->end();
QPixmap *pRenderPixmap=new QPixmap(rect().width(),rect().height());
pRenderPixmap->fill(m_attr.cBackground);
pPainter->begin(pRenderPixmap);
if(rect().height()>map.height())
{
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_attr.cBackground);
if(bshBackground != Qt::NoBrush) {
pPainter->fillRect(0,map.height(),rect().width(),rect().height()-map.height(), bshBackground);
}
QRectF rectDest(0,0,rect().width(),rect().height());
QRectF rectSource(0,0,rect().width(),map.height());
pPainter->drawPixmap(map.rect(),map,map.rect());
pRenderPixmap->save(strPngPathAndName);
}
else {
QRectF rectSource(0,0,rect().width(),rect().height());
pPainter->drawPixmap(rect(),map,rectSource);
map.copy(OutPutBmpRect.toRect()).save(strPngPathAndName);
}
pPainter->end();
}
delete pPainter;
}
//生成翻页和静态图片素材
void eText::MakeMuliPngToDir(int iPageIndex,QString strPngPathAndName)
{
iPageIndex=iPageIndex-1;
if(iPageIndex<0)
iPageIndex=0;
if(iPageIndex>=0)
{
if(m_list.count()>0&&iPageIndex<m_list.count())
{
QPixmap *tempPixamp=new QPixmap(rect().width(),rect().height());
tempPixamp->fill(m_ImageBkColor);
int res = m_attr.opt.alignment();
QPixmap tmp=m_list.at(iPageIndex).bitmap;
if((res & Qt::Alignment::enum_type::AlignTop)!=0)
{
QPainter painter;
painter.begin(tempPixamp);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_ImageBkColor);
if(bshBackground != Qt::NoBrush) {
painter.fillRect(0,tmp.height(),tmp.width(),rect().height()-tmp.height(), bshBackground);
}
painter.drawPixmap(0,0,tmp.width(),tmp.height(),tmp);
painter.end();
}
else if((res & Qt::Alignment::enum_type::AlignBottom)!=0)
{
QPainter painter;
painter.begin(tempPixamp);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_ImageBkColor);
if(bshBackground != Qt::NoBrush) {
painter.fillRect(0,0,tmp.width(),rect().height()-tmp.height(), bshBackground);
}
painter.drawPixmap(0,rect().height()-tmp.height(),tmp.width(),tmp.height(),tmp);
painter.end();
}
else
{
QPainter painter;
painter.begin(tempPixamp);
QBrush bshBackground = LoAppTools::getInstance()->getBrush(m_ImageBkColor);
int iTopOffset=(rect().height()-tmp.height())/2;
if(bshBackground != Qt::NoBrush) {
painter.fillRect(0,0,tmp.width(),iTopOffset, bshBackground);
painter.fillRect(0,iTopOffset+tmp.height(),tmp.width(),rect().height()-iTopOffset-tmp.height() ,bshBackground);
}
painter.drawPixmap(0,iTopOffset,tmp.width(),tmp.height(),tmp);
painter.end();
}
tempPixamp->save(strPngPathAndName,"PNG");
delete tempPixamp;
}
}
else {
}
}
void eText::onTurningEffectChanged(QString strEffect)
{
m_attr.stTurning.strEffect = strEffect;
updateGeometry();
}