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
......@@ -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()
......@@ -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);
}
......
......@@ -60,12 +60,6 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
int openDocumentCount() const;
void addEditor(IEditor *editor, bool isDuplicate = false);
void addRestoredEditor(const QString &fileName, const QString &displayName, const Id &id);
QModelIndex firstRestoredEditor() const;
struct CORE_EXPORT Entry {
Entry();
IEditor *editor;
......@@ -76,24 +70,30 @@ public:
QString m_displayName;
Id m_id;
};
QList<Entry> entries() const;
IEditor *editorAt(int row) const;
Entry *entryAtRow(int row) const;
int rowOfEditor(IEditor *editor) const;
int openDocumentCount() const;
void addEditor(IEditor *editor, bool isDuplicate = false);
void addRestoredEditor(const QString &fileName, const QString &displayName, const Id &id);
Entry *firstRestoredEditor() const;
QList<Entry *> entries() const;
void removeEntry(Entry *entry);
void removeEditor(IEditor *editor);
void removeEditor(const QModelIndex &index);
void removeEditor(const QString &fileName);
void removeAllRestoredEditors();
void emitDataChanged(IEditor *editor);
QList<IEditor *> editors() const;
QList<Entry> restoredEditors() const;
bool isDuplicate(IEditor *editor) const;
QList<IEditor *> duplicatesFor(IEditor *editor) const;
IEditor *originalForDuplicate(IEditor *duplicate) const;
void makeOriginal(IEditor *duplicate);
QModelIndex indexOf(IEditor *editor) const;
int indexOfEditor(IEditor *editor) const;
QString displayNameForDocument(IDocument *document) const;
......@@ -101,10 +101,11 @@ private slots:
void itemChanged();
private:
void addEntry(const Entry &entry);
void addEntry(Entry *entry);
int findEditor(IEditor *editor) const;
int findFileName(const QString &filename) const;
void removeEditor(int idx);
void emitDataChanged(IEditor *editor);
OpenEditorsModelPrivate *d;
};
......
......@@ -124,12 +124,13 @@ OpenEditorsWidget::~OpenEditorsWidget()
void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
{
if (!editor) {
EditorManager *em = EditorManager::instance();
QModelIndex index = model()->index(em->openedEditorsModel()->rowOfEditor(editor), 0);
if (!index.isValid()) {
clearSelection();
return;
}
EditorManager *em = EditorManager::instance();
setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
setCurrentIndex(index);
selectionModel()->select(currentIndex(),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
scrollTo(currentIndex());
......@@ -189,12 +190,14 @@ void OpenEditorsWidget::handleClicked(const QModelIndex &index)
void OpenEditorsWidget::activateEditor(const QModelIndex &index)
{
selectionModel()->select(index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
EditorManager::instance()->activateEditorForIndex(index);
EditorManager *em = EditorManager::instance();
em->activateEditorForEntry(em->openedEditorsModel()->entryAtRow(index.row()));
}
void OpenEditorsWidget::closeEditor(const QModelIndex &index)
{
EditorManager::instance()->closeEditor(index);
EditorManager *em = EditorManager::instance();
em->closeEditor(em->openedEditorsModel()->entryAtRow(index.row()));
// work around selection changes
updateCurrentItem(EditorManager::currentEditor());
}
......@@ -203,9 +206,11 @@ void OpenEditorsWidget::contextMenuRequested(QPoint pos)
{
QMenu contextMenu;
QModelIndex editorIndex = indexAt(pos);
EditorManager::instance()->addSaveAndCloseEditorActions(&contextMenu, editorIndex);
OpenEditorsModel::Entry *entry = EditorManager::instance()->openedEditorsModel()->entryAtRow(
editorIndex.row());
EditorManager::instance()->addSaveAndCloseEditorActions(&contextMenu, entry);
contextMenu.addSeparator();
EditorManager::instance()->addNativeDirActions(&contextMenu, editorIndex);
EditorManager::instance()->addNativeDirActions(&contextMenu, entry);
contextMenu.exec(mapToGlobal(pos));
}
......
......@@ -203,15 +203,15 @@ void OpenEditorsWindow::setEditors(const QList<EditLocation> &globalHistory, Edi
addHistoryItems(globalHistory, view, model, documentsDone);
// add purely restored editors which are not initialised yet
foreach (const OpenEditorsModel::Entry &entry, model->entries()) {
if (entry.editor)
foreach (OpenEditorsModel::Entry *entry, model->entries()) {
if (entry->editor)
continue;
QTreeWidgetItem *item = new QTreeWidgetItem();
QString title = entry.displayName();
QString title = entry->displayName();
item->setIcon(0, m_emptyIcon);
item->setText(0, title);
item->setToolTip(0, entry.fileName());
item->setData(0, Qt::UserRole+2, QVariant::fromValue(entry.id()));
item->setToolTip(0, entry->fileName());
item->setData(0, Qt::UserRole+2, QVariant::fromValue(entry->id()));
item->setTextAlignment(0, Qt::AlignLeft);
m_editorList->addTopLevelItem(item);
......
......@@ -290,13 +290,12 @@ void EditorToolBar::setToolbarCreationFlags(ToolbarCreationFlags flags)
void EditorToolBar::setCurrentEditor(IEditor *editor)
{
QTC_ASSERT(editor, return);
d->m_editorList->setCurrentIndex(d->m_editorsListModel->indexOf(editor).row());
d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfEditor(editor));
// If we never added the toolbar from the editor, we will never change
// the editor, so there's no need to update the toolbar either.
if (!d->m_isStandalone)
updateToolBar(editor->toolBar());
updateToolBar(editor ? editor->toolBar() : 0);
updateEditorStatus(editor);
}
......@@ -304,28 +303,28 @@ void EditorToolBar::setCurrentEditor(IEditor *editor)
void EditorToolBar::updateEditorListSelection(IEditor *newSelection)
{
if (newSelection)
d->m_editorList->setCurrentIndex(d->m_editorsListModel->indexOf(newSelection).row());
d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfEditor(newSelection));
}
void EditorToolBar::changeActiveEditor(int row)
{
EditorManager *em = ICore::editorManager();
QAbstractItemModel *model = d->m_editorList->model();
em->activateEditorForIndex(model->index(row, 0));
EditorManager *em = EditorManager::instance();
em->activateEditorForEntry(d->m_editorsListModel->entryAtRow(row));
}
void EditorToolBar::listContextMenu(QPoint pos)
{
QModelIndex index = d->m_editorsListModel->index(d->m_editorList->currentIndex(), 0);
QString fileName = d->m_editorsListModel->data(index, Qt::UserRole + 1).toString();
OpenEditorsModel::Entry *entry = EditorManager::instance()
->openedEditorsModel()->entryAtRow(d->m_editorList->currentIndex());
QString fileName = entry ? entry->fileName() : QString();
if (fileName.isEmpty())
return;
QMenu menu;
QAction *copyPath = menu.addAction(tr("Copy Full Path to Clipboard"));
menu.addSeparator();
EditorManager::instance()->addSaveAndCloseEditorActions(&menu, index);
EditorManager::instance()->addSaveAndCloseEditorActions(&menu, entry);
menu.addSeparator();
EditorManager::instance()->addNativeDirActions(&menu, index);
EditorManager::instance()->addNativeDirActions(&menu, entry);
QAction *result = menu.exec(d->m_editorList->mapToGlobal(pos));
if (result == copyPath)
QApplication::clipboard()->setText(QDir::toNativeSeparators(fileName));
......@@ -376,7 +375,7 @@ void EditorToolBar::updateEditorStatus(IEditor *editor)
return;
}
d->m_editorList->setCurrentIndex(d->m_editorsListModel->indexOf(editor).row());
d->m_editorList->setCurrentIndex(d->m_editorsListModel->rowOfEditor(editor));
if (editor->document()->fileName().isEmpty()) {
d->m_lockButton->setIcon(QIcon());
......
......@@ -1999,19 +1999,19 @@ int FakeVimPluginPrivate::currentFile() const
{
OpenEditorsModel *model = EditorManager::instance()->openedEditorsModel();
IEditor *editor = EditorManager::currentEditor();
return model->indexOf(editor).row();
return model->indexOfEditor(editor);
}
void FakeVimPluginPrivate::switchToFile(int n)
{
EditorManager *editorManager = ICore::editorManager();
OpenEditorsModel *model = editorManager->openedEditorsModel();
int size = model->rowCount();
int size = model->openDocumentCount();
QTC_ASSERT(size, return);
n = n % size;
if (n < 0)
n += size;
editorManager->activateEditorForIndex(model->index(n, 0));
editorManager->activateEditorForEntry(model->entries().at(n));
}
ExCommandMap &FakeVimExCommandsPage::exCommandMap()
......
......@@ -87,14 +87,14 @@ QList<FilterEntry> OpenDocumentsFilter::matchesFor(QFutureInterface<Locator::Fil
void OpenDocumentsFilter::refreshInternally()
{
m_editors.clear();
foreach (IEditor *editor, m_editorManager->openedEditors()) {
foreach (OpenEditorsModel::Entry *e, EditorManager::instance()->openedEditorsModel()->entries()) {
OpenEditorsModel::Entry entry;
// don't work on IEditor directly, since that will be useless with split windows
entry.m_displayName = editor->displayName();
entry.m_fileName = editor->document()->fileName();
// create copy with only the information relevant to use
// to avoid model deleting entries behind our back
entry.m_displayName = e->displayName();
entry.m_fileName = e->fileName();
m_editors.append(entry);
}
m_editors += m_editorManager->openedEditorsModel()->restoredEditors();
}
void OpenDocumentsFilter::refresh(QFutureInterface<void> &future)
......
......@@ -51,7 +51,6 @@ QT_END_NAMESPACE
namespace Core {
class SideBar;
class SideBarItem;
class OpenEditorsModel;
class EditorToolBar;
class OutputPanePlaceHolder;
}
......
......@@ -67,9 +67,9 @@ Utils::FileIterator *FindInOpenFiles::files(const QStringList &nameFilters,
QMap<QString, QTextCodec *> openEditorEncodings = ITextEditor::openedTextEditorsEncodings();
QStringList fileNames;
QList<QTextCodec *> codecs;
foreach (const Core::OpenEditorsModel::Entry &entry,
foreach (Core::OpenEditorsModel::Entry *entry,
Core::EditorManager::instance()->openedEditorsModel()->entries()) {
QString fileName = entry.fileName();
QString fileName = entry->fileName();
if (!fileName.isEmpty()) {
fileNames.append(fileName);
QTextCodec *codec = openEditorEncodings.value(fileName);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment