added a standard item model to the editormanager to maintain the list of open editors.

This commit is contained in:
mae
2009-01-12 20:49:16 +01:00
parent 7d1629f6f0
commit 4237c8cfd4
7 changed files with 103 additions and 163 deletions

View File

@@ -93,8 +93,38 @@ int EditorModel::columnCount(const QModelIndex &parent) const
int EditorModel::rowCount(const QModelIndex &parent) const int EditorModel::rowCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); if (!parent.isValid())
return m_editors.count(); return m_editors.count();
return 0;
}
void EditorModel::insertEditor(int index, IEditor *editor)
{
beginInsertRows(QModelIndex(), index, index);
m_editors.insert(index, editor);
connect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
endInsertRows();
}
void EditorModel::removeEditor(IEditor *editor)
{
int idx = m_editors.indexOf(editor);
if (idx < 0)
return;
beginRemoveRows(QModelIndex(), idx, idx);
m_editors.removeAt(idx);
endRemoveRows();
disconnect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
}
void EditorModel::emitDataChanged(IEditor *editor)
{
int idx = m_editors.indexOf(editor);
if (idx < 0)
return;
QModelIndex mindex = index(idx, 0);
emit dataChanged(mindex, mindex);
} }
QModelIndex EditorModel::index(int row, int column, const QModelIndex &parent) const QModelIndex EditorModel::index(int row, int column, const QModelIndex &parent) const
@@ -136,10 +166,24 @@ QModelIndex EditorModel::indexOf(IEditor *editor) const
{ {
int idx = m_editors.indexOf(editor); int idx = m_editors.indexOf(editor);
if (idx < 0) if (idx < 0)
return QModelIndex(); return indexOf(editor->file()->fileName());
return createIndex(idx, 0); return createIndex(idx, 0);
} }
QModelIndex EditorModel::indexOf(const QString &fileName) const
{
for (int i = 0; i < m_editors.count(); ++i)
if (m_editors.at(i)->file()->fileName() == fileName)
return createIndex(i, 0);
return QModelIndex();
}
void EditorModel::itemChanged()
{
emitDataChanged(qobject_cast<IEditor*>(sender()));
}
//================EditorGroupContext=============== //================EditorGroupContext===============
EditorGroupContext::EditorGroupContext(EditorGroup *editorGroup) EditorGroupContext::EditorGroupContext(EditorGroup *editorGroup)

View File

