Commit 3557603b authored by Eike Ziller's avatar Eike Ziller

Editors: Don't use model index outside of model/view.

The model index is an internal detail of the model/view and shouldn't be
used outside that context.

Change-Id: I2c1f742fff427484f6ff244dd3e0d8428a7318d3
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 02512e23
......@@ -212,7 +212,7 @@ public:
QAction *m_closeOtherEditorsContextAction;
QAction *m_openGraphicalShellAction;
QAction *m_openTerminalAction;
QModelIndex m_contextMenuEditorIndex;
OpenEditorsModel::Entry *m_contextMenuEntry;
Internal::OpenEditorsWindow *m_windowPopup;
Internal::EditorClosingCoreListener *m_coreListener;
......@@ -812,16 +812,16 @@ static void assignAction(QAction *self, QAction *other)
self->setIconVisibleInMenu(other->isIconVisibleInMenu());
}
void EditorManager::addSaveAndCloseEditorActions(QMenu *contextMenu, const QModelIndex &editorIndex)
void EditorManager::addSaveAndCloseEditorActions(QMenu *contextMenu, OpenEditorsModel::Entry *entry)
{
QTC_ASSERT(contextMenu, return);
d->m_contextMenuEditorIndex = editorIndex;
d->m_contextMenuEntry = entry;
assignAction(d->m_saveCurrentEditorContextAction, ActionManager::command(Constants::SAVE)->action());
assignAction(d->m_saveAsCurrentEditorContextAction, ActionManager::command(Constants::SAVEAS)->action());
assignAction(d->m_revertToSavedCurrentEditorContextAction, ActionManager::command(Constants::REVERTTOSAVED)->action());
IEditor *editor = d->m_contextMenuEditorIndex.data(Qt::UserRole).value<Core::IEditor*>();
IEditor *editor = entry ? entry->editor : 0;
setupSaveActions(editor,
d->m_saveCurrentEditorContextAction,
......@@ -835,25 +835,26 @@ void EditorManager::addSaveAndCloseEditorActions(QMenu *contextMenu, const QMode
contextMenu->addSeparator();
d->m_closeCurrentEditorContextAction->setText(editorIndex.isValid()
? tr("Close \"%1\"").arg(editorIndex.data().toString())
d->m_closeCurrentEditorContextAction->setText(entry
? tr("Close \"%1\"").arg(entry->displayName())
: tr("Close Editor"));
d->m_closeOtherEditorsContextAction->setText(editorIndex.isValid()
? tr("Close All Except \"%1\"").arg(editorIndex.data().toString())
d->m_closeOtherEditorsContextAction->setText(entry
? tr("Close All Except \"%1\"").arg(entry->displayName())
: tr("Close Other Editors"));
d->m_closeCurrentEditorContextAction->setEnabled(editorIndex.isValid());
d->m_closeOtherEditorsContextAction->setEnabled(editorIndex.isValid());
d->m_closeCurrentEditorContextAction->setEnabled(entry != 0);
d->m_closeOtherEditorsContextAction->setEnabled(entry != 0);
d->m_closeAllEditorsContextAction->setEnabled(!openedEditors().isEmpty());
contextMenu->addAction(d->m_closeCurrentEditorContextAction);
contextMenu->addAction(d->m_closeAllEditorsContextAction);
contextMenu->addAction(d->m_closeOtherEditorsContextAction);
}
void EditorManager::addNativeDirActions(QMenu *contextMenu, const QModelIndex &editorIndex)
void EditorManager::addNativeDirActions(QMenu *contextMenu, OpenEditorsModel::Entry *entry)
{
QTC_ASSERT(contextMenu, return);
d->m_openGraphicalShellAction->setEnabled(editorIndex.isValid());
d->m_openTerminalAction->setEnabled(editorIndex.isValid());
bool enabled = entry && !entry->fileName().isEmpty();
d->m_openGraphicalShellAction->setEnabled(enabled);
d->m_openTerminalAction->setEnabled(enabled);
contextMenu->addAction(d->m_openGraphicalShellAction);
contextMenu->addAction(d->m_openTerminalAction);
}
......@@ -953,45 +954,50 @@ void EditorManager::doEscapeKeyFocusMoveMagic()
void EditorManager::saveDocumentFromContextMenu()
{
IEditor *editor = d->m_contextMenuEditorIndex.data(Qt::UserRole).value<Core::IEditor*>();
IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0;
if (editor)
saveDocument(editor->document());
}
void EditorManager::saveDocumentAsFromContextMenu()
{
IEditor *editor = d->m_contextMenuEditorIndex.data(Qt::UserRole).value<Core::IEditor*>();
IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0;
if (editor)
saveDocumentAs(editor->document());
}
void EditorManager::revertToSavedFromContextMenu()
{
IEditor *editor = d->m_contextMenuEditorIndex.data(Qt::UserRole).value<Core::IEditor*>();
IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0;
if (editor)
revertToSaved(editor);
}
void EditorManager::closeEditorFromContextMenu()
{
closeEditor(d->m_contextMenuEditorIndex);
IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0;
if (editor)
closeEditor(editor);
}
void EditorManager::closeOtherEditorsFromContextMenu()
{
closeOtherEditors(d->m_contextMenuEditorIndex.data(Qt::UserRole).value<IEditor *>());
IEditor *editor = d->m_contextMenuEntry ? d->m_contextMenuEntry->editor : 0;
closeOtherEditors(editor);
}
void EditorManager::showInGraphicalShell()
{
const QString path = d->m_contextMenuEditorIndex.data(Qt::UserRole + 1).toString();
Core::FileUtils::showInGraphicalShell(ICore::mainWindow(), path);
if (!d->m_contextMenuEntry || d->m_contextMenuEntry->fileName().isEmpty())
return;
Core::FileUtils::showInGraphicalShell(ICore::mainWindow(), d->m_contextMenuEntry->fileName());
}
void EditorManager::openTerminal()
{
const QString path = QFileInfo(d->m_contextMenuEditorIndex.data(Qt::UserRole + 1).toString()).path();
Core::FileUtils::openTerminal(path);
if (!d->m_contextMenuEntry || d->m_contextMenuEntry->fileName().isEmpty())
return;
Core::FileUtils::openTerminal(QFileInfo(d->m_contextMenuEntry->fileName()).path());
}
void EditorManager::rootDestroyed(QObject *root)
......@@ -1057,13 +1063,14 @@ void EditorManager::closeEditor(Core::IEditor *editor)
closeEditors(QList<IEditor *>() << editor);
}
void EditorManager::closeEditor(const QModelIndex &index)
void EditorManager::closeEditor(OpenEditorsModel::Entry *entry)
{
IEditor *editor = index.data(Qt::UserRole).value<Core::IEditor*>();
if (editor)
closeEditor(editor);
if (!entry)
return;
if (entry->editor)
closeEditor(entry->editor);
else
d->m_editorModel->removeEditor(index);
d->m_editorModel->removeEntry(entry);
}
bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool askAboutModifiedEditors)
......@@ -1143,20 +1150,26 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
}
}
bool currentViewHandled = false;
foreach (EditorView *view, closedViews) {
OpenEditorFlags flags;
if (view == currentView)
currentViewHandled = true;
else
flags = OpenEditorFlags(DoNotChangeCurrentEditor);
IEditor *newCurrent = view->currentEditor();
if (!newCurrent)
newCurrent = pickUnusedEditor();
if (newCurrent) {
activateEditor(view, newCurrent, DoNotChangeCurrentEditor);
activateEditor(view, newCurrent, flags);
} else {
QModelIndex idx = d->m_editorModel->firstRestoredEditor();
if (idx.isValid()) {
activateEditorForIndex(view, idx, DoNotChangeCurrentEditor);
OpenEditorsModel::Entry *entry = d->m_editorModel->firstRestoredEditor();
if (entry) {
activateEditorForEntry(view, entry, flags);
} else {
const QList<IEditor *> editors = d->m_editorModel->editors();
if (!editors.isEmpty())
activateEditor(view, editors.last(), DoNotChangeCurrentEditor);
activateEditor(view, editors.last(), flags);
}
}
}
......@@ -1167,7 +1180,7 @@ bool EditorManager::closeEditors(const QList<IEditor*> &editorsToClose, bool ask
delete editor;
}
if (currentView) {
if (currentView && !currentViewHandled) {
if (IEditor *editor = currentView->currentEditor())
activateEditor(currentView, editor);
else
......@@ -1213,9 +1226,9 @@ void EditorManager::closeDuplicate(Core::IEditor *editor)
if (newCurrent) {
activateEditor(view, newCurrent, DoNotChangeCurrentEditor);
} else {
QModelIndex idx = d->m_editorModel->firstRestoredEditor();
if (idx.isValid())
activateEditorForIndex(view, idx, DoNotChangeCurrentEditor);
OpenEditorsModel::Entry *entry = d->m_editorModel->firstRestoredEditor();
if (entry)
activateEditorForEntry(view, entry, DoNotChangeCurrentEditor);
}
}
......@@ -1237,24 +1250,23 @@ Core::IEditor *EditorManager::pickUnusedEditor() const
return 0;
}
void EditorManager::activateEditorForIndex(const QModelIndex &index, OpenEditorFlags flags)
void EditorManager::activateEditorForEntry(OpenEditorsModel::Entry *entry, OpenEditorFlags flags)
{
activateEditorForIndex(currentEditorView(), index, flags);
activateEditorForEntry(currentEditorView(), entry, flags);
}
void EditorManager::activateEditorForIndex(Internal::EditorView *view, const QModelIndex &index, OpenEditorFlags flags)
void EditorManager::activateEditorForEntry(Internal::EditorView *view, OpenEditorsModel::Entry *entry, OpenEditorFlags flags)
{
Q_ASSERT(view);
IEditor *editor = index.data(Qt::UserRole).value<IEditor*>();
QTC_ASSERT(view, return);
QTC_ASSERT(entry, return);
IEditor *editor = entry->editor;
if (editor) {
activateEditor(view, editor, flags);
return;
}
QString fileName = index.data(Qt::UserRole + 1).toString();
Core::Id id = index.data(Qt::UserRole + 2).value<Core::Id>();
if (!openEditor(view, fileName, id, flags))
d->m_editorModel->removeEditor(index);
if (!openEditor(view, entry->fileName(), entry->id(), flags))
d->m_editorModel->removeEntry(entry);
}
void EditorManager::activateView(EditorView *view)
......@@ -2226,19 +2238,19 @@ QByteArray EditorManager::saveState() const
stream << d->m_editorStates;
QList<OpenEditorsModel::Entry> entries = d->m_editorModel->entries();
QList<OpenEditorsModel::Entry *> entries = d->m_editorModel->entries();
int entriesCount = 0;
foreach (const OpenEditorsModel::Entry &entry, entries) {
foreach (OpenEditorsModel::Entry *entry, entries) {
// The editor may be 0 if it was not loaded yet: In that case it is not temporary
if (!entry.editor || !entry.editor->isTemporary())
if (!entry->editor || !entry->editor->isTemporary())
++entriesCount;
}
stream << entriesCount;
foreach (const OpenEditorsModel::Entry &entry, entries) {
if (!entry.editor || !entry.editor->isTemporary())
stream << entry.fileName() << entry.displayName() << entry.id();
foreach (OpenEditorsModel::Entry *entry, entries) {
if (!entry->editor || !entry->editor->isTemporary())
stream << entry->fileName() << entry->displayName() << entry->id();
}
stream << d->m_root.first()->saveState(); // TODO
......
......@@ -32,6 +32,8 @@
#include "../core_global.h"
#include "openeditorsmodel.h"
#include <coreplugin/id.h>
#include <coreplugin/idocument.h> // enumerations
......@@ -40,7 +42,6 @@
#include <QMenu>
QT_BEGIN_NAMESPACE
class QModelIndex;
QT_END_NAMESPACE
namespace Core {
......@@ -63,8 +64,6 @@ enum MakeWritableResult {
Failed
};
class OpenEditorsModel;
namespace Internal {
class EditorClosingCoreListener;
class EditorView;
......@@ -132,11 +131,11 @@ public:
QList<IEditor*> openedEditors() const;
static void activateEditor(IEditor *editor, OpenEditorFlags flags = 0);
void activateEditorForIndex(const QModelIndex &index, OpenEditorFlags = 0);
void activateEditorForEntry(OpenEditorsModel::Entry *entry, OpenEditorFlags flags = 0);
IEditor *activateEditorForDocument(Internal::EditorView *view, IDocument *document, OpenEditorFlags flags = 0);
OpenEditorsModel *openedEditorsModel() const;
void closeEditor(const QModelIndex &index);
void closeEditor(OpenEditorsModel::Entry *entry);
void closeOtherEditors(IEditor *editor);
QList<IEditor*> editorsForDocuments(QList<IDocument *> documents) const;
......@@ -184,8 +183,8 @@ public:
void setWindowTitleAddition(const QString &addition);
QString windowTitleAddition() const;
void addSaveAndCloseEditorActions(QMenu *contextMenu, const QModelIndex &editorIndex);
void addNativeDirActions(QMenu *contextMenu, const QModelIndex &editorIndex);
void addSaveAndCloseEditorActions(QMenu *contextMenu, OpenEditorsModel::Entry *entry);
void addNativeDirActions(QMenu *contextMenu, OpenEditorsModel::Entry *entry);
signals:
void currentEditorChanged(Core::IEditor *editor);
......@@ -259,7 +258,7 @@ private:
IEditor *placeEditor(Internal::EditorView *view, IEditor *editor);
IEditor *duplicateEditor(IEditor *editor);
IEditor *activateEditor(Internal::EditorView *view, IEditor *editor, OpenEditorFlags flags = 0);
void activateEditorForIndex(Internal::EditorView *view, const QModelIndex &index, OpenEditorFlags = 0);
void activateEditorForEntry(Internal::EditorView *view, OpenEditorsModel::Entry *entry, OpenEditorFlags flags = 0);
void activateView(Internal::EditorView *view);
IEditor *openEditor(Internal::EditorView *view, const QString &fileName,
const Id &id = Id(), OpenEditorFlags flags = 0, bool *newEditor = 0);
......
......@@ -291,15 +291,15 @@ void EditorView::removeEditor(IEditor *editor)
IEditor *EditorView::currentEditor() const
{
if (m_container->count() > 0)
if (m_editors.size() > 0)
return m_widgetEditorMap.value(m_container->currentWidget());
return 0;
}
void EditorView::listSelectionActivated(int index)
{
QAbstractItemModel *model = EditorManager::instance()->openedEditorsModel();
EditorManager::instance()->activateEditorForIndex(this, model->index(index, 0));
OpenEditorsModel *model = EditorManager::instance()->openedEditorsModel();
EditorManager::instance()->activateEditorForEntry(this, model->entryAtRow(index));
}
void EditorView::splitHorizontally()
......@@ -338,7 +338,7 @@ void EditorView::setParentSplitterOrView(SplitterOrView *splitterOrView)
void EditorView::setCurrentEditor(IEditor *editor)
{
if (!editor || m_container->count() <= 0
|| m_container->indexOf(editor->widget()) == -1) {
|| m_container->indexOf(editor->widget()) == -1) {
m_toolBar->updateEditorStatus(0);
m_infoBarDisplay->setInfoBar(0);
QTC_CHECK(m_container->count() == 0);
......@@ -360,12 +360,12 @@ void EditorView::setCurrentEditor(IEditor *editor)
int EditorView::editorCount() const
{
return m_container->count();
return m_editors.size();
}
QList<IEditor *> EditorView::editors() const
{
return m_widgetEditorMap.values();
return m_editors;
}
void EditorView::updateEditorHistory(IEditor *editor)
......@@ -788,9 +788,9 @@ void SplitterOrView::restoreState(const QByteArray &state)
| Core::EditorManager::DoNotChangeCurrentEditor);
if (!e) {
QModelIndex idx = em->openedEditorsModel()->firstRestoredEditor();
if (idx.isValid())
em->activateEditorForIndex(view(), idx, Core::EditorManager::IgnoreNavigationHistory
OpenEditorsModel::Entry *entry = em->openedEditorsModel()->firstRestoredEditor();
if (entry)
em->activateEditorForEntry(view(), entry, Core::EditorManager::IgnoreNavigationHistory
| Core::EditorManager::DoNotChangeCurrentEditor);
}
......
......@@ -31,6 +31,8 @@
#include "ieditor.h"
#include "idocument.h"
#include <utils/qtcassert.h>
#include <QDir>
#include <QIcon>
......@@ -43,7 +45,7 @@ struct OpenEditorsModelPrivate
const QIcon m_lockedIcon;
const QIcon m_unlockedIcon;
QList<OpenEditorsModel::Entry> m_editors;
QList<OpenEditorsModel::Entry *> m_editors;
QList<IEditor *> m_duplicateEditors;
};
......@@ -93,8 +95,9 @@ Id OpenEditorsModel::Entry::id() const
int OpenEditorsModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
if (!parent.isValid())
return 2;
return 0;
}
int OpenEditorsModel::rowCount(const QModelIndex &parent) const
......@@ -107,9 +110,9 @@ int OpenEditorsModel::rowCount(const QModelIndex &parent) const
QList<IEditor *> OpenEditorsModel::editors() const
{
QList<IEditor *> result;
foreach (const Entry &entry, d->m_editors)
if (entry.editor)
result += entry.editor;
foreach (const Entry *entry, d->m_editors)
if (entry->editor)
result += entry->editor;
return result;
}
......@@ -123,52 +126,52 @@ void OpenEditorsModel::addEditor(IEditor *editor, bool isDuplicate)
return;
}
Entry entry;
entry.editor = editor;
Entry *entry = new Entry;
entry->editor = editor;
addEntry(entry);
}
void OpenEditorsModel::addRestoredEditor(const QString &fileName, const QString &displayName, const Id &id)
{
Entry entry;
entry.m_fileName = fileName;
entry.m_displayName = displayName;
entry.m_id = id;
Entry *entry = new Entry;
entry->m_fileName = fileName;
entry->m_displayName = displayName;
entry->m_id = id;
addEntry(entry);
}
QModelIndex OpenEditorsModel::firstRestoredEditor() const
OpenEditorsModel::Entry *OpenEditorsModel::firstRestoredEditor() const
{
for (int i = 0; i < d->m_editors.count(); ++i)
if (!d->m_editors.at(i).editor)
return createIndex(i, 0);
return QModelIndex();
if (!d->m_editors.at(i)->editor)
return d->m_editors.at(i);
return 0;
}
void OpenEditorsModel::addEntry(const Entry &entry)
void OpenEditorsModel::addEntry(Entry *entry)
{
QString fileName = entry.fileName();
QString fileName = entry->fileName();
int previousIndex = findFileName(fileName);
if (previousIndex >= 0) {
if (entry.editor && d->m_editors.at(previousIndex).editor == 0) {
if (entry->editor && d->m_editors.at(previousIndex)->editor == 0) {
d->m_editors[previousIndex] = entry;
connect(entry.editor, SIGNAL(changed()), this, SLOT(itemChanged()));
connect(entry->editor, SIGNAL(changed()), this, SLOT(itemChanged()));
}
return;
}
int index;
QString displayName = entry.displayName();
QString displayName = entry->displayName();
for (index = 0; index < d->m_editors.count(); ++index) {
if (displayName < d->m_editors.at(index).displayName())
if (displayName < d->m_editors.at(index)->displayName())
break;
}
beginInsertRows(QModelIndex(), index, index);
int row = index + 1/*<no document>*/;
beginInsertRows(QModelIndex(), row, row);
d->m_editors.insert(index, entry);
if (entry.editor)
connect(entry.editor, SIGNAL(changed()), this, SLOT(itemChanged()));
if (entry->editor)
connect(entry->editor, SIGNAL(changed()), this, SLOT(itemChanged()));
endInsertRows();
}
......@@ -176,7 +179,7 @@ void OpenEditorsModel::addEntry(const Entry &entry)
int OpenEditorsModel::findEditor(IEditor *editor) const
{
for (int i = 0; i < d->m_editors.count(); ++i)
if (d->m_editors.at(i).editor == editor)
if (d->m_editors.at(i)->editor == editor)
return i;
return -1;
}
......@@ -186,21 +189,22 @@ int OpenEditorsModel::findFileName(const QString &filename) const
if (filename.isEmpty())
return -1;
for (int i = 0; i < d->m_editors.count(); ++i) {
if (d->m_editors.at(i).fileName() == filename)
if (d->m_editors.at(i)->fileName() == filename)
return i;
}
return -1;
}
void OpenEditorsModel::removeEditor(IEditor *editor)
void OpenEditorsModel::removeEntry(OpenEditorsModel::Entry *entry)
{
d->m_duplicateEditors.removeAll(editor);
removeEditor(findEditor(editor));
int index = d->m_editors.indexOf(entry);
removeEditor(index);
}
void OpenEditorsModel::removeEditor(const QModelIndex &index)
void OpenEditorsModel::removeEditor(IEditor *editor)
{
removeEditor(index.row());
d->m_duplicateEditors.removeAll(editor);
removeEditor(findEditor(editor));
}
void OpenEditorsModel::removeEditor(const QString &fileName)
......@@ -210,10 +214,11 @@ void OpenEditorsModel::removeEditor(const QString &fileName)
void OpenEditorsModel::removeEditor(int idx)
{
if (idx < 0)
if (idx < 0 || idx >= d->m_editors.size())
return;
IEditor *editor= d->m_editors.at(idx).editor;
beginRemoveRows(QModelIndex(), idx, idx);
IEditor *editor = d->m_editors.at(idx)->editor;
int row = idx;
beginRemoveRows(QModelIndex(), row, row);
d->m_editors.removeAt(idx);
endRemoveRows();
if (editor)
......@@ -223,24 +228,15 @@ void OpenEditorsModel::removeEditor(int idx)
void OpenEditorsModel::removeAllRestoredEditors()
{
for (int i = d->m_editors.count()-1; i >= 0; --i) {
if (!d->m_editors.at(i).editor) {
beginRemoveRows(QModelIndex(), i, i);
if (!d->m_editors.at(i)->editor) {
int row = i;
beginRemoveRows(QModelIndex(), row, row);
d->m_editors.removeAt(i);
endRemoveRows();
}
}
}
QList<OpenEditorsModel::Entry> OpenEditorsModel::restoredEditors() const
{
QList<Entry> result;
for (int i = d->m_editors.count()-1; i >= 0; --i) {
if (!d->m_editors.at(i).editor)
result.append(d->m_editors.at(i));
}
return result;
}
bool OpenEditorsModel::isDuplicate(IEditor *editor) const
{
return editor && d->m_duplicateEditors.contains(editor);
......@@ -249,9 +245,9 @@ bool OpenEditorsModel::isDuplicate(IEditor *editor) const
IEditor *OpenEditorsModel::originalForDuplicate(IEditor *duplicate) const
{
IDocument *document = duplicate->document();
foreach (const Entry &e, d->m_editors)
if (e.editor && e.editor->document() == document)
return e.editor;
foreach (const Entry *e, d->m_editors)
if (e->editor && e->editor->document() == document)
return e->editor;
return 0;
}
......@@ -271,13 +267,20 @@ void OpenEditorsModel::makeOriginal(IEditor *duplicate)
IEditor *original = originalForDuplicate(duplicate);
Q_ASSERT(original);
int i = findEditor(original);
d->m_editors[i].editor = duplicate;
d->m_editors[i]->editor = duplicate;
d->m_duplicateEditors.removeOne(duplicate);
d->m_duplicateEditors.append(original);
disconnect(original, SIGNAL(changed()), this, SLOT(itemChanged()));
connect(duplicate, SIGNAL(changed()), this, SLOT(itemChanged()));
}
int OpenEditorsModel::indexOfEditor(IEditor *editor) const
{
if (!editor)
return -1;
return findEditor(editor);
}