/src/gui/dialogs/qfiledialog.cpp
C++ | 3562 lines | 2192 code | 318 blank | 1052 comment | 472 complexity | af8e6538cc6c39d348fdce609ec09158 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-3.0, BSD-3-Clause, CC0-1.0, CC-BY-SA-4.0, LGPL-2.1, GPL-3.0, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- /****************************************************************************
- **
- ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
- ** All rights reserved.
- ** Contact: Nokia Corporation (qt-info@nokia.com)
- **
- ** This file is part of the QtGui module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:LGPL$
- ** GNU Lesser General Public License Usage
- ** This file may be used under the terms of the GNU Lesser General Public
- ** License version 2.1 as published by the Free Software Foundation and
- ** appearing in the file LICENSE.LGPL included in the packaging of this
- ** file. Please review the following information to ensure the GNU Lesser
- ** General Public License version 2.1 requirements will be met:
- ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- **
- ** In addition, as a special exception, Nokia gives you certain additional
- ** rights. These rights are described in the Nokia Qt LGPL Exception
- ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU General
- ** Public License version 3.0 as published by the Free Software Foundation
- ** and appearing in the file LICENSE.GPL included in the packaging of this
- ** file. Please review the following information to ensure the GNU General
- ** Public License version 3.0 requirements will be met:
- ** http://www.gnu.org/copyleft/gpl.html.
- **
- ** Other Usage
- ** Alternatively, this file may be used in accordance with the terms and
- ** conditions contained in a signed written agreement between you and Nokia.
- **
- **
- **
- **
- **
- ** $QT_END_LICENSE$
- **
- ****************************************************************************/
- #include <qvariant.h>
- #include <private/qwidgetitemdata_p.h>
- #include "qfiledialog.h"
- #ifndef QT_NO_FILEDIALOG
- #include "qfiledialog_p.h"
- #include <qfontmetrics.h>
- #include <qaction.h>
- #include <qheaderview.h>
- #include <qshortcut.h>
- #include <qgridlayout.h>
- #include <qmenu.h>
- #include <qmessagebox.h>
- #include <qinputdialog.h>
- #include <stdlib.h>
- #include <qsettings.h>
- #include <qdebug.h>
- #include <qapplication.h>
- #include <qstylepainter.h>
- #if !defined(Q_WS_WINCE) && !defined(Q_OS_SYMBIAN)
- #include "ui_qfiledialog.h"
- #else
- #define Q_EMBEDDED_SMALLSCREEN
- #include "ui_qfiledialog_embedded.h"
- #if defined(Q_OS_WINCE)
- extern bool qt_priv_ptr_valid;
- #endif
- #if defined(Q_OS_UNIX)
- #include <pwd.h>
- #endif
- #endif
- QT_BEGIN_NAMESPACE
- Q_GLOBAL_STATIC(QString, lastVisitedDir)
- /*
- \internal
- Exported hooks that can be used to customize the static functions.
- */
- typedef QString (*_qt_filedialog_existing_directory_hook)(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options);
- Q_GUI_EXPORT _qt_filedialog_existing_directory_hook qt_filedialog_existing_directory_hook = 0;
- typedef QString (*_qt_filedialog_open_filename_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);
- Q_GUI_EXPORT _qt_filedialog_open_filename_hook qt_filedialog_open_filename_hook = 0;
- typedef QStringList (*_qt_filedialog_open_filenames_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);
- Q_GUI_EXPORT _qt_filedialog_open_filenames_hook qt_filedialog_open_filenames_hook = 0;
- typedef QString (*_qt_filedialog_save_filename_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);
- Q_GUI_EXPORT _qt_filedialog_save_filename_hook qt_filedialog_save_filename_hook = 0;
- /*!
- \class QFileDialog
- \brief The QFileDialog class provides a dialog that allow users to select files or directories.
- \ingroup standard-dialogs
- The QFileDialog class enables a user to traverse the file system in
- order to select one or many files or a directory.
- The easiest way to create a QFileDialog is to use the static
- functions. On Windows, Mac OS X, KDE and GNOME, these static functions will
- call the native file dialog when possible.
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 0
- In the above example, a modal QFileDialog is created using a static
- function. The dialog initially displays the contents of the "/home/jana"
- directory, and displays files matching the patterns given in the
- string "Image Files (*.png *.jpg *.bmp)". The parent of the file dialog
- is set to \e this, and the window title is set to "Open Image".
- If you want to use multiple filters, separate each one with
- \e two semicolons. For example:
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 1
- You can create your own QFileDialog without using the static
- functions. By calling setFileMode(), you can specify what the user must
- select in the dialog:
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 2
- In the above example, the mode of the file dialog is set to
- AnyFile, meaning that the user can select any file, or even specify a
- file that doesn't exist. This mode is useful for creating a
- "Save As" file dialog. Use ExistingFile if the user must select an
- existing file, or \l Directory if only a directory may be selected.
- See the \l QFileDialog::FileMode enum for the complete list of modes.
- The fileMode property contains the mode of operation for the dialog;
- this indicates what types of objects the user is expected to select.
- Use setNameFilter() to set the dialog's file filter. For example:
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 3
- In the above example, the filter is set to \c{"Images (*.png *.xpm *.jpg)"},
- this means that only files with the extension \c png, \c xpm,
- or \c jpg will be shown in the QFileDialog. You can apply
- several filters by using setNameFilters(). Use selectNameFilter() to select
- one of the filters you've given as the file dialog's default filter.
- The file dialog has two view modes: \l{QFileDialog::}{List} and
- \l{QFileDialog::}{Detail}.
- \l{QFileDialog::}{List} presents the contents of the current directory
- as a list of file and directory names. \l{QFileDialog::}{Detail} also
- displays a list of file and directory names, but provides additional
- information alongside each name, such as the file size and modification
- date. Set the mode with setViewMode():
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 4
- The last important function you will need to use when creating your
- own file dialog is selectedFiles().
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 5
- In the above example, a modal file dialog is created and shown. If
- the user clicked OK, the file they selected is put in \c fileName.
- The dialog's working directory can be set with setDirectory().
- Each file in the current directory can be selected using
- the selectFile() function.
- The \l{dialogs/standarddialogs}{Standard Dialogs} example shows
- how to use QFileDialog as well as other built-in Qt dialogs.
- \sa QDir, QFileInfo, QFile, QPrintDialog, QColorDialog, QFontDialog, {Standard Dialogs Example},
- {Application Example}
- */
- /*!
- \enum QFileDialog::AcceptMode
- \value AcceptOpen
- \value AcceptSave
- */
- /*!
- \enum QFileDialog::ViewMode
- This enum describes the view mode of the file dialog; i.e. what
- information about each file will be displayed.
- \value Detail Displays an icon, a name, and details for each item in
- the directory.
- \value List Displays only an icon and a name for each item in the
- directory.
- \sa setViewMode()
- */
- /*!
- \enum QFileDialog::FileMode
- This enum is used to indicate what the user may select in the file
- dialog; i.e. what the dialog will return if the user clicks OK.
- \value AnyFile The name of a file, whether it exists or not.
- \value ExistingFile The name of a single existing file.
- \value Directory The name of a directory. Both files and
- directories are displayed.
- \value ExistingFiles The names of zero or more existing files.
- This value is obsolete since Qt 4.5:
- \value DirectoryOnly Use \c Directory and setOption(ShowDirsOnly, true) instead.
- \sa setFileMode()
- */
- /*!
- \enum QFileDialog::Option
- \value ShowDirsOnly Only show directories in the file dialog. By
- default both files and directories are shown. (Valid only in the
- \l Directory file mode.)
- \value DontResolveSymlinks Don't resolve symlinks in the file
- dialog. By default symlinks are resolved.
- \value DontConfirmOverwrite Don't ask for confirmation if an
- existing file is selected. By default confirmation is requested.
- \value DontUseNativeDialog Don't use the native file dialog. By
- default, the native file dialog is used unless you use a subclass
- of QFileDialog that contains the Q_OBJECT macro.
- \value ReadOnly Indicates that the model is readonly.
- \value HideNameFilterDetails Indicates if the file name filter details are
- hidden or not.
- \value DontUseSheet In previous versions of Qt, the static
- functions would create a sheet by default if the static function
- was given a parent. This is no longer supported and does nothing in Qt 4.5, The
- static functions will always be an application modal dialog. If
- you want to use sheets, use QFileDialog::open() instead.
- */
- /*!
- \enum QFileDialog::DialogLabel
- \value LookIn
- \value FileName
- \value FileType
- \value Accept
- \value Reject
- */
- /*!
- \fn void QFileDialog::filesSelected(const QStringList &selected)
- When the selection changes and the dialog is accepted, this signal is
- emitted with the (possibly empty) list of \a selected files.
- \sa currentChanged(), QDialog::Accepted
- */
- /*!
- \fn void QFileDialog::fileSelected(const QString &file)
- When the selection changes and the dialog is accepted, this signal is
- emitted with the (possibly empty) selected \a file.
- \sa currentChanged(), QDialog::Accepted
- */
- /*!
- \fn void QFileDialog::currentChanged(const QString &path)
- When the current file changes, this signal is emitted with the
- new file name as the \a path parameter.
- \sa filesSelected()
- */
- /*!
- \fn void QFileDialog::directoryEntered(const QString &directory)
- \since 4.3
- This signal is emitted when the user enters a \a directory.
- */
- /*!
- \fn void QFileDialog::filterSelected(const QString &filter)
- \since 4.3
- This signal is emitted when the user selects a \a filter.
- */
- #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
- bool Q_GUI_EXPORT qt_use_native_dialogs = true; // for the benefit of testing tools, until we have a proper API
- #endif
- QT_BEGIN_INCLUDE_NAMESPACE
- #ifdef Q_WS_WIN
- #include <qwindowsstyle.h>
- #endif
- #include <qshortcut.h>
- #ifdef Q_WS_MAC
- #include <qmacstyle_mac.h>
- #endif
- QT_END_INCLUDE_NAMESPACE
- /*!
- \fn QFileDialog::QFileDialog(QWidget *parent, Qt::WindowFlags flags)
- Constructs a file dialog with the given \a parent and widget \a flags.
- */
- QFileDialog::QFileDialog(QWidget *parent, Qt::WindowFlags f)
- : QDialog(*new QFileDialogPrivate, parent, f)
- {
- Q_D(QFileDialog);
- d->init();
- d->lineEdit()->selectAll();
- }
- /*!
- Constructs a file dialog with the given \a parent and \a caption that
- initially displays the contents of the specified \a directory.
- The contents of the directory are filtered before being shown in the
- dialog, using a semicolon-separated list of filters specified by
- \a filter.
- */
- QFileDialog::QFileDialog(QWidget *parent,
- const QString &caption,
- const QString &directory,
- const QString &filter)
- : QDialog(*new QFileDialogPrivate, parent, 0)
- {
- Q_D(QFileDialog);
- d->init(directory, filter, caption);
- d->lineEdit()->selectAll();
- }
- /*!
- \internal
- */
- QFileDialog::QFileDialog(const QFileDialogArgs &args)
- : QDialog(*new QFileDialogPrivate, args.parent, 0)
- {
- Q_D(QFileDialog);
- d->init(args.directory, args.filter, args.caption);
- setFileMode(args.mode);
- setOptions(args.options);
- selectFile(args.selection);
- d->lineEdit()->selectAll();
- }
- /*!
- Destroys the file dialog.
- */
- QFileDialog::~QFileDialog()
- {
- Q_D(QFileDialog);
- #ifndef QT_NO_SETTINGS
- QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
- settings.beginGroup(QLatin1String("Qt"));
- settings.setValue(QLatin1String("filedialog"), saveState());
- #endif
- d->deleteNativeDialog_sys();
- }
- /*!
- \since 4.3
- Sets the \a urls that are located in the sidebar.
- For instance:
- \snippet doc/src/snippets/filedialogurls.cpp 0
- The file dialog will then look like this:
- \image filedialogurls.png
- \sa sidebarUrls()
- */
- void QFileDialog::setSidebarUrls(const QList<QUrl> &urls)
- {
- Q_D(QFileDialog);
- d->qFileDialogUi->sidebar->setUrls(urls);
- }
- /*!
- \since 4.3
- Returns a list of urls that are currently in the sidebar
- */
- QList<QUrl> QFileDialog::sidebarUrls() const
- {
- Q_D(const QFileDialog);
- return d->qFileDialogUi->sidebar->urls();
- }
- static const qint32 QFileDialogMagic = 0xbe;
- const char *qt_file_dialog_filter_reg_exp =
- "^(.*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$";
- /*!
- \since 4.3
- Saves the state of the dialog's layout, history and current directory.
- Typically this is used in conjunction with QSettings to remember the size
- for a future session. A version number is stored as part of the data.
- */
- QByteArray QFileDialog::saveState() const
- {
- Q_D(const QFileDialog);
- int version = 3;
- QByteArray data;
- QDataStream stream(&data, QIODevice::WriteOnly);
- stream << qint32(QFileDialogMagic);
- stream << qint32(version);
- stream << d->qFileDialogUi->splitter->saveState();
- stream << d->qFileDialogUi->sidebar->urls();
- stream << history();
- stream << *lastVisitedDir();
- stream << d->qFileDialogUi->treeView->header()->saveState();
- stream << qint32(viewMode());
- return data;
- }
- /*!
- \since 4.3
- Restores the dialogs's layout, history and current directory to the \a state specified.
- Typically this is used in conjunction with QSettings to restore the size
- from a past session.
- Returns false if there are errors
- */
- bool QFileDialog::restoreState(const QByteArray &state)
- {
- Q_D(QFileDialog);
- int version = 3;
- QByteArray sd = state;
- QDataStream stream(&sd, QIODevice::ReadOnly);
- if (stream.atEnd())
- return false;
- QByteArray splitterState;
- QByteArray headerData;
- QList<QUrl> bookmarks;
- QStringList history;
- QString currentDirectory;
- qint32 marker;
- qint32 v;
- qint32 viewMode;
- stream >> marker;
- stream >> v;
- if (marker != QFileDialogMagic || v != version)
- return false;
- stream >> splitterState
- >> bookmarks
- >> history
- >> currentDirectory
- >> headerData
- >> viewMode;
- if (!d->qFileDialogUi->splitter->restoreState(splitterState))
- return false;
- QList<int> list = d->qFileDialogUi->splitter->sizes();
- if (list.count() >= 2 && list.at(0) == 0 && list.at(1) == 0) {
- for (int i = 0; i < list.count(); ++i)
- list[i] = d->qFileDialogUi->splitter->widget(i)->sizeHint().width();
- d->qFileDialogUi->splitter->setSizes(list);
- }
- d->qFileDialogUi->sidebar->setUrls(bookmarks);
- while (history.count() > 5)
- history.pop_front();
- setHistory(history);
- setDirectory(lastVisitedDir()->isEmpty() ? currentDirectory : *lastVisitedDir());
- if (!d->qFileDialogUi->treeView->header()->restoreState(headerData))
- return false;
- setViewMode(ViewMode(viewMode));
- return true;
- }
- /*!
- \reimp
- */
- void QFileDialog::changeEvent(QEvent *e)
- {
- Q_D(QFileDialog);
- if (e->type() == QEvent::LanguageChange) {
- d->retranslateWindowTitle();
- d->retranslateStrings();
- }
- QDialog::changeEvent(e);
- }
- QFileDialogPrivate::QFileDialogPrivate()
- :
- #ifndef QT_NO_PROXYMODEL
- proxyModel(0),
- #endif
- model(0),
- fileMode(QFileDialog::AnyFile),
- acceptMode(QFileDialog::AcceptOpen),
- currentHistoryLocation(-1),
- renameAction(0),
- deleteAction(0),
- showHiddenAction(0),
- useDefaultCaption(true),
- defaultFileTypes(true),
- fileNameLabelExplicitlySat(false),
- nativeDialogInUse(false),
- #ifdef Q_WS_MAC
- mDelegate(0),
- #ifndef QT_MAC_USE_COCOA
- mDialog(0),
- mDialogStarted(false),
- mDialogClosed(true),
- #endif
- #endif
- qFileDialogUi(0)
- {
- }
- QFileDialogPrivate::~QFileDialogPrivate()
- {
- }
- void QFileDialogPrivate::retranslateWindowTitle()
- {
- Q_Q(QFileDialog);
- if (!useDefaultCaption || setWindowTitle != q->windowTitle())
- return;
- if (acceptMode == QFileDialog::AcceptOpen) {
- if (fileMode == QFileDialog::DirectoryOnly || fileMode == QFileDialog::Directory)
- q->setWindowTitle(QFileDialog::tr("Find Directory"));
- else
- q->setWindowTitle(QFileDialog::tr("Open"));
- } else
- q->setWindowTitle(QFileDialog::tr("Save As"));
- setWindowTitle = q->windowTitle();
- }
- void QFileDialogPrivate::setLastVisitedDirectory(const QString &dir)
- {
- *lastVisitedDir() = dir;
- }
- void QFileDialogPrivate::retranslateStrings()
- {
- Q_Q(QFileDialog);
- /* WIDGETS */
- if (defaultFileTypes)
- q->setNameFilter(QFileDialog::tr("All Files (*)"));
- QList<QAction*> actions = qFileDialogUi->treeView->header()->actions();
- QAbstractItemModel *abstractModel = model;
- #ifndef QT_NO_PROXYMODEL
- if (proxyModel)
- abstractModel = proxyModel;
- #endif
- int total = qMin(abstractModel->columnCount(QModelIndex()), actions.count() + 1);
- for (int i = 1; i < total; ++i) {
- actions.at(i - 1)->setText(QFileDialog::tr("Show ") + abstractModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
- }
- /* MENU ACTIONS */
- renameAction->setText(QFileDialog::tr("&Rename"));
- deleteAction->setText(QFileDialog::tr("&Delete"));
- showHiddenAction->setText(QFileDialog::tr("Show &hidden files"));
- newFolderAction->setText(QFileDialog::tr("&New Folder"));
- qFileDialogUi->retranslateUi(q);
- if (!fileNameLabelExplicitlySat){
- if (fileMode == QFileDialog::DirectoryOnly || fileMode == QFileDialog::Directory) {
- q->setLabelText(QFileDialog::FileName, QFileDialog::tr("Directory:"));
- } else {
- q->setLabelText(QFileDialog::FileName, QFileDialog::tr("File &name:"));
- }
- fileNameLabelExplicitlySat = false;
- }
- }
- void QFileDialogPrivate::emitFilesSelected(const QStringList &files)
- {
- Q_Q(QFileDialog);
- emit q->filesSelected(files);
- if (files.count() == 1)
- emit q->fileSelected(files.first());
- }
- bool QFileDialogPrivate::canBeNativeDialog()
- {
- Q_Q(QFileDialog);
- if (nativeDialogInUse)
- return true;
- if (q->testAttribute(Qt::WA_DontShowOnScreen))
- return false;
- if (opts & QFileDialog::DontUseNativeDialog)
- return false;
- QLatin1String staticName(QFileDialog::staticMetaObject.className());
- QLatin1String dynamicName(q->metaObject()->className());
- return (staticName == dynamicName);
- }
- /*!
- \since 4.5
- Sets the given \a option to be enabled if \a on is true; otherwise,
- clears the given \a option.
- \sa options, testOption()
- */
- void QFileDialog::setOption(Option option, bool on)
- {
- Q_D(QFileDialog);
- if (!(d->opts & option) != !on)
- setOptions(d->opts ^ option);
- }
- /*!
- \since 4.5
- Returns true if the given \a option is enabled; otherwise, returns
- false.
- \sa options, setOption()
- */
- bool QFileDialog::testOption(Option option) const
- {
- Q_D(const QFileDialog);
- return (d->opts & option) != 0;
- }
- /*!
- \property QFileDialog::options
- \brief the various options that affect the look and feel of the dialog
- \since 4.5
- By default, all options are disabled.
- Options should be set before showing the dialog. Setting them while the
- dialog is visible is not guaranteed to have an immediate effect on the
- dialog (depending on the option and on the platform).
- \sa setOption(), testOption()
- */
- void QFileDialog::setOptions(Options options)
- {
- Q_D(QFileDialog);
- Options changed = (options ^ d->opts);
- if (!changed)
- return;
- d->opts = options;
- if (changed & DontResolveSymlinks)
- d->model->setResolveSymlinks(!(options & DontResolveSymlinks));
- if (changed & ReadOnly) {
- bool ro = (options & ReadOnly);
- d->model->setReadOnly(ro);
- d->qFileDialogUi->newFolderButton->setEnabled(!ro);
- d->renameAction->setEnabled(!ro);
- d->deleteAction->setEnabled(!ro);
- }
- if (changed & HideNameFilterDetails)
- setNameFilters(d->nameFilters);
- if (changed & ShowDirsOnly)
- setFilter((options & ShowDirsOnly) ? filter() & ~QDir::Files : filter() | QDir::Files);
- }
- QFileDialog::Options QFileDialog::options() const
- {
- Q_D(const QFileDialog);
- return d->opts;
- }
- /*!
- \overload
- \since 4.5
- This function connects one of its signals to the slot specified by \a receiver
- and \a member. The specific signal depends is filesSelected() if fileMode is
- ExistingFiles and fileSelected() if fileMode is anything else.
- The signal will be disconnected from the slot when the dialog is closed.
- */
- void QFileDialog::open(QObject *receiver, const char *member)
- {
- Q_D(QFileDialog);
- const char *signal = (fileMode() == ExistingFiles) ? SIGNAL(filesSelected(QStringList))
- : SIGNAL(fileSelected(QString));
- connect(this, signal, receiver, member);
- d->signalToDisconnectOnClose = signal;
- d->receiverToDisconnectOnClose = receiver;
- d->memberToDisconnectOnClose = member;
- QDialog::open();
- }
- /*!
- \reimp
- */
- void QFileDialog::setVisible(bool visible)
- {
- Q_D(QFileDialog);
- if (visible){
- if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden))
- return;
- } else if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden))
- return;
- if (d->canBeNativeDialog()){
- if (d->setVisible_sys(visible)){
- d->nativeDialogInUse = true;
- // Set WA_DontShowOnScreen so that QDialog::setVisible(visible) below
- // updates the state correctly, but skips showing the non-native version:
- setAttribute(Qt::WA_DontShowOnScreen);
- #ifndef QT_NO_FSCOMPLETER
- //So the completer don't try to complete and therefore to show a popup
- d->completer->setModel(0);
- #endif
- } else {
- d->nativeDialogInUse = false;
- setAttribute(Qt::WA_DontShowOnScreen, false);
- #ifndef QT_NO_FSCOMPLETER
- if (d->proxyModel != 0)
- d->completer->setModel(d->proxyModel);
- else
- d->completer->setModel(d->model);
- #endif
- }
- }
- if (!d->nativeDialogInUse)
- d->qFileDialogUi->fileNameEdit->setFocus();
- QDialog::setVisible(visible);
- }
- /*!
- \internal
- set the directory to url
- */
- void QFileDialogPrivate::_q_goToUrl(const QUrl &url)
- {
- //The shortcut in the side bar may have a parent that is not fetched yet (e.g. an hidden file)
- //so we force the fetching
- QFileSystemModelPrivate::QFileSystemNode *node = model->d_func()->node(url.toLocalFile(), true);
- QModelIndex idx = model->d_func()->index(node);
- _q_enterDirectory(idx);
- }
- /*!
- \fn void QFileDialog::setDirectory(const QDir &directory)
- \overload
- */
- /*!
- Sets the file dialog's current \a directory.
- */
- void QFileDialog::setDirectory(const QString &directory)
- {
- Q_D(QFileDialog);
- QString newDirectory = directory;
- QFileInfo info(directory);
- //we remove .. and . from the given path if exist
- if (!directory.isEmpty())
- newDirectory = QDir::cleanPath(directory);
- if (!directory.isEmpty() && newDirectory.isEmpty())
- return;
- d->setLastVisitedDirectory(newDirectory);
- if (d->nativeDialogInUse){
- d->setDirectory_sys(newDirectory);
- return;
- }
- if (d->rootPath() == newDirectory)
- return;
- QModelIndex root = d->model->setRootPath(newDirectory);
- d->qFileDialogUi->newFolderButton->setEnabled(d->model->flags(root) & Qt::ItemIsDropEnabled);
- if (root != d->rootIndex()) {
- #ifndef QT_NO_FSCOMPLETER
- if (directory.endsWith(QLatin1Char('/')))
- d->completer->setCompletionPrefix(newDirectory);
- else
- d->completer->setCompletionPrefix(newDirectory + QLatin1Char('/'));
- #endif
- d->setRootIndex(root);
- }
- d->qFileDialogUi->listView->selectionModel()->clear();
- }
- /*!
- Returns the directory currently being displayed in the dialog.
- */
- QDir QFileDialog::directory() const
- {
- Q_D(const QFileDialog);
- return QDir(d->nativeDialogInUse ? d->directory_sys() : d->rootPath());
- }
- /*!
- Selects the given \a filename in the file dialog.
- \sa selectedFiles()
- */
- void QFileDialog::selectFile(const QString &filename)
- {
- Q_D(QFileDialog);
- if (filename.isEmpty())
- return;
- if (d->nativeDialogInUse){
- d->selectFile_sys(filename);
- return;
- }
- if (!QDir::isRelativePath(filename)) {
- QFileInfo info(filename);
- QString filenamePath = info.absoluteDir().path();
- if (d->model->rootPath() != filenamePath)
- setDirectory(filenamePath);
- }
- QModelIndex index = d->model->index(filename);
- QString file;
- if (!index.isValid()) {
- // save as dialog where we want to input a default value
- QString text = filename;
- if (QFileInfo(filename).isAbsolute()) {
- QString current = d->rootPath();
- text.remove(current);
- if (text.at(0) == QDir::separator()
- #ifdef Q_OS_WIN
- //On Windows both cases can happen
- || text.at(0) == QLatin1Char('/')
- #endif
- )
- text = text.remove(0,1);
- }
- file = text;
- } else {
- file = index.data().toString();
- }
- d->qFileDialogUi->listView->selectionModel()->clear();
- if (!isVisible() || !d->lineEdit()->hasFocus())
- d->lineEdit()->setText(file);
- }
- #ifdef Q_OS_UNIX
- Q_AUTOTEST_EXPORT QString qt_tildeExpansion(const QString &path, bool *expanded = 0)
- {
- if (expanded != 0)
- *expanded = false;
- if (!path.startsWith(QLatin1Char('~')))
- return path;
- QString ret = path;
- #if !defined(Q_OS_INTEGRITY)
- QStringList tokens = ret.split(QDir::separator());
- if (tokens.first() == QLatin1String("~")) {
- ret.replace(0, 1, QDir::homePath());
- } else {
- QString userName = tokens.first();
- userName.remove(0, 1);
- #if defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
- passwd pw;
- passwd *tmpPw;
- char buf[200];
- const int bufSize = sizeof(buf);
- int err = 0;
- #if defined(Q_OS_SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L)
- tmpPw = getpwnam_r(userName.toLocal8Bit().constData(), &pw, buf, bufSize);
- #else
- err = getpwnam_r(userName.toLocal8Bit().constData(), &pw, buf, bufSize, &tmpPw);
- #endif
- if (err || !tmpPw)
- return ret;
- const QString homePath = QString::fromLocal8Bit(pw.pw_dir);
- #else
- passwd *pw = getpwnam(userName.toLocal8Bit().constData());
- if (!pw)
- return ret;
- const QString homePath = QString::fromLocal8Bit(pw->pw_dir);
- #endif
- ret.replace(0, tokens.first().length(), homePath);
- }
- if (expanded != 0)
- *expanded = true;
- #endif
- return ret;
- }
- #endif
- /**
- Returns the text in the line edit which can be one or more file names
- */
- QStringList QFileDialogPrivate::typedFiles() const
- {
- Q_Q(const QFileDialog);
- QStringList files;
- QString editText = lineEdit()->text();
- if (!editText.contains(QLatin1Char('"'))) {
- #ifdef Q_OS_UNIX
- const QString prefix = q->directory().absolutePath() + QDir::separator();
- if (QFile::exists(prefix + editText))
- files << editText;
- else
- files << qt_tildeExpansion(editText);
- #else
- files << editText;
- #endif
- } else {
- // " is used to separate files like so: "file1" "file2" "file3" ...
- // ### need escape character for filenames with quotes (")
- QStringList tokens = editText.split(QLatin1Char('\"'));
- for (int i=0; i<tokens.size(); ++i) {
- if ((i % 2) == 0)
- continue; // Every even token is a separator
- #ifdef Q_OS_UNIX
- const QString token = tokens.at(i);
- const QString prefix = q->directory().absolutePath() + QDir::separator();
- if (QFile::exists(prefix + token))
- files << token;
- else
- files << qt_tildeExpansion(token);
- #else
- files << toInternal(tokens.at(i));
- #endif
- }
- }
- return addDefaultSuffixToFiles(files);
- }
- QStringList QFileDialogPrivate::addDefaultSuffixToFiles(const QStringList filesToFix) const
- {
- QStringList files;
- for (int i=0; i<filesToFix.size(); ++i) {
- QString name = toInternal(filesToFix.at(i));
- QFileInfo info(name);
- // if the filename has no suffix, add the default suffix
- if (!defaultSuffix.isEmpty() && !info.isDir() && name.lastIndexOf(QLatin1Char('.')) == -1)
- name += QLatin1Char('.') + defaultSuffix;
- if (info.isAbsolute()) {
- files.append(name);
- } else {
- // at this point the path should only have Qt path separators.
- // This check is needed since we might be at the root directory
- // and on Windows it already ends with slash.
- QString path = rootPath();
- if (!path.endsWith(QLatin1Char('/')))
- path += QLatin1Char('/');
- path += name;
- files.append(path);
- }
- }
- return files;
- }
- /*!
- Returns a list of strings containing the absolute paths of the
- selected files in the dialog. If no files are selected, or
- the mode is not ExistingFiles or ExistingFile, selectedFiles() contains the current path in the viewport.
- \sa selectedNameFilter(), selectFile()
- */
- QStringList QFileDialog::selectedFiles() const
- {
- Q_D(const QFileDialog);
- if (d->nativeDialogInUse)
- return d->addDefaultSuffixToFiles(d->selectedFiles_sys());
- QModelIndexList indexes = d->qFileDialogUi->listView->selectionModel()->selectedRows();
- QStringList files;
- for (int i = 0; i < indexes.count(); ++i)
- files.append(indexes.at(i).data(QFileSystemModel::FilePathRole).toString());
- if (files.isEmpty() && !d->lineEdit()->text().isEmpty())
- files = d->typedFiles();
- if (files.isEmpty() && !(d->fileMode == ExistingFile || d->fileMode == ExistingFiles))
- files.append(d->rootIndex().data(QFileSystemModel::FilePathRole).toString());
- return files;
- }
- /*
- Makes a list of filters from ;;-separated text.
- Used by the mac and windows implementations
- */
- QStringList qt_make_filter_list(const QString &filter)
- {
- QString f(filter);
- if (f.isEmpty())
- return QStringList();
- QString sep(QLatin1String(";;"));
- int i = f.indexOf(sep, 0);
- if (i == -1) {
- if (f.indexOf(QLatin1Char('\n'), 0) != -1) {
- sep = QLatin1Char('\n');
- i = f.indexOf(sep, 0);
- }
- }
- return f.split(sep);
- }
- /*!
- \since 4.4
- Sets the filter used in the file dialog to the given \a filter.
- If \a filter contains a pair of parentheses containing one or more
- of \bold{anything*something}, separated by spaces, then only the
- text contained in the parentheses is used as the filter. This means
- that these calls are all equivalent:
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 6
- \sa setNameFilters()
- */
- void QFileDialog::setNameFilter(const QString &filter)
- {
- setNameFilters(qt_make_filter_list(filter));
- }
- /*!
- \obsolete
- Use setNameFilter() instead.
- */
- void QFileDialog::setFilter(const QString &filter)
- {
- setNameFilter(filter);
- }
- /*!
- \property QFileDialog::nameFilterDetailsVisible
- \obsolete
- \brief This property holds whether the filter details is shown or not.
- \since 4.4
- When this property is true (the default), the filter details are shown
- in the combo box. When the property is set to false, these are hidden.
- Use setOption(HideNameFilterDetails, !\e enabled) or
- !testOption(HideNameFilterDetails).
- */
- void QFileDialog::setNameFilterDetailsVisible(bool enabled)
- {
- setOption(HideNameFilterDetails, !enabled);
- }
- bool QFileDialog::isNameFilterDetailsVisible() const
- {
- return !testOption(HideNameFilterDetails);
- }
- /*
- Strip the filters by removing the details, e.g. (*.*).
- */
- QStringList qt_strip_filters(const QStringList &filters)
- {
- QStringList strippedFilters;
- QRegExp r(QString::fromLatin1(qt_file_dialog_filter_reg_exp));
- for (int i = 0; i < filters.count(); ++i) {
- QString filterName;
- int index = r.indexIn(filters[i]);
- if (index >= 0)
- filterName = r.cap(1);
- strippedFilters.append(filterName.simplified());
- }
- return strippedFilters;
- }
- /*!
- \since 4.4
- Sets the \a filters used in the file dialog.
- \snippet doc/src/snippets/code/src_gui_dialogs_qfiledialog.cpp 7
- */
- void QFileDialog::setNameFilters(const QStringList &filters)
- {
- Q_D(QFileDialog);
- d->defaultFileTypes = (filters == QStringList(QFileDialog::tr("All Files (*)")));
- QStringList cleanedFilters;
- for (int i = 0; i < filters.count(); ++i) {
- cleanedFilters << filters[i].simplified();
- }
- d->nameFilters = cleanedFilters;
- if (d->nativeDialogInUse){
- d->setNameFilters_sys(cleanedFilters);
- return;
- }
- d->qFileDialogUi->fileTypeCombo->clear();
- if (cleanedFilters.isEmpty())
- return;
- if (testOption(HideNameFilterDetails))
- d->qFileDialogUi->fileTypeCombo->addItems(qt_strip_filters(cleanedFilters));
- else
- d->qFileDialogUi->fileTypeCombo->addItems(cleanedFilters);
- d->_q_useNameFilter(0);
- }
- /*!
- \obsolete
- Use setNameFilters() instead.
- */
- void QFileDialog::setFilters(const QStringList &filters)
- {
- setNameFilters(filters);
- }
- /*!
- \since 4.4
- Returns the file type filters that are in operation on this file
- dialog.
- */
- QStringList QFileDialog::nameFilters() const
- {
- return d_func()->nameFilters;
- }
- /*!
- \obsolete
- Use nameFilters() instead.
- */
- QStringList QFileDialog::filters() const
- {
- return nameFilters();
- }
- /*!
- \since 4.4
- Sets the current file type \a filter. Multiple filters can be
- passed in \a filter by separating them with semicolons or spaces.
- \sa setNameFilter(), setNameFilters(), selectedNameFilter()
- */
- void QFileDialog::selectNameFilter(const QString &filter)
- {
- Q_D(QFileDialog);
- if (d->nativeDialogInUse) {
- d->selectNameFilter_sys(filter);
- return;
- }
- int i;
- if (testOption(HideNameFilterDetails)) {
- i = d->qFileDialogUi->fileTypeCombo->findText(qt_strip_filters(qt_make_filter_list(filter)).first());
- } else {
- i = d->qFileDialogUi->fileTypeCombo->findText(filter);
- }
- if (i >= 0) {
- d->qFileDialogUi->fileTypeCombo->setCurrentIndex(i);
- d->_q_useNameFilter(d->qFileDialogUi->fileTypeCombo->currentIndex());
- }
- }
- /*!
- \obsolete
- Use selectNameFilter() instead.
- */
- void QFileDialog::selectFilter(const QString &filter)
- {
- selectNameFilter(filter);
- }
- /*!
- \since 4.4
- Returns the filter that the user selected in the file dialog.
- \sa selectedFiles()
- */
- QString QFileDialog::selectedNameFilter() const
- {
- Q_D(const QFileDialog);
- if (d->nativeDialogInUse)
- return d->selectedNameFilter_sys();
- return d->qFileDialogUi->fileTypeCombo->currentText();
- }
- /*!
- \obsolete
- Use selectedNameFilter() instead.
- */
- QString QFileDialog::selectedFilter() const
- {
- return selectedNameFilter();
- }
- /*!
- \since 4.4
- Returns the filter that is used when displaying files.
- \sa setFilter()
- */
- QDir::Filters QFileDialog::filter() const
- {
- Q_D(const QFileDialog);
- return d->model->filter();
- }
- /*!
- \since 4.4
- Sets the filter used by the model to \a filters. The filter is used
- to specify the kind of files that should be shown.
- \sa filter()
- */
- void QFileDialog::setFilter(QDir::Filters filters)
- {
- Q_D(QFileDialog);
- d->model->setFilter(filters);
- if (d->nativeDialogInUse){
- d->setFilter_sys();
- return;
- }
- d->showHiddenAction->setChecked((filters & QDir::Hidden));
- }
- /*!
- \property QFileDialog::viewMode
- \brief the way files and directories are displayed in the dialog
- By default, the \c Detail mode is used to display information about
- files and directories.
- \sa ViewMode
- */
- void QFileDialog::setViewMode(QFileDialog::ViewMode mode)
- {
- Q_D(QFileDialog);
- if (mode == Detail)
- d->_q_showDetailsView();
- else
- d->_q_showListView();
- }
- QFileDialog::ViewMode QFileDialog::viewMode() const
- {
- Q_D(const QFileDialog);
- return (d->qFileDialogUi->stackedWidget->currentWidget() == d->qFileDialogUi->listView->parent() ? QFileDialog::List : QFileDialog::Detail);
- }
- /*!
- \property QFileDialog::fileMode
- \brief the file mode of the dialog
- The file mode defines the number and type of items that the user is
- expected to select in the dialog.
- By default, this property is set to AnyFile.
- This function will set the labels for the FileName and
- \l{QFileDialog::}{Accept} \l{DialogLabel}s. It is possible to set
- custom text after the call to setFileMode().
- \sa FileMode
- */
- void QFileDialog::setFileMode(QFileDialog::FileMode mode)
- {
- Q_D(QFileDialog);
- d->fileMode = mode;
- d->retranslateWindowTitle();
- // keep ShowDirsOnly option in sync with fileMode (BTW, DirectoryOnly is obsolete)
- setOption(ShowDirsOnly, mode == DirectoryOnly);
- // set selection mode and behavior
- QAbstractItemView::SelectionMode selectionMode;
- if (mode == QFileDialog::ExistingFiles)
- selectionMode = QAbstractItemView::ExtendedSelection;
- else
- selectionMode = QAbstractItemView::SingleSelection;
- d->qFileDialogUi->listView->setSelectionMode(selectionMode);
- d->qFileDialogUi->treeView->setSelectionMode(selectionMode);
- // set filter
- d->model->setFilter(d->filterForMode(filter()));
- // setup file type for directory
- QString buttonText = (d->acceptMode == AcceptOpen ? tr("&Open") : tr("&Save"));
- if (mode == DirectoryOnly || mode == Directory) {
- d->qFileDialogUi->fileTypeCombo->clear();
- d->qFileDialogUi->fileTypeCombo->addItem(tr("Directories"));
- d->qFileDialogUi->fileTypeCombo->setEnabled(false);
- if (!d->fileNameLabelExplicitlySat){
- setLabelText(FileName, tr("Directory:"));
- d->fileNameLabelExplicitlySat = false;
- }
- buttonText = tr("&Choose");
- } else {
- if (!d->fileNameLabelExplicitlySat){
- setLabelText(FileName, tr("File &name:"));
- d->fileNameLabelExplicitlySat = false;
- }
- }
- setLabelText(Accept, buttonText);
- if (d->nativeDialogInUse){
- d->setFilter_sys();
- return;
- }
- d->qFileDialogUi->fileTypeCombo->setEnabled(!testOption(ShowDirsOnly));
- d->_q_updateOkButton();
- }
- QFileDialog::FileMode QFileDialog::fileMode() const
- {
- Q_D(const QFileDialog);
- return d->fileMode;
- }
- /*!
- \property QFileDialog::acceptMode
- \brief the accept mode of the dialog
- The action mode defines whether the dialog is for opening or saving files.
- By default, this property is set to \l{AcceptOpen}.
- \sa AcceptMode
- */
- void QFileDialog::setAcceptMode(QFileDialog::AcceptMode mode)
- {
- Q_D(QFileDialog);
- d->acceptMode = mode;
- bool directoryMode = (d->fileMode == Directory || d->fileMode == DirectoryOnly);
- QDialogButtonBox::StandardButton button = (mode == AcceptOpen ? QDialogButtonBox::Open : QDialogButtonBox::Save);
- d->qFileDialogUi->buttonBox->setStandardButtons(button | QDialogButtonBox::Cancel);
- d->qFileDialogUi->buttonBox->button(button)->setEnabled(false);
- d->_q_updateOkButton();
- if (mode == AcceptOpen && directoryMode)
- setLabelText(Accept, tr("&Choose"));
- else
- setLabelText(Accept, (mode == AcceptOpen ? tr("&Open") : tr("&Save")));
- if (mode == AcceptSave) {
- d->qFileDialogUi->lookInCombo->setEditable(false);
- }
- d->retranslateWindowTitle();
- #if defined(Q_WS_MAC)
- d->deleteNativeDialog_sys();
- setAttribute(Qt::WA_DontShowOnScreen, false);
- #endif
- }
- /*
- Returns the file system model index that is the root index in the
- views
- */
- QModelIndex QFileDialogPrivate::rootIndex() const {
- return mapToSource(qFileDialogUi->listView->rootIndex());
- }
- QAbstractItemView *QFileDialogPrivate::currentView() const {
- if (!qFileDialogUi->stackedWidget)
- return 0;
- if (qFileDialogUi->stackedWidget->currentWidget() == qFileDialogUi->listView->parent())
- return qFileDialogUi->listView;
- return qFileDialogUi->treeView;
- }
- QLineEdit *QFileDialogPrivate::lineEdit() const {
- return (QLineEdit*)qFileDialogUi->fileNameEdit;
- }
- /*
- Sets the view root index to be the file system model index
- */
- void QFileDialogPrivate::setRootIndex(const QModelIndex &index) const {
- Q_ASSERT(index.isValid() ? index.model() == model : true);
- QModelIndex idx = mapFromSource(index);
- qFileDialogUi->treeView->setRootIndex(idx);
- qFileDialogUi->listView->setRootIndex(idx);
- }
- /*
- Select a file system model index
- returns the index that was selected (or not depending upon sortfilterproxymodel)
- */
- QModelIndex QFileDialogPrivate::select(const QModelIndex &index) const {
- Q_ASSERT(index.isValid() ? index.model() == model : true);
- QModelIndex idx = mapFromSource(index);
- if (idx.isValid() && !qFileDialogUi->listView->selectionModel()->isSelected(idx))
- qFileDialogUi->listView->selectionModel()->select(idx,
- QItemSelectionModel::Select | QItemSelectionModel::Rows);
- return idx;
- }
- QFileDialog::AcceptMode QFileDialog::acceptMode() const
- {
- Q_D(const QFileDialog);
- return d->acceptMode;
- }
- /*!
- \property QFileDialog::readOnly
- \obsolete
- \brief Whether the filedialog is read-only
- If this property is set to false, the file dialog will allow renaming,
- and deleting of files and directories and creating directories.
- Use setOption(ReadOnly, \e enabled) or testOption(ReadOnly) instead.
- */
- void QFileDialog::setReadOnly(bool enabled)
- {
- setOption(ReadOnly, enabled);
- }
- bool QFileDialog::isReadOnly() const
- {
- return testOption(ReadOnly);
- }
- /*!
- \property QFileDialog::resolveSymlinks
- \obsolete
- \brief whether the filedialog should resolve shortcuts
- If this property is set to true, the file dialog will resolve
- shortcuts or symbolic links.
- Use setOption(DontResolveSymlinks, !\a enabled) or
- !testOption(DontResolveSymlinks).
- */
- void QFileDialog::setResolveSymlinks(bool enabled)
- {
- setOption(DontResolveSymlinks, !enabled);
- }
- bool QFileDialog::resolveSymlinks() const
- {
- return !testOption(DontResolveSymlinks);
- }
- /*!
- \property QFileDialog::confirmOverwrite
- \obsolete
- \brief whether the filedialog should ask before accepting a selected file,
- when the accept mode is AcceptSave
- Use setOption(DontConfirmOverwrite, !\e enabled) or
- !testOption(DontConfirmOverwrite) instead.
- */
- void QFileDialog::setConfirmOverwrite(bool enabled)
- {
- setOption(DontConfirmOverwrite, !enabled);
- }
- bool QFileDialog::confirmOverwrite() const
- {
- return !testOption(DontConfirmOverwrite);
- }
- /*!
- \property QFileDialog::defaultSuffix
- \brief suffix added to the filename if no other suffix was specified
- This property specifies a string that will be added to the
- filename if it has no suffix already. The suffix is typically
- used to indicate the file type (e.g. "txt" indicates a text
- file).
- */
- void QFileDialog::setDefaultSuffix(const QString &suffix)
- {
- Q_D(QFileDialog);
- d->defaultSuffix = suffix;
- }
- QString QFileDialog::defaultSuffix() const
- {
- Q_D(const QFileDialog);
- return d->defaultSuffix;
- }
- /*!
- Sets the browsing history of the filedialog to contain the given
- \a paths.
- */
- void QFileDialog::setHistory(const QStringList &paths)
- {
- Q_D(QFileDialog);
- d->qFileDialogUi->lookInCombo->setHistory(paths);
- }
- void QFileDialogComboBox::setHistory(const QStringList &paths)
- {
- m_history = paths;
- // Only populate the first item, showPopup will populate the rest if needed
- QList<QUrl> list;
- QModelIndex idx = d_ptr->model->index(d_ptr->rootPath());
- //On windows the popup display the "C:\", convert to nativeSeparators
- QUrl url = QUrl::fromLocalFile(QDir::toNativeSeparators(idx.data(QFileSystemModel::FilePathRole).toString()));
- if (url.isValid())
- list.append(url);
- urlModel->setUrls(list);
- }
- /*!
- Returns the browsing history of the filedialog as a list of paths.
- */
- QStringList QFileDialog::history() const
- {
- Q_D(const QFileDialog);
- QStringList currentHistory = d->qFileDialogUi->lookInCombo->history();
- //On windows the popup display the "C:\", convert to nativeSeparators
- QString newHistory = QDir::toNativeSeparators(d->rootIndex().data(QFileSystemModel::FilePathRole).toString());
- if (!currentHistory.contains(newHistory))
- currentHistory << newHistory;
- return currentHistory;
- }
- /*!
- Sets the item delegate used to render items in the views in the
- file dialog to the given \a delegate.
- \warning You should not share the same instance of a delegate between views.
- Doing so can cause incorrect or unintuitive editing behavior since each
- view connected to a given delegate may receive the \l{QAbstractItemDelegate::}{closeEditor()}
- signal, and attempt to access, modify or close an editor that has already been closed.
- Note that the model used is QFileSystemModel. It has custom item data roles, which is
- described by the \l{QFileSystemModel::}{Roles} enum. You can use a QFileIconProvider if
- you only want custom icons.
- \sa itemDelegate(), setIconProvider(), QFileSystemModel
- */
- void QFileDialog::setItemDelegate(QAbstractItemDelegate *delegate)
- {
- Q_D(QFileDialog);
- d->qFileDialogUi->listView->setItemDelegate(delegate);
- d->qFileDialogUi->treeView->setItemDelegate(delegate);
- }
- /*!
- Returns the item delegate used to render the items in the views in the filedialog.
- */
- QAbstractItemDelegate *QFileDialog::itemDelegate() const
- {
- Q_D(const QFileDialog);
- return d->qFileDialogUi->listView->itemDelegate();
- }
- /*!
- Sets the icon provider used by the filedialog to the specified \a provider.
- */
- void QFileDialog::setIconProvider(QFileIconProvider *provider)
- {
- Q_D(QFileDialog);
- d->model->setIconProvider(provider);
- //It forces the refresh of all entries in the side bar, then we can get new icons
- d->qFileDialogUi->sidebar->setUrls(d->qFileDialogUi->sidebar->urls());
- }
- /*!
- Returns the icon provider used by the filedialog.
- */
- QFileIconProvider *QFileDialog::iconProvider() const
- {
- Q_D(const QFileDialog);
- return d->model->iconProvider();
- }
- /*!
- Sets the \a text shown in the filedialog in the specified \a label.
- */
- void QFileDialog::setLabelText(DialogLabel label, const QString &text)
- {
- Q_D(QFileDialog);
- QPushButton *button;
- switch (label) {
- case LookIn:
- d->qFileDialogUi->lookInLabel->setText(text);
- break;
- case FileName:
- d->qFileDialogUi->fileNameLabel->setText(text);
- d->fileNameLabelExplicitlySat = true;
- break;
- case FileType:
- d->qFileDialogUi->fileTypeLabel->setText(text);
- break;
- case Accept:
- d->acceptLabel = text;
- if (acceptMode() == AcceptOpen)
- button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Open);
- else
- button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Save);
- if (button)
- button->setText(text);
- break;
- case Reject:
- button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Cancel);
- if (button)
- button->setText(text);
- break;
- }
- }
- /*!
- Returns the text shown in the filedialog in the specified \a label.
- */
- QString QFileDialog::labelText(DialogLabel label) const
- {
- QPushButton *button;
- Q_D(const QFileDialog);
- switch (label) {
- case LookIn:
- return d->qFileDialogUi->lookInLabel->text();
- case FileName:
- return d->qFileDialogUi->fileNameLabel->text();
- case FileType:
- return d->qFileDialogUi->fileTypeLabel->text();
- case Accept:
- if (acceptMode() == AcceptOpen)
- button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Open);
- else
- button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Save);
- if (button)
- return button->text();
- case Reject:
- button = d->qFileDialogUi->buttonBox->button(QDialogButtonBox::Cancel);
- if (button)
- return button->text();
- }
- return QString();
- }
- /*
- For the native file dialogs
- */
- #if defined(Q_WS_WIN)
- extern QString qt_win_get_open_file_name(const QFileDialogArgs &args,
- QString *initialDirectory,
- QString *selectedFilter);
- extern QString qt_win_get_save_file_name(const QFileDialo…
Large files files are truncated, but you can click here to view the full file