Skip to content

Commit

Permalink
Merge pull request #1864 from michaelkirk/feature/export-png-first-8493
Browse files Browse the repository at this point in the history
[fixes #8493] "Save as Image" defaults to PNG
  • Loading branch information
NathanW2 committed Jan 27, 2015
2 parents 1cd7dc6 + d6c969c commit 6dd42ca
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/gui/qgisgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace QgisGui
if ( format == "svg" )
continue;

filterMap.insert( createFileFilter_( format.toUpper() + " format", "*." + format ), format );
filterMap.insert( createFileFilter_( format ), format );
}

#ifdef QGISDEBUG
Expand All @@ -104,14 +104,13 @@ namespace QgisGui
}
#endif

//find out the last used filter
QSettings settings; // where we keep last used filter in persistent state
QString lastUsedFilter = settings.value( "/UI/lastSaveAsImageFilter" ).toString();
QString lastUsedDir = settings.value( "/UI/lastSaveAsImageDir", "." ).toString();

QString outputFileName;
QString selectedFilter = lastUsedFilter;
QString ext;
// Prefer "png" format unless the user previously chose a different format
QString pngExtension = "png";
QString pngFilter = createFileFilter_( pngExtension );
QString selectedFilter = settings.value( "/UI/lastSaveAsImageFilter", pngFilter ).toString();

QString initialPath;
if ( defaultFilename.isNull() )
Expand All @@ -125,6 +124,8 @@ namespace QgisGui
initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
}

QString outputFileName;
QString ext;
#if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX)
outputFileName = QFileDialog::getSaveFileName( theParent, theMessage, initialPath, QStringList( filterMap.keys() ).join( ";;" ), &selectedFilter );

Expand Down Expand Up @@ -176,7 +177,14 @@ namespace QgisGui

QString createFileFilter_( QString const &longName, QString const &glob )
{
return longName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
return QString("%1 (%2 %3)").arg( longName ).arg( glob.toLower() ).arg( glob.toUpper() );
}

QString createFileFilter_( QString const &format )
{
QString longName = format.toUpper() + " format";
QString glob = "*." + format;
return createFileFilter_( longName, glob );
}

} // end of QgisGui namespace
9 changes: 9 additions & 0 deletions src/gui/qgisgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ namespace QgisGui
case versions added.
*/
QString GUI_EXPORT createFileFilter_( QString const &longName, QString const &glob );

/**
* Create file filters suitable for use with QFileDialog
*
* @param format extension e.g. "png"
* @return QString e.g. "PNG format (*.png, *.PNG)"
*/
QString GUI_EXPORT createFileFilter_( QString const &format );

}

#endif
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ ADD_QGIS_TEST(zoomtest testqgsmaptoolzoom.cpp)

#ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
ADD_QGIS_TEST(projectionissues testprojectionissues.cpp)
ADD_QGIS_TEST(qgsguitest testqgsgui.cpp)
ADD_QGIS_TEST(scalecombobox testqgsscalecombobox.cpp)
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp )
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)
Expand Down
45 changes: 45 additions & 0 deletions tests/src/gui/testqgsgui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/***************************************************************************
testqgsgui.cpp
--------------------------------------
Date : 26.1.2015
Copyright : (C) 2015 Michael Kirk
Email : michael at jackpine dot me
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QtTest/QtTest>
#include <qgisgui.h>

class TestQgsGui : public QObject
{
Q_OBJECT
private slots:
void createFileFilterForFormat();
void createFileFilter();

};

void TestQgsGui::createFileFilterForFormat()
{
QString expected = "FOO format (*.foo *.FOO)";
QString actual = QgisGui::createFileFilter_("foo");

QCOMPARE( actual, expected );
}

void TestQgsGui::createFileFilter()
{
QString expected = "My Description (my_regex MY_REGEX)";
QString actual = QgisGui::createFileFilter_("My Description", "my_regex");

QCOMPARE( actual, expected );
}

QTEST_MAIN( TestQgsGui )
#include "testqgsgui.moc"

0 comments on commit 6dd42ca

Please sign in to comment.