NorMIT-nav  18.04
An IGT application
cxScreenVideoProvider.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 =========================================================================*/
11 #include "cxScreenVideoProvider.h"
12 
13 #include <QPixmap>
14 #include "cxPatientModelService.h"
15 #include <QtConcurrent>
16 #include <QDesktopWidget>
17 #include <QApplication>
18 #include "cxReporter.h"
19 #include "boost/bind.hpp"
20 #include <QScreen>
21 #include <QVBoxLayout>
22 #include "cxViewService.h"
23 #include "cxViewCollectionWidget.h"
24 #include "vtkRenderer.h"
25 #include "vtkWindowToImageFilter.h"
26 #include "vtkRenderWindow.h"
27 #include "vtkPNGWriter.h"
28 #include "vtkUnsignedCharArray.h"
29 #include <QPainter>
31 #include <QScrollArea>
32 
33 namespace cx
34 {
35 
37  QWidget(parent),
38  mViewService(viewService),
39  mSecondaryLayoutId(1)
40 {
41  this->setLayout(new QVBoxLayout(this));
42  this->layout()->setMargin(0);
43  this->setWindowTitle("View Layout");
44 }
45 
46 void SecondaryViewLayoutWindow::showEvent(QShowEvent* event)
47 {
48  QWidget* widget = mViewService->createLayoutWidget(this, 1);
49  this->layout()->addWidget(widget);
50  if (mViewService->getActiveLayout(mSecondaryLayoutId).isEmpty())
51  mViewService->setActiveLayout("LAYOUT_OBLIQUE_3DAnyDual_x1", 1);
52 }
53 
54 void SecondaryViewLayoutWindow::hideEvent(QCloseEvent* event)
55 {
56  mViewService->setActiveLayout("", mSecondaryLayoutId);
57 }
58 
59 void SecondaryViewLayoutWindow::closeEvent(QCloseEvent *event)
60 {
61  mViewService->setActiveLayout("", mSecondaryLayoutId);
62 }
63 
64 
65 //---------------------------------------------------------
66 //---------------------------------------------------------
67 //---------------------------------------------------------
68 
70  mServices(services),
71  mWriter(services->patient())
72 {
73 
74 }
75 
76 
77 void ScreenVideoProvider::saveScreenShot(QImage image, QString id)
78 {
79  mWriter.save(image,id);
80 }
81 
83 {
84  QByteArray ba;
85  QBuffer buffer(&ba);
86  buffer.open(QIODevice::WriteOnly);
87  image.save(&buffer, "PNG"); // writes image into ba in PNG format// QString ending = "png";
88  return ba;
89 }
90 
91 QPixmap ScreenVideoProvider::grabScreen(unsigned screenid)
92 {
93  return mWriter.grab(screenid);
94 }
95 
96 
97 \
98 void ScreenVideoProvider::showSecondaryLayout(QSize size, QString layout)
99 {
100  if (!mTopWindow)
101  {
102  mTopWindow = new QWidget;
103  mTopWindow->setLayout(new QVBoxLayout);
104  mTopWindow->layout()->setMargin(0);
105 
106  QScrollArea* scrollArea = new QScrollArea;
107  scrollArea->setBackgroundRole(QPalette::Dark);
108  mTopWindow->layout()->addWidget(scrollArea);
109 
110  mSecondaryViewLayoutWindow = new SecondaryViewLayoutWindow(NULL, mServices->view());
111  scrollArea->setWidget(mSecondaryViewLayoutWindow);
112  scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
113  scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
114  }
115  mTopWindow->show();
116 
117  this->setWidgetToNiceSizeInLowerRightCorner(size);
118 
119  if (!layout.isEmpty())
120  mServices->view()->setActiveLayout(layout, mSecondaryViewLayoutWindow->mSecondaryLayoutId);
121 
122  ViewCollectionWidget* layoutWidget = this->getSecondaryLayoutWidget();
123  layoutWidget->setGridMargin(0);
124  layoutWidget->setGridSpacing(0);
125 }
126 
127 void ScreenVideoProvider::setWidgetToNiceSizeInLowerRightCorner(QSize size)
128 {
129  QDesktopWidget* desktop = qApp->desktop();
130  QList<QScreen*> screens = qApp->screens();
131 
132  QRect rect_s = desktop->availableGeometry(mTopWindow);
133 
134  // default to 33% of the screen
135  if (size.width()==0 || size.height()==0)
136  {
137  size = QSize(rect_s.width()/3, rect_s.height()/3);
138  }
139 
140  QRect rect_full = QRect(QPoint(0,0), size);
141 
142  // constrain widget to a max of 75% of the screen
143  size = QSize(std::min<int>(size.width(), rect_s.width()*0.75),
144  std::min<int>(size.height(), rect_s.height()*0.75));
145  // make sure all of scroll area is visible:
146  int margin = 20;
147  size = QSize(size.width()+margin, size.height()+margin);
148  mTopWindow->setGeometry(QRect(QPoint(0,0), size));
149 
150  // reposition window to lower right corner:
151  QRect rect_t = mTopWindow->frameGeometry();
152  mTopWindow->move(rect_s.topLeft()
153  + QPoint(rect_s.width(), rect_s.height())
154  - QPoint(rect_t.width(), rect_t.height()));
155 
156  // set size of canvas inside widget where stuff is rendered:
157  mSecondaryViewLayoutWindow->setGeometry(rect_full);
158 
159  qDebug() << "layout onscreen: " << mTopWindow->geometry();
160  qDebug() << "layout internal: " << mSecondaryViewLayoutWindow->geometry();
161 }
162 
164 {
165  mSecondaryViewLayoutWindow->hide();
166  delete mSecondaryViewLayoutWindow;
167 }
168 
170 {
172  if (!widget)
173  return QImage();
174 
175  ViewCollectionImageWriter grabber(widget);
177 }
178 
180 {
181  if (!mSecondaryViewLayoutWindow)
182  return NULL;
183  QWidget* widget = mServices->view()->getLayoutWidget(mSecondaryViewLayoutWindow->mSecondaryLayoutId);
184  ViewCollectionWidget* vcWidget = dynamic_cast<ViewCollectionWidget*>(widget);
185  return vcWidget;
186 }
187 
188 } // namespace cx
QPixmap grab(unsigned screenid)
ScreenVideoProvider(VisServicesPtr services)
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
QByteArray generatePNGEncoding(QImage image)
boost::shared_ptr< class ViewService > ViewServicePtr
virtual void closeEvent(QCloseEvent *event)
virtual void setGridMargin(int val)=0
virtual void setGridSpacing(int val)=0
static QImage vtkImageData2QImage(vtkImageDataPtr input)
Experimental class for IPad usage.
void showSecondaryLayout(QSize size, QString layout)
void save(QImage image, QString id)
class ViewCollectionWidget * getSecondaryLayoutWidget()
void saveScreenShot(QImage image, QString id)
virtual void showEvent(QShowEvent *event)
QPixmap grabScreen(unsigned screenid)
virtual void hideEvent(QCloseEvent *event)
Namespace for all CustusX production code.