Commit 0d507a18 authored by mae's avatar mae
Browse files

getting closer

parent b9655fd1
......@@ -450,8 +450,11 @@ void EditorManager::updateEditorHistory()
void EditorManager::removeEditor(IEditor *editor)
{
bool isDuplicate = m_d->m_editorModel->isDuplicate(editor);
m_d->m_editorModel->removeEditor(editor);
m_d->m_core->fileManager()->removeFile(editor->file());
if (!isDuplicate) {
m_d->m_core->fileManager()->removeFile(editor->file());
}
m_d->m_editorHistory.removeAll(editor);
m_d->m_core->removeContextObject(editor);
......@@ -510,31 +513,35 @@ IEditor *EditorManager::currentEditor() const
// we simply postpone it with a single shot timer
void EditorManager::closeEditor()
{
static bool postpone = true;
if (postpone) {
QTimer::singleShot(0, this, SLOT(closeEditor()));
postpone = false;
} else {
closeEditor(m_d->m_splitter->findView(m_d->m_currentEditor)->view(), m_d->m_currentEditor);
postpone = true;
}
closeEditor(m_d->m_currentEditor);
}
void EditorManager::closeEditor(Core::Internal::EditorView *view, Core::IEditor *editor)
void EditorManager::closeView(Core::Internal::EditorView *view)
{
if (!editor || !view)
if (!view)
return;
Q_ASSERT(view->hasEditor(editor));
if (view == m_d->m_view) {
closeEditors(QList<IEditor *>() << editor);
closeEditors(QList<IEditor *>() << view->currentEditor());
} else {
view->removeEditor(editor);
closeDuplicate(editor, true);
QList<IEditor *> editors = view->editors();
foreach (IEditor *editor, editors) {
emit editorAboutToClose(editor);
removeEditor(editor);
view->removeEditor(editor);
}
emit editorsClosed(editors);
foreach (IEditor *editor, editors) {
delete editor;
}
}
}
void EditorManager::closeEditor(Core::IEditor *editor)
{
if (!editor)
return;
closeEditors(QList<IEditor *>() << editor);
}
QList<IEditor*>
......@@ -582,6 +589,8 @@ bool EditorManager::closeEditors(const QList<IEditor*> editorsToClose, bool askA
pluginManager()->getObjects<ICoreListener>();
foreach (IEditor *editor, editorsToClose) {
bool editorAccepted = true;
if (m_d->m_editorModel->isDuplicate(editor))
editor = m_d->m_editorModel->originalForDuplicate(editor);
foreach (ICoreListener *listener, listeners) {
if (!listener->editorAboutToClose(editor)) {
editorAccepted = false;
......@@ -609,6 +618,11 @@ bool EditorManager::closeEditors(const QList<IEditor*> editorsToClose, bool askA
}
if (acceptedEditors.isEmpty())
return false;
// add duplicates
foreach(IEditor *editor, acceptedEditors)
acceptedEditors += m_d->m_editorModel->duplicatesFor(editor);
bool currentEditorRemoved = false;
IEditor *current = currentEditor();
if (current)
......@@ -627,8 +641,10 @@ bool EditorManager::closeEditors(const QList<IEditor*> editorsToClose, bool askA
if (!state.isEmpty())
m_d->m_editorStates.insert(editor->file()->fileName(), QVariant(state));
}
removeEditor(editor);
m_d->m_view->removeEditor(editor);
if (SplitterOrView *view = m_d->m_splitter->findView(editor))
view->view()->removeEditor(editor);
}
emit editorsClosed(acceptedEditors);
foreach (IEditor *editor, acceptedEditors) {
......@@ -640,18 +656,13 @@ bool EditorManager::closeEditors(const QList<IEditor*> editorsToClose, bool askA
return !closingFailed;
}
void EditorManager::closeDuplicate(Core::IEditor *editor, bool doDelete)
{
m_d->m_editorHistory.removeAll(editor);
emit editorAboutToClose(editor);
emit editorsClosed(QList<Core::IEditor *>() << editor);
if (doDelete)
delete editor;
}
void EditorManager::activateEditor(IEditor *editor, OpenEditorFlags flags)
{
activateEditor(m_d->m_splitter->findView(m_d->m_currentEditor)->view(), editor, flags);
SplitterOrView *splitterOrView = m_d->m_currentEditor ?
m_d->m_splitter->findView(m_d->m_currentEditor)
: m_d->m_splitter->findFirstView();
activateEditor(splitterOrView->view(), editor, flags);
}
......@@ -662,10 +673,24 @@ void EditorManager::activateEditor(Core::Internal::EditorView *view, Core::IEdit
Q_ASSERT(view && editor);
qDebug() << "activateEditor" << editor->file()->fileName() << view;
if (!view->hasEditor(editor)) {
if (SplitterOrView *sourceView = m_d->m_splitter->findView(editor))
sourceView->view()->removeEditor(editor);
qDebug() << "not in requested view";
bool duplicateSupported = editor->duplicateSupported();
qDebug() << "duplicateSupported" << duplicateSupported;
if (SplitterOrView *sourceView = m_d->m_splitter->findView(editor)) {
qDebug() << "found editor in another view";
if (editor != sourceView->editor() || !duplicateSupported) {
qDebug() << "steal editor";
sourceView->view()->removeEditor(editor);
} else if (duplicateSupported) {
qDebug() << "do duplicate";
editor = duplicateEditor(editor);
Q_ASSERT(editor);
}
}
view->addEditor(editor);
view->setCurrentEditor(editor);
}
setCurrentEditor(editor, (flags & IgnoreNavigationHistory));
......@@ -788,8 +813,8 @@ void EditorManager::addEditor(IEditor *editor, bool isDuplicate)
return;
m_d->m_core->addContextObject(editor);
m_d->m_editorModel->addEditor(editor, isDuplicate);
if (!isDuplicate) {
m_d->m_editorModel->addEditor(editor);
m_d->m_core->fileManager()->addFile(editor->file());
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
}
......@@ -1579,20 +1604,16 @@ void EditorManager::unsplitAll()
void EditorManager::gotoOtherWindow()
{
qDebug() << "gotoOtherWindow";
if (!m_d->m_currentEditor)
return;
qDebug() << "current editor" << m_d->m_currentEditor->file()->fileName();
if (m_d->m_splitter->isSplitter()) {
qDebug() << "we have a splitter";
SplitterOrView *view = m_d->m_splitter->findNextView(m_d->m_currentEditor);
qDebug() << "next view is" << view;
if (!view)
view = m_d->m_splitter->findFirstView();
if (view) {
if (IEditor *editor = view->editor()) {
qDebug() << "set new current editor to" << editor->file()->fileName();
view->view()->setCurrentEditor(editor);
setCurrentEditor(editor);
editor->widget()->setFocus();
}
}
}
......
......@@ -222,10 +222,10 @@ private:
void restoreEditorState(IEditor *editor);
Core::IEditor *duplicateEditor(IEditor *editor);
void closeDuplicate(Core::IEditor *editor, bool doDelete);
void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
void activateEditor(Core::Internal::EditorView *view, Core::IEditor *editor, OpenEditorFlags flags = 0);
void closeEditor(Core::Internal::EditorView *view, Core::IEditor *editor);
void closeEditor(Core::IEditor *editor);
void closeView(Core::Internal::EditorView *view);
static EditorManager *m_instance;
EditorManagerPrivate *m_d;
......
......@@ -77,8 +77,13 @@ int EditorModel::rowCount(const QModelIndex &parent) const
return 0;
}
void EditorModel::addEditor(IEditor *editor)
void EditorModel::addEditor(IEditor *editor, bool isDuplicate)
{
if (isDuplicate) {
m_duplicateEditors.append(editor);
return;
}
int index = 0;
QString fileName = editor->file()->fileName();
......@@ -94,6 +99,7 @@ void EditorModel::addEditor(IEditor *editor)
void EditorModel::removeEditor(IEditor *editor)
{
m_duplicateEditors.removeAll(editor);
int idx = m_editors.indexOf(editor);
if (idx < 0)
return;
......@@ -103,6 +109,29 @@ void EditorModel::removeEditor(IEditor *editor)
disconnect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
}
bool EditorModel::isDuplicate(IEditor *editor) const
{
return m_duplicateEditors.contains(editor);
}
IEditor *EditorModel::originalForDuplicate(IEditor *duplicate) const
{
IFile *file = duplicate->file();
foreach(IEditor *e, m_editors)
if (e->file() == file)
return e;
return 0;
}
QList<IEditor *> EditorModel::duplicatesFor(IEditor *editor) const
{
QList<IEditor *> result;
IFile *file = editor->file();
foreach(IEditor *e, m_duplicateEditors)
if (e->file() == file)
result += e;
return result;
}
void EditorModel::emitDataChanged(IEditor *editor)
{
......@@ -236,7 +265,7 @@ EditorView::EditorView(EditorModel *model, QWidget *parent) :
connect(m_editorList, SIGNAL(activated(int)), this, SLOT(listSelectionActivated(int)));
connect(m_lockButton, SIGNAL(clicked()), this, SLOT(makeEditorWritable()));
connect(m_closeButton, SIGNAL(clicked()), this, SLOT(sendCloseRequest()));
connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()));
}
{
m_infoWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
......@@ -326,10 +355,12 @@ bool EditorView::hasEditor(IEditor *editor) const
return (m_container->indexOf(editor->widget()) != -1);
}
void EditorView::sendCloseRequest()
void EditorView::closeView()
{
if (editorCount() == 0)
return;
EditorManager *em = CoreImpl::instance()->editorManager();
em->closeEditor(this, currentEditor());
em->closeView(this);
}
void EditorView::removeEditor(IEditor *editor)
......@@ -367,7 +398,7 @@ void EditorView::setCurrentEditor(IEditor *editor)
|| m_container->indexOf(editor->widget()) == -1)
return;
if (editor)
qDebug() << "EditorView::setCurrentEditor" << editor->file()->fileName();
qDebug() << "EditorView::setCurrentEditor" << editor << editor->file()->fileName();
const int idx = m_container->indexOf(editor->widget());
QTC_ASSERT(idx >= 0, return);
......@@ -513,6 +544,21 @@ SplitterOrView *SplitterOrView::findSplitter(Core::IEditor *editor)
return 0;
}
SplitterOrView *SplitterOrView::findSplitter(SplitterOrView *child)
{
if (m_splitter) {
for (int i = 0; i < m_splitter->count(); ++i) {
if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i))) {
if (splitterOrView == child)
return this;
if (SplitterOrView *result = splitterOrView->findSplitter(child))
return result;
}
}
}
return 0;
}
SplitterOrView *SplitterOrView::findNextView(Core::IEditor *editor)
{
bool found = false;
......@@ -569,10 +615,12 @@ void SplitterOrView::split(Qt::Orientation orientation)
void SplitterOrView::close()
{
qDebug() << "SplitterOrView::close TODO";
return;
foreach(Core::IEditor *e, editors())
CoreImpl::instance()->editorManager()->closeDuplicate(e, false);
Q_ASSERT(!m_isRoot);
if (m_view) {
m_view->closeView();
delete m_view;
m_view = 0;
}
closeSplitterEditors();
}
......@@ -587,22 +635,42 @@ void SplitterOrView::closeSplitterEditors()
}
}
void SplitterOrView::unsplit(Core::IEditor */*editor*/)
void SplitterOrView::unsplit(Core::IEditor *editor)
{
qDebug() << "SplitterOrView::unsplit TODO";
return;
/*
if (!m_splitter)
return;
Q_ASSERT(m_isRoot || (m_view == 0 && editor));
if (!m_isRoot) {
m_view = new EditorView(CoreImpl::instance()->editorManager()->openedEditorsModel());
m_view->addEditor(editor);
m_layout->addWidget(m_view);
m_view->setCurrentEditor(editor);
qDebug() << "unsplit" << this << m_splitter;
#if 0
SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(0));
Q_ASSERT(splitterOrView != 0);
qDebug() << "splitter or view is" << splitterOrView;
if (editor) { // pick the other side
if (SplitterOrView *view = findView(editor)) {
qDebug() << "view to close is" << view;
view->close();
delete view;
}
splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(0));
qDebug() << "other splitter or view is" << splitterOrView;
}
closeSplitterEditors();
delete m_splitter;
m_splitter = 0;
*/
QSplitter *old_splitter = m_splitter;
EditorView *old_view = m_view;
m_splitter = splitterOrView->splitter();
m_view = splitterOrView->view();
qDebug() << "new splitter/view" << m_splitter << m_view;
if (m_splitter)
m_layout->addWidget(m_splitter);
if (m_view)
m_layout->addWidget(m_view);
// delete old_view;
// delete old_splitter;
#endif
}
......@@ -73,12 +73,15 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
void addEditor(IEditor *editor);
void addEditor(IEditor *editor, bool isDuplicate = false);
void removeEditor(IEditor *editor);
void emitDataChanged(IEditor *editor);
QList<IEditor *> editors() const { return m_editors; }
bool isDuplicate(IEditor *editor) const;
QList<IEditor *> duplicatesFor(IEditor *editor) const;
IEditor *originalForDuplicate(IEditor *duplicate) const;
QModelIndex indexOf(IEditor *editor) const;
QModelIndex indexOf(const QString &filename) const;
......@@ -86,6 +89,7 @@ private slots:
void itemChanged();
private:
QList<IEditor *> m_editors;
QList<IEditor *>m_duplicateEditors;
};
......@@ -114,8 +118,10 @@ public:
void hideEditorInfoBar(const QString &kind);
public slots:
void closeView();
private slots:
void sendCloseRequest();
void updateEditorStatus(Core::IEditor *editor = 0);
void checkEditorStatus();
void makeEditorWritable();
......@@ -152,6 +158,8 @@ public:
void unsplit(Core::IEditor *editor);
bool isView() const { return m_view != 0; }
bool isRoot() const { return m_isRoot; }
bool isSplitter() const { return m_splitter != 0; }
Core::IEditor *editor() const { return m_view ? m_view->currentEditor() : 0; }
QList<Core::IEditor *> editors() const { return m_view ? m_view->editors() : QList<Core::IEditor*>(); }
......@@ -162,6 +170,7 @@ public:
SplitterOrView *findView(Core::IEditor *editor);
SplitterOrView *findFirstView();
SplitterOrView *findSplitter(Core::IEditor *editor);
SplitterOrView *findSplitter(SplitterOrView *child);
SplitterOrView *findNextView(Core::IEditor *editor);
......
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