📄 qfiledialog.cpp
字号:
can contain both the initial directory and initial selection /home/user/foo.txt \sa workingDirectory() */QString QFileDialogPrivate::initialSelection(const QString &path){ if (!path.isEmpty()) { QFileInfo info(path); if (!info.isDir()) return info.fileName(); } return QString();}/*! \reimp*/void QFileDialog::done(int result){ QDialog::done(result);}/*! \reimp*/void QFileDialog::accept(){ Q_D(QFileDialog); QStringList files = selectedFiles(); if (files.isEmpty()) return; QString lineEditText = d->lineEdit()->text(); // "hidden feature" type .. and then enter, and it will move up a dir // special case for ".." if (lineEditText == QLatin1String("..")) { d->_q_navigateToParent(); bool block = d->qFileDialogUi->fileNameEdit->blockSignals(true); d->lineEdit()->selectAll(); d->qFileDialogUi->fileNameEdit->blockSignals(block); return; } switch (d->fileMode) { case DirectoryOnly: case Directory: { QString fn = files.first(); QFileInfo info(fn); if (!info.exists()) info = QFileInfo(d->getEnvironmentVariable(fn)); if (!info.exists()) {#ifndef QT_NO_MESSAGEBOX QString message = tr("%1\nDirectory not found.\nPlease verify the " "correct directory name was given."); QMessageBox::warning(this, windowTitle(), message.arg(info.fileName()));#endif // QT_NO_MESSAGEBOX return; } if (info.isDir()) { emit filesSelected(files); QDialog::accept(); } return; } case AnyFile: { QString fn = files.first(); QFileInfo info(fn); if (info.isDir()) { setDirectory(info.absoluteFilePath()); d->lineEdit()->clear(); return; } if (!info.exists()) { int maxNameLength = d->maxNameLength(info.path()); if (maxNameLength >= 0 && info.fileName().length() > maxNameLength) return; } // check if we have to ask for permission to overwrite the file if (!info.exists() || !confirmOverwrite() || acceptMode() == AcceptOpen) { emit filesSelected(QStringList(fn)); QDialog::accept();#ifndef QT_NO_MESSAGEBOX } else { if (QMessageBox::warning(this, windowTitle(), tr("%1 already exists.\nDo you want to replace it?") .arg(info.fileName()), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) emit filesSelected(QStringList(fn)); QDialog::accept();#endif } return; } case ExistingFile: case ExistingFiles: for (int i = 0; i < files.count(); ++i) { QFileInfo info(files.at(i)); if (!info.exists()) info = QFileInfo(d->getEnvironmentVariable(files.at(i))); if (!info.exists()) {#ifndef QT_NO_MESSAGEBOX QString message = tr("%1\nFile not found.\nPlease verify the " "correct file name was given."); QMessageBox::warning(this, windowTitle(), message.arg(info.fileName()));#endif // QT_NO_MESSAGEBOX return; } if (info.isDir()) { setDirectory(info.absoluteFilePath()); d->lineEdit()->clear(); return; } } emit filesSelected(files); QDialog::accept(); return; }}/*! \internal Create widgets, layout and set default values*/void QFileDialogPrivate::init(const QString &directory, const QString &nameFilter, const QString &caption){ Q_Q(QFileDialog); if (!caption.isEmpty()) { useDefaultCaption = false; setWindowTitle = caption; q->setWindowTitle(caption); } createWidgets(); createMenuActions(); retranslateStrings(); q->setFileMode(fileMode);#ifndef QT_NO_SETTINGS QSettings settings(QSettings::UserScope, QLatin1String("Trolltech")); settings.beginGroup(QLatin1String("Qt")); q->restoreState(settings.value(QLatin1String("filedialog")).toByteArray());#endif // Default case if (!nameFilter.isEmpty()) q->setFilter(nameFilter); q->setAcceptMode(QFileDialog::AcceptOpen); q->setDirectory(workingDirectory(directory)); q->selectFile(initialSelection(directory)); qFileDialogUi->fileNameEdit->setFocus(); _q_updateOkButton(); q->resize(q->sizeHint());}/*! \internal Create the widgets, set properties and connections*/void QFileDialogPrivate::createWidgets(){ Q_Q(QFileDialog); model = new QFileSystemModel(q); model->setObjectName(QLatin1String("qt_filesystem_model"));#ifdef Q_WS_MAC model->setNameFilterDisables(true);#else model->setNameFilterDisables(false);#endif QFileDialog::connect(model, SIGNAL(rootPathChanged(const QString &)), q, SLOT(_q_pathChanged(const QString &))); QFileDialog::connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), q, SLOT(_q_rowsInserted(const QModelIndex &))); model->setReadOnly(false); qFileDialogUi = new Ui_QFileDialog(); qFileDialogUi->setupUi(q); QList<QUrl> initialBookmarks; initialBookmarks << QUrl::fromLocalFile(QLatin1String("")) << QUrl::fromLocalFile(QDir::homePath()); qFileDialogUi->sidebar->init(model, initialBookmarks); QFileDialog::connect(qFileDialogUi->sidebar, SIGNAL(goToUrl(const QUrl &)), q, SLOT(_q_goToUrl(const QUrl &))); QObject::connect(qFileDialogUi->buttonBox, SIGNAL(accepted()), q, SLOT(accept())); QObject::connect(qFileDialogUi->buttonBox, SIGNAL(rejected()), q, SLOT(reject())); urlModel = new QUrlModel(q); urlModel->showFullPath = true; urlModel->setFileSystemModel(model); qFileDialogUi->lookInCombo->setModel(urlModel); QObject::connect(qFileDialogUi->lookInCombo, SIGNAL(activated(QString)), q, SLOT(_q_goToDirectory(QString))); qFileDialogUi->lookInCombo->setInsertPolicy(QComboBox::NoInsert); qFileDialogUi->lookInCombo->setDuplicatesEnabled(false); // filename qFileDialogUi->fileNameEdit->init(this);#ifndef QT_NO_SHORTCUT qFileDialogUi->fileNameLabel->setBuddy(qFileDialogUi->fileNameEdit);#endif#ifndef QT_NO_COMPLETER completer = new QFSCompletor(model, q); qFileDialogUi->fileNameEdit->setCompleter(completer); QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)), q, SLOT(_q_autoCompleteFileName(QString)));#endif // QT_NO_COMPLETER QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(textChanged(QString)), q, SLOT(_q_updateOkButton())); QObject::connect(qFileDialogUi->fileNameEdit, SIGNAL(returnPressed()), q, SLOT(accept())); // filetype qFileDialogUi->fileTypeCombo->setDuplicatesEnabled(false); qFileDialogUi->fileTypeCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); qFileDialogUi->fileTypeCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); QObject::connect(qFileDialogUi->fileTypeCombo, SIGNAL(activated(const QString &)), q, SLOT(_q_useNameFilter(const QString &))); QObject::connect(qFileDialogUi->fileTypeCombo, SIGNAL(activated(const QString &)), q, SIGNAL(filterSelected(const QString &))); qFileDialogUi->listView->init(this); qFileDialogUi->listView->setModel(model); QObject::connect(qFileDialogUi->listView, SIGNAL(activated(QModelIndex)), q, SLOT(_q_enterDirectory(QModelIndex))); QObject::connect(qFileDialogUi->listView, SIGNAL(customContextMenuRequested(QPoint)), q, SLOT(_q_showContextMenu(QPoint)));#ifndef QT_NO_SHORTCUT QShortcut *shortcut = new QShortcut(qFileDialogUi->listView); shortcut->setKey(QKeySequence(QLatin1String("Delete"))); QObject::connect(shortcut, SIGNAL(activated()), q, SLOT(_q_deleteCurrent()));#endif qFileDialogUi->treeView->init(this); qFileDialogUi->treeView->setModel(model); QHeaderView *treeHeader = qFileDialogUi->treeView->header(); QFontMetrics fm(q->font()); treeHeader->resizeSection(0, fm.width(QLatin1String("wwwwwwwwwwwwwwwwwwwwwwwwww"))); treeHeader->resizeSection(1, fm.width(QLatin1String("128.88 GB"))); treeHeader->resizeSection(2, fm.width(QLatin1String("mp3Folder"))); treeHeader->resizeSection(3, fm.width(QLatin1String("10/29/81 02:02PM"))); treeHeader->setContextMenuPolicy(Qt::ActionsContextMenu); QActionGroup *showActionGroup = new QActionGroup(q); showActionGroup->setExclusive(false); QObject::connect(showActionGroup, SIGNAL(triggered(QAction *)), q, SLOT(_q_showHeader(QAction *)));; QAbstractItemModel *abstractModel = model;#ifndef QT_NO_PROXYMODEL if (proxyModel) abstractModel = proxyModel;#endif for (int i = 1; i < abstractModel->columnCount(QModelIndex()); ++i) { QAction *showHeader = new QAction(showActionGroup); showHeader->setCheckable(true); showHeader->setChecked(true); treeHeader->addAction(showHeader); } QItemSelectionModel *selModel = qFileDialogUi->treeView->selectionModel(); qFileDialogUi->treeView->setSelectionModel(qFileDialogUi->listView->selectionModel()); delete selModel; QObject::connect(qFileDialogUi->treeView, SIGNAL(activated(QModelIndex)), q, SLOT(_q_enterDirectory(QModelIndex))); QObject::connect(qFileDialogUi->treeView, SIGNAL(customContextMenuRequested(QPoint)), q, SLOT(_q_showContextMenu(QPoint)));#ifndef QT_NO_SHORTCUT shortcut = new QShortcut(qFileDialogUi->treeView); shortcut->setKey(QKeySequence(QLatin1String("Delete"))); QObject::connect(shortcut, SIGNAL(activated()), q, SLOT(_q_deleteCurrent()));#endif // Selections QItemSelectionModel *selections = qFileDialogUi->listView->selectionModel(); QObject::connect(selections, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), q, SLOT(_q_selectionChanged())); QObject::connect(selections, SIGNAL(currentChanged(QModelIndex,QModelIndex)), q, SLOT(_q_currentChanged(QModelIndex))); qFileDialogUi->splitter->setStretchFactor(qFileDialogUi->splitter->indexOf(qFileDialogUi->splitter->widget(1)), QSizePolicy::Expanding); createToolButtons();}void QFileDialogPrivate::_q_showHeader(QAction *action){ Q_Q(QFileDialog); QActionGroup *actionGroup = qobject_cast<QActionGroup*>(q->sender()); qFileDialogUi->treeView->header()->setSectionHidden(actionGroup->actions().indexOf(action) + 1, !action->isChecked());}#ifndef QT_NO_PROXYMODEL/*! \since 4.3 Sets the model for the views to the given \a proxyModel. This is useful if you want to modify the underlying model; for example, to add columns, filter data or add drives. Any existing proxy model will be removed, but not deleted. The file dialog will take ownership of the \a proxyModel. \sa proxyModel()*/void QFileDialog::setProxyModel(QAbstractProxyModel *proxyModel){ Q_D(QFileDialog); if ((!proxyModel && !d->proxyModel) || (proxyModel == d->proxyModel)) return; QModelIndex idx = d->rootIndex(); if (d->proxyModel) { disconnect(d->proxyModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(_q_rowsInserted(const QModelIndex &))); } else { disconnect(d->model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(_q_rowsInserted(const QModelIndex &))); } if (proxyModel != 0) { proxyModel->setParent(this); d->proxyModel = proxyModel; proxyModel->setSourceModel(d->model); d->qFileDialogUi->listView->setModel(d->proxyModel); d->qFileDialogUi->treeView->setModel(d->proxyModel); connect(d->proxyModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(_q_rowsInserted(const QModelIndex &))); } else { d->proxyModel = 0; d->qFileDialogUi->listView->setModel(d->model); d->qFileDialogUi->treeView->setModel(d->model); connect(d->model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(_q_rowsInserted(const QModelIndex &))); } QItemSelectionModel *selModel = d->qFileDialogUi->treeView->selectionModel(); d->qFileDialogUi->treeView->setSelectionModel(d->qFileDialogUi->listView->selectionModel()); delete selModel; d->setRootIndex(idx); // reconnect selection QItemSelectionModel *selections = d->qFileDialogUi->listView->selectionModel(); QObject::connect(selections, SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)), this, SLOT(_q_selectionChanged())); QObject::connect(selections, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(_q_currentChanged(QModelIndex)));}/*! Returns the proxy model used by the file dialog. By default no proxy is set. \sa setProxyModel()*/QAbstractProxyModel *QFileDialog::proxyModel() const{ Q_D(const QFileDialog); return d->proxyModel;}#endif // QT_NO_PROXYMODEL/*! \internal Create tool buttons, set properties and connections*/void QFileDialogPrivate::createToolButtons(){ Q_Q(QFileDialog); qFileDialogUi->backButton->setIcon(q->style
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -