Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetLabel.cpp
1 // Copyright (C) 2014-2022  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
41   myPrefix  = theData->getProperty(ATTR_LABEL);
42
43   bool aIsHtml = theData->getBooleanAttribute(ATTR_HTML_STYLE, false);
44
45   QString aLabelIcon = QString::fromStdString(theData->getProperty("icon"));
46   myLabel = new QLabel(aText, theParent);
47   if (!aLabelIcon.isEmpty()) {
48     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
49     myLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
50   } else {
51     myLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
52   }
53   myLabel->setWordWrap(true);
54   myLabel->setIndent(5);
55   myLabel->setContentsMargins(0,0,0,4);
56   if (aIsHtml)
57     myLabel->setTextFormat(Qt::RichText);
58
59   QVBoxLayout* aLayout = new QVBoxLayout(this);
60   ModuleBase_Tools::zeroMargins(aLayout);
61   aLayout->addWidget(myLabel);
62   setLayout(aLayout);
63
64   std::string aStyleSheet = theData->getProperty(ATTR_STYLE_SHEET);
65   if (!aStyleSheet.empty())
66     myLabel->setStyleSheet(QString("QLabel {%1}").arg(aStyleSheet.c_str()));
67
68   aStyleSheet = theData->getProperty(ATTR_IS_SELECTABLE);
69   if ( aStyleSheet == "true")
70     myLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
71 }
72
73 ModuleBase_WidgetLabel::~ModuleBase_WidgetLabel()
74 {
75 }
76
77 QList<QWidget*> ModuleBase_WidgetLabel::getControls() const
78 {
79   return QList<QWidget*>();
80 }
81
82 bool ModuleBase_WidgetLabel::restoreValueCustom()
83 {
84   DataPtr aData = myFeature->data();
85   AttributeStringPtr aStrAttr = aData->string(attributeID());
86   if (aStrAttr.get()) {
87     QString aText;
88     if (aStrAttr.get()) {
89       if (aStrAttr->isUValue()) { // already translated text
90         char16_t* aStr = aStrAttr->valueU();
91         std::wstring aWStr((wchar_t*)aStr);
92         static const int aBufSize = 1000;
93         static char aMBStr[aBufSize];
94 #ifdef _DEBUG
95         size_t aLen =
96 #endif
97           wcstombs(aMBStr, aWStr.c_str(), aBufSize);
98         std::string aCodec = Config_Translator::codec("");
99         aText = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aMBStr);
100       } else {
101         std::string aMsg = aStrAttr->value();
102         aText = ModuleBase_Tools::translate(myFeature->getKind(), aMsg);
103       }
104     }
105     if (myPrefix == "") {
106       myLabel->setText(aText);
107     } else {
108       myLabel->setText(ModuleBase_Tools::translate(myFeature->getKind(), myPrefix) + aText);
109     }
110   }
111   return true;
112 }
113
114 bool ModuleBase_WidgetLabel::focusTo()
115 {
116   restoreValue();
117   return false;
118 }