本页内容

Core::EditorManager Class

class Core::EditorManager

EditorManager 类根据文件的 MIME 类型来管理为这些文件创建的编辑器。更多内容...

Header: #include <coreplugin/editormanager/editormanager.h>
Inherits: QObject

公共类型

enum OpenEditorFlag { NoFlags, DoNotChangeCurrentEditor, IgnoreNavigationHistory, DoNotMakeVisible, OpenInOtherSplit, …, AllowExternalEditor }
flags OpenEditorFlags

公共槽

信号

void aboutToSave(Core::IDocument *document, Core::IDocument::SaveOption option)
void autoSaved()
void currentDocumentStateChanged()
void currentEditorAboutToChange(Core::IEditor *editor)
void currentEditorChanged(Core::IEditor *editor)
void documentClosed(Core::IDocument *document)
void documentOpened(Core::IDocument *document)
void documentStateChanged(Core::IDocument *document)
void editorAboutToClose(Core::IEditor *editor)
void editorCreated(Core::IEditor *editor, const Utils::FilePath &filePath)
void editorOpened(Core::IEditor *editor)
void editorsClosed(QList<Core::IEditor *> editors)
void saved(Core::IDocument *document, Core::IDocument::SaveOption option)

静态公共成员

void activateEditor(Core::IEditor *editor, Core::EditorManager::OpenEditorFlags flags = NoFlags)
Core::IEditor *activateEditorForDocument(Core::IDocument *document, Core::EditorManager::OpenEditorFlags flags = NoFlags)
void activateEditorForEntry(Core::DocumentModel::Entry *entry, Core::EditorManager::OpenEditorFlags flags = NoFlags)
void addCloseEditorListener(const std::function<bool (IEditor *)> &listener)
void addCurrentPositionToNavigationHistory(const QByteArray &saveState = QByteArray())
void addEditor(Core::IEditor *editor, Core::EditorManager::OpenEditorFlags flags = NoFlags)
void clearUniqueId(Core::IDocument *document)
bool closeAllDocuments()
bool closeDocuments(const QList<Core::DocumentModel::Entry *> &entries)
bool closeDocuments(const QList<Core::IDocument *> &documents, bool askAboutModifiedEditors = true)
bool closeEditors(const QList<Core::IEditor *> &editorsToClose, bool askAboutModifiedEditors = true)
void closeOtherDocuments(Core::IDocument *document)
Core::IDocument *currentDocument()
Core::IEditor *currentEditor()
void cutForwardNavigationHistory()
Utils::TextFileFormat::LineTerminationMode defaultLineEnding()
Utils::TextEncoding defaultTextEncoding()
Utils::FilePaths getOpenFilePaths(int options = {})
Core::EditorManager *instance()
bool isAutoSaveFile(const QString &filePath)
qint64 maxTextFileSize()
Core::IEditor *openEditor(const Utils::FilePath &filePath, Utils::Id editorId = {}, Core::EditorManager::OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr)
Core::IEditor *openEditorAt(const Utils::Link &link, Utils::Id editorId = {}, Core::EditorManager::OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr)
void openEditorAtSearchResult(const Utils::SearchResultItem &item, Utils::Id editorId = {}, Core::EditorManager::OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr)
Core::IEditor *openEditorWithContents(Utils::Id editorId, QString *titlePattern = nullptr, const QByteArray &contents = QByteArray(), const QString &uniqueId = QString(), Core::EditorManager::OpenEditorFlags flags = NoFlags)
bool openExternalEditor(const Utils::FilePath &filePath, Utils::Id editorId)
void populateOpenWithMenu(QMenu *menu, const Utils::FilePath &filePath)
Core::IDocument::ReloadSetting reloadSetting()
bool saveDocument(Core::IDocument *document)
void setLastEditLocation(const Core::IEditor *editor)
bool skipOpeningBigTextFile(const Utils::FilePath &filePath)
QList<Core::IEditor *> visibleEditors()

详细说明

每当用户想要编辑或创建文件时,EditorManager 会扫描所有 `IEditorFactory ` 接口以查找合适的编辑器。随后,系统会根据文件的 MIME 类型,要求选定的 `IEditorFactory ` 创建一个编辑器。

用户可以拆分编辑器视图,或在新窗口中打开编辑器,以便在同一屏幕或多台屏幕上处理和查看多个文件。有关更多信息,请参阅“拆分编辑器视图”。

插件使用 EditorManager 在编辑器中打开或关闭文档,并在文档被打开、关闭或保存时接收通知。

成员类型文档

enum EditorManager::OpenEditorFlag
flags EditorManager::OpenEditorFlags

此枚举指定在编辑器中打开文件的设置。

