Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ofiledialog.cpp

Go to the documentation of this file.
00001 /*
00002                =.            This file is part of the OPIE Project
00003              .=l.            Copyright (C) Holger Freyther <freyther@handhelds.org>
00004            .>+-=
00005  _;:,     .>    :=|.         This library is free software; you can
00006 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00007 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00008 .="- .-=="i,     .._         License as published by the Free Software
00009  - .   .-<_>     .<>         Foundation; version 2 of the License.
00010      ._= =}       :
00011     .%`+i>       _;_.
00012     .i_,=:_.      -<s.       This library is distributed in the hope that
00013      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00014     : ..    .:,     . . .    without even the implied warranty of
00015     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00016   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00017 ..}^=.=       =       ;      Library General Public License for more
00018 ++=   -.     .`     .:       details.
00019  :     =  ...= . :.=-
00020  -.   .:....=;==+<;          You should have received a copy of the GNU
00021   -_. . .   )=.  =           Library General Public License along with
00022     --        :-=`           this library; see the file COPYING.LIB.
00023                              If not, write to the Free Software Foundation,
00024                              Inc., 59 Temple Place - Suite 330,
00025                              Boston, MA 02111-1307, USA.
00026 
00027 */
00028 
00029 
00030 /* OPIE */
00031 #include <opie2/ofiledialog.h>
00032 #include <qpe/applnk.h>
00033 #include <qpe/config.h>
00034 #include <qpe/qpeapplication.h>
00035 
00036 /* QT */
00037 #include <qfileinfo.h>
00038 #include <qstring.h>
00039 #include <qapplication.h>
00040 #include <qlayout.h>
00041 
00042 
00043 /* TRANSLATOR Opie::Ui::OFileDialog */
00044 
00045 using namespace Opie::Ui;
00046 
00047 namespace
00048 {
00049 /*
00050  * helper functions to load the start dir
00051  * and to save it
00052  * helper to extract the dir out of a file name
00053  */
00058 QString lastUsedDir( const QString& key )
00059 {
00060     if ( qApp->argc() < 1 )
00061         return QString::null;
00062 
00063     Config cfg( QFileInfo(qApp->argv()[0]).fileName() ); // appname
00064     cfg.setGroup( key );
00065     return cfg.readEntry("LastDir", QPEApplication::documentDir() );
00066 }
00067 
00068 void saveLastDir( const QString& key, const QString& file )
00069 {
00070     if ( qApp->argc() < 1 )
00071         return;
00072 
00073     Config cfg(  QFileInfo(qApp->argv()[0]).fileName() );
00074     cfg.setGroup( key );
00075     QFileInfo inf( file );
00076     if ( inf.isFile() )
00077         cfg.writeEntry("LastDir", inf.dirPath( true ) );
00078     else
00079         cfg.writeEntry("LastDir", file );
00080 }
00081 };
00082 
00094 OFileDialog::OFileDialog(const QString &caption,
00095                          QWidget *wid, int mode, int selector,
00096                          const QString &dirName,
00097                          const QString &fileName,
00098                          const QMap<QString,QStringList>& mimetypes )
00099         : QDialog( wid, "OFileDialog", true )
00100 {
00101     QVBoxLayout *lay = new QVBoxLayout(this );
00102     file = new OFileSelector(this , mode, selector,
00103                              dirName, fileName,
00104                              mimetypes );
00105     lay->addWidget( file );
00106 
00107     setCaption( caption.isEmpty() ? tr("FileDialog") : caption );
00108     connect(file, SIGNAL(fileSelected(const QString&) ),
00109             this, SLOT(slotFileSelected(const QString&) ) );
00110     connect(file, SIGNAL(ok() ),
00111             this, SLOT(slotSelectorOk()) ) ;
00112 
00113     connect(file, SIGNAL(dirSelected(const QString&) ), this, SLOT(slotDirSelected(const QString&) ) );
00114 }
00119 QString OFileDialog::mimetype()const
00120 {
00121     return QString::null;
00122 }
00123 
00127 QString OFileDialog::fileName()const
00128 {
00129     return file->selectedName();
00130 }
00131 
00135 DocLnk OFileDialog::selectedDocument()const
00136 {
00137     return file->selectedDocument();
00138 }
00139 
00151 QString OFileDialog::getOpenFileName(int selector,
00152                                      const QString &_startDir,
00153                                      const QString &file,
00154                                      const MimeTypes &mimes,
00155                                      QWidget *wid,
00156                                      const QString &caption )
00157 {
00158     QString ret;
00159     QString startDir = _startDir;
00160     if (startDir.isEmpty() )
00161         startDir = lastUsedDir( "FileDialog-OPEN" );
00162 
00163 
00164     OFileDialog dlg( caption.isEmpty() ? tr("Open") : caption,
00165                      wid, OFileSelector::Open, selector, startDir, file, mimes);
00166     if( QPEApplication::execDialog(&dlg ) )
00167     {
00168         ret = dlg.fileName();
00169         saveLastDir( "FileDialog-OPEN", ret );
00170     }
00171 
00172     return ret;
00173 }
00174 
00179 QString OFileDialog::getSaveFileName(int selector,
00180                                      const QString &_startDir,
00181                                      const QString &file,
00182                                      const MimeTypes &mimes,
00183                                      QWidget *wid,
00184                                      const QString &caption )
00185 {
00186     QString ret;
00187     QString startDir = _startDir;
00188     if (startDir.isEmpty() )
00189         startDir = lastUsedDir( "FileDialog-SAVE" );
00190 
00191     OFileDialog dlg( caption.isEmpty() ? tr("Save") : caption,
00192                      wid, OFileSelector::Save, selector, startDir, file, mimes);
00193 
00194     /*
00195      * For the save mode we do not want a file to be written
00196      * if the user just clicked on it
00197      * #1522
00198      */
00199     dlg.file->disconnect( &dlg );
00200 
00201     if( QPEApplication::execDialog(&dlg) )
00202     {
00203         ret = dlg.fileName();
00204         saveLastDir( "FileDialog-SAVE", ret );
00205     }
00206 
00207     return ret;
00208 }
00209 
00219 QString OFileDialog::getDirectory(int selector,
00220                                  const QString &_startDir,
00221                                  QWidget *wid,
00222                                  const QString &caption )
00223 {
00224     QString ret;
00225     QString startDir = _startDir;
00226     if ( startDir.isEmpty() )
00227         startDir = lastUsedDir( "FileDialog-SELECTDIR" );
00228 
00229     OFileDialog dlg( caption.isEmpty() ? tr( "Select Directory" ) : caption,
00230                      wid, OFileSelector::DirectorySelector, selector, startDir );
00231 
00232     if ( QPEApplication::execDialog(&dlg) )
00233     {
00234         ret = dlg.fileName();
00235         saveLastDir( "FileDialog-SELECTDIR", ret );
00236     }
00237     return ret;
00238 }
00239 
00240 void OFileDialog::slotFileSelected(const QString & )
00241 {
00242     accept();
00243 }
00244 
00245 void OFileDialog::slotSelectorOk( )
00246 {
00247     accept();
00248 }
00249 
00250 void OFileDialog::slotDirSelected(const QString &dir )
00251 {
00252     setCaption( dir );
00253 }

Generated on Sat Nov 5 16:16:24 2005 for OPIE by  doxygen 1.4.2