Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxScreenShotImageWriter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
33 
34 
35 #include <QPixmap>
36 #include "cxPatientModelService.h"
37 #include <QtConcurrent>
38 #include <QDesktopWidget>
39 #include <QApplication>
40 #include "cxReporter.h"
41 #include "boost/bind.hpp"
42 #include <QScreen>
43 #include <QVBoxLayout>
44 #include "cxViewService.h"
45 //#include "cxSecondaryViewLayoutWindow.h"
46 #include "cxViewCollectionWidget.h"
47 #include "vtkRenderer.h"
48 #include "vtkWindowToImageFilter.h"
49 #include "vtkRenderWindow.h"
50 #include "vtkPNGWriter.h"
51 #include "vtkUnsignedCharArray.h"
52 #include <QPainter>
53 //#include "cxViewCollectionImageWriter.h"
54 
55 namespace cx
56 {
57 
58 namespace // unnamed
59 {
63 void saveScreenShotThreaded(QImage pixmap, QString filename)
64 {
65  pixmap.save(filename, "png");
66  report("Saved screenshot to " + filename);
67  reporter()->playScreenShotSound();
68 }
69 } // unnamed ns
70 
72  mPatient(patient)
73 {
74 }
75 
77 {
78  QDesktopWidget* desktop = qApp->desktop();
79  QList<QScreen*> screens = qApp->screens();
80 
81  for (int i=0; i<desktop->screenCount(); ++i)
82  {
83  QPixmap pm = this->grab(i);
84  QString name = this->getName(i);
85  this->save(pm.toImage(), name);
86  }
87 }
88 
89 void ScreenShotImageWriter::save(QImage image, QString id)
90 {
91  QString ending = "png";
92  if (!id.isEmpty())
93  ending = id + "." + ending;
94  QString path = mPatient->generateFilePath("Screenshots", ending);
95  QtConcurrent::run(boost::bind(&saveScreenShotThreaded, image, path));
96 }
97 
98 QPixmap ScreenShotImageWriter::grab(unsigned screenid)
99 {
100  QDesktopWidget* desktop = qApp->desktop();
101  QList<QScreen*> screens = qApp->screens();
102 
103  QWidget* screenWidget = desktop->screen(screenid);
104  WId screenWinId = screenWidget->winId();
105  QRect geo = desktop->screenGeometry(screenid);
106 // QString name = "";
107 // if (desktop->screenCount()>1)
108 // {
109 // name = screens[screenid]->name().split(" ").join("");
110 // //On windows screens[i]->name() is "\\.\DISPLAY1",
111 // //Have to remove unvalid chars for the filename
112 // name.replace("\\", "");
113 // name.replace(".", "");
114 // }
115  QPixmap pixmap = screens[screenid]->grabWindow(screenWinId, geo.left(), geo.top(), geo.width(), geo.height());
116  return pixmap;
117 }
118 
119 QString ScreenShotImageWriter::getName(unsigned screenid)
120 {
121  QDesktopWidget* desktop = qApp->desktop();
122  QList<QScreen*> screens = qApp->screens();
123 
124  QString name = "";
125  if (desktop->screenCount()>1)
126  {
127  name = screens[screenid]->name().split(" ").join("");
128  //On windows screens[i]->name() is "\\.\DISPLAY1",
129  //Have to remove unvalid chars for the filename
130  name.replace("\\", "");
131  name.replace(".", "");
132  }
133  return name;
134 }
135 
136 } // namespace cx
QString getName(unsigned screenid)
ScreenShotImageWriter(PatientModelServicePtr patient)
ReporterPtr reporter()
Definition: cxReporter.cpp:59
QPixmap grab(unsigned screenid)
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void save(QImage image, QString id)
void report(QString msg)
Definition: cxLogger.cpp:90