editorview.cpp 25.3 KB
Newer Older
1
/**************************************************************************
2 3 4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8 9
**
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
30
**
31
**************************************************************************/
32 33 34

#include "editorview.h"
#include "editormanager.h"
hjk's avatar
hjk committed
35
#include "icore.h"
36
#include "minisplitter.h"
37
#include "openeditorsmodel.h"
Lasse Holmstedt's avatar
Lasse Holmstedt committed
38

39
#include <coreplugin/editortoolbar.h>
40
#include <coreplugin/coreconstants.h>
41
#include <coreplugin/infobar.h>
42
#include <coreplugin/actionmanager/actionmanager.h>
43
#include <coreplugin/editormanager/ieditor.h>
44

45
#include <coreplugin/findplaceholder.h>
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <utils/qtcassert.h>

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QMimeData>

#include <QtGui/QApplication>
#include <QtGui/QComboBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QMouseEvent>
#include <QtGui/QPainter>
#include <QtGui/QStackedWidget>
#include <QtGui/QStyle>
#include <QtGui/QStyleOption>
#include <QtGui/QToolButton>
63 64
#include <QtGui/QMenu>
#include <QtGui/QClipboard>
65 66 67
#include <QtGui/QAction>
#include <QtGui/QSplitter>
#include <QtGui/QStackedLayout>
68

69 70 71 72 73 74 75 76
#ifdef Q_WS_MAC
#include <qmacstyle_mac.h>
#endif

using namespace Core;
using namespace Core::Internal;


Friedemann Kleint's avatar
Friedemann Kleint committed
77
// ================EditorView====================
78

Lasse Holmstedt's avatar
Lasse Holmstedt committed
79
EditorView::EditorView(QWidget *parent) :
80
    QWidget(parent),
Lasse Holmstedt's avatar
Lasse Holmstedt committed
81
    m_toolBar(EditorManager::createToolBar(this)),
82
    m_container(new QStackedWidget(this)),
83
    m_infoBarDisplay(new InfoBarDisplay(this)),
hjk's avatar
hjk committed
84
    m_statusHLine(new QFrame(this)),
85 86
    m_statusWidget(new QFrame(this)),
    m_currentNavigationHistoryPosition(0)
87
{
88
    QVBoxLayout *tl = new QVBoxLayout(this);
89 90 91
    tl->setSpacing(0);
    tl->setMargin(0);
    {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
92 93
        connect(m_toolBar, SIGNAL(goBackClicked()), this, SLOT(goBackInNavigationHistory()));
        connect(m_toolBar, SIGNAL(goForwardClicked()), this, SLOT(goForwardInNavigationHistory()));
94 95
        connect(m_toolBar, SIGNAL(closeClicked()), this, SLOT(closeView()));
        connect(m_toolBar, SIGNAL(listSelectionActivated(int)), this, SLOT(listSelectionActivated(int)));
96 97 98
        connect(m_toolBar, SIGNAL(horizontalSplitClicked()), this, SLOT(splitHorizontally()));
        connect(m_toolBar, SIGNAL(verticalSplitClicked()), this, SLOT(splitVertically()));
        connect(m_toolBar, SIGNAL(closeSplitClicked()), this, SLOT(closeSplit()));
Lasse Holmstedt's avatar
Lasse Holmstedt committed
99
        tl->addWidget(m_toolBar);
100
    }
101 102

    m_infoBarDisplay->setTarget(tl, 1);
103

104 105
    tl->addWidget(m_container);

con's avatar
con committed
106 107
    tl->addWidget(new FindToolBarPlaceHolder(this));

108
    {
hjk's avatar
hjk committed
109 110
        m_statusHLine->setFrameStyle(QFrame::HLine);

111 112
        m_statusWidget->setFrameStyle(QFrame::NoFrame);
        m_statusWidget->setLineWidth(0);
113 114 115
        m_statusWidget->setAutoFillBackground(true);

        QHBoxLayout *hbox = new QHBoxLayout(m_statusWidget);
116 117 118
        hbox->setContentsMargins(1, 0, 1, 1);
        m_statusWidgetLabel = new QLabel;
        m_statusWidgetLabel->setContentsMargins(3, 0, 3, 0);
119 120 121 122
        hbox->addWidget(m_statusWidgetLabel);
        hbox->addStretch(1);

        m_statusWidgetButton = new QToolButton;
123
        m_statusWidgetButton->setContentsMargins(0, 0, 0, 0);
124 125
        hbox->addWidget(m_statusWidgetButton);

126
        m_statusHLine->setVisible(false);
127
        m_statusWidget->setVisible(false);
hjk's avatar
hjk committed
128
        tl->addWidget(m_statusHLine);
129 130
        tl->addWidget(m_statusWidget);
    }
131

Lasse Holmstedt's avatar
Lasse Holmstedt committed
132
    updateNavigatorActions();
133 134 135 136
}

