forked from qt-creator/qt-creator
Editors: Set window title for external editor windows
Change-Id: I54ed77c0f1b2122ae8833109d8dcac7d8eec7ac4 Task-number: QTCREATORBUG-9612 Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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::updateWindowTitleForDocument(IDocument *document, QWidget *window)
|
||||
{
|
||||
QTC_ASSERT(window, return);
|
||||
QString windowTitle;
|
||||
const QString dashSep = QLatin1String(" - ");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void EditorManagerPrivate::updateWindowTitle()
|
||||
{
|
||||
QString windowTitle = tr("Qt Creator");
|
||||
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());
|
||||
}
|
||||
ICore::mainWindow()->setWindowTitle(windowTitle);
|
||||
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;
|
||||
foreach (EditorArea *area, d->m_editorAreas)
|
||||
emit area->windowTitleNeedsUpdate();
|
||||
}
|
||||
|
||||
void EditorManager::setWindowTitleVcsTopic(const QString &topic)
|
||||
void EditorManager::setWindowTitleVcsTopicHandler(WindowTitleHandler handler)
|
||||
{
|
||||
d->m_titleVcsTopic = topic;
|
||||
EditorManagerPrivate::updateWindowTitle();
|
||||
}
|
||||
|
||||
QString EditorManager::windowTitleVcsTopic()
|
||||
{
|
||||
return d->m_titleVcsTopic;
|
||||
d->m_titleVcsTopicHandler = handler;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -46,6 +46,8 @@ public:
|
||||
EditorArea *editorArea() const;
|
||||
|
||||
private:
|
||||
void updateWindowTitle();
|
||||
|
||||
EditorArea *m_area;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user