editorview.cpp 27.6 KB
Newer Older
1
/**************************************************************************
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
26
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
27
**
28
**************************************************************************/
29
30
31
32

#include "editorview.h"
#include "editormanager.h"
#include "coreimpl.h"
33
#include "minisplitter.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#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/QToolBar>
#include <QtGui/QToolButton>
#ifdef Q_WS_MAC
#include <qmacstyle_mac.h>
#endif

Q_DECLARE_METATYPE(Core::IEditor *)

using namespace Core;
using namespace Core::Internal;


//================EditorModel====================
64
65
66
67
68
69
70
71
72
73
74
75

QString EditorModel::Entry::fileName() const {
    return editor ? editor->file()->fileName() : m_fileName;
}
QString EditorModel::Entry::displayName() const {
    return editor ? editor->displayName() : m_displayName;
}
QByteArray EditorModel::Entry::kind() const
{
    return editor ? QByteArray(editor->kind()) : m_kind;
}

76
77
78
int EditorModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
79
    return 2;
80
81
82
83
84
85
86
87
88
}

int EditorModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return m_editors.count();
    return 0;
}

89
90
91
92
93
94
95
96
97
QList<IEditor *> EditorModel::editors() const
{
    QList<IEditor *> result;
    foreach (Entry entry, m_editors)
        if (entry.editor)
            result += entry.editor;
    return result;
}

mae's avatar
mae committed
98
void EditorModel::addEditor(IEditor *editor, bool isDuplicate)
99
{
mae's avatar
mae committed
100
101
102
103
104
    if (isDuplicate) {
        m_duplicateEditors.append(editor);
        return;
    }

105
106
107
108
109
110
111
112
113
114
115
116
117
    Entry entry;
    entry.editor = editor;
    addEntry(entry);
}

void EditorModel::addRestoredEditor(const QString &fileName, const QString &displayName, const QByteArray &kind)
{
    Entry entry;
    entry.m_fileName = fileName;
    entry.m_displayName = displayName;
    entry.m_kind = kind;
    addEntry(entry);
}
118

119
120
121
122
123
124
125
126
QModelIndex EditorModel::firstRestoredEditor() const
{
    for (int i = 0; i < m_editors.count(); ++i)
        if (!m_editors.at(i).editor)
            return createIndex(i, 0);
    return QModelIndex();
}

127
128
129
130
131
132
133
134
void EditorModel::addEntry(const Entry &entry)
{
    QString fileName = entry.fileName();

    int previousIndex = findFileName(fileName);
    if (previousIndex >= 0) {
        if (entry.editor && m_editors.at(previousIndex).editor == 0) {
            m_editors[previousIndex] = entry;
135
            connect(entry.editor, SIGNAL(changed()), this, SLOT(itemChanged()));
136
137
138
139
140
        }
        return;
    }

    int index;
141
    QString displayName = entry.displayName();
142
    for (index = 0; index < m_editors.count(); ++index) {
143
        if (displayName < m_editors.at(index).displayName())
144
            break;
145
    }
146
147

    beginInsertRows(QModelIndex(), index, index);
148
149
150
    m_editors.insert(index, entry);
    if (entry.editor)
        connect(entry.editor, SIGNAL(changed()), this, SLOT(itemChanged()));
151
152
153
    endInsertRows();
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

int EditorModel::findEditor(IEditor *editor) const
{
    for (int i = 0; i < m_editors.count(); ++i)
        if (m_editors.at(i).editor == editor)
            return i;
    return -1;
}

int EditorModel::findFileName(const QString &filename) const
{
    for (int i = 0; i < m_editors.count(); ++i) {
        if (m_editors.at(i).fileName() == filename)
            return i;
    }
    return -1;
}

172
173
void EditorModel::removeEditor(IEditor *editor)
{
mae's avatar
mae committed
174
    m_duplicateEditors.removeAll(editor);
175
    int idx = findEditor(editor);
176
177
178
179
180
181
182
183
    if (idx < 0)
        return;
    beginRemoveRows(QModelIndex(), idx, idx);
    m_editors.removeAt(idx);
    endRemoveRows();
    disconnect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
}

184
185
186
187
188
189
190
191
192
193
194
195
196
void EditorModel::removeEditor(const QModelIndex &index)
{
    int idx = index.row();
    if (idx < 0)
        return;
    IEditor *editor= m_editors.at(idx).editor;
    beginRemoveRows(QModelIndex(), idx, idx);
    m_editors.removeAt(idx);
    endRemoveRows();
    if (editor)
        disconnect(editor, SIGNAL(changed()), this, SLOT(itemChanged()));
}

197
198
199
200
201
202
203
204
205
206
207
void EditorModel::removeAllRestoredEditors()
{
    for (int i = m_editors.count()-1; i >= 0; --i) {
        if (!m_editors.at(i).editor) {
            beginRemoveRows(QModelIndex(), i, i);
            m_editors.removeAt(i);
            endRemoveRows();
        }
    }
}

mae's avatar
mae committed
208
209
210
211
212
213
214
215
bool EditorModel::isDuplicate(IEditor *editor) const
{
    return m_duplicateEditors.contains(editor);
}

IEditor *EditorModel::originalForDuplicate(IEditor *duplicate) const
{
    IFile *file = duplicate->file();
216
217
218
    foreach(Entry e, m_editors)
        if (e.editor && e.editor->file() == file)
            return e.editor;
mae's avatar
mae committed
219
220
221
222
223
224
225
226
227
228
229
230
    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;
}
231
232
233

void EditorModel::emitDataChanged(IEditor *editor)
{
234
    int idx = findEditor(editor);
235
236
237
238
239
240
241
242
243
    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);
244
    if (column < 0 || column > 1 || row < 0 || row >= m_editors.count())
245
246
247
248
249
250
        return QModelIndex();
    return createIndex(row, column);
}

