Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <QDateTime>
00031 #include <QMessageBox>
00032 #include <QImageWriter>
00033 #include <QVBoxLayout>
00034 #include <QHBoxLayout>
00035 #include <QDialogButtonBox>
00036 #include <QLabel>
00037 #include <QPushButton>
00038 #include <QFileDialog>
00039 #include <QCheckBox>
00040 #include <QTimer>
00041
00042 #include "scaled_image_widget.h"
00043 #include "screenshot_dialog.h"
00044
00045 namespace rviz
00046 {
00047
00048 ScreenshotDialog::ScreenshotDialog( QWidget* main_window, QWidget* render_window, const QString& default_save_dir )
00049 : QWidget( NULL )
00050 , main_window_( main_window )
00051 , render_window_( render_window )
00052 , save_full_window_( false )
00053 , delay_timer_( new QTimer( this ))
00054 , first_time_( true )
00055 , default_save_dir_( default_save_dir )
00056 {
00057 image_widget_ = new ScaledImageWidget( .5 );
00058
00059 takeScreenshotNow();
00060
00061 QCheckBox* full_window_checkbox = new QCheckBox( "Save entire rviz window" );
00062
00063 button_box_ = new QDialogButtonBox( QDialogButtonBox::Save |
00064 QDialogButtonBox::Retry |
00065 QDialogButtonBox::Cancel );
00066
00067 QVBoxLayout* main_layout = new QVBoxLayout;
00068 main_layout->addWidget( image_widget_, 100 );
00069 main_layout->addWidget( new QLabel( "Image will be saved at the original resolution." ));
00070 main_layout->addWidget( full_window_checkbox );
00071 main_layout->addWidget( button_box_ );
00072
00073 setLayout( main_layout );
00074
00075 connect( button_box_, SIGNAL( clicked( QAbstractButton* )), this, SLOT( onButtonClicked( QAbstractButton* )));
00076 connect( full_window_checkbox, SIGNAL( toggled( bool )), this, SLOT( setSaveFullWindow( bool )));
00077 connect( delay_timer_, SIGNAL( timeout() ), this, SLOT( onTimeout() ));
00078 }
00079
00080 void ScreenshotDialog::showEvent( QShowEvent* event )
00081 {
00082 if( first_time_ )
00083 {
00084 QPoint center = main_window_->rect().center();
00085 move( center.x() - width() / 2,
00086 center.y() - height() / 2 );
00087
00088 first_time_ = false;
00089 }
00090 QWidget::showEvent( event );
00091 }
00092
00093 void ScreenshotDialog::setSaveFullWindow( bool save_full_window )
00094 {
00095 save_full_window_ = save_full_window;
00096 takeScreenshot();
00097 }
00098
00099 void ScreenshotDialog::takeScreenshot()
00100 {
00101 main_window_->raise();
00102 delay_timer_->start(100);
00103 }
00104
00105 void ScreenshotDialog::onTimeout()
00106 {
00107 delay_timer_->stop();
00108 takeScreenshotNow();
00109 raise();
00110 activateWindow();
00111 }
00112
00113 void ScreenshotDialog::takeScreenshotNow()
00114 {
00115 if( save_full_window_ )
00116 {
00117 screenshot_ = QPixmap::grabWindow( main_window_->winId() );
00118 }
00119 else
00120 {
00121 screenshot_ = QPixmap::grabWindow( render_window_->winId() );
00122 }
00123 image_widget_->setImage( screenshot_ );
00124 }
00125
00126 void ScreenshotDialog::onButtonClicked( QAbstractButton* clicked )
00127 {
00128 if( clicked == button_box_->button( QDialogButtonBox::Save ))
00129 {
00130 save();
00131 }
00132 else if( clicked == button_box_->button( QDialogButtonBox::Retry ))
00133 {
00134 takeScreenshot();
00135 }
00136 else if( clicked == button_box_->button( QDialogButtonBox::Cancel ))
00137 {
00138 close();
00139 }
00140 }
00141
00142 void ScreenshotDialog::save()
00143 {
00144 QString default_save_file =
00145 default_save_dir_ +
00146 "/rviz_screenshot_" +
00147 QDateTime::currentDateTime().toString( "yyyy_MM_dd-hh_mm_ss" ) +
00148 ".png";
00149 QString filename = QFileDialog::getSaveFileName( this, "Save image", default_save_file );
00150 if( filename != "" )
00151 {
00152 QString with_slashes = QDir::fromNativeSeparators( filename );
00153 QString file_part = with_slashes.section( '/', -1 );
00154 default_save_dir_ = QDir::toNativeSeparators( with_slashes.section( '/', 0, -2 ));
00155 Q_EMIT savedInDirectory( default_save_dir_ );
00156
00157
00158
00159
00160 if( file_part.lastIndexOf( "." ) <= 0 )
00161 {
00162 filename += ".png";
00163 }
00164 QImageWriter writer( filename );
00165 if( writer.write( screenshot_.toImage() ))
00166 {
00167 close();
00168 }
00169 else
00170 {
00171 QString error_message;
00172 if( writer.error() == QImageWriter::UnsupportedFormatError )
00173 {
00174 QString suffix = filename.section( '.', -1 );
00175 QString formats_string;
00176 QList<QByteArray> formats = QImageWriter::supportedImageFormats();
00177 formats_string = formats[0];
00178 for( int i = 1; i < formats.size(); i++ )
00179 {
00180 formats_string += ", " + formats[ i ];
00181 }
00182
00183 error_message =
00184 "File type '" + suffix + "' is not supported.\n" +
00185 "Supported image formats are: " + formats_string + "\n";
00186 }
00187 else
00188 {
00189 error_message = "Failed to write image to file " + filename;
00190 }
00191
00192 QMessageBox::critical( this, "Error", error_message );
00193 }
00194 }
00195 }
00196
00197 }