#include "etext.h" #include "etextattr.h" #include "etextinput.h" #include #include #include #include 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 = "\n\n

"+tr("Hello Text !")+"

"; 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(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(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; } QJsonObject 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_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;ip{" "line-height:%1px;" "white-space:pre-wrap;" "}" "

%2

") .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(); LoQGraphicsObject::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(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(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=0;m--) { for (int n=0;n1) { 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=0;y--) { bool bFinished=false; for (int x=0;x=0;y--) { bool bFinished=false; for (int x=0;xiRectHeight) 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=0) { if(m_list.count()>0&&iPageIndexfill(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("" "

%2

") .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("" "

%2

") .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;y2) { 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("" "

%2

") .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 listPageWidth; int iPicLongSize=0; for (int j=0;j4096-rect().width()) { listPageWidth.append(iPicLongSize); // iPicLongSize=0; } } // if(iPicLongSize!=0) // { // listPageWidth.append(iPicLongSize); // } int iIOffset=0; for (int m=0;mbegin(&bigOneLongPixmap); iPicLongSize=0; for (int i=iIOffset;ifillRect((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;ybegin(&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=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;yp{" "line-height:%1px;" "white-space:pre-wrap;" "}" "

%2

") .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&&iPageIndexfill(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(); }