Commit c0c53423 authored by mae's avatar mae

first edition of the new editor view, splitting still missing

parent aaf7c8bc
......@@ -132,7 +132,6 @@ const char * const REMOVE = "QtCreator.Remove";
const char * const SAVEASDEFAULT = "QtCreator.SaveAsDefaultLayout";
const char * const RESTOREDEFAULT = "QtCreator.RestoreDefaultLayout";
const char * const CLOSE = "QtCreator.Close";
const char * const DUPLICATEDOCUMENT = "QtCreator.DuplicateDocument";
const char * const CLOSEALL = "QtCreator.CloseAll";
const char * const GOTONEXT = "QtCreator.GotoNext";
const char * const GOTOPREV = "QtCreator.GotoPrevious";
......
......@@ -36,6 +36,7 @@ SOURCES += mainwindow.cpp \
versiondialog.cpp \
editormanager/editorgroup.cpp \
editormanager/editormanager.cpp \
editormanager/editorview.cpp \
editormanager/stackededitorgroup.cpp \
editormanager/editorsplitter.cpp \
editormanager/openeditorsview.cpp \
......@@ -92,6 +93,7 @@ HEADERS += mainwindow.h \
viewmanager.h \
editormanager/editorgroup.h \
editormanager/editormanager.h \
editormanager/editorview.h \
editormanager/stackededitorgroup.h \
editormanager/editorsplitter.h \
editormanager/openeditorsview.h \
......
......@@ -34,7 +34,7 @@
#include "editorgroup.h"
#include "editormanager.h"
#include "editorview.h"
#include <coreplugin/coreconstants.h>
#include <utils/qtcassert.h>
......@@ -84,105 +84,6 @@ private:
} // namespace Internal
} // namespace Core
//================EditorModel====================
int EditorModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
int EditorModel::rowCount(const QModelIndex &parent) const
{
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
{
Q_UNUSED(parent);
if (column != 0 || row < 0 || row >= m_editors.count())
return QModelIndex();
return createIndex(row, column);
}
QVariant EditorModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
IEditor *editor = m_editors.at(index.row());
QTC_ASSERT(editor, return QVariant());
switch (role) {
case Qt::DisplayRole:
return editor->file()->isModified()
? editor->displayName() + QLatin1String("*")
: editor->displayName();
case Qt::DecorationRole:
return editor->file()->isReadOnly()
? QIcon(QLatin1String(":/core/images/locked.png"))
: QIcon();
case Qt::ToolTipRole:
return editor->file()->fileName().isEmpty()
? editor->displayName()
: QDir::toNativeSeparators(editor->file()->fileName());
case Qt::UserRole:
return qVariantFromValue(editor);
default:
return QVariant();
}
return QVariant();
}
QModelIndex EditorModel::indexOf(IEditor *editor) const
{
int idx = m_editors.indexOf(editor);
if (idx < 0)
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===============
......
......@@ -124,33 +124,6 @@ private:
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;
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 addEditor(IEditor *editor) { insertEditor(rowCount(), editor); }
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;
};
} // namespace Internal
} // namespace Core
......
......@@ -32,7 +32,7 @@
***************************************************************************/
#include "editormanager.h"
#include "editorsplitter.h"
#include "editorview.h"
#include "openeditorswindow.h"
#include "openwithdialog.h"
#include "filemanager.h"
......@@ -128,7 +128,7 @@ struct EditorManagerPrivate {
};
explicit EditorManagerPrivate(ICore *core, QWidget *parent);
~EditorManagerPrivate();
Internal::EditorSplitter *m_splitter;
Internal::EditorView *m_view;
ICore *m_core;
bool m_suppressEditorChanges;
......@@ -141,7 +141,6 @@ struct EditorManagerPrivate {
QAction *m_closeAllEditorsAction;
QAction *m_gotoNextDocHistoryAction;
QAction *m_gotoPreviousDocHistoryAction;
QAction *m_duplicateAction;
QAction *m_goBackAction;
QAction *m_goForwardAction;
QAction *m_openInExternalEditorAction;
......@@ -153,9 +152,6 @@ struct EditorManagerPrivate {
Core::BaseView *m_openEditorsView;
Internal::EditorClosingCoreListener *m_coreListener;
typedef QMap<IEditor *, QList<IEditor *> *> DuplicateMap;
DuplicateMap m_duplicates;
QMap<QString, QVariant> m_editorStates;
Internal::OpenEditorsViewFactory *m_openEditorsFactory;
......@@ -168,7 +164,7 @@ struct EditorManagerPrivate {
}
EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_splitter(0),
m_view(0),
m_core(core),
m_suppressEditorChanges(false),
m_revertToSavedAction(new QAction(EditorManager::tr("Revert to Saved"), parent)),
......@@ -178,7 +174,6 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_closeAllEditorsAction(new QAction(EditorManager::tr("Close All"), parent)),
m_gotoNextDocHistoryAction(new QAction(EditorManager::tr("Next Document in History"), parent)),
m_gotoPreviousDocHistoryAction(new QAction(EditorManager::tr("Previous Document in History"), parent)),
m_duplicateAction(new QAction(EditorManager::tr("Duplicate Document"), parent)),
m_goBackAction(new QAction(EditorManager::tr("Go back"), parent)),
m_goForwardAction(new QAction(EditorManager::tr("Go forward"), parent)),
m_openInExternalEditorAction(new QAction(EditorManager::tr("Open in External Editor"), parent)),
......@@ -272,11 +267,6 @@ EditorManager::EditorManager(ICore *core, QWidget *parent) :
mfile->addAction(cmd, Constants::G_FILE_CLOSE);
connect(m_d->m_closeAllEditorsAction, SIGNAL(triggered()), this, SLOT(closeAllEditors()));
//Duplicate Action
cmd = am->registerAction(m_d->m_duplicateAction, Constants::DUPLICATEDOCUMENT, editManagerContext);
mwindow->addAction(cmd, Constants::G_WINDOW_CLOSE);
connect(m_d->m_duplicateAction, SIGNAL(triggered()), this, SLOT(duplicateEditor()));
// Goto Previous In History Action
cmd = am->registerAction(m_d->m_gotoPreviousDocHistoryAction, Constants::GOTOPREVINHISTORY, editManagerContext);
#ifdef Q_WS_MAC
......@@ -334,16 +324,14 @@ EditorManager::EditorManager(ICore *core, QWidget *parent) :
this, SLOT(updateActions()));
connect(this, SIGNAL(currentEditorChanged(Core::IEditor*)),
this, SLOT(updateEditorHistory()));
m_d->m_splitter = new EditorSplitter(m_d->m_core);
connect(m_d->m_splitter, SIGNAL(closeRequested(Core::IEditor *)),
m_d->m_view = new EditorView(m_d->m_editorModel, this);
connect(m_d->m_view, SIGNAL(closeRequested(Core::IEditor *)),
this, SLOT(closeEditor(Core::IEditor *)));
connect(m_d->m_splitter, SIGNAL(editorGroupsChanged()),
this, SIGNAL(editorGroupsChanged()));
QHBoxLayout *l = new QHBoxLayout(this);
l->setSpacing(0);
l->setMargin(0);
l->addWidget(m_d->m_splitter);
l->addWidget(m_d->m_view);
updateActions();
......@@ -394,11 +382,6 @@ QString EditorManager::defaultExternalEditor() const
#endif
}
EditorSplitter *EditorManager::editorSplitter() const
{
return m_d->m_splitter;
}
void EditorManager::updateEditorHistory()
{
IEditor *editor = currentEditor();
......@@ -412,10 +395,9 @@ 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());
}
m_d->m_core->fileManager()->addFile(editor->file());
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
m_d->m_editorHistory.removeAll(editor);
m_d->m_editorHistory.prepend(editor);
return true;
......@@ -427,8 +409,7 @@ 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_core->fileManager()->removeFile(editor->file());
m_d->m_editorHistory.removeAll(editor);
return true;
}
......@@ -439,13 +420,8 @@ void EditorManager::updateCurrentEditorAndGroup(IContext *context)
{
if (debugEditorManager)
qDebug() << Q_FUNC_INFO;
EditorGroupContext *groupContext = context ? qobject_cast<EditorGroupContext*>(context) : 0;
IEditor *editor = context ? qobject_cast<IEditor*>(context) : 0;
if (groupContext) {
m_d->m_splitter->setCurrentGroup(groupContext->editorGroup());
setCurrentEditor(0);
updateActions();
} else if (editor) {
if (editor) {
setCurrentEditor(editor);
} else {
updateActions();
......@@ -468,12 +444,8 @@ void EditorManager::setCurrentEditor(IEditor *editor, bool ignoreNavigationHisto
qDebug() << Q_FUNC_INFO << (addToHistory ? "adding to history" : "not adding to history");
if (addToHistory)
addCurrentPositionToNavigationHistory(true);
EditorGroup *group = groupOfEditor(editor);
if (!group)
return;
m_d->m_suppressEditorChanges = true;
m_d->m_splitter->setCurrentGroup(group);
group->setCurrentEditor(editor);
m_d->m_view->setCurrentEditor(editor);
m_d->m_suppressEditorChanges = false;
if (addToHistory)
addCurrentPositionToNavigationHistory();
......@@ -486,15 +458,6 @@ void EditorManager::editorChanged(IEditor *toEditor)
emit currentEditorChanged(toEditor);
}
EditorGroup *EditorManager::groupOfEditor(IEditor *editor) const
{
foreach (EditorGroup *group, m_d->m_splitter->groups()) {
if (group->editors().contains(editor))
return group;
}
return 0;
}
QList<IEditor *> EditorManager::editorsForFileName(const QString &filename) const
{
QList<IEditor *> found;
......@@ -508,23 +471,9 @@ QList<IEditor *> EditorManager::editorsForFileName(const QString &filename) cons
IEditor *EditorManager::currentEditor() const
{
return m_d->m_splitter->currentGroup()->currentEditor();
}
EditorGroup *EditorManager::currentEditorGroup() const
{
return m_d->m_splitter->currentGroup();
return m_d->m_view->currentEditor();
}
void EditorManager::duplicateEditor()
{
IEditor *curEditor = currentEditor();
if (!curEditor || !curEditor->duplicateSupported())
return;
IEditor *editor = curEditor->duplicate(this);
registerDuplicate(curEditor, editor);
insertEditor(editor);
}
// SLOT connected to action
// since this is potentially called in the event handler of the editor
......@@ -559,13 +508,7 @@ QList<IEditor*>
foreach (IFile *file, files) {
foreach (IEditor *editor, editors) {
if (editor->file() == file && !found.contains(editor)) {
if (hasDuplicate(editor)) {
foreach (IEditor *duplicate, duplicates(editor)) {
found << duplicate;
}
} else {
found << editor;
}
}
}
}
......@@ -580,13 +523,7 @@ QList<IFile *>
foreach (IEditor *editor, editors) {
if (!handledEditors.contains(editor)) {
files << editor->file();
if (hasDuplicate(editor)) {
foreach (IEditor *duplicate, duplicates(editor)) {
handledEditors << duplicate;
}
} else {
handledEditors.insert(editor);
}
handledEditors.insert(editor);
}
}
return files;
......@@ -654,14 +591,10 @@ bool EditorManager::closeEditors(const QList<IEditor*> editorsToClose, bool askA
m_d->m_editorStates.insert(editor->file()->fileName(), QVariant(state));
}
unregisterEditor(editor);
if (hasDuplicate(editor))
unregisterDuplicate(editor);
m_d->m_core->removeContextObject(editor);
EditorGroup *group = groupOfEditor(editor);
const bool suppress = m_d->m_suppressEditorChanges;
m_d->m_suppressEditorChanges = true;
if (group)
group->removeEditor(editor);
m_d->m_view->removeEditor(editor);
m_d->m_suppressEditorChanges = suppress;
}
emit editorsClosed(acceptedEditors);
......@@ -772,18 +705,13 @@ IEditor *EditorManager::createEditor(const QString &editorKind,
}
void EditorManager::insertEditor(IEditor *editor,
bool ignoreNavigationHistory,
EditorGroup *group)
bool ignoreNavigationHistory)
{
if (!editor)
return;
m_d->m_core->addContextObject(editor);
registerEditor(editor);
if (group)
group->addEditor(editor);
else
m_d->m_splitter->currentGroup()->addEditor(editor);
m_d->m_view->addEditor(editor);
setCurrentEditor(editor, ignoreNavigationHistory);
emit editorOpened(editor);
}
......@@ -1160,7 +1088,7 @@ void EditorManager::updateActions()
m_d->m_revertToSavedAction->setText(tr("Revert %1 to Saved").arg(fName));
m_d->m_closeCurrentEditorAction->setEnabled(m_d->m_splitter->currentGroup()->editorCount() > 0);
m_d->m_closeCurrentEditorAction->setEnabled(curEditor != 0);
m_d->m_closeCurrentEditorAction->setText(tr("Close %1").arg(fName));
m_d->m_closeAllEditorsAction->setEnabled(openedCount > 0);
......@@ -1169,33 +1097,25 @@ void EditorManager::updateActions()
m_d->m_goBackAction->setEnabled(m_d->currentNavigationHistoryPosition > 0);
m_d->m_goForwardAction->setEnabled(m_d->currentNavigationHistoryPosition < m_d->m_navigationHistory.size()-1);
m_d->m_duplicateAction->setEnabled(curEditor != 0 && curEditor->duplicateSupported());
m_d->m_openInExternalEditorAction->setEnabled(curEditor != 0);
}
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) {
editors += group->editors();
}
return editors;
return m_d->m_view->editors();
}
Internal::EditorModel *EditorManager::openedEditorsModel() const
QList<IEditor*> EditorManager::openedEditorsNoDuplicates() const
{
return m_d->m_editorModel;
return m_d->m_view->editors();
}
QList<EditorGroup *> EditorManager::editorGroups() const
Internal::EditorModel *EditorManager::openedEditorsModel() const
{
return m_d->m_splitter->groups();
return m_d->m_editorModel;
}
QList<IEditor*> EditorManager::editorHistory() const
{
return m_d->m_editorHistory;
......@@ -1299,79 +1219,49 @@ void EditorManager::showWindowPopup() const
m_d->m_windowPopup->setVisible(true);
}
void EditorManager::registerDuplicate(IEditor *original,
IEditor *duplicate)
{
QList<IEditor *> *duplicateList;
if (m_d->m_duplicates.contains(original)) {
duplicateList = m_d->m_duplicates.value(original);
} else {
duplicateList = new QList<IEditor *>;
duplicateList->append(original);
m_d->m_duplicates.insert(original, duplicateList);
}
duplicateList->append(duplicate);
m_d->m_duplicates.insert(duplicate, duplicateList);
}
void EditorManager::unregisterDuplicate(IEditor *editor)
{
if (!m_d->m_duplicates.contains(editor))
return;
QList<IEditor *> *duplicateList = m_d->m_duplicates.value(editor);
duplicateList->removeAll(editor);
m_d->m_duplicates.remove(editor);
if (duplicateList->count() < 2) {
foreach (IEditor *other, *duplicateList) {
m_d->m_duplicates.remove(other);
}
delete duplicateList;
}
}
bool EditorManager::hasDuplicate(IEditor *editor) const
{
return m_d->m_duplicates.contains(editor);
}
QList<IEditor *>
EditorManager::duplicates(IEditor *editor) const
{
if (m_d->m_duplicates.contains(editor))
return *m_d->m_duplicates.value(editor);
return QList<IEditor *>() << editor;
}
QByteArray EditorManager::saveState() const
{
//todo: versioning
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
stream << m_d->m_splitter->saveState();
stream << saveOpenEditorList();
qDebug() << "saveState";
stream << QByteArray("EditorManagerV1");
stream << m_d->m_editorStates;
QList<IEditor *> editors = openedEditorsNoDuplicates();
int editorCount = editors.count();
qDebug() << "save editors:" << editorCount;
stream << editorCount;
foreach (IEditor *editor, editors) {
stream << editor->file()->fileName() << QByteArray(editor->kind());
}
return bytes;
}
bool EditorManager::restoreState(const QByteArray &state)
{
closeAllEditors(true);
//todo: versioning
QDataStream stream(state);
QByteArray data;
QMap<QString, QVariant> editorstates;
stream >> data;
const bool success = m_d->m_splitter->restoreState(data);
if (!success)
QByteArray version;
stream >> version;
qDebug() << "restore state" << version;
if (version != "EditorManagerV1")
return false;
QMap<QString, QVariant> editorstates;
QApplication::setOverrideCursor(Qt::WaitCursor);
bool editorChangesSuppressed = m_d->m_suppressEditorChanges;
m_d->m_suppressEditorChanges = true;
stream >> data;
restoreOpenEditorList(data);
stream >> editorstates;
QMapIterator<QString, QVariant> i(editorstates);
while (i.hasNext()) {
......@@ -1379,6 +1269,18 @@ bool EditorManager::restoreState(const QByteArray &state)
m_d->m_editorStates.insert(i.key(), i.value());
}
int editorCount = 0;
stream >> editorCount;
qDebug() << "restore editors:" << editorCount;
while (--editorCount >= 0) {
QString fileName;
stream >> fileName;
QByteArray kind;
stream >> kind;
qDebug() << "openEditor" << fileName << kind;