Commit 4a24df38 authored by hjk's avatar hjk
Browse files

EditorManager: Use static pattern, adjust surrounding code



Change-Id: I3255a0150cd9a730336456c5a9f986eb74fefbff
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
parent e1823072
......@@ -579,8 +579,7 @@ void BookmarkManager::documentPrevNext(bool next)
nextLine = markLine;
}
Core::EditorManager *em = Core::EditorManager::instance();
em->addCurrentPositionToNavigationHistory();
EditorManager::addCurrentPositionToNavigationHistory();
if (next) {
if (nextLine == -1)
editor->gotoLine(firstLine);
......
......@@ -1350,7 +1350,7 @@ void ClearCasePlugin::describe(const QString &source, const QString &changeNr)
void ClearCasePlugin::checkInSelected()
{
m_submitActionTriggered = true;
Core::EditorManager::instance()->closeEditor();
Core::EditorManager::closeEditor();
}
QString ClearCasePlugin::runCleartoolSync(const QString &workingDir,
......
......@@ -267,7 +267,7 @@ int ReadOnlyFilesDialog::exec()
}
break;
case RO_SaveAs:
if (!EditorManager::instance()->saveDocumentAs(d->document)) {
if (!EditorManager::saveDocumentAs(d->document)) {
failedToMakeWritable << buttengroup.fileName;
continue;
}
......
......@@ -646,7 +646,7 @@ static QList<IDocument *> saveModifiedFilesHelper(const QList<IDocument *> &docu
}
}
foreach (IDocument *document, documentsToSave) {
if (!EditorManager::instance()->saveDocument(document)) {
if (!EditorManager::saveDocument(document)) {
if (cancelled)
*cancelled = true;
notSaved.append(document);
......@@ -823,7 +823,7 @@ void DocumentManager::checkForReload()
d->m_blockActivated = true;
IDocument::ReloadSetting defaultBehavior = EditorManager::instance()->reloadSetting();
IDocument::ReloadSetting defaultBehavior = EditorManager::reloadSetting();
Utils::ReloadPromptAnswer previousAnswer = Utils::ReloadCurrent;
QList<IDocument *> documentsToClose;
......@@ -1340,7 +1340,7 @@ void DocumentManager::executeOpenWithMenuAction(QAction *action)
if (entry.editorFactory->id() == openEditor->id())
editorsOpenForFile.removeAll(openEditor);
}
if (!EditorManager::instance()->closeEditors(editorsOpenForFile)) // don't open if cancel was pressed
if (!EditorManager::closeEditors(editorsOpenForFile)) // don't open if cancel was pressed
return;
}
......
......@@ -49,7 +49,6 @@ EditMode::EditMode() :
m_splitter(new MiniSplitter),
m_rightSplitWidgetLayout(new QVBoxLayout)
{
m_editorManager = EditorManager::instance();
setObjectName(QLatin1String("EditMode"));
setDisplayName(tr("Edit"));
setIcon(QIcon(QLatin1String(":/fancyactionbar/images/mode_Edit.png")));
......@@ -84,7 +83,7 @@ EditMode::EditMode() :
connect(ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode*)),
this, SLOT(grabEditorManager(Core::IMode*)));
m_splitter->setFocusProxy(m_editorManager);
m_splitter->setFocusProxy(EditorManager::instance());
setWidget(m_splitter);
setContext(Context(Constants::C_EDIT_MODE,
......@@ -95,7 +94,7 @@ EditMode::EditMode() :
EditMode::~EditMode()
{
// Make sure the editor manager does not get deleted
m_editorManager->setParent(0);
EditorManager::instance()->setParent(0);
delete m_splitter;
}
......
......@@ -55,7 +55,6 @@ private slots:
void grabEditorManager(Core::IMode *mode);
private:
EditorManager *m_editorManager;
QSplitter *m_splitter;
QVBoxLayout *m_rightSplitWidgetLayout;
};
......
......@@ -96,31 +96,15 @@ namespace Internal {
class EditorClosingCoreListener : public ICoreListener
{
public:
EditorClosingCoreListener(EditorManager *em);
bool editorAboutToClose(IEditor *editor);
bool coreAboutToClose();
private:
EditorManager *m_em;
bool editorAboutToClose(IEditor *) { return true; }
bool coreAboutToClose()
{
// Do not ask for files to save.
// MainWindow::closeEvent has already done that.
return EditorManager::closeAllEditors(false);
}
};
EditorClosingCoreListener::EditorClosingCoreListener(EditorManager *em)
: m_em(em)
{
}
bool EditorClosingCoreListener::editorAboutToClose(IEditor *)
{
return true;
}
bool EditorClosingCoreListener::coreAboutToClose()
{
// Do not ask for files to save.
// MainWindow::closeEvent has already done that.
return m_em->closeAllEditors(false);
}
} // namespace Internal
} // namespace Core
......@@ -269,7 +253,7 @@ EditorManagerPrivate::~EditorManagerPrivate()
static EditorManager *m_instance = 0;
static EditorManagerPrivate *d;
EditorManager *EditorManager::instance() { return m_instance; }
QWidget *EditorManager::instance() { return m_instance; }
EditorManager::EditorManager(QWidget *parent) :
QWidget(parent)
......@@ -477,7 +461,7 @@ EditorManager::~EditorManager()
void EditorManager::init()
{
d->m_coreListener = new EditorClosingCoreListener(this);
d->m_coreListener = new EditorClosingCoreListener();
ExtensionSystem::PluginManager::addObject(d->m_coreListener);
d->m_openEditorsFactory = new OpenEditorsViewFactory();
......@@ -489,7 +473,7 @@ void EditorManager::init()
VariableManager::registerVariable(kCurrentDocumentYPos,
tr("Y-coordinate of the current editor's upper left corner, relative to screen."));
connect(VariableManager::instance(), SIGNAL(variableUpdateRequested(QByteArray)),
this, SLOT(updateVariable(QByteArray)));
m_instance, SLOT(updateVariable(QByteArray)));
}
void EditorManager::updateAutoSave()
......@@ -530,7 +514,7 @@ void EditorManager::handleContextChange(const QList<Core::IContext *> &context)
// the locator line edit) first activates the window and sets focus to its focus widget.
// Only afterwards the focus is shifted to the widget that received the click.
d->m_scheduledCurrentEditor = editor;
QTimer::singleShot(0, this, SLOT(setCurrentEditorFromContextChange()));
QTimer::singleShot(0, m_instance, SLOT(setCurrentEditorFromContextChange()));
} else {
updateActions();
}
......@@ -555,7 +539,7 @@ void EditorManager::setCurrentEditor(IEditor *editor, bool ignoreNavigationHisto
}
updateActions();
updateWindowTitle();
emit currentEditorChanged(editor);
emit m_instance->currentEditorChanged(editor);
}
......@@ -578,7 +562,7 @@ void EditorManager::setCurrentView(Internal::EditorView *view)
}
}
Internal::EditorView *EditorManager::currentEditorView() const
Internal::EditorView *EditorManager::currentEditorView()
{
EditorView *view = d->m_currentView;
if (!view) {
......@@ -656,12 +640,12 @@ void EditorManager::emptyView(Core::Internal::EditorView *view)
view->removeEditor(editor);
continue; // don't close the editor
}
emit editorAboutToClose(editor);
emit m_instance->editorAboutToClose(editor);
removeEditor(editor);
view->removeEditor(editor);
}
if (!editors.isEmpty()) {
emit editorsClosed(editors);
emit m_instance->editorsClosed(editors);
foreach (IEditor *editor, editors) {
delete editor;
}
......@@ -1078,7 +1062,7 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
// remove the editors
foreach (IEditor *editor, acceptedEditors) {
emit editorAboutToClose(editor);
emit m_instance->editorAboutToClose(editor);
if (!editor->document()->filePath().isEmpty()
&& !editor->document()->isTemporary()) {
QByteArray state = editor->saveState();
......@@ -1127,7 +1111,7 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
}
}
emit editorsClosed(acceptedEditors.toList());
emit m_instance->editorsClosed(acceptedEditors.toList());
foreach (IEditor *editor, acceptedEditors) {
delete editor;
......@@ -1141,7 +1125,7 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
}
if (!currentEditor()) {
emit currentEditorChanged(0);
emit m_instance->currentEditorChanged(0);
updateActions();
updateWindowTitle();
}
......@@ -1149,7 +1133,7 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
return !closingFailed;
}
Core::IEditor *EditorManager::pickUnusedEditor(EditorView **foundView) const
Core::IEditor *EditorManager::pickUnusedEditor(EditorView **foundView)
{
foreach (IEditor *editor,
d->m_documentModel->editorsForDocuments(d->m_documentModel->openedDocuments())) {
......@@ -1413,13 +1397,13 @@ void EditorManager::addEditor(IEditor *editor)
if (!isTemporary)
DocumentManager::addToRecentFiles(editor->document()->filePath(), editor->id());
}
emit editorOpened(editor);
emit m_instance->editorOpened(editor);
}
// Run the OpenWithDialog and return the editor id
// selected by the user.
Core::Id EditorManager::getOpenWithEditorId(const QString &fileName,
bool *isExternalEditor) const
bool *isExternalEditor)
{
// Collect editors that can open the file
MimeType mt = ICore::mimeDatabase()->findByFile(fileName);
......@@ -1619,7 +1603,7 @@ bool EditorManager::openExternalEditor(const QString &fileName, const Core::Id &
return ok;
}
QStringList EditorManager::getOpenFileNames() const
QStringList EditorManager::getOpenFileNames()
{
QString selectedFilter;
const QString &fileFilters = ICore::mimeDatabase()->allFiltersString(&selectedFilter);
......@@ -1945,12 +1929,12 @@ void EditorManager::updateMakeWritableWarning()
InfoBarEntry info(Id(kMakeWritableWarning),
tr("<b>Warning:</b> This file was not opened in %1 yet.")
.arg(versionControl->displayName()));
info.setCustomButtonInfo(tr("Open"), this, SLOT(vcsOpenCurrentEditor()));
info.setCustomButtonInfo(tr("Open"), m_instance, SLOT(vcsOpenCurrentEditor()));
document->infoBar()->addInfo(info);
} else {
InfoBarEntry info(Id(kMakeWritableWarning),
tr("<b>Warning:</b> You are changing a read-only file."));
info.setCustomButtonInfo(tr("Make Writable"), this, SLOT(makeCurrentEditorWritable()));
info.setCustomButtonInfo(tr("Make Writable"), m_instance, SLOT(makeCurrentEditorWritable()));
document->infoBar()->addInfo(info);
}
} else {
......@@ -1984,10 +1968,10 @@ void EditorManager::updateActions()
if (curDocument) {
if (HostOsInfo::isMacHost())
window()->setWindowModified(curDocument->isModified());
m_instance->window()->setWindowModified(curDocument->isModified());
updateMakeWritableWarning();
} else /* curEditor */ if (HostOsInfo::isMacHost()) {
window()->setWindowModified(false);
m_instance->window()->setWindowModified(false);
}
foreach (SplitterOrView *root, d->m_root)
......@@ -2031,7 +2015,7 @@ void EditorManager::setCloseSplitEnabled(SplitterOrView *splitterOrView, bool en
}
}
bool EditorManager::hasSplitter() const
bool EditorManager::hasSplitter()
{
EditorView *view = currentEditorView();
QTC_ASSERT(view, return false);
......@@ -2040,7 +2024,7 @@ bool EditorManager::hasSplitter() const
return root->isSplitter();
}
QList<IEditor*> EditorManager::visibleEditors() const
QList<IEditor*> EditorManager::visibleEditors()
{
QList<IEditor *> editors;
foreach (SplitterOrView *root, d->m_root) {
......@@ -2098,12 +2082,12 @@ void EditorManager::goForwardInNavigationHistory()
updateActions();
}
OpenEditorsWindow *EditorManager::windowPopup() const
OpenEditorsWindow *EditorManager::windowPopup()
{
return d->m_windowPopup;
}
void EditorManager::showPopupOrSelectDocument() const
void EditorManager::showPopupOrSelectDocument()
{
if (QApplication::keyboardModifiers() == Qt::NoModifier) {
windowPopup()->selectAndHide();
......@@ -2134,7 +2118,7 @@ void EditorManager::showPopupOrSelectDocument() const
}
// Save state of all non-teporary editors.
QByteArray EditorManager::saveState() const
QByteArray EditorManager::saveState()
{
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
......@@ -2323,7 +2307,7 @@ void EditorManager::setReloadSetting(IDocument::ReloadSetting behavior)
d->m_reloadSetting = behavior;
}
IDocument::ReloadSetting EditorManager::reloadSetting() const
IDocument::ReloadSetting EditorManager::reloadSetting()
{
return d->m_reloadSetting;
}
......@@ -2334,7 +2318,7 @@ void EditorManager::setAutoSaveEnabled(bool enabled)
updateAutoSave();
}
bool EditorManager::autoSaveEnabled() const
bool EditorManager::autoSaveEnabled()
{
return d->m_autoSaveEnabled;
}
......@@ -2345,12 +2329,12 @@ void EditorManager::setAutoSaveInterval(int interval)
updateAutoSave();
}
int EditorManager::autoSaveInterval() const
int EditorManager::autoSaveInterval()
{
return d->m_autoSaveInterval;
}
QTextCodec *EditorManager::defaultTextCodec() const
QTextCodec *EditorManager::defaultTextCodec()
{
QSettings *settings = Core::ICore::settings();
if (QTextCodec *candidate = QTextCodec::codecForName(
......@@ -2368,7 +2352,7 @@ Core::IEditor *EditorManager::duplicateEditor(Core::IEditor *editor)
IEditor *duplicate = editor->duplicate(0);
duplicate->restoreState(editor->saveState());
emit editorCreated(duplicate, duplicate->document()->filePath());
emit m_instance->editorCreated(duplicate, duplicate->document()->filePath());
addEditor(duplicate);
return duplicate;
}
......@@ -2502,7 +2486,7 @@ void EditorManager::setWindowTitleAddition(const QString &addition)
updateWindowTitle();
}
QString EditorManager::windowTitleAddition() const
QString EditorManager::windowTitleAddition()
{
return d->m_titleAddition;
}
......
......@@ -95,7 +95,7 @@ public:
typedef QList<IEditorFactory *> EditorFactoryList;
typedef QList<IExternalEditor *> ExternalEditorList;
static EditorManager *instance();
static QWidget *instance();
static EditorToolBar *createToolBar(QWidget *parent = 0);
......@@ -119,74 +119,74 @@ public:
static bool openExternalEditor(const QString &fileName, const Id &editorId);
QStringList getOpenFileNames() const;
Id getOpenWithEditorId(const QString &fileName, bool *isExternalEditor = 0) const;
static QStringList getOpenFileNames();
static Id getOpenWithEditorId(const QString &fileName, bool *isExternalEditor = 0);
static IDocument *currentDocument();
static IEditor *currentEditor();
QList<IEditor *> visibleEditors() const;
static QList<IEditor *> visibleEditors();
static void activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
void activateEditorForEntry(DocumentModel::Entry *entry, OpenEditorFlags flags = 0);
IEditor *activateEditorForDocument(IDocument *document, OpenEditorFlags flags = 0);
IEditor *activateEditorForDocument(Internal::EditorView *view, IDocument *document, OpenEditorFlags flags = 0);
static void activateEditorForEntry(DocumentModel::Entry *entry, OpenEditorFlags flags = 0);
static IEditor *activateEditorForDocument(IDocument *document, OpenEditorFlags flags = 0);
static IEditor *activateEditorForDocument(Internal::EditorView *view, IDocument *document, OpenEditorFlags flags = 0);
static DocumentModel *documentModel();
static bool closeDocuments(const QList<IDocument *> &documents, bool askAboutModifiedEditors = true);
void closeEditor(DocumentModel::Entry *entry);
void closeOtherEditors(IDocument *document);
static void closeEditor(DocumentModel::Entry *entry);
static void closeOtherEditors(IDocument *document);
void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray());
void cutForwardNavigationHistory();
static void addCurrentPositionToNavigationHistory(IEditor *editor = 0, const QByteArray &saveState = QByteArray());
static void cutForwardNavigationHistory();
bool saveEditor(IEditor *editor);
static bool saveEditor(IEditor *editor);
bool closeEditors(const QList<IEditor *> &editorsToClose, bool askAboutModifiedEditors = true);
void closeEditor(IEditor *editor, bool askAboutModifiedEditors = true);
static bool closeEditors(const QList<IEditor *> &editorsToClose, bool askAboutModifiedEditors = true);
static void closeEditor(IEditor *editor, bool askAboutModifiedEditors = true);
MakeWritableResult makeFileWritable(IDocument *document);
static MakeWritableResult makeFileWritable(IDocument *document);
QByteArray saveState() const;
bool restoreState(const QByteArray &state);
bool hasSplitter() const;
static QByteArray saveState();
static bool restoreState(const QByteArray &state);
static bool hasSplitter();
void saveSettings();
void readSettings();
static void saveSettings();
static void readSettings();
Internal::OpenEditorsWindow *windowPopup() const;
void showPopupOrSelectDocument() const;
static Internal::OpenEditorsWindow *windowPopup();
static void showPopupOrSelectDocument();
void showEditorStatusBar(const QString &id,
static void showEditorStatusBar(const QString &id,
const QString &infoText,
const QString &buttonText = QString(),
QObject *object = 0, const char *member = 0);
void hideEditorStatusBar(const QString &id);
static void hideEditorStatusBar(const QString &id);
static EditorFactoryList editorFactories(const MimeType &mimeType, bool bestMatchOnly = true);
static ExternalEditorList externalEditors(const MimeType &mimeType, bool bestMatchOnly = true);
void setReloadSetting(IDocument::ReloadSetting behavior);
IDocument::ReloadSetting reloadSetting() const;
static void setReloadSetting(IDocument::ReloadSetting behavior);
static IDocument::ReloadSetting reloadSetting();
void setAutoSaveEnabled(bool enabled);
bool autoSaveEnabled() const;
void setAutoSaveInterval(int interval);
int autoSaveInterval() const;
static void setAutoSaveEnabled(bool enabled);
static bool autoSaveEnabled();
static void setAutoSaveInterval(int interval);
static int autoSaveInterval();
static bool isAutoSaveFile(const QString &fileName);
QTextCodec *defaultTextCodec() const;
static QTextCodec *defaultTextCodec();
static qint64 maxTextFileSize();
void setWindowTitleAddition(const QString &addition);
QString windowTitleAddition() const;
static void setWindowTitleAddition(const QString &addition);
static QString windowTitleAddition();
static void setWindowTitleVcsTopic(const QString &topic);
static QString windowTitleVcsTopic();
void addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentModel::Entry *entry);
void addNativeDirActions(QMenu *contextMenu, DocumentModel::Entry *entry);
static void addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentModel::Entry *entry);
static void addNativeDirActions(QMenu *contextMenu, DocumentModel::Entry *entry);
signals:
void currentEditorChanged(Core::IEditor *editor);
......@@ -197,88 +197,88 @@ signals:
void editorsClosed(QList<Core::IEditor *> editors);
public slots:
bool closeAllEditors(bool askAboutModifiedEditors = true);
static bool closeAllEditors(bool askAboutModifiedEditors = true);
bool saveDocument(Core::IDocument *documentParam = 0);
bool saveDocumentAs(Core::IDocument *documentParam = 0);
void revertToSaved();
void revertToSaved(IDocument *document);
void closeEditor();
void closeOtherEditors();
void doEscapeKeyFocusMoveMagic();
static bool saveDocument(Core::IDocument *documentParam = 0);
static bool saveDocumentAs(Core::IDocument *documentParam = 0);
static void revertToSaved();
static void revertToSaved(IDocument *document);
static void closeEditor();
static void closeOtherEditors();
static void doEscapeKeyFocusMoveMagic();
private slots:
void gotoNextDocHistory();
void gotoPreviousDocHistory();
void handleContextChange(const QList<Core::IContext *> &context);
void updateActions();
void makeCurrentEditorWritable();
void vcsOpenCurrentEditor();
void updateWindowTitle();
static void gotoNextDocHistory();
static void gotoPreviousDocHistory();
static void handleContextChange(const QList<Core::IContext *> &context);
static void updateActions();
static void makeCurrentEditorWritable();
static void vcsOpenCurrentEditor();
static void updateWindowTitle();
void handleDocumentStateChange();
void updateVariable(const QByteArray &variable);
void autoSave();
static void updateVariable(const QByteArray &variable);
static void autoSave();
void saveDocumentFromContextMenu();
void saveDocumentAsFromContextMenu();
void revertToSavedFromContextMenu();
static void saveDocumentFromContextMenu();
static void saveDocumentAsFromContextMenu();
static void revertToSavedFromContextMenu();
void closeEditorFromContextMenu();
void closeOtherEditorsFromContextMenu();
static void closeEditorFromContextMenu();
static void closeOtherEditorsFromContextMenu();
void showInGraphicalShell();
void openTerminal();
static void showInGraphicalShell();
static void openTerminal();
void rootDestroyed(QObject *root);
void setCurrentEditorFromContextChange();
static void rootDestroyed(QObject *root);
static void setCurrentEditorFromContextChange();
void gotoNextSplit();
static void gotoNextSplit();
public slots:
void goBackInNavigationHistory();
void goForwardInNavigationHistory();
void split(Qt::Orientation orientation);
void split();
void splitSideBySide();
void splitNewWindow();
void removeCurrentSplit();
void removeAllSplits();
void gotoOtherSplit();
static void goBackInNavigationHistory();
static void goForwardInNavigationHistory();
static void split(Qt::Orientation orientation);
static void split();
static void splitSideBySide();
static void splitNewWindow();
static void removeCurrentSplit();
static void removeAllSplits();
static void gotoOtherSplit();
private:
explicit EditorManager(QWidget *parent);
virtual ~EditorManager();
void init();
~EditorManager();
static void init();
static IEditor *createEditor(const Id &id = Id(), const QString &fileName = QString());
void addEditor(IEditor *editor);
void removeEditor(IEditor *editor);