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