Add HTML/CSS Styles to Annotation

This commit is contained in:
WandererFan 2016-02-09 17:47:15 -05:00 committed by wmayer
parent c7d845cc5b
commit 699c066991
7 changed files with 99 additions and 19 deletions

View File

@ -29,6 +29,8 @@
#endif
#include <QColor>
#include <sstream>
#include <iomanip>
namespace App
{
@ -116,6 +118,18 @@ public:
{
return(QColor(int(r*255.0),int(g*255.0),int(b*255.0)));
}
/**
* returns color as CSS color "#RRGGBB"
*
*/
std::string asCSSString() {
std::stringstream ss;
ss << "#" << std::hex << std::uppercase << std::setfill('0') << std::setw(2) << int(r*255.0)
<< std::setw(2) << int(g*255.0)
<< std::setw(2) << int(b*255.0);
return ss.str();
}
/// color values, public accesible
float r,g,b,a;
};

View File

@ -48,6 +48,11 @@ using namespace std;
PROPERTY_SOURCE(TechDraw::DrawViewAnnotation, TechDraw::DrawView)
const char* DrawViewAnnotation::TextFormatEnums[]= {"Normal",
"Bold",
"Italic",
"Bold-Italic",
NULL};
DrawViewAnnotation::DrawViewAnnotation(void)
{
@ -58,10 +63,15 @@ DrawViewAnnotation::DrawViewAnnotation(void)
std::string fontName = hGrp->GetASCII("LabelFont", "osifont");
ADD_PROPERTY_TYPE(Text ,("Default Text"),vgroup,App::Prop_None,"The text to be displayed");
ADD_PROPERTY_TYPE(Font ,(fontName.c_str()) ,vgroup,App::Prop_None, "The name of the font to use");
ADD_PROPERTY_TYPE(Font ,(fontName.c_str()),vgroup,App::Prop_None, "The name of the font to use");
ADD_PROPERTY_TYPE(TextColor,(0.0f,0.0f,0.0f),vgroup,App::Prop_None,"The color of the text");
ADD_PROPERTY_TYPE(TextSize,(8),vgroup,App::Prop_None,"The size of the text in mm");
ADD_PROPERTY_TYPE(MaxWidth,(-1.0),vgroup,App::Prop_None,"The maximum width of the Annotation block");
ADD_PROPERTY_TYPE(LineSpace,(80),vgroup,App::Prop_None,"Line spacing adjustment");
TextFormat.setEnums(TextFormatEnums);
ADD_PROPERTY(TextFormat, ((long)0));
//Scale.StatusBits.set(3); //hide scale. n/a for Annotation
//ScaleType.StatusBits.set(3);
@ -73,9 +83,30 @@ DrawViewAnnotation::~DrawViewAnnotation()
{
}
void DrawViewAnnotation::onChanged(const App::Property* prop)
{
if (!isRestoring()) {
if (prop == &Text ||
prop == &Font ||
prop == &TextColor ||
prop == &TextSize ||
prop == &LineSpace ||
prop == &TextFormat || //changing this doesn't recompute until focus changes??
prop == &MaxWidth) {
try {
App::DocumentObjectExecReturn *ret = recompute();
delete ret;
}
catch (...) {
}
}
}
TechDraw::DrawView::onChanged(prop);
}
App::DocumentObjectExecReturn *DrawViewAnnotation::execute(void)
{
return App::DocumentObject::StdReturn;
return TechDraw::DrawView::execute();
}
// Python Drawing feature ---------------------------------------------------------

View File

@ -21,7 +21,7 @@
* *
***************************************************************************/
#ifndef _DrawViewAnnotation_h_
@ -30,9 +30,10 @@
#include <App/DocumentObject.h>
#include <App/PropertyLinks.h>
#include "DrawView.h"
#include "App/PropertyStandard.h"
#include <App/FeaturePython.h>
#include "DrawView.h"
namespace TechDraw
{
@ -53,6 +54,9 @@ public:
App::PropertyString Font;
App::PropertyColor TextColor;
App::PropertyInteger TextSize;
App::PropertyInteger LineSpace;
App::PropertyEnumeration TextFormat; // Plain,Bold,Italic,Bold-Italic
App::PropertyFloat MaxWidth;
/** @name methods overide Feature */
//@{
@ -64,6 +68,12 @@ public:
virtual const char* getViewProviderName(void) const {
return "TechDrawGui::ViewProviderAnnotation";
}
protected:
virtual void onChanged(const App::Property* prop);
private:
static const char* TextFormatEnums[];
};
typedef App::FeaturePythonT<DrawViewAnnotation> DrawViewAnnotationPython;

View File

@ -82,7 +82,8 @@ PyObject* DrawViewClipPy::getChildViewNames(PyObject* args)
std::vector<std::string>::iterator it = strings.begin();
for( ; it != strings.end(); it++) {
PyObject* pString = PyString_FromString(it->c_str()); //TODO: unicode & py3
int rc = PyList_Append(result, pString);
//int rc =
static_cast<void> (PyList_Append(result, pString));
}
// PyErr_SetString(PyExc_NotImplementedError, "Not yet implemented");

View File

@ -60,7 +60,7 @@ public:
}
protected:
void onChanged(const App::Property* prop);
virtual void onChanged(const App::Property* prop);
Base::BoundBox3d bbox;
std::string getSVGHead(void);
std::string getSVGTail(void);

