[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    KFileDialog patch: auto-select format based on extension.
From:       David Faure <faure () kde ! org>
Date:       2005-10-10 10:20:57
Message-ID: 200510101220.57743.faure () kde ! org
[Download RAW message or body]

Here's a patch for KFileDialog in saving mode:

Clarence added "auto select extension based on format", which is good, however
people still have the problem that they type "foo.txt" without remembering to change
select the format in the filter combo, and as a result they get a native KWord document
named "foo.txt". Or the same for any mismatching combination of an extension and a file format :)

The idea to fix this is: also auto select the output format based on the typed extension,
when there's one. To avoid hidden behavior and nasty surprises, the format changes
in the filter combo immediately when the extension is recognized.

Do you think I should commit this to the 3.5 branch?  (I committed to trunk already)
IMHO it fixes an important usability problem in e.g. KOffice.

It really makes little sense to save a file with an extension .a and format B,
when all of KDE will later on recognize that file as format A due to its extension.

-- 
David Faure, faure@kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).

["kfiledialog-autoselect.diff" (text/x-diff)]

Index: kfilefiltercombo.h
===================================================================
--- kfilefiltercombo.h	(revision 462386)
+++ kfilefiltercombo.h	(working copy)
@@ -79,10 +79,11 @@
      * @see setDefaultFilter
      */
     QString defaultFilter() const;
-    
+
  protected:
     virtual bool eventFilter( QObject *o, QEvent *e );
 
+// KDE4 TODO: private, and s/filters/m_filters/, and add filters()
     QStringList filters;
     bool m_allTypes;
 
@@ -95,6 +96,7 @@
 protected:
     virtual void virtual_hook( int id, void* data );
 private:
+    friend class KFileDialog; // KDE4: it should call filters() instead
     class KFileFilterComboPrivate;
     KFileFilterComboPrivate *d;
 };
Index: kfiledialog.cpp
===================================================================
--- kfiledialog.cpp	(revision 462386)
+++ kfiledialog.cpp	(working copy)
@@ -1294,6 +1294,8 @@
 {
     if ( text.isEmpty() && ops->view() )
         ops->view()->clearSelection();
+
+    updateFilter();
 }
 
 void KFileDialog::updateStatusLine(int /* dirs */, int /* files */)
@@ -2077,11 +2079,32 @@
             fileName.truncate (dot);
 
         // add extension
-        locationEdit->setCurrentText (urlStr.left (fileNameOffset) + fileName + d->extension);
-        locationEdit->lineEdit()->setEdited (true);
+        const QString newText = urlStr.left (fileNameOffset) + fileName + d->extension;
+        if ( newText != locationEdit->currentText() )
+        {
+            locationEdit->setCurrentText (urlStr.left (fileNameOffset) + fileName + d->extension);
+            locationEdit->lineEdit()->setEdited (true);
+        }
     }
 }
 
+// Updates the filter if the extension of the filename specified in locationEdit is changed
+// (this prevents you from accidently saving "file.kwd" as RTF, for example)
+void KFileDialog::updateFilter ()
+{
+    if ((operationMode() == Saving) && (mode() & KFile::File) ) {
+        const QString urlStr = locationEdit->currentText ();
+        if (urlStr.isEmpty ())
+            return;
+
+        KMimeType::Ptr mime = KMimeType::findByPath(urlStr, 0, true);
+        if (mime && mime->name() != KMimeType::defaultMimeType()) {
+            if (filterWidget->filters.findIndex(mime->name()) != -1 )
+                filterWidget->setCurrentFilter(mime->name());
+        }
+    }
+}
+
 // applies only to a file that doesn't already exist
 void KFileDialog::appendExtension (KURL &url)
 {
Index: kfiledialog.h
===================================================================
--- kfiledialog.h	(revision 462386)
+++ kfiledialog.h	(working copy)
@@ -968,6 +968,7 @@
 
     void appendExtension(KURL &url);
     void updateLocationEditExtension(const QString &);
+    void updateFilter();
 
     static void initStatic();
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic