1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

使用Rect的QQtWord

This commit is contained in:
AbelTian 2018-07-30 12:32:36 +08:00
parent f321fff8e8
commit a1c9c3b8d0
5 changed files with 367 additions and 290 deletions

View File

@ -1,91 +1,22 @@
#include "qqt-qt.h"
#include "qqt-qt.h"
#include "qqtcore.h"
#include "qqtprinter.h"
#include "qqtgraphicsscene.h"
#include "qqtgraphicsitem.h"
#include "qqtword.h"
QQtWord::QQtWord(QObject* parent) :
QObject(parent),
fmt(0), headerFmt(0), titleFmt(0), title2Fmt(0), mainFmt(0)
QQtWord::QQtWord ( QObject* parent ) :
QObject ( parent ),
fmt ( 0 ), headerFmt ( 0 ), titleFmt ( 0 ), title2Fmt ( 0 ), mainFmt ( 0 )
{
//setup printer
/**
MIPS bug: you must use QQtPrinter(QPrinter::HighResolution)
Other constructer won't work well
*/
pr = new QQtPrinter(QPrinter::HighResolution);
pr->setFullPage(true);
pr->setColorMode(QPrinter::Color);
pr->setPaperSize(QPrinter::A4);
pr->setOrientation(QPrinter::Portrait);
pr->setOutputFormat(QPrinter::PdfFormat);
logicalDpiX = logicalDpiY = 300;
sceneRect = QRectF ( 0, 0, 2480, 3508 );
/*
* Font目标是打印纸上
* Pos目标不是纸上的DPI
* 使
* View 142,138 //PCView 96 //打印机View 1200
* 1200
* DrawText可用空间减小甚至切掉一部分
*/
#ifdef __EMBEDDED_LINUX__
/*
*
*/
logicalDpiX = 136;
logicalDpiY = 156;
#else
/*
*
*/
logicalDpiX = 96;
logicalDpiY = 96;
#endif
QRect rect = pr->paperRect();
sceneRect = QRectF(0.0, 0.0, logicalDpiX * rect.width() / pr->logicalDpiX(),
logicalDpiY * rect.height() / pr->logicalDpiY());
#if 0
//1200 9917,14033 printerRect 固定
//116 958,1356 sceneRect
//142 1113,1660 sceneRect
pline() << pr->logicalDpiX() << pr->logicalDpiY();
pline() << logicalDpiX << logicalDpiY << pr->pageRect() << sceneRect;
pline() << pr->paperRect(QPrinter::DevicePixel);
pline() << pr->paperRect(QPrinter::Millimeter);
pline() << pr->paperRect(QPrinter::Point);
pline() << pr->paperRect(QPrinter::Inch);
pline() << pr->paperRect(QPrinter::Pica);
pline() << pr->paperRect(QPrinter::Didot);
pline() << pr->paperRect(QPrinter::Cicero);
#endif
m_mainFont = QApplication::font();
if (mainFmt)
delete mainFmt;
mainFmt = new QFontMetrics(m_mainFont);
m_titleFont = QApplication::font();
m_titleFont.setPointSize(22);
if (titleFmt)
delete titleFmt;
titleFmt = new QFontMetrics(m_titleFont);
m_title2Font = QApplication::font();;
m_title2Font.setPointSize(16);
if (title2Fmt)
delete title2Fmt;
title2Fmt = new QFontMetrics(m_title2Font);
initConstFont();
setMargin();
setHeaderSize();
setFooterSize();
setHeaderHeight();
setFooterHeight();
setFont();
setHeaderFont();
@ -97,40 +28,41 @@ QQtWord::QQtWord(QObject* parent) :
initWord();
}
void QQtWord::setMargin(qreal left, qreal right, qreal top, qreal botoom)
void QQtWord::setMargin ( qreal left, qreal right, qreal top, qreal botoom )
{
leftMargin = 134.6;
rightMargin = 134.6;
topMargin = 177.7;
bottomMargin = 177.7;
leftMargin = left;
rightMargin = right;
topMargin = top;
bottomMargin = botoom;
}
QRectF QQtWord::clientRectF()
{
return QRectF(leftMargin, topMargin,
sceneRect.width() - leftMargin - rightMargin,
sceneRect.height() - topMargin - bottomMargin);
return QRectF ( leftMargin, topMargin,
sceneRect.width() - leftMargin - rightMargin,
sceneRect.height() - topMargin - bottomMargin );
}
QRectF QQtWord::paperRect()
{
return QRectF(0, 0,
sceneRect.width(),
sceneRect.height());
return QRectF ( 0, 0,
sceneRect.width(),
sceneRect.height() );
}
void QQtWord::setFont(QFont font)
void QQtWord::setFont ( QFont font )
{
//normal font 11
m_font = QApplication::font();
if ( font == QFont() )
font = QApplication::font();
if (fmt)
m_font = font;
if ( fmt )
delete fmt;
fmt = new QFontMetrics(m_font);
fmt = new QFontMetrics ( m_font );
}
void QQtWord::setLineSpacing(qreal spacing)
void QQtWord::setLineSpacing ( qreal spacing )
{
/*
*
@ -146,168 +78,168 @@ void QQtWord::setLineSpacing(qreal spacing)
headerSpacing = headerFmt->height() * 1;
}
void QQtWord::addText(const QString& text, QFont font, Qt::Alignment align, QPointF point)
void QQtWord::addText ( const QString& text, QFont font, Qt::Alignment align, QPointF point )
{
QFontMetrics fmt = QFontMetrics(font);
QFontMetrics fmt = QFontMetrics ( font );
int spacing = fmt.height() * 2;
int height = fmt.height();
int width = fmt.width(text);
int width = fmt.width ( text );
//pline() << font.pointSize() << fmt.height();
adjustdy(height + spacing);
adjustdy ( height + spacing );
QQtGraphicsTextItem* item = pageScene->addText(text, font);
QQtGraphicsTextItem* item = pageScene->addText ( text, font );
if (align & Qt::AlignLeft)
item->moveBy(dx, dy + height);
else if (align & Qt::AlignRight)
item->moveBy(xpos2 - width, dy + height);
else if (align & Qt::AlignHCenter)
item->moveBy((pageScene->width() / 2) - (width / 2), dy + height);
if ( align & Qt::AlignLeft )
item->moveBy ( dx, dy + height );
else if ( align & Qt::AlignRight )
item->moveBy ( xpos2 - width, dy + height );
else if ( align & Qt::AlignHCenter )
item->moveBy ( ( pageScene->width() / 2 ) - ( width / 2 ), dy + height );
dy += height + spacing;
}
void QQtWord::addSignoffText(const QString& text, QFont font)
void QQtWord::addSignoffText ( const QString& text, QFont font )
{
adjustdy(mainHeight + mainSpacing);
adjustdy ( mainHeight + mainSpacing );
int ddy = dy;
dy = ypos2 - ((mainHeight + mainSpacing) * 2);
dy = ypos2 - ( ( mainHeight + mainSpacing ) * 2 );
addText(text, m_mainFont, Qt::AlignRight);
addText ( text, m_mainFont, Qt::AlignRight );
dy = ddy;
}
void QQtWord::addTable(const QTableView* table, QPointF pos)
void QQtWord::addTable ( const QTableView* table, QPointF pos )
{
Q_ASSERT(table);
Q_ASSERT ( table );
QAbstractItemModel* model = table->model();
QFont tableFont = table->font();
QFontMetrics tableFmt = QFontMetrics(tableFont);
QFontMetrics tableFmt = QFontMetrics ( tableFont );
int tableRowHeight = 0;
if (!table->horizontalHeader()->isHidden())
if ( !table->horizontalHeader()->isHidden() )
{
tableRowHeight = table->horizontalHeader()->height();
adjustdy(tableRowHeight);
adjustdy ( tableRowHeight );
for (int col = 0; col < model->columnCount(); col++)
for ( int col = 0; col < model->columnCount(); col++ )
{
int logicalIndex = table->horizontalHeader()->logicalIndex(col);
int actColSize = table->columnWidth(logicalIndex);
int logicalIndex = table->horizontalHeader()->logicalIndex ( col );
int actColSize = table->columnWidth ( logicalIndex );
if (table->horizontalHeader()->isSectionHidden(col))
if ( table->horizontalHeader()->isSectionHidden ( col ) )
continue;
QPen pen(Qt::gray, 0.1);
QBrush brush(QColor(255, 250, 250));
QPen pen ( Qt::gray, 0.1 );
QBrush brush ( QColor ( 255, 250, 250 ) );
//QBrush brush(tableView->horizontalHeader()->palette().background());
pageScene->addRect(dx, dy, actColSize, tableRowHeight, pen, brush);
pageScene->addRect ( dx, dy, actColSize, tableRowHeight, pen, brush );
QString txt = model->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString();
txt = tableFmt.elidedText(txt, Qt::ElideRight, actColSize - 2);
QQtGraphicsTextItem* item = pageScene->addText(txt, tableFont);
item->moveBy(dx, dy);
QString txt = model->headerData ( logicalIndex, Qt::Horizontal, Qt::DisplayRole ).toString();
txt = tableFmt.elidedText ( txt, Qt::ElideRight, actColSize - 2 );
QQtGraphicsTextItem* item = pageScene->addText ( txt, tableFont );
item->moveBy ( dx, dy );
dx += actColSize;
}
dy += tableRowHeight;
}
QHash<int, ESpanFlags> spans = tableSpans(table);
QHashIterator<int, ESpanFlags> it(spans);
QHash<int, ESpanFlags> spans = tableSpans ( table );
QHashIterator<int, ESpanFlags> it ( spans );
while (0 && it.hasNext())
while ( 0 && it.hasNext() )
{
it.next();
pline() << it.key() << it.value();
pline() << it.value().testFlag(ESpanLeft) <<
it.value().testFlag(ESpanTop) <<
it.value().testFlag(ESpanRight) <<
it.value().testFlag(ESpanBottom) <<
it.value().testFlag(ESpanMiddle) ;
pline() << it.value().testFlag ( ESpanLeft ) <<
it.value().testFlag ( ESpanTop ) <<
it.value().testFlag ( ESpanRight ) <<
it.value().testFlag ( ESpanBottom ) <<
it.value().testFlag ( ESpanMiddle ) ;
}
//Table rows
QPen pen(Qt::gray, 0.1);
QBrush brush(Qt::gray, Qt::SolidPattern);
QPen pen ( Qt::gray, 0.1 );
QBrush brush ( Qt::gray, Qt::SolidPattern );
int row = 0;
for (;;)
for ( ;; )
{
if (row >= model->rowCount())
if ( row >= model->rowCount() )
{
break;
}
if (table->isRowHidden(row))
if ( table->isRowHidden ( row ) )
continue;
tableRowHeight = table->rowHeight(row);
adjustdy(tableRowHeight);
tableRowHeight = table->rowHeight ( row );
adjustdy ( tableRowHeight );
for (int col = 0; col < model->columnCount(); col++)
for ( int col = 0; col < model->columnCount(); col++ )
{
int logicalIndex = table->horizontalHeader()->logicalIndex(col);
int actColSize = table->columnWidth(logicalIndex);
int logicalIndex = table->horizontalHeader()->logicalIndex ( col );
int actColSize = table->columnWidth ( logicalIndex );
if (table->isColumnHidden(col))
if ( table->isColumnHidden ( col ) )
continue;
int point = row * model->columnCount() + col;
ESpanFlags flags = spans.value(point);
ESpanFlags flags = spans.value ( point );
QPen pen(Qt::gray, 0.1);
QBrush brush(table->palette().window());
QPen pen ( Qt::gray, 0.1 );
QBrush brush ( table->palette().window() );
bool balt = table->alternatingRowColors();
if (ESpanNone == flags)
if ( ESpanNone == flags )
{
if (balt)
if ( balt )
{
int modulo = row % 2;
if (modulo != 0)
if ( modulo != 0 )
{
//rectangle grey
pageScene->addRect(dx, dy, actColSize, tableRowHeight, pen, brush);
pageScene->addRect ( dx, dy, actColSize, tableRowHeight, pen, brush );
}
else
{
pageScene->addRect(dx, dy, actColSize, tableRowHeight, pen);
pageScene->addRect ( dx, dy, actColSize, tableRowHeight, pen );
}
}
else
{
pageScene->addRect(dx, dy, actColSize, tableRowHeight, pen);
pageScene->addRect ( dx, dy, actColSize, tableRowHeight, pen );
}
}
if (flags.testFlag(ESpanLeft))
pageScene->addLine(dx, dy, dx, dy + tableRowHeight, pen);
if ( flags.testFlag ( ESpanLeft ) )
pageScene->addLine ( dx, dy, dx, dy + tableRowHeight, pen );
if (flags.testFlag(ESpanTop))
pageScene->addLine(dx, dy, dx + actColSize, dy, pen);
if ( flags.testFlag ( ESpanTop ) )
pageScene->addLine ( dx, dy, dx + actColSize, dy, pen );
if (flags.testFlag(ESpanRight))
pageScene->addLine(dx + actColSize, dy, dx + actColSize, dy + tableRowHeight, pen);
if ( flags.testFlag ( ESpanRight ) )
pageScene->addLine ( dx + actColSize, dy, dx + actColSize, dy + tableRowHeight, pen );
if (flags.testFlag(ESpanBottom))
pageScene->addLine(dx, dy + tableRowHeight, dx + actColSize, dy + tableRowHeight, pen);
if ( flags.testFlag ( ESpanBottom ) )
pageScene->addLine ( dx, dy + tableRowHeight, dx + actColSize, dy + tableRowHeight, pen );
if (ESpanNone == flags ||
(flags.testFlag(ESpanLeft) && flags.testFlag(ESpanTop)))
if ( ESpanNone == flags ||
( flags.testFlag ( ESpanLeft ) && flags.testFlag ( ESpanTop ) ) )
{
QString txt = model->data(model->index(row, logicalIndex)).toString();
txt = tableFmt.elidedText(txt, Qt::ElideRight, actColSize - 2);
QQtGraphicsTextItem* item = pageScene->addText(txt, tableFont);
item->moveBy(dx, dy);
QString txt = model->data ( model->index ( row, logicalIndex ) ).toString();
txt = tableFmt.elidedText ( txt, Qt::ElideRight, actColSize - 2 );
QQtGraphicsTextItem* item = pageScene->addText ( txt, tableFont );
item->moveBy ( dx, dy );
}
dx += actColSize;
@ -321,95 +253,101 @@ void QQtWord::addTable(const QTableView* table, QPointF pos)
int QQtWord::pageNum() { return pageSceneVector.size(); }
void QQtWord::exportPdf(const QString& pdf)
QQtGraphicsScene* QQtWord::getPage ( int num )
{
// setup printer
pr->setOutputFileName(pdf);
// print pdf
QPainter p(pr);
QQtGraphicsScene* pageScene = 0;
foreach (pageScene, pageSceneVector)
{
pageScene->render(&p, pr->paperRect(), sceneRect);
if (pageScene != pageSceneVector.last())
pr->newPage();
}
}
QQtGraphicsScene* QQtWord::getPage(int num)
{
if (num < 1 || num > pageSceneVector.size())
if ( num < 1 || num > pageSceneVector.size() )
return NULL;
return pageSceneVector.at(num - 1);
return pageSceneVector.at ( num - 1 );
}
void QQtWord::print()
{
pr->print();
}
void QQtWord::setHeaderFont(QFont font)
void QQtWord::setHeaderFont ( QFont font )
{
//header font
m_headerFont = QApplication::font();;
m_headerFont.setPointSize(9);
m_headerFont.setPointSize ( 9 );
if (headerFmt)
if ( headerFmt )
delete headerFmt;
headerFmt = new QFontMetrics(m_headerFont);
headerFmt = new QFontMetrics ( m_headerFont );
}
void QQtWord::setHeaderSize(qreal size)
void QQtWord::setHeaderHeight ( qreal size )
{
headerSize = 70;
headerSize = size;
}
void QQtWord::setHeaderLine(bool show)
void QQtWord::setHeaderLine ( bool show )
{
}
void QQtWord::setHeaderText(const QString& text, QFont font, Qt::Alignment align)
void QQtWord::setHeaderText ( const QString& text, QFont font, Qt::Alignment align )
{
headerText = text;
paintPageHeader();
}
void QQtWord::setFooterSize(qreal size)
void QQtWord::setFooterFont ( QFont font )
{
footerSize = 70;
setHeaderFont ( font );
}
void QQtWord::setFooterHeight ( qreal size )
{
footerSize = size;
}
QFont QQtWord::font() { return m_font; }
void QQtWord::setFooterLine(bool show)
void QQtWord::setFooterLine ( bool show )
{
Q_UNUSED(show)
Q_UNUSED ( show )
}
void QQtWord::setFooterText(const QString& text, QFont font, Qt::Alignment align)
void QQtWord::setFooterText ( const QString& text, QFont font, Qt::Alignment align )
{
Q_UNUSED(font)
Q_UNUSED(align)
Q_UNUSED ( font )
Q_UNUSED ( align )
footerText = text;
paintPageFooter();
}
void QQtWord::exportImage ( QList<QImage>& targetPapers, QRectF targetRect )
{
while ( !targetPapers.isEmpty() )
targetPapers.removeAt ( 0 );
if ( targetRect == QRectF() )
targetRect = sceneRect;
QQtGraphicsScene* pageScene = 0;
foreach ( pageScene, pageSceneVector )
{
QImage page ( targetRect.size().toSize(), QImage::Format_ARGB32 );
QPainter p ( &page );
//我写的这个类,QQtGraphicsScene这个类,还是有问题的,不能对改变了DPI的整体缩放.
//但是!这里可以对改变了DPI的进行清晰化和模糊化!
#if 1
p.scale ( targetRect.width() / sceneRect.width(),
targetRect.height() / sceneRect.height() );
pageScene->render ( &p, sceneRect, sceneRect );
#else
pageScene->render ( &p, targetRect, sceneRect );
#endif
targetPapers.push_back ( page );
}
}
void QQtWord::initWord()
{
while (! pageSceneVector.isEmpty())
while ( ! pageSceneVector.isEmpty() )
{
QQtGraphicsScene* pageScene = pageSceneVector.first();
pageScene->clear();
delete pageScene;
pageSceneVector.remove(0);
pageSceneVector.remove ( 0 );
}
headerText = "";
@ -417,11 +355,11 @@ void QQtWord::initWord()
createFrame();
}
void QQtWord::adjustdy(qreal dy0)
void QQtWord::adjustdy ( qreal dy0 )
{
dx = xpos;
if (dy + dy0 < ypos2)
if ( dy + dy0 < ypos2 )
return;
createFrame();
@ -437,8 +375,8 @@ void QQtWord::createFrame()
dx = xpos;
dy = ypos;
pageScene = new QQtGraphicsScene(sceneRect);
pageSceneVector.append(pageScene);
pageScene = new QQtGraphicsScene ( sceneRect );
pageSceneVector.append ( pageScene );
paintPageHeader();
paintPageFooter();
}
@ -446,7 +384,7 @@ void QQtWord::createFrame()
void QQtWord::paintPageHeader()
{
// Page header
if (headerText.isEmpty())
if ( headerText.isEmpty() )
return;
int sx = xpos;
@ -454,26 +392,26 @@ void QQtWord::paintPageHeader()
/*
*
*/
QQtGraphicsTextItem* headerItem = pageScene->addText(headerText, m_headerFont);
headerItem->moveBy(sx, sy);
QQtGraphicsTextItem* headerItem = pageScene->addText ( headerText, m_headerFont );
headerItem->moveBy ( sx, sy );
//std text
QString date = QDate::currentDate().toString(QLocale().dateFormat());
QString time = QTime::currentTime().toString(QLocale().timeFormat(QLocale::ShortFormat));
QString date = QDate::currentDate().toString ( QLocale().dateFormat() );
QString time = QTime::currentTime().toString ( QLocale().timeFormat ( QLocale::ShortFormat ) );
QString headerStdText;
headerStdText = date + " " + time;
QQtGraphicsTextItem* item = pageScene->addText(headerStdText, m_headerFont);
item->moveBy(xpos2 - headerFmt->width(headerStdText), sy);
QQtGraphicsTextItem* item = pageScene->addText ( headerStdText, m_headerFont );
item->moveBy ( xpos2 - headerFmt->width ( headerStdText ), sy );
sy += headerItem->boundingRect().height();
//line
pageScene->addLine(xpos, sy, xpos2, sy, QPen(Qt::black, 1.0));
pageScene->addLine ( xpos, sy, xpos2, sy, QPen ( Qt::black, 1.0 ) );
}
void QQtWord::paintPageFooter()
{
if (footerText.isEmpty())
if ( footerText.isEmpty() )
return;
/*
@ -481,21 +419,21 @@ void QQtWord::paintPageFooter()
*/
int sx = xpos;
QString footerStdText = tr("Page") + QString::number(pageSceneVector.size()) + tr(" ");
QQtGraphicsTextItem* item = pageScene->addText(footerStdText, m_headerFont);
QString footerStdText = tr ( "Page " ) + QString::number ( pageSceneVector.size() ) + tr ( " " );
QQtGraphicsTextItem* item = pageScene->addText ( footerStdText, m_headerFont );
int height = item->boundingRect().height();
int sy = ypos2 + footerSize - height;
item->moveBy(xpos2 - headerFmt->width(footerStdText), sy);
item->moveBy ( xpos2 - headerFmt->width ( footerStdText ), sy );
pageScene->addLine(xpos, sy, xpos2, sy, QPen(Qt::black, 1.0));
pageScene->addLine ( xpos, sy, xpos2, sy, QPen ( Qt::black, 1.0 ) );
QQtGraphicsTextItem* footerItem = pageScene->addText(footerText, m_headerFont);
footerItem->moveBy(xpos, sy);
QQtGraphicsTextItem* footerItem = pageScene->addText ( footerText, m_headerFont );
footerItem->moveBy ( xpos, sy );
}
QHash<int, ESpanFlags> QQtWord::tableSpans(const QTableView* table)
QHash<int, ESpanFlags> QQtWord::tableSpans ( const QTableView* table )
{
Q_ASSERT(table);
Q_ASSERT ( table );
QAbstractItemModel* model = table->model();
int colCount = model->columnCount();
@ -503,65 +441,65 @@ QHash<int, ESpanFlags> QQtWord::tableSpans(const QTableView* table)
QHash<int, ESpanFlags> spans;
for (int row = 0; row < rowCount; row++)
for ( int row = 0; row < rowCount; row++ )
{
for (int col = 0; col < colCount; col++)
for ( int col = 0; col < colCount; col++ )
{
int rowSpan = table->rowSpan(row, col);
int colSpan = table->columnSpan(row, col);
int rowSpan = table->rowSpan ( row, col );
int colSpan = table->columnSpan ( row, col );
int point = (row) * colCount + (col);
int point = ( row ) * colCount + ( col );
ESpanFlags flags = ESpanNone;
/*
*
*/
if (rowSpan == 1 && colSpan == 1)
if ( rowSpan == 1 && colSpan == 1 )
{
spans.insert(point, flags);
spans.insert ( point, flags );
continue;
}
for (int i = 0; i < rowSpan; i++)
for ( int i = 0; i < rowSpan; i++ )
{
point = (row + i) * colCount + col + 0;
point = ( row + i ) * colCount + col + 0;
/*
* SpanSpans已经赋值break
*/
if (ESpanNone != spans.value(point, ESpanNone))
if ( ESpanNone != spans.value ( point, ESpanNone ) )
break;
for (int j = 0; j < colSpan; j++)
for ( int j = 0; j < colSpan; j++ )
{
point = (row + i) * colCount + col + j;
point = ( row + i ) * colCount + col + j;
/*
* SpanSpans已经赋值break
*/
if (ESpanNone != spans.value(point, ESpanNone))
if ( ESpanNone != spans.value ( point, ESpanNone ) )
break;
ESpanFlags flags = ESpanNone;
if (i == 0)
flags |= ESpanFlags(ESpanTop);
if ( i == 0 )
flags |= ESpanFlags ( ESpanTop );
if (i == rowSpan - 1)
flags |= ESpanFlags(ESpanBottom);
if ( i == rowSpan - 1 )
flags |= ESpanFlags ( ESpanBottom );
if (j == 0)
flags |= ESpanFlags(ESpanLeft);
if ( j == 0 )
flags |= ESpanFlags ( ESpanLeft );
if (j == colSpan - 1)
flags |= ESpanFlags(ESpanRight);
if ( j == colSpan - 1 )
flags |= ESpanFlags ( ESpanRight );
if (i != 0 && j != 0 && i != rowSpan - 1 && j != colSpan - 1)
if ( i != 0 && j != 0 && i != rowSpan - 1 && j != colSpan - 1 )
flags |= ESpanMiddle;
spans.insert(point, flags);
spans.insert ( point, flags );
}
}
}
@ -569,3 +507,26 @@ QHash<int, ESpanFlags> QQtWord::tableSpans(const QTableView* table)
return spans;
}
void QQtWord::initConstFont()
{
//mainFont 正文
m_mainFont = QApplication::font();
if ( mainFmt )
delete mainFmt;
mainFmt = new QFontMetrics ( m_mainFont );
//标题
m_titleFont = QApplication::font();
m_titleFont.setPointSize ( 22 );
if ( titleFmt )
delete titleFmt;
titleFmt = new QFontMetrics ( m_titleFont );
//标题2
m_title2Font = QApplication::font();;
m_title2Font.setPointSize ( 16 );
if ( title2Fmt )
delete title2Fmt;
title2Fmt = new QFontMetrics ( m_title2Font );
}

View File

@ -29,6 +29,7 @@ Q_DECLARE_FLAGS ( ESpanFlags, ESpanFlag )
* , , .
* ,
* Widget,
* ,.
*
* DPI,.
* Rect1 : DPI1 = Rect2 : DPI2
@ -38,12 +39,13 @@ Q_DECLARE_FLAGS ( ESpanFlags, ESpanFlag )
* DWin屏幕(View) 800*600 136(x) 156(y) 142 138
* PC屏幕(View) 1920*1080 96 96
* Printer 9917*14033 1200
* Image 1920*1080 96 96
*
* Scene可以设置纸张类型, A3 DPI不变 SceneRect会变.Rect像素大小,PS.
*
*
* 1. ,,,.
*
* 2. Scene使用默认的分辨率300, rect根据纸的类型而变化.
*/
class QQTSHARED_EXPORT QQtWord : public QObject
{
@ -55,6 +57,9 @@ public:
* @brief initWord
*/
void initWord();
/**
* @brief addText
*/
void addText ( const QString& text, QFont m_font = QFont(),
Qt::Alignment align = Qt::AlignHCenter, QPointF point = QPointF ( 0, 0 ) );
void addTable ( const QTableView* table, QPointF pos = QPointF ( 0, 0 ) );
@ -65,28 +70,60 @@ public:
QRectF clientRectF();
QRectF paperRect();
void setMargin ( qreal left = 0, qreal right = 0,
qreal top = 0, qreal botoom = 0 );
void setLineSpacing ( qreal mainSpacing = 0 );
void setHeaderSize ( qreal size = 0 );
void setFooterSize ( qreal size = 0 );
//单位:像素. 可以结合Adobe PhotoShop和WPS Word进行计算.
void setMargin ( qreal left = 375.5898, qreal right = 375.5898,
qreal top = 299.9994, qreal botoom = 299.9994 );
void setLineSpacing ( qreal mainSpacing = 0 );
void setHeaderHeight ( qreal size = 195.32 );
void setFooterHeight ( qreal size = 195.32 );
//正文,正在使用中
QFont font();
//default: QApplication::font()
void setFont ( QFont m_font = QFont() );
void setHeaderFont ( QFont m_font = QFont() );
//header,使用中.
void setHeaderFont ( QFont font = QFont() );
void setHeaderLine ( bool show = false );
void setHeaderText ( const QString& text, QFont m_font = QFont(),
Qt::Alignment align = Qt::AlignHCenter );
//footer,使用中
void setFooterFont ( QFont font = QFont() );
void setFooterLine ( bool show = false );
void setFooterText ( const QString& text, QFont m_font = QFont(),
Qt::Alignment align = Qt::AlignHCenter );
/**
* @brief mainFont
*/
QFont mainFont() { return m_mainFont; }
QFont titleFont() { return m_titleFont; }
QFont title2Font() { return m_title2Font; }
QFont headerFont() { return m_headerFont; }
void setHeaderLine ( bool show = false );
void setFooterLine ( bool show = false );
void setHeaderText ( const QString& text, QFont m_font = QFont(),
Qt::Alignment align = Qt::AlignHCenter );
void setFooterText ( const QString& text, QFont m_font = QFont(),
Qt::Alignment align = Qt::AlignHCenter );
void exportPdf ( const QString& pdf );
void print();
/**
* @brief exportImages Word导出为多个图纸.
*/
//内部有默认的大小和分辨率,根据这些获取目标Rect. 导出图片要用这个Rect.
//sceneRect / sceneDPI = targetRect / targetDPI 注释:DPI为logicDPI.
//可选输入: 桌面屏幕 96; DWIN屏 136,156; 打印机 1200; 其他 待测试;
//DPI保证了(屏幕)所见即(A4纸)所得
QRectF getTargetRectF ( qreal targetDPIX, qreal targetDPIY ) {
QRectF targetRect;
targetRect = QRectF ( 0.0, 0.0, targetDPIX * sceneRect.width() / logicalDpiX /*300*/,
targetDPIY * sceneRect.height() / logicalDpiY /*300*/ );
return targetRect;
}
//如果targetRect为空,那么按照SceneRect的大小,分辨率300DPI(内部默认)导出.
//虽然这里导出图片,但是通过图片可以导出PDF(QQtPrinter),可以显示(QQtWidget),
//相当于把pageScene按照确定比例投影到确定大小的画纸(幕布)上,是个列表,很多页.
//sceneRect dpi1 300
//imageRect dpi2 96
//printerPaperRect dpi3 1200
//确定比值相同. dpi 像素每英寸 像素 / 像素每英寸 = 英寸 如果是A4纸,那么一定是固定值咯.
void exportImage ( QList<QImage>& targetPapers , QRectF targetRect = QRectF() );
protected:
virtual void adjustdy ( qreal dy0 );
@ -99,11 +136,6 @@ signals:
public slots:
private:
/*
*
*/
QQtPrinter* pr;
/*
*
*/
@ -113,8 +145,8 @@ private:
/*
*
*/
int logicalDpiX;
int logicalDpiY;
qreal logicalDpiX;
qreal logicalDpiY;
QRectF sceneRect;
qreal xpos, xpos2, ypos, ypos2;
@ -175,6 +207,8 @@ private:
QHash<int, ESpanFlags> tableSpans ( const QTableView* table );
void initConstFont();
};
#endif // QQTWORD_H

View File

@ -1,5 +1,6 @@
#include "qqtprinter.h"
#include "stdlib.h"
#include <QImage>
#include <QPainter>
QQtPrinter::QQtPrinter ( QPrinter::PrinterMode mode ) :
QPrinter ( mode )
@ -15,6 +16,56 @@ QQtPrinter::QQtPrinter ( QPrinter::PrinterMode mode ) :
setPaperSize ( QPrinter::A4 );
setOrientation ( QPrinter::Portrait );
setOutputFormat ( QPrinter::PdfFormat );
#if 0
//setup printer
/**
MIPS bug: you must use QQtPrinter(QPrinter::HighResolution)
Other constructer won't work well
*/
pr = new QQtPrinter ( QPrinter::HighResolution );
pr->setFullPage ( true );
pr->setColorMode ( QPrinter::Color );
pr->setPaperSize ( QPrinter::A4 );
pr->setOrientation ( QPrinter::Portrait );
pr->setOutputFormat ( QPrinter::PdfFormat );
/*
* Font目标是打印纸上
* Pos目标不是纸上的DPI
* 使
* View 142,138 //PCView 96 //打印机View 1200
* 1200
* DrawText可用空间减小甚至切掉一部分
*/
#ifdef __EMBEDDED_LINUX__
/*
*
*/
logicalDpiX = 136;
logicalDpiY = 156;
#else
/*
*
*/
logicalDpiX = 96;
logicalDpiY = 96;
#endif
//1200 9917,14033 printerRect 固定
//116 958,1356 sceneRect
//142 1113,1660 sceneRect
pline() << pr->logicalDpiX() << pr->logicalDpiY();
pline() << logicalDpiX << logicalDpiY << pr->pageRect() << sceneRect;
pline() << pr->paperRect ( QPrinter::DevicePixel );
pline() << pr->paperRect ( QPrinter::Millimeter );
pline() << pr->paperRect ( QPrinter::Point );
pline() << pr->paperRect ( QPrinter::Inch );
pline() << pr->paperRect ( QPrinter::Pica );
pline() << pr->paperRect ( QPrinter::Didot );
pline() << pr->paperRect ( QPrinter::Cicero );
QRect rect = pr->paperRect();
sceneRect = QRectF ( 0.0, 0.0, logicalDpiX * rect.width() / pr->logicalDpiX(),
logicalDpiY * rect.height() / pr->logicalDpiY() );
#endif
}
void QQtPrinter::setOutputFileName ( const QString& name )
@ -23,12 +74,25 @@ void QQtPrinter::setOutputFileName ( const QString& name )
QPrinter::setOutputFileName ( m_outputname );
}
void QQtPrinter::generateToFile ( const QList<QImage>& imgs )
void QQtPrinter::generateToPdfFile ( const QList<QImage>& sourcePapers , QRectF sourceRect )
{
if ( sourceRect == QRectF() )
sourceRect = paperRect();
// print pdf
QPainter p ( this );
QListIterator<QImage> itor ( sourcePapers );
while ( itor.hasNext() )
{
const QImage& page = itor.next();
p.drawImage ( paperRect(), page, sourceRect );
if ( itor.hasNext() )
newPage();
}
}
void QQtPrinter::print()
void QQtPrinter::printPdf()
{
/*
*

View File

@ -12,11 +12,15 @@ public:
//输出到文件
void setOutputFileName ( const QString& );
//把QImage列表输出到上边的位置。QQtWord会输出这个list。
void generateToFile ( const QList<QImage>& imgs );
//把QImage列表输出(投影)到上边的位置。
//QQtWord会输出这个list。
//如果sourceRect为空,那么按照打印机的PaperRect,这说明sourcePaper使用了打印机的分辨率1200.
//只要纸张类型设置的正确,比如都是A4纸那样的长宽比,那么就不会失真.关键在长宽比和分辨率.
//分辨率决定是否失真. 长宽比决定是否变形.
void generateToPdfFile ( const QList<QImage>& sourcePapers, QRectF sourceRect = QRectF() );
//嵌入式系统里打印到打印机
void print();
void printPdf();
//输出到打印机
//内部实现

View File

@ -4,7 +4,7 @@
#include "qqtframe.h"
#include "qqtword.h"
#include "qqtcore.h"
#include "qqtprinter.h"
MainWindow::MainWindow ( QWidget* parent ) :
QMainWindow ( parent ),
@ -28,14 +28,19 @@ void MainWindow::on_pushButton_clicked()
return;
QQtWord word;
//所有对QQtWord的设置,做在这里.
word.initWord();
//work flow
word.setFooterText ( "Company T", word.headerFont(), Qt::AlignLeft );
word.setHeaderText ( "Company T Header", word.headerFont(), Qt::AlignLeft );
word.addText ( "Main Page", word.titleFont(), Qt::AlignCenter );
word.addText ( " Now, this is first section.", word.mainFont(), Qt::AlignLeft );
word.addText ( " Now, this is second section.", word.mainFont(), Qt::AlignLeft );
word.addText ( "Now, this is first section.", word.mainFont(), Qt::AlignLeft );
word.addText ( "Now, this is second section.", word.mainFont(), Qt::AlignLeft );
word.addText ( "Now, this is third section.", word.mainFont(), Qt::AlignLeft );
word.addText ( "Now, this is four section.", word.mainFont(), Qt::AlignLeft );
word.addText ( "八荣八耻", word.title2Font(), Qt::AlignLeft );
word.addText ( "以热爱祖国为荣、以危害祖国为耻。" );
@ -61,10 +66,19 @@ void MainWindow::on_pushButton_clicked()
tableWidget.query ( "" );
tableWidget.horizontalHeader()->setSectionResizeMode ( QQtHeaderView::ResizeToContents );
tableWidget.setFixedWidth ( 523.8 );
pline() << word.clientRectF();
pline() << word.clientRectF() << word.paperRect();
word.addTable ( &tableWidget );
word.addSignoffText ( "Reporter: Tianduanrui" );
word.exportPdf ( ui->lineEdit->text() );
QList<QImage> papers;
//分辨率可以随意一点.一般用于显示器正常显示,为96.
QRectF paperRect = word.getTargetRectF ( 300, 300 );
word.exportImage ( papers, paperRect );
pline() << papers;
QQtPrinter printer;
pline() << printer.logicalDpiX() << printer.logicalDpiY();
printer.setOutputFileName ( ui->lineEdit->text() );
printer.generateToPdfFile ( papers, paperRect );
}