EditorView::~EditorView()
{
137 138
}

139 140 141

void EditorView::closeView()
{
hjk's avatar
hjk committed
142
    EditorManager *em = ICore::instance()->editorManager();
143 144 145
    IEditor *editor = currentEditor();
    if (editor)
       em->closeEditor(editor);
146
}
147

148
void EditorView::showEditorStatusBar(const QString &id,
149 150 151
                                     const QString &infoText,
                                     const QString &buttonText,
                                     QObject *object, const char *member)
152
{
153
    m_statusWidgetId = id;
154 155
    m_statusWidgetLabel->setText(infoText);
    m_statusWidgetButton->setText(buttonText);
156
    m_statusWidgetButton->setToolTip(buttonText);
157 158
    m_statusWidgetButton->disconnect();
    if (object && member)
159
        connect(m_statusWidgetButton, SIGNAL(clicked()), object, member);
160
    m_statusWidget->setVisible(true);
hjk's avatar
hjk committed
161
    m_statusHLine->setVisible(true);
162 163
    //m_editorForInfoWidget = currentEditor();
}
164

165
void EditorView::hideEditorStatusBar(const QString &id)
166
{
167
    if (id == m_statusWidgetId) {
168
        m_statusWidget->setVisible(false);
hjk's avatar
hjk committed
169 170
        m_statusHLine->setVisible(false);
    }
171 172
}

173 174 175 176 177
void EditorView::setCloseSplitEnabled(bool enable)
{
    m_toolBar->setCloseSplitEnabled(enable);
}

178 179
void EditorView::addEditor(IEditor *editor)
{
mae's avatar
mae committed
180
    if (m_editors.contains(editor))
181 182
        return;

mae's avatar
mae committed
183 184
    m_editors.append(editor);

mae's avatar
mae committed
185
    m_container->addWidget(editor->widget());
186
    m_widgetEditorMap.insert(editor->widget(), editor);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
187
    m_toolBar->addEditor(editor);
mae's avatar
mae committed
188

mae's avatar
mae committed
189 190
    if (editor == currentEditor())
        setCurrentEditor(editor);
191 192
}

193 194
bool EditorView::hasEditor(IEditor *editor) const
{
mae's avatar
mae committed
195
    return m_editors.contains(editor);
196 197
}

198 199 200
void EditorView::removeEditor(IEditor *editor)
{
    QTC_ASSERT(editor, return);
mae's avatar
mae committed
201 202 203
    if (!m_editors.contains(editor))
        return;

204
    const int index = m_container->indexOf(editor->widget());
mae's avatar
mae committed
205
    QTC_ASSERT((index != -1), return);
mae's avatar
mae committed
206
    bool wasCurrent = (index == m_container->currentIndex());
mae's avatar
mae committed
207 208 209 210 211
    m_editors.removeAll(editor);

    m_container->removeWidget(editor->widget());
    m_widgetEditorMap.remove(editor->widget());
    editor->widget()->setParent(0);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
212 213
    m_toolBar->removeToolbarForEditor(editor);

mae's avatar
mae committed
214 215
    if (wasCurrent)
        setCurrentEditor(m_editors.count() ? m_editors.last() : 0);
216 217 218 219 220 221 222 223 224
}

