2017-06-16 13:53:08 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 14 6, 2017
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2017-06-16 13:53:08 +02:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2017 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2017-06-16 13:53:08 +02:00
|
|
|
**
|
|
|
|
** 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 "global.h"
|
|
|
|
#include "../vmisc/def.h"
|
2018-02-06 19:20:45 +01:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
2022-01-29 09:59:02 +01:00
|
|
|
#include "../vmisc/compatibility.h"
|
2017-06-16 13:53:08 +02:00
|
|
|
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
#include <QGraphicsScene>
|
2017-10-31 09:31:50 +01:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2017-06-16 13:53:08 +02:00
|
|
|
#include <QGraphicsView>
|
|
|
|
|
2017-10-28 12:02:51 +02:00
|
|
|
const qreal minVisibleFontSize = 5;
|
2017-06-16 13:53:08 +02:00
|
|
|
|
2018-02-06 19:20:45 +01:00
|
|
|
inline qreal DefPointRadiusPixel()
|
|
|
|
{
|
2021-02-06 14:52:21 +01:00
|
|
|
return (VAbstractApplication::VApp()->Settings()->GetLineWidth() + 0.8) / 25.4 * PrintDPI;
|
2018-02-06 19:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-06-16 13:53:08 +02:00
|
|
|
qreal SceneScale(QGraphicsScene *scene)
|
|
|
|
{
|
|
|
|
qreal scale = 1;
|
|
|
|
|
|
|
|
if (scene)
|
|
|
|
{
|
|
|
|
const QList<QGraphicsView *> views = scene->views();
|
|
|
|
if (not views.isEmpty())
|
|
|
|
{
|
2022-01-29 09:59:02 +01:00
|
|
|
scale = ConstFirst(views)->transform().m11();
|
2017-06-16 13:53:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QColor CorrectColor(const QGraphicsItem *item, const QColor &color)
|
|
|
|
{
|
|
|
|
SCASSERT(item != nullptr)
|
|
|
|
|
|
|
|
if (item->isEnabled())
|
|
|
|
{
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Qt::gray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QRectF PointRect(qreal radius)
|
|
|
|
{
|
|
|
|
QRectF rec = QRectF(0, 0, radius*2, radius*2);
|
|
|
|
rec.translate(-rec.center().x(), -rec.center().y());
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal ScaledRadius(qreal scale)
|
|
|
|
{
|
2018-02-06 19:20:45 +01:00
|
|
|
qreal scaledRadius = DefPointRadiusPixel();
|
2017-06-16 13:53:08 +02:00
|
|
|
if (scale > 1)
|
|
|
|
{
|
2018-02-06 19:20:45 +01:00
|
|
|
scaledRadius = qMax(DefPointRadiusPixel()/96, DefPointRadiusPixel()/scale);
|
2017-06-16 13:53:08 +02:00
|
|
|
}
|
|
|
|
return scaledRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void ScaleCircleSize(QGraphicsEllipseItem *item, qreal scale)
|
|
|
|
{
|
|
|
|
SCASSERT(item != nullptr)
|
|
|
|
|
|
|
|
item->setRect(PointRect(ScaledRadius(scale)));
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
qreal ScaleWidth(qreal width, qreal scale)
|
|
|
|
{
|
|
|
|
if (scale > 1)
|
|
|
|
{
|
2017-09-30 20:13:22 +02:00
|
|
|
width = qMax(0.01, width/scale);
|
2017-06-16 13:53:08 +02:00
|
|
|
}
|
|
|
|
return width;
|
|
|
|
}
|
2017-08-01 11:57:19 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QPainterPath ItemShapeFromPath(const QPainterPath &path, const QPen &pen)
|
|
|
|
{
|
|
|
|
// We unfortunately need this hack as QPainterPathStroker will set a width of 1.0
|
|
|
|
// if we pass a value of 0.0 to QPainterPathStroker::setWidth()
|
|
|
|
const qreal penWidthZero = qreal(0.00000001);
|
|
|
|
|
|
|
|
if (path == QPainterPath() || pen == Qt::NoPen)
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
QPainterPathStroker ps;
|
|
|
|
ps.setCapStyle(pen.capStyle());
|
|
|
|
if (pen.widthF() <= 0.0)
|
|
|
|
{
|
|
|
|
ps.setWidth(penWidthZero);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ps.setWidth(pen.widthF());
|
|
|
|
}
|
|
|
|
ps.setJoinStyle(pen.joinStyle());
|
|
|
|
ps.setMiterLimit(pen.miterLimit());
|
|
|
|
QPainterPath p = ps.createStroke(path);
|
|
|
|
p.addPath(path);
|
|
|
|
return p;
|
|
|
|
}
|
2017-10-29 10:06:17 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void GraphicsItemHighlightSelected(const QRectF &boundingRect, qreal itemPenWidth, QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option)
|
|
|
|
{
|
|
|
|
const QRectF murect = painter->transform().mapRect(QRectF(0, 0, 1, 1));
|
|
|
|
if (qFuzzyIsNull(qMax(murect.width(), murect.height())))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QRectF mbrect = painter->transform().mapRect(boundingRect);
|
|
|
|
if (qMin(mbrect.width(), mbrect.height()) < qreal(1.0))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const qreal pad = itemPenWidth / 2;
|
|
|
|
|
|
|
|
const qreal penWidth = 0; // cosmetic pen
|
|
|
|
|
|
|
|
const QColor fgcolor = option->palette.windowText().color();
|
|
|
|
const QColor bgcolor( // ensure good contrast against fgcolor
|
|
|
|
fgcolor.red() > 127 ? 0 : 255,
|
|
|
|
fgcolor.green() > 127 ? 0 : 255,
|
|
|
|
fgcolor.blue() > 127 ? 0 : 255);
|
|
|
|
|
|
|
|
painter->setPen(QPen(bgcolor, penWidth, Qt::SolidLine));
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
painter->drawRect(boundingRect.adjusted(pad, pad, -pad, -pad));
|
|
|
|
|
|
|
|
painter->setPen(QPen(option->palette.windowText(), 0, Qt::DashLine));
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
painter->drawRect(boundingRect.adjusted(pad, pad, -pad, -pad));
|
|
|
|
}
|
2017-10-31 09:31:50 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
bool IsSelectedByReleaseEvent(QGraphicsItem *item, QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
SCASSERT(item != nullptr)
|
|
|
|
return event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick
|
|
|
|
&& item->contains(event->pos());
|
|
|
|
}
|