screenshot_dialog.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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> // Included so we know that QPushButton inherits QAbstractButton 
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 ) // This should be a top-level window to act like a dialog.
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     // If filename has no dot, like "image" or has a dot in the zeroth
00164     // position, like ".image", add ".png" to give a default file
00165     // format.
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 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28