QVariant EditorModel::data(const QModelIndex &index, int role) const
{
251
    if (!index.isValid() || (index.column() != 0 && role < Qt::UserRole))
252
        return QVariant();
253
    Entry e = m_editors.at(index.row());
254
255
    switch (role) {
    case Qt::DisplayRole:
256
257
258
        return (e.editor && e.editor->file()->isModified())
                ? e.displayName() + QLatin1String("*")
                : e.displayName();
259
    case Qt::DecorationRole:
260
        return (e.editor && e.editor->file()->isReadOnly())
261
262
263
                ? QIcon(QLatin1String(":/core/images/locked.png"))
                : QIcon();
    case Qt::ToolTipRole:
264
265
266
        return e.fileName().isEmpty()
                ? e.displayName()
                : QDir::toNativeSeparators(e.fileName());
267
    case Qt::UserRole:
268
269
270
271
272
        return qVariantFromValue(e.editor);
    case Qt::UserRole + 1:
        return e.fileName();
    case Qt::UserRole + 2:
        return e.editor ? QByteArray(e.editor->kind()) : e.kind();
273
274
275
276
277
278
279
280
    default:
        return QVariant();
    }
    return QVariant();
}

QModelIndex EditorModel::indexOf(IEditor *editor) const
{
281
    int idx = findEditor(editor);
282
283
284
285
286
287
288
    if (idx < 0)
        return indexOf(editor->file()->fileName());
    return createIndex(idx, 0);
}

QModelIndex EditorModel::indexOf(const QString &fileName) const
{
289
290
291
    int idx = findFileName(fileName);
    if (idx >= 0)
        return createIndex(idx, 0);
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    return QModelIndex();
}


void EditorModel::itemChanged()
{
    emitDataChanged(qobject_cast<IEditor*>(sender()));
}

//================EditorView====================

EditorView::EditorView(EditorModel *model, QWidget *parent) :
    QWidget(parent),
    m_toolBar(new QWidget),
    m_container(new QStackedWidget(this)),
    m_editorList(new QComboBox),
    m_closeButton(new QToolButton),
    m_lockButton(new QToolButton),
    m_defaultToolBar(new QToolBar(this)),
    m_infoWidget(new QFrame(this)),
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
312
    m_editorForInfoWidget(0),
hjk's avatar
hjk committed
313
    m_statusHLine(new QFrame(this)),
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
314
    m_statusWidget(new QFrame(this))
