1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include "dirdialog.h"
#include <QDir>
#include <QFileDialog>
static QString lastPath;
dirDialog::dirDialog(QWidget * parent,QString title)
{
parentPtr=parent;
dialogTitle=title;
}
dirDialog::~dirDialog()
{
}
/*!
\fn dirDialog::openFileName(const QString &path, const QString &filter)
\brief selection of a file
\param path directory to open (preselected) if empty, the last accessed directory will be used
\param filter types to select from (e.g. mydirs*)
\return if canceled or no selection then returns an empty string else return string containing absolute filename
*/
QString dirDialog::openFileName(const QString &path, const QString &filter)
{
QString fn;
if (path.isEmpty() && lastPath.isEmpty())
{
lastPath=QDir::homePath();
}
else if (!path.isEmpty())
{
lastPath=path;
}
fn=QFileDialog::getOpenFileName(parentPtr,dialogTitle,lastPath,filter);
if(!fn.isEmpty())
{
QFileInfo fi(fn);
lastPath=fi.absolutePath();
}
return fn;
}
/*!
\fn dirDialog::openDirName(const QString &path)
\brief selection of a directory
\param path directory to open (preselected)
\return if canceled or no selection then return an empty string else return string containing absolute dirname
*/
QString dirDialog::openDirName(const QString &path)
{
QString fn;
if ((path.isEmpty()) && lastPath.isEmpty())
{
lastPath=QDir::homePath();
}
else if (!path.isEmpty())
{
lastPath=path;
}
fn=QFileDialog::getExistingDirectory(parentPtr,dialogTitle,lastPath);
if(!fn.isEmpty())
{
lastPath=fn;
}
return fn;
}
/*!
\fn dirDialog::saveFileName(const QString &path, const QString &filter,QString extension)
\brief Save a file to disk
Saves a file to disk. A dialogbox is opened with \a startWith directory (or /dir/subdir/..../filename) preselected
\param path directory to open (can include filename to preselect)
\param filter file types to select from (e.g. *.txt *.doc)
\param extension if extension is not empty or NULL, thenn this string will be appended to the filename. A dot will automatically be insterted (i.e specify "txt" not ".txt").
\return if canceled or no selection then return an empty string else return string containing absolute filename.
*/
QString dirDialog::saveFileName(const QString &path, const QString &filter, QString extension)
{
QString fn;
if ((path.isEmpty()) && lastPath.isEmpty())
{
lastPath=QDir::currentPath();
}
else if (!path.isEmpty())
{
lastPath=path;
}
QString exten(extension);
fn=QFileDialog::getSaveFileName(parentPtr,dialogTitle,lastPath,filter);
if(fn.isEmpty()) return fn;
QFileInfo fi(fn);
if(!exten.isEmpty())
{
if(fi.suffix()=="")
{
fi.setFile(fi.absoluteFilePath()+"."+exten);
}
}
lastPath=fi.absolutePath();
return fi.absoluteFilePath();
}
|