Commit 6e584b5b authored by hjk's avatar hjk
Browse files

DocumentModel: Make interface static



Move item model implementation to private, adjust user code.

Change-Id: Ifbe94e7c7b9b1e8be1b4c531958dbd7a9413af13
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 226c1abc
......@@ -716,7 +716,7 @@ void IndexerPrivate::runCore(const QHash<QString, FileData> & /*headers*/,
QHash<ProjectPart::Ptr, QList<IndexerPrivate::FileData> > parts;
typedef QHash<ProjectPart::Ptr, QList<IndexerPrivate::FileData> >::Iterator PartIter;
QList<Core::IDocument *> docs = Core::EditorManager::documentModel()->openedDocuments();
QList<Core::IDocument *> docs = Core::DocumentModel::openedDocuments();
QSet<QString> openDocs;
foreach (Core::IDocument *doc, docs)
openDocs.insert(doc->filePath());
......
......@@ -197,7 +197,7 @@ bool CMakeProject::parseCMakeLists()
}
CMakeBuildConfiguration *activeBC = static_cast<CMakeBuildConfiguration *>(activeTarget()->activeBuildConfiguration());
foreach (Core::IDocument *document, Core::EditorManager::documentModel()->openedDocuments())
foreach (Core::IDocument *document, Core::DocumentModel::openedDocuments())
if (isProjectFile(document->filePath()))
document->infoBar()->removeInfo("CMakeEditor.RunCMake");
......
......@@ -1434,7 +1434,7 @@ void DocumentManager::executeOpenWithMenuAction(QAction *action)
if (entry.editorFactory) {
// close any open editors that have this file open, but have a different type.
QList<IEditor *> editorsOpenForFile
= EditorManager::documentModel()->editorsForFilePath(entry.fileName);
= DocumentModel::editorsForFilePath(entry.fileName);
if (!editorsOpenForFile.isEmpty()) {
foreach (IEditor *openEditor, editorsOpenForFile) {
if (entry.editorFactory->id() == openEditor->document()->id())
......
......@@ -39,11 +39,31 @@
namespace Core {
struct DocumentModelPrivate
class DocumentModelPrivate : public QAbstractItemModel
{
Q_OBJECT
public:
DocumentModelPrivate();
~DocumentModelPrivate();
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex &/*index*/) const { return QModelIndex(); }
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
void addEntry(DocumentModel::Entry *entry);
void removeDocument(int idx);
int indexOfFilePath(const QString &filePath) const;
int indexOfDocument(IDocument *document) const;
private slots:
friend class DocumentModel;
void itemChanged();
private:
const QIcon m_lockedIcon;
const QIcon m_unlockedIcon;
......@@ -62,36 +82,49 @@ DocumentModelPrivate::~DocumentModelPrivate()
qDeleteAll(m_documents);
}
static DocumentModelPrivate *d;
DocumentModel::Entry::Entry() :
document(0)
{
}
DocumentModel::DocumentModel(QObject *parent) :
QAbstractItemModel(parent), d(new DocumentModelPrivate)
DocumentModel::DocumentModel()
{
}
DocumentModel::~DocumentModel()
void DocumentModel::init()
{
d = new DocumentModelPrivate;
}
void DocumentModel::destroy()
{
delete d;
}
QIcon DocumentModel::lockedIcon() const
QIcon DocumentModel::lockedIcon()
{
return d->m_lockedIcon;
}
QIcon DocumentModel::unlockedIcon() const
QIcon DocumentModel::unlockedIcon()
{
return d->m_unlockedIcon;
}
QString DocumentModel::Entry::fileName() const {
QAbstractItemModel *DocumentModel::model()
{
return d;
}
QString DocumentModel::Entry::fileName() const
{
return document ? document->filePath() : m_fileName;
}
QString DocumentModel::Entry::displayName() const {
QString DocumentModel::Entry::displayName() const
{
return document ? document->displayName() : m_displayName;
}
......@@ -100,22 +133,22 @@ Id DocumentModel::Entry::id() const
return m_id;
}
int DocumentModel::columnCount(const QModelIndex &parent) const
int DocumentModelPrivate::columnCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return 2;
return 0;
}
int DocumentModel::rowCount(const QModelIndex &parent) const
int DocumentModelPrivate::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid())
return d->m_documents.count() + 1/*<no document>*/;
return m_documents.count() + 1/*<no document>*/;
return 0;
}
// TODO remove
QList<IEditor *> DocumentModel::oneEditorForEachOpenedDocument() const
QList<IEditor *> DocumentModel::oneEditorForEachOpenedDocument()
{
QList<IEditor *> result;
QMapIterator<IDocument *, QList<IEditor *> > it(d->m_editors);
......@@ -138,7 +171,7 @@ void DocumentModel::addEditor(IEditor *editor, bool *isNewDocument)
Entry *entry = new Entry;
entry->document = editor->document();
entry->m_id = editor->document()->id();
addEntry(entry);
d->addEntry(entry);
}
}
......@@ -148,10 +181,10 @@ void DocumentModel::addRestoredDocument(const QString &fileName, const QString &
entry->m_fileName = fileName;
entry->m_displayName = displayName;
entry->m_id = id;
addEntry(entry);
d->addEntry(entry);
}
DocumentModel::Entry *DocumentModel::firstRestoredDocument() const
DocumentModel::Entry *DocumentModel::firstRestoredDocument()
{
for (int i = 0; i < d->m_documents.count(); ++i)
if (!d->m_documents.at(i)->document)
......@@ -159,16 +192,16 @@ DocumentModel::Entry *DocumentModel::firstRestoredDocument() const
return 0;
}
void DocumentModel::addEntry(Entry *entry)
void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry)
{
QString fileName = entry->fileName();
// replace a non-loaded entry (aka 'restored') if possible
int previousIndex = indexOfFilePath(fileName);
if (previousIndex >= 0) {
if (entry->document && d->m_documents.at(previousIndex)->document == 0) {
Entry *previousEntry = d->m_documents.at(previousIndex);
d->m_documents[previousIndex] = entry;
if (entry->document && m_documents.at(previousIndex)->document == 0) {
DocumentModel::Entry *previousEntry = m_documents.at(previousIndex);
m_documents[previousIndex] = entry;
delete previousEntry;
connect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged()));
} else {
......@@ -179,19 +212,24 @@ void DocumentModel::addEntry(Entry *entry)
int index;
QString displayName = entry->displayName();
for (index = 0; index < d->m_documents.count(); ++index) {
if (displayName < d->m_documents.at(index)->displayName())
for (index = 0; index < m_documents.count(); ++index) {
if (displayName < m_documents.at(index)->displayName())
break;
}
int row = index + 1/*<no document>*/;
beginInsertRows(QModelIndex(), row, row);
d->m_documents.insert(index, entry);
m_documents.insert(index, entry);
if (entry->document)
connect(entry->document, SIGNAL(changed()), this, SLOT(itemChanged()));
endInsertRows();
}
int DocumentModel::indexOfFilePath(const QString &filePath) const
int DocumentModel::indexOfFilePath(const QString &filePath)
{
return d->indexOfFilePath(filePath);
}
int DocumentModelPrivate::indexOfFilePath(const QString &filePath) const
{
if (filePath.isEmpty())
return -1;
......@@ -207,7 +245,7 @@ void DocumentModel::removeEntry(DocumentModel::Entry *entry)
{
QTC_ASSERT(!entry->document, return); // we wouldn't know what to do with the associated editors
int index = d->m_documents.indexOf(entry);
removeDocument(index);
d->removeDocument(index);
}
void DocumentModel::removeEditor(IEditor *editor, bool *lastOneForDocument)
......@@ -222,7 +260,7 @@ void DocumentModel::removeEditor(IEditor *editor, bool *lastOneForDocument)
if (lastOneForDocument)
*lastOneForDocument = true;
d->m_editors.remove(document);
removeDocument(indexOfDocument(document));
d->removeDocument(indexOfDocument(document));
}
}
......@@ -230,10 +268,10 @@ void DocumentModel::removeDocument(const QString &fileName)
{
int index = indexOfFilePath(fileName);
QTC_ASSERT(!d->m_documents.at(index)->document, return); // we wouldn't know what to do with the associated editors
removeDocument(index);
d->removeDocument(index);
}
void DocumentModel::removeDocument(int idx)
void DocumentModelPrivate::removeDocument(int idx)
{
if (idx < 0)
return;
......@@ -252,19 +290,24 @@ void DocumentModel::removeAllRestoredDocuments()
for (int i = d->m_documents.count()-1; i >= 0; --i) {
if (!d->m_documents.at(i)->document) {
int row = i + 1/*<no document>*/;
beginRemoveRows(QModelIndex(), row, row);
d->beginRemoveRows(QModelIndex(), row, row);
delete d->m_documents.takeAt(i);
endRemoveRows();
d->endRemoveRows();
}
}
}
QList<IEditor *> DocumentModel::editorsForDocument(IDocument *document) const
QList<IEditor *> DocumentModel::editorsForDocument(IDocument *document)
{
return d->m_editors.value(document);
}
QList<IEditor *> DocumentModel::editorsForDocuments(const QList<IDocument *> &documents) const
QList<IEditor *> DocumentModel::editorsForOpenedDocuments()
{
return editorsForDocuments(openedDocuments());
}
QList<IEditor *> DocumentModel::editorsForDocuments(const QList<IDocument *> &documents)
{
QList<IEditor *> result;
foreach (IDocument *document, documents)
......@@ -272,15 +315,20 @@ QList<IEditor *> DocumentModel::editorsForDocuments(const QList<IDocument *> &do
return result;
}
int DocumentModel::indexOfDocument(IDocument *document) const
int DocumentModel::indexOfDocument(IDocument *document)
{
for (int i = 0; i < d->m_documents.count(); ++i)
if (d->m_documents.at(i)->document == document)
return d->indexOfDocument(document);
}
int DocumentModelPrivate::indexOfDocument(IDocument *document) const
{
for (int i = 0; i < m_documents.count(); ++i)
if (m_documents.at(i)->document == document)
return i;
return -1;
}
DocumentModel::Entry *DocumentModel::entryForDocument(IDocument *document) const
DocumentModel::Entry *DocumentModel::entryForDocument(IDocument *document)
{
int index = indexOfDocument(document);
if (index < 0)
......@@ -288,12 +336,12 @@ DocumentModel::Entry *DocumentModel::entryForDocument(IDocument *document) const
return d->m_documents.at(index);
}
QList<IDocument *> DocumentModel::openedDocuments() const
QList<IDocument *> DocumentModel::openedDocuments()
{
return d->m_editors.keys();
}
IDocument *DocumentModel::documentForFilePath(const QString &filePath) const
IDocument *DocumentModel::documentForFilePath(const QString &filePath)
{
int index = indexOfFilePath(filePath);
if (index < 0)
......@@ -301,7 +349,7 @@ IDocument *DocumentModel::documentForFilePath(const QString &filePath) const
return d->m_documents.at(index)->document;
}
QList<IEditor *> DocumentModel::editorsForFilePath(const QString &filePath) const
QList<IEditor *> DocumentModel::editorsForFilePath(const QString &filePath)
{
IDocument *document = documentForFilePath(filePath);
if (document)
......@@ -309,15 +357,15 @@ QList<IEditor *> DocumentModel::editorsForFilePath(const QString &filePath) cons
return QList<IEditor *>();
}
QModelIndex DocumentModel::index(int row, int column, const QModelIndex &parent) const
QModelIndex DocumentModelPrivate::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent)
if (column < 0 || column > 1 || row < 0 || row >= d->m_documents.count() + 1/*<no document>*/)
if (column < 0 || column > 1 || row < 0 || row >= m_documents.count() + 1/*<no document>*/)
return QModelIndex();
return createIndex(row, column);
}
DocumentModel::Entry *DocumentModel::documentAtRow(int row) const
DocumentModel::Entry *DocumentModel::documentAtRow(int row)
{
int entryIndex = row - 1/*<no document>*/;
if (entryIndex < 0)
......@@ -325,12 +373,12 @@ DocumentModel::Entry *DocumentModel::documentAtRow(int row) const
return d->m_documents[entryIndex];
}
int DocumentModel::documentCount() const
int DocumentModel::documentCount()
{
return d->m_documents.count();
}
QVariant DocumentModel::data(const QModelIndex &index, int role) const
QVariant DocumentModelPrivate::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || (index.column() != 0 && role < Qt::UserRole))
return QVariant();
......@@ -346,7 +394,7 @@ QVariant DocumentModel::data(const QModelIndex &index, int role) const
return QVariant();
}
}
const Entry *e = d->m_documents.at(entryIndex);
const DocumentModel::Entry *e = m_documents.at(entryIndex);
switch (role) {
case Qt::DisplayRole:
return (e->document && e->document->isModified())
......@@ -362,7 +410,7 @@ QVariant DocumentModel::data(const QModelIndex &index, int role) const
} else {
showLock = !QFileInfo(e->m_fileName).isWritable();
}
return showLock ? d->m_lockedIcon : QIcon();
return showLock ? m_lockedIcon : QIcon();
}
case Qt::ToolTipRole:
return e->fileName().isEmpty()
......@@ -374,14 +422,14 @@ QVariant DocumentModel::data(const QModelIndex &index, int role) const
return QVariant();
}
int DocumentModel::rowOfDocument(IDocument *document) const
int DocumentModel::rowOfDocument(IDocument *document)
{
if (!document)
return 0 /*<no document>*/;
return indexOfDocument(document) + 1/*<no document>*/;
}
void DocumentModel::itemChanged()
void DocumentModelPrivate::itemChanged()
{
IDocument *document = qobject_cast<IDocument *>(sender());
......@@ -392,9 +440,11 @@ void DocumentModel::itemChanged()
emit dataChanged(mindex, mindex);
}
QList<DocumentModel::Entry *> DocumentModel::documents() const
QList<DocumentModel::Entry *> DocumentModel::documents()
{
return d->m_documents;
}
} // namespace Core
#include "documentmodel.moc"
......@@ -39,26 +39,19 @@ QT_FORWARD_DECLARE_CLASS(QIcon)
namespace Core {
struct DocumentModelPrivate;
class IEditor;
class IDocument;
class CORE_EXPORT DocumentModel : public QAbstractItemModel
class CORE_EXPORT DocumentModel
{
Q_OBJECT
public:
explicit DocumentModel(QObject *parent);
virtual ~DocumentModel();
static void init();
static void destroy();
QIcon lockedIcon() const;
QIcon unlockedIcon() const;
static QIcon lockedIcon();
static QIcon unlockedIcon();
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QModelIndex parent(const QModelIndex &/*index*/) const { return QModelIndex(); }
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
static QAbstractItemModel *model();
struct CORE_EXPORT Entry {
Entry();
......@@ -71,39 +64,34 @@ public:
Id m_id;
};
Entry *documentAtRow(int row) const;
int rowOfDocument(IDocument *document) const;
static Entry *documentAtRow(int row);
static int rowOfDocument(IDocument *document);
int documentCount() const;
QList<Entry *> documents() const;
int indexOfDocument(IDocument *document) const;
int indexOfFilePath(const QString &filePath) const;
Entry *entryForDocument(IDocument *document) const;
QList<IDocument *> openedDocuments() const;
static int documentCount();
static QList<Entry *> documents();
static int indexOfDocument(IDocument *document);
static int indexOfFilePath(const QString &filePath);
static Entry *entryForDocument(IDocument *document);
static QList<IDocument *> openedDocuments();
IDocument *documentForFilePath(const QString &filePath) const;
QList<IEditor *> editorsForFilePath(const QString &filePath) const;
QList<IEditor *> editorsForDocument(IDocument *document) const;
QList<IEditor *> editorsForDocuments(const QList<IDocument *> &documents) const;
QList<IEditor *> oneEditorForEachOpenedDocument() const;
static IDocument *documentForFilePath(const QString &filePath);
static QList<IEditor *> editorsForFilePath(const QString &filePath);
static QList<IEditor *> editorsForDocument(IDocument *document);
static QList<IEditor *> editorsForDocuments(const QList<IDocument *> &documents);
static QList<IEditor *> oneEditorForEachOpenedDocument();
static QList<IEditor *> editorsForOpenedDocuments();
// editor manager related functions, nobody else should call it
void addEditor(IEditor *editor, bool *isNewDocument);
void addRestoredDocument(const QString &fileName, const QString &displayName, const Id &id);
Entry *firstRestoredDocument() const;
void removeEditor(IEditor *editor, bool *lastOneForDocument);
void removeDocument(const QString &fileName);
void removeEntry(Entry *entry);
void removeAllRestoredDocuments();
private slots:
void itemChanged();
static void addEditor(IEditor *editor, bool *isNewDocument);
static void addRestoredDocument(const QString &fileName, const QString &displayName, const Id &id);
static Entry *firstRestoredDocument();
static void removeEditor(IEditor *editor, bool *lastOneForDocument);
static void removeDocument(const QString &fileName);
static void removeEntry(Entry *entry);
static void removeAllRestoredDocuments();
private:
void addEntry(Entry *entry);
void removeDocument(int idx);
DocumentModelPrivate *d;
DocumentModel();
};
} // namespace Core
......
......@@ -203,8 +203,6 @@ public:
QMap<QString, QVariant> m_editorStates;
Internal::OpenEditorsViewFactory *m_openEditorsFactory;
DocumentModel *m_documentModel;
IDocument::ReloadSetting m_reloadSetting;
QString m_titleAddition;
......@@ -244,12 +242,13 @@ EditorManagerPrivate::EditorManagerPrivate(QWidget *parent) :
m_autoSaveEnabled(true),
m_autoSaveInterval(5)
{
m_documentModel = new DocumentModel(parent);
DocumentModel::init();
}
EditorManagerPrivate::~EditorManagerPrivate()
{
// clearNavigationHistory();
DocumentModel::destroy();
}
static EditorManager *m_instance = 0;
......@@ -503,7 +502,7 @@ EditorToolBar *EditorManager::createToolBar(QWidget *parent)
void EditorManager::removeEditor(IEditor *editor)
{
bool lastOneForDocument = false;
d->m_documentModel->removeEditor(editor, &lastOneForDocument);
DocumentModel::removeEditor(editor, &lastOneForDocument);
if (lastOneForDocument)
DocumentManager::removeDocument(editor->document());
ICore::removeContextObject(editor);
......@@ -639,7 +638,7 @@ void EditorManager::emptyView(Core::Internal::EditorView *view)
QList<IEditor *> editors = view->editors();
foreach (IEditor *editor, editors) {
if (d->m_documentModel->editorsForDocument(editor->document()).size() == 1) {
if (DocumentModel::editorsForDocument(editor->document()).size() == 1) {
// it's the only editor for that file
// so we need to keep it around (--> in the editor model)
if (currentEditor() == editor) {
......@@ -720,16 +719,16 @@ void EditorManager::closeView(Core::Internal::EditorView *view)
bool EditorManager::closeAllEditors(bool askAboutModifiedEditors)
{
d->m_documentModel->removeAllRestoredDocuments();
if (closeDocuments(d->m_documentModel->openedDocuments(), askAboutModifiedEditors))
DocumentModel::removeAllRestoredDocuments();
if (closeDocuments(DocumentModel::openedDocuments(), askAboutModifiedEditors))
return true;
return false;
}
void EditorManager::closeAllEditorsExceptVisible()
{
d->m_documentModel->removeAllRestoredDocuments();
QList<IDocument *> documentsToClose = d->m_documentModel->openedDocuments();
DocumentModel::removeAllRestoredDocuments();
QList<IDocument *> documentsToClose = DocumentModel::openedDocuments();
foreach (IEditor *editor, visibleEditors())
documentsToClose.removeAll(editor->document());
closeDocuments(documentsToClose, true);
......@@ -737,8 +736,8 @@ void EditorManager::closeAllEditorsExceptVisible()
void EditorManager::closeOtherEditors(IDocument *document)
{
d->m_documentModel->removeAllRestoredDocuments();
QList<IDocument *> documentsToClose = d->m_documentModel->openedDocuments();
DocumentModel::removeAllRestoredDocuments();
QList<IDocument *> documentsToClose = DocumentModel::openedDocuments();
documentsToClose.removeAll(document);
closeDocuments(documentsToClose, true);
}
......@@ -799,8 +798,8 @@ void EditorManager::addSaveAndCloseEditorActions(QMenu *contextMenu, DocumentMod
: tr("Close Other Editors"));
d->m_closeCurrentEditorContextAction->setEnabled(entry != 0);
d->m_closeOtherEditorsContextAction->setEnabled(entry != 0);
d->m_closeAllEditorsContextAction->setEnabled(!d->m_documentModel->documents().isEmpty());
d->m_closeAllEditorsExceptVisibleContextAction->setEnabled(visibleDocumentsCount() < d->m_documentModel->documents().count());
d->m_closeAllEditorsContextAction->setEnabled(!DocumentModel::documents().isEmpty());
d->m_closeAllEditorsExceptVisibleContextAction->setEnabled(visibleDocumentsCount() < DocumentModel::documents().count());
contextMenu->addAction(d->m_closeCurrentEditorContextAction);
contextMenu->addAction(d->m_closeAllEditorsContextAction);
contextMenu->addAction(d->m_closeOtherEditorsContextAction);
......@@ -944,7 +943,7 @@ void EditorManager::closeEditorFromContextMenu()
{
IDocument *document = d->m_contextMenuEntry ? d->m_contextMenuEntry->document : 0;
if (document)
closeEditors(d->m_documentModel->editorsForDocument(document));
closeEditors(DocumentModel::editorsForDocument(document));