315
{
316
    QVBoxLayout *tl = new QVBoxLayout(this);
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    tl->setSpacing(0);
    tl->setMargin(0);
    {
        m_editorList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        m_editorList->setMinimumContentsLength(20);
        m_editorList->setModel(model);
        m_editorList->setMaxVisibleItems(40);

        QToolBar *editorListToolBar = new QToolBar;

        editorListToolBar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
        editorListToolBar->addWidget(m_editorList);

        m_defaultToolBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        m_activeToolBar = m_defaultToolBar;

        QHBoxLayout *toolBarLayout = new QHBoxLayout;
        toolBarLayout->setMargin(0);
        toolBarLayout->setSpacing(0);
        toolBarLayout->addWidget(m_defaultToolBar);
        m_toolBar->setLayout(toolBarLayout);

        m_lockButton->setAutoRaise(true);
        m_lockButton->setProperty("type", QLatin1String("dockbutton"));

        m_closeButton->setAutoRaise(true);
        m_closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
        m_closeButton->setProperty("type", QLatin1String("dockbutton"));

        QToolBar *rightToolBar = new QToolBar;
        rightToolBar->setLayoutDirection(Qt::RightToLeft);
        rightToolBar->addWidget(m_closeButton);
        rightToolBar->addWidget(m_lockButton);

        QHBoxLayout *toplayout = new QHBoxLayout;
        toplayout->setSpacing(0);
        toplayout->setMargin(0);
        toplayout->addWidget(editorListToolBar);
        toplayout->addWidget(m_toolBar, 1); // Custom toolbar stretches
        toplayout->addWidget(rightToolBar);

        QWidget *top = new QWidget;
        QVBoxLayout *vlayout = new QVBoxLayout(top);
        vlayout->setSpacing(0);
        vlayout->setMargin(0);
        vlayout->addLayout(toplayout);
        tl->addWidget(top);

mae's avatar
mae committed
365
        connect(m_editorList, SIGNAL(activated(int)), this, SLOT(listSelectionActivated(int)));
366
        connect(m_lockButton, SIGNAL(clicked()), this, SLOT(makeEditorWritable()));
mae's avatar
mae committed
367
        connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeView()), Qt::QueuedConnection);
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
    }
    {
        m_infoWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
        m_infoWidget->setLineWidth(1);
        m_infoWidget->setForegroundRole(QPalette::ToolTipText);
        m_infoWidget->setBackgroundRole(QPalette::ToolTipBase);
        m_infoWidget->setAutoFillBackground(true);

        QHBoxLayout *hbox = new QHBoxLayout(m_infoWidget);
        hbox->setMargin(2);
        m_infoWidgetLabel = new QLabel("Placeholder");
        m_infoWidgetLabel->setForegroundRole(QPalette::ToolTipText);
        hbox->addWidget(m_infoWidgetLabel);
        hbox->addStretch(1);

        m_infoWidgetButton = new QToolButton;
        m_infoWidgetButton->setText(tr("Placeholder"));
        hbox->addWidget(m_infoWidgetButton);

        QToolButton *closeButton = new QToolButton;
        closeButton->setAutoRaise(true);
        closeButton->setIcon(QIcon(":/core/images/clear.png"));
        closeButton->setToolTip(tr("Close"));
        connect(closeButton, SIGNAL(clicked()), m_infoWidget, SLOT(hide()));

        hbox->addWidget(closeButton);

        m_infoWidget->setVisible(false);
        tl->addWidget(m_infoWidget);
    }
398

399
400
    tl->addWidget(m_container);

