Fraxinus  18.10
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
12 
13 
14 #include <QPixmap>
15 #include "cxPatientModelService.h"
16 #include <QtConcurrent>
17 #include <QDesktopWidget>
18 #include <QApplication>
19 #include "cxReporter.h"
20 #include "boost/bind.hpp"
21 #include <QScreen>
22 #include <QVBoxLayout>
23 #include "cxViewService.h"
24 //#include "cxSecondaryViewLayoutWindow.h"
25 #include "cxViewCollectionWidget.h"
26 #include "vtkRenderer.h"
27 #include "vtkWindowToImageFilter.h"
28 #include "vtkRenderWindow.h"
29 #include "vtkPNGWriter.h"
30 #include "vtkUnsignedCharArray.h"
31 #include <QPainter>
32 //#include "cxViewCollectionImageWriter.h"
33 
34 namespace cx
35 {
36 
37 namespace // unnamed
38 {
42 void saveScreenShotThreaded(QImage pixmap, QString filename)
43 {
44  pixmap.save(filename, "png");
45  report("Saved screenshot to " + filename);
46  reporter()->playScreenShotSound();
47 }
48 } // unnamed ns
49 
51  mPatient(patient)
52 {
53 }
54 
56 {
57  QDesktopWidget* desktop = qApp->desktop();
58  QList<QScreen*> screens = qApp->screens();
59 
60  for (int i=0; i<desktop->screenCount(); ++i)
61  {
62  QPixmap pm = this->grab(i);
63  QString name = this->getName(i);
64  this->save(pm.toImage(), name);
65  }
66 }
67 
68 void ScreenShotImageWriter::save(QImage image, QString id)
69 {
70  QString ending = "png";
71  if (!id.isEmpty())
72  ending = id + "." + ending;
73  QString path = mPatient->generateFilePath("Screenshots", ending);
74  QtConcurrent::run(boost::bind(&saveScreenShotThreaded, image, path));
75 }
76 
77 QPixmap ScreenShotImageWriter::grab(unsigned screenid)
78 {
79  QDesktopWidget* desktop = qApp->desktop();
80  QList<QScreen*> screens = qApp->screens();
81 
82  QWidget* screenWidget = desktop->screen(screenid);
83  WId screenWinId = screenWidget->winId();
84  QRect geo = desktop->screenGeometry(screenid);
85 // QString name = "";
86 // if (desktop->screenCount()>1)
87 // {
88 // name = screens[screenid]->name().split(" ").join("");
89 // //On windows screens[i]->name() is "\\.\DISPLAY1",
90 // //Have to remove unvalid chars for the filename
91 // name.replace("\\", "");
92 // name.replace(".", "");
93 // }
94  QPixmap pixmap = screens[screenid]->grabWindow(screenWinId, geo.left(), geo.top(), geo.width(), geo.height());
95  return pixmap;
96 }
97 
98 QString ScreenShotImageWriter::getName(unsigned screenid)
99 {
100  QDesktopWidget* desktop = qApp->desktop();
101  QList<QScreen*> screens = qApp->screens();
102 
103  QString name = "";
104  if (desktop->screenCount()>1)
105  {
106  name = screens[screenid]->name().split(" ").join("");
107  //On windows screens[i]->name() is "\\.\DISPLAY1",
108  //Have to remove unvalid chars for the filename
109  name.replace("\\", "");
110  name.replace(".", "");
111  }
112  return name;
113 }
114 
115 } // namespace cx
QString getName(unsigned screenid)
ScreenShotImageWriter(PatientModelServicePtr patient)
ReporterPtr reporter()
Definition: cxReporter.cpp:38
QPixmap grab(unsigned screenid)
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void save(QImage image, QString id)
void report(QString msg)
Definition: cxLogger.cpp:69
Namespace for all CustusX production code.