QGIS API Documentation  2.0.1-Dufour
 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 )
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 + " 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 #if defined(Q_OS_WIN) || defined(Q_OS_MAC)
117  outputFileName = QFileDialog::getSaveFileName( theParent, theMessage, lastUsedDir, QStringList( filterMap.keys() ).join( ";;" ), &selectedFilter );
118 
119  if ( !outputFileName.isNull() )
120  {
121  ext = filterMap.value( selectedFilter, QString::null );
122  if ( !ext.isNull() )
123  settings.setValue( "/UI/lastSaveAsImageFilter", selectedFilter );
124  settings.setValue( "/UI/lastSaveAsImageDir", QFileInfo( outputFileName ).absolutePath() );
125  }
126 #else
127  //create a file dialog using the filter list generated above
128  std::auto_ptr<QFileDialog> fileDialog( new QFileDialog( theParent, theMessage, lastUsedDir, QStringList( filterMap.keys() ).join( ";;" ) ) );
129 
130  // allow for selection of more than one file
131  fileDialog->setFileMode( QFileDialog::AnyFile );
132  fileDialog->setAcceptMode( QFileDialog::AcceptSave );
133  fileDialog->setConfirmOverwrite( true );
134 
135  if ( !lastUsedFilter.isEmpty() ) // set the filter to the last one used
136  {
137  fileDialog->selectFilter( lastUsedFilter );
138  }
139 
140  //prompt the user for a fileName
141  if ( fileDialog->exec() == QDialog::Accepted )
142  {
143  outputFileName = fileDialog->selectedFiles().first();
144  }
145 
146  selectedFilter = fileDialog->selectedFilter();
147  QgsDebugMsg( "Selected filter: " + selectedFilter );
148  ext = filterMap.value( selectedFilter, QString::null );
149 
150  if ( !ext.isNull() )
151  settings.setValue( "/UI/lastSaveAsImageFilter", selectedFilter );
152 
153  settings.setValue( "/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
154 #endif
155 
156  // Add the file type suffix to the fileName if required
157  if ( !ext.isNull() && !outputFileName.endsWith( "." + ext ) )
158  {
159  outputFileName += "." + ext;
160  }
161 
162  return qMakePair<QString, QString>( outputFileName, ext );
163  }
164 
165  QString createFileFilter_( QString const &longName, QString const &glob )
166  {
167  return longName + " (" + glob.toLower() + " " + glob.toUpper() + ")";
168  }
169 
170 } // end of QgisGui namespace