[QGIS Commit] r14328 - in trunk/qgis/src: app gui ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Mon Oct 4 10:57:40 EDT 2010


Author: timlinux
Date: 2010-10-04 14:57:40 +0000 (Mon, 04 Oct 2010)
New Revision: 14328

Modified:
   trunk/qgis/src/app/qgisapp.cpp
   trunk/qgis/src/app/qgsrasterlayerproperties.cpp
   trunk/qgis/src/app/qgsrasterlayerproperties.h
   trunk/qgis/src/gui/qgisgui.cpp
   trunk/qgis/src/gui/qgisgui.h
   trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui
Log:
Added option to save histogram and also refactored dialog filetype list for saving images into qgisgui

Modified: trunk/qgis/src/app/qgisapp.cpp
===================================================================
--- trunk/qgis/src/app/qgisapp.cpp	2010-10-04 10:00:28 UTC (rev 14327)
+++ trunk/qgis/src/app/qgisapp.cpp	2010-10-04 14:57:40 UTC (rev 14328)
@@ -2593,24 +2593,10 @@
 
 
 
-/**
 
-  Convenience function for readily creating file filters.
 
-  Given a long name for a file filter and a regular expression, return
-  a file filter string suitable for use in a QFileDialog::OpenFiles()
-  call.  The regular express, glob, will have both all lower and upper
-  case versions added.
 
-*/
-static QString createFileFilter_( QString const &longName, QString const &glob )
-{
-  return longName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
-}                               // createFileFilter_
 
-
-
-
 /**
   This method prompts the user for a list of vector file names  with a dialog.
   */
@@ -3558,94 +3544,16 @@
 
 void QgisApp::saveMapAsImage()
 {
-  //create a map to hold the QImageIO names and the filter names
-  //the QImageIO name must be passed to the mapcanvas saveas image function
-  typedef QMap<QString, QString> FilterMap;
-  FilterMap myFilterMap;
-
-  //find out the last used filter
-  QSettings myQSettings;  // where we keep last used filter in persistent state
-  QString myLastUsedFilter = myQSettings.value( "/UI/lastSaveAsImageFilter" ).toString();
-  QString myLastUsedDir = myQSettings.value( "/UI/lastSaveAsImageDir", "." ).toString();
-
-  // get a list of supported output image types
-  int myCounterInt = 0;
-  QString myFilters;
-  QList<QByteArray> formats = QImageWriter::supportedImageFormats();
-
-  for ( ; myCounterInt < formats.count(); myCounterInt++ )
+  QPair< QString,QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
+  if ( myFileNameAndFilter.first != "" )
   {
-    QString myFormat = QString( formats.at( myCounterInt ) );
-    //svg doesnt work so skip it
-    if ( myFormat ==  "svg" )
-      continue;
-
-    QString myFilter = createFileFilter_( myFormat + " format", "*." + myFormat );
-    if ( !myFilters.isEmpty() )
-      myFilters += ";;";
-    myFilters += myFilter;
-    myFilterMap[myFilter] = myFormat;
-  }
-#ifdef QGISDEBUG
-  QgsDebugMsg( "Available Filters Map: " );
-  FilterMap::Iterator myIterator;
-  for ( myIterator = myFilterMap.begin(); myIterator != myFilterMap.end(); ++myIterator )
-  {
-    QgsDebugMsg( myIterator.key() + "  :  " + myIterator.value() );
-  }
-#endif
-
-  //create a file dialog using the the filter list generated above
-  std::auto_ptr < QFileDialog > myQFileDialog( new QFileDialog( this,
-      tr( "Choose a file name to save the map image as" ),
-      myLastUsedDir, myFilters ) );
-
-  // allow for selection of more than one file
-  myQFileDialog->setFileMode( QFileDialog::AnyFile );
-
-  myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
-
-  myQFileDialog->setConfirmOverwrite( true );
-
-
-  if ( !myLastUsedFilter.isEmpty() )     // set the filter to the last one used
-  {
-    myQFileDialog->selectFilter( myLastUsedFilter );
-  }
-
-
-  //prompt the user for a fileName
-  QString myOutputFileNameQString; // = myQFileDialog->getSaveFileName(); //delete this
-  if ( myQFileDialog->exec() == QDialog::Accepted )
-  {
-    myOutputFileNameQString = myQFileDialog->selectedFiles().first();
-  }
-
-  QString myFilterString = myQFileDialog->selectedFilter();
-  QgsDebugMsg( "Selected filter: " + myFilterString );
-  QgsDebugMsg( "Image type to be passed to mapcanvas: " + myFilterMap[myFilterString] );
-
-  // Add the file type suffix to the fileName if required
-  if ( !myOutputFileNameQString.endsWith( myFilterMap[myFilterString] ) )
-  {
-    myOutputFileNameQString += "." + myFilterMap[myFilterString];
-  }
-
-  myQSettings.setValue( "/UI/lastSaveAsImageFilter", myFilterString );
-  myQSettings.setValue( "/UI/lastSaveAsImageDir", myQFileDialog->directory().absolutePath() );
-
-  if ( myOutputFileNameQString != "" )
-  {
-
     //save the mapview to the selected file
-    mMapCanvas->saveAsImage( myOutputFileNameQString, NULL, myFilterMap[myFilterString] );
-    statusBar()->showMessage( tr( "Saved map image to %1" ).arg( myOutputFileNameQString ) );
+    mMapCanvas->saveAsImage( myFileNameAndFilter.first, NULL, myFileNameAndFilter.second );
+    statusBar()->showMessage( tr( "Saved map image to %1" ).arg( myFileNameAndFilter.first ) );
   }
 
 } // saveMapAsImage
 
-
-
 //overloaded version of the above function
 void QgisApp::saveMapAsImage( QString theImageFileNameQString, QPixmap * theQPixmap )
 {

Modified: trunk/qgis/src/app/qgsrasterlayerproperties.cpp
===================================================================
--- trunk/qgis/src/app/qgsrasterlayerproperties.cpp	2010-10-04 10:00:28 UTC (rev 14327)
+++ trunk/qgis/src/app/qgsrasterlayerproperties.cpp	2010-10-04 14:57:40 UTC (rev 14328)
@@ -66,7 +66,8 @@
     : QDialog( parent, fl ),
     // Constant that signals property not used.
     TRSTRING_NOT_SET( tr( "Not Set" ) ),
-    mRasterLayer( qobject_cast<QgsRasterLayer *>( lyr ) )
+    mRasterLayer( qobject_cast<QgsRasterLayer *>( lyr ) ),
+    mpPlot( 0 )
 {
   ignoreSpinBoxEvent = false; //Short circuit signal loop between min max field and stdDev spin box
   mGrayMinimumMaximumEstimated = true;
@@ -204,6 +205,8 @@
   pbtnExportColorMapToFile->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );
   pbtnLoadColorMapFromFile->setIcon( QgisApp::getThemeIcon( "/mActionFileOpen.png" ) );
 
+  mSaveAsImageButton->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );
+  
   mMapCanvas = theCanvas;
   mPixelSelectorTool = 0;
   if ( mMapCanvas )
