File size: 7,520 Bytes
a5ffdcd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | /*******************************************************************************
*
This file is part of the LibreCAD project, a 2D CAD program
Copyright (C) 2025 LibreCAD.org
Copyright (C) 2025 sand1024
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#include "lc_graphicviewportrenderer.h"
#include "lc_defaults.h"
#include "lc_graphicviewport.h"
#include "lc_linemath.h"
#include "rs_entity.h"
#include "rs_graphic.h"
#include "rs_painter.h"
#include "rs_units.h"
LC_GraphicViewportRenderer::LC_GraphicViewportRenderer(LC_GraphicViewport* v, QPaintDevice* painterDevice):
pd{painterDevice}
, viewport{v}
, graphic {viewport->getGraphic()}
{
}
void LC_GraphicViewportRenderer::render() {
renderBoundingClipRect = prepareBoundingClipRect();
doRender();
}
void LC_GraphicViewportRenderer::renderEntityAsChild(RS_Painter *painter, RS_Entity *e) {
#ifdef DEBUG_RENDERING
drawEntityCount++;
drawTimer.start();
#endif
e->drawAsChild(painter);
#ifdef DEBUG_RENDERING
qint64 elapsed = drawTimer.nsecsElapsed();
entityDrawTime+= elapsed;
#endif
}
void LC_GraphicViewportRenderer::loadSettings() {
auto g = getGraphic();
if (g != nullptr){
updateGraphicRelatedSettings(g);
}
}
LC_Rect LC_GraphicViewportRenderer::prepareBoundingClipRect(){
int width = viewport->getWidth();
int height = viewport->getHeight();
const RS_Vector ucsViewportLeftBottom = viewport->toUCSFromGui(0, 0);
const RS_Vector ucsViewportRightTop = viewport->toUCSFromGui(width, height);
if (viewport->hasUCS()){
// here were extend (enlarge) clipping rect to ensure that if there is shift/rotation in ucs, resulting bounding box cover the entire screen
// thus we'll paint a bit more entities that is necessary (and more comparing to non-ucs world mode), yet ensure that they are not clipped
RS_Vector wcsViewportMin;
RS_Vector wcsViewportMax;
viewport->worldBoundingBox(ucsViewportLeftBottom, ucsViewportRightTop, wcsViewportMin, wcsViewportMax);
return LC_Rect(wcsViewportMin, wcsViewportMax);
}
else{
// clipping rect is defined by the screen (0,0 and width/height)
return LC_Rect(ucsViewportLeftBottom, ucsViewportRightTop);
}
}
bool LC_GraphicViewportRenderer::isOutsideOfBoundingClipRect(RS_Entity* e, bool constructionEntity){
// test if the entity is in the viewport
switch (e->rtti()){
/* case RS2::EntityGraphic:
break;*/
case RS2::EntityLine:{
if (constructionEntity){
if (!LC_LineMath::hasIntersectionLineRect(e->getMin(), e->getMax(), renderBoundingClipRect.minP(), renderBoundingClipRect.maxP())){
return true;
}
}
else{ // normal line
if (e->getMax().x < renderBoundingClipRect.minP().x || e->getMin().x > renderBoundingClipRect.maxP().x ||
e->getMin().y > renderBoundingClipRect.maxP().y || e->getMax().y < renderBoundingClipRect.minP().y){
return true;
}
}
break;
}
default:
if (e->getMax().x < renderBoundingClipRect.minP().x || e->getMin().x > renderBoundingClipRect.maxP().x ||
e->getMin().y > renderBoundingClipRect.maxP().y || e->getMax().y < renderBoundingClipRect.minP().y){
return true;
}
}
return false;
}
/**
* Draws an entity.
* The painter must be initialized and all the attributes (pen) must be set.
*/
void LC_GraphicViewportRenderer::justDrawEntity(RS_Painter *painter, RS_Entity *e) {
#ifdef DEBUG_RENDERING
drawEntityCount++;
drawTimer.start();
#endif
e->draw(painter);
#ifdef DEBUG_RENDERING
qint64 elapsed = drawTimer.nsecsElapsed();
entityDrawTime+= elapsed;
#endif
}
void LC_GraphicViewportRenderer::updateEndCapsStyle(const RS_Graphic *graphic) {// Lineweight endcaps setting for new objects:
// 0 = none; 1 = round; 2 = angle; 3 = square
int endCaps = graphic->getGraphicVariableInt("$ENDCAPS", 1);
switch (endCaps){
case 0:
penCapStyle = Qt::FlatCap;
break;
case 1:
penCapStyle = Qt::RoundCap;
break;
case 2:
penCapStyle = Qt::MPenCapStyle;
break;
case 3:
penCapStyle = Qt::SquareCap;
break;
default:
penCapStyle = Qt::FlatCap; // fixme - or round?
}
}
void LC_GraphicViewportRenderer::updatePointEntitiesStyle(RS_Graphic *graphic) {
pdmode = graphic->getGraphicVariableInt("$PDMODE", LC_DEFAULTS_PDMode);
pdsize = graphic->getGraphicVariableDouble("$PDSIZE", LC_DEFAULTS_PDSize);
}
void LC_GraphicViewportRenderer::updateJoinStyle(const RS_Graphic *graphic) {//0=none; 1= round; 2 = angle; 3 = flat
int joinStyle = graphic->getGraphicVariableInt("$JOINSTYLE", 1);
switch (joinStyle){
case 0:
penJoinStyle = Qt::BevelJoin;
break;
case 1:
penJoinStyle = Qt::RoundJoin;
break;
case 2:
penJoinStyle = Qt::MiterJoin;
break;
case 3:
penJoinStyle = Qt::BevelJoin;
break;
default:
penJoinStyle = Qt::RoundJoin;
}
}
void LC_GraphicViewportRenderer::setBackground(const RS_Color &bg) {
m_colorBackground = bg;
RS_Color black(0, 0, 0);
if (black.colorDistance(bg) >= RS_Color::MinColorDistance) {
m_colorForeground = black;
} else {
m_colorForeground = RS_Color(255, 255, 255);
}
}
void LC_GraphicViewportRenderer::updateGraphicRelatedSettings(RS_Graphic *g) {
updateUnitAndDefaultWidthFactors(g);
updatePointEntitiesStyle(g);
updateEndCapsStyle(g);
updateJoinStyle(g);
updateAnglesBasis(g);
}
void LC_GraphicViewportRenderer::updateUnitAndDefaultWidthFactors(const RS_Graphic *g) {
unitFactor = RS_Units::convert(1.0, RS2::Millimeter, g->getUnit());
unitFactor100 = unitFactor / 100.0;
defaultWidthFactor = g->getVariableDouble("$DIMSCALE", 1.0);
}
void LC_GraphicViewportRenderer::setupPainter(RS_Painter *painter) {
painter->setRenderer(this);
painter->setViewPort(viewport);
painter->updatePointsScreenSize(pdsize);
painter->setPointsMode(pdmode);
painter->setDefaultWidthFactor(defaultWidthFactor);
painter->setWorldBoundingRect(renderBoundingClipRect);
}
bool LC_GraphicViewportRenderer::isTextLineNotRenderable([[maybe_unused]]double d) const {
return false;
}
void LC_GraphicViewportRenderer::updateAnglesBasis(RS_Graphic *g) {
m_angleBasisBaseAngle = g->getAnglesBase();
m_angleBasisCounterClockwise = g->areAnglesCounterClockWise();
}
|