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