Files
create/src/Mod/TechDraw/Gui/QGIHighlight.cpp
WandererFan 51184b99d5 [TechDraw]Detail highlight drag (fix #21828) (#22036)
* [TD]add preferences for detail highlight snapping

* [TD]fix highlight drag issues

* Update src/Mod/TechDraw/Gui/TaskDetail.cpp

minor format change from benj5378.

Co-authored-by: Benjamin Bræstrup Sayoc <benj5378@outlook.com>

---------

Co-authored-by: Benjamin Bræstrup Sayoc <benj5378@outlook.com>
2025-06-23 11:13:27 -05:00

204 lines
6.2 KiB
C++

/***************************************************************************
* Copyright (c) 2016 WandererFan <wandererfan@gmail.com> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QPainter>
# include <QStyleOptionGraphicsItem>
#endif
#include <Base/Tools.h>
#include <Mod/TechDraw/App/DrawUtil.h>
#include "QGIHighlight.h"
#include "PreferencesGui.h"
#include "QGIViewPart.h"
#include "Rez.h"
using namespace TechDrawGui;
using namespace TechDraw;
QGIHighlight::QGIHighlight() :
m_referenceAngle(0.0)
{
m_refSize = 0.0;
setInteractive(false);
m_circle = new QGraphicsEllipseItem();
addToGroup(m_circle);
m_circle->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_rect = new QGCustomRect();
addToGroup(m_rect);
m_rect->setFlag(QGraphicsItem::ItemIsSelectable, false);
m_reference = new QGCustomText();
addToGroup(m_reference);
m_reference->setFlag(QGraphicsItem::ItemIsSelectable, false);
setWidth(Rez::guiX(0.75));
}
QGIHighlight::~QGIHighlight()
{
}
// QGIHighlight is no longer dragged except through TaskDetail.
void QGIHighlight::onDragFinished()
{
// Base::Console().message("QGIH::onDragFinished - pos: %s\n",
// DrawUtil::formatVector(pos()).c_str());
QGraphicsItem* parent = parentItem();
auto qgivp = dynamic_cast<QGIViewPart*>(parent);
if (qgivp) {
qgivp->highlightMoved(this, pos());
}
}
void QGIHighlight::draw()
{
prepareGeometryChange();
makeHighlight();
makeReference();
update();
}
void QGIHighlight::makeHighlight()
{
QRectF r(m_start, m_end);
m_circle->setRect(r);
m_rect->setRect(r);
if (getHoleStyle() == 0) {
m_rect->hide();
m_circle->show();
} else {
m_rect->show();
m_circle->hide();
}
}
void QGIHighlight::makeReference()
{
prepareGeometryChange();
int fontSize = QGIView::exactFontSize(m_refFont.family().toStdString(),
m_refSize);
m_refFont .setPixelSize(fontSize);
m_reference->setFont(m_refFont);
m_reference->setPlainText(m_refText);
double vertOffset = 0.0;
if (m_referenceAngle >= 0.0 &&
m_referenceAngle <= 180.0) {
//above the X axis
//referenceText is positioned by top-left, need to adjust upwards by text height
vertOffset = m_reference->boundingRect().height();
} else {
//below X axis. need to adjust upwards a bit because there is blank space above text
vertOffset = m_reference->tightBoundingAdjust().y();
}
double horizOffset = 0.0;
if (m_referenceAngle > 90.0 &&
m_referenceAngle < 270.0) {
//to left of Y axis
horizOffset = -m_reference->boundingRect().width();
}
QRectF r(m_start, m_end);
double radius = r.width() / 2.0;
QPointF center = r.center();
double angleRad = Base::toRadians(m_referenceAngle);
double posX = center.x() + cos(angleRad) * radius + horizOffset;
double posY = center.y() - sin(angleRad) * radius - vertOffset;
m_reference->setPos(posX, posY);
double highRot = rotation();
if (!TechDraw::DrawUtil::fpCompare(highRot, 0.0)) {
QRectF refBR = m_reference->boundingRect();
QPointF refCenter = refBR.center();
m_reference->setTransformOriginPoint(refCenter);
m_reference->setRotation(-highRot);
}
}
void QGIHighlight::setInteractive(bool state)
{
// setAcceptHoverEvents(state);
setFlag(QGraphicsItem::ItemIsSelectable, state);
setFlag(QGraphicsItem::ItemIsMovable, state);
setFlag(QGraphicsItem::ItemSendsScenePositionChanges, state);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, state);
}
void QGIHighlight::setBounds(double x1, double y1, double x2, double y2)
{
m_start = QPointF(Rez::guiX(x1), Rez::guiX(-y1));
m_end = QPointF(Rez::guiX(x2), Rez::guiX(-y2));
}
void QGIHighlight::setReference(const char* ref)
{
m_refText = QString::fromUtf8(ref);
}
void QGIHighlight::setFont(QFont f, double fsize)
{
m_refFont = f;
m_refSize = fsize;
}
//obs?
QColor QGIHighlight::getHighlightColor()
{
return PreferencesGui::sectionLineQColor();
}
int QGIHighlight::getHoleStyle()
{
return TechDraw::Preferences::mattingStyle();
}
void QGIHighlight::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) {
QStyleOptionGraphicsItem myOption(*option);
// myOption.state &= ~QStyle::State_Selected;
setTools();
// painter->drawRect(boundingRect()); //good for debugging
QGIDecoration::paint (painter, &myOption, widget);
}
void QGIHighlight::setTools()
{
m_circle->setPen(m_pen);
m_rect->setPen(m_pen);
m_reference->setDefaultTextColor(m_pen.color());
}
void QGIHighlight::setLinePen(QPen isoPen)
{
m_pen = isoPen;
}