401
    {
hjk's avatar
hjk committed
402
403
        m_statusHLine->setFrameStyle(QFrame::HLine);

404
405
        m_statusWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
        m_statusWidget->setLineWidth(1);
hjk's avatar
hjk committed
406
407
        //m_statusWidget->setForegroundRole(QPalette::ToolTipText);
        //m_statusWidget->setBackgroundRole(QPalette::ToolTipBase);
408
409
410
411
412
413
414
415
416
417
418
419
420
421
        m_statusWidget->setAutoFillBackground(true);


        QHBoxLayout *hbox = new QHBoxLayout(m_statusWidget);
        hbox->setMargin(2);
        m_statusWidgetLabel = new QLabel("Placeholder");
        m_statusWidgetLabel->setForegroundRole(QPalette::ToolTipText);
        hbox->addWidget(m_statusWidgetLabel);
        hbox->addStretch(1);

        m_statusWidgetButton = new QToolButton;
        m_statusWidgetButton->setText(tr("Placeholder"));
        hbox->addWidget(m_statusWidgetButton);

422
        m_statusHLine->setVisible(false);
423
        m_statusWidget->setVisible(false);
hjk's avatar
hjk committed
424
        tl->addWidget(m_statusHLine);
425
426
427
428
429
430
        tl->addWidget(m_statusWidget);
    }
}

EditorView::~EditorView()
{
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
}

void EditorView::showEditorInfoBar(const QString &kind,
                                           const QString &infoText,
                                           const QString &buttonText,
                                           QObject *object, const char *member)
{
    m_infoWidgetKind = kind;
    m_infoWidgetLabel->setText(infoText);
    m_infoWidgetButton->setText(buttonText);
    m_infoWidgetButton->disconnect();
    if (object && member)
        connect(m_infoWidgetButton, SIGNAL(clicked()), object, member);
    m_infoWidget->setVisible(true);
    m_editorForInfoWidget = currentEditor();
}

void EditorView::hideEditorInfoBar(const QString &kind)
{
    if (kind == m_infoWidgetKind)
        m_infoWidget->setVisible(false);
}

454
void EditorView::showEditorStatusBar(const QString &kind,
455
456
457
                                     const QString &infoText,
                                     const QString &buttonText,
                                     QObject *object, const char *member)
458
459
460
461
462
463
{
    m_statusWidgetKind = kind;
    m_statusWidgetLabel->setText(infoText);
    m_statusWidgetButton->setText(buttonText);
    m_statusWidgetButton->disconnect();
    if (object && member)
464
        connect(m_statusWidgetButton, SIGNAL(clicked()), object, member);
465
    m_statusWidget->setVisible(true);
hjk's avatar
hjk committed
466
    m_statusHLine->setVisible(true);
467
468
    //m_editorForInfoWidget = currentEditor();
}
469

470
void EditorView::hideEditorStatusBar(const QString &kind)
471
{
hjk's avatar
hjk committed
472
    if (kind == m_statusWidgetKind) {
473
        m_statusWidget->setVisible(false);
hjk's avatar
hjk committed
474
475
        m_statusHLine->setVisible(false);
    }
476
477
478
479
}

void EditorView::addEditor(IEditor *editor)
{
mae's avatar
mae committed
480
    if (m_editors.contains(editor))
481
482
        return;

mae's avatar
mae committed
483
484
    m_editors.append(editor);

mae's avatar
mae committed
485
    m_container->addWidget(editor->widget());
486
487
488
489
490
491
492
493
    m_widgetEditorMap.insert(editor->widget(), editor);

    QToolBar *toolBar = editor->toolBar();
    if (toolBar) {
        toolBar->setVisible(false); // will be made visible in setCurrentEditor
        m_toolBar->layout()->addWidget(toolBar);
    }
    connect(editor, SIGNAL(changed()), this, SLOT(checkEditorStatus()));
mae's avatar
mae committed
494

mae's avatar
mae committed
495
496
    if (editor == currentEditor())
        setCurrentEditor(editor);
497
498
}

499
500
bool EditorView::hasEditor(IEditor *editor) const
{
mae's avatar
mae committed
501
    return m_editors.contains(editor);
502
503
}

mae's avatar
mae committed
504
void EditorView::closeView()
505
{
506
    EditorManager *em = CoreImpl::instance()->editorManager();
mae's avatar
mae committed
507
    em->closeView(this);
508
509
510
511
512
}

void EditorView::removeEditor(IEditor *editor)
{
    QTC_ASSERT(editor, return);
mae's avatar
mae committed
513
514
515
    if (!m_editors.contains(editor))
        return;

516
    const int index = m_container->indexOf(editor->widget());
mae's avatar
mae committed
517
    QTC_ASSERT((index != -1), return);
mae's avatar
mae committed
518
    bool wasCurrent = (index == m_container->currentIndex());
mae's avatar
mae committed
519
520
521
522
523
524
525
526
527
528
529
    m_editors.removeAll(editor);

    m_container->removeWidget(editor->widget());
    m_widgetEditorMap.remove(editor->widget());
    editor->widget()->setParent(0);
    disconnect(editor, SIGNAL(changed()), this, SLOT(updateEditorStatus()));
    QToolBar *toolBar = editor->toolBar();
    if (toolBar != 0) {
        if (m_activeToolBar == toolBar) {
            m_activeToolBar = m_defaultToolBar;
            m_activeToolBar->setVisible(true);
530
        }
mae's avatar
mae committed
531
532
533
        m_toolBar->layout()->removeWidget(toolBar);
        toolBar->setVisible(false);
        toolBar->setParent(0);
534
    }
mae's avatar
mae committed
535
536
    if (wasCurrent && m_editors.count())
        setCurrentEditor(m_editors.last());
537
538
539
540
541
542
543
544
545
}

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

mae's avatar
mae committed
546
void EditorView::setCurrentEditor(IEditor *editor)
547
548
549
550
{
    if (!editor || m_container->count() <= 0
        || m_container->indexOf(editor->widget()) == -1)
        return;
mae's avatar
mae committed
551
552
    m_editors.removeAll(editor);
    m_editors.append(editor);
553

554
555
    const int idx = m_container->indexOf(editor->widget());
    QTC_ASSERT(idx >= 0, return);
556
557
    m_container->setCurrentIndex(idx);
    m_editorList->setCurrentIndex(qobject_cast<EditorModel*>(m_editorList->model())->indexOf(editor->file()->fileName()).row());
558
559
    updateEditorStatus(editor);
    updateToolBar(editor);
mae's avatar
mae committed
560

561
    // FIXME: this keeps the editor hidden if switching from A to B and back
562
563
564
565
566
567
568
569
    if (editor != m_editorForInfoWidget) {
        m_infoWidget->hide();
        m_editorForInfoWidget = 0;
    }
}

void EditorView::checkEditorStatus()
{
570
571
572
    IEditor *editor = qobject_cast<IEditor *>(sender());
    if (editor == currentEditor())
        updateEditorStatus(editor);
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
}

void EditorView::updateEditorStatus(IEditor *editor)
{
    static const QIcon lockedIcon(QLatin1String(":/core/images/locked.png"));
    static const QIcon unlockedIcon(QLatin1String(":/core/images/unlocked.png"));

    if (editor->file()->isReadOnly()) {
        m_lockButton->setIcon(lockedIcon);
        m_lockButton->setEnabled(!editor->file()->fileName().isEmpty());
        m_lockButton->setToolTip(tr("Make writable"));
    } else {
        m_lockButton->setIcon(unlockedIcon);
        m_lockButton->setEnabled(false);
        m_lockButton->setToolTip(tr("File is writable"));
    }
    if (currentEditor() == editor)
        m_editorList->setToolTip(
                editor->file()->fileName().isEmpty()
                ? editor->displayName()
                    : QDir::toNativeSeparators(editor->file()->fileName())
                    );

}

void EditorView::updateToolBar(IEditor *editor)
{
    QToolBar *toolBar = editor->toolBar();
    if (!toolBar)
        toolBar = m_defaultToolBar;
    if (m_activeToolBar == toolBar)
        return;
    toolBar->setVisible(true);
    m_activeToolBar->setVisible(false);
    m_activeToolBar = toolBar;
}

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

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


void EditorView::makeEditorWritable()
{
    CoreImpl::instance()->editorManager()->makeEditorWritable(currentEditor());
}

mae's avatar
mae committed
626
void EditorView::listSelectionActivated(int index)
627
{
628
    EditorManager *em = CoreImpl::instance()->editorManager();
629
    QAbstractItemModel *model = m_editorList->model();
630
631
632
633
634
    if (IEditor *editor = model->data(model->index(index, 0), Qt::UserRole).value<IEditor*>()) {
        em->activateEditor(this, editor);
    } else {
        em->activateEditor(model->index(index, 0), this);
    }
635
636
637
}


mae's avatar
mae committed
638
639

SplitterOrView::SplitterOrView(Internal::EditorModel *model)
mae's avatar
mae committed
640
{
mae's avatar
mae committed
641
    m_isRoot = false;
mae's avatar
mae committed
642
    m_layout = new QStackedLayout(this);
mae's avatar
mae committed
643
    m_view = new EditorView(model ? model : CoreImpl::instance()->editorManager()->openedEditorsModel());
mae's avatar
mae committed
644
645
    m_splitter = 0;
    m_layout->addWidget(m_view);
mae's avatar
mae committed
646
    setFocusPolicy(Qt::ClickFocus);
mae's avatar
mae committed
647
648
}

mae's avatar
mae committed
649
SplitterOrView::SplitterOrView(Core::IEditor *editor)
mae's avatar
mae committed
650
651
652
653
654
655
{
    m_isRoot = false;
    m_layout = new QStackedLayout(this);
    m_view = new EditorView(CoreImpl::instance()->editorManager()->openedEditorsModel());
    m_view->addEditor(editor);
    m_splitter = 0;
mae's avatar
mae committed
656
    m_layout->addWidget(m_view);
mae's avatar
mae committed
657
    setFocusPolicy(Qt::ClickFocus);
mae's avatar
mae committed
658
659
}

660
661
662
663
664
665
666
667
668
669
670
671
SplitterOrView::~SplitterOrView()
{
    delete m_layout;
    m_layout = 0;
    delete m_view;
    m_view = 0;
    delete m_splitter;
    m_splitter = 0;
}



mae's avatar
mae committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
void SplitterOrView::focusInEvent(QFocusEvent *)
{
    CoreImpl::instance()->editorManager()->setCurrentView(this);
}

void SplitterOrView::paintEvent(QPaintEvent *)
{
    if  (CoreImpl::instance()->editorManager()->currentView() != this)
        return;
    QPainter painter(this);

    // Discreet indication where an editor would be
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    QColor shadeBrush(Qt::black);
    shadeBrush.setAlpha(10);
    painter.setBrush(shadeBrush);
    const int r = 3;
    painter.drawRoundedRect(rect().adjusted(r, r, -r, -r), r * 2, r * 2);

    if (hasFocus()) {
#ifdef Q_WS_MAC
        // With QMacStyle, we have to draw our own focus rect, since I didn't find
        // a way to draw the nice mac focus rect _inside_ this widget
        if (qobject_cast<QMacStyle *>(style())) {
            painter.setPen(Qt::DotLine);
            painter.setBrush(Qt::NoBrush);
            painter.setOpacity(0.75);
            painter.drawRect(rect());
        } else {
#endif
            QStyleOptionFocusRect option;
            option.initFrom(this);
            option.backgroundColor = palette().color(QPalette::Background);

            // Some styles require a certain state flag in order to draw the focus rect
            option.state |= QStyle::State_KeyboardFocusChange;

            style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, &painter);
#ifdef Q_WS_MAC
        }
#endif
    }
}
716
717
718
719
720
721
722
723
724
725
726
727
728
729

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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
745
746
SplitterOrView *SplitterOrView::findView(Core::IEditor *editor)
{
747
    if (!editor || hasEditor(editor))
mae's avatar
mae committed
748
749
750
751
752
753
754
755
756
757
758
        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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
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
803
SplitterOrView *SplitterOrView::findNextView(SplitterOrView *view)
804
805
{
    bool found = false;
mae's avatar
mae committed
806
    return findNextView_helper(view, &found);
807
808
}

mae's avatar
mae committed
809
SplitterOrView *SplitterOrView::findNextView_helper(SplitterOrView *view, bool *found)
810
811
812
813
814
{
    if (*found && m_view) {
        return this;
    }

mae's avatar
mae committed
815
    if (this == view) {
816
817
818
819
820
821
822
        *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
823
                if (SplitterOrView *result = splitterOrView->findNextView_helper(view, found))
824
825
826
827
828
829
830
                    return result;
            }
        }
    }
    return 0;
}

831
832
833
834
835
836
837
QSize SplitterOrView::minimumSizeHint() const
{
    if (m_splitter)
        return m_splitter->minimumSizeHint();
    return QSize(64, 64);
}

838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
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
856
857
858
void SplitterOrView::split(Qt::Orientation orientation)
{
    Q_ASSERT(m_view && m_splitter == 0);
859
    m_splitter = new MiniSplitter(this);
mae's avatar
mae committed
860
    m_splitter->setOrientation(orientation);
mae's avatar
mae committed
861
862
    m_layout->addWidget(m_splitter);
    EditorManager *em = CoreImpl::instance()->editorManager();
mae's avatar
mae committed
863
864
    Core::IEditor *e = m_view->currentEditor();

mae's avatar
mae committed
865
866
867
868
    if (e) {

        m_view->removeEditor(e);
        m_splitter->addWidget(new SplitterOrView(e));
869

mae's avatar
mae committed
870
871
872
873
874
875
876
877
878
879
        if (e->duplicateSupported()) {
            Core::IEditor *duplicate = em->duplicateEditor(e);
            m_splitter->addWidget(new SplitterOrView(duplicate));
        } else {
            m_splitter->addWidget(new SplitterOrView());
        }
    } else {
        m_splitter->addWidget(new SplitterOrView());
        m_splitter->addWidget(new SplitterOrView());
    }
mae's avatar
mae committed
880

mae's avatar
mae committed
881
    m_layout->setCurrentWidget(m_splitter);
mae's avatar
mae committed
882

mae's avatar
mae committed
883
884
    if (m_view && !m_isRoot) {
        em->emptyView(m_view);
mae's avatar
mae committed
885
886
887
888
        delete m_view;
        m_view = 0;
    }

889
    em->setCurrentView(findFirstView());
mae's avatar
mae committed
890
891
    if (e)
        em->activateEditor(e);
mae's avatar
mae committed
892
893
}

894
void SplitterOrView::unsplitAll()
mae's avatar
mae committed
895
{
896
897
898
899
900
    m_splitter->hide();
    m_layout->removeWidget(m_splitter); // workaround Qt bug
    unsplitAll_helper();
    delete m_splitter;
    m_splitter = 0;
mae's avatar
mae committed
901
902
}

903
void SplitterOrView::unsplitAll_helper()
mae's avatar
mae committed
904
{
905
906
907
908
909
910
911
    if (!m_isRoot && m_view)
        CoreImpl::instance()->editorManager()->emptyView(m_view);
    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
912
913
914
915
        }
    }
}

mae's avatar
mae committed
916
void SplitterOrView::unsplit()
mae's avatar
mae committed
917
918
919
{
    if (!m_splitter)
        return;
mae's avatar
mae committed
920

921
922
923
924
925
    Q_ASSERT(m_splitter->count() == 1);
    EditorManager *em = CoreImpl::instance()->editorManager();
    SplitterOrView *childSplitterOrView = qobject_cast<SplitterOrView*>(m_splitter->widget(0));
    QSplitter *oldSplitter = m_splitter;
    m_splitter = 0;
mae's avatar
mae committed
926

927
928
    if (childSplitterOrView->isSplitter()) {
        Q_ASSERT(childSplitterOrView->view() == 0);
929
        m_splitter = childSplitterOrView->takeSplitter();
930
931
932
933
934
935
936
937
938
939
940
941
942
        m_layout->addWidget(m_splitter);
        m_layout->setCurrentWidget(m_splitter);
    } else {
        EditorView *childView = childSplitterOrView->view();
        Q_ASSERT(childView);
        if (m_view) {
            if (IEditor *e = childView->currentEditor()) {
                childView->removeEditor(e);
                m_view->addEditor(e);
                m_view->setCurrentEditor(e);
            }
            em->emptyView(childView);
        } else {
943
            m_view = childSplitterOrView->takeView();
944
            m_layout->addWidget(m_view);
mae's avatar
mae committed
945
        }
946
        m_layout->setCurrentWidget(m_view);
mae's avatar
mae committed
947
    }
948
949
    delete oldSplitter;
    em->setCurrentView(findFirstView());
mae's avatar
mae committed
950
}