QGIS API Documentation  2.8.2-Wien
 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 <QImageWriter>
19 #include "qgsencodingfiledialog.h"
20 #include "qgslogger.h"
21 
22 #include <QFontDialog>
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->selectNameFilter( 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 ), 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  QSettings settings; // where we keep last used filter in persistent state
108  QString lastUsedDir = settings.value( "/UI/lastSaveAsImageDir", "." ).toString();
109 
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();
114 
115  QString initialPath;
116  if ( defaultFilename.isNull() )
117  {
118  //no default filename provided, just use last directory
119  initialPath = lastUsedDir;
120  }
121  else
122  {
123  //a default filename was provided, so use it to build the initial path
124  initialPath = QDir( lastUsedDir ).filePath( defaultFilename );
125  }
126 
127  QString outputFileName;
128  QString ext;
129 #if defined(Q_OS_WIN) || defined(Q_OS_MAC) || defined(Q_OS_LINUX)
130  outputFileName = QFileDialog::getSaveFileName( theParent, theMessage, initialPath, QStringList( filterMap.keys() ).join( ";;" ), &selectedFilter );
131 
132  if ( !outputFileName.isNull() )
133  {
134  ext = filterMap.value( selectedFilter, QString::null );
135  if ( !ext.isNull() )
136  settings.setValue( "/UI/lastSaveAsImageFilter", selectedFilter );
137  settings.setValue( "/UI/lastSaveAsImageDir", QFileInfo( outputFileName ).absolutePath() );
138  }
139 #else
140 
141  //create a file dialog using the filter list generated above
142  QScopedPointer<QFileDialog> fileDialog( new QFileDialog( theParent, theMessage, initialPath, QStringList( filterMap.keys() ).join( ";;" ) ) );
143 
144  // allow for selection of more than one file
145  fileDialog->setFileMode( QFileDialog::AnyFile );
146  fileDialog->setAcceptMode( QFileDialog::AcceptSave );
147  fileDialog->setConfirmOverwrite( true );
148 
149  if ( !selectedFilter.isEmpty() ) // set the filter to the last one used
150  {
151  fileDialog->selectNameFilter( selectedFilter );
152  }
153 
154  //prompt the user for a fileName
155  if ( fileDialog->exec() == QDialog::Accepted )
156  {
157  outputFileName = fileDialog->selectedFiles().first();
158  }
159 
160  selectedFilter = fileDialog->selectedFilter();
161  QgsDebugMsg( "Selected filter: " + selectedFilter );
162  ext = filterMap.value( selectedFilter, QString::null );
163 
164  if ( !ext.isNull() )
165  settings.setValue( "/UI/lastSaveAsImageFilter", selectedFilter );
166 
167  settings.setValue( "/UI/lastSaveAsImageDir", fileDialog->directory().absolutePath() );
168 #endif
169 
170  // Add the file type suffix to the fileName if required
171  if ( !ext.isNull() && !outputFileName.toLower().endsWith( "." + ext.toLower() ) )
172  {
173  outputFileName += "." + ext;
174  }
175 
176  return qMakePair<QString, QString>( outputFileName, ext );
177  }
178 
179  QString createFileFilter_( QString const &longName, QString const &glob )
180  {
181  return QString( "%1 (%2 %3)" ).arg( longName ).arg( glob.toLower() ).arg( glob.toUpper() );
182  }
183 
184  QString createFileFilter_( QString const &format )
185  {
186  QString longName = format.toUpper() + " format";
187  QString glob = "*." + format;
188  return createFileFilter_( longName, glob );
189  }
190 
191  QFont getFont( bool &ok, const QFont &initial, const QString &title )
192  {
193  // parent is intentionally not set to 'this' as
194  // that would make it follow the style sheet font
195  // see also #12233 and #4937
196 #if defined(Q_OS_MAC) && defined(QT_MAC_USE_COCOA)
197  // Native Mac dialog works only for Qt Carbon
198  return QFontDialog::getFont( &ok, initial, 0, title, QFontDialog::DontUseNativeDialog );
199 #else
200  return QFontDialog::getFont( &ok, initial, 0, title );
201 #endif
202  }
203 
204 } // end of QgisGui namespace