[PATCH] Encoding box in the file dialog

Joseph Wenninger jowenn at kde.org
Thu Sep 25 13:53:46 BST 2003


Hi

I enclose a patch, which adds some additional static methods to the
kfiledialog to make the encoding selection more usable, without the need
of inheritance or #define protectd/private public

The code is not really tested it, I just would like to know, if
something like my patch would be accepted, before I put further work
into it


Kind regards
Joseph Wenninger

-------------- next part --------------
? additional_statics.diff
Index: kfiledialog.cpp
===================================================================
RCS file: /home/kde/kdelibs/kio/kfile/kfiledialog.cpp,v
retrieving revision 1.360
diff -u -3 -p -r1.360 kfiledialog.cpp
--- kfiledialog.cpp	23 Sep 2003 11:36:19 -0000	1.360
+++ kfiledialog.cpp	25 Sep 2003 12:48:40 -0000
@@ -2246,4 +2246,138 @@ void KFileDialog::setStartDir( const KUR
 void KFileDialog::virtual_hook( int id, void* data )
 { KDialogBase::virtual_hook( id, data ); }
 
+
+
+
+
+KFileDialog::ExtendedResult KFileDialog::getOpenFileNameAndEncoding(const QString& encoding,
+ 				     const QString& startDir,
+                                     const QString& filter,
+                                     QWidget *parent, const QString& caption)
+{
+    KFileDialog dlg(startDir, encoding,caption.isNull() ? i18n("Open") : caption,Opening,parent, "filedialog", true);
+    dlg.setFilter(filter);
+
+    dlg.setMode( KFile::File | KFile::LocalOnly );
+    dlg.ops->clearHistory();
+    dlg.exec();
+ 
+    ExtendedResult res;
+    res.fileNames<<dlg.selectedFile();
+    res.encoding=dlg.selectedEncoding();	
+    return res;
+}
+
+KFileDialog::ExtendedResult KFileDialog::getOpenFileNamesAndEncoding(const QString& encoding,
+					  const QString& startDir,
+                                          const QString& filter,
+                                          QWidget *parent,
+                                          const QString& caption)
+{
+    KFileDialog dlg(startDir, encoding,caption.isNull() ? i18n("Open") : caption,Opening,parent, "filedialog", true);
+    dlg.setFilter(filter);
+    dlg.setMode(KFile::Files | KFile::LocalOnly);
+    dlg.ops->clearHistory();
+    dlg.exec();
+
+    ExtendedResult res;
+    res.fileNames=dlg.selectedFiles();
+    res.encoding=dlg.selectedEncoding();
+    return res;
+}
+
+KFileDialog::ExtendedResult KFileDialog::getOpenURLAndEncoding(const QString& encoding, const QString& startDir, 
+				const QString& filter, QWidget *parent, const QString& caption)
+{
+    KFileDialog dlg(startDir, encoding,caption.isNull() ? i18n("Open") : caption,Opening,parent, "filedialog", true);
+    dlg.setFilter(filter);
+
+    dlg.setMode( KFile::File );
+    dlg.ops->clearHistory();
+    dlg.exec();
+
+    ExtendedResult res;
+    res.URLs<<dlg.selectedURL();
+    res.encoding=dlg.selectedEncoding();
+    return res;
+}
+
+KFileDialog::ExtendedResult KFileDialog::getOpenURLsAndEncoding(const QString& encoding, const QString& startDir,
+                                          const QString& filter,
+                                          QWidget *parent,
+                                          const QString& caption)
+{
+    KFileDialog dlg(startDir, encoding,caption.isNull() ? i18n("Open") : caption,Opening,parent, "filedialog", true);
+    dlg.setFilter(filter);
+
+    dlg.setMode(KFile::Files);
+    dlg.ops->clearHistory();
+    dlg.exec();
+
+    ExtendedResult res;
+    res.URLs=dlg.selectedURLs();
+    res.encoding=dlg.selectedEncoding();
+    return res;
+}
+
+
+KFileDialog::ExtendedResult KFileDialog::getSaveFileNameAndEncoding(const QString& encoding,
+			             const QString& dir, 
+				     const QString& filter,
+                                     QWidget *parent,
+                                     const QString& caption)
+{
+    bool specialDir = dir.at(0) == ':';
+    KFileDialog dlg(specialDir?dir:QString::null, encoding,caption.isNull() ? i18n("Save As") : caption,
+	Saving,parent, "filedialog", true);
+    dlg.setFilter(filter);
+    if ( !specialDir )
+        dlg.setSelection( dir ); // may also be a filename
+    dlg.exec();
+
+    QString filename = dlg.selectedFile();
+    if (!filename.isEmpty())
+        KRecentDocument::add(filename);
+
+    ExtendedResult res;
+    res.fileNames<<filename;
+    res.encoding=dlg.selectedEncoding();
+    return res;
+}
+
+
+KFileDialog::ExtendedResult  KFileDialog::getSaveURLAndEncoding(const QString& encoding,
+			     const QString& dir, const  QString& filter,
+                             QWidget *parent, const QString& caption)
+{
+    bool specialDir = dir.at(0) == ':';
+    KFileDialog dlg(specialDir?dir:QString::null, encoding,caption.isNull() ? i18n("Save As") : caption,
+        Saving,parent, "filedialog", true);
+    dlg.setFilter(filter);
+
+    if ( !specialDir )
+    dlg.setSelection( dir ); // may also be a filename
+
+    dlg.exec();
+
+    KURL url = dlg.selectedURL();
+    if (url.isValid())
+        KRecentDocument::add( url );
+
+    ExtendedResult res;
+    res.URLs<<url;
+    res.encoding=dlg.selectedEncoding();
+    return res;
+}
+
+
+
+
+
+
+
+
+
+
+
 #include "kfiledialog.moc"
Index: kfiledialog.h
===================================================================
RCS file: /home/kde/kdelibs/kio/kfile/kfiledialog.h,v
retrieving revision 1.150
diff -u -3 -p -r1.150 kfiledialog.h
--- kfiledialog.h	23 Sep 2003 06:06:07 -0000	1.150
+++ kfiledialog.h	25 Sep 2003 12:48:40 -0000
@@ -80,6 +80,12 @@ class KFileDialog : public KDialogBase
     Q_OBJECT
 
 public:
+    class ExtendedResult {
+	public:
+		QStringList fileNames;
+		KURL::List  URLs;
+		QString encoding;
+    };
     /**
      * Defines some default behavior of the filedialog.
      * E.g. in mode @p Opening and @p Saving, the selected files/urls will
@@ -414,6 +420,40 @@ public:
 
     /**
      * Creates a modal file dialog and return the selected
+     * filename or an empty string if none was chosen additionally a chosen
+     * encoding value is returned.
+     *
+     * Note that with
+     * this method the user must select an existing filename.
+     *
+     * @param startDir This can either be
+     *         @li The URL of the directory to start in.
+     *         @li QString::null to start in the current working
+     *		    directory, or the last directory where a file has been
+     *		    selected.
+     *         @li ':<keyword>' to start in the directory last used
+     *             by a filedialog in the same application that specified
+     *             the same keyword.
+     *         @li '::<keyword>' to start in the directory last used
+     *             by a filedialog in any application that specified the
+     *             same keyword.
+     * @param filter This is a space separated list of shell globs.
+     * You can set the text to be displayed for the glob, and
+     * provide multiple globs.  See setFilter() for details on
+     * how to do this...
+     * @param parent The widget the dialog will be centered on initially.
+     * @param caption The name of the dialog widget.
+     */
+    static ExtendedResult getOpenFileNameAndEncoding(const QString& encoding=QString::null,
+   				   const QString& startDir= QString::null,
+				   const QString& filter= QString::null,
+				   QWidget *parent= 0,
+				   const QString& caption = QString::null);
+
+
+
+    /**
+     * Creates a modal file dialog and return the selected
      * filename or an empty string if none was chosen.
      *
      * Note that with
@@ -443,6 +483,7 @@ public:
 				   const QString& caption = QString::null);
 
 
+
     /**
      * Creates a modal file dialog and returns the selected
      * filenames or an empty list if none was chosen.
@@ -473,6 +514,39 @@ public:
 					QWidget *parent = 0,
 					const QString& caption= QString::null);
 
+
+
+    /**
+     * Creates a modal file dialog and returns the selected encoding and the selected
+     * filenames or an empty list if none was chosen.
+     *
+     * Note that with
+     * this method the user must select an existing filename.
+     *
+     * @param startDir This can either be
+     *         @li The URL of the directory to start in.
+     *         @li QString::null to start in the current working
+     *		    directory, or the last directory where a file has been
+     *		    selected.
+     *         @li ':<keyword>' to start in the directory last used
+     *             by a filedialog in the same application that specified
+     *             the same keyword.
+     *         @li '::<keyword>' to start in the directory last used
+     *             by a filedialog in any application that specified the
+     *             same keyword.
+     * @param filter This is a space separated list of shell globs.
+     * You can set the text to be displayed for the glob, and
+     * provide multiple globs.  See setFilter() for details on
+     * how to do this...
+     * @param parent The widget the dialog will be centered on initially.
+     * @param caption The name of the dialog widget.
+     */
+    static ExtendedResult getOpenFileNamesAndEncoding(const QString& encoding=QString::null,
+					const QString& startDir= QString::null,
+					const QString& filter= QString::null,
+					QWidget *parent = 0,
+					const QString& caption= QString::null);
+
     /**
      * Creates a modal file dialog and returns the selected
      * URL or an empty string if none was chosen.
@@ -504,6 +578,41 @@ public:
 			   const QString& caption = QString::null);
 
 
+
+
+    /**
+     * Creates a modal file dialog and returns the selected
+     * URL or an empty string if none was chosen.
+     *
+     * Note that with
+     * this method the user must select an existing URL.
+     *
+     * @param startDir This can either be
+     *         @li The URL of the directory to start in.
+     *         @li QString::null to start in the current working
+     *		    directory, or the last directory where a file has been
+     *		    selected.
+     *         @li ':<keyword>' to start in the directory last used
+     *             by a filedialog in the same application that specified
+     *             the same keyword.
+     *         @li '::<keyword>' to start in the directory last used
+     *             by a filedialog in any application that specified the
+     *             same keyword.
+     * @param filter This is a space separated list of shell globs.
+     * You can set the text to be displayed for the glob, and
+     * provide multiple globs.  See setFilter() for details on
+     * how to do this...
+     * @param parent The widget the dialog will be centered on initially.
+     * @param caption The name of the dialog widget.
+     */
+    static ExtendedResult getOpenURLAndEncoding(const QString& encoding=QString::null,
+			   const QString& startDir = QString::null,
+			   const QString& filter= QString::null,
+			   QWidget *parent= 0,
+			   const QString& caption = QString::null);
+
+
+
     /**
      * Creates a modal file dialog and returns the selected
      * URLs or an empty list if none was chosen.
@@ -534,6 +643,41 @@ public:
 				  QWidget *parent = 0,
 				  const QString& caption= QString::null);
 
+
+
+    /**
+     * Creates a modal file dialog and returns the selected
+     * URLs or an empty list if none was chosen.
+     *
+     * Note that with
+     * this method the user must select an existing filename.
+     *
+     * @param startDir This can either be
+     *         @li The URL of the directory to start in.
+     *         @li QString::null to start in the current working
+     *		    directory, or the last directory where a file has been
+     *		    selected.
+     *         @li ':<keyword>' to start in the directory last used
+     *             by a filedialog in the same application that specified
+     *             the same keyword.
+     *         @li '::<keyword>' to start in the directory last used
+     *             by a filedialog in any application that specified the
+     *             same keyword.
+     * @param filter This is a space separated list of shell globs.
+     * You can set the text to be displayed for the glob, and
+     * provide multiple globs.  See setFilter() for details on
+     * how to do this...
+     * @param parent The widget the dialog will be centered on initially.
+     * @param caption The name of the dialog widget.
+     */
+    static ExtendedResult getOpenURLsAndEncoding(const QString& encoding=QString::null,
+				  const QString& startDir= QString::null,
+				  const QString& filter= QString::null,
+				  QWidget *parent = 0,
+				  const QString& caption= QString::null);
+
+
+
     /**
      * Creates a modal file dialog and returns the selected
      * filename or an empty string if none was chosen.
@@ -566,6 +710,40 @@ public:
 				   QWidget *parent= 0,
 				   const QString& caption = QString::null);
 
+
+    /**
+     * Creates a modal file dialog and returns the selected
+     * filename or an empty string if none was chosen.
+     *
+     * Note that with this
+     * method the user need not select an existing filename.
+     *
+     * @param startDir This can either be
+     *         @li The URL of the directory to start in.
+     *         @li a relative path or a filename determining the
+     *             directory to start in and the file to be selected.
+     *         @li QString::null to start in the current working
+     *		    directory, or the last directory where a file has been
+     *		    selected.
+     *         @li ':<keyword>' to start in the directory last used
+     *             by a filedialog in the same application that specified
+     *             the same keyword.
+     *         @li '::<keyword>' to start in the directory last used
+     *             by a filedialog in any application that specified the
+     *             same keyword.
+     * @param filter This is a space separated list of shell globs.
+     * You can set the text to be displayed for the glob, and
+     * provide multiple globs.  See setFilter() for details on
+     * how to do this...
+     * @param parent The widget the dialog will be centered on initially.
+     * @param caption The name of the dialog widget.
+     */
+    static ExtendedResult getSaveFileNameAndEncoding(const QString& encoding=QString::null,
+				   const QString& startDir=QString::null,
+				   const QString& filter= QString::null,
+				   QWidget *parent= 0,
+				   const QString& caption = QString::null);
+
     /**
      * Creates a modal file dialog and returns the selected
      * filename or an empty string if none was chosen.
@@ -597,6 +775,42 @@ public:
 			   const QString& filter= QString::null,
 			   QWidget *parent= 0,
 			   const QString& caption = QString::null);
+
+
+    /**
+     * Creates a modal file dialog and returns the selected
+     * filename or an empty string if none was chosen.
+     *
+     * Note that with this
+     * method the user need not select an existing filename.
+     *
+     * @param startDir This can either be
+     *         @li The URL of the directory to start in.
+     *         @li a relative path or a filename determining the
+     *             directory to start in and the file to be selected.
+     *         @li QString::null to start in the current working
+     *		    directory, or the last directory where a file has been
+     *		    selected.
+     *         @li ':<keyword>' to start in the directory last used
+     *             by a filedialog in the same application that specified
+     *             the same keyword.
+     *         @li '::<keyword>' to start in the directory last used
+     *             by a filedialog in any application that specified the
+     *             same keyword.
+     * @param filter This is a space separated list of shell globs.
+     * You can set the text to be displayed for the glob, and
+     * provide multiple globs.  See setFilter() for details on
+     * how to do this...
+     * @param parent The widget the dialog will be centered on initially.
+     * @param caption The name of the dialog widget.
+     */
+    static ExtendedResult getSaveURLAndEncoding(const QString& encoding=QString::null,
+			   const QString& startDir= QString::null,
+			   const QString& filter= QString::null,
+			   QWidget *parent= 0,
+			   const QString& caption = QString::null);
+
+
     /**
      * Creates a modal file dialog and returns the selected
      * directory or an empty string if none was chosen.
@@ -784,7 +998,6 @@ public:
      * Used by KDirSelectDialog to share the dialog's start directory.
      */
     static void setStartDir( const KURL& directory );
-
 
 signals:
     /**


More information about the kde-core-devel mailing list