editorview.cpp 26.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
hjk's avatar
hjk committed
3
4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
31

#include "editorview.h"
#include "editormanager.h"
hjk's avatar
hjk committed
32
#include "icore.h"
33
#include "minisplitter.h"
34
#include "openeditorsmodel.h"
Lasse Holmstedt's avatar
Lasse Holmstedt committed
35

36
#include <coreplugin/editortoolbar.h>
37
#include <coreplugin/coreconstants.h>
38
#include <coreplugin/infobar.h>
39
#include <coreplugin/actionmanager/actionmanager.h>
40
#include <coreplugin/editormanager/ieditor.h>
41

42
#include <coreplugin/findplaceholder.h>
43
44
#include <utils/qtcassert.h>

45
46
47
48
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QMimeData>
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <QApplication>
#include <QComboBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QPainter>
#include <QStackedWidget>
#include <QStyle>
#include <QStyleOption>
#include <QToolButton>
#include <QMenu>
#include <QClipboard>
#include <QAction>
#include <QSplitter>
#include <QStackedLayout>
65

66
#ifdef Q_OS_MAC
67
68
69
70
71
72
73
#include <qmacstyle_mac.h>
#endif

using namespace Core;
using namespace Core::Internal;


Friedemann Kleint's avatar
Friedemann Kleint committed
74
// ================EditorView====================
75

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

    m_infoBarDisplay->setTarget(tl, 1);
100

101
102
    tl->addWidget(m_container);

con's avatar
con committed
103
104
    tl->addWidget(new FindToolBarPlaceHolder(this));

105
    {
hjk's avatar
hjk committed
106
107
        m_statusHLine->setFrameStyle(QFrame::HLine);

108
109
        m_statusWidget->setFrameStyle(QFrame::NoFrame);
        m_statusWidget->setLineWidth(0);
110
111
112
        m_statusWidget->setAutoFillBackground(true);

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

        m_statusWidgetButton = new QToolButton;
120
        m_statusWidgetButton->setContentsMargins(0, 0, 0, 0);
121
122
        hbox->addWidget(m_statusWidgetButton);

123
        m_statusHLine->setVisible(false);
124
        m_statusWidget->setVisible(false);
hjk's avatar
hjk committed
125
        tl->addWidget(m_statusHLine);
126
127
        tl->addWidget(m_statusWidget);
    }
128

Lasse Holmstedt's avatar
Lasse Holmstedt committed
129
    updateNavigatorActions();
130
131
132
133
}

EditorView::~EditorView()
{
134
135
}

136
137
138

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

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

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

170
171
172
173
174
void EditorView::setCloseSplitEnabled(bool enable)
{
    m_toolBar->setCloseSplitEnabled(enable);
}

Eike Ziller's avatar
Eike Ziller committed
175
176
177
178
179
void EditorView::setCloseSplitIcon(const QIcon &icon)
{
    m_toolBar->setCloseSplitIcon(icon);
}

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

mae's avatar
mae committed
185
186
    m_editors.append(editor);

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

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

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

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

206
    const int index = m_container->indexOf(editor->widget());
mae's avatar
mae committed
207
    QTC_ASSERT((index != -1), return);
mae's avatar
mae committed
208
    bool wasCurrent = (index == m_container->currentIndex());
mae's avatar
mae committed
209
210
211
212
213
    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
214
215
    m_toolBar->removeToolbarForEditor(editor);

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

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

227
228
229
void EditorView::listSelectionActivated(int index)
{
    QAbstractItemModel *model = EditorManager::instance()->openedEditorsModel();
230
    EditorManager::instance()->activateEditorForIndex(this, model->index(index, 0), Core::EditorManager::ModeSwitch);
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
256
257
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
258
void EditorView::setCurrentEditor(IEditor *editor)
259
260
{
    if (!editor || m_container->count() <= 0
mae's avatar
mae committed
261
        || m_container->indexOf(editor->widget()) == -1) {
Lasse Holmstedt's avatar
Lasse Holmstedt committed
262
        m_toolBar->updateEditorStatus(0);
263
        m_infoBarDisplay->setInfoBar(0);
Eike Ziller's avatar
Eike Ziller committed
264
        QTC_CHECK(m_container->count() == 0);
265
        return;
mae's avatar
mae committed
266
    }
267

mae's avatar
mae committed
268
269
    m_editors.removeAll(editor);
    m_editors.append(editor);
270

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

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

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

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

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

mae's avatar
mae committed
291
292
293
294
void EditorView::updateEditorHistory(IEditor *editor)
{
    if (!editor)
        return;
295
    IDocument *document = editor->document();
mae's avatar
mae committed
296

297
    if (!document)
mae's avatar
mae committed
298
299
300
301
302
        return;

    QByteArray state = editor->saveState();

    EditLocation location;
303
304
    location.document = document;
    location.fileName = document->fileName();
305
    location.id = editor->id();
mae's avatar
mae committed
306
307
308
    location.state = QVariant(state);

    for(int i = 0; i < m_editorHistory.size(); ++i) {
309
310
        if (m_editorHistory.at(i).document == 0
            || m_editorHistory.at(i).document == document
mae's avatar
mae committed
311
312
313
314
315
316
            ){
            m_editorHistory.removeAt(i--);
            continue;
        }
    }
    m_editorHistory.prepend(location);
317
318
319
320
321
322
}

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

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

331
332
333
334
    if (!editor)
        editor = currentEditor();
    if (!editor)
        return;
335
    IDocument *document = editor->document();
336

337
    if (!document)
338
339
340
341
342
343
344
345
346
        return;

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

mae's avatar
mae committed
347
    EditLocation location;
348
349
    location.document = document;
    location.fileName = document->fileName();
350
    location.id = editor->id();
mae's avatar
mae committed
351
    location.state = QVariant(state);
352
353
354
355
356
357
    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) {
358
            m_navigationHistory.removeFirst();
359
360
            --m_currentNavigationHistoryPosition;
        } else {
361
            m_navigationHistory.removeLast();
362
363
        }
    }
Lasse Holmstedt's avatar
Lasse Holmstedt committed
364
    updateNavigatorActions();
365
366
}

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

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

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

void EditorView::updateCurrentPositionInNavigationHistory()
{
    IEditor *editor = currentEditor();
392
    if (!editor || !editor->document())
393
394
        return;

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

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

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


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

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

482
483
484
485
486
487
488
489
490
491
SplitterOrView::~SplitterOrView()
{
    delete m_layout;
    m_layout = 0;
    delete m_view;
    m_view = 0;
    delete m_splitter;
    m_splitter = 0;
}

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

void SplitterOrView::paintEvent(QPaintEvent *)
{
hjk's avatar
hjk committed
502
    if (ICore::editorManager()->currentSplitterOrView() != this)
503
504
505
        return;

    if (!m_view || hasEditors())
mae's avatar
mae committed
506
507
        return;

508
509
    // Discreet indication where an editor would be if there is none
    QPainter painter(this);
mae's avatar
mae committed
510
511
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
512
    painter.setBrush(palette().color(QPalette::Background).darker(107));
mae's avatar
mae committed
513
    const int r = 3;
514
515
516
    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
517
}
518
519
520
521
522
523
524
525
526
527
528
529
530
531

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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
547
548
SplitterOrView *SplitterOrView::findView(Core::IEditor *editor)
{
549
    if (!editor || hasEditor(editor))
mae's avatar
mae committed
550
551
552
553
554
555
556
557
558
559
560
        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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
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
605
SplitterOrView *SplitterOrView::findNextView(SplitterOrView *view)
606
607
{
    bool found = false;
mae's avatar
mae committed
608
    return findNextView_helper(view, &found);
609
610
}

mae's avatar
mae committed
611
SplitterOrView *SplitterOrView::findNextView_helper(SplitterOrView *view, bool *found)
612
613
614
615
616
{
    if (*found && m_view) {
        return this;
    }

mae's avatar
mae committed
617
    if (this == view) {
618
619
620
621
622
623
624
        *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
625
                if (SplitterOrView *result = splitterOrView->findNextView_helper(view, found))
626
627
628
629
630
631
632
                    return result;
            }
        }
    }
    return 0;
}

633
634
635
636
637
638
639
QSize SplitterOrView::minimumSizeHint() const
{
    if (m_splitter)
        return m_splitter->minimumSizeHint();
    return QSize(64, 64);
}

640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
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
658
659
660
void SplitterOrView::split(Qt::Orientation orientation)
{
    Q_ASSERT(m_view && m_splitter == 0);
661
    m_splitter = new MiniSplitter(this);
mae's avatar
mae committed
662
    m_splitter->setOrientation(orientation);
mae's avatar
mae committed
663
    m_layout->addWidget(m_splitter);
hjk's avatar
hjk committed
664
    EditorManager *em = ICore::editorManager();
mae's avatar
mae committed
665
666
    Core::IEditor *e = m_view->currentEditor();

667
    SplitterOrView *view = 0;
668
    SplitterOrView *otherView = 0;
mae's avatar
mae committed
669
670
    if (e) {

671
672
673
        foreach(IEditor *editor, m_view->editors())
            m_view->removeEditor(editor);

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

mae's avatar
mae committed
686
    m_layout->setCurrentWidget(m_splitter);
mae's avatar
mae committed
687

688
    view->view()->copyNavigationHistoryFrom(m_view);
689
    view->view()->setCurrentEditor(view->view()->currentEditor());
690
    otherView->view()->copyNavigationHistoryFrom(m_view);
691
    otherView->view()->setCurrentEditor(otherView->view()->currentEditor());
692

Eike Ziller's avatar
Eike Ziller committed
693
694
695
696
697
698
699
700
    if (orientation == Qt::Horizontal) {
        view->view()->setCloseSplitIcon(QIcon(QLatin1String(Constants::ICON_CLOSE_SPLIT_LEFT)));
        otherView->view()->setCloseSplitIcon(QIcon(QLatin1String(Constants::ICON_CLOSE_SPLIT_RIGHT)));
    } else {
        view->view()->setCloseSplitIcon(QIcon(QLatin1String(Constants::ICON_CLOSE_SPLIT_TOP)));
        otherView->view()->setCloseSplitIcon(QIcon(QLatin1String(Constants::ICON_CLOSE_SPLIT_BOTTOM)));
    }

mae's avatar
mae committed
701
702
    if (m_view && !m_isRoot) {
        em->emptyView(m_view);
mae's avatar
mae committed
703
704
705
706
        delete m_view;
        m_view = 0;
    }

mae's avatar
mae committed
707
    if (e)
708
709
710
        em->activateEditor(view->view(), e);
    else
        em->setCurrentView(view);
mae's avatar
mae committed
711
712
}

713
void SplitterOrView::unsplitAll()
mae's avatar
mae committed
714
{
715
716
717
718
719
    m_splitter->hide();
    m_layout->removeWidget(m_splitter); // workaround Qt bug
    unsplitAll_helper();
    delete m_splitter;
    m_splitter = 0;
mae's avatar
mae committed
720
721
}

722
void SplitterOrView::unsplitAll_helper()
mae's avatar
mae committed
723
{
724
    if (!m_isRoot && m_view)
hjk's avatar
hjk committed
725
        ICore::editorManager()->emptyView(m_view);
726
727
728
729
730
    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
731
732
733
734
        }
    }
}

mae's avatar
mae committed
735
void SplitterOrView::unsplit()
mae's avatar
mae committed
736
737
738
{
    if (!m_splitter)
        return;
mae's avatar
mae committed
739

740
    Q_ASSERT(m_splitter->count() == 1);
hjk's avatar
hjk committed
741
    EditorManager *em = ICore::editorManager();
742
743
744
    SplitterOrView *childSplitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(0));
    QSplitter *oldSplitter = m_splitter;
    m_splitter = 0;
mae's avatar
mae committed
745

746
747
    if (childSplitterOrView->isSplitter()) {
        Q_ASSERT(childSplitterOrView->view() == 0);
748
        m_splitter = childSplitterOrView->takeSplitter();
749
750
751
752
753
754
        m_layout->addWidget(m_splitter);
        m_layout->setCurrentWidget(m_splitter);
    } else {
        EditorView *childView = childSplitterOrView->view();
        Q_ASSERT(childView);
        if (m_view) {
755
            m_view->copyNavigationHistoryFrom(childView);
756
757
758
759
760
761
762
            if (IEditor *e = childView->currentEditor()) {
                childView->removeEditor(e);
                m_view->addEditor(e);
                m_view->setCurrentEditor(e);
            }
            em->emptyView(childView);
        } else {
763
            m_view = childSplitterOrView->takeView();
764
            m_layout->addWidget(m_view);
Eike Ziller's avatar
Eike Ziller committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
            QSplitter *parentSplitter = qobject_cast<QSplitter *>(parentWidget());
            if (parentSplitter) { // not the toplevel splitterOrView
                if (parentSplitter->orientation() == Qt::Horizontal) {
                    if (parentSplitter->widget(0) == this)
                        m_view->setCloseSplitIcon(QIcon(Constants::ICON_CLOSE_SPLIT_LEFT));
                    else
                        m_view->setCloseSplitIcon(QIcon(Constants::ICON_CLOSE_SPLIT_RIGHT));
                } else {
                    if (parentSplitter->widget(0) == this)
                        m_view->setCloseSplitIcon(QIcon(Constants::ICON_CLOSE_SPLIT_TOP));
                    else
                        m_view->setCloseSplitIcon(QIcon(Constants::ICON_CLOSE_SPLIT_BOTTOM));
                }
            }
mae's avatar
mae committed
779
        }
780
        m_layout->setCurrentWidget(m_view);
mae's avatar
mae committed
781
    }
782
783
    delete oldSplitter;
    em->setCurrentView(findFirstView());
mae's avatar
mae committed
784
}
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800


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();

801
        // don't save state of temporary or ad-hoc editors
802
        if (e && (e->isTemporary() || e->document()->fileName().isEmpty())) {
803
804
805
            // look for another editor that is more suited
            e = 0;
            foreach (IEditor *otherEditor, editors()) {
806
                if (!otherEditor->isTemporary() && !otherEditor->document()->fileName().isEmpty()) {
807
808
809
810
811
812
813
814
                    e = otherEditor;
                    break;
                }
            }
        }

        if (!e) {
            stream << QByteArray("empty");
hjk's avatar
hjk committed
815
        } else if (e == EditorManager::currentEditor()) {
816
            stream << QByteArray("currenteditor")
817
                    << e->document()->fileName() << e->id().toString() << e->saveState();
818
        } else {
819
            stream << QByteArray("editor")
820
                    << e->document()->fileName() << e->id().toString() << e->saveState();
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
        }
    }
    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
840
        EditorManager *em = ICore::editorManager();
841
        QString fileName;
842
        QString id;
843
        QByteArray editorState;
844
        stream >> fileName >> id >> editorState;
845
846
        if (!QFile::exists(fileName))
            return;
847
        IEditor *e = em->openEditor(view(), fileName, Id(id), Core::EditorManager::IgnoreNavigationHistory
848
849
850
851
852
                                    | Core::EditorManager::NoActivate);

        if (!e) {
            QModelIndex idx = em->openedEditorsModel()->firstRestoredEditor();
            if (idx.isValid())
853
                em->activateEditorForIndex(view(), idx, Core::EditorManager::IgnoreNavigationHistory
854
855
856
857
858
859
860
861
862
863
                                    | Core::EditorManager::NoActivate);
        }

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