/*************************************************************************** * Copyright (c) 2013 Luke Parry * * Copyright (c) 2014 WandererFan * * * * 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 #include #include #include #include #include #include #include #include #include #include #endif #include #include #include #include #include #include #include #include "DlgStringListEditor.h" #include "QGCustomText.h" #include "QGIViewAnnotation.h" #include "Rez.h" using namespace TechDrawGui; QGIViewAnnotation::QGIViewAnnotation() { setCacheMode(QGraphicsItem::NoCache); setFlag(QGraphicsItem::ItemIsSelectable, true); setFlag(QGraphicsItem::ItemIsMovable, true); setAcceptHoverEvents(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.); } void QGIViewAnnotation::setViewAnnoFeature(TechDraw::DrawViewAnnotation* obj) { // called from QGVPage. (once) setViewFeature(static_cast(obj)); } void QGIViewAnnotation::updateView(bool update) { auto viewAnno(dynamic_cast(getViewObject())); if (!viewAnno) { return; } if (update || viewAnno->isTouched() || viewAnno->Text.isTouched() || viewAnno->Font.isTouched() || viewAnno->TextColor.isTouched() || viewAnno->TextSize.isTouched()) { draw(); } QGIView::updateView(update); } void QGIViewAnnotation::draw() { // Base::Console().Message("QGIVA::draw()\n"); if (!isVisible()) { return; } drawAnnotation(); QGIView::draw(); rotateView(); } //TODO: text is positioned slightly high (and left??) on page save to SVG file void QGIViewAnnotation::drawAnnotation() { // Base::Console().Message("QGIVA::drawAnnotation()\n"); auto viewAnno(dynamic_cast(getViewObject())); if (!viewAnno) { return; } const std::vector& annoRawText = viewAnno->Text.getValues(); std::vector annoText; // v0.19- stored text as escapedUnicode // v0.20+ stores text as utf8 for (auto& line : annoRawText) { if (line.find("\\x") == std::string::npos) { // not escaped annoText.push_back(line); } else { // is escaped std::string newLine = Base::Tools::escapedUnicodeToUtf8(line); annoText.push_back(newLine); } } int scaledSize = exactFontSize(viewAnno->Font.getValue(), viewAnno->TextSize.getValue()); //build HTML/CSS formatting around Text lines std::stringstream ss; ss << "\n\n\n\n\n

"; for (std::vector::const_iterator it = annoText.begin(); it != annoText.end(); it++) { if (it != annoText.begin()) { ss << "
"; } //"less than" symbol chops off line. need to use html sub. std::string lt = std::regex_replace((*it), std::regex("<"), "<"); ss << lt; } ss << "

\n\n "; prepareGeometryChange(); m_textItem->setTextWidth(Rez::guiX(viewAnno->MaxWidth.getValue())); QString qs = QString::fromUtf8(ss.str().c_str()); m_textItem->setHtml(qs); m_textItem->centerAt(0., 0.); } void QGIViewAnnotation::rotateView() { QRectF r = m_textItem->boundingRect(); m_textItem->setTransformOriginPoint(r.center()); double rot = getViewObject()->Rotation.getValue(); m_textItem->setRotation(-rot); } void QGIViewAnnotation::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) { Q_UNUSED(event); TechDraw::DrawViewAnnotation* annotation = dynamic_cast(getViewObject()); if (!annotation) { return; } const std::vector& values = annotation->Text.getValues(); DlgStringListEditor dlg(values, Gui::getMainWindow()); dlg.setWindowTitle(QString::fromUtf8("Annotation Text Editor")); if (dlg.exec() == QDialog::Accepted) { App::GetApplication().setActiveTransaction("Set Annotation Text"); annotation->Text.setValues(dlg.getTexts()); App::GetApplication().closeActiveTransaction(); } }