Commit 49a8cf44 authored by Eike Ziller's avatar Eike Ziller

Editors: Set window title for external editor windows

Change-Id: I54ed77c0f1b2122ae8833109d8dcac7d8eec7ac4
Task-number: QTCREATORBUG-9612
Reviewed-by: default avatarDaniel Teske <daniel.teske@digia.com>
parent 2a9a014c
...@@ -29,26 +29,99 @@ ...@@ -29,26 +29,99 @@
#include "editorarea.h" #include "editorarea.h"
#include "editormanager.h"
#include "ieditor.h"
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/qtcassert.h>
#include <QApplication>
namespace Core { namespace Core {
namespace Internal { namespace Internal {
EditorArea::EditorArea() EditorArea::EditorArea()
: m_currentView(0),
m_currentDocument(0)
{ {
m_context = new IContext; m_context = new IContext;
m_context->setContext(Context(Constants::C_EDITORMANAGER)); m_context->setContext(Context(Constants::C_EDITORMANAGER));
m_context->setWidget(this); m_context->setWidget(this);
ICore::addContextObject(m_context); ICore::addContextObject(m_context);
setCurrentView(view());
connect(qApp, &QApplication::focusChanged,
this, &EditorArea::focusChanged);
} }
EditorArea::~EditorArea() EditorArea::~EditorArea()
{ {
// disconnect
setCurrentView(0);
disconnect(qApp, &QApplication::focusChanged,
this, &EditorArea::focusChanged);
ICore::removeContextObject(m_context); ICore::removeContextObject(m_context);
delete 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 } // Internal
} // Core } // Core
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
#include "editorview.h" #include "editorview.h"
#include <QPointer>
namespace Core { namespace Core {
class IContext; class IContext;
...@@ -46,8 +48,19 @@ public: ...@@ -46,8 +48,19 @@ public:
EditorArea(); EditorArea();
~EditorArea(); ~EditorArea();
IDocument *currentDocument() const;
signals:
void windowTitleNeedsUpdate();
private: private:
void focusChanged(QWidget *old, QWidget *now);
void setCurrentView(EditorView *view);
void updateCurrentEditor(IEditor *editor);
IContext *m_context; IContext *m_context;
QPointer<EditorView> m_currentView;
QPointer<IDocument> m_currentDocument;
}; };
} // Internal } // Internal
......
...@@ -451,6 +451,8 @@ void EditorManagerPrivate::init() ...@@ -451,6 +451,8 @@ void EditorManagerPrivate::init()
// other setup // other setup
auto mainEditorArea = new EditorArea(); auto mainEditorArea = new EditorArea();
mainEditorArea->hide(); mainEditorArea->hide();
connect(mainEditorArea, &EditorArea::windowTitleNeedsUpdate,
this, &EditorManagerPrivate::updateWindowTitle);
connect(mainEditorArea, SIGNAL(destroyed(QObject*)), this, SLOT(editorAreaDestroyed(QObject*))); connect(mainEditorArea, SIGNAL(destroyed(QObject*)), this, SLOT(editorAreaDestroyed(QObject*)));
d->m_editorAreas.append(mainEditorArea); d->m_editorAreas.append(mainEditorArea);
d->m_currentView = mainEditorArea->view(); d->m_currentView = mainEditorArea->view();
...@@ -491,7 +493,7 @@ void EditorManagerPrivate::init() ...@@ -491,7 +493,7 @@ void EditorManagerPrivate::init()
}); });
} }
QWidget *EditorManagerPrivate::mainEditorArea() EditorArea *EditorManagerPrivate::mainEditorArea()
{ {
return d->m_editorAreas.at(0); return d->m_editorAreas.at(0);
} }
...@@ -1065,7 +1067,6 @@ void EditorManagerPrivate::setCurrentEditor(IEditor *editor, bool ignoreNavigati ...@@ -1065,7 +1067,6 @@ void EditorManagerPrivate::setCurrentEditor(IEditor *editor, bool ignoreNavigati
EditorView::updateEditorHistory(editor, d->m_globalHistory); EditorView::updateEditorHistory(editor, d->m_globalHistory);
} }
updateActions(); updateActions();
updateWindowTitle();
emit m_instance->currentEditorChanged(editor); emit m_instance->currentEditorChanged(editor);
} }
...@@ -1297,13 +1298,8 @@ void EditorManagerPrivate::updateActions() ...@@ -1297,13 +1298,8 @@ void EditorManagerPrivate::updateActions()
IDocument *curDocument = EditorManager::currentDocument(); IDocument *curDocument = EditorManager::currentDocument();
const int openedCount = DocumentModel::entryCount(); const int openedCount = DocumentModel::entryCount();
if (curDocument) { if (curDocument)
if (HostOsInfo::isMacHost())
mainEditorArea()->window()->setWindowModified(curDocument->isModified());
updateMakeWritableWarning(); updateMakeWritableWarning();
} else /* curEditor */ if (HostOsInfo::isMacHost()) {
mainEditorArea()->window()->setWindowModified(false);
}
foreach (EditorArea *area, d->m_editorAreas) foreach (EditorArea *area, d->m_editorAreas)
setCloseSplitEnabled(area, area->isSplitter()); setCloseSplitEnabled(area, area->isSplitter());
...@@ -1335,32 +1331,53 @@ void EditorManagerPrivate::updateActions() ...@@ -1335,32 +1331,53 @@ void EditorManagerPrivate::updateActions()
d->m_gotoNextSplitAction->setEnabled(hasSplitter || d->m_editorAreas.size() > 1); 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(" - "); const QString dashSep = QLatin1String(" - ");
QString vcsTopic;
IDocument *document = EditorManager::currentDocument();
if (!d->m_titleVcsTopic.isEmpty()) QString filePath = document ? QFileInfo(document->filePath()).absoluteFilePath()
vcsTopic = QLatin1String(" [") + d->m_titleVcsTopic + QLatin1Char(']'); : QString();
if (!d->m_titleAddition.isEmpty()) {
windowTitle.prepend(dashSep); const QString windowTitleAddition = d->m_titleAdditionHandler
if (!document) ? d->m_titleAdditionHandler(filePath)
windowTitle.prepend(vcsTopic); : QString();
windowTitle.prepend(d->m_titleAddition);
} QString windowTitleVcsTopic;
if (document) { if (d->m_titleVcsTopicHandler)
const QString documentName = document->displayName(); windowTitleVcsTopic = d->m_titleVcsTopicHandler(filePath);
if (!documentName.isEmpty()) if (!windowTitleVcsTopic.isEmpty())
windowTitle.prepend(documentName + vcsTopic + dashSep); windowTitleVcsTopic = QStringLiteral(" [") + windowTitleVcsTopic + QStringLiteral("]");
QString filePath = QFileInfo(document->filePath()).absoluteFilePath();
if (!filePath.isEmpty()) const QString documentName = document ? document->displayName() : QString();
ICore::mainWindow()->setWindowFilePath(filePath);
} else { if (!documentName.isEmpty())
ICore::mainWindow()->setWindowFilePath(QString()); 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() void EditorManagerPrivate::gotoNextDocHistory()
...@@ -1444,7 +1461,6 @@ void EditorManagerPrivate::handleDocumentStateChange() ...@@ -1444,7 +1461,6 @@ void EditorManagerPrivate::handleDocumentStateChange()
if (!document->isModified()) if (!document->isModified())
document->removeAutoSaveFile(); document->removeAutoSaveFile();
if (EditorManager::currentDocument() == document) { if (EditorManager::currentDocument() == document) {
updateWindowTitle();
emit m_instance->currentDocumentStateChanged(); emit m_instance->currentDocumentStateChanged();
} }
} }
...@@ -2045,7 +2061,6 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask ...@@ -2045,7 +2061,6 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
if (!currentEditor()) { if (!currentEditor()) {
emit m_instance->currentEditorChanged(0); emit m_instance->currentEditorChanged(0);
EditorManagerPrivate::updateActions(); EditorManagerPrivate::updateActions();
EditorManagerPrivate::updateWindowTitle();
} }
return !closingFailed; return !closingFailed;
...@@ -2524,24 +2539,18 @@ qint64 EditorManager::maxTextFileSize() ...@@ -2524,24 +2539,18 @@ qint64 EditorManager::maxTextFileSize()
return qint64(3) << 24; return qint64(3) << 24;
} }
void EditorManager::setWindowTitleAddition(const QString &addition) void EditorManager::setWindowTitleAdditionHandler(WindowTitleHandler handler)
{ {
d->m_titleAddition = addition; d->m_titleAdditionHandler = handler;
EditorManagerPrivate::updateWindowTitle();
} }
QString EditorManager::windowTitleAddition() void EditorManager::updateWindowTitles()
{ {
return d->m_titleAddition; foreach (EditorArea *area, d->m_editorAreas)
} emit area->windowTitleNeedsUpdate();
void EditorManager::setWindowTitleVcsTopic(const QString &topic)
{
d->m_titleVcsTopic = topic;
EditorManagerPrivate::updateWindowTitle();
} }
QString EditorManager::windowTitleVcsTopic() void EditorManager::setWindowTitleVcsTopicHandler(WindowTitleHandler handler)
{ {
return d->m_titleVcsTopic; d->m_titleVcsTopicHandler = handler;
} }
...@@ -41,6 +41,8 @@ ...@@ -41,6 +41,8 @@
#include <QWidget> #include <QWidget>
#include <QMenu> #include <QMenu>
#include <functional>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
QT_END_NAMESPACE QT_END_NAMESPACE
...@@ -93,6 +95,7 @@ class CORE_EXPORT EditorManager : public QObject ...@@ -93,6 +95,7 @@ class CORE_EXPORT EditorManager : public QObject
public: public:
typedef QList<IEditorFactory *> EditorFactoryList; typedef QList<IEditorFactory *> EditorFactoryList;
typedef QList<IExternalEditor *> ExternalEditorList; typedef QList<IExternalEditor *> ExternalEditorList;
typedef std::function<QString (const QString &)> WindowTitleHandler;
static EditorManager *instance(); static EditorManager *instance();
...@@ -160,11 +163,8 @@ public: ...@@ -160,11 +163,8 @@ public:
static qint64 maxTextFileSize(); static qint64 maxTextFileSize();
static void setWindowTitleAddition(const QString &addition); static void setWindowTitleAdditionHandler(WindowTitleHandler handler);
static QString windowTitleAddition(); static void setWindowTitleVcsTopicHandler(WindowTitleHandler handler);
static void setWindowTitleVcsTopic(const QString &topic);
static QString windowTitleVcsTopic();
static void addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentModel::Entry *entry); static void addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentModel::Entry *entry);
static void addNativeDirAndOpenWithActions(QMenu *contextMenu, DocumentModel::Entry *entry); static void addNativeDirAndOpenWithActions(QMenu *contextMenu, DocumentModel::Entry *entry);
...@@ -189,6 +189,7 @@ public slots: ...@@ -189,6 +189,7 @@ public slots:
static void gotoOtherSplit(); static void gotoOtherSplit();
static void goBackInNavigationHistory(); static void goBackInNavigationHistory();
static void goForwardInNavigationHistory(); static void goForwardInNavigationHistory();
static void updateWindowTitles();
private: private:
explicit EditorManager(QObject *parent); explicit EditorManager(QObject *parent);
......
...@@ -67,7 +67,7 @@ class EditorManagerPrivate : public QObject ...@@ -67,7 +67,7 @@ class EditorManagerPrivate : public QObject
friend class Core::EditorManager; friend class Core::EditorManager;
public: public:
static QWidget *mainEditorArea(); static EditorArea *mainEditorArea();
static EditorView *currentEditorView(); static EditorView *currentEditorView();
static void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false); static void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
static IEditor *openEditor(EditorView *view, static IEditor *openEditor(EditorView *view,
...@@ -105,6 +105,8 @@ public: ...@@ -105,6 +105,8 @@ public:
static void updateActions(); static void updateActions();
static void updateWindowTitleForDocument(IDocument *document, QWidget *window);
public slots: public slots:
static bool saveDocument(Core::IDocument *document); static bool saveDocument(Core::IDocument *document);
static bool saveDocumentAs(Core::IDocument *document); static bool saveDocumentAs(Core::IDocument *document);
...@@ -217,8 +219,8 @@ private: ...@@ -217,8 +219,8 @@ private:
IDocument::ReloadSetting m_reloadSetting; IDocument::ReloadSetting m_reloadSetting;
QString m_titleAddition; EditorManager::WindowTitleHandler m_titleAdditionHandler;
QString m_titleVcsTopic; EditorManager::WindowTitleHandler m_titleVcsTopicHandler;
bool m_autoSaveEnabled; bool m_autoSaveEnabled;
int m_autoSaveInterval; int m_autoSaveInterval;
......
...@@ -359,6 +359,7 @@ void EditorView::setCurrentEditor(IEditor *editor) ...@@ -359,6 +359,7 @@ void EditorView::setCurrentEditor(IEditor *editor)
m_toolBar->setCurrentEditor(0); m_toolBar->setCurrentEditor(0);
m_infoBarDisplay->setInfoBar(0); m_infoBarDisplay->setInfoBar(0);
m_container->setCurrentIndex(0); m_container->setCurrentIndex(0);
emit currentEditorChanged(0);
return; return;
} }
...@@ -373,6 +374,7 @@ void EditorView::setCurrentEditor(IEditor *editor) ...@@ -373,6 +374,7 @@ void EditorView::setCurrentEditor(IEditor *editor)
updateEditorHistory(editor); updateEditorHistory(editor);
m_infoBarDisplay->setInfoBar(editor->document()->infoBar()); m_infoBarDisplay->setInfoBar(editor->document()->infoBar());
emit currentEditorChanged(editor);
} }
int EditorView::editorCount() const int EditorView::editorCount() const
......
...@@ -103,6 +103,9 @@ public: ...@@ -103,6 +103,9 @@ public:
static void updateEditorHistory(IEditor *editor, QList<EditLocation> &history); static void updateEditorHistory(IEditor *editor, QList<EditLocation> &history);
signals:
void currentEditorChanged(Core::IEditor *editor);
protected: protected:
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "editorwindow.h" #include "editorwindow.h"
#include "editorarea.h" #include "editorarea.h"
#include "editormanager_p.h"
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
...@@ -55,6 +56,10 @@ EditorWindow::EditorWindow(QWidget *parent) : ...@@ -55,6 +56,10 @@ EditorWindow::EditorWindow(QWidget *parent) :
static int windowId = 0; static int windowId = 0;
ICore::registerWindow(this, Context(Id("EditorManager.ExternalWindow.").withSuffix(++windowId))); ICore::registerWindow(this, Context(Id("EditorManager.ExternalWindow.").withSuffix(++windowId)));
connect(m_area, &EditorArea::windowTitleNeedsUpdate,
this, &EditorWindow::updateWindowTitle);
updateWindowTitle();
} }
EditorArea *EditorWindow::editorArea() const EditorArea *EditorWindow::editorArea() const
...@@ -62,5 +67,10 @@ EditorArea *EditorWindow::editorArea() const ...@@ -62,5 +67,10 @@ EditorArea *EditorWindow::editorArea() const
return m_area; return m_area;
} }
void EditorWindow::updateWindowTitle()
{
EditorManagerPrivate::updateWindowTitleForDocument(m_area->currentDocument(), this);
}
} // Internal } // Internal
} // Core } // Core
...@@ -46,6 +46,8 @@ public: ...@@ -46,6 +46,8 @@ public:
EditorArea *editorArea() const; EditorArea *editorArea() const;
private: private:
void updateWindowTitle();
EditorArea *m_area; EditorArea *m_area;
}; };
......
...@@ -100,6 +100,8 @@ public: ...@@ -100,6 +100,8 @@ public:
void dependencies(const QString &proName, QStringList &result) const; void dependencies(const QString &proName, QStringList &result) const;
public: public:
static QString windowTitleAddition(const QString &filePath);
SessionNode *m_sessionNode; SessionNode *m_sessionNode;
QString m_sessionName; QString m_sessionName;
bool m_virginSession; bool m_virginSession;
...@@ -134,12 +136,18 @@ SessionManager::SessionManager(QObject *parent) ...@@ -134,12 +136,18 @@ SessionManager::SessionManager(QObject *parent)
connect(EditorManager::instance(), SIGNAL(editorCreated(Core::IEditor*,QString)), connect(EditorManager::instance(), SIGNAL(editorCreated(Core::IEditor*,QString)),
this, SLOT(configureEditor(Core::IEditor*,QString))); this, SLOT(configureEditor(Core::IEditor*,QString)));
connect(ProjectExplorerPlugin::instance(), SIGNAL(currentProjectChanged(ProjectExplorer::Project*)), connect(this, SIGNAL(projectAdded(ProjectExplorer::Project*)),
this, SLOT(updateWindowTitle())); EditorManager::instance(), SLOT(updateWindowTitles()));
connect(this, SIGNAL(projectRemoved(ProjectExplorer::Project*)),
EditorManager::instance(), SLOT(updateWindowTitles()));
connect(this, SIGNAL(projectDisplayNameChanged(ProjectExplorer::Project*)),
EditorManager::instance(), SLOT(updateWindowTitles()));
connect(EditorManager::instance(), SIGNAL(editorOpened(Core::IEditor*)), connect(EditorManager::instance(), SIGNAL(editorOpened(Core::IEditor*)),