📄 q3filedialog.cpp
字号:
viewport()->setFocusProxy(lined); renaming = true;}void Q3FileDialogQFileListView::clear(){ cancelRename(); Q3ListView::clear();}void Q3FileDialogQFileListView::rename(){ if (!lined->text().isEmpty()) { QString file = currentItem()->text(0); if (lined->text() != file) filedialog->d->url.rename(file, lined->text()); } cancelRename();}void Q3FileDialogQFileListView::cancelRename(){ renameItem = 0; lined->parentWidget()->hide(); viewport()->setFocusProxy(this); renaming = false; if (currentItem()) currentItem()->repaint(); if (lined->hasFocus()) viewport()->setFocus();}void Q3FileDialogQFileListView::contentsMoved(int, int){ changeDirTimer->stop();#ifndef QT_NO_DRAGANDDROP setCurrentDropItem(QPoint(-1, -1));#endif}Q3FileDialogPrivate::File::~File(){ if (d->pendingItems.findRef(this)) d->pendingItems.removeRef(this);}QString Q3FileDialogPrivate::File::text(int column) const{ makeVariables(); switch(column) { case 0: return info.name(); case 1: if (info.isFile()) { QIODevice::Offset size = info.size(); return QString::number(size); } else { return QString::fromLatin1(""); } case 2: if (info.isFile() && info.isSymLink()) { return d->symLinkToFile; } else if (info.isFile()) { return d->file; } else if (info.isDir() && info.isSymLink()) { return d->symLinkToDir; } else if (info.isDir()) { return d->dir; } else if (info.isSymLink()) { return d->symLinkToSpecial; } else { return d->special; } case 3: { return info.lastModified().toString(Qt::LocalDate); } case 4: if (info.isReadable()) return info.isWritable() ? d->rw : d->ro; else return info.isWritable() ? d->wo : d->inaccessible; } return QString::fromLatin1("<--->");}const QPixmap * Q3FileDialogPrivate::File::pixmap(int column) const{ if (column) { return 0; } else if (Q3ListViewItem::pixmap(column)) { return Q3ListViewItem::pixmap(column); } else if (info.isSymLink()) { if (info.isFile()) return symLinkFileIcon; else return symLinkDirIcon; } else if (info.isDir()) { return closedFolderIcon; } else if (info.isFile()) { return fileIcon; } else { return fifteenTransparentPixels; }}Q3FileDialogPrivate::MCItem::MCItem(Q3ListBox * lb, Q3ListViewItem * item) : Q3ListBoxItem(){ i = item; if (lb) lb->insertItem(this);}Q3FileDialogPrivate::MCItem::MCItem(Q3ListBox * lb, Q3ListViewItem * item, Q3ListBoxItem *after) : Q3ListBoxItem(){ i = item; if (lb) lb->insertItem(this, after);}QString Q3FileDialogPrivate::MCItem::text() const{ return i->text(0);}const QPixmap *Q3FileDialogPrivate::MCItem::pixmap() const{ return i->pixmap(0);}int Q3FileDialogPrivate::MCItem::height(const Q3ListBox * lb) const{ int hf = lb->fontMetrics().height(); int hp = pixmap() ? pixmap()->height() : 0; return qMax(hf, hp) + 2;}int Q3FileDialogPrivate::MCItem::width(const Q3ListBox * lb) const{ QFontMetrics fm = lb->fontMetrics(); int w = 2; if (pixmap()) w += pixmap()->width() + 4; else w += 18; w += fm.width(text()); w += -fm.minLeftBearing(); w += -fm.minRightBearing(); w += 6; return w;}void Q3FileDialogPrivate::MCItem::paint(QPainter * ptr){ QFontMetrics fm = ptr->fontMetrics(); int h; if (pixmap()) h = qMax(fm.height(), pixmap()->height()) + 2; else h = fm.height() + 2; const QPixmap * pm = pixmap(); if (pm) ptr->drawPixmap(2, 1, *pm); ptr->drawText(pm ? pm->width() + 4 : 22, h - fm.descent() - 2, text());}static QStringList makeFiltersList(const QString &filter){ if (filter.isEmpty()) return QStringList(); int i = filter.indexOf(QLatin1String(";;"), 0); QString sep(QLatin1String(";;")); if (i == -1) { if (filter.contains(QLatin1Char('\n'))) { sep = QLatin1Char('\n'); i = filter.indexOf(sep); } } return QStringList::split(sep, filter);}/*! \class Q3FileDialog qfiledialog.h \brief The Q3FileDialog class provides dialogs that allow users to select files or directories. \compat The Q3FileDialog class enables a user to traverse their file system in order to select one or many files or a directory. The easiest way to create a Q3FileDialog 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 = Q3FileDialog::getOpenFileName( "/home", "Images (*.png *.xpm *.jpg)", this, "open file dialog", "Choose a file"); \endcode In the above example, a modal Q3FileDialog is created using a static function. The startup directory is set to "/home". The file filter is set to "Images (*.png *.xpm *.jpg)". The parent of the file dialog is set to \e this and it is given the identification name - "open file dialog". The caption at the top of file dialog is set to "Choose a file". If you want to use multiple filters, separate each one with \e two semicolons, e.g. \code "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)" \endcode You can create your own Q3FileDialog without using the static functions. By calling setMode(), you can set what can be returned by the Q3FileDialog. \code Q3FileDialog* fd = new Q3FileDialog(this, "file dialog", true); fd->setMode(Q3FileDialog::AnyFile); \endcode In the above example, the mode of the file dialog is set to \l 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 "File Save As" file dialog. Use \l ExistingFile if the user must select an existing file or \l Directory if only a directory may be selected. (See the \l Q3FileDialog::Mode 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, e.g. \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 Q3FileDialog. You can apply several filters by using setFilters() and add additional filters with addFilter(). Use setSelectedFilter() to select one of the filters you've given as the file dialog's default filter. Whenever the user changes the filter the filterSelected() signal is emitted. The file dialog has two view modes, Q3FileDialog::List which simply lists file and directory names and Q3FileDialog::Detail which displays additional information alongside each name, e.g. file size, modification date, etc. Set the mode with setViewMode(). \code fd->setViewMode(Q3FileDialog::Detail); \endcode The last important function you will need to use when creating your own file dialog is selectedFile(). \code QString fileName; if (fd->exec() == QDialog::Accepted) fileName = fd->selectedFile(); \endcode In the above example, a modal file dialog is created and shown. If the user clicked OK, then the file they selected is put in \c fileName. If you are using the \l ExistingFiles mode then you will need to use selectedFiles() which will return the selected files in a QStringList. The dialog's working directory can be set with setDir(). The display of hidden files is controlled with setShowHiddenFiles(). The dialog can be forced to re-read the directory with rereadDir() and re-sort the directory with resortDir(). All the files in the current directory can be selected with selectAll(). \section1 Creating and using preview widgets There are two kinds of preview widgets that can be used with Q3FileDialogs: \e content preview widgets and \e information preview widgets. They are created and used in the same way except that the function names differ, e.g. setContentsPreview() and setInfoPreview(). A preview widget is a widget that is placed inside a Q3FileDialog so that the user can see either the contents of the file, or information about the file. \code class Preview : public QLabel, public Q3FilePreview { public: Preview(QWidget *parent=0) : QLabel(parent) {} void previewUrl(const Q3Url &u) { QString path = u.path(); QPixmap pix(path); if (pix.isNull()) setText("This is not a pixmap"); else setPixmap(pix); } }; \endcode In the above snippet, we create a preview widget which inherits from QLabel and Q3FilePreview. File preview widgets \e must inherit from Q3FilePreview. Inside the class we reimplement Q3FilePreview::previewUrl(), this is where we determine what happens when a file is selected. In the above example we only show a preview of the file if it is a valid pixmap. Here's how to make a file dialog use a preview widget: \code Preview* p = new Preview; Q3FileDialog* fd = new Q3FileDialog(this); fd->setContentsPreviewEnabled(true); fd->setContentsPreview(p, p); fd->setPreviewMode(Q3FileDialog::Contents); fd->show(); \endcode The first line creates an instance of our preview widget. We then create our file dialog and call setContentsPreviewEnabled(true), this tell the file dialog to preview the contents of the currently selected file. We then call setContentsPreview() -- note that we pass the same preview widget twice. Finally, before showing the file dialog, we call setPreviewMode() setting the mode to \e Contents which will show the contents preview of the file that the user has selected. If you create another preview widget that is used for displaying information about a file, create it in the same way as the contents preview widget and call setInfoPreviewEnabled(), and setInfoPreview(). Then the user will be able to switch between the two preview modes. For more information about creating a Q3FilePreview widget see \l{Q3FilePreview}.*//*! \enum Q3FileDialog::Mode 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. See setMode().*//*! \enum Q3FileDialog::ViewMode This enum describes the view mode of the file dialog, i.e. what information about each file will be displayed. \value List Display file and directory names with icons. \value Detail Display file and directory names with icons plus additional information, such as file size and modification date. See setViewMode().*//*! \enum Q3FileDialog::PreviewMode This enum describes the preview mode of the file dialog. \value NoPreview No preview is shown at all. \value Contents Show a preview of the contents of the current file using the contents preview widget. \value Info Show information about the current file using the info preview widget. See setPreviewMode(), setContentsPreview() and setInfoPreview().*//*! \fn void Q3FileDialog::detailViewSelectionChanged() \internal*//*! \fn void Q3FileDialog::listBoxSelectionChanged() \internal*/extern const char qt3_file_dialog_filter_reg_exp[] = "([a-zA-Z0-9]*)\\(([a-zA-Z0-9_.*? +;#\\[\\]]*)\\)$";/*! Constructs a file dialog called \a name, with the parent, \a parent. If \a modal is true then the file dialog is modal; otherwise it is modeless.*/Q3FileDialog::Q3FileDialog(QWidget *parent, const char *name, bool modal) : QDialog(parent, name, modal, (modal ? (Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_Title | Qt::WStyle_SysMenu) : Qt::WindowFlags(0))){ init(); d->mode = ExistingFile; d->types->insertItem(tr("All Files (*)")); d->cursorOverride = false; emit dirEntered(d->url.dirPath()); rereadDir();}/*! Constructs a file dialog called \a name with the parent, \a parent. If \a modal is true then the file dialog is modal; otherwise it is modeless. If \a dirName is specified then it will be used as the dialog's working directory, i.e. it will be the directory that is shown when the dialog appears. If \a filter is specified it will be used as the dialog's file filter.*/Q3FileDialog::Q3FileDialog(const QString& dirName, const QString & filter, QWidget *parent, const char *name, bool modal) : QDialog(parent, name, modal, (modal ? (Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_Title | Qt::WStyle_SysMenu) : Qt::WindowFlags(0))){ init(); d->mode = ExistingFile; rereadDir(); Q3UrlOperator u(dirName); if (!dirName.isEmpty() && (!u.isLocalFile() || QDir(dirName).exists())) setSelection(dirName); else if (workingDirectory && !workingDirectory->isEmpty()) setDir(*workingDirectory); if (!filter.isEmpty()) { setFilters(filter); if (!dirName.isEmpty()) { int dotpos = dirName.indexOf(QLatin1Char('.'), 0, Qt::CaseInsensitive); if (dotpos != -1) { for (int b=0 ; b<d->types->count() ; b++) { if (d->types->text(b).contains(dirName.right(dirName.length() - dotpos))) { d->types->setCurrentItem(b); setFilter(d->types->text(b)); return; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -