2
* fileutil.h - common file dialogs
3
* Copyright (C) 2008 Michail Pishchagin
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
#include <QFileDialog>
26
#include <QMessageBox>
28
#include "psioptions.h"
30
static QString lastUsedOpenPathOptionPath = "options.ui.last-used-open-path";
31
static QString lastUsedSavePathOptionPath = "options.ui.last-used-save-path";
33
QString FileUtil::lastUsedOpenPath()
35
return PsiOptions::instance()->getOption(lastUsedOpenPathOptionPath).toString();
38
void FileUtil::setLastUsedOpenPath(const QString& path)
42
PsiOptions::instance()->setOption(lastUsedOpenPathOptionPath, path);
46
QString FileUtil::lastUsedSavePath()
48
return PsiOptions::instance()->getOption(lastUsedSavePathOptionPath).toString();
51
void FileUtil::setLastUsedSavePath(const QString& path)
55
PsiOptions::instance()->setOption(lastUsedSavePathOptionPath, path);
59
QString FileUtil::getOpenFileName(QWidget* parent, const QString& caption, const QString& filter, QString* selectedFilter)
62
if (lastUsedOpenPath().isEmpty()) {
63
setLastUsedOpenPath(QDir::homeDirPath());
65
QString fileName = QFileDialog::getOpenFileName(parent, caption, lastUsedOpenPath(), filter, selectedFilter);
66
if (!fileName.isEmpty()) {
67
QFileInfo fi(fileName);
69
QMessageBox::information(parent, tr("Error"), tr("The file specified does not exist."));
73
setLastUsedOpenPath(fi.dirPath());
82
QString FileUtil::getSaveFileName(QWidget* parent, const QString& caption, const QString& defaultFileName, const QString& filter, QString* selectedFilter)
84
if (lastUsedSavePath().isEmpty()) {
85
if (!lastUsedOpenPath().isEmpty()) {
86
setLastUsedSavePath(lastUsedOpenPath());
89
setLastUsedSavePath(QDir::homeDirPath());
93
QString dir = QDir(lastUsedSavePath()).filePath(defaultFileName);
94
QString fileName = QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter);
95
if (!fileName.isEmpty()) {
96
QFileInfo fi(fileName);
97
if (QDir(fi.dirPath()).exists()) {
98
setLastUsedSavePath(fi.dirPath());
106
QString FileUtil::getImageFileName(QWidget* parent)
108
return FileUtil::getOpenFileName(parent, tr("Choose a file"),
109
tr("Images (*.png *.xpm *.jpg *.PNG *.XPM *.JPG)"));