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 
00045 namespace rviz
00046 {
00047 
00048 ScreenshotDialog::ScreenshotDialog( QWidget* main_window, QWidget* render_window, const QString& default_save_dir )
00049   : QWidget( NULL ) // This should be a top-level window to act like a dialog.
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     // If filename has no dot, like "image" or has a dot in the zeroth
00158     // position, like ".image", add ".png" to give a default file
00159     // format.
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 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:36