IEditor *EditorView::currentEditor() const
{
    if (m_container->count() > 0)
        return m_widgetEditorMap.value(m_container->currentWidget());
    return 0;
}

225 226 227
void EditorView::listSelectionActivated(int index)
{
    QAbstractItemModel *model = EditorManager::instance()->openedEditorsModel();
228
    EditorManager::instance()->activateEditorForIndex(this, model->index(index, 0), Core::EditorManager::ModeSwitch);
229 230
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
void EditorView::splitHorizontally()
{
    EditorManager *editorManager = EditorManager::instance();
    SplitterOrView *splitterOrView = editorManager->topSplitterOrView()->findView(this);
    if (splitterOrView)
        splitterOrView->split(Qt::Vertical);
    editorManager->updateActions();
}

void EditorView::splitVertically()
{
    EditorManager *editorManager = EditorManager::instance();
    SplitterOrView *splitterOrView = editorManager->topSplitterOrView()->findView(this);
    if (splitterOrView)
        splitterOrView->split(Qt::Horizontal);
    editorManager->updateActions();
}

void EditorView::closeSplit()
{
    EditorManager *editorManager = EditorManager::instance();
    editorManager->closeView(this);
    editorManager->updateActions();
}

mae's avatar
mae committed
256
void EditorView::setCurrentEditor(IEditor *editor)
257 258
{
    if (!editor || m_container->count() <= 0
mae's avatar
mae committed
259
        || m_container->indexOf(editor->widget()) == -1) {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
260
        m_toolBar->updateEditorStatus(0);
261
        m_infoBarDisplay->setInfoBar(0);
262
        // ### TODO the combo box m_editorList should show an empty item
263
        return;
mae's avatar
mae committed
264
    }
265

mae's avatar
mae committed
266 267
    m_editors.removeAll(editor);
    m_editors.append(editor);
268

269 270
    const int idx = m_container->indexOf(editor->widget());
    QTC_ASSERT(idx >= 0, return);
271
    m_container->setCurrentIndex(idx);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
272
    m_toolBar->setCurrentEditor(editor);
273

Lasse Holmstedt's avatar
Lasse Holmstedt committed
274
    updateEditorHistory(editor);
275 276

    m_infoBarDisplay->setInfoBar(editor->file()->infoBar());
277 278 279 280 281 282 283 284 285 286 287 288
}

int EditorView::editorCount() const
{
    return m_container->count();
}

QList<IEditor *> EditorView::editors() const
{
    return m_widgetEditorMap.values();
}

mae's avatar
mae committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302
void EditorView::updateEditorHistory(IEditor *editor)
{
    if (!editor)
        return;
    IFile *file = editor->file();

    if (!file)
        return;

    QByteArray state = editor->saveState();

    EditLocation location;
    location.file = file;
    location.fileName = file->fileName();
303
    location.id = editor->id();
mae's avatar
mae committed
304 305 306 307 308 309 310 311 312 313 314
    location.state = QVariant(state);

    for(int i = 0; i < m_editorHistory.size(); ++i) {
        if (m_editorHistory.at(i).file == 0
            || m_editorHistory.at(i).file == file
            ){
            m_editorHistory.removeAt(i--);
            continue;
        }
    }
    m_editorHistory.prepend(location);
315 316 317 318 319 320
}

QRect EditorView::editorArea() const
{
    const QRect cRect = m_container->rect();
    return QRect(m_container->mapToGlobal(cRect.topLeft()), cRect.size());
mae's avatar
mae committed
321 322
}

323 324 325
void EditorView::addCurrentPositionToNavigationHistory(IEditor *editor, const QByteArray &saveState)
{
    if (editor && editor != currentEditor()) {
mae's avatar
mae committed
326
        return; // we only save editor sate for the current editor, when the user interacts
327
    }
mae's avatar
mae committed
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    if (!editor)
        editor = currentEditor();
    if (!editor)
        return;
    IFile *file = editor->file();

    if (!file)
        return;

    QByteArray state;
    if (saveState.isNull()) {
        state = editor->saveState();
    } else {
        state = saveState;
    }

mae's avatar
mae committed
345 346 347
    EditLocation location;
    location.file = file;
    location.fileName = file->fileName();
348
    location.id = editor->id();
mae's avatar
mae committed
349
    location.state = QVariant(state);
350 351 352 353 354 355
    m_currentNavigationHistoryPosition = qMin(m_currentNavigationHistoryPosition, m_navigationHistory.size()); // paranoia
    m_navigationHistory.insert(m_currentNavigationHistoryPosition, location);
    ++m_currentNavigationHistoryPosition;

    while (m_navigationHistory.size() >= 30) {
        if (m_currentNavigationHistoryPosition > 15) {
356
            m_navigationHistory.removeFirst();
357 358
            --m_currentNavigationHistoryPosition;
        } else {
359
            m_navigationHistory.removeLast();
360 361
        }
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
362
    updateNavigatorActions();
363 364
}

365 366 367 368 369 370
void EditorView::cutForwardNavigationHistory()
{
    while (m_currentNavigationHistoryPosition < m_navigationHistory.size() - 1)
        m_navigationHistory.removeLast();
}

Lasse Holmstedt's avatar
Lasse Holmstedt committed
371
void EditorView::updateNavigatorActions()
372
{
Lasse Holmstedt's avatar
Lasse Holmstedt committed
373 374
    m_toolBar->setCanGoBack(canGoBack());
    m_toolBar->setCanGoForward(canGoForward());
375 376 377 378
}

void EditorView::copyNavigationHistoryFrom(EditorView* other)
{
mae's avatar
mae committed
379 380
    if (!other)
        return;
381 382
    m_currentNavigationHistoryPosition = other->m_currentNavigationHistoryPosition;
    m_navigationHistory = other->m_navigationHistory;
mae's avatar
mae committed
383
    m_editorHistory = other->m_editorHistory;
Lasse Holmstedt's avatar
Lasse Holmstedt committed
384
    updateNavigatorActions();
385 386 387 388 389 390 391 392 393 394 395
}

void EditorView::updateCurrentPositionInNavigationHistory()
{
    IEditor *editor = currentEditor();
    if (!editor || !editor->file())
        return;

    IFile *file = editor->file();
    EditLocation *location;
    if (m_currentNavigationHistoryPosition < m_navigationHistory.size()) {
mae's avatar
mae committed
396
        location = &m_navigationHistory[m_currentNavigationHistoryPosition];
397
    } else {
mae's avatar
mae committed
398 399
        m_navigationHistory.append(EditLocation());
        location = &m_navigationHistory[m_navigationHistory.size()-1];
400 401 402
    }
    location->file = file;
    location->fileName = file->fileName();
403
    location->id = editor->id();
404 405 406 407 408
    location->state = QVariant(editor->saveState());
}

void EditorView::goBackInNavigationHistory()
{
hjk's avatar
hjk committed
409
    EditorManager *em = ICore::instance()->editorManager();
410 411 412
    updateCurrentPositionInNavigationHistory();
    while (m_currentNavigationHistoryPosition > 0) {
        --m_currentNavigationHistoryPosition;
mae's avatar
mae committed
413
        EditLocation location = m_navigationHistory.at(m_currentNavigationHistoryPosition);
414
        IEditor *editor;
mae's avatar
mae committed
415
        if (location.file) {
416
            editor = em->activateEditorForFile(this, location.file,
417
                                        EditorManager::IgnoreNavigationHistory | EditorManager::ModeSwitch);
418
        } else {
419 420
            editor = em->openEditor(this, location.fileName, location.id,
                                    EditorManager::IgnoreNavigationHistory | EditorManager::ModeSwitch);
421
            if (!editor) {
mae's avatar
mae committed
422
                m_navigationHistory.removeAt(m_currentNavigationHistoryPosition);
423 424 425
                continue;
            }
        }
mae's avatar
mae committed
426
        editor->restoreState(location.state.toByteArray());
427 428
        break;
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
429
    updateNavigatorActions();
430 431 432 433
}

void EditorView::goForwardInNavigationHistory()
{
hjk's avatar
hjk committed
434
    EditorManager *em = ICore::instance()->editorManager();
435 436 437 438
    updateCurrentPositionInNavigationHistory();
    if (m_currentNavigationHistoryPosition >= m_navigationHistory.size()-1)
        return;
    ++m_currentNavigationHistoryPosition;
mae's avatar
mae committed
439
    EditLocation location = m_navigationHistory.at(m_currentNavigationHistoryPosition);
440
    IEditor *editor;
mae's avatar
mae committed
441
    if (location.file) {
442
        editor = em->activateEditorForFile(this, location.file,
443
                                    EditorManager::IgnoreNavigationHistory | EditorManager::ModeSwitch);
444
    } else {
445
        editor = em->openEditor(this, location.fileName, location.id, EditorManager::IgnoreNavigationHistory);
446 447
        if (!editor) {
            //TODO
mae's avatar
mae committed
448
            qDebug() << Q_FUNC_INFO << "can't open file" << location.fileName;
449 450 451
            return;
        }
    }
mae's avatar
mae committed
452
    editor->restoreState(location.state.toByteArray());
Lasse Holmstedt's avatar
Lasse Holmstedt committed
453
    updateNavigatorActions();
454 455 456
}


457
SplitterOrView::SplitterOrView(OpenEditorsModel *model)
mae's avatar
mae committed
458
{
459 460
    Q_ASSERT(model);
    m_isRoot = true;
mae's avatar
mae committed
461
    m_layout = new QStackedLayout(this);
Lasse Holmstedt's avatar
Lasse Holmstedt committed
462
    m_view = new EditorView();
mae's avatar
mae committed
463 464 465 466
    m_splitter = 0;
    m_layout->addWidget(m_view);
}

mae's avatar
mae committed
467
SplitterOrView::SplitterOrView(Core::IEditor *editor)
mae's avatar
mae committed
468 469 470
{
    m_isRoot = false;
    m_layout = new QStackedLayout(this);
471 472 473
    m_view = new EditorView();
    if (editor)
        m_view->addEditor(editor);
mae's avatar
mae committed
474
    m_splitter = 0;
mae's avatar
mae committed
475
    m_layout->addWidget(m_view);
mae's avatar
mae committed
476 477
}

478 479 480 481 482 483 484 485 486 487
SplitterOrView::~SplitterOrView()
{
    delete m_layout;
    m_layout = 0;
    delete m_view;
    m_view = 0;
    delete m_splitter;
    m_splitter = 0;
}

488
void SplitterOrView::mousePressEvent(QMouseEvent *e)
mae's avatar
mae committed
489
{
490 491 492
    if (e->button() != Qt::LeftButton)
        return;
    setFocus(Qt::MouseFocusReason);
hjk's avatar
hjk committed
493
    ICore::instance()->editorManager()->setCurrentView(this);
mae's avatar
mae committed
494 495 496 497
}

void SplitterOrView::paintEvent(QPaintEvent *)
{
hjk's avatar
hjk committed
498
    if (ICore::instance()->editorManager()->currentSplitterOrView() != this)
499 500 501
        return;

    if (!m_view || hasEditors())
mae's avatar
mae committed
502 503
        return;

504 505
    // Discreet indication where an editor would be if there is none
    QPainter painter(this);
mae's avatar
mae committed
506 507
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
508
    painter.setBrush(palette().color(QPalette::Background).darker(107));
mae's avatar
mae committed
509
    const int r = 3;
510 511 512
    const QRect areaGlobal(view()->editorArea());
    const QRect areaLocal(mapFromGlobal(areaGlobal.topLeft()), areaGlobal.size());
    painter.drawRoundedRect(areaLocal.adjusted(r , r, -r, -r), r * 2, r * 2);
mae's avatar
mae committed
513
}
514 515 516 517 518 519 520 521 522 523 524 525 526 527

SplitterOrView *SplitterOrView::findFirstView()
{
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i)))
                if (SplitterOrView *result = splitterOrView->findFirstView())
                    return result;
        }
        return 0;
    }
    return this;
}

mae's avatar
mae committed
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
SplitterOrView *SplitterOrView::findEmptyView()
{
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i)))
                if (SplitterOrView *result = splitterOrView->findEmptyView())
                    return result;
        }
        return 0;
    }
    if (!hasEditors())
        return this;
    return 0;
}

mae's avatar
mae committed
543 544
SplitterOrView *SplitterOrView::findView(Core::IEditor *editor)
{
545
    if (!editor || hasEditor(editor))
mae's avatar
mae committed
546 547 548 549 550 551 552 553 554 555 556
        return this;
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i)))
                if (SplitterOrView *result = splitterOrView->findView(editor))
                    return result;
        }
    }
    return 0;
}

mae's avatar
mae committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570
SplitterOrView *SplitterOrView::findView(EditorView *view)
{
    if (view == m_view)
        return this;
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i)))
                if (SplitterOrView *result = splitterOrView->findView(view))
                    return result;
        }
    }
    return 0;
}

mae's avatar
mae committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
SplitterOrView *SplitterOrView::findSplitter(Core::IEditor *editor)
{
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i))) {
                if (splitterOrView->hasEditor(editor))
                    return this;
                if (SplitterOrView *result = splitterOrView->findSplitter(editor))
                    return result;
            }
        }
    }
    return 0;
}

mae's avatar
mae committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
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;
}

mae's avatar
mae committed
601
SplitterOrView *SplitterOrView::findNextView(SplitterOrView *view)
602 603
{
    bool found = false;
mae's avatar
mae committed
604
    return findNextView_helper(view, &found);
605 606
}

mae's avatar
mae committed
607
SplitterOrView *SplitterOrView::findNextView_helper(SplitterOrView *view, bool *found)
608 609 610 611 612
{
    if (*found && m_view) {
        return this;
    }

mae's avatar
mae committed
613
    if (this == view) {
614 615 616 617 618 619 620
        *found = true;
        return 0;
    }

    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i))) {
mae's avatar
mae committed
621
                if (SplitterOrView *result = splitterOrView->findNextView_helper(view, found))
622 623 624 625 626 627 628
                    return result;
            }
        }
    }
    return 0;
}

629 630 631 632 633 634 635
QSize SplitterOrView::minimumSizeHint() const
{
    if (m_splitter)
        return m_splitter->minimumSizeHint();
    return QSize(64, 64);
}

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
QSplitter *SplitterOrView::takeSplitter()
{
    QSplitter *oldSplitter = m_splitter;
    if (m_splitter)
        m_layout->removeWidget(m_splitter);
    m_splitter = 0;
    return oldSplitter;
}

