Skip to content

Commit

Permalink
Added option to save histogram and also refactored dialog filetype li…
Browse files Browse the repository at this point in the history
…st for saving images into qgisgui

git-svn-id: http://svn.osgeo.org/qgis/trunk@14328 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Oct 4, 2010
1 parent 79241a8 commit 4613f71
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 114 deletions.
100 changes: 4 additions & 96 deletions src/app/qgisapp.cpp
Expand Up @@ -2593,20 +2593,6 @@ void QgisApp::about()



/**
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_



Expand Down Expand Up @@ -3558,94 +3544,16 @@ void QgisApp::showComposerManager()

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++ )
{
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 )
QPair< QString,QString> myFileNameAndFilter = QgisGui::getSaveAsImageName( this, tr( "Choose a file name to save the map image as" ) );
if ( myFileNameAndFilter.first != "" )
{
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 )
{
Expand Down
61 changes: 45 additions & 16 deletions src/app/qgsrasterlayerproperties.cpp
Expand Up @@ -66,7 +66,8 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
: 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;
Expand Down Expand Up @@ -204,6 +205,8 @@ QgsRasterLayerProperties::QgsRasterLayerProperties( QgsMapLayer* lyr, QgsMapCanv
pbtnExportColorMapToFile->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );
pbtnLoadColorMapFromFile->setIcon( QgisApp::getThemeIcon( "/mActionFileOpen.png" ) );

mSaveAsImageButton->setIcon( QgisApp::getThemeIcon( "/mActionFileSave.png" ) );

mMapCanvas = theCanvas;
mPixelSelectorTool = 0;
if ( mMapCanvas )
Expand Down Expand Up @@ -1849,29 +1852,31 @@ void QgsRasterLayerProperties::on_tabBar_currentChanged( int theTab )

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
Expand Down Expand Up @@ -1917,15 +1922,39 @@ void QgsRasterLayerProperties::refreshHistogram()
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;
Expand Down
5 changes: 4 additions & 1 deletion src/app/qgsrasterlayerproperties.h
Expand Up @@ -31,7 +31,7 @@ class QgsMapLayer;
class QgsMapCanvas;
class QgsRasterLayer;
class QgsPixelSelectorTool;

class QwtPlot;

/**Property sheet for a raster map layer
*@author Tim Sutton
Expand Down Expand Up @@ -144,6 +144,8 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope
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:

Expand Down Expand Up @@ -219,6 +221,7 @@ class QgsRasterLayerProperties : public QDialog, private Ui::QgsRasterLayerPrope

QgsMapCanvas* mMapCanvas;
QgsPixelSelectorTool* mPixelSelectorTool;
QwtPlot * mpPlot;
};

/**
Expand Down
92 changes: 91 additions & 1 deletion src/gui/qgisgui.cpp
Expand Up @@ -15,8 +15,12 @@
#include "qgisgui.h"

#include <QSettings>
#include <QObject> //for tr
#include <QImageWriter>
#include "qgsencodingfiledialog.h"
#include "qgslogger.h"
#include <memory> //for auto_ptr


namespace QgisGui
{
Expand All @@ -34,7 +38,7 @@ namespace QgisGui

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();

Expand Down Expand Up @@ -84,5 +88,91 @@ namespace QgisGui
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

0 comments on commit 4613f71

Please sign in to comment.