QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgisgui.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgisgui.cpp - Constants used throughout the QGIS GUI.
3  --------------------------------------
4  Date : 11-Jan-2006
5  Copyright : (C) 2006 by Tom Elwertowski
6  Email : telwertowski at users dot sourceforge dot net
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 #include "qgisgui.h"
16 
17 #include <QSettings>
18 #include <QObject> //for tr
19 #include <QImageWriter>
20 #include "qgsencodingfiledialog.h"
21 #include "qgslogger.h"
22 #include <memory> //for auto_ptr
23 
24 
25 namespace QgisGui
26 {
27 
28  bool GUI_EXPORT openFilesRememberingFilter( QString const &filterName,
29  QString const &filters, QStringList & selectedFiles, QString& enc, QString &title,
30  bool cancelAll )
31  {
32  Q_UNUSED( enc );
33 
34  QSettings settings;
35  QString lastUsedFilter = settings.value( "/UI/" + filterName, "" ).toString();
36  QString lastUsedDir = settings.value( "/UI/" + filterName + "Dir", "." ).toString();
37 
38  QgsDebugMsg( "Opening file dialog with filters: " + filters );
39  if ( !cancelAll )
40  {
41  selectedFiles = QFileDialog::getOpenFileNames( 0, title, lastUsedDir, filters, &lastUsedFilter );
42  }
43  else //we have to use non-native dialog to add cancel all button
44  {
45  QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog( 0, title, lastUsedDir, filters, QString( "" ) );
46 
47  // allow for selection of more than one file
48  openFileDialog->setFileMode( QFileDialog::ExistingFiles );
49 
50  if ( !lastUsedFilter.isEmpty() )
51  {
52  openFileDialog->selectFilter( lastUsedFilter );
53  }
54  openFileDialog->addCancelAll();
55  if ( openFileDialog->exec() == QDialog::Accepted )
56  {
57  selectedFiles = openFileDialog->selectedFiles();
58  }
59  else
60  {
61  //cancel or cancel all?
62  if ( openFileDialog->cancelAll() )
63  {
64  return true;
65  }
66  }
67  }
68 
69  if ( !selectedFiles.isEmpty() )
70  {
71  // Fix by Tim - getting the dirPath from the dialog
72  // directly truncates the last node in the dir path.
73  // This is a workaround for that
74  QString firstFileName = selectedFiles.first();
75  QFileInfo fi( firstFileName );
76  QString path = fi.path();
77 
78  QgsDebugMsg( "Writing last used dir: " + path );
79 
80  settings.setValue( "/UI/" + filterName, lastUsedFilter );
81  settings.setValue( "/UI/" + filterName + "Dir", path );
82  }
83  return false;
84  }
85 
86  QPair<QString, QString> GUI_EXPORT getSaveAsImageName( QWidget *theParent, QString theMessage, QString defaultFilename )
87  {
88  // get a list of supported output image types
89  QMap<QString, QString> filterMap;
90  foreach ( QByteArray format, QImageWriter::supportedImageFormats() )
91  {
92  //svg doesnt work so skip it
93  if ( format == "svg" )
94  continue;
95 
96  filterMap.insert( createFileFilter_( format.toUpper() + " format", "*." + format ), format );
97  }
98 
99 #ifdef QGISDEBUG
100  QgsDebugMsg( "Available Filters Map: " );
101  for ( QMap<QString, QString>::iterator it = filterMap.begin(); it != filterMap.end(); ++it )
102  {
103  QgsDebugMsg( it.key() + " : " + it.value() );
104  }
105 #endif
106 
107  //find out the last used filter
108  QSettings settings; // where we keep last used filter in persistent state
109  QString lastUsedFilter = settings.value( "/UI/lastSaveAsImageFilter" ).toString();
110  QString lastUsedDir = settings.value( "/UI/lastSaveAsImageDir", "." ).toString();
111 
112  QString outputFileName;
113  QString selectedFilter = lastUsedFilter;
114  QString ext;
115 
116  QString initialPath;
117  if ( defaultFilename.isNull() )
118  {
119  //no default filename provided, just use last directory
120  initialPath = lastUsedDir;
121  }
122  else
123  {
124  //a default filename was provided, so use it to build the initial path
125  initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
126  }
127 
128 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX)
129  outputFileName = QFileDialog::getSaveFileName( theParent, theMessage, initialPath, QStringList( filterMap.keys() ).join( ";;" ), &selectedFilter );
130 
131  if ( !outputFileName.isNull() )
132  {
133  ext = filterMap.value( selectedFilter, QString::null );
134  if ( !ext.isNull() )
135  settings.setValue( "/UI/lastSaveAsImageFilter", selectedFilter );
136  settings.setValue( "/UI/lastSaveAsImageDir", QFileInfo( outputFileName ).absolutePath() );
137  }
138 #else
139  //create a file dialog using the filter list generated above
140  std::auto_ptr<QFileDialog> fileDialog( new QFileDialog( theParent, theMessage, initialPath, QStringList( filterMap.keys() ).join( ";;" ) ) );
141 
142  // allow for selection of more than one file
143  fileDialog->setFileMode( QFileDialog::AnyFile );
144  fileDialog->setAcceptMode( QFileDialog::AcceptSave );
145  fileDialog->setConfirmOverwrite( true );
146 
147  if ( !lastUsedFilter.isEmpty() ) // set the filter to the last one used
148  {
149  fileDialog->selectFilter( lastUsedFilter );
150  }
151 
152  //prompt the user for a fileName
153  if ( fileDialog->exec() == QDialog::Accepted )
154  {
155  outputFileName = fileDialog->selectedFiles().first();
156  }
157 
158  selectedFilter = fileDialog->selectedFilter();
159  QgsDebugMsg( "Selected filter: " + selectedFilter );
160  ext = filterMap.value( selectedFilter, QString::null );
161 
162  if ( !ext.isNull() )
163  settings.setValue( "/UI/lastSaveAsImageFilter", selectedFilter );
164 
165  settings.setValue( "/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
166 #endif
167 
168  // Add the file type suffix to the fileName if required
169  if ( !ext.isNull() && !outputFileName.toLower().endsWith( "." + ext.toLower() ) )
170  {
171  outputFileName += "." + ext;
172  }
173 
174  return qMakePair<QString, QString>( outputFileName, ext );
175  }
176 
177  QString createFileFilter_( QString const &longName, QString const &glob )
178  {
179  return longName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
180  }
181 
182 } // end of QgisGui namespace