screenshot_dialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <QDateTime>
31 #include <QMessageBox>
32 #include <QImageWriter>
33 #include <QVBoxLayout>
34 #include <QHBoxLayout>
35 #include <QDialogButtonBox>
36 #include <QLabel>
37 #include <QPushButton> // Included so we know that QPushButton inherits QAbstractButton
38 #include <QFileDialog>
39 #include <QCheckBox>
40 #include <QTimer>
41 
42 #include "scaled_image_widget.h"
43 #include "screenshot_dialog.h"
44 
45 namespace rviz
46 {
47 
48 ScreenshotDialog::ScreenshotDialog( QWidget* main_window, QWidget* render_window, const QString& default_save_dir )
49  : QWidget( NULL ) // This should be a top-level window to act like a dialog.
50  , main_window_( main_window )
51  , render_window_( render_window )
52  , save_full_window_( false )
53  , delay_timer_( new QTimer( this ))
54  , first_time_( true )
55  , default_save_dir_( default_save_dir )
56 {
57  image_widget_ = new ScaledImageWidget( .5 );
58 
60 
61  QCheckBox* full_window_checkbox = new QCheckBox( "Save entire rviz window" );
62 
63  button_box_ = new QDialogButtonBox( QDialogButtonBox::Save |
64  QDialogButtonBox::Retry |
65  QDialogButtonBox::Cancel );
66 
67  QVBoxLayout* main_layout = new QVBoxLayout;
68  main_layout->addWidget( image_widget_, 100 );
69  main_layout->addWidget( new QLabel( "Image will be saved at the original resolution." ));
70  main_layout->addWidget( full_window_checkbox );
71  main_layout->addWidget( button_box_ );
72 
73  setLayout( main_layout );
74 
75  connect( button_box_, SIGNAL( clicked( QAbstractButton* )), this, SLOT( onButtonClicked( QAbstractButton* )));
76  connect( full_window_checkbox, SIGNAL( toggled( bool )), this, SLOT( setSaveFullWindow( bool )));
77  connect( delay_timer_, SIGNAL( timeout() ), this, SLOT( onTimeout() ));
78 }
79 
80 void ScreenshotDialog::showEvent( QShowEvent* event )
81 {
82  if( first_time_ )
83  {
84  QPoint center = main_window_->rect().center();
85  move( center.x() - width() / 2,
86  center.y() - height() / 2 );
87 
88  first_time_ = false;
89  }
90  QWidget::showEvent( event );
91 }
92 
93 void ScreenshotDialog::setSaveFullWindow( bool save_full_window )
94 {
95  save_full_window_ = save_full_window;
97 }
98 
100 {
101  main_window_->raise();
102  delay_timer_->start(100);
103 }
104 
106 {
107  delay_timer_->stop();
109  raise();
110  activateWindow();
111 }
112 
114 {
115  if( save_full_window_ )
116  {
117  screenshot_ = QPixmap::grabWindow( main_window_->winId() );
118  }
119  else
120  {
121  screenshot_ = QPixmap::grabWindow( render_window_->winId() );
122  }
124 }
125 
126 void ScreenshotDialog::onButtonClicked( QAbstractButton* clicked )
127 {
128  if( clicked == button_box_->button( QDialogButtonBox::Save ))
129  {
130  save();
131  }
132  else if( clicked == button_box_->button( QDialogButtonBox::Retry ))
133  {
134  takeScreenshot();
135  }
136  else if( clicked == button_box_->button( QDialogButtonBox::Cancel ))
137  {
138  close();
139  }
140 }
141 
143 {
144  QString default_save_file =
146  "/rviz_screenshot_" +
147  QDateTime::currentDateTime().toString( "yyyy_MM_dd-hh_mm_ss" ) +
148  ".png";
149  QString filename = QFileDialog::getSaveFileName( this, "Save image", default_save_file );
150  if( filename != "" )
151  {
152  QString with_slashes = QDir::fromNativeSeparators( filename );
153  QString file_part = with_slashes.section( '/', -1 );
154  default_save_dir_ = QDir::toNativeSeparators( with_slashes.section( '/', 0, -2 ));
156 
157  // If filename has no dot, like "image" or has a dot in the zeroth
158  // position, like ".image", add ".png" to give a default file
159  // format.
160  if( file_part.lastIndexOf( "." ) <= 0 )
161  {
162  filename += ".png";
163  }
164  QImageWriter writer( filename );
165  if( writer.write( screenshot_.toImage() ))
166  {
167  close();
168  }
169  else
170  {
171  QString error_message;
172  if( writer.error() == QImageWriter::UnsupportedFormatError )
173  {
174  QString suffix = filename.section( '.', -1 );
175  QString formats_string;
176  QList<QByteArray> formats = QImageWriter::supportedImageFormats();
177  formats_string = formats[0];
178  for( int i = 1; i < formats.size(); i++ )
179  {
180  formats_string += ", " + formats[ i ];
181  }
182 
183  error_message =
184  "File type '" + suffix + "' is not supported.\n" +
185  "Supported image formats are: " + formats_string + "\n";
186  }
187  else
188  {
189  error_message = "Failed to write image to file " + filename;
190  }
191 
192  QMessageBox::critical( this, "Error", error_message );
193  }
194  }
195 }
196 
197 } // end namespace rviz
#define NULL
Definition: global.h:37
A widget for showing a scaled version of an image (QPixmap).
filename
ScreenshotDialog(QWidget *main_window, QWidget *render_window, const QString &default_save_dir=QString())
void savedInDirectory(const QString &directory)
Emitted when the user saves a file.
ScaledImageWidget * image_widget_
void setImage(QPixmap image)
void onButtonClicked(QAbstractButton *clicked)
QDialogButtonBox * button_box_
virtual void showEvent(QShowEvent *event)
void setSaveFullWindow(bool save_full_window)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:42