7 #include "ioQt/SPdfWriter.hpp" 9 #include <fwData/location/Folder.hpp> 10 #include <fwData/location/SingleFile.hpp> 12 #include <fwDataTools/helper/Image.hpp> 14 #include <fwGui/dialog/LocationDialog.hpp> 15 #include <fwGui/dialog/MessageDialog.hpp> 16 #include <fwGui/GuiRegistry.hpp> 18 #include <fwGuiQt/container/QtContainer.hpp> 20 #include <fwRuntime/ConfigurationElement.hpp> 21 #include <fwRuntime/ConfigurationElementContainer.hpp> 23 #include <fwServices/macros.hpp> 25 #include <fwThread/Pool.hpp> 26 #include <fwThread/Worker.hpp> 36 const ::fwServices::IService::KeyType s_IMAGE_INPUT =
"image";
37 const ::fwServices::IService::KeyType s_CONTAINER_INPUT =
"container";
50 _sstream << std::endl <<
" External data file reader";
66 typedef ::fwRuntime::ConfigurationElement::sptr ConfigurationType;
67 const ConfigurationType containersConfig =
m_configuration->findConfigurationElement(
"container");
70 const std::vector< ConfigurationType > containersCfg = containersConfig->find(s_CONTAINER_INPUT);
71 for (
const auto& cfg : containersCfg)
73 SLM_ASSERT(
"Missing attribute 'uid'.", cfg->hasAttribute(
"uid"));
74 const std::string
id = cfg->getAttributeValue(
"uid");
75 m_containersIDs.push_back(
id );
84 static ::boost::filesystem::path _sDefaultPath;
91 dialogFile.
setOption(::fwGui::dialog::ILocationDialog::WRITE);
93 ::fwData::location::SingleFile::sptr result;
94 result = ::fwData::location::SingleFile::dynamicCast( dialogFile.
show() );
97 _sDefaultPath = result->getPath();
99 this->
setFile(result->getPath());
117 QPdfWriter pdfWriter( this->
getLocations().front().
string().c_str() );
118 QPainter painter( &pdfWriter );
119 pdfWriter.setPageSize( QPagedPaintDevice::A4 );
122 const int scale =
static_cast<const int>(pdfWriter.logicalDpiX() * 8);
126 ImagesScaledListType imagesToScale;
127 std::vector< std::shared_future< QImage > > futuresQImage;
128 for( const ::fwData::Image::sptr& fwImage : m_imagesToExport )
130 std::shared_future< QImage > future;
131 future = pool.
post(&SPdfWriter::convertFwImageToQImage, fwImage);
132 futuresQImage.push_back( future );
134 std::for_each(futuresQImage.begin(), futuresQImage.end(), std::mem_fn(&std::shared_future< QImage >::wait));
135 for (
auto& future : futuresQImage)
137 QImage imageToDraw = future.get();
138 imagesToScale.push_back(imageToDraw);
142 for( QWidget*& qtContainer : m_containersToExport )
144 QImage imageToDraw = qtContainer->grab().toImage();
145 imagesToScale.push_back(imageToDraw);
149 std::vector< std::shared_future< void > > futures;
150 const size_t sizeImagesToScale = imagesToScale.size();
151 for(
size_t idx = 0; idx < sizeImagesToScale; ++idx )
153 std::shared_future<void> future;
154 future = pool.
post(&SPdfWriter::scaleQImage, std::ref(imagesToScale[idx]), scale);
155 futures.push_back( future );
157 std::for_each(futures.begin(), futures.end(), std::mem_fn(&std::shared_future<void>::wait));
160 for( QImage& qImage : imagesToScale )
162 if ( pdfWriter.newPage() )
164 pdfWriter.setPageSize( QPagedPaintDevice::A4 );
165 if ( !qImage.isNull() && qImage.bits() != nullptr )
167 painter.drawImage( 0, 0, qImage);
180 for (
size_t idxImage = 0; idxImage < groupImageSize; ++idxImage)
182 ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INPUT, idxImage);
183 m_imagesToExport.push_back(image);
186 for(
const auto&
id : m_containersIDs)
188 ::fwGuiQt::container::QtContainer::sptr containerElt;
189 ::fwGui::container::fwContainer::sptr fwContainerFromConfig;
198 if (fwContainerFromConfig)
200 containerElt = ::fwGuiQt::container::QtContainer::dynamicCast(fwContainerFromConfig);
201 m_containersToExport.push_back(containerElt->getQtContainer());
210 for( QWidget*& qtContainer : m_containersToExport )
212 qtContainer =
nullptr;
214 m_containersToExport.clear();
215 m_imagesToExport.clear();
227 void SPdfWriter::scaleQImage(QImage& qImage,
const int scale)
230 qImage = qImage.scaledToWidth(scale, Qt::FastTransformation);
235 QImage SPdfWriter::convertFwImageToQImage(::fwData::Image::sptr fwImage)
237 if (fwImage->getNumberOfComponents() == 3
238 && fwImage->getType().string() ==
"uint8" 239 && fwImage->getSize()[2] == 1)
242 const ::fwData::Image::SizeType dimension = fwImage->getSize();
243 const int width =
static_cast<int>(dimension[0]);
244 const int height =
static_cast<int>(dimension[1]);
246 QImage qImage(width, height, QImage::Format_ARGB32);
247 std::uint8_t* qImageBuffer = qImage.bits();
250 const std::uint8_t* fwImageBuffer =
reinterpret_cast< const std::uint8_t*
>( imgHelper.
getBuffer() );
252 const unsigned int size =
static_cast<unsigned int>( width * height) * 4;
253 for(
unsigned int idx = 0; idx < size; idx += 4)
255 qImageBuffer[idx+3] = (255 & 0xFF);
256 qImageBuffer[idx+2] = (*fwImageBuffer++ & 0xFF);
257 qImageBuffer[idx+1] = (*fwImageBuffer++ & 0xFF);
258 qImageBuffer[idx+0] = (*fwImageBuffer++ & 0xFF);
260 return qImage.mirrored(0, 1);
static FWGUI_API::fwGui::container::fwContainer::sptr getWIDContainer(std::string wid)
Returns fwContainer associate with window ID, null if not found.
Creates and writes a PDF containing images.
IOQT_API void configureWithIHM() override
Configure the image path.
IOQT_API ~SPdfWriter() noexcept
Destructor.
virtual IOQT_API void starting() override
Starting method : Fills the list of Qt containers as well as the list of images got from the configur...
IOQT_API SPdfWriter()
Constructor : does nothing.
auto post(F &&f, Args &&...args) -> std::shared_future< typename std::result_of< F(Args...)>::type >
add new work item to the pool
virtual IOQT_API void configuring() override
Configure service. This method is called by configure() from base service ( fwServices::IService ) ...
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
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.
static FWGUI_API::fwGui::container::fwContainer::sptr getSIDContainer(std::string sid)
Returns fwContainer associate with service ID, null if not found.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
virtual IOQT_API void stopping() override
Stopping method : default does nothing.
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
#define SLM_ERROR_IF(message, cond)
This class creates and manages a pool of threads which process tasks.
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
virtual IOQT_API void info(std::ostream &_sstream) override
Info method.
Base class for each data object.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
IOQT_API::fwIO::IOPathType getIOPathType() const override
Returns managed path type, here service manages only single file.
size_t getKeyGroupSize(const KeyType &keybase) const
Return the number of key in a group of keys.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Writer service API. It manages extension points definition and extension configuration.
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
FWIO_APIconst::fwIO::LocationsType & getLocations() const
Returns file/files/folder paths set by the user or set during service configuration.
static FWGUI_API bool hasSIDContainer(std::string sid)
Verifies if a SID exists in the global SID container.
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
IOQT_API void updating() override
Updating method. Creates a new PDF.
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
virtual FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.