2015-04-11 13:01:25 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vposter.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 11 4, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2015-04-11 13:01:25 +02:00
|
|
|
** 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"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
#include <QGraphicsLineItem>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QGraphicsPixmapItem>
|
|
|
|
#include <QGraphicsTextItem>
|
2016-06-15 12:55:43 +02:00
|
|
|
#include <QPen>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QPixmap>
|
|
|
|
#include <QPrinter>
|
|
|
|
#include <QRectF>
|
|
|
|
#include <QString>
|
|
|
|
#include <QVector>
|
|
|
|
#include <Qt>
|
2017-10-10 11:22:59 +02:00
|
|
|
#include <QDebug>
|
2015-10-08 19:07:48 +02:00
|
|
|
|
2016-08-16 18:57:32 +02:00
|
|
|
#include "../vmisc/vmath.h"
|
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)
|
2017-01-23 17:20:15 +01:00
|
|
|
:printer(printer), allowance(static_cast<quint32>(qRound(10./25.4*PrintDPI)))//1 cm
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-10-10 11:22:59 +02:00
|
|
|
QVector<PosterData> VPoster::Calc(const QRect &imageRect, int page, PageOrientation orientation) const
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
2016-06-15 12:55:43 +02:00
|
|
|
QVector<PosterData> poster;
|
2015-04-11 13:01:25 +02:00
|
|
|
|
|
|
|
if (printer == nullptr)
|
|
|
|
{
|
|
|
|
return poster;
|
|
|
|
}
|
|
|
|
|
2017-10-10 11:22:59 +02:00
|
|
|
const int rows = CountRows(imageRect.height(), orientation);
|
|
|
|
const int columns = CountColumns(imageRect.width(), orientation);
|
2015-04-11 13:01:25 +02:00
|
|
|
|
|
|
|
for (int i=0; i < rows; i++)
|
|
|
|
{
|
2016-06-01 20:08:36 +02:00
|
|
|
for (int j=0; j< columns; j++)
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
2017-10-10 11:22:59 +02:00
|
|
|
PosterData data = Cut(i, j, imageRect, orientation);
|
2016-11-01 09:42:32 +01:00
|
|
|
data.index = static_cast<quint32>(page);
|
|
|
|
data.rows = static_cast<quint32>(rows);
|
|
|
|
data.columns = static_cast<quint32>(columns);
|
2016-06-15 12:55:43 +02:00
|
|
|
poster.append(data);
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return poster;
|
|
|
|
}
|
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVector<QGraphicsItem *> VPoster::Borders(QGraphicsItem *parent, const PosterData &img, int sheets) const
|
|
|
|
{
|
2019-08-05 09:21:04 +02:00
|
|
|
SCASSERT(parent != nullptr)
|
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
QVector<QGraphicsItem *> data;
|
|
|
|
QPen pen(Qt::NoBrush, 1, Qt::DashLine);
|
2019-12-07 22:18:44 +01:00
|
|
|
QPen rulePen(Qt::NoBrush, 1, Qt::SolidLine);
|
2016-06-15 12:55:43 +02:00
|
|
|
pen.setColor(Qt::black);
|
2019-12-07 22:18:44 +01:00
|
|
|
rulePen.setColor(Qt::black);
|
2016-06-15 12:55:43 +02:00
|
|
|
|
2017-10-11 10:34:22 +02:00
|
|
|
if (img.columns == 1 && img.rows == 1)
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
const QRect rec = img.rect;
|
|
|
|
if (img.column != 0)
|
|
|
|
{// Left border
|
|
|
|
auto *line = new QGraphicsLineItem(parent);
|
|
|
|
line->setPen(pen);
|
|
|
|
line->setLine(rec.x(), rec.y(), rec.x(), rec.y() + rec.height());
|
|
|
|
data.append(line);
|
|
|
|
|
2019-08-05 09:21:04 +02:00
|
|
|
auto *scissors = new QGraphicsPixmapItem(QPixmap(QStringLiteral("://scissors_vertical.png")), parent);
|
2017-01-23 17:20:15 +01:00
|
|
|
scissors->setPos(rec.x(), rec.y() + rec.height()-static_cast<int>(allowance));
|
2016-06-15 12:55:43 +02:00
|
|
|
data.append(scissors);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img.column != img.columns-1)
|
|
|
|
{// Right border
|
|
|
|
auto *line = new QGraphicsLineItem(parent);
|
|
|
|
line->setPen(pen);
|
2017-01-23 17:20:15 +01:00
|
|
|
line->setLine(rec.x() + rec.width()-static_cast<int>(allowance), rec.y(),
|
|
|
|
rec.x() + rec.width()-static_cast<int>(allowance), rec.y() + rec.height());
|
2016-06-15 12:55:43 +02:00
|
|
|
data.append(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (img.row != 0)
|
|
|
|
{// Top border
|
|
|
|
auto *line = new QGraphicsLineItem(parent);
|
|
|
|
line->setPen(pen);
|
|
|
|
line->setLine(rec.x(), rec.y(), rec.x() + rec.width(), rec.y());
|
|
|
|
data.append(line);
|
|
|
|
|
2019-08-05 09:21:04 +02:00
|
|
|
auto *scissors = new QGraphicsPixmapItem(QPixmap(QStringLiteral("://scissors_horizontal.png")), parent);
|
2017-01-23 17:20:15 +01:00
|
|
|
scissors->setPos(rec.x() + rec.width()-static_cast<int>(allowance), rec.y());
|
2016-06-15 12:55:43 +02:00
|
|
|
data.append(scissors);
|
|
|
|
}
|
|
|
|
|
2019-08-05 09:21:04 +02:00
|
|
|
// Bottom border (mandatory)
|
|
|
|
auto *line = new QGraphicsLineItem(parent);
|
|
|
|
line->setPen(pen);
|
|
|
|
line->setLine(rec.x(), rec.y() + rec.height()-static_cast<int>(allowance),
|
|
|
|
rec.x() + rec.width(), rec.y() + rec.height()-static_cast<int>(allowance));
|
|
|
|
data.append(line);
|
2016-06-15 12:55:43 +02:00
|
|
|
|
2019-12-07 22:18:44 +01:00
|
|
|
double marksCount = rec.width() / ToPixel(10);
|
|
|
|
int i = 0;
|
|
|
|
while (i < marksCount)
|
|
|
|
{
|
|
|
|
auto *ruleLine = new QGraphicsLineItem(parent);
|
|
|
|
ruleLine->setPen(rulePen);
|
|
|
|
ruleLine->setLine(rec.x() + ToPixel(10) * i,
|
|
|
|
rec.y() + rec.height()-static_cast<int>(allowance),
|
|
|
|
rec.x() + ToPixel(10) * i,
|
|
|
|
rec.y() + rec.height()-static_cast<int>(allowance) + ToPixel(2));
|
|
|
|
data.append(ruleLine);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
// Labels
|
|
|
|
auto *labels = new QGraphicsTextItem(parent);
|
|
|
|
|
|
|
|
const int layoutX = 15;
|
|
|
|
const int layoutY = 5;
|
2017-01-23 17:20:15 +01:00
|
|
|
labels->setPos(rec.x() + layoutX, rec.y() + rec.height()-static_cast<int>(allowance)+layoutY);
|
|
|
|
labels->setTextWidth(rec.width()-(static_cast<int>(allowance)+layoutX));
|
2016-06-15 12:55:43 +02:00
|
|
|
|
|
|
|
const QString grid = tr("Grid ( %1 , %2 )").arg(img.row+1).arg(img.column+1);
|
|
|
|
const QString page = tr("Page %1 of %2").arg(img.row*(img.columns)+img.column+1).arg(img.rows*img.columns);
|
|
|
|
|
|
|
|
QString sheet;
|
|
|
|
if (sheets > 1)
|
|
|
|
{
|
|
|
|
sheet = tr("Sheet %1 of %2").arg(img.index+1).arg(sheets);
|
|
|
|
}
|
|
|
|
|
|
|
|
labels->setHtml(QString("<table width='100%'>"
|
|
|
|
"<tr>"
|
|
|
|
"<td>%1</td><td align='center'>%2</td><td align='right'>%3</td>"
|
|
|
|
"</tr>"
|
|
|
|
"</table>")
|
|
|
|
.arg(grid, page, sheet));
|
|
|
|
|
|
|
|
data.append(labels);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:01:25 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-10-10 11:22:59 +02:00
|
|
|
int VPoster::CountRows(int height, PageOrientation orientation) const
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
|
|
|
const qreal imgLength = height;
|
2017-10-10 11:22:59 +02:00
|
|
|
qreal pageLength = 0;
|
|
|
|
|
|
|
|
if(orientation == PageOrientation::Landscape)
|
|
|
|
{
|
|
|
|
pageLength = PageRect().width();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pageLength = PageRect().height();
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:22 +02:00
|
|
|
if (pageLength >= imgLength)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return qCeil(imgLength/(pageLength - static_cast<int>(allowance)));
|
|
|
|
}
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-10-10 11:22:59 +02:00
|
|
|
int VPoster::CountColumns(int width, PageOrientation orientation) const
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
|
|
|
const qreal imgLength = width;
|
2017-10-10 11:22:59 +02:00
|
|
|
qreal pageLength = 0;
|
|
|
|
|
|
|
|
if(orientation == PageOrientation::Landscape)
|
|
|
|
{
|
|
|
|
pageLength = PageRect().height();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pageLength = PageRect().width();
|
|
|
|
}
|
|
|
|
|
2017-10-11 10:34:22 +02:00
|
|
|
if (pageLength >= imgLength)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return qCeil(imgLength/(pageLength-static_cast<int>(allowance)));
|
|
|
|
}
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-10-10 11:22:59 +02:00
|
|
|
PosterData VPoster::Cut(int i, int j, const QRect &imageRect, PageOrientation orientation) const
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
2016-12-20 20:19:21 +01:00
|
|
|
Q_UNUSED(imageRect)
|
2016-07-06 20:49:36 +02:00
|
|
|
|
2017-10-10 11:22:59 +02:00
|
|
|
int pageLengthX, pageLengthY;
|
|
|
|
|
|
|
|
if(orientation == PageOrientation::Landscape)
|
|
|
|
{
|
|
|
|
pageLengthX = PageRect().height();
|
|
|
|
pageLengthY = PageRect().width();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pageLengthX = PageRect().width();
|
|
|
|
pageLengthY = PageRect().height();
|
|
|
|
}
|
|
|
|
|
|
|
|
const int x = j*pageLengthX - j*static_cast<int>(allowance);
|
|
|
|
const int y = i*pageLengthY - i*static_cast<int>(allowance);
|
2015-04-11 13:01:25 +02:00
|
|
|
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(x <= imageRect.width())
|
|
|
|
SCASSERT(y <= imageRect.height())
|
2015-04-11 13:01:25 +02:00
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
PosterData data;
|
2016-11-01 09:42:32 +01:00
|
|
|
data.row = static_cast<quint32>(i);
|
|
|
|
data.column = static_cast<quint32>(j);
|
2017-10-10 11:22:59 +02:00
|
|
|
data.rect = QRect(x, y, pageLengthX, pageLengthY);
|
2015-04-11 13:01:25 +02:00
|
|
|
|
2016-06-15 12:55:43 +02:00
|
|
|
return data;
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
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
|
2015-11-09 12:33:36 +01:00
|
|
|
// we can't use method pageRect(QPrinter::Point). Our dpi value can be different.
|
2015-04-15 11:10:35 +02:00
|
|
|
// We convert value yourself to pixels.
|
2015-04-11 13:01:25 +02:00
|
|
|
const QRectF rect = printer->pageRect(QPrinter::Millimeter);
|
2016-10-23 11:11:00 +02:00
|
|
|
|
|
|
|
if(printer->fullPage())
|
|
|
|
{
|
2019-04-05 14:42:22 +02:00
|
|
|
QMarginsF pMargins = printer->pageLayout().margins();
|
|
|
|
QRectF newRect = rect.marginsRemoved(pMargins);
|
|
|
|
const QRect pageRectFP(0, 0, qFloor(ToPixel(newRect.width())), qFloor(ToPixel(newRect.height())));
|
|
|
|
return pageRectFP;
|
2016-10-23 11:11:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const QRect pageRect(0, 0, qFloor(ToPixel(rect.width())), qFloor(ToPixel(rect.height())));
|
|
|
|
return pageRect;
|
|
|
|
}
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-06-15 12:55:43 +02:00
|
|
|
qreal VPoster::ToPixel(qreal val)
|
2015-04-11 13:01:25 +02:00
|
|
|
{
|
2016-06-15 12:55:43 +02:00
|
|
|
return val / 25.4 * PrintDPI; // Mm to pixels with current dpi.
|
2015-04-11 13:01:25 +02:00
|
|
|
}
|