fw4spl
Code.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "guiQt/editor/Code.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 
12 #include <fwCore/base.hpp>
13 
14 #include <fwData/String.hpp>
15 
16 #include <fwGuiQt/container/QtContainer.hpp>
17 #include <fwGuiQt/highlighter/CppHighlighter.hpp>
18 #include <fwGuiQt/highlighter/PythonHighlighter.hpp>
19 
20 #include <fwServices/macros.hpp>
21 
22 #include <QHBoxLayout>
23 
24 namespace guiQt
25 {
26 
27 namespace editor
28 {
29 
30 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::guiQt::editor::Code, ::fwData::String );
31 
32 //------------------------------------------------------------------------------
33 
34 const std::string Code::s_PYTHON = "Python";
35 const std::string Code::s_CPP = "Cpp";
36 
37 static const ::fwServices::IService::KeyType s_STRING_INOUT = "string";
38 
39 //------------------------------------------------------------------------------
40 
41 Code::Code() noexcept :
42  m_language(s_PYTHON)
43 {
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 Code::~Code() noexcept
49 {
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
58 
59  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
60  this->getContainer() );
61 
62  ::fwData::String::sptr stringObj = this->getInOut< ::fwData::String >(s_STRING_INOUT);
63 
64  QHBoxLayout* layout = new QHBoxLayout();
65  m_valueCtrl = new QTextEdit( );
66  layout->addWidget( m_valueCtrl, 1);
67 
68  if(m_language == s_PYTHON )
69  {
70  m_highlighter = new ::fwGuiQt::highlighter::PythonHighlighter(m_valueCtrl->document());
71  }
72  else if(m_language == s_CPP )
73  {
74  m_highlighter = new ::fwGuiQt::highlighter::CppHighlighter(m_valueCtrl->document());
75  }
76  else
77  {
78  OSLM_WARN("Language "<<m_language<<" not yet supported.");
79  }
80 
81  qtContainer->setLayout( layout );
82 
83  QObject::connect(m_valueCtrl, SIGNAL(textChanged()), this, SLOT(onModifyValue()));
84  this->updating();
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
92 
93  QObject::disconnect(m_valueCtrl, SIGNAL(textChanged()), this, SLOT(onModifyValue()));
94 
95  this->destroy();
96 }
97 
98 //------------------------------------------------------------------------------
99 
101 {
102  SLM_TRACE_FUNC();
104  std::vector < ConfigurationType > vectConfig = m_configuration->find("config");
105  if(!vectConfig.empty())
106  {
107  std::vector < ConfigurationType > vectLanguage = vectConfig.at(0)->find("language");
108  if(!vectLanguage.empty())
109  {
110  ConfigurationType configLanguage = vectLanguage.at(0);
111  SLM_ASSERT("missing 'name' attribute in language tag", configLanguage->hasAttribute("name"));
112  m_language = configLanguage->getAttributeValue("name");
113  }
114  }
115 }
116 
117 //------------------------------------------------------------------------------
118 
120 {
121  ::fwData::String::sptr stringObj = this->getInOut< ::fwData::String >(s_STRING_INOUT);
122  SLM_ASSERT("The given string object is null", stringObj);
123 
124  m_valueCtrl->setText(QString::fromStdString(stringObj->value()));
125  OSLM_TRACE(stringObj->getID() << " updated value : " << stringObj->value());
126 }
127 
128 //------------------------------------------------------------------------------
129 
131 {
132  this->updating();
133 }
134 
135 //------------------------------------------------------------------------------
136 
137 void Code::info( std::ostream& _sstream )
138 {
139  _sstream << "String Editor";
140 }
141 
142 //------------------------------------------------------------------------------
143 
145 {
146  QString value = m_valueCtrl->toPlainText();
147  ::fwData::String::sptr stringObj = this->getInOut< ::fwData::String >(s_STRING_INOUT);
148  ::fwData::String::sptr oldValue;
149  oldValue = ::fwData::Object::copy(stringObj);
150 
151  std::string strValue = value.toStdString();
152  stringObj->value() = strValue;
153 
154  if ( oldValue->value() != stringObj->value() )
155  {
156  OSLM_TRACE( stringObj->getID() << " modified");
157 
159  {
160  ::fwCom::Connection::Blocker block(sig->getConnection(m_slotUpdate));
161  sig->asyncEmit();
162  }
163  }
164 }
165 
166 //------------------------------------------------------------------------------
167 
169 {
170  KeyConnectionsMap connections;
171  connections.push(s_STRING_INOUT, ::fwData::Object::s_MODIFIED_SIG, s_UPDATE_SLOT);
172 
173  return connections;
174 }
175 
176 //------------------------------------------------------------------------------
177 
178 } // namespace editor
179 
180 } // namespace guiQt
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
virtual void swapping() override
Update the value from the String object.
Definition: Code.cpp:130
void onModifyValue()
This method is called when the value change.
Definition: Code.cpp:144
Class allowing to block a Connection.
Definition: Connection.hpp:20
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
This service displays a code editor and works on a fwData::String.
Definition: Code.hpp:46
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
virtual void updating() override
Update the value from the String object.
Definition: Code.cpp:119
GUIQT_API Code() noexcept
Constructor. Do nothing.
Definition: Code.cpp:41
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
virtual void starting() override
Install the layout.
Definition: Code.cpp:54
virtual void info(std::ostream &_sstream) override
Overrides.
Definition: Code.cpp:137
virtual GUIQT_API ~Code() noexcept
Destructor. Do nothing.
Definition: Code.cpp:48
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
virtual void stopping() override
Destroy the layout.
Definition: Code.cpp:89
#define OSLM_WARN(message)
Definition: spyLog.hpp:263
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
virtual GUIQT_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: Code.cpp:168
The namespace guiQt contains the basic services to build the application IHM with Qt...
Definition: Code.hpp:21
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
This class contains an std::string value.
virtual void configuring() override
Parses the configuration.
Definition: Code.cpp:100
FWGUI_API void initialize()
Initialize managers.