2015-04-11 13:01:25 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vposter.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 11 4, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2015 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
|
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vposter.h"
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPrinter>
|
2015-04-15 19:41:23 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 1, 0)
|
2015-06-15 14:02:42 +02:00
|
|
|
# include "../libs/vmisc/vmath.h"
|
2015-04-15 19:41:23 +02:00
|
|
|
#else
|
|
|
|
# include <QtMath>
|
|
|
|
#endif
|
2015-04-11 13:01:25 +02:00
|
|
|
|
2015-06-15 13:43:41 +02:00
|
|
|
#include "../vmisc/def.h"
|
2015-04-11 13:01:25 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VPoster::VPoster(const QPrinter *printer)
|
2015-04-19 12:59:19 +02:00
|
|
|
:printer(printer), allowence(qRound(10./25.4*printer->resolution()))//1 cm
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 10:36:08 +02:00
|
|
|
QVector<QImage> VPoster::Generate(const QImage &image, int page, int sheets) const
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
|
|
|
QVector<QImage> poster;
|
|
|
|
|
|
|
|
if (printer == nullptr)
|
|
|
|
{
|
|
|
|
return poster;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int rows = CountRows(image.rect().height());
|
|
|
|
const int colomns = CountColomns(image.rect().width());
|
|
|
|
|
|
|
|
for (int i=0; i < rows; i++)
|
|
|
|
{
|
|
|
|
for (int j=0; j< colomns; j++)
|
|
|
|
{
|
|
|
|
QImage img = Cut(i, j, image);
|
2015-04-15 10:36:08 +02:00
|
|
|
img = Borders(rows, colomns, i, j, img, page, sheets);
|
2015-04-11 13:01:25 +02:00
|
|
|
poster.append(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return poster;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VPoster::CountRows(int height) const
|
|
|
|
{
|
|
|
|
const qreal imgLength = height;
|
|
|
|
const qreal pageLength = PageRect().height();
|
|
|
|
|
|
|
|
// Example
|
|
|
|
// ― ―
|
|
|
|
// * *
|
|
|
|
// * *
|
|
|
|
// * *
|
|
|
|
// * * ―
|
|
|
|
// ― ― *
|
|
|
|
// * *
|
|
|
|
// * *
|
|
|
|
// * * ―
|
|
|
|
// * ― *
|
|
|
|
// — *
|
|
|
|
// * *
|
|
|
|
// * * ―
|
|
|
|
// * ― *
|
|
|
|
// * *
|
|
|
|
// — *
|
|
|
|
// * * ―
|
|
|
|
// * ― * <-(2)
|
|
|
|
// * + *
|
|
|
|
// * + *
|
|
|
|
// — + * ― <-(4)
|
|
|
|
// ^ ^ ― *
|
|
|
|
//(3) (1) *
|
|
|
|
// *
|
|
|
|
// *
|
|
|
|
// ―
|
|
|
|
|
|
|
|
const int pCount = qCeil(imgLength/pageLength);// Pages count without allowence (or allowence = 0) (3)
|
|
|
|
|
|
|
|
// Calculate how many pages will be after using allowence.
|
|
|
|
// We know start pages count. This number not enought because
|
|
|
|
// each n-1 pages add (n-1)*allowence length to page (1).
|
|
|
|
const qreal addionalLength = (pCount-1)*allowence;
|
|
|
|
|
|
|
|
// Calculate additional length form pages that will cover this length (2).
|
|
|
|
// In the end add page length (3).
|
|
|
|
// Bottom page have mandatory border (4)
|
|
|
|
return qCeil((addionalLength +
|
|
|
|
qCeil(addionalLength/pageLength)*allowence + allowence + imgLength)/pageLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VPoster::CountColomns(int width) const
|
|
|
|
{
|
|
|
|
const qreal imgLength = width;
|
|
|
|
const qreal pageLength = PageRect().width();
|
|
|
|
|
|
|
|
// Example
|
|
|
|
// |----|----|----|----| <- (3)
|
|
|
|
// |----|+++++++++++++++
|
|
|
|
// |----|+++++++++++
|
|
|
|
// |----|+++++++
|
|
|
|
// |----|+++ <- (1)
|
|
|
|
// |----|
|
|
|
|
// ^
|
|
|
|
// (2)
|
|
|
|
const int pCount = qCeil(imgLength/pageLength);// Pages count without allowence (or allowence = 0) (3)
|
|
|
|
|
|
|
|
// Calculate how many pages will be after using allowence.
|
|
|
|
// We know start pages count. This number not enought because
|
|
|
|
// each n-1 pages add (n-1)*allowence length to page (1).
|
|
|
|
const qreal addionalLength = (pCount-1)*allowence;
|
|
|
|
|
|
|
|
// Calculate additional length form pages that will cover this length (2).
|
|
|
|
// In the end add page length (3).
|
|
|
|
return qCeil((addionalLength + qCeil(addionalLength/pageLength)*allowence + imgLength)/pageLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QImage VPoster::Cut(int i, int j, const QImage &image) const
|
|
|
|
{
|
|
|
|
const int x = j*PageRect().width() - j*allowence;
|
|
|
|
const int y = i*PageRect().height() - i*allowence;
|
|
|
|
|
|
|
|
SCASSERT(x <= image.rect().width());
|
|
|
|
SCASSERT(y <= image.rect().height());
|
|
|
|
|
|
|
|
QRect copyRect(x, y, PageRect().width(), PageRect().height());
|
|
|
|
|
|
|
|
if (not image.rect().contains(copyRect))
|
|
|
|
{
|
2015-04-15 11:10:35 +02:00
|
|
|
// Create full page with white background
|
2015-04-11 13:01:25 +02:00
|
|
|
QImage fullPage(copyRect.size(), image.format());
|
|
|
|
fullPage.fill(Qt::white);
|
|
|
|
|
2015-04-15 11:10:35 +02:00
|
|
|
// Real size that we can copy from image.
|
|
|
|
// Because in areas beyond the image, pixels are set to 0 by copy() method.
|
|
|
|
// For 32-bit RGB images, this means black.
|
2015-04-11 13:01:25 +02:00
|
|
|
copyRect = image.rect().intersected(copyRect);
|
|
|
|
|
|
|
|
QPainter painter(&fullPage);
|
|
|
|
painter.drawImage(QPointF(), image.copy( copyRect));
|
|
|
|
painter.end();
|
|
|
|
|
|
|
|
return fullPage;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return image.copy(copyRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 10:36:08 +02:00
|
|
|
QImage VPoster::Borders(int rows, int colomns, int i, int j, QImage &image, int page, int sheets) const
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
|
|
|
QPainter painter(&image);
|
|
|
|
|
|
|
|
QPen pen = QPen(Qt::NoBrush, 1, Qt::DashLine);
|
|
|
|
pen.setColor(Qt::black);
|
|
|
|
painter.setPen(pen);
|
|
|
|
|
2015-04-15 10:36:08 +02:00
|
|
|
if (j != 0 && PageRect().x() > 0)
|
2015-04-11 13:01:25 +02:00
|
|
|
{// Left border
|
|
|
|
painter.drawLine(QLine(0, 0, 0, image.rect().height()));
|
|
|
|
painter.drawImage(QPoint(0, image.rect().height()-allowence), QImage("://scissors_vertical.png"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j != colomns-1)
|
|
|
|
{// Right border
|
|
|
|
painter.drawLine(QLine(image.rect().width()-allowence, 0,
|
|
|
|
image.rect().width()-allowence, image.rect().height()));
|
|
|
|
}
|
|
|
|
|
2015-04-15 10:36:08 +02:00
|
|
|
if (i != 0 && PageRect().y() > 0)
|
2015-04-11 13:01:25 +02:00
|
|
|
{// Top border
|
|
|
|
painter.drawLine(QLine(0, 0, image.rect().width(), 0));
|
|
|
|
painter.drawImage(QPoint(image.rect().width()-allowence, 0), QImage("://scissors_horizontal.png"));
|
|
|
|
}
|
|
|
|
|
2015-04-15 11:10:35 +02:00
|
|
|
if (rows*colomns > 1)
|
|
|
|
{ // Don't show bottom border if only one page need
|
|
|
|
// Bottom border (mandatory)
|
|
|
|
painter.drawLine(QLine(0, image.rect().height()-allowence,
|
|
|
|
image.rect().width(), image.rect().height()-allowence));
|
|
|
|
if (i == rows-1)
|
|
|
|
{
|
|
|
|
painter.drawImage(QPoint(image.rect().width()-allowence, image.rect().height()-allowence),
|
|
|
|
QImage("://scissors_horizontal.png"));
|
|
|
|
}
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Labels
|
|
|
|
const int layoutX = 15;
|
|
|
|
const int layoutY = 5;
|
|
|
|
QRect labels(layoutX, image.rect().height()-allowence+layoutY,
|
|
|
|
image.rect().width()-(allowence+layoutX), allowence-layoutY);
|
2015-04-19 16:41:46 +02:00
|
|
|
painter.drawText(labels, Qt::AlignLeft, tr("Grid ( %1 , %2 )").arg(i+1).arg(j+1));
|
2015-04-11 13:01:25 +02:00
|
|
|
painter.drawText(labels, Qt::AlignHCenter, tr("Page %1 of %2").arg(i*(colomns)+j+1).arg(rows*colomns));
|
2015-04-15 10:36:08 +02:00
|
|
|
if (sheets > 1)
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
2015-04-15 10:36:08 +02:00
|
|
|
painter.drawText(labels, Qt::AlignRight, tr("Sheet %1 of %2").arg(page).arg(sheets));
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
painter.end();
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QRect VPoster::PageRect() const
|
|
|
|
{
|
2015-04-15 11:10:35 +02:00
|
|
|
// Because the Point unit is defined to be 1/72th of an inch
|
|
|
|
// we can't use method pageRect(QPrinter::Point). Our dpi different can be different.
|
|
|
|
// We convert value yourself to pixels.
|
2015-04-11 13:01:25 +02:00
|
|
|
const QRectF rect = printer->pageRect(QPrinter::Millimeter);
|
|
|
|
QRect pageRect(qFloor(ToPixel(rect.x())), qFloor(ToPixel(rect.y())), qFloor(ToPixel(rect.width())),
|
|
|
|
qFloor(ToPixel(rect.height())));
|
|
|
|
return pageRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal VPoster::ToPixel(qreal val) const
|
|
|
|
{
|
|
|
|
return val / 25.4 * printer->resolution(); // Mm to pixels with current dpi.
|
|
|
|
}
|