View File

@ -58,7 +58,7 @@ public:
}
protected:
void onChanged(const App::Property* prop);
virtual void onChanged(const App::Property* prop);
Base::BoundBox3d bbox;
};

View File

@ -38,6 +38,11 @@
#endif
#include <qmath.h>
#include <QTextDocument>
#include <QTextBlock>
#include <QTextBlockFormat>
#include <QTextFrame>
#include <QSizeF>
#include <App/Application.h>
#include <App/Material.h>
@ -59,8 +64,13 @@ QGIViewAnnotation::QGIViewAnnotation(const QPoint &pos, QGraphicsScene *scene)
setFlag(QGraphicsItem::ItemIsMovable, true);
m_textItem = new QGCustomText();
m_textItem->setTextInteractionFlags(Qt::NoTextInteraction);
//To allow on screen editing of text:
//m_textItem->setTextInteractionFlags(Qt::TextEditorInteraction); //this works
//QObject::connect(QGraphicsTextItem::document(), SIGNAL(contentsChanged()),m_textItem, SLOT(updateText())); //not tested
addToGroup(m_textItem);
m_textItem->setPos(0.,0.);
}
QGIViewAnnotation::~QGIViewAnnotation()
@ -114,29 +124,43 @@ void QGIViewAnnotation::drawAnnotation()
TechDraw::DrawViewAnnotation *viewAnno = dynamic_cast<TechDraw::DrawViewAnnotation *>(getViewObject());
// get the Text values
const std::vector<std::string>& annoText = viewAnno->Text.getValues();
//build HTML/CSS formating around Text lines
std::stringstream ss;
ss << "<html>\n<head>\n<style>\n";
ss << "p {";
ss << "font-family:" << viewAnno->Font.getValue() << "; ";
ss << "font-size:" << viewAnno->TextSize.getValue() << "pt; "; //units compatibility???
if (viewAnno->TextFormat.isValue("Normal")) {
ss << "font-weight:normal; font-style:normal; ";
} else if (viewAnno->TextFormat.isValue("Bold")) {
ss << "font-weight:bold; font-style:normal; ";
} else if (viewAnno->TextFormat.isValue("Italic")) {
ss << "font-weight:normal; font-style:italic; ";
} else if (viewAnno->TextFormat.isValue("Bold-Italic")) {
ss << "font-weight:bold; font-style:italic; ";
} else {
Base::Console().Warning("%s has invalid TextFormat\n",viewAnno->getNameInDocument());
ss << "font-weight:normal; font-style:normal; ";
}
ss << "line-height:" << viewAnno->LineSpace.getValue() << "%; ";
App::Color c = viewAnno->TextColor.getValue();
ss << "color:" << c.asCSSString() << "; ";
ss << "}\n</style>\n</head>\n<body>\n<p>";
for(std::vector<std::string>::const_iterator it = annoText.begin(); it != annoText.end(); it++) {
if (it == annoText.begin()) {
ss << *it;
} else {
ss << "\n" << *it ;
ss << "<br>" << *it ;
}
}
QFont font;
font.setFamily(QString::fromUtf8(viewAnno->Font.getValue()));
font.setPointSizeF(viewAnno->TextSize.getValue()); //scene units (mm), not points
m_textItem->setFont(font);
App::Color c = viewAnno->TextColor.getValue();
m_textItem->setDefaultTextColor(c.asQColor());
ss << "</p>\n</body>\n</html> ";
prepareGeometryChange();
m_textItem->setTextWidth(viewAnno->MaxWidth.getValue());
QString qs = QString::fromUtf8(ss.str().c_str());
m_textItem->setPlainText(qs);
m_textItem->adjustSize();
m_textItem->setHtml(qs);
m_textItem->setPos(0.,0.);
}