Commit 4237c8cf authored by mae's avatar mae
Browse files

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

parent 7d1629f6
......@@ -93,8 +93,38 @@ int EditorModel::columnCount(const QModelIndex &parent) const
int EditorModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_editors.count();
if (!parent.isValid())
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
......@@ -136,10 +166,24 @@ QModelIndex EditorModel::indexOf(IEditor *editor) const
{
int idx = m_editors.indexOf(editor);
if (idx < 0)
return QModelIndex();
return indexOf(editor->file()->fileName());
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(EditorGroup *editorGroup)
......
......@@ -127,6 +127,7 @@ namespace Internal {
// Also used by StackedEditorGroup
class EditorModel : public QAbstractItemModel
{
Q_OBJECT
public:
EditorModel(QObject *parent) : QAbstractItemModel(parent) {}
int columnCount(const QModelIndex &parent = QModelIndex()) const;
......@@ -137,30 +138,16 @@ public:
void addEditor(IEditor *editor) { insertEditor(rowCount(), editor); }
void insertEditor(int index, IEditor *editor)
{
beginInsertRows(QModelIndex(), index, index);
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);
}
void insertEditor(int index, IEditor *editor);
void removeEditor(IEditor *editor);
void emitDataChanged(IEditor *editor);
QList<IEditor *> editors() const { return m_editors; }
QModelIndex indexOf(IEditor *editor) const;
QModelIndex indexOf(const QString &filename) const;
private slots:
void itemChanged();
private:
QList<IEditor *> m_editors;
};
......
......@@ -118,7 +118,8 @@ EditorManagerPlaceHolder* EditorManagerPlaceHolder::current()
// ---------------- EditorManager
struct Core::EditorManagerPrivate {
namespace Core {
struct EditorManagerPrivate {
struct EditLocation {
QPointer<IEditor> editor;
QString fileName;
......@@ -161,8 +162,10 @@ struct Core::EditorManagerPrivate {
QString fileFilters;
QString selectedFilter;
EditorModel *m_editorModel;
QString m_externalEditor;
};
}
EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_splitter(0),
......@@ -183,7 +186,7 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_windowPopup(0),
m_coreListener(0)
{
m_editorModel = new EditorModel(parent);
}
EditorManagerPrivate::~EditorManagerPrivate()
......@@ -408,6 +411,7 @@ void EditorManager::updateEditorHistory()
bool EditorManager::registerEditor(IEditor *editor)
{
if (editor) {
m_d->m_editorModel->addEditor(editor);
if (!hasDuplicate(editor)) {
m_d->m_core->fileManager()->addFile(editor->file());
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
......@@ -422,6 +426,7 @@ bool EditorManager::registerEditor(IEditor *editor)
bool EditorManager::unregisterEditor(IEditor *editor)
{
if (editor) {
m_d->m_editorModel->removeEditor(editor);
if (!hasDuplicate(editor))
m_d->m_core->fileManager()->removeFile(editor->file());
m_d->m_editorHistory.removeAll(editor);
......@@ -1171,6 +1176,7 @@ void EditorManager::updateActions()
QList<IEditor*> EditorManager::openedEditors() const
{
return m_d->m_editorModel->editors();
QList<IEditor*> editors;
const QList<EditorGroup*> groups = m_d->m_splitter->groups();
foreach (EditorGroup *group, groups) {
......@@ -1179,6 +1185,12 @@ QList<IEditor*> EditorManager::openedEditors() const
return editors;
}
Internal::EditorModel *EditorManager::openedEditorsModel() const
{
return m_d->m_editorModel;
}
QList<EditorGroup *> EditorManager::editorGroups() const
{
return m_d->m_splitter->groups();
......
......@@ -68,10 +68,12 @@ struct EditorManagerPrivate;
namespace Internal {
class OpenEditorsWindow;
class EditorModel;
class EditorSplitter;
class EditorClosingCoreListener;
class OpenEditorsViewFactory;
} // namespace Internal
class CORE_EXPORT EditorManagerPlaceHolder : public QWidget
......@@ -119,6 +121,9 @@ public:
EditorGroup *currentEditorGroup() const;
QList<IEditor*> openedEditors() const;
Internal::EditorModel *openedEditorsModel() const;
QList<IEditor*> editorsForFiles(QList<IFile*> files) const;
QList<EditorGroup *> editorGroups() const;
QList<IEditor*> editorHistory() const;
......
......@@ -63,7 +63,6 @@ OpenEditorsWidget::OpenEditorsWidget()
setWindowTitle(tr("Open Documents"));
setWindowIcon(QIcon(Constants::ICON_DIR));
setFocusProxy(m_ui.editorList);
m_ui.editorList->setColumnCount(1);
m_ui.editorList->header()->hide();
m_ui.editorList->setIndentation(0);
m_ui.editorList->setSelectionMode(QAbstractItemView::ExtendedSelection);
......@@ -71,20 +70,11 @@ OpenEditorsWidget::OpenEditorsWidget()
m_ui.editorList->installEventFilter(this);
m_ui.editorList->setFrameStyle(QFrame::NoFrame);
EditorManager *em = EditorManager::instance();
foreach (IEditor *editor, em->openedEditors()) {
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()));
m_ui.editorList->setModel(em->openedEditorsModel());
connect(em, SIGNAL(currentEditorChanged(Core::IEditor*)),
this, SLOT(updateCurrentItem()));
connect(m_ui.editorList, SIGNAL(itemActivated(QTreeWidgetItem*, int)),
this, SLOT(selectEditor(QTreeWidgetItem*)));
updateEditorList();
this, SLOT(updateCurrentItem(Core::IEditor*)));
connect(m_ui.editorList, SIGNAL(activated(QModelIndex)),
this, SLOT(selectEditor(QModelIndex)));
}
OpenEditorsWidget::~OpenEditorsWidget()
......@@ -92,126 +82,32 @@ OpenEditorsWidget::~OpenEditorsWidget()
}
void OpenEditorsWidget::registerEditor(IEditor *editor)
{
connect(editor, SIGNAL(changed()), this, SLOT(updateEditor()));
updateEditorList();
}
void OpenEditorsWidget::unregisterEditors(QList<IEditor *> editors)
void OpenEditorsWidget::updateCurrentItem(Core::IEditor *editor)
{
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);
if (!editor) {
m_ui.editorList->clearSelection();
return;
}
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());
m_ui.editorList->setCurrentIndex(em->openedEditorsModel()->indexOf(editor));
m_ui.editorList->scrollTo(m_ui.editorList->currentIndex());
}
//todo: this is almost duplicated in openeditorswindow
void OpenEditorsWidget::updateItem(QTreeWidgetItem *item, IEditor *editor)
void OpenEditorsWidget::selectEditor(const QModelIndex &index)
{
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;
IEditor *editor = item->data(0, Qt::UserRole).value<IEditor*>();
IEditor *editor = index.data(Qt::UserRole).value<IEditor*>();
EditorManager::instance()->setCurrentEditor(editor);
}
void OpenEditorsWidget::updateEditor()
void OpenEditorsWidget::selectEditor()
{
IEditor *editor = qobject_cast<IEditor *>(sender());
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;
}
}
selectEditor(m_ui.editorList->currentIndex());
}
void OpenEditorsWidget::closeEditors()
{
/* ### TODO
QList<IFile *> selectedFiles;
QList<IEditor *> selectedEditors;
foreach (QTreeWidgetItem *item, m_ui.editorList->selectedItems()) {
......@@ -226,6 +122,7 @@ void OpenEditorsWidget::closeEditors()
core->editorManager()->
closeEditors(selectedEditors);
updateEditorList();
*/
}
void OpenEditorsWidget::closeAllEditors()
......@@ -242,7 +139,7 @@ bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key()) {
case Qt::Key_Return:
selectEditor(m_ui.editorList->currentItem());
selectEditor(m_ui.editorList->currentIndex());
return true;
case Qt::Key_Delete: //fall through
case Qt::Key_Backspace:
......@@ -259,9 +156,9 @@ bool OpenEditorsWidget::eventFilter(QObject *obj, QEvent *event)
QContextMenuEvent *contextMenuEvent = static_cast<QContextMenuEvent *>(event);
QMenu menu;
menu.addAction(tr("&Select"), this, SLOT(selectEditor()));
menu.addAction(tr("&Close"), this, SLOT(closeEditors()));
menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors()));
if (m_ui.editorList->selectedItems().isEmpty())
//todo menu.addAction(tr("&Close"), this, SLOT(closeEditors()));
//todo menu.addAction(tr("Close &All"), this, SLOT(closeAllEditors()));
if (m_ui.editorList->selectionModel()->selectedIndexes().isEmpty())
menu.setEnabled(false);
menu.exec(contextMenuEvent->globalPos());
return true;
......
......@@ -59,19 +59,14 @@ public:
bool eventFilter(QObject *obj, QEvent *event);
private slots:
void registerEditor(Core::IEditor *editor);
void unregisterEditors(QList<Core::IEditor *> editors);
void updateEditorList();
void selectEditor(QTreeWidgetItem *item = 0);
void updateEditor();
void selectEditor(const QModelIndex &);
void selectEditor();
void closeEditors();
void closeAllEditors();
void updateCurrentItem(QTreeWidgetItem *currentItem = 0);
void updateCurrentItem(Core::IEditor*);
void putFocusToEditorList();
private:
static void updateItem(QTreeWidgetItem *item, Core::IEditor *editor);
Ui::OpenEditorsView m_ui;
QWidget *m_widget;
};
......
......@@ -26,7 +26,7 @@
<number>0</number>
</property>
<item row="0" column="0" >
<widget class="QTreeWidget" name="editorList" >
<widget class="QTreeView" name="editorList" >
<property name="uniformRowHeights" >
<bool>true</bool>
</property>
......
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