Skip to content

Commit 6dd42ca

Browse files
committed
Merge pull request #1864 from michaelkirk/feature/export-png-first-8493
[fixes #8493] "Save as Image" defaults to PNG
2 parents 1cd7dc6 + d6c969c commit 6dd42ca

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

src/gui/qgisgui.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace QgisGui
9393
if ( format == "svg" )
9494
continue;
9595

96-
filterMap.insert( createFileFilter_( format.toUpper() + " format", "*." + format ), format );
96+
filterMap.insert( createFileFilter_( format ), format );
9797
}
9898

9999
#ifdef QGISDEBUG
@@ -104,14 +104,13 @@ namespace QgisGui
104104
}
105105
#endif
106106

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

112-
QString outputFileName;
113-
QString selectedFilter = lastUsedFilter;
114-
QString ext;
110+
// Prefer "png" format unless the user previously chose a different format
111+
QString pngExtension = "png";
112+
QString pngFilter = createFileFilter_( pngExtension );
113+
QString selectedFilter = settings.value( "/UI/lastSaveAsImageFilter", pngFilter ).toString();
115114

116115
QString initialPath;
117116
if ( defaultFilename.isNull() )
@@ -125,6 +124,8 @@ namespace QgisGui
125124
initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
126125
}
127126

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

@@ -176,7 +177,14 @@ namespace QgisGui
176177

177178
QString createFileFilter_( QString const &longName, QString const &glob )
178179
{
179-
return longName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
180+
return QString("%1 (%2 %3)").arg( longName ).arg( glob.toLower() ).arg( glob.toUpper() );
181+
}
182+
183+
QString createFileFilter_( QString const &format )
184+
{
185+
QString longName = format.toUpper() + " format";
186+
QString glob = "*." + format;
187+
return createFileFilter_( longName, glob );
180188
}
181189

182190
} // end of QgisGui namespace

src/gui/qgisgui.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ namespace QgisGui
9191
case versions added.
9292
*/
9393
QString GUI_EXPORT createFileFilter_( QString const &longName, QString const &glob );
94+
95+
/**
96+
* Create file filters suitable for use with QFileDialog
97+
*
98+
* @param format extension e.g. "png"
99+
* @return QString e.g. "PNG format (*.png, *.PNG)"
100+
*/
101+
QString GUI_EXPORT createFileFilter_( QString const &format );
102+
94103
}
95104

96105
#endif

tests/src/gui/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ ADD_QGIS_TEST(zoomtest testqgsmaptoolzoom.cpp)
117117

118118
#ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
119119
ADD_QGIS_TEST(projectionissues testprojectionissues.cpp)
120+
ADD_QGIS_TEST(qgsguitest testqgsgui.cpp)
120121
ADD_QGIS_TEST(scalecombobox testqgsscalecombobox.cpp)
121122
ADD_QGIS_TEST(dualviewtest testqgsdualview.cpp )
122123
ADD_QGIS_TEST(doublespinbox testqgsdoublespinbox.cpp)

tests/src/gui/testqgsgui.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/***************************************************************************
2+
testqgsgui.cpp
3+
--------------------------------------
4+
Date : 26.1.2015
5+
Copyright : (C) 2015 Michael Kirk
6+
Email : michael at jackpine dot me
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include <QtTest/QtTest>
17+
#include <qgisgui.h>
18+
19+
class TestQgsGui : public QObject
20+
{
21+
Q_OBJECT
22+
private slots:
23+
void createFileFilterForFormat();
24+
void createFileFilter();
25+
26+
};
27+
28+
void TestQgsGui::createFileFilterForFormat()
29+
{
30+
QString expected = "FOO format (*.foo *.FOO)";
31+
QString actual = QgisGui::createFileFilter_("foo");
32+
33+
QCOMPARE( actual, expected );
34+
}
35+
36+
void TestQgsGui::createFileFilter()
37+
{
38+
QString expected = "My Description (my_regex MY_REGEX)";
39+
QString actual = QgisGui::createFileFilter_("My Description", "my_regex");
40+
41+
QCOMPARE( actual, expected );
42+
}
43+
44+
QTEST_MAIN( TestQgsGui )
45+
#include "testqgsgui.moc"

0 commit comments

Comments
 (0)