EditorView *SplitterOrView::takeView()
{
    EditorView *oldView = m_view;
    if (m_view)
        m_layout->removeWidget(m_view);
    m_view = 0;
    return oldView;
}

mae's avatar
mae committed
654 655 656
void SplitterOrView::split(Qt::Orientation orientation)
{
    Q_ASSERT(m_view && m_splitter == 0);
657
    m_splitter = new MiniSplitter(this);
mae's avatar
mae committed
658
    m_splitter->setOrientation(orientation);
mae's avatar
mae committed
659
    m_layout->addWidget(m_splitter);
hjk's avatar
hjk committed
660
    EditorManager *em = ICore::instance()->editorManager();
mae's avatar
mae committed
661 662
    Core::IEditor *e = m_view->currentEditor();

663
    SplitterOrView *view = 0;
664
    SplitterOrView *otherView = 0;
mae's avatar
mae committed
665 666
    if (e) {

667 668 669
        foreach(IEditor *editor, m_view->editors())
            m_view->removeEditor(editor);

670
        m_splitter->addWidget((view = new SplitterOrView(e)));
mae's avatar
mae committed
671 672
        if (e->duplicateSupported()) {
            Core::IEditor *duplicate = em->duplicateEditor(e);
673
            m_splitter->addWidget((otherView = new SplitterOrView(duplicate)));
mae's avatar
mae committed
674
        } else {
675
            m_splitter->addWidget((otherView = new SplitterOrView()));
mae's avatar
mae committed
676 677
        }
    } else {
678
        m_splitter->addWidget((otherView = new SplitterOrView()));
679
        m_splitter->addWidget((view = new SplitterOrView()));
mae's avatar
mae committed
680
    }
mae's avatar
mae committed
681

mae's avatar
mae committed
682
    m_layout->setCurrentWidget(m_splitter);
mae's avatar
mae committed
683

684
    view->view()->copyNavigationHistoryFrom(m_view);
685
    view->view()->setCurrentEditor(view->view()->currentEditor());
686
    otherView->view()->copyNavigationHistoryFrom(m_view);
687
    otherView->view()->setCurrentEditor(otherView->view()->currentEditor());
688

mae's avatar
mae committed
689 690
    if (m_view && !m_isRoot) {
        em->emptyView(m_view);
mae's avatar
mae committed
691 692 693 694
        delete m_view;
        m_view = 0;
    }

mae's avatar
mae committed
695
    if (e)
696 697 698
        em->activateEditor(view->view(), e);
    else
        em->setCurrentView(view);
mae's avatar
mae committed
699 700
}