常量值描述
Core::EditorManager::NoFlags0不使用任何设置。
Core::EditorManager::DoNotChangeCurrentEditor1不将焦点切换到新打开的编辑器。
Core::EditorManager::IgnoreNavigationHistory2不会为已打开的编辑器向导航历史记录中添加条目。
Core::EditorManager::DoNotMakeVisible4不强制使编辑器显示。
Core::EditorManager::OpenInOtherSplit8在窗口的另一个分屏区域中打开文档。
Core::EditorManager::DoNotSwitchToDesignMode16以当前模式打开文档。
Core::EditorManager::DoNotSwitchToEditMode32在当前模式下打开文档。
Core::EditorManager::SwitchSplitIfAlreadyVisible64如果文档已在另一个分屏中显示,则切换到该分屏。
Core::EditorManager::DoNotRaise128防止将Qt Creator 窗口调至前台。
Core::EditorManager::AllowExternalEditor256允许在外部编辑器中打开文件。

OpenEditorFlags 类型是QFlags<OpenEditorFlag> 的 typedef。它存储了 OpenEditorFlag 值的按“或”运算组合。

成员函数文档

[signal] void EditorManager::aboutToSave(Core::IDocument *document, Core::IDocument::SaveOption option)

在通过 `option` 选项保存 `document ` 之前,会触发此信号。

[static] void EditorManager::activateEditor(Core::IEditor *editor, Core::EditorManager::OpenEditorFlags flags = NoFlags)

使用指定的flags ,在当前活动的分屏中启用editor 。

另请参阅 currentEditor()。

[static] Core::IEditor *EditorManager::activateEditorForDocument(Core::IDocument *document, Core::EditorManager::OpenEditorFlags flags = NoFlags)

使用指定的flags ,在当前拆分窗口中为document 激活一个编辑器。

[static] void EditorManager::activateEditorForEntry(Core::DocumentModel::Entry *entry, Core::EditorManager::OpenEditorFlags flags = NoFlags)

使用指定的flags ,在当前拆分窗口中为entry 指定的文档激活编辑器。

[static] void EditorManager::addCloseEditorListener(const std::function<bool (IEditor *)> &listener)

将 `listener ` 添加到用于判断是否可关闭编辑器的钩子中。

当编辑器请求关闭时,将调用所有监听器。如果其中一个调用返回false ,则终止该过程并忽略该事件。如果所有调用均返回true ,则触发editorAboutToClose() 并接受该事件。

[static] void EditorManager::addCurrentPositionToNavigationHistory(const QByteArray &saveState = QByteArray())

将由 `saveState ` 指定的当前光标位置添加到导航历史记录中。如果 `saveState ` 为 `null `(默认值),则使用当前活动编辑器的当前状态。否则,`saveState ` 必须是活动编辑器的一个有效状态。

另请参阅 IEditor::saveState()。

[static] void EditorManager::addEditor(Core::IEditor *editor, Core::EditorManager::OpenEditorFlags flags = NoFlags)

将一个IEditor 实例editor 告知EditorManager (该实例此前未被其识别),并使用flags 将其激活。

[signal] void EditorManager::autoSaved()

该信号是在自动保存触发后发出的。

[static] void EditorManager::clearUniqueId(Core::IDocument *document)

清除document 的唯一标识符。

另请参阅 openEditorWithContents()。

[static] bool EditorManager::closeAllDocuments()

关闭所有打开的文档,但固定文件除外。

返回是否已关闭所有编辑器。

[static slot] bool EditorManager::closeAllEditors(bool askAboutModifiedEditors = true)

关闭所有打开的编辑器。如果askAboutModifiedEditors 的值为true ,则在关闭编辑器前会提示用户保存更改。

返回是否已关闭所有编辑器。

[static] bool EditorManager::closeDocuments(const QList<Core::DocumentModel::Entry *> &entries)

关闭由entries 指定的文档。

返回所有文档是否均已关闭。

[static] bool EditorManager::closeDocuments(const QList<Core::IDocument *> &documents, bool askAboutModifiedEditors = true)

关闭documents 。如果askAboutModifiedEditors 的值为true ,则在关闭文档前会提示用户保存更改。

返回文档是否已关闭。

[static] bool EditorManager::closeEditors(const QList<Core::IEditor *> &editorsToClose, bool askAboutModifiedEditors = true)

关闭由editorsToClose 指定的编辑器。如果askAboutModifiedEditors 的值为true ,则在关闭编辑器前会提示用户保存更改。

返回所有编辑器是否均已关闭。

通常,closeDocuments() 是更好的选择。

另请参阅 closeDocuments()。

[static slot] void EditorManager::closeOtherDocuments()

关闭除当前文档以外的所有打开的文档。

注意:此 插槽已被重载。要连接到此插槽:

// Connect using qOverload:
connect(sender, &SenderClass::signal,
        editorManager, qOverload<>(&EditorManager::closeOtherDocuments));

// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
        editorManager, [receiver = editorManager]() { receiver->closeOtherDocuments(); });
有关更多示例和方法,请参阅“连接到重载的槽”。

[static] void EditorManager::closeOtherDocuments(Core::IDocument *document)

关闭所有打开的文档,但document 和已固定的文件除外。

[static] Core::IDocument *EditorManager::currentDocument()

返回当前活动编辑器的文档。

另请参阅 currentEditor()。

[signal] void EditorManager::currentDocumentStateChanged()

当当前文档的元数据(例如文件名或修改状态)发生变化时,会发出此信号。

另请参阅 IDocument::changed()。

[static] Core::IEditor *EditorManager::currentEditor()

返回当前活动的编辑器。

另请参阅 ` currentDocument()`。

[signal] void EditorManager::currentEditorAboutToChange(Core::IEditor *editor)

该信号是在当前编辑器切换为editor 之前发出的。

[signal] void EditorManager::currentEditorChanged(Core::IEditor *editor)

当当前编辑器切换为editor 后,将发出此信号。

[static] void EditorManager::cutForwardNavigationHistory()

删除导航历史记录的前一部分,因此用户将无法再访问Go Forward (除非用户再次向后翻页)。

另请参阅 goForwardInNavigationHistory() 和addCurrentPositionToNavigationHistory()。

[static] Utils::TextFileFormat::LineTerminationMode EditorManager::defaultLineEnding()

返回用户在设置中指定的默认行结束符。

[static] Utils::TextEncoding EditorManager::defaultTextEncoding()

返回用户在设置中指定的默认文本编码。

[signal] void EditorManager::documentClosed(Core::IDocument *document)

该信号在document 关闭后、但被删除之前触发。

[signal] void EditorManager::documentOpened(Core::IDocument *document)

当document 的第一个编辑器在编辑器视图中打开后,会触发此信号。

[signal] void EditorManager::documentStateChanged(Core::IDocument *document)

当document 的元数据(例如文件名或修改状态)发生变化时,会发出此信号。

另请参阅 IDocument::changed()。

[signal] void EditorManager::editorAboutToClose(Core::IEditor *editor)

该信号在关闭editor 之前触发。可用于单独释放为编辑器分配的资源,而无需关闭编辑器本身。它无法用于阻止编辑器关闭。有关阻止编辑器关闭的方法,请参阅addCloseEditorListener()。

通常,更合适的监听信号是documentClosed()。

另请参阅 addCloseEditorListener()。

[signal] void EditorManager::editorCreated(Core::IEditor *editor, const Utils::FilePath &filePath)

当为位于filePath 的文件创建了editor 后,但在该文件在编辑器视图中被打开之前,会发出此信号。

[signal] void EditorManager::editorOpened(Core::IEditor *editor)

当在编辑器视图中打开一个新的editor 后,会触发此信号。

通常,更适合监听的信号是documentOpened()。

[signal] void EditorManager::editorsClosed(QList<Core::IEditor *> editors)

该信号在editors 关闭后、但被删除之前触发。

通常,更合适的监听信号是 `documentClosed()`。

[static] Utils::FilePaths EditorManager::getOpenFilePaths(int options = {})

请求用户提供要打开的文件列表,并返回所选文件。

options 参数包含有关如何运行对话框的各种选项。有关可传递标志的更多信息,请参阅QFileDialog::Options 枚举。

另请参阅 DocumentManager::getOpenFileNames() 和QFileDialog::Options 。

[static slot] void EditorManager::goBackInNavigationHistory()

返回导航历史记录。

另请参阅 goForwardInNavigationHistory() 和addCurrentPositionToNavigationHistory()。

[static slot] void EditorManager::goForwardInNavigationHistory()

在导航历史记录中向前翻页。

另请参阅 goBackInNavigationHistory() 和addCurrentPositionToNavigationHistory()。

[static slot] void EditorManager::gotoOtherSplit()

将焦点移至另一个分区,如有必要则创建该分区。如果不存在分区且没有其他窗口,则创建并排分区。如果当前窗口已分区,则将焦点移至该窗口内的下一个分区,并按顺序循环。如果当前窗口未分区,则将焦点移至下一个窗口。

[static] Core::EditorManager *EditorManager::instance()

返回该实例的指针。仅用于连接信号。

[static] bool EditorManager::isAutoSaveFile(const QString &filePath)

返回filePath 是否为由Qt Creator 生成的自动保存文件。

[static] qint64 EditorManager::maxTextFileSize()

返回文本编辑器中应打开的文件最大大小。

[static] Core::IEditor *EditorManager::openEditor(const Utils::FilePath &filePath, Utils::Id editorId = {}, Core::EditorManager::OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr)

使用编辑器类型 `editorId ` 以及指定的 `flags` 打开由 `filePath ` 指定的文档。