@@ -1849,29 +1852,31 @@
 
 void QgsRasterLayerProperties::refreshHistogram()
 {
+  if ( mpPlot != 0 )
+  {
+    delete mpPlot;
+  }
   mHistogramProgress->show();
   connect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
   QApplication::setOverrideCursor( Qt::WaitCursor );
   QgsDebugMsg( "entered." );
-
-  QwtPlot * mypPlot = new QwtPlot( mChartWidget );
-  mypPlot->canvas()->setCursor( Qt::ArrowCursor );
+  mpPlot = new QwtPlot( mChartWidget );
   //ensure all children get removed
-  mypPlot->setAutoDelete( true );
+  mpPlot->setAutoDelete( true );
   QVBoxLayout *mpHistogramLayout = new QVBoxLayout( mChartWidget );
   mpHistogramLayout->setContentsMargins( 0, 0, 0, 0 );
-  mpHistogramLayout->addWidget( mypPlot );
+  mpHistogramLayout->addWidget( mpPlot );
   mChartWidget->setLayout( mpHistogramLayout );
-  mypPlot->setTitle( QObject::tr( "Raster Histogram" ) );
-  mypPlot->insertLegend( new QwtLegend(), QwtPlot::BottomLegend );
+  mpPlot->setTitle( QObject::tr( "Raster Histogram") );
+  mpPlot->insertLegend( new QwtLegend(), QwtPlot::BottomLegend );
   // Set axis titles
-  mypPlot->setAxisTitle( QwtPlot::xBottom, QObject::tr( "Pixel Value" ) );
-  mypPlot->setAxisTitle( QwtPlot::yLeft, QObject::tr( "Frequency" ) );
-  mypPlot->setAxisAutoScale( QwtPlot::xBottom );
-  mypPlot->setAxisAutoScale( QwtPlot::yLeft );
+  mpPlot->setAxisTitle( QwtPlot::xBottom, QObject::tr("Pixel Value") );
+  mpPlot->setAxisTitle( QwtPlot::yLeft, QObject::tr("Frequency") );
+  mpPlot->setAxisAutoScale( QwtPlot::xBottom );
+  mpPlot->setAxisAutoScale( QwtPlot::yLeft );
   // add a grid
   QwtPlotGrid * myGrid = new QwtPlotGrid();
-  myGrid->attach( mypPlot );
+  myGrid->attach(mpPlot);
   // Explanation:
   // We use the gdal histogram creation routine is called for each selected
   // layer. Currently the hist is hardcoded
@@ -1917,15 +1922,39 @@
       myX2Data.append( double( myBin ) );
       myY2Data.append( double( myBinValue ) );
     }
-    mypCurve->setData( myX2Data, myY2Data );
-    mypCurve->attach( mypPlot );
+    mypCurve->setData(myX2Data,myY2Data);
+    mypCurve->attach(mpPlot);
   }
-  mypPlot->replot();
+  mpPlot->replot();
   disconnect( mRasterLayer, SIGNAL( progressUpdate( int ) ), mHistogramProgress, SLOT( setValue( int ) ) );
   mHistogramProgress->hide();
+  mpPlot->canvas()->setCursor(Qt::ArrowCursor);
   QApplication::restoreOverrideCursor();
 }
 
+void QgsRasterLayerProperties::on_mSaveAsImageButton_clicked()
+{
+  if ( mpPlot == 0 )
+  {
+    return;
+  }
+  
+  QPixmap myPixmap(600, 600);
+  myPixmap.fill(Qt::white); // Qt::transparent ?
+
+  QwtPlotPrintFilter myFilter;
+  int myOptions = QwtPlotPrintFilter::PrintAll;
+  myOptions &= ~QwtPlotPrintFilter::PrintBackground;
+  myOptions |= QwtPlotPrintFilter::PrintFrameWithScales;
+  myFilter.setOptions(myOptions);
+
+  mpPlot->print(myPixmap, myFilter);
+  QPair< QString,QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
+  if ( myFileNameAndFilter.first != "" )
+  {
+    myPixmap.save( myFileNameAndFilter.first );
+  }
+}
 void QgsRasterLayerProperties::on_pbnImportTransparentPixelValues_clicked()
 {
   int myLineCounter = 0;

Modified: trunk/qgis/src/app/qgsrasterlayerproperties.h
===================================================================
--- trunk/qgis/src/app/qgsrasterlayerproperties.h	2010-10-04 10:00:28 UTC (rev 14327)
+++ trunk/qgis/src/app/qgsrasterlayerproperties.h	2010-10-04 14:57:40 UTC (rev 14328)
@@ -31,8 +31,8 @@
 class QgsMapCanvas;
 class QgsRasterLayer;
 class QgsPixelSelectorTool;
+class QwtPlot;
 
-
 /**Property sheet for a raster map layer
   *@author Tim Sutton
   */
@@ -144,6 +144,8 @@
     void on_pbnSaveStyleAs_clicked();
     /** Help button */
     void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
+    /** This slot lets you save the histogram as an image to disk */
+    void on_mSaveAsImageButton_clicked();
 
   signals:
 
@@ -219,6 +221,7 @@
 
     QgsMapCanvas* mMapCanvas;
     QgsPixelSelectorTool* mPixelSelectorTool;
+    QwtPlot * mpPlot;
 };
 
 /**

Modified: trunk/qgis/src/gui/qgisgui.cpp
===================================================================
--- trunk/qgis/src/gui/qgisgui.cpp	2010-10-04 10:00:28 UTC (rev 14327)
+++ trunk/qgis/src/gui/qgisgui.cpp	2010-10-04 14:57:40 UTC (rev 14328)
@@ -15,9 +15,13 @@
 #include "qgisgui.h"
 
 #include <QSettings>
+#include <QObject> //for tr
+#include <QImageWriter>
 #include "qgsencodingfiledialog.h"
 #include "qgslogger.h"
+#include <memory> //for auto_ptr
 
+
 namespace QgisGui
 {
 
@@ -34,7 +38,7 @@
 
     haveLastUsedFilter = settings.contains( "/UI/" + filterName );
     QString lastUsedFilter = settings.value( "/UI/" + filterName,
-                             QVariant( QString::null ) ).toString();
+        QVariant( QString::null ) ).toString();
 
     QString lastUsedDir = settings.value( "/UI/" + filterName + "Dir", "." ).toString();
 
@@ -84,5 +88,91 @@
     return false;
   }
 
+  QPair<QString, QString> GUI_EXPORT getSaveAsImageName( QWidget * theParent, QString theMessage )
+  {
+    //create a map to hold the QImageIO names and the filter names
+    //the QImageIO name must be passed to the mapcanvas saveas image function
+    typedef QMap<QString, QString> FilterMap;
+    FilterMap myFilterMap;
 
+    //find out the last used filter
+    QSettings myQSettings;  // where we keep last used filter in persistent state
+    QString myLastUsedFilter = myQSettings.value( "/UI/lastSaveAsImageFilter" ).toString();
+    QString myLastUsedDir = myQSettings.value( "/UI/lastSaveAsImageDir", "." ).toString();
+
+    // get a list of supported output image types
+    int myCounterInt = 0;
+    QString myFilters;
+    QList<QByteArray> formats = QImageWriter::supportedImageFormats();
+
+    for ( ; myCounterInt < formats.count(); myCounterInt++ )
+    {
+      QString myFormat = QString( formats.at( myCounterInt ) );
+      //svg doesnt work so skip it
+      if ( myFormat ==  "svg" )
+        continue;
+
+      QString myFilter = createFileFilter_( myFormat + " format", "*." + myFormat );
+      if ( !myFilters.isEmpty() )
+        myFilters += ";;";
+      myFilters += myFilter;
+      myFilterMap[myFilter] = myFormat;
+    }
+#ifdef QGISDEBUG
+    QgsDebugMsg( "Available Filters Map: " );
+    FilterMap::Iterator myIterator;
+    for ( myIterator = myFilterMap.begin(); myIterator != myFilterMap.end(); ++myIterator )
+    {
+      QgsDebugMsg( myIterator.key() + "  :  " + myIterator.value() );
+    }
+#endif
+
+    //create a file dialog using the the filter list generated above
+    std::auto_ptr < QFileDialog > myQFileDialog( new QFileDialog( theParent,
+          QObject::tr( "Choose a file name to save the map image as" ),
+          myLastUsedDir, myFilters ) );
+
+    // allow for selection of more than one file
+    myQFileDialog->setFileMode( QFileDialog::AnyFile );
+
+    myQFileDialog->setAcceptMode( QFileDialog::AcceptSave );
+
+    myQFileDialog->setConfirmOverwrite( true );
+
+
+    if ( !myLastUsedFilter.isEmpty() )     // set the filter to the last one used
+    {
+      myQFileDialog->selectFilter( myLastUsedFilter );
+    }
+
+    //prompt the user for a fileName
+    QString myOutputFileName; // = myQFileDialog->getSaveFileName(); //delete this
+    if ( myQFileDialog->exec() == QDialog::Accepted )
+    {
+      myOutputFileName = myQFileDialog->selectedFiles().first();
+    }
+
+    QString myFilterString = myQFileDialog->selectedFilter();
+    QgsDebugMsg( "Selected filter: " + myFilterString );
+    QgsDebugMsg( "Image type: " + myFilterMap[myFilterString] );
+
+    // Add the file type suffix to the fileName if required
+    if ( !myOutputFileName.endsWith( myFilterMap[myFilterString] ) )
+    {
+      myOutputFileName += "." + myFilterMap[myFilterString];
+    }
+
+    myQSettings.setValue( "/UI/lastSaveAsImageFilter", myFilterString );
+    myQSettings.setValue( "/UI/lastSaveAsImageDir", myQFileDialog->directory().absolutePath() );
+    QPair <QString, QString> myPair;
+    myPair.first = myOutputFileName;
+    myPair.second = myFilterMap[myFilterString];
+    return myPair;
+  } // 
+
+  static QString createFileFilter_( QString const &longName, QString const &glob )
+  {
+    return longName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
+  }                               // createFileFilter_
+
 } // end of QgisGui namespace

Modified: trunk/qgis/src/gui/qgisgui.h
===================================================================
--- trunk/qgis/src/gui/qgisgui.h	2010-10-04 10:00:28 UTC (rev 14327)
+++ trunk/qgis/src/gui/qgisgui.h	2010-10-04 14:57:40 UTC (rev 14328)
@@ -17,6 +17,7 @@
 #define QGISGUI_H
 
 #include <Qt>
+#include <QPair>
 
 class QStringList;
 
@@ -75,6 +76,29 @@
       QString const &filters, QStringList & selectedFiles, QString& enc, QString &title,
       bool cancelAll = false );
 
+  /** A helper function to get an image name from the user. It will nicely 
+   * provide filters with all available writable image formats.
+   * @param QWidget - widget that should act as the parent for the file dialog
+   * @param QString the message to display to the user
+   * @return QPair<QString, QString> where first is the file name and second is
+   * the file type
+   * @note added in 1.6
+   */
+  QPair<QString, QString> GUI_EXPORT getSaveAsImageName(  QWidget * theParent, QString theMessage  );
+  
+  /**
+
+    Convenience function for readily creating file filters.
+
+    Given a long name for a file filter and a regular expression, return
+    a file filter string suitable for use in a QFileDialog::OpenFiles()
+    call.  The regular express, glob, will have both all lower and upper
+    case versions added.
+   * @note added in 1.6
+
+  */
+  static QString createFileFilter_( QString const &longName, QString const &glob );
+
 }
 
 #endif

Modified: trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui
===================================================================
--- trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui	2010-10-04 10:00:28 UTC (rev 14327)
+++ trunk/qgis/src/ui/qgsrasterlayerpropertiesbase.ui	2010-10-04 14:57:40 UTC (rev 14328)
@@ -1762,6 +1762,9 @@
 &lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;/p&gt;
 &lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;/p&gt;
 &lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;/p&gt;
+&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;/p&gt;
+&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;/p&gt;
+&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;/p&gt;
 &lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;&quot;&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</string>
          </property>
         </widget>
@@ -1854,6 +1857,17 @@
          </property>
         </widget>
        </item>
+       <item row="1" column="2">
+        <widget class="QToolButton" name="mSaveAsImageButton">
+         <property name="text">
+          <string>Save as image...</string>
+         </property>
+         <property name="icon">
+          <iconset>
+           <normaloff>../../images/themes/default/mActionFileSave.png</normaloff>../../images/themes/default/mActionFileSave.png</iconset>
+         </property>
+        </widget>
+       </item>
       </layout>
      </widget>
     </widget>



More information about the QGIS-commit mailing list