fw4spl
SnapshotEditor.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 "uiVisuQt/SnapshotEditor.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/location/Folder.hpp>
16 #include <fwData/location/SingleFile.hpp>
17 
18 #include <fwGui/dialog/LocationDialog.hpp>
19 #include <fwGui/dialog/MessageDialog.hpp>
20 
21 #include <fwGuiQt/container/QtContainer.hpp>
22 
23 #include <fwRuntime/operations.hpp>
24 
25 #include <fwServices/macros.hpp>
26 
27 #include <boost/filesystem.hpp>
28 #include <boost/filesystem/convenience.hpp>
29 
30 #include <QHBoxLayout>
31 #include <QIcon>
32 #include <QPushButton>
33 #include <QString>
34 #include <QWidget>
35 
36 namespace uiVisuQt
37 {
38 
39 //------------------------------------------------------------------------------
40 
41 const ::fwCom::Signals::SignalKeyType SnapshotEditor::s_SNAPPED_SIG = "snapped";
42 
43 fwServicesRegisterMacro( ::fwGui::editor::IEditor, ::uiVisuQt::SnapshotEditor );
44 
45 //------------------------------------------------------------------------------
46 
48 {
49  m_sigSnapped = newSignal< SnappedSignalType >(s_SNAPPED_SIG);
50 }
51 
52 //------------------------------------------------------------------------------
53 
55 {
56 }
57 
58 //------------------------------------------------------------------------------
59 
61 {
62  this->create();
63 
64  ::fwGuiQt::container::QtContainer::sptr qtContainer
65  = ::fwGuiQt::container::QtContainer::dynamicCast(this->getContainer() );
66 
67  ::boost::filesystem::path path = ::fwRuntime::getBundleResourceFilePath("uiVisuQt", "camera-photo.png");
68  QIcon icon(QString::fromStdString(path.string()));
69  m_snapButton = new QPushButton(icon, "");
70  m_snapButton->setToolTip(QObject::tr("Snapshot"));
71 
72  QHBoxLayout* hlayout = new QHBoxLayout();
73  hlayout->addWidget(m_snapButton);
74  hlayout->setContentsMargins(0, 0, 0, 0);
75 
76  qtContainer->setLayout(hlayout);
77 
78  QObject::connect(m_snapButton, SIGNAL(clicked()), this, SLOT(onSnapButton()));
79 
80 }
81 
82 //------------------------------------------------------------------------------
83 
85 {
86  this->destroy();
87 }
88 
89 //------------------------------------------------------------------------------
90 
92 {
93  this->initialize();
94 }
95 
96 //------------------------------------------------------------------------------
97 
99 {
100 }
101 
102 //------------------------------------------------------------------------------
103 
105 {
106 
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 void SnapshotEditor::info( std::ostream& _sstream )
112 {
113 }
114 
115 //------------------------------------------------------------------------------
116 
118 {
119  SLM_TRACE_FUNC();
120  ::fwGuiQt::container::QtContainer::sptr qtContainer = ::fwGuiQt::container::QtContainer::dynamicCast(
121  this->getContainer() );
122  QWidget* container = qtContainer->getQtContainer();
123  SLM_ASSERT("container not instanced", container);
124  if( container->isVisible() )
125  {
126  std::string filename = this->requestFileName();
127 
128  if(!filename.empty())
129  {
130  m_sigSnapped->asyncEmit(filename);
131  }
132  }
133  else
134  {
135  std::string msgInfo("It is not possible to snapshot the negato view. This view is not shown on screen.");
137  messageBox.setTitle("Negato view snapshot");
138  messageBox.setMessage( msgInfo );
139  messageBox.setIcon(::fwGui::dialog::IMessageDialog::WARNING);
140  messageBox.addButton(::fwGui::dialog::IMessageDialog::OK);
141  messageBox.show();
142  }
143 }
144 
145 //------------------------------------------------------------------------------
146 
147 std::string SnapshotEditor::requestFileName()
148 {
149  std::string fileName = "";
150 
152  dialogFile.setTitle("Save snapshot as");
153  dialogFile.addFilter("Image file", "*.jpg *.jpeg *.bmp *.png *.tiff");
154  dialogFile.addFilter("jpeg", "*.jpg *.jpeg");
155  dialogFile.addFilter("bmp", "*.bmp");
156  dialogFile.addFilter("png", "*.png");
157  dialogFile.addFilter("tiff", "*.tiff");
158  dialogFile.addFilter("all", "*.*");
159  dialogFile.setOption(::fwGui::dialog::ILocationDialog::WRITE);
160 
161  ::fwData::location::SingleFile::sptr result;
162  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
163  if (result)
164  {
165  fileName = result->getPath().string();
166  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(result->getPath().parent_path()) );
167  }
168 
169  return fileName;
170 }
171 //------------------------------------------------------------------------------
172 
173 } // namespace uiVisuQt
void onSnapButton()
Show a file dialog and notify the scene must be printed.
virtual void starting() override
Install the layout.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
virtual UIVISUQT_API ~SnapshotEditor() noexcept
Destructor. Do nothing.
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
Defines the service interface managing the editor service for object.
Definition: IEditor.hpp:25
Defines the generic message box for IHM. Use the Delegate design pattern.
FWGUI_API void destroy()
Stops sub-views and toobar services. Destroys view, sub-views and toolbar containers.
The namespace uiVisuQt supplies user interface editors done with Qt.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
virtual FWGUI_API void addButton(IMessageDialog::Buttons button) override
Add a button (OK, YES_NO, YES, NO, CANCEL)
virtual FWGUI_API IMessageDialog::Buttons show() override
Show the message box and return the clicked button.
virtual void configuring() override
Configure the editor.
#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
virtual void info(std::ostream &_sstream) override
Overrides.
virtual void stopping() override
Destroy the layout.
FWGUI_API void create()
Creates view, sub-views and toolbar containers. Manages sub-views and toobar services.
virtual FWGUI_API void setIcon(IMessageDialog::Icons icon) override
Set the icon (CRITICAL, WARNING, INFO or QUESTION)
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
virtual void swapping() override
Do nothing.
SnapshotEditor service is represented by a button. It allows to snap shot a generic scene...
UIVISUQT_API SnapshotEditor() noexcept
Constructor. Do nothing.
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the message box.
virtual void updating() override
Do nothing.
FWGUI_API void initialize()
Initialize managers.