00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2007 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef FILEBROWSER_H 00011 #define FILEBROWSER_H 00012 00013 #include <QtGui/QLineEdit> 00014 #include <QtGui/QPushButton> 00015 #include <QtGui/QWidget> 00016 00017 class Filename; 00018 00019 /** 00020 * A widget to allow the user to browse for files. 00021 * 00022 * The FileBrowser extends the QWidget class to provide a single QWidget 00023 * capable of performing typical file-browsing tasks. This class associates an 00024 * editable text-entry field with a button which brings up a file selection 00025 * dialog by calling either the QFileDialog::getOpenFileName() method or the 00026 * QFileDialog::getSaveFileName() method. Selecting a file through the file 00027 * selection dialog will populate the text field. 00028 */ 00029 class FileBrowser : public QWidget 00030 { 00031 Q_OBJECT 00032 00033 public: 00034 /** 00035 * Creates a FileBrowser with no file selected. 00036 * 00037 * @param pParent 00038 * The parent widget. 00039 */ 00040 FileBrowser(QWidget* pParent = NULL); 00041 00042 /** 00043 * Destroys the widget and all child widget items. 00044 */ 00045 ~FileBrowser(); 00046 00047 /** 00048 * Sets the name of the file. 00049 * 00050 * This method sets the name of the file to be displayed in the FileBrowser. 00051 * 00052 * @param filename 00053 * The name of the file. 00054 */ 00055 void setFilename(const QString& filename); 00056 00057 /** 00058 * Sets the name of the file. 00059 * 00060 * This method sets the name of the file to be displayed in the FileBrowser. 00061 * 00062 * @param filename 00063 * The name of the file. 00064 */ 00065 void setFilename(const Filename& filename); 00066 00067 /** 00068 * Gets the name of the file. 00069 * 00070 * This method gets the name of the file displayed in the FileBrowser. 00071 * 00072 * @return The name of the file. 00073 */ 00074 QString getFilename() const; 00075 00076 /** 00077 * Sets the browse caption. 00078 * 00079 * This method sets the browse caption of the FileBrowser. For more information on browse captions, 00080 * please refer to the QFileDialog::getOpenFileName documentation. 00081 * 00082 * @param caption 00083 * The browse caption. 00084 */ 00085 void setBrowseCaption(const QString& caption); 00086 00087 /** 00088 * Gets the browse caption. 00089 * 00090 * This method gets the browse caption of the FileBrowser. For more information on browse captions, 00091 * please refer to the QFileDialog::getOpenFileName documentation. 00092 * 00093 * @return The browse caption. 00094 * 00095 */ 00096 QString getBrowseCaption() const; 00097 00098 /** 00099 * Sets the browse directory. 00100 * 00101 * This method sets the browse directory of the FileBrowser. For more information on browse directories, 00102 * please refer to the QFileDialog::getOpenFileName documentation. 00103 * 00104 * @param directory 00105 * The browse directory. 00106 */ 00107 void setBrowseDirectory(const QString& directory); 00108 00109 /** 00110 * Gets the browse directory. 00111 * 00112 * This method gets the browse directory of the FileBrowser. For more information on browse directories, 00113 * please refer to the QFileDialog::getOpenFileName documentation. 00114 * 00115 * @return The browse directory. 00116 */ 00117 QString getBrowseDirectory() const; 00118 00119 /** 00120 * Sets the browse file filters. 00121 * 00122 * This method sets the browse file filters of the FileBrowser. For more information on browse file filters, 00123 * please refer to the QFileDialog::getOpenFileName documentation. 00124 * 00125 * @param filters 00126 * The browse file filters. 00127 */ 00128 void setBrowseFileFilters(const QString& filters); 00129 00130 /** 00131 * Gets the browse file filters. 00132 * 00133 * This method gets the browse file filters of the FileBrowser. For more information on browse file filters, 00134 * please refer to the QFileDialog::getOpenFileName documentation. 00135 * 00136 * @return The browse file filters. 00137 */ 00138 QString getBrowseFileFilters() const; 00139 00140 /** 00141 * Sets the file selection dialog to only browse for existing files. 00142 * 00143 * By default if this method is not called, isBrowseExistingFile() returns 00144 * \c true. 00145 * 00146 * @param bExistingFile 00147 * Set this value to \c true to browse only for existing files. 00148 * Set the value to \c false to allow non-existent files to be 00149 * selected. 00150 * 00151 * @see QFileDialog::getOpenFileName(), QFileDialog::getSaveFileName() 00152 */ 00153 void setBrowseExistingFile(bool bExistingFile); 00154 00155 /** 00156 * Queries whether the file selection dialog only browses for existing 00157 * files. 00158 * 00159 * @return Returns \c true if the file selection dialog only browses for 00160 * existing files. Returns \c false if non-existent files can be 00161 * selected. Returns \c true by default if setBrowseExistingFile() 00162 * has not been called on this instance of the file browser. 00163 * 00164 * @see QFileDialog::getOpenFileName(), QFileDialog::getSaveFileName() 00165 */ 00166 bool isBrowseExistingFile() const; 00167 00168 signals: 00169 /** 00170 * This signal is emitted when the filename is changed. 00171 */ 00172 void filenameChanged(const QString& filename); 00173 00174 protected: 00175 /** 00176 * Sends a focus out event if needed based on the current editing status. 00177 * 00178 * @param pObject 00179 * The object prompting the event. 00180 * @param pEvent 00181 * The event invoked by the object. 00182 * 00183 * @return Returns the value returned by the default QWidget 00184 * implementation. 00185 */ 00186 bool eventFilter(QObject* pObject, QEvent* pEvent); 00187 00188 protected slots: 00189 /** 00190 * Calls QFileDialog::getOpenFileName to allow the user to specify a file. 00191 * If no browse directory has been set, then the current file's directory will be used as the initial directory. 00192 */ 00193 void browse(); 00194 00195 private: 00196 FileBrowser(const FileBrowser& rhs); 00197 FileBrowser& operator=(const FileBrowser& rhs); 00198 QLineEdit* mpFileEdit; 00199 QPushButton* mpBrowseButton; 00200 QString mBrowseCaption; 00201 QString mBrowseDirectory; 00202 QString mBrowseFilters; 00203 bool mExistingFile; 00204 }; 00205 00206 #endif