如果editorId 为Id() ,则编辑器类型由文件的 MIME 类型派生而来。

如果newEditor 不是nullptr ,且创建了一个新的编辑器实例,则将其设置为true 。如果使用的是现有的编辑器实例,则将其设置为false 。

另请参阅 openEditorAt()、openEditorWithContents(),以及openExternalEditor()。

[static] Core::IEditor *EditorManager::openEditorAt(const Utils::Link &link, Utils::Id editorId = {}, Core::EditorManager::OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr)

使用编辑器类型editorId 和指定的flags 打开由link 指定的文档。

将文本光标移至link 中指定的行和列。

如果editorId 为Id() ,则编辑器类型由文件的 MIME 类型派生而来。

如果 `newEditor ` 不是 `nullptr`,并且创建了一个新的编辑器实例,则将其设置为 `true`。如果使用了现有的编辑器实例,则将其设置为 `false`。

另请参阅 openEditor()、openEditorAtSearchResult()、openEditorWithContents()、openExternalEditor() 和IEditor::gotoLine()。

[static] void EditorManager::openEditorAtSearchResult(const Utils::SearchResultItem &item, Utils::Id editorId = {}, Core::EditorManager::OpenEditorFlags flags = NoFlags, bool *newEditor = nullptr)

使用编辑器类型editorId 和指定的flags ,在搜索结果item 的位置打开文档。

如果editorId 为Id() ,则编辑器类型由文件的 MIME 类型推导而来。

如果newEditor 不是nullptr ,且创建了一个新的编辑器实例,则将其设置为true 。如果使用了现有的编辑器实例,则将其设置为false 。

另请参阅 openEditorAt()。

[static] Core::IEditor *EditorManager::openEditorWithContents(Utils::Id editorId, QString *titlePattern = nullptr, const QByteArray &contents = QByteArray(), const QString &uniqueId = QString(), Core::EditorManager::OpenEditorFlags flags = NoFlags)

使用指定的flags 在类型为editorId 的编辑器中打开contents 。

编辑器的显示名称基于titlePattern 生成。如果指定了非空的uniqueId ,且找到了具有该唯一 ID 的编辑器,则会复用该编辑器。否则,将创建一个具有该唯一 ID 的新编辑器。

返回新创建或复用的编辑器。

另请参阅 clearUniqueId()。

[static] bool EditorManager::openExternalEditor(const Utils::FilePath &filePath, Utils::Id editorId)

在由editorId 指定的外部编辑器中打开由filePath 指定的文档。

如果editorId 不是外部编辑器的 ID,或者无法打开该外部编辑器,则返回false 并显示一条错误消息。

另请参阅 openEditor()。

[static] void EditorManager::populateOpenWithMenu(QMenu *menu, const Utils::FilePath &filePath)

将适合打开文档filePath 的编辑器添加到“Open With ”菜单menu 中。

[static] Core::IDocument::ReloadSetting EditorManager::reloadSetting()

返回重新加载行为的设置。

[static slot] void EditorManager::revertToSaved()

将当前文档恢复到上次保存时的状态。

[static slot] void EditorManager::saveDocument()

保存当前文档。

注意:此 插槽已被重载。要连接到此插槽:

// Connect using qOverload:
connect(sender, &SenderClass::signal,
        editorManager, qOverload<>(&EditorManager::saveDocument));

// Or using a lambda as wrapper:
connect(sender, &SenderClass::signal,
        editorManager, [receiver = editorManager]() { receiver->saveDocument(); });
有关更多示例和方法,请参阅“连接到重载的槽”。

[static] bool EditorManager::saveDocument(Core::IDocument *document)

将更改保存到document 中。

返回该操作是否成功。

[static slot] void EditorManager::saveDocumentAs()

将当前文档以另一个文件名保存。

[static slot] void EditorManager::saveDocumentWithoutFormatting()

将当前文档保存为无自动格式化的文件。

[signal] void EditorManager::saved(Core::IDocument *document, Core::IDocument::SaveOption option)

当使用保存选项 `option` 保存 `document ` 后,会发出此信号。

[static] void EditorManager::setLastEditLocation(const Core::IEditor *editor)

将最后修改的位置设置为editor 。用于Window >Go to Last Edit 。

[static] bool EditorManager::skipOpeningBigTextFile(const Utils::FilePath &filePath)

返回是否应打开由 `filePath ` 指定的文档,即使该文档体积较大。根据设置不同,系统可能会提示用户决定是否打开该文件。

[static slot] void EditorManager::split()

将编辑器视图垂直拆分为堆叠视图。

[static slot] void EditorManager::splitSideBySide()

将编辑器视图水平拆分为相邻的视图。

[static] QList<Core::IEditor *> EditorManager::visibleEditors()

返回可见编辑器的列表。

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.