Salome HOME
CEA : Lot2 - Point coordinates
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetLabel.cpp
1 // Copyright (C) 2014-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_WidgetLabel.h"
21
22 #include <Config_WidgetAPI.h>
23 #include <Config_Keywords.h>
24 #include <ModuleBase_IconFactory.h>
25 #include <ModuleBase_Tools.h>
26
27 #include <ModelAPI_AttributeString.h>
28 #include <Config_Translator.h>
29
30 #include <QTextCodec>
31 #include <QLabel>
32 #include <QVBoxLayout>
33
34
35 ModuleBase_WidgetLabel::ModuleBase_WidgetLabel(QWidget* theParent,
36                                                const Config_WidgetAPI* theData)
37 : ModuleBase_ModelWidget(theParent, theData)
38 {
39   QString aText = translate(theData->getProperty("title"));
40   bool aIsHtml = theData->getBooleanAttribute(ATTR_HTML_STYLE, false);
41
42   QString aLabelIcon = QString::fromStdString(theData->getProperty("icon"));
43   myLabel = new QLabel(aText, theParent);
44   if (!aLabelIcon.isEmpty()) {
45     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
46     myLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
47   } else {
48     myLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
49   }
50   myLabel->setWordWrap(true);
51   myLabel->setIndent(5);
52   myLabel->setContentsMargins(0,0,0,4);
53   if (aIsHtml)
54     myLabel->setTextFormat(Qt::RichText);
55
56   QVBoxLayout* aLayout = new QVBoxLayout(this);
57   ModuleBase_Tools::zeroMargins(aLayout);
58   aLayout->addWidget(myLabel);
59   setLayout(aLayout);
60
61   std::string aStyleSheet = theData->getProperty(ATTR_STYLE_SHEET);
62   if (!aStyleSheet.empty())
63     myLabel->setStyleSheet(QString("QLabel {%1}").arg(aStyleSheet.c_str()));
64
65   aStyleSheet = theData->getProperty(ATTR_IS_SELECTABLE);
66   if ( aStyleSheet == "true")
67     myLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
68 }
69
70 ModuleBase_WidgetLabel::~ModuleBase_WidgetLabel()
71 {
72 }
73
74 QList<QWidget*> ModuleBase_WidgetLabel::getControls() const
75 {
76   return QList<QWidget*>();
77 }
78
79 bool ModuleBase_WidgetLabel::restoreValueCustom()
80 {
81   DataPtr aData = myFeature->data();
82   AttributeStringPtr aStrAttr = aData->string(attributeID());
83   if (aStrAttr.get()) {
84     QString aText;
85     if (aStrAttr.get()) {
86       if (aStrAttr->isUValue()) { // already translated text
87         char16_t* aStr = aStrAttr->valueU();
88         std::wstring aWStr((wchar_t*)aStr);
89         static const int aBufSize = 1000;
90         static char aMBStr[aBufSize];
91 #ifdef _DEBUG
92         size_t aLen =
93 #endif
94           wcstombs(aMBStr, aWStr.c_str(), aBufSize);
95         std::string aCodec = Config_Translator::codec("");
96         aText = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aMBStr);
97       } else {
98         std::string aMsg = aStrAttr->value();
99         aText = ModuleBase_Tools::translate(myFeature->getKind(), aMsg);
100       }
101     }
102     myLabel->setText(aText);
103   }
104   return true;
105 }
106
107 bool ModuleBase_WidgetLabel::focusTo()
108 {
109   restoreValue();
110   return false;
111 }