diff options
author | Eike Ziller <eike.ziller@digia.com> | 2014-07-23 15:36:55 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@digia.com> | 2014-08-13 15:47:58 +0200 |
commit | 49a8cf44daa965d421466fb9c922b08594658e2d (patch) | |
tree | 85af246435cd46478b15cb25e17785578b442003 /src/plugins/coreplugin | |
parent | 2a9a014c9d78b902da6332f081b37a830bb0541d (diff) | |
download | qt-creator-49a8cf44daa965d421466fb9c922b08594658e2d.tar.gz |
Editors: Set window title for external editor windows
Change-Id: I54ed77c0f1b2122ae8833109d8dcac7d8eec7ac4
Task-number: QTCREATORBUG-9612
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
Diffstat (limited to 'src/plugins/coreplugin')
9 files changed, 168 insertions, 53 deletions
diff --git a/src/plugins/coreplugin/editormanager/editorarea.cpp b/src/plugins/coreplugin/editormanager/editorarea.cpp index 54c9f8b21f..b5f8956a44 100644 --- a/src/plugins/coreplugin/editormanager/editorarea.cpp +++ b/src/plugins/coreplugin/editormanager/editorarea.cpp @@ -29,26 +29,99 @@ #include "editorarea.h" +#include "editormanager.h" +#include "ieditor.h" + #include <coreplugin/coreconstants.h> #include <coreplugin/icontext.h> #include <coreplugin/icore.h> +#include <utils/qtcassert.h> + +#include <QApplication> namespace Core { namespace Internal { EditorArea::EditorArea() + : m_currentView(0), + m_currentDocument(0) { m_context = new IContext; m_context->setContext(Context(Constants::C_EDITORMANAGER)); m_context->setWidget(this); ICore::addContextObject(m_context); + + setCurrentView(view()); + + connect(qApp, &QApplication::focusChanged, + this, &EditorArea::focusChanged); } EditorArea::~EditorArea() { + // disconnect + setCurrentView(0); + disconnect(qApp, &QApplication::focusChanged, + this, &EditorArea::focusChanged); + ICore::removeContextObject(m_context); delete m_context; } +IDocument *EditorArea::currentDocument() const +{ + return m_currentDocument; +} + +void EditorArea::focusChanged(QWidget *old, QWidget *now) +{ + Q_UNUSED(old) + // only interesting if the focus moved within the editor area + if (!focusWidget() || focusWidget() != now) + return; + // find the view with focus + EditorView *current = findFirstView(); + while (current) { + if (current->focusWidget() && current->focusWidget() == now) { + setCurrentView(current); + break; + } + current = current->findNextView(); + } +} + +void EditorArea::setCurrentView(EditorView *view) +{ + if (view == m_currentView) + return; + if (m_currentView) { + disconnect(m_currentView.data(), &EditorView::currentEditorChanged, + this, &EditorArea::updateCurrentEditor); + } + m_currentView = view; + if (m_currentView) { + connect(m_currentView.data(), &EditorView::currentEditorChanged, + this, &EditorArea::updateCurrentEditor); + } + updateCurrentEditor(m_currentView ? m_currentView->currentEditor() : 0); +} + +void EditorArea::updateCurrentEditor(IEditor *editor) +{ + IDocument *document = editor ? editor->document() : 0; + if (document == m_currentDocument) + return; + if (m_currentDocument) { + disconnect(m_currentDocument.data(), &IDocument::changed, + this, &EditorArea::windowTitleNeedsUpdate); + } + m_currentDocument = document; + if (m_currentDocument) { + connect(m_currentDocument.data(), &IDocument::changed, + this, &EditorArea::windowTitleNeedsUpdate); + } + emit windowTitleNeedsUpdate(); +} + } // Internal } // Core diff --git a/src/plugins/coreplugin/editormanager/editorarea.h b/src/plugins/coreplugin/editormanager/editorarea.h index 0bdffbfb69..2c9d2e04f9 100644 --- a/src/plugins/coreplugin/editormanager/editorarea.h +++ b/src/plugins/coreplugin/editormanager/editorarea.h @@ -32,6 +32,8 @@ #include "editorview.h" +#include <QPointer> + namespace Core { class IContext; @@ -46,8 +48,19 @@ public: EditorArea(); ~EditorArea(); + IDocument *currentDocument() const; + +signals: + void windowTitleNeedsUpdate(); + private: + void focusChanged(QWidget *old, QWidget *now); + void setCurrentView(EditorView *view); + void updateCurrentEditor(IEditor *editor); + IContext *m_context; + QPointer<EditorView> m_currentView; + QPointer<IDocument> m_currentDocument; }; } // Internal diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index afffbd9f55..9e5d7715ef 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -451,6 +451,8 @@ void EditorManagerPrivate::init() // other setup auto mainEditorArea = new EditorArea(); mainEditorArea->hide(); + connect(mainEditorArea, &EditorArea::windowTitleNeedsUpdate, + this, &EditorManagerPrivate::updateWindowTitle); connect(mainEditorArea, SIGNAL(destroyed(QObject*)), this, SLOT(editorAreaDestroyed(QObject*))); d->m_editorAreas.append(mainEditorArea); d->m_currentView = mainEditorArea->view(); @@ -491,7 +493,7 @@ void EditorManagerPrivate::init() }); } -QWidget *EditorManagerPrivate::mainEditorArea() +EditorArea *EditorManagerPrivate::mainEditorArea() { return d->m_editorAreas.at(0); } @@ -1065,7 +1067,6 @@ void EditorManagerPrivate::setCurrentEditor(IEditor *editor, bool ignoreNavigati EditorView::updateEditorHistory(editor, d->m_globalHistory); } updateActions(); - updateWindowTitle(); emit m_instance->currentEditorChanged(editor); } @@ -1297,13 +1298,8 @@ void EditorManagerPrivate::updateActions() IDocument *curDocument = EditorManager::currentDocument(); const int openedCount = DocumentModel::entryCount(); - if (curDocument) { - if (HostOsInfo::isMacHost()) - mainEditorArea()->window()->setWindowModified(curDocument->isModified()); + if (curDocument) updateMakeWritableWarning(); - } else /* curEditor */ if (HostOsInfo::isMacHost()) { - mainEditorArea()->window()->setWindowModified(false); - } foreach (EditorArea *area, d->m_editorAreas) setCloseSplitEnabled(area, area->isSplitter()); @@ -1335,32 +1331,53 @@ void EditorManagerPrivate::updateActions() d->m_gotoNextSplitAction->setEnabled(hasSplitter || d->m_editorAreas.size() > 1); } -void EditorManagerPrivate::updateWindowTitle() +void EditorManagerPrivate::updateWindowTitleForDocument(IDocument *document, QWidget *window) { - QString windowTitle = tr("Qt Creator"); + QTC_ASSERT(window, return); + QString windowTitle; const QString dashSep = QLatin1String(" - "); - QString vcsTopic; - IDocument *document = EditorManager::currentDocument(); - if (!d->m_titleVcsTopic.isEmpty()) - vcsTopic = QLatin1String(" [") + d->m_titleVcsTopic + QLatin1Char(']'); - if (!d->m_titleAddition.isEmpty()) { - windowTitle.prepend(dashSep); - if (!document) - windowTitle.prepend(vcsTopic); - windowTitle.prepend(d->m_titleAddition); - } - if (document) { - const QString documentName = document->displayName(); - if (!documentName.isEmpty()) - windowTitle.prepend(documentName + vcsTopic + dashSep); - QString filePath = QFileInfo(document->filePath()).absoluteFilePath(); - if (!filePath.isEmpty()) - ICore::mainWindow()->setWindowFilePath(filePath); - } else { - ICore::mainWindow()->setWindowFilePath(QString()); + QString filePath = document ? QFileInfo(document->filePath()).absoluteFilePath() + : QString(); + + const QString windowTitleAddition = d->m_titleAdditionHandler + ? d->m_titleAdditionHandler(filePath) + : QString(); + + QString windowTitleVcsTopic; + if (d->m_titleVcsTopicHandler) + windowTitleVcsTopic = d->m_titleVcsTopicHandler(filePath); + if (!windowTitleVcsTopic.isEmpty()) + windowTitleVcsTopic = QStringLiteral(" [") + windowTitleVcsTopic + QStringLiteral("]"); + + const QString documentName = document ? document->displayName() : QString(); + + if (!documentName.isEmpty()) + windowTitle.append(documentName + windowTitleVcsTopic + dashSep); + if (!windowTitleAddition.isEmpty()) { + windowTitle.append(windowTitleAddition); + if (documentName.isEmpty()) // vcs topic not already added + windowTitle.append(windowTitleVcsTopic); + windowTitle.append(dashSep); + } + + windowTitle.append(tr("Qt Creator")); + window->window()->setWindowTitle(windowTitle); + window->window()->setWindowFilePath(filePath); + + if (HostOsInfo::isMacHost()) { + if (document) + window->window()->setWindowModified(document->isModified()); + else + window->window()->setWindowModified(false); } - ICore::mainWindow()->setWindowTitle(windowTitle); +} + +void EditorManagerPrivate::updateWindowTitle() +{ + EditorArea *mainArea = mainEditorArea(); + IDocument *document = mainArea->currentDocument(); + updateWindowTitleForDocument(document, mainArea->window()); } void EditorManagerPrivate::gotoNextDocHistory() @@ -1444,7 +1461,6 @@ void EditorManagerPrivate::handleDocumentStateChange() if (!document->isModified()) document->removeAutoSaveFile(); if (EditorManager::currentDocument() == document) { - updateWindowTitle(); emit m_instance->currentDocumentStateChanged(); } } @@ -2045,7 +2061,6 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask if (!currentEditor()) { emit m_instance->currentEditorChanged(0); EditorManagerPrivate::updateActions(); - EditorManagerPrivate::updateWindowTitle(); } return !closingFailed; @@ -2524,24 +2539,18 @@ qint64 EditorManager::maxTextFileSize() return qint64(3) << 24; } -void EditorManager::setWindowTitleAddition(const QString &addition) +void EditorManager::setWindowTitleAdditionHandler(WindowTitleHandler handler) { - d->m_titleAddition = addition; - EditorManagerPrivate::updateWindowTitle(); + d->m_titleAdditionHandler = handler; } -QString EditorManager::windowTitleAddition() +void EditorManager::updateWindowTitles() { - return d->m_titleAddition; -} - -void EditorManager::setWindowTitleVcsTopic(const QString &topic) -{ - d->m_titleVcsTopic = topic; - EditorManagerPrivate::updateWindowTitle(); + foreach (EditorArea *area, d->m_editorAreas) + emit area->windowTitleNeedsUpdate(); } -QString EditorManager::windowTitleVcsTopic() +void EditorManager::setWindowTitleVcsTopicHandler(WindowTitleHandler handler) { - return d->m_titleVcsTopic; + d->m_titleVcsTopicHandler = handler; } diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h index 9dcd84117d..ece8c6ef35 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.h +++ b/src/plugins/coreplugin/editormanager/editormanager.h @@ -41,6 +41,8 @@ #include <QWidget> #include <QMenu> +#include <functional> + QT_BEGIN_NAMESPACE QT_END_NAMESPACE @@ -93,6 +95,7 @@ class CORE_EXPORT EditorManager : public QObject public: typedef QList<IEditorFactory *> EditorFactoryList; typedef QList<IExternalEditor *> ExternalEditorList; + typedef std::function<QString (const QString &)> WindowTitleHandler; static EditorManager *instance(); @@ -160,11 +163,8 @@ public: static qint64 maxTextFileSize(); - static void setWindowTitleAddition(const QString &addition); - static QString windowTitleAddition(); - - static void setWindowTitleVcsTopic(const QString &topic); - static QString windowTitleVcsTopic(); + static void setWindowTitleAdditionHandler(WindowTitleHandler handler); + static void setWindowTitleVcsTopicHandler(WindowTitleHandler handler); static void addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentModel::Entry *entry); static void addNativeDirAndOpenWithActions(QMenu *contextMenu, DocumentModel::Entry *entry); @@ -189,6 +189,7 @@ public slots: static void gotoOtherSplit(); static void goBackInNavigationHistory(); static void goForwardInNavigationHistory(); + static void updateWindowTitles(); private: explicit EditorManager(QObject *parent); diff --git a/src/plugins/coreplugin/editormanager/editormanager_p.h b/src/plugins/coreplugin/editormanager/editormanager_p.h index 01cb23d337..4b840d311a 100644 --- a/src/plugins/coreplugin/editormanager/editormanager_p.h +++ b/src/plugins/coreplugin/editormanager/editormanager_p.h @@ -67,7 +67,7 @@ class EditorManagerPrivate : public QObject friend class Core::EditorManager; public: - static QWidget *mainEditorArea(); + static EditorArea *mainEditorArea(); static EditorView *currentEditorView(); static void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false); static IEditor *openEditor(EditorView *view, @@ -105,6 +105,8 @@ public: static void updateActions(); + static void updateWindowTitleForDocument(IDocument *document, QWidget *window); + public slots: static bool saveDocument(Core::IDocument *document); static bool saveDocumentAs(Core::IDocument *document); @@ -217,8 +219,8 @@ private: IDocument::ReloadSetting m_reloadSetting; - QString m_titleAddition; - QString m_titleVcsTopic; + EditorManager::WindowTitleHandler m_titleAdditionHandler; + EditorManager::WindowTitleHandler m_titleVcsTopicHandler; bool m_autoSaveEnabled; int m_autoSaveInterval; diff --git a/src/plugins/coreplugin/editormanager/editorview.cpp b/src/plugins/coreplugin/editormanager/editorview.cpp index 420911dcf7..3fbb09b623 100644 --- a/src/plugins/coreplugin/editormanager/editorview.cpp +++ b/src/plugins/coreplugin/editormanager/editorview.cpp @@ -359,6 +359,7 @@ void EditorView::setCurrentEditor(IEditor *editor) m_toolBar->setCurrentEditor(0); m_infoBarDisplay->setInfoBar(0); m_container->setCurrentIndex(0); + emit currentEditorChanged(0); return; } @@ -373,6 +374,7 @@ void EditorView::setCurrentEditor(IEditor *editor) updateEditorHistory(editor); m_infoBarDisplay->setInfoBar(editor->document()->infoBar()); + emit currentEditorChanged(editor); } int EditorView::editorCount() const diff --git a/src/plugins/coreplugin/editormanager/editorview.h b/src/plugins/coreplugin/editormanager/editorview.h index 592f3e1426..08f567e1b6 100644 --- a/src/plugins/coreplugin/editormanager/editorview.h +++ b/src/plugins/coreplugin/editormanager/editorview.h @@ -103,6 +103,9 @@ public: static void updateEditorHistory(IEditor *editor, QList<EditLocation> &history); +signals: + void currentEditorChanged(Core::IEditor *editor); + protected: void paintEvent(QPaintEvent *); void mousePressEvent(QMouseEvent *e); diff --git a/src/plugins/coreplugin/editormanager/editorwindow.cpp b/src/plugins/coreplugin/editormanager/editorwindow.cpp index a25e828101..c6036de2b0 100644 --- a/src/plugins/coreplugin/editormanager/editorwindow.cpp +++ b/src/plugins/coreplugin/editormanager/editorwindow.cpp @@ -30,6 +30,7 @@ #include "editorwindow.h" #include "editorarea.h" +#include "editormanager_p.h" #include <coreplugin/icontext.h> #include <coreplugin/icore.h> @@ -55,6 +56,10 @@ EditorWindow::EditorWindow(QWidget *parent) : static int windowId = 0; ICore::registerWindow(this, Context(Id("EditorManager.ExternalWindow.").withSuffix(++windowId))); + + connect(m_area, &EditorArea::windowTitleNeedsUpdate, + this, &EditorWindow::updateWindowTitle); + updateWindowTitle(); } EditorArea *EditorWindow::editorArea() const @@ -62,5 +67,10 @@ EditorArea *EditorWindow::editorArea() const return m_area; } +void EditorWindow::updateWindowTitle() +{ + EditorManagerPrivate::updateWindowTitleForDocument(m_area->currentDocument(), this); +} + } // Internal } // Core diff --git a/src/plugins/coreplugin/editormanager/editorwindow.h b/src/plugins/coreplugin/editormanager/editorwindow.h index 0ab287ed8d..eecb7586a6 100644 --- a/src/plugins/coreplugin/editormanager/editorwindow.h +++ b/src/plugins/coreplugin/editormanager/editorwindow.h @@ -46,6 +46,8 @@ public: EditorArea *editorArea() const; private: + void updateWindowTitle(); + EditorArea *m_area; }; |