@@ -127,6 +127,7 @@ namespace Internal {
// Also used by StackedEditorGroup // Also used by StackedEditorGroup
class EditorModel : public QAbstractItemModel class EditorModel : public QAbstractItemModel
{ {
Q_OBJECT
public: public:
EditorModel(QObject *parent) : QAbstractItemModel(parent) {} EditorModel(QObject *parent) : QAbstractItemModel(parent) {}
int columnCount(const QModelIndex &parent = QModelIndex()) const; int columnCount(const QModelIndex &parent = QModelIndex()) const;
@@ -137,30 +138,16 @@ public:
void addEditor(IEditor *editor) { insertEditor(rowCount(), editor); } void addEditor(IEditor *editor) { insertEditor(rowCount(), editor); }
void insertEditor(int index, IEditor *editor) void insertEditor(int index, IEditor *editor);
{ void removeEditor(IEditor *editor);
beginInsertRows(QModelIndex(), index, index); void emitDataChanged(IEditor *editor);
m_editors.insert(index, editor);
endInsertRows();
}
void removeEditor(IEditor *editor)
{
int index = m_editors.indexOf(editor);
beginRemoveRows(QModelIndex(), index, index);
m_editors.removeAt(index);
endRemoveRows();
}
void emitDataChanged(IEditor *editor)
{
int idx = m_editors.indexOf(editor);
QModelIndex mindex = index(idx, 0);
emit dataChanged(mindex, mindex);
}
QList<IEditor *> editors() const { return m_editors; } QList<IEditor *> editors() const { return m_editors; }
QModelIndex indexOf(IEditor *editor) const; QModelIndex indexOf(IEditor *editor) const;
QModelIndex indexOf(const QString &filename) const;
private slots:
void itemChanged();
private: private:
QList<IEditor *> m_editors; QList<IEditor *> m_editors;
}; };

View File

@@ -118,7 +118,8 @@ EditorManagerPlaceHolder* EditorManagerPlaceHolder::current()
// ---------------- EditorManager // ---------------- EditorManager
struct Core::EditorManagerPrivate { namespace Core {
struct EditorManagerPrivate {
struct EditLocation { struct EditLocation {
QPointer<IEditor> editor; QPointer<IEditor> editor;
QString fileName; QString fileName;
@@ -161,8 +162,10 @@ struct Core::EditorManagerPrivate {
QString fileFilters; QString fileFilters;
QString selectedFilter; QString selectedFilter;
EditorModel *m_editorModel;
QString m_externalEditor; QString m_externalEditor;
}; };
}
EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) : EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_splitter(0), m_splitter(0),
@@ -183,7 +186,7 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_windowPopup(0), m_windowPopup(0),
m_coreListener(0) m_coreListener(0)
{ {
m_editorModel = new EditorModel(parent);
} }
EditorManagerPrivate::~EditorManagerPrivate() EditorManagerPrivate::~EditorManagerPrivate()
@@ -408,6 +411,7 @@ void EditorManager::updateEditorHistory()
bool EditorManager::registerEditor(IEditor *editor) bool EditorManager::registerEditor(IEditor *editor)
{ {
if (editor) { if (editor) {
m_d->m_editorModel->addEditor(editor);
if (!hasDuplicate(editor)) { if (!hasDuplicate(editor)) {
m_d->m_core->fileManager()->addFile(editor->file()); m_d->m_core->fileManager()->addFile(editor->file());
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName()); m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
@@ -422,6 +426,7 @@ bool EditorManager::registerEditor(IEditor *editor)
bool EditorManager::unregisterEditor(IEditor *editor) bool EditorManager::unregisterEditor(IEditor *editor)
{ {
if (editor) { if (editor) {
m_d->m_editorModel->removeEditor(editor);
if (!hasDuplicate(editor)) if (!hasDuplicate(editor))
m_d->m_core->fileManager()->removeFile(editor->file()); m_d->m_core->fileManager()->removeFile(editor->file());
m_d->m_editorHistory.removeAll(editor); m_d->m_editorHistory.removeAll(editor);
@@ -1171,6 +1176,7 @@ void EditorManager::updateActions()
QList<IEditor*> EditorManager::openedEditors() const QList<IEditor*> EditorManager::openedEditors() const
{ {
return m_d->m_editorModel->editors();
QList<IEditor*> editors; QList<IEditor*> editors;
const QList<EditorGroup*> groups = m_d->m_splitter->groups(); const QList<EditorGroup*> groups = m_d->m_splitter->groups();
foreach (EditorGroup *group, groups) { foreach (EditorGroup *group, groups) {
@@ -1179,6 +1185,12 @@ QList<IEditor*> EditorManager::openedEditors() const
return editors; return editors;
} }
Internal::EditorModel *EditorManager::openedEditorsModel() const
{
return m_d->m_editorModel;
}
QList<EditorGroup *> EditorManager::editorGroups() const QList<EditorGroup *> EditorManager::editorGroups() const
{ {
return m_d->m_splitter->groups(); return m_d->m_splitter->groups();

View File

@@ -68,10 +68,12 @@ struct EditorManagerPrivate;
namespace Internal { namespace Internal {
class OpenEditorsWindow; class OpenEditorsWindow;
class EditorModel;
class EditorSplitter; class EditorSplitter;
class EditorClosingCoreListener; class EditorClosingCoreListener;
class OpenEditorsViewFactory; class OpenEditorsViewFactory;
} // namespace Internal } // namespace Internal
class CORE_EXPORT EditorManagerPlaceHolder : public QWidget class CORE_EXPORT EditorManagerPlaceHolder : public QWidget
@@ -119,6 +121,9 @@ public:
EditorGroup *currentEditorGroup() const; EditorGroup *currentEditorGroup() const;
QList<IEditor*> openedEditors() const; QList<IEditor*> openedEditors() const;
Internal::EditorModel *openedEditorsModel() const;
QList<IEditor*> editorsForFiles(QList<IFile*> files) const; QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
QList<EditorGroup *> editorGroups() const; QList<EditorGroup *> editorGroups() const;
QList<IEditor*> editorHistory() const; QList<IEditor*> editorHistory() const;

View File

@@ -63,7 +63,6 @@ OpenEditorsWidget::OpenEditorsWidget()
setWindowTitle(tr("Open Documents")); setWindowTitle(tr("Open Documents"));
setWindowIcon(QIcon(Constants::ICON_DIR)); setWindowIcon(QIcon(Constants::ICON_DIR));
setFocusProxy(m_ui.editorList); setFocusProxy(m_ui.editorList);
m_ui.editorList->setColumnCount(1);
m_ui.editorList->header()->hide(); m_ui.editorList->header()->hide();
m_ui.editorList->setIndentation(0); m_ui.editorList->setIndentation(0);
m_ui.editorList->setSelectionMode(QAbstractItemView::ExtendedSelection); m_ui.editorList->setSelectionMode(QAbstractItemView::ExtendedSelection);
@@ -71,20 +70,11 @@ OpenEditorsWidget::OpenEditorsWidget()
m_ui.editorList->installEventFilter(this); m_ui.editorList->installEventFilter(this);
m_ui.editorList->setFrameStyle(QFrame::NoFrame); m_ui.editorList->setFrameStyle(QFrame::NoFrame);
EditorManager *em = EditorManager::instance(); EditorManager *em = EditorManager::instance();
foreach (IEditor *editor, em->openedEditors()) { m_ui.editorList->setModel(em->openedEditorsModel());
registerEditor(editor);
}
connect(em, SIGNAL(editorOpened(Core::IEditor*)),
this, SLOT(registerEditor(Core::IEditor*)));
connect(em, SIGNAL(editorsClosed(QList<Core::IEditor*>)),
this, SLOT(unregisterEditors(QList<Core::IEditor*>)));
connect(em, SIGNAL(editorGroupsChanged()),
this, SLOT(updateEditorList()));
connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)), connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
this, SLOT(updateCurrentItem())); this, SLOT(updateCurrentItem(Core::IEditor*)));
connect(m_ui.editorList, SIGNAL(itemActivated(QTreeWidgetItem*, int)), connect(m_ui.editorList, SIGNAL(activated(QModelIndex)),
this, SLOT(selectEditor(QTreeWidgetItem*))); this, SLOT(selectEditor(QModelIndex)));
updateEditorList();
} }
OpenEditorsWidget::~OpenEditorsWidget() OpenEditorsWidget::~OpenEditorsWidget()
@@ -92,126 +82,32 @@ OpenEditorsWidget::~OpenEditorsWidget()
} }
void OpenEditorsWidget::registerEditor(IEditor *editor) void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
{ {
connect(editor, SIGNAL(changed()), this, SLOT(updateEditor())); if (!editor) {
updateEditorList(); m_ui.editorList->clearSelection();
}
void OpenEditorsWidget::unregisterEditors(QList<IEditor *> editors)
{
foreach (IEditor *editor, editors)
disconnect(editor, SIGNAL(changed()), this, SLOT(updateEditor()));
updateEditorList();
}
void OpenEditorsWidget::updateEditorList()
{
EditorManager *em = EditorManager::instance();
QList<EditorGroup *> groups = em->editorGroups();
IEditor *curEditor = em->currentEditor();
int oldNum = m_ui.editorList->topLevelItemCount();
QTreeWidgetItem *currentItem = 0;
int currItemIndex = 0;
for (int i = 0; i < groups.count(); ++i) {
QTreeWidgetItem *item;
if (groups.count() > 1) {
if (currItemIndex < oldNum) {
item = m_ui.editorList->topLevelItem(currItemIndex);
} else {
item = new QTreeWidgetItem(QStringList()<<"");
m_ui.editorList->addTopLevelItem(item);
}
currItemIndex++;
item->setIcon(0, QIcon());
item->setText(0, tr("---Group %1---").arg(i));
item->setFlags(0);
item->setToolTip(0, "");
item->setData(0, Qt::UserRole, QVariant());
item->setTextAlignment(0, Qt::AlignLeft);
}
foreach (IEditor *editor, groups.at(i)->editors()) {
if (currItemIndex < oldNum) {
item = m_ui.editorList->topLevelItem(currItemIndex);
} else {
item = new QTreeWidgetItem(QStringList()<<"");
m_ui.editorList->addTopLevelItem(item);
}
currItemIndex++;
updateItem(item, editor);
if (editor == curEditor)
currentItem = item;
}
}
for (int i = oldNum-1; i >= currItemIndex; --i) {
delete m_ui.editorList->takeTopLevelItem(i);
}
updateCurrentItem(currentItem);
}
void OpenEditorsWidget::updateCurrentItem(QTreeWidgetItem *currentItem)
{
EditorManager *em = EditorManager::instance();
IEditor *curEditor = em->currentEditor();
m_ui.editorList->clearSelection();
if (!currentItem && curEditor) {
int count = m_ui.editorList->topLevelItemCount();
for (int i = 0; i < count; ++i) {
if (m_ui.editorList->topLevelItem(i)->data(0, Qt::UserRole).value<IEditor *>()
== curEditor) {
currentItem = m_ui.editorList->topLevelItem(i);
break;
}
}
}
m_ui.editorList->setCurrentItem(currentItem);
if (currentItem)
m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
}
//todo: this is almost duplicated in openeditorswindow
void OpenEditorsWidget::updateItem(QTreeWidgetItem *item, IEditor *editor)
{
static const QIcon lockedIcon(QLatin1String(":/core/images/locked.png"));
static const QIcon emptyIcon(QLatin1String(":/core/images/empty14.png"));
QString title = editor->displayName();
if (editor->file()->isModified())
title += tr("*");
item->setIcon(0, editor->file()->isReadOnly() ? lockedIcon : emptyIcon);
item->setText(0, title);
item->setToolTip(0, editor->file()->fileName());
item->setData(0, Qt::UserRole, QVariant::fromValue(editor));
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
item->setTextAlignment(0, Qt::AlignLeft);
}
void OpenEditorsWidget::selectEditor(QTreeWidgetItem *item)
{
if (item == 0)
item = m_ui.editorList->currentItem();
if (item == 0)
return; return;
IEditor *editor = item->data(0, Qt::UserRole).value<IEditor*>(); }
EditorManager *em = EditorManager::instance();
m_ui.editorList->setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
}
void OpenEditorsWidget::selectEditor(const QModelIndex &index)
{
IEditor *editor = index.data(Qt::UserRole).value<IEditor*>();
EditorManager::instance()->setCurrentEditor(editor); EditorManager::instance()->setCurrentEditor(editor);
} }
void OpenEditorsWidget::updateEditor()
void OpenEditorsWidget::selectEditor()
{ {
IEditor *editor = qobject_cast<IEditor *>(sender()); selectEditor(m_ui.editorList->currentIndex());
QTC_ASSERT(editor, return);
int num = m_ui.editorList->topLevelItemCount();
for (int i = 0; i < num; ++i) {
QTreeWidgetItem *item = m_ui.editorList->topLevelItem(i);
if (item->data(0, Qt::UserRole).value<IEditor *>()
== editor) {
updateItem(item, editor);
return;
}
}
} }
void OpenEditorsWidget::closeEditors() void OpenEditorsWidget::closeEditors()
{ {
/* ### TODO
QList<IFile *> selectedFiles; QList<IFile *> selectedFiles;
QList<IEditor *> selectedEditors; QList<IEditor *> selectedEditors;
foreach (QTreeWidgetItem *item, m_ui.editorList->selectedItems()) { foreach (QTreeWidgetItem *item, m_ui.editorList->selectedItems()) {
@@ -226,6 +122,7 @@ void OpenEditorsWidget::closeEditors()
core->editorManager()-> core->editorManager()->
closeEditors(selectedEditors); closeEditors(selectedEditors);
updateEditorList(); updateEditorList();
*/
} }
void OpenEditorsWidget::closeAllEditors() void OpenEditorsWidget::closeAllEditors()
@@ -242,7 +139,7 @@ bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key()) { switch (keyEvent->key()) {
case Qt::Key_Return: case Qt::Key_Return:
selectEditor(m_ui.editorList->currentItem()); selectEditor(m_ui.editorList->currentIndex());
return true; return true;
case Qt::Key_Delete: //fall through case Qt::Key_Delete: //fall through
case Qt::Key_Backspace: case Qt::Key_Backspace:
@@ -259,9 +156,9 @@ bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent *>(event); QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent *>(event);
QMenu menu; QMenu menu;
menu.addAction(tr("&Select"), this, SLOT(selectEditor())); menu.addAction(tr("&Select"), this, SLOT(selectEditor()));
menu.addAction(tr("&Close"), this, SLOT(closeEditors())); //todo menu.addAction(tr("&Close"), this, SLOT(closeEditors()));
menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors())); //todo menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors()));
if (m_ui.editorList->selectedItems().isEmpty()) if (m_ui.editorList->selectionModel()->selectedIndexes().isEmpty())
menu.setEnabled(false); menu.setEnabled(false);
menu.exec(contextMenuEvent->globalPos()); menu.exec(contextMenuEvent->globalPos());
return true; return true;

View File

@@ -59,19 +59,14 @@ public:
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);
private slots: private slots:
void registerEditor(Core::IEditor *editor); void selectEditor(const QModelIndex &);
void unregisterEditors(QList<Core::IEditor *> editors); void selectEditor();
void updateEditorList();
void selectEditor(QTreeWidgetItem *item = 0);
void updateEditor();
void closeEditors(); void closeEditors();
void closeAllEditors(); void closeAllEditors();
void updateCurrentItem(QTreeWidgetItem *currentItem = 0); void updateCurrentItem(Core::IEditor*);
void putFocusToEditorList(); void putFocusToEditorList();
private: private:
static void updateItem(QTreeWidgetItem *item, Core::IEditor *editor);
Ui::OpenEditorsView m_ui; Ui::OpenEditorsView m_ui;
QWidget *m_widget; QWidget *m_widget;
}; };

View File

@@ -26,7 +26,7 @@
<number>0</number> <number>0</number>
</property> </property>
<item row="0" column="0" > <item row="0" column="0" >
<widget class="QTreeWidget" name="editorList" > <widget class="QTreeView" name="editorList" >
<property name="uniformRowHeights" > <property name="uniformRowHeights" >
<bool>true</bool> <bool>true</bool>
</property> </property>