From 6d51083240066742a2ed5fd54e98afac7d0bdf09 Mon Sep 17 00:00:00 2001 From: marioalexis Date: Fri, 10 Sep 2021 17:16:39 -0300 Subject: [PATCH 1/3] Gui: Fix Unicode and UTF-8 encoding character behavior --- src/Gui/propertyeditor/PropertyItem.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index e94075eb73..b98adfa45b 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -2545,7 +2545,6 @@ QVariant PropertyStringListItem::toString(const QVariant& prop) const } QString text = QString::fromUtf8("[%1]").arg(list.join(QLatin1String(","))); - text.replace(QString::fromUtf8("'"),QString::fromUtf8("\\'")); return QVariant(text); } @@ -2553,11 +2552,10 @@ QVariant PropertyStringListItem::toString(const QVariant& prop) const QVariant PropertyStringListItem::value(const App::Property* prop) const { assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyStringList::getClassTypeId())); - QStringList list; - const std::vector& value = ((App::PropertyStringList*)prop)->getValues(); + const std::vector& value = (static_cast(prop))->getValues(); for ( std::vector::const_iterator jt = value.begin(); jt != value.end(); ++jt ) { - list << QString::fromUtf8(Base::Tools::escapedUnicodeToUtf8(*jt).c_str()); + list << QString::fromUtf8((*jt).c_str()); } return QVariant(list); @@ -2570,16 +2568,16 @@ void PropertyStringListItem::setValue(const QVariant& value) QStringList values = value.toStringList(); QString data; QTextStream str(&data); + str.setCodec("UTF-8"); + str << "["; for (QStringList::Iterator it = values.begin(); it != values.end(); ++it) { QString text(*it); - text.replace(QString::fromUtf8("'"),QString::fromUtf8("\\'")); - - std::string pystr = Base::Tools::escapedUnicodeFromUtf8(text.toUtf8()); - pystr = Base::Interpreter().strToPython(pystr.c_str()); - str << "u\"" << pystr.c_str() << "\", "; + std::string pystr = Base::Interpreter().strToPython(text.toUtf8().constData()); + str << "\"" << QString::fromUtf8(pystr.c_str()) << "\", "; } str << "]"; + setPropertyValue(data); } From 63d34c8e417095f231eb0e901f1b76b5df4046fc Mon Sep 17 00:00:00 2001 From: marioalexis Date: Fri, 10 Sep 2021 17:18:19 -0300 Subject: [PATCH 2/3] TechDraw: Fix Unicode and UTF-8 encoding character behavior --- src/Mod/TechDraw/App/DrawViewSymbol.cpp | 4 ++-- src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp | 17 ++++------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewSymbol.cpp b/src/Mod/TechDraw/App/DrawViewSymbol.cpp index 3671a6c83f..d31f763720 100644 --- a/src/Mod/TechDraw/App/DrawViewSymbol.cpp +++ b/src/Mod/TechDraw/App/DrawViewSymbol.cpp @@ -111,7 +111,7 @@ void DrawViewSymbol::onChanged(const App::Property* prop) while (!queryResult.next().isNull()) { QDomElement tspanElement = model.toDomNode(queryResult.current().toNodeModelIndex()).toElement(); - editables.push_back(Base::Tools::escapedUnicodeFromUtf8(tspanElement.text().toStdString().c_str())); + editables.push_back(tspanElement.text().toStdString()); } } else { @@ -188,7 +188,7 @@ App::DocumentObjectExecReturn *DrawViewSymbol::execute(void) // Finally append text node with editable replacement as the only descendant tspanElement.appendChild(symbolDocument.createTextNode( - QString::fromUtf8(Base::Tools::escapedUnicodeToUtf8(editText[count]).c_str()))); + QString::fromUtf8(editText[count].c_str()))); ++count; } diff --git a/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp b/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp index 7c88c270cc..5a252d0b54 100644 --- a/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp @@ -160,18 +160,9 @@ void QGIViewAnnotation::drawAnnotation() if (it != annoText.begin()) { ss << "
"; } - //TODO: there is still a bug here. entering "'" works, save and restore works, but edit after - // save and restore brings "\'" back into text. manually deleting the "\" fixes it until the next - // save/restore/edit cycle. - // a guess is that the editor for propertyStringList is too enthusiastic about substituting. - // the substituting might be necessary for using the strings in Python. - // ' doesn't seem to help in this case. - std::string u8String = Base::Tools::escapedUnicodeToUtf8(*it); //from \x??\x?? to real utf8 - std::string apos = std::regex_replace((u8String), std::regex("\\\\"), ""); //remove doubles. - apos = std::regex_replace((apos), std::regex("\\'"), "'"); //replace escaped apos //"less than" symbol chops off line. need to use html sub. - std::string lt = std::regex_replace((apos), std::regex("<"), "<"); + std::string lt = std::regex_replace((*it), std::regex("<"), "<"); ss << lt; } ss << "

\n\n "; @@ -203,11 +194,11 @@ void QGIViewAnnotation::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) const std::vector &values = annotation->Text.getValues(); QString text; if (values.size() > 0) { - text = QString::fromUtf8(Base::Tools::escapedUnicodeToUtf8(values[0]).c_str()); + text = QString::fromUtf8(values[0].c_str()); for (unsigned int i = 1; i < values.size(); ++i) { text += QChar::fromLatin1('\n'); - text += QString::fromUtf8(Base::Tools::escapedUnicodeToUtf8(values[i]).c_str()); + text += QString::fromUtf8(values[i].c_str()); } } @@ -233,7 +224,7 @@ void QGIViewAnnotation::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) std::vector newValues; for (int i = 0; i < list.size(); ++i) { - newValues.push_back(Base::Tools::escapedUnicodeFromUtf8(list[i].toStdString().c_str())); + newValues.push_back(list[i].toStdString()); } App::GetApplication().setActiveTransaction("Set Annotation Text"); From 00e95ba55d09da5feab708924577834eaf60c29a Mon Sep 17 00:00:00 2001 From: marioalexis Date: Fri, 10 Sep 2021 17:41:19 -0300 Subject: [PATCH 3/3] Draft: Fix Unicode and UTF-8 encoding character behavior --- src/Mod/Draft/DraftGui.py | 6 +++++- src/Mod/Draft/draftguitools/gui_texts.py | 13 +++++-------- src/Mod/Draft/draftviewproviders/view_label.py | 6 ++---- src/Mod/Draft/draftviewproviders/view_text.py | 6 +++++- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Mod/Draft/DraftGui.py b/src/Mod/Draft/DraftGui.py index 2e254f123b..6aa812558f 100644 --- a/src/Mod/Draft/DraftGui.py +++ b/src/Mod/Draft/DraftGui.py @@ -1740,7 +1740,11 @@ class DraftToolBar: """this function sends the entered text to the active draft command if enter has been pressed twice. Otherwise it blanks the line. """ - self.sourceCmd.text = self.textValue.toPlainText().splitlines() + self.sourceCmd.text = self.textValue.toPlainText()\ + .replace("\\","\\\\")\ + .replace("\"","\\\"")\ + .replace("\'","\\\'")\ + .splitlines() self.sourceCmd.createObject() def displayPoint(self, point=None, last=None, plane=None, mask=None): diff --git a/src/Mod/Draft/draftguitools/gui_texts.py b/src/Mod/Draft/draftguitools/gui_texts.py index ef40496ffb..b0038d0f1b 100644 --- a/src/Mod/Draft/draftguitools/gui_texts.py +++ b/src/Mod/Draft/draftguitools/gui_texts.py @@ -86,19 +86,16 @@ class Text(gui_base_original.Creator): def createObject(self): """Create the actual object in the current document.""" text_list = self.text - text_list = [text.replace("\"","\\\"") for text in text_list] + + if not text_list: + self.finish() + return None # If the last element is an empty string "" we remove it if not text_list[-1]: text_list.pop() - # For Python 2 we convert the string to unicode, - # Python 3 nothing needs to be done - if sys.version_info.major < 3: - u_list = [unicode(line) for line in text_list] - t_list = ['"' + str(line.encode("utf8")) + '"' for line in u_list] - else: - t_list = ['"' + line + '"' for line in text_list] + t_list = ['"' + line + '"' for line in text_list] list_as_text = ", ".join(t_list) diff --git a/src/Mod/Draft/draftviewproviders/view_label.py b/src/Mod/Draft/draftviewproviders/view_label.py index 79bfe73607..197a3f5cab 100644 --- a/src/Mod/Draft/draftviewproviders/view_label.py +++ b/src/Mod/Draft/draftviewproviders/view_label.py @@ -229,6 +229,7 @@ class ViewProviderLabel(ViewProviderDraftAnnotation): self.text2d.string = self.text3d.string = "Label" self.text2d.justification = coin.SoText2.RIGHT self.text3d.justification = coin.SoAsciiText.RIGHT + self.font.name = utils.get_param("textfont") switchnode = coin.SoSeparator() switchnode.addChild(self.line) @@ -310,10 +311,7 @@ class ViewProviderLabel(ViewProviderDraftAnnotation): self.text2d.string.setValue("") self.text3d.string.setValue("") - if sys.version_info.major >= 3: - _list = [l for l in obj.Text if l] - else: - _list = [l.encode("utf8") for l in obj.Text if l] + _list = [l for l in obj.Text if l] self.text2d.string.setValues(_list) self.text3d.string.setValues(_list) diff --git a/src/Mod/Draft/draftviewproviders/view_text.py b/src/Mod/Draft/draftviewproviders/view_text.py index db80e8c881..8da0e4b28b 100644 --- a/src/Mod/Draft/draftviewproviders/view_text.py +++ b/src/Mod/Draft/draftviewproviders/view_text.py @@ -245,9 +245,13 @@ class ViewProviderText(ViewProviderDraftAnnotation): def createObject(self): + import FreeCAD import FreeCADGui if hasattr(self,"Object"): - txt = [t.replace("\"","\\\"") for t in self.text] + txt = self.text + if not txt: + self.finish() + return None # If the last element is an empty string "" we remove it if not txt[-1]: txt.pop()