701
void SplitterOrView::unsplitAll()
mae's avatar
mae committed
702
{
703 704 705 706 707
    m_splitter->hide();
    m_layout->removeWidget(m_splitter); // workaround Qt bug
    unsplitAll_helper();
    delete m_splitter;
    m_splitter = 0;
mae's avatar
mae committed
708 709
}

710
void SplitterOrView::unsplitAll_helper()
mae's avatar
mae committed
711
{
712
    if (!m_isRoot && m_view)
hjk's avatar
hjk committed
713
        ICore::instance()->editorManager()->emptyView(m_view);
714 715 716 717 718
    if (m_splitter) {
        for (int i = 0; i < m_splitter->count(); ++i) {
            if (SplitterOrView *splitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(i))) {
                splitterOrView->unsplitAll_helper();
            }
mae's avatar
mae committed
719 720 721 722
        }
    }
}

mae's avatar
mae committed
723
void SplitterOrView::unsplit()
mae's avatar
mae committed
724 725 726
{
    if (!m_splitter)
        return;
mae's avatar
mae committed
727

728
    Q_ASSERT(m_splitter->count() == 1);
hjk's avatar
hjk committed
729
    EditorManager *em = ICore::instance()->editorManager();
730 731 732
    SplitterOrView *childSplitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(0));
    QSplitter *oldSplitter = m_splitter;
    m_splitter = 0;
mae's avatar
mae committed
733

734 735
    if (childSplitterOrView->isSplitter()) {
        Q_ASSERT(childSplitterOrView->view() == 0);
736
        m_splitter = childSplitterOrView->takeSplitter();
737 738 739 740 741 742
        m_layout->addWidget(m_splitter);
        m_layout->setCurrentWidget(m_splitter);
    } else {
        EditorView *childView = childSplitterOrView->view();
        Q_ASSERT(childView);
        if (m_view) {
743
            m_view->copyNavigationHistoryFrom(childView);
744 745 746 747 748 749 750
            if (IEditor *e = childView->currentEditor()) {
                childView->removeEditor(e);
                m_view->addEditor(e);
                m_view->setCurrentEditor(e);
            }
            em->emptyView(childView);
        } else {
751
            m_view = childSplitterOrView->takeView();
752
            m_layout->addWidget(m_view);
mae's avatar
mae committed
753
        }
754
        m_layout->setCurrentWidget(m_view);
mae's avatar
mae committed
755
    }
756 757
    delete oldSplitter;
    em->setCurrentView(findFirstView());
mae's avatar
mae committed
758
}
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773


QByteArray SplitterOrView::saveState() const
{
    QByteArray bytes;
    QDataStream stream(&bytes, QIODevice::WriteOnly);

    if (m_splitter) {
        stream << QByteArray("splitter")
                << (qint32)m_splitter->orientation()
                << m_splitter->saveState()
                << static_cast<SplitterOrView*>(m_splitter->widget(0))->saveState()
                << static_cast<SplitterOrView*>(m_splitter->widget(1))->saveState();
    } else {
        IEditor* e = editor();
hjk's avatar
hjk committed
774
        EditorManager *em = ICore::instance()->editorManager();
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
        // don't save state of temporary or ad-hoc editors
        if (e && (e->isTemporary() || e->file()->fileName().isEmpty())) {
            // look for another editor that is more suited
            e = 0;
            foreach (IEditor *otherEditor, editors()) {
                if (!otherEditor->isTemporary() && !otherEditor->file()->fileName().isEmpty()) {
                    e = otherEditor;
                    break;
                }
            }
        }

        if (!e) {
            stream << QByteArray("empty");
        } else if (e == em->currentEditor()) {
791
            stream << QByteArray("currenteditor")
hjk's avatar
hjk committed
792
                    << e->file()->fileName() << e->id().toString() << e->saveState();
793
        } else {
794
            stream << QByteArray("editor")
hjk's avatar
hjk committed
795
                    << e->file()->fileName() << e->id().toString() << e->saveState();
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
        }
    }
    return bytes;
}

void SplitterOrView::restoreState(const QByteArray &state)
{
    QDataStream stream(state);
    QByteArray mode;
    stream >> mode;
    if (mode == "splitter") {
        qint32 orientation;
        QByteArray splitter, first, second;
        stream >> orientation >> splitter >> first >> second;
        split((Qt::Orientation)orientation);
        m_splitter->restoreState(splitter);
        static_cast<SplitterOrView*>(m_splitter->widget(0))->restoreState(first);
        static_cast<SplitterOrView*>(m_splitter->widget(1))->restoreState(second);
    } else if (mode == "editor" || mode == "currenteditor") {
hjk's avatar
hjk committed
815
        EditorManager *em = ICore::instance()->editorManager();
816
        QString fileName;
817
        QString id;
818
        QByteArray editorState;
819
        stream >> fileName >> id >> editorState;
820 821
        if (!QFile::exists(fileName))
            return;
822
        IEditor *e = em->openEditor(view(), fileName, Id(id), Core::EditorManager::IgnoreNavigationHistory
823 824 825 826 827
                                    | Core::EditorManager::NoActivate);

        if (!e) {
            QModelIndex idx = em->openedEditorsModel()->firstRestoredEditor();
            if (idx.isValid())
828
                em->activateEditorForIndex(view(), idx, Core::EditorManager::IgnoreNavigationHistory
829 830 831 832 833 834 835 836 837 838
                                    | Core::EditorManager::NoActivate);
        }

        if (e) {
            e->restoreState(editorState);
            if (mode == "currenteditor")
                em->setCurrentEditor(e);
        }
    }
}