From af99e09b0531e312e901ddeb139775e2104fedd8 Mon Sep 17 00:00:00 2001
From: con <qtc-committer@nokia.com>
Date: Tue, 30 Nov 2010 12:55:41 +0100
Subject: [PATCH] Window title didn't show nice name for e.g. diff views.

Use the editor's displayName for the window title.
Also there were missing change signal emissions in setDisplayName
implementations.
Moves the actual handling of the window title from Session to
EditorManager (so it now is also done for the hypothetical case of no
project explorer plugin).

Task-number: QTCREATORBUG-3207
---
 .../editormanager/editormanager.cpp           | 46 ++++++++++++++++++-
 .../coreplugin/editormanager/editormanager.h  |  5 +-
 src/plugins/designer/formwindoweditor.cpp     |  1 +
 src/plugins/projectexplorer/session.cpp       | 30 +-----------
 src/plugins/projectexplorer/session.h         |  2 -
 src/plugins/resourceeditor/resourceeditorw.h  |  2 +-
 src/plugins/texteditor/basetexteditor.cpp     |  1 +
 src/plugins/texteditor/basetexteditor.h       |  2 +-
 src/plugins/vcsbase/vcsbasesubmiteditor.cpp   |  1 +
 9 files changed, 55 insertions(+), 35 deletions(-)

diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index 8e06c851758..1ed1edae57f 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -218,6 +218,8 @@ struct EditorManagerPrivate {
 
     IFile::ReloadSetting m_reloadSetting;
     IFile::Utf8BomSetting m_utf8BomSetting;
+
+    QString m_titleAddition;
 };
 }
 
@@ -530,13 +532,17 @@ void EditorManager::setCurrentEditor(IEditor *editor, bool ignoreNavigationHisto
     if (m_d->m_currentEditor && !ignoreNavigationHistory)
         addCurrentPositionToNavigationHistory();
 
+    if (m_d->m_currentEditor)
+        disconnect(m_d->m_currentEditor, SIGNAL(changed()), this, SLOT(updateWindowTitle()));
     m_d->m_currentEditor = editor;
     if (editor) {
         if (SplitterOrView *splitterOrView = m_d->m_splitter->findView(editor))
             splitterOrView->view()->setCurrentEditor(editor);
         m_d->m_view->updateEditorHistory(editor); // the global view should have a complete history
+        connect(m_d->m_currentEditor, SIGNAL(changed()), this, SLOT(updateWindowTitle()));
     }
     updateActions();
+    updateWindowTitle();
     emit currentEditorChanged(editor);
 }
 
@@ -847,6 +853,7 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
     if (!currentEditor()) {
         emit currentEditorChanged(0);
         updateActions();
+        updateWindowTitle();
     }
 
     return !closingFailed;
@@ -1569,6 +1576,26 @@ void EditorManager::makeCurrentEditorWritable()
         makeEditorWritable(curEditor);
 }
 
+void EditorManager::updateWindowTitle()
+{
+    QString windowTitle = tr("Qt Creator");
+    if (!m_d->m_titleAddition.isEmpty()) {
+        windowTitle.prepend(m_d->m_titleAddition + " - ");
+    }
+    IEditor *curEditor = currentEditor();
+    if (curEditor) {
+        QString editorName = curEditor->displayName();
+        if (!editorName.isEmpty())
+            windowTitle.prepend(editorName + " - ");
+        QString filePath = QFileInfo(curEditor->file()->fileName()).absoluteFilePath();
+        if (!filePath.isEmpty())
+            m_d->m_core->mainWindow()->setWindowFilePath(filePath);
+    } else {
+        m_d->m_core->mainWindow()->setWindowFilePath(QString());
+    }
+    m_d->m_core->mainWindow()->setWindowTitle(windowTitle);
+}
+
 void EditorManager::updateActions()
 {
     QString fName;
@@ -2069,7 +2096,12 @@ void EditorManager::removeAllSplits()
     if (!m_d->m_splitter->isSplitter())
         return;
     IEditor *editor = m_d->m_currentEditor;
-    m_d->m_currentEditor = 0; // trigger update below
+    {
+        // trigger update below
+        disconnect(m_d->m_currentEditor, SIGNAL(changed()),
+                   this, SLOT(updateWindowTitle()));
+        m_d->m_currentEditor = 0;
+    }
     if (editor && m_d->m_editorModel->isDuplicate(editor))
         m_d->m_editorModel->makeOriginal(editor);
     m_d->m_splitter->unsplitAll();
@@ -2104,5 +2136,15 @@ qint64 EditorManager::maxTextFileSize()
 {
     return (qint64(3) << 24);
 }
-//===================EditorClosingCoreListener======================
+
+void EditorManager::setWindowTitleAddition(const QString &addition)
+{
+    m_d->m_titleAddition = addition;
+    updateWindowTitle();
+}
+
+QString EditorManager::windowTitleAddition() const
+{
+    return m_d->m_titleAddition;
+}
 
diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h
index 6b55561dc68..2dbdc4adec9 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.h
+++ b/src/plugins/coreplugin/editormanager/editormanager.h
@@ -208,6 +208,9 @@ public:
 
     static qint64 maxTextFileSize();
 
+    void setWindowTitleAddition(const QString &addition);
+    QString windowTitleAddition() const;
+
 signals:
     void currentEditorChanged(Core::IEditor *editor);
     void editorCreated(Core::IEditor *editor, const QString &fileName);
@@ -231,6 +234,7 @@ private slots:
     void handleContextChange(Core::IContext *context);
     void updateActions();
     void makeCurrentEditorWritable();
+    void updateWindowTitle();
 
 public slots:
     void goBackInNavigationHistory();
@@ -269,7 +273,6 @@ private:
     Core::Internal::EditorView *currentEditorView() const;
     IEditor *pickUnusedEditor() const;
 
-
     static EditorManager *m_instance;
     EditorManagerPrivate *m_d;
 
diff --git a/src/plugins/designer/formwindoweditor.cpp b/src/plugins/designer/formwindoweditor.cpp
index f18f57e0a7a..9cdd5125c90 100644
--- a/src/plugins/designer/formwindoweditor.cpp
+++ b/src/plugins/designer/formwindoweditor.cpp
@@ -212,6 +212,7 @@ QString FormWindowEditor::displayName() const
 void FormWindowEditor::setDisplayName(const QString &title)
 {
     d->m_textEditable.setDisplayName(title);
+    emit changed();
 }
 
 bool FormWindowEditor::duplicateSupported() const
diff --git a/src/plugins/projectexplorer/session.cpp b/src/plugins/projectexplorer/session.cpp
index 9b04fbe1265..1082c7ea38d 100644
--- a/src/plugins/projectexplorer/session.cpp
+++ b/src/plugins/projectexplorer/session.cpp
@@ -312,7 +312,6 @@ SessionManager::SessionManager(QObject *parent)
     m_core(Core::ICore::instance()),
     m_file(new SessionFile),
     m_sessionNode(new Internal::SessionNodeImpl(this)),
-    m_currentEditor(0),
     m_virginSession(true)
 {
     connect(m_core->modeManager(), SIGNAL(currentModeChanged(Core::IMode*)),
@@ -324,8 +323,6 @@ SessionManager::SessionManager(QObject *parent)
             this, SLOT(setEditorCodec(Core::IEditor *, QString)));
     connect(ProjectExplorerPlugin::instance(), SIGNAL(currentProjectChanged(ProjectExplorer::Project *)),
             this, SLOT(updateWindowTitle()));
-    connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
-            this, SLOT(handleCurrentEditorChange(Core::IEditor*)));
     connect(em, SIGNAL(editorOpened(Core::IEditor*)),
             this, SLOT(markSessionFileDirty()));
     connect(em, SIGNAL(editorsClosed(QList<Core::IEditor*>)),
@@ -839,40 +836,17 @@ QString SessionManager::currentSession() const
     return m_file->fileName();
 }
 
-void SessionManager::handleCurrentEditorChange(Core::IEditor *editor)
-{
-    if (editor != m_currentEditor) {
-        if (m_currentEditor)
-            disconnect(m_currentEditor, SIGNAL(changed()), this, SLOT(updateWindowTitle()));
-        if (editor)
-            connect(editor, SIGNAL(changed()), this, SLOT(updateWindowTitle()));
-        m_currentEditor = editor;
-    }
-    updateWindowTitle();
-}
-
 void SessionManager::updateWindowTitle()
 {
-    QString windowTitle = tr("Qt Creator");
     if (isDefaultSession(m_sessionName)) {
         if (Project *currentProject = ProjectExplorerPlugin::instance()->currentProject())
-            windowTitle.prepend(currentProject->displayName() + " - ");
+            m_core->editorManager()->setWindowTitleAddition(currentProject->displayName());
     } else {
         QString sessionName = m_sessionName;
         if (sessionName.isEmpty())
             sessionName = tr("Untitled");
-        windowTitle.prepend(sessionName + " - ");
-    }
-    if (m_core->editorManager()->currentEditor()) {
-        QFileInfo fi(m_core->editorManager()->currentEditor()->file()->fileName());
-        QString fileName = fi.fileName();
-        if (!fileName.isEmpty())
-            windowTitle.prepend(fileName + " - ");
-        m_core->mainWindow()->setWindowFilePath(fi.absoluteFilePath());
-    } else {
-        m_core->mainWindow()->setWindowFilePath(QString());
+        m_core->editorManager()->setWindowTitleAddition(sessionName);
     }
-    m_core->mainWindow()->setWindowTitle(windowTitle);
 }
 
 void SessionManager::updateName(const QString &session)
diff --git a/src/plugins/projectexplorer/session.h b/src/plugins/projectexplorer/session.h
index 213730b5f68..50b30212c8a 100644
--- a/src/plugins/projectexplorer/session.h
+++ b/src/plugins/projectexplorer/session.h
@@ -155,7 +155,6 @@ private slots:
     void saveActiveMode(Core::IMode *mode);
     void clearProjectFileCache();
     void setEditorCodec(Core::IEditor *editor, const QString &fileName);
-    void handleCurrentEditorChange(Core::IEditor *editor);
     void updateWindowTitle();
 
     void markSessionFileDirty(bool makeDefaultVirginDirty = true);
@@ -176,7 +175,6 @@ private:
 
     Internal::SessionFile *m_file;
     Internal::SessionNodeImpl *m_sessionNode;
-    QPointer<Core::IEditor> m_currentEditor;
     QString m_sessionName;
     bool m_virginSession;
 
diff --git a/src/plugins/resourceeditor/resourceeditorw.h b/src/plugins/resourceeditor/resourceeditorw.h
index b05f5eee3a2..5acd1099006 100644
--- a/src/plugins/resourceeditor/resourceeditorw.h
+++ b/src/plugins/resourceeditor/resourceeditorw.h
@@ -89,7 +89,7 @@ public:
     Core::IFile *file() { return m_resourceFile; }
     QString id() const;
     QString displayName() const { return m_displayName; }
-    void setDisplayName(const QString &title) { m_displayName = title; }
+    void setDisplayName(const QString &title) { m_displayName = title; emit changed(); }
     QWidget *toolBar() { return 0; }
     QByteArray saveState() const { return QByteArray(); }
     bool restoreState(const QByteArray &/*state*/) { return true; }
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 73147180db5..1e58cf2265c 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -2050,6 +2050,7 @@ QString BaseTextEditor::displayName() const
 void BaseTextEditor::setDisplayName(const QString &title)
 {
     d->m_displayName = title;
+    emit changed();
 }
 
 BaseTextDocument *BaseTextEditor::baseTextDocument() const
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index ca7d1db5a38..c6e3fe6ef79 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -572,7 +572,7 @@ public:
         return e->open(fileName);
     }
     inline QString displayName() const { return e->displayName(); }
-    inline void setDisplayName(const QString &title) { e->setDisplayName(title); }
+    inline void setDisplayName(const QString &title) { e->setDisplayName(title); emit changed(); }
 
     inline QByteArray saveState() const { return e->saveState(); }
     inline bool restoreState(const QByteArray &state) { return e->restoreState(state); }
diff --git a/src/plugins/vcsbase/vcsbasesubmiteditor.cpp b/src/plugins/vcsbase/vcsbasesubmiteditor.cpp
index a3a2b9618a5..ec5578ea20e 100644
--- a/src/plugins/vcsbase/vcsbasesubmiteditor.cpp
+++ b/src/plugins/vcsbase/vcsbasesubmiteditor.cpp
@@ -329,6 +329,7 @@ QString VCSBaseSubmitEditor::displayName() const
 void VCSBaseSubmitEditor::setDisplayName(const QString &title)
 {
     m_d->m_displayName = title;
+    emit changed();
 }
 
 QString VCSBaseSubmitEditor::checkScriptWorkingDirectory() const
-- 
GitLab