[Qgis-developer] Mac Use of QFileDialog

Tom Elwertowski telwertowski at comcast.net
Mon May 19 00:37:38 EDT 2008


Attached is a patch which uses setSidebarUrls to access multiple Mac drives and make QFileDialog look more like a Mac dialog. It has two deficiencies.

1. The volumes are listed alphabetically rather by mount order.

2. If you navigate into the root of the boot volume, for example /Applications or /Network, QFileDialog discovers that /Volumes should be invisible and no longer lets you browse /Volumes. The workaround is to cancel and open the dialog again. If the user goes directly to an external volume, this won't be noticed.


This demo patch only fixes file dialogs in qgisapp.cpp. These are probably the most noticeable. To fix them all, QFileDialog needs to subclassed. I'll do this if other Mac users think this is better than the current QFileDialog behavior.

Tom


Tom Elwertowski wrote:
> QFileDialog is not including /Volumes in its left sidebar. This appears 
> to be a Qt bug which I will report. I will suggest that they approximate 
> the sidebar of a native file dialog as much as possible but definitely 
> show all volumes. Under /Computer I see /dev/fd. It doesn't show 
> anything useful but perhaps is supposed to show volumes.
> 
> QFileDialog has a setSidebarUrls() method. A workaround for now is to 
> use this to append the /Volumes list ourselves.
-------------- next part --------------
Index: src/app/qgisapp.cpp
===================================================================
--- src/app/qgisapp.cpp	(revision 8455)
+++ src/app/qgisapp.cpp	(working copy)
@@ -1877,6 +1877,29 @@
 }                               // buildSupportedVectorFileFilter_()
 
 
+#ifdef Q_WS_MAC
+/**
+  Fill QFileDialog sidebar with items approximating a native Mac file dialog.
+*/
+static void setupSidebar_(QFileDialog* fileDialog)
+{
+  QList<QUrl> sidebarUrls;
+  QDir volDir("/Volumes");
+  QStringList volumes(volDir.entryList(volDir.filter() | QDir::NoDotAndDotDot));
+  QStringList::const_iterator vol;
+  for (vol = volumes.begin(); vol != volumes.end(); ++vol)
+  {
+    sidebarUrls.append(QUrl::fromLocalFile("/Volumes/" + *vol));
+  }
+  sidebarUrls.append(QUrl::fromLocalFile("/Network"));
+  QString home(QDir::homePath());
+  sidebarUrls.append(QUrl::fromLocalFile(home + "/Desktop"));
+  sidebarUrls.append(QUrl::fromLocalFile(home));
+  sidebarUrls.append(QUrl::fromLocalFile("/Applications"));
+  sidebarUrls.append(QUrl::fromLocalFile(home + "/Documents"));
+  fileDialog->setSidebarUrls(sidebarUrls);
+}
+#endif
 
 
 /**
@@ -1925,6 +1948,10 @@
   QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(0,
       title, lastUsedDir, filters, lastUsedEncoding);
 
+#ifdef Q_WS_MAC
+  setupSidebar_(openFileDialog);
+#endif
+
   // allow for selection of more than one file
   openFileDialog->setMode(QFileDialog::ExistingFiles);
 
@@ -2599,6 +2626,10 @@
   QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(this,
     tr("Save As"), lastUsedDir, "", lastUsedEncoding);
 
+#ifdef Q_WS_MAC
+  setupSidebar_(openFileDialog);
+#endif
+
   // allow for selection of more than one file
   openFileDialog->setMode(QFileDialog::AnyFile);
   openFileDialog->setAcceptMode(QFileDialog::AcceptSave); 
@@ -2701,6 +2732,9 @@
         lastUsedDir, QObject::tr("QGis files (*.qgs)"));
     openFileDialog->setMode(QFileDialog::ExistingFile);
 
+#ifdef Q_WS_MAC
+  setupSidebar_(openFileDialog);
+#endif
 
     QString fullPath;
     if (openFileDialog->exec() == QDialog::Accepted)
@@ -2872,6 +2906,10 @@
         tr("Choose a QGIS project file"),
         lastUsedDir, QObject::tr("QGis files (*.qgs)")) );
 
+#ifdef Q_WS_MAC
+    setupSidebar_(&*saveFileDialog);
+#endif
+
     saveFileDialog->setMode(QFileDialog::AnyFile);
     saveFileDialog->setAcceptMode(QFileDialog::AcceptSave); 
     saveFileDialog->setConfirmOverwrite( true ); 
@@ -2945,6 +2983,10 @@
       tr("Choose a filename to save the QGIS project file as"),
       lastUsedDir, QObject::tr("QGis files (*.qgs)")) );
 
+#ifdef Q_WS_MAC
+  setupSidebar_(&*saveFileDialog);
+#endif
+
   saveFileDialog->setMode(QFileDialog::AnyFile);
 
   saveFileDialog->setAcceptMode(QFileDialog::AcceptSave);
@@ -3185,6 +3227,10 @@
       tr("Choose a filename to save the map image as"),
       myLastUsedDir, myFilters) );
 
+#ifdef Q_WS_MAC
+  setupSidebar_(&*myQFileDialog);
+#endif
+
   // allow for selection of more than one file
   myQFileDialog->setMode(QFileDialog::AnyFile);
 


More information about the Qgis-developer mailing list