qfiledialog.cpp
来自「qt-x11-opensource-src-4.1.4.tar.gz源码」· C++ 代码 · 共 2,073 行 · 第 1/5 页
CPP
2,073 行
}/*! \brief returns the text shown in the filedialog in the specified \a label*/QString QFileDialog::labelText(DialogLabel label) const{ Q_D(const QFileDialog); switch (label) { case LookIn: return d->lookInLabel->text(); case FileName: return d->fileNameLabel->text(); case FileType: return d->fileTypeLabel->text(); case Accept: return d->acceptButton->text(); case Reject: return d->rejectButton->text(); } 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 fn = d->fileNameEdit->text(); // special case for ".." if (fn == "..") { d->_q_navigateToParent(); bool block = d->fileNameEdit->blockSignals(true); d->fileNameEdit->setText(fn); d->fileNameEdit->selectAll(); d->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()) { QString message = tr("\nFile not found.\nPlease verify the " "correct file name was given"); QMessageBox::warning(this, d->acceptButton->text(), info.fileName() + message); 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->fileNameEdit->selectAll(); return; } // check if we have to ask for permission to overwrite the file if (!info.exists() || !confirmOverwrite() || acceptMode() == AcceptOpen) QDialog::accept(); else if (QMessageBox::warning(this, windowTitle(), tr("%1 already exists.\nDo you want to replace it?") .arg(info.fileName()), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) QDialog::accept(); 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()) { QString message = tr("%1\nFile not found.\nPlease verify the " "correct file name was given."); QMessageBox::warning(this, d->acceptButton->text(), message.arg(info.fileName())); return; } if (info.isDir()) { setDirectory(info.absoluteFilePath()); d->fileNameEdit->selectAll(); return; } } emit filesSelected(files); QDialog::accept(); return; }}/*! \internal Private object constructor.*/QFileDialogPrivate::QFileDialogPrivate() : QDialogPrivate(), model(0), listView(0), treeView(0), fileMode(QFileDialog::AnyFile), acceptMode(QFileDialog::AcceptOpen), confirmOverwrite(true), lookInCombo(0), fileNameEdit(0), fileTypeCombo(0), openAction(0), renameAction(0), deleteAction(0), reloadAction(0), sortByNameAction(0), sortBySizeAction(0), sortByDateAction(0), unsortedAction(0), showHiddenAction(0), acceptButton(0), rejectButton(0), backButton(0), toParentButton(0), newFolderButton(0), detailModeButton(0), listModeButton(0), lookInLabel(0), fileNameLabel(0), fileTypeLabel(0){}/*! \internal Refreshes the display of the current directory in the dialog.*/void QFileDialogPrivate::_q_reload(){ model->refresh(rootIndex());}/*! \internal Navigates to the last directory viewed in the dialog.*/void QFileDialogPrivate::_q_navigateToPrevious(){ if (!history.isEmpty()) { QString path = history.back(); history.pop_back(); QModelIndex root = model->index(path); setRootIndex(root); updateButtons(root); }}/*! \internal Navigates to the parent directory of the currently displayed directory in the dialog.*/void QFileDialogPrivate::_q_navigateToParent(){ QPersistentModelIndex index = rootIndex(); QString path = model->filePath(index); history.push_back(path); QModelIndex parent = model->parent(index); setRootIndex(parent); updateButtons(parent);}/*! \internal This is called when the user double clicks on a file with the corresponding model item \a index.*/void QFileDialogPrivate::_q_enterDirectory(const QModelIndex &index){ Q_Q(QFileDialog); // if it is "My Computer" or a directory, enter it QPersistentModelIndex persistent(index); if (!persistent.isValid() || model->isDir(persistent)) { QString path = model->filePath(rootIndex()); history.push_back(path); setRootIndex(persistent); updateButtons(persistent); } else { q->accept(); }}/*! \internal Changes the file dialog's current directory to the one specified by \a path.*/void QFileDialogPrivate::_q_enterDirectory(const QString &path){ Q_Q(QFileDialog); QPersistentModelIndex index = model->index(path); if (!index.isValid()) index = model->index(getEnvironmentVariable(path)); if (index.isValid() || path.isEmpty() || path == QFileDialog::tr("My Computer")) { _q_enterDirectory(index); } else { QString message = QFileDialog::tr("%1\nDirectory not found.\nPlease verify the " "correct directory name was given."); QMessageBox::warning(q, q->windowTitle(), message.arg(path)); }}/*! \internal Sets the current directory to the path showed in the "look in" combobox. Called when the enterPressed() signal is emitted.*/void QFileDialogPrivate::_q_enterDirectory(){ QString path = toInternal(lookInCombo->currentText()); _q_enterDirectory(path);}/*! \internal Displays the contents of the current directory in the form of a list of icons and names. \sa ViewMode*/void QFileDialogPrivate::_q_showList(){ listModeButton->setDown(true); detailModeButton->setDown(false); listView->show(); listView->doItemsLayout(); treeView->hide();}/*! \internal Displays the contents of the current directory in the form of a list of icons and names, alongside which are displayed further details of each item. \sa ViewMode*/void QFileDialogPrivate::_q_showDetails(){ listModeButton->setDown(false); detailModeButton->setDown(true); treeView->show(); treeView->doItemsLayout(); listView->hide();}/*! \internal Includes hidden files and directories in the items displayed in the dialog.*/void QFileDialogPrivate::_q_showHidden(){ QDir::Filters filters = model->filter(); if (showHiddenAction->isChecked()) filters |= QDir::Hidden; else filters &= ~(int)QDir::Hidden; setDirFilter(filters);}/*! \internal Adds the given \a filter to the file dialog's name filter. (Actually the name filters are held in the underlying model and this is what is really updated.)*/void QFileDialogPrivate::_q_useFilter(const QString &filter){ QStringList filters = qt_clean_filter_list(filter); // If acceptMode is AcceptSave, replace the file extension // in the fileNameEdit with the new filter extension. if (acceptMode == QFileDialog::AcceptSave) { QString filterExtension; if (filters.count() > 0) filterExtension = QFileInfo(filters.at(0)).suffix(); QString fileNameText = fileNameEdit->text(); const QString fileNameExtension = QFileInfo(fileNameText).suffix(); if (fileNameExtension.isEmpty() == false && filterExtension.isEmpty() == false) { const int fileNameExtensionLenght = fileNameExtension.count(); fileNameText.replace(fileNameText.count() - fileNameExtensionLenght, fileNameExtensionLenght, filterExtension); fileNameEdit->setText(fileNameText); } } model->setNameFilters(filters); model->refresh(rootIndex());}/*! \internal This is called when the model index corresponding to the current file is changed from \a index to \a current.*/void QFileDialogPrivate::_q_updateFileName(const QItemSelection &selection){ Q_UNUSED(selection); if (fileNameEdit->hasFocus()) return; // the selection changed because of autocompletion QString text; QModelIndexList indexes = selections->selectedIndexes(); QList<QModelIndex>::iterator it = indexes.begin(); while (it != indexes.end()) { // remove all indexes except column 0 if ((*it).column() != 0) it = indexes.erase(it); else ++it; } bool stripDirs = false; if (fileMode != QFileDialog::DirectoryOnly && fileMode != QFileDialog::Directory) stripDirs = true; // when selecting files, strip out the directories bool addQuotes = indexes.count() > 1; for (int i = 0; i < indexes.count(); ++i) { QString name = model->data(indexes.at(i)).toString(); if (stripDirs && model->isDir(indexes[i])) continue; if (addQuotes) text.append("\""); text.append(name); if (addQuotes) text.append("\" "); } if (!text.isEmpty()) fileNameEdit->setText(text);}/*! \internal This is called when the text in the filename lineedit is changed; the new text is passed as \a text. This function provides autocompletion for filenames.*/void QFileDialogPrivate::_q_autoCompleteFileName(const QString &text){ Q_Q(QFileDialog); // if we hanve no filename or the last character is '/', then don't autocomplete if (text.isEmpty() || text.at(text.length() - 1) == QDir::separator()) return;#ifdef Q_OS_WIN // autocompleting the <host> part of UNC paths is just too slow if (text.startsWith("\\\\") || text.startsWith("//")) return;#endif // the user is not typing or the text is a valid file, there is no need for autocompletion if (!fileNameEdit->hasFocus()) return; QFileInfo info(toInternal(text)); QFileInfo absoluteInfo; if (info.isAbsolute()) absoluteInfo = info; else absoluteInfo = QFileInfo(toInternal(q->directory().absolutePath() + QDir::separator() + text)); if (absoluteInfo.exists()) { QModelIndex index = model->index(absoluteInfo.absoluteFilePath()); if (index.isValid()) treeView->setCurrentIndex(index); else selections->clear();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?