[TD]make symbol text adjustable

- also fix QGIWeldSymbol bounding rect
This commit is contained in:
wandererfan
2019-12-24 12:04:18 -05:00
committed by WandererFan
parent 983c038949
commit b3a926f1d4
7 changed files with 160 additions and 57 deletions

View File

@@ -33,6 +33,7 @@
#include <App/Material.h>
#include <Base/Console.h>
#include <Base/Parameter.h>
#include <Base/Tools.h>
#include <Mod/TechDraw/App/DrawUtil.h>
#include <Mod/TechDraw/App/DrawTile.h>
@@ -114,7 +115,6 @@ void QGITile::draw(void)
prepareGeometryChange();
m_wide = getSymbolWidth();
// m_high = getSymbolHeight() * scaleToFont();
m_high = getSymbolHeight();
makeText();
@@ -161,34 +161,45 @@ void QGITile::makeSymbol(void)
}
// m_qgSvg->setGraphicsEffect(m_effect);
QFileInfo fi(m_svgPath);
if (fi.isReadable()) {
QFile svgFile(m_svgPath);
if(svgFile.open(QIODevice::ReadOnly)) {
QByteArray qba = svgFile.readAll();
if (!m_qgSvg->load(&qba)) {
Base::Console().Error("Error - Could not load SVG renderer with **%s**\n", qPrintable(m_svgPath));
return;
}
svgFile.close();
// m_qgSvg->setScale(scaleToFont());
m_qgSvg->setScale(getSymbolFactor());
m_qgSvg->centerAt(0.0, 0.0); //(0,0) is based on symbol size
} else {
Base::Console().Error("Error - Could not open file **%s**\n", qPrintable(m_svgPath));
}
} else {
Base::Console().Error("QGIT::makeSymbol - file: **%s** is not readable\n",qPrintable(m_svgPath));
QByteArray qba = getSvgString(m_svgPath);
if (qba.isEmpty()) {
Base::Console().Message("QGIT::makeSymbol - no data from file: %s\n", qPrintable(m_svgPath));
return;
}
if (!m_qgSvg->load(&qba)) {
Base::Console().Error("Error - Could not load SVG renderer with **%s**\n", qPrintable(m_svgPath));
return;
}
m_qgSvg->setScale(getSymbolFactor());
m_qgSvg->centerAt(0.0, 0.0); //(0,0) is based on symbol size
}
//re PropertyFileIncluded locking problem - ensure Qt file functions destroyed by going out of scope
QByteArray QGITile::getSvgString(QString svgPath)
{
QByteArray qba;
QFileInfo fi(svgPath);
if (fi.isReadable()) {
QFile svgFile(svgPath);
if(svgFile.open(QIODevice::ReadOnly)) {
qba = svgFile.readAll();
svgFile.close();
} else {
Base::Console().Error("Error - Could not open file **%s**\n", qPrintable(svgPath));
}
} else {
Base::Console().Error("QGIT::makeSymbol - file: **%s** is not readable\n",qPrintable(svgPath));
}
return qba;
}
void QGITile::makeText(void)
{
// Base::Console().Message("QGIT::makeText()\n");
prepareGeometryChange();
m_font.setPixelSize(prefFontSize());
// m_font.setPixelSize(prefFontSize());
double verticalFudge = 0.10;
//(0, 0) is 1/2 up symbol (above line symbol)!
@@ -204,8 +215,8 @@ void QGITile::makeText(void)
double vertAdjust = 0.0;
double minVertAdjust = prefFontSize() * 0.1;
if (prefFontSize() > m_high) { //text is bigger than symbol
vertAdjust = ((prefFontSize() - m_high) / 2.0) + minVertAdjust;
if (m_font.pixelSize() > m_high) {
vertAdjust = ((m_font.pixelSize() - m_high) / 2.0) + minVertAdjust;
}
double textHeightL = m_qgTextL->boundingRect().height();
@@ -273,12 +284,20 @@ void QGITile::setTileTextCenter(std::string s)
m_textC = QString::fromUtf8(s.c_str());
}
//using label font and dimension font size. could change later
//void QGITile::setFont(QFont f, double fsize)
//{
// m_font = f;
// m_textSize = fsize;
//}
void QGITile::setFont(QFont f, double fSizePx)
{
// Base::Console().Message("QGIT::setFont(%s, %.3f)\n", qPrintable(f.family()), fSizePx);
m_font = f;
m_font.setPixelSize(fSizePx);
}
void QGITile::setFont(std::string fName, double fSizePx)
{
QString qFName = Base::Tools::fromStdString(fName);
QFont f(qFName);
setFont(f, fSizePx);
}
void QGITile::setSymbolFile(std::string s)
{
@@ -385,17 +404,6 @@ double QGITile::prefFontSize(void) const
return fontSize;
}
//factor to scale symbol to match font size
double QGITile::scaleToFont(void) const
{
double fpx = prefFontSize();
double spx = getSymbolHeight();
// double factor = getSymbolFactor();
double factor = 1.0;
double sf = (fpx / spx) * factor;
return sf;
}
QString QGITile::prefTextFont(void) const
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().

View File

@@ -64,7 +64,8 @@ public:
void setTileTextLeft(std::string s);
void setTileTextRight(std::string s);
void setTileTextCenter(std::string s);
void setFont(QFont f, double fsize);
void setFont(QFont f, double fSizePx);
void setFont(std::string fName, double fSizePx);
void setSymbolFile(std::string s);
void setTilePosition(QPointF org, int r, int c);
void setTileScale(double s);
@@ -84,9 +85,10 @@ protected:
double getSymbolWidth(void) const;
double getSymbolHeight(void) const;
double getSymbolFactor(void) const;
QByteArray getSvgString(QString svgPath);
QString prefTextFont(void) const;
double prefFontSize(void) const;
double scaleToFont(void) const;
void makeSymbol(void);
void makeText(void);

View File

@@ -43,6 +43,7 @@
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Parameter.h>
#include <Base/Tools.h>
#include <Base/UnitsApi.h>
#include <Gui/Command.h>
@@ -164,7 +165,6 @@ void QGIWeldSymbol::updateView(bool update)
void QGIWeldSymbol::draw()
{
// Base::Console().Message("QGIWS::draw()- %s\n", getFeature()->getNameInDocument());
if (!isVisible()) {
return;
}
@@ -195,6 +195,18 @@ void QGIWeldSymbol::drawTile(TechDraw::DrawTileWeld* tileFeat)
return;
}
const auto sym( dynamic_cast<TechDraw::DrawWeldSymbol *>(getViewObject()) );
if( sym == nullptr ) {
return;
}
auto vp = static_cast<ViewProviderWeld*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
std::string fontName = vp->Font.getValue();
double sizeMM = vp->TileFontSize.getValue();
double fontSize = QGIView::calculateFontPixelSize(sizeMM);
double featScale = m_leadFeat->getScale();
std::string tileTextL = tileFeat->LeftText.getValue();
@@ -209,6 +221,7 @@ void QGIWeldSymbol::drawTile(TechDraw::DrawTileWeld* tileFeat)
QPointF org = getTileOrigin();
tile->setTilePosition(org, row, col);
tile->setFont(fontName, fontSize);
tile->setColor(getCurrentColor());
tile->setTileTextLeft(tileTextL);
tile->setTileTextRight(tileTextR);
@@ -225,9 +238,13 @@ void QGIWeldSymbol::drawTile(TechDraw::DrawTileWeld* tileFeat)
void QGIWeldSymbol::drawAllAround(void)
{
// Base::Console().Message("QGIWS::drawAllAround()\n");
QPointF allAroundPos = getKinkPoint();
m_allAround->setPos(allAroundPos);
if (getFeature()->AllAround.getValue()) {
m_allAround->show();
} else {
m_allAround->hide();
return;
}
@@ -239,16 +256,13 @@ void QGIWeldSymbol::drawAllAround(void)
double width = m_qgLead->getLineWidth();
m_allAround->setWidth(width);
m_allAround->setZValue(ZVALUE::DIMENSION);
QPointF allAroundPos = getKinkPoint();
m_allAround->setPos(allAroundPos);
}
void QGIWeldSymbol::drawTailText(void)
{
// Base::Console().Message("QGIWS::drawTailText()\n");
QPointF textPos = getTailPoint();
m_tailText->setPos(textPos); //avoid messing up brect with empty item at 0,0
m_tailText->setPos(textPos); //avoid messing up brect with empty item at 0,0 !!!
std::string tText = getFeature()->TailText.getValue();
if (tText.empty()) {
m_tailText->hide();
@@ -256,9 +270,21 @@ void QGIWeldSymbol::drawTailText(void)
} else {
m_tailText->show();
}
const auto sym( dynamic_cast<TechDraw::DrawWeldSymbol *>(getViewObject()) );
if( sym == nullptr ) {
return;
}
auto vp = static_cast<ViewProviderWeld*>(getViewProvider(getViewObject()));
if ( vp == nullptr ) {
return;
}
std::string fontName = vp->Font.getValue();
QString qFontName = Base::Tools::fromStdString(fontName);
double sizeMM = vp->FontSize.getValue();
double fontSize = QGIView::calculateFontPixelSize(sizeMM);
m_font.setFamily(getPrefFont());
m_font.setPixelSize(prefFontSize());
m_font.setFamily(qFontName);
m_font.setPixelSize(fontSize);
m_tailText->setFont(m_font);
m_tailText->setPlainText(
@@ -283,6 +309,9 @@ void QGIWeldSymbol::drawTailText(void)
void QGIWeldSymbol::drawFieldFlag()
{
// Base::Console().Message("QGIWS::drawFieldFlag()\n");
QPointF fieldFlagPos = getKinkPoint();
m_fieldFlag->setPos(fieldFlagPos);
if (getFeature()->FieldWeld.getValue()) {
m_fieldFlag->show();
} else {
@@ -308,9 +337,6 @@ void QGIWeldSymbol::drawFieldFlag()
m_fieldFlag->setZValue(ZVALUE::DIMENSION);
m_fieldFlag->setPath(path);
QPointF fieldFlagPos = getKinkPoint();
m_fieldFlag->setPos(fieldFlagPos);
}
void QGIWeldSymbol::getTileFeats(void)
@@ -353,7 +379,7 @@ void QGIWeldSymbol::removeQGITiles(void)
}
}
std::vector<QGITile*> QGIWeldSymbol::getQGITiles(void)
std::vector<QGITile*> QGIWeldSymbol::getQGITiles(void) const
{
std::vector<QGITile*> result;
QList<QGraphicsItem*> children = childItems();
@@ -511,9 +537,29 @@ double QGIWeldSymbol::prefFontSize(void) const
QRectF QGIWeldSymbol::boundingRect() const
{
return childrenBoundingRect();
return customBoundingRect();
}
QRectF QGIWeldSymbol::customBoundingRect() const
{
QRectF result;
QRectF childRect = mapFromItem(m_tailText, m_tailText->boundingRect()).boundingRect();
result = result.united(childRect);
childRect = mapFromItem(m_fieldFlag, m_fieldFlag->boundingRect()).boundingRect();
result = result.united(childRect);
childRect = mapFromItem(m_allAround, m_allAround->boundingRect()).boundingRect();
result = result.united(childRect);
std::vector<QGITile*> qgTiles = getQGITiles();
for (auto& t: qgTiles) {
childRect = mapFromItem(t, t->boundingRect()).boundingRect();
result = result.united(childRect);
}
return result;
}
QPainterPath QGIWeldSymbol::shape() const
{
return QGraphicsItemGroup::shape();

View File

@@ -104,7 +104,7 @@ protected:
protected:
void removeQGITiles(void);
std::vector<QGITile*> getQGITiles(void);
std::vector<QGITile*> getQGITiles(void) const;
virtual QColor prefNormalColor();
double prefArrowSize();
@@ -127,6 +127,8 @@ protected:
bool m_blockDraw; //prevent redraws while updating.
std::string m_weldFeatName;
virtual QRectF customBoundingRect() const;
};
}

View File

@@ -573,7 +573,7 @@ std::vector<App::DocumentObject*> TaskWeldingSymbol::updateTiles(void)
if (m_arrowIn != nullptr) {
tileName = m_arrowIn->getNameInDocument();
}
if (m_arrowIn == nullptr) { // this should never happen on an update!
if (m_arrowIn == nullptr) {
tileName = m_leadFeat->getDocument()->getUniqueObjectName("DrawTileWeld");
Command::doCommand(Command::Doc,"App.activeDocument().addObject('%s','%s')",
tileType.c_str(),tileName.c_str());
@@ -590,7 +590,7 @@ std::vector<App::DocumentObject*> TaskWeldingSymbol::updateTiles(void)
Command::doCommand(Command::Doc,"App.activeDocument().%s.TileColumn = %d",
tileName.c_str(), m_arrowOut.col);
if (m_otherOut.symbolPath.empty()) {
if (m_arrowOut.symbolPath.empty()) {
Command::doCommand(Command::Doc,"App.activeDocument().%s.SymbolFile = ''",
tileName.c_str());
} else {

View File

@@ -57,6 +57,13 @@ PROPERTY_SOURCE(TechDrawGui::ViewProviderWeld, TechDrawGui::ViewProviderDrawingV
ViewProviderWeld::ViewProviderWeld()
{
sPixmap = "actions/techdraw-weldsymbol";
static const char *group = "Text";
ADD_PROPERTY_TYPE(Font, (prefFontName().c_str()),group,App::Prop_None, "The name of the font to use");
ADD_PROPERTY_TYPE(FontSize, (prefFontSize()), group,
(App::PropertyType)(App::Prop_None),"Tail text size");
ADD_PROPERTY_TYPE(TileFontSize, (prefFontSize() * prefTileTextAdjust()), group,
(App::PropertyType)(App::Prop_None),"Text size on individual symbol tiles");
}
ViewProviderWeld::~ViewProviderWeld()
@@ -142,6 +149,34 @@ bool ViewProviderWeld::doubleClicked(void)
return true;
}
std::string ViewProviderWeld::prefFontName(void)
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->
GetGroup("Mod/TechDraw/Labels");
std::string fontName = hGrp->GetASCII("LabelFont", "osifont");
return fontName;
}
double ViewProviderWeld::prefFontSize(void)
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->
GetGroup("Mod/TechDraw/Dimensions");
double fontSize = hGrp->GetFloat("FontSize", QGIView::DefaultFontSizeInMM);
return fontSize;
}
double ViewProviderWeld::prefTileTextAdjust(void)
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->
GetGroup("Mod/TechDraw/Dimensions");
double adjust = hGrp->GetFloat("TileTextAdjust", 0.75);
return adjust;
}
TechDraw::DrawWeldSymbol* ViewProviderWeld::getViewObject() const
{

View File

@@ -45,6 +45,10 @@ public:
/// destructor
virtual ~ViewProviderWeld();
App::PropertyString Font;
App::PropertyLength FontSize;
App::PropertyLength TileFontSize;
virtual void attach(App::DocumentObject *);
virtual void setDisplayMode(const char* ModeName);
virtual bool useNewSelectionModel(void) const {return false;}
@@ -58,6 +62,12 @@ public:
virtual TechDraw::DrawWeldSymbol* getViewObject() const;
virtual TechDraw::DrawWeldSymbol* getFeature() const;
std::string prefFontName(void);
double prefFontSize(void);
double prefTileTextAdjust(void);
};
} // namespace TechDrawGui