casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
QtFileDialog.qo.h
Go to the documentation of this file.
00001 //# QtFileDialog.qo.h: Subclass of QFileDialog with additional functionality.
00002 //# Copyright (C) 2009
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef QTFILEDIALOG_QO_H_
00028 #define QTFILEDIALOG_QO_H_
00029 
00030 #include <casa/BasicSL/String.h>
00031 
00032 #include <QFileDialog>
00033 #include <QLabel>
00034 #include <QTimer>
00035 
00036 #include <casa/namespace.h>
00037 using namespace std;
00038 
00039 namespace casa {
00040 
00041 // Subclass of QFileDialog with additional functionality.
00042 // IMPORTANT: This class needs to be checked when the Qt version changes!
00043 // Specifically:
00044 // 1) The label at the bottom assumes that the layout is a QGridLayout, where
00045 //    the first column is for a label and the rest can be used for a widget.
00046 // 2) As of Qt 4.3.4, the QFileDialog::filesSelected() signal is NOT emitted
00047 //    whenever the selection changes, but only when the selection is finalized.
00048 //    Because we're using a label to keep track of the current selection, this
00049 //    class uses a timer to update the label at a set time to the current
00050 //    selection.  This is slightly inefficient, but there's no better way to do
00051 //    it without making our own file chooser dialog.
00052 class QtFileDialog : public QFileDialog {
00053     Q_OBJECT
00054     
00055 public:
00056     // Static //
00057     
00058     // Returns an existing directory using the given optional parent, caption,
00059     // and starting directory parameters.  See QFileDialog.
00060     // <group>
00061     static QString qgetExistingDir(QWidget* parent = NULL,
00062             const QString& caption = QString(),
00063             const QString& directory = lastDirectory(),
00064             int histLimit = historyLimit()) {
00065         return qgetHelper(AcceptOpen, DirectoryOnly, parent, caption,
00066                           directory, QString(), histLimit); }
00067     static String getExistingDir(QWidget* parent = NULL,
00068             const QString& caption = QString(),
00069             const QString& directory = lastDirectory(),
00070             int histLimit = historyLimit()) {
00071         return qgetExistingDir(parent, caption, directory,
00072                                histLimit).toStdString(); }
00073     // </group>
00074     
00075     // Returns an existing file using the given optional parent, caption,
00076     // starting directory, and filter parameters.  See QFileDialog.
00077     // <group>
00078     static QString qgetExistingFile(QWidget* parent = NULL,
00079             const QString& caption = QString(),
00080             const QString& directory = lastDirectory(),
00081             const QString& filter = QString(), int histLimit = historyLimit()){
00082         (void)filter;
00083         return qgetHelper(AcceptOpen, ExistingFile, parent, caption, directory,
00084                           QString(), histLimit); }
00085     static String getExistingFile(QWidget* parent = NULL,
00086             const QString& caption = QString(),
00087             const QString& directory = lastDirectory(),
00088             const QString& filter = QString(), int histLimit = historyLimit()){
00089         return qgetExistingFile(parent, caption, directory, filter,
00090                                 histLimit).toStdString(); }
00091     // </group>
00092     
00093     // Returns a new filename using the given optional parent, caption,
00094     // starting directory, and filter parameters.  See QFileDialog.
00095     // <group>
00096     static QString qgetAnyFile(
00097         QWidget* parent = NULL, const QString& caption = QString(),
00098         const QString& directory = lastDirectory(),
00099         const QString& /*filter*/ = QString(), int histLimit = historyLimit()){
00100         return qgetHelper(AcceptSave, AnyFile, parent, caption, directory,
00101                           QString(), histLimit); }
00102     static String getAnyFile(QWidget* parent = NULL,
00103             const QString& caption = QString(),
00104             const QString& directory = lastDirectory(),
00105             const QString& filter = QString(), int histLimit = historyLimit()){
00106         return qgetAnyFile(parent, caption, directory, filter,
00107                            histLimit).toStdString(); }
00108     // </group>
00109     
00110     // Gets/Sets the last directory to be used by a QtFileDialog.  Blank means
00111     // the current directory, which is the default.
00112     // <group>
00113     static const QString& lastDirectory();
00114     static void setNextDirectory(const QString& directory);
00115     // </group>
00116     
00117     // Gets/Sets the limit for the history size used by a QtFileDialog.  -1
00118     // means it is not managed, which is the default.
00119     // <group>
00120     static int historyLimit();
00121     static void setHistoryLimit(int limit);
00122     // </group>
00123     
00124     
00125     // Non-Static //
00126     
00127     // See QFileDialog constructors.
00128     // <group>
00129     QtFileDialog(QWidget* parent, Qt::WindowFlags flags);
00130     QtFileDialog(QWidget* parent = NULL, const QString& caption = QString(),
00131             const QString& directory = lastDirectory(),
00132             const QString& filter = QString());
00133     // </group>
00134     
00135     // Destructor.
00136     ~QtFileDialog();
00137     
00138 public slots:
00139     // Overrides QFileDialog::accept().
00140     void accept();
00141     
00142 private:
00143     // Label to display the currently chosen file(s).
00144     QLabel* chosenLabel;
00145     
00146     // Timer.
00147     QTimer* timer;
00148     
00149     
00150     // To be called from the constructors.
00151     void initialize();
00152     
00153     
00154     // Static //
00155     
00156     // Last directory.
00157     static QString lastDirectory_;
00158     
00159     // Set history limit.
00160     static int historyLimit_;
00161     
00162     
00163     // Helper method for the static qget* functions.
00164     static QString qgetHelper(AcceptMode acceptMode, FileMode fileMode,
00165             QWidget* parent, const QString& caption, const QString& directory,
00166             const QString& filter, int histLimit);
00167     
00168 private slots:
00169     // Signal for timer timeout.
00170     void timeout();
00171 };
00172 
00173 }
00174 
00175 #endif /* QTFILEDIALOG_QO_H_ */