⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qfiledialog.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.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 GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qfiledialog.h"#ifndef QT_NO_FILEDIALOG#include <qcombobox.h>#include <qdirmodel.h>#include <qheaderview.h>#include <qlistview.h>#include <qtreeview.h>#include <qlabel.h>#include <qlayout.h>#include <qlineedit.h>#include <qmenu.h>#include <qevent.h>#include <qpixmap.h>#include <qpushbutton.h>#include <qregexp.h>#include <qtoolbutton.h>#include <qmessagebox.h>#include <qapplication.h>#ifdef Q_WS_WIN#include <qwindowsstyle.h>#endif#include <qshortcut.h>#ifdef Q_WS_MAC#include <private/qunicodetables_p.h>#include <qmacstyle_mac.h>#endif#include <qdebug.h>#include <private/qfiledialog_p.h>#include <stdlib.h> // getenv#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#endifconst char *qt_file_dialog_filter_reg_exp =    "([a-zA-Z0-9]*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$";// Makes a list of filters from ;;-separated text.QStringList qt_make_filter_list(const QString &filter){    QString f(filter);    if (f.isEmpty())        return QStringList();    QString sep(";;");    int i = f.indexOf(sep, 0);    if (i == -1) {        if (f.indexOf("\n", 0) != -1) {            sep = "\n";            i = f.indexOf(sep, 0);        }    }    return f.split(sep);}// Makes a list of filters from a normal filter string "Image Files (*.png *.jpg)"static QStringList qt_clean_filter_list(const QString &filter){    QRegExp regexp(QString::fromLatin1(qt_file_dialog_filter_reg_exp));    QString f = filter;    int i = regexp.indexIn(f);    if (i >= 0)        f = regexp.cap(2);    return f.split(' ', QString::SkipEmptyParts);}class QFileDialogLineEdit : public QLineEdit{public:    QFileDialogLineEdit(QWidget *parent)        : QLineEdit(parent), key(0) {}    inline int lastKeyPressed() { return key; }protected:    void keyPressEvent(QKeyEvent *e);private:    int key;};void QFileDialogLineEdit::keyPressEvent(QKeyEvent *e){    key = e->key();    QLineEdit::keyPressEvent(e);    // FIXME: this is a hack to avoid propagating key press events    // to the dialog and from there to the "Ok" button    if (key != Qt::Key_Escape)        e->accept();}/*!  \class QFileDialog  \brief The QFileDialog class provides a dialog that allow users to select files or directories.  \ingroup dialogs  \mainclass  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, these static functions will call the native  Windows file dialog, and on Mac OS X these static function will call  the native Mac OS X file dialog.  \code    QString s = QFileDialog::getOpenFileName(                    this,                    "Choose a file",                    "/home",                    "Images (*.png *.xpm *.jpg)");  \endcode  In the above example, a modal QFileDialog is created using a static  function. The dialog initially displays the contents of the "/home"  directory, and displays files matching the patterns given in the  string "Images (*.png *.xpm *.jpg)". The parent of the file dialog  is set to \e this, and the dialog is named "open file dialog".  The caption at the top of file dialog is set to "Choose a file"  instead of the default.  If you want to use multiple filters, separate each one with  \e two semicolons. For example:  \code  "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"  \endcode  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:  \code    QFileDialog *fd = new QFileDialog(this);    fd->setFileMode(QFileDialog::AnyFile);  \endcode  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.  You can retrieve the dialog's mode with mode(). Use setFilter() to set  the dialog's file filter. For example:  \code    fd->setFilter( "Images (*.png *.xpm *.jpg)" );  \endcode  In the above example, the filter is set to "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 setFilters(). Use selectFilter() to select one of the filters  you've given as the file dialog's default filter.  The file dialog has two view modes: QFileDialog::List and  QFileDialog::Detail.  QFileDialog::List presents the contents of the current directory as a  list of file and directory names. 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().  \code    fd->setViewMode(QFileDialog::Detail);  \endcode  The last important function you will need to use when creating your  own file dialog is selectedFiles().  \code    QStringList fileNames;    if (fileDialog->exec())        fileNames = fileDialog->selectedFiles();  \endcode  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.*//*!    \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 DirectoryOnly  The name of a directory. The file dialog will only display directories.    \value ExistingFiles  The names of zero or more existing files.    \sa setFileMode()*//*!    \enum QFileDialog::Option    \value ShowDirsOnly Only show directories in the file dialog. By default both files and    directories are shown.    \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 DontUseSheet Don't make the native file dialog a sheet. By default on Mac OS X, the    native file dialog is made a sheet if it has a parent that can take a sheet.    \value DontUseNativeDialog Don't use the native file dialog.  By default on Mac OS X and Windows,    the native file dialog is used.*//*!  \enum QFileDialog::DialogLabel  \value LookIn  \value FileName  \value FileType  \value Accept  \value Reject*//*!    \fn void QFileDialog::filesSelected(const QStringList &selected)    When the selection changes, this signal is emitted with the    (possibly empty) list of \a selected files.    \sa currentChanged()*//*!    \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 QFileDialog::QFileDialog(QWidget *parent, Qt::WFlags flags)    Constructs a file dialog with the given \a parent and widget \a flags.*/QFileDialog::QFileDialog(QWidget *parent, Qt::WFlags f)    : QDialog(*new QFileDialogPrivate, parent, f){    QDir dir = QDir::current();    d_func()->setup(dir.absolutePath(), dir.nameFilters());}/*!    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);    setWindowTitle(caption);    QStringList nameFilter = qt_make_filter_list(filter);    if (nameFilter.isEmpty())        d->setup(directory, QStringList(tr("All Files (*)")));    else        d->setup(directory, nameFilter);}/*!    \internal*/QFileDialog::QFileDialog(const QFileDialogArgs &args)    : QDialog(*new QFileDialogPrivate, args.parent, 0){    Q_D(QFileDialog);    d->fileMode = args.mode;    d->confirmOverwrite = !(args.options & DontConfirmOverwrite);    setWindowTitle(args.caption);    QStringList nameFilter = qt_make_filter_list(args.filter);    if (nameFilter.isEmpty())        d->setup(args.directory, QStringList(tr("All Files (*)")));    else        d->setup(args.directory, nameFilter);    setResolveSymlinks(!(args.options & DontResolveSymlinks));}/*!*/QFileDialog::~QFileDialog(){}/*!    \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);    QModelIndex index = d->model->index(directory);    if (!index.isValid())        return;    if (!d->model->isDir(index))        return;    d->setRootIndex(index);    d->updateButtons(index);}/*!    Returns the directory currently being displayed in the dialog.*/QDir QFileDialog::directory() const{    Q_D(const QFileDialog);    QDir dir(d->model->filePath(d->rootIndex()));    dir.setNameFilters(d->model->nameFilters());    dir.setSorting(d->model->sorting());    dir.setFilter(d->model->filter());    return dir;}/*!    Selects the given \a filename in both the file dialog.*/void QFileDialog::selectFile(const QString &filename){    Q_D(QFileDialog);    QModelIndex index;    QString text = filename;    if (QFileInfo(filename).isAbsolute()) {        index = d->model->index(filename);        QString current = d->model->filePath(d->rootIndex());        text.remove(current);    } else { // faster than asking for model()->index(currentPath + filename)        QStringList entries = directory().entryList(d->model->filter(), d->model->sorting());        // The model does not contain ".." and ".", remove those from entries so the indexes match.        int i = entries.indexOf(QLatin1String("."));        if (i != -1)            entries.removeAt(i);        i = entries.indexOf(QLatin1String(".."));        if (i != -1)            entries.removeAt(i);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -