outputpane.cpp 17 KB
Newer Older
con's avatar
con committed
1
2
3
4
/***************************************************************************
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
10
11
**
** Non-Open Source Usage
**
con's avatar
con committed
12
13
14
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
15
16
17
18
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
19
20
21
22
23
24
25
26
27
28
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
29
** rights. These rights are described in the Nokia Qt GPL Exception
30
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
31
32
**
***************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
#include "outputpane.h"
#include "coreconstants.h"
36
#include "icore.h"
con's avatar
con committed
37
38
39
40
#include "ioutputpane.h"
#include "mainwindow.h"
#include "modemanager.h"

41
#include <coreplugin/actionmanager/actionmanager.h>
42
#include <coreplugin/actionmanager/actioncontainer.h>
con's avatar
con committed
43
44
45
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/editorgroup.h>

46
47
#include <extensionsystem/pluginmanager.h>

con's avatar
con committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QComboBox>
#include <QtGui/QFocusEvent>
#include <QtGui/QHBoxLayout>
#include <QtGui/QMenu>
#include <QtGui/QPainter>
#include <QtGui/QPushButton>
#include <QtGui/QToolBar>
#include <QtGui/QToolButton>
#include <QtGui/QStackedWidget>

using namespace Core;
using namespace Core::Internal;

namespace Core {
namespace Internal {

class OutputPaneToggleButton : public QPushButton
{
public:
    OutputPaneToggleButton(int number, const QString &text, QWidget *parent = 0);
    QSize sizeHint() const;
    void paintEvent(QPaintEvent *event);

private:
    QString m_number;
    QString m_text;
};

} // namespace Internal
} // namespace Core

OutputPanePlaceHolder *OutputPanePlaceHolder::m_current = 0;

OutputPanePlaceHolder::OutputPanePlaceHolder(Core::IMode *mode, QWidget *parent)
84
   : QWidget(parent), m_mode(mode), m_closeable(true)
con's avatar
con committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
    setVisible(false);
    setLayout(new QVBoxLayout);
    QSizePolicy sp;
    sp.setHorizontalPolicy(QSizePolicy::Preferred);
    sp.setVerticalPolicy(QSizePolicy::Preferred);
    sp.setHorizontalStretch(0);
    setSizePolicy(sp);
    layout()->setMargin(0);
    connect(Core::ModeManager::instance(), SIGNAL(currentModeChanged(Core::IMode *)),
            this, SLOT(currentModeChanged(Core::IMode *)));
}

OutputPanePlaceHolder::~OutputPanePlaceHolder()
{
    if (m_current == this) {
101
102
        OutputPaneManager::instance()->setParent(0);
        OutputPaneManager::instance()->hide();
con's avatar
con committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    }
}

void OutputPanePlaceHolder::setCloseable(bool b)
{
    m_closeable = b;
}

bool OutputPanePlaceHolder::closeable()
{
    return m_closeable;
}

void OutputPanePlaceHolder::currentModeChanged(Core::IMode *mode)
{
    if (m_current == this) {
        m_current = 0;
120
121
122
        OutputPaneManager::instance()->setParent(0);
        OutputPaneManager::instance()->hide();
        OutputPaneManager::instance()->updateStatusButtons(false);
con's avatar
con committed
123
124
125
    }
    if (m_mode == mode) {
        m_current = this;
126
127
128
129
        layout()->addWidget(OutputPaneManager::instance());
        OutputPaneManager::instance()->show();
        OutputPaneManager::instance()->updateStatusButtons(isVisible());
        OutputPaneManager::instance()->setCloseable(m_closeable);
con's avatar
con committed
130
131
132
133
    }
}

////
134
// OutputPaneManager
con's avatar
con committed
135
136
////

137
static OutputPaneManager *m_instance = 0;
con's avatar
con committed
138

139
140
141
142
143
144
145
146
147
148
149
150
void OutputPaneManager::create()
{
   m_instance = new OutputPaneManager; 
}

void OutputPaneManager::destroy()
{
    delete m_instance;
    m_instance = 0;
}

OutputPaneManager *OutputPaneManager::instance()
con's avatar
con committed
151
152
153
154
{
    return m_instance;
}

155
void OutputPaneManager::updateStatusButtons(bool visible)
con's avatar
con committed
156
157
158
159
160
161
{
    int idx = m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt();
    if (m_buttons.value(idx))
        m_buttons.value(idx)->setChecked(visible);
}

162
OutputPaneManager::OutputPaneManager(QWidget *parent) :
con's avatar
con committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    QWidget(parent),
    m_widgetComboBox(new QComboBox),
    m_clearButton(new QToolButton),
    m_closeButton(new QToolButton),
    m_closeAction(0),
    m_lastIndex(-1),
    m_outputWidgetPane(new QStackedWidget),
    m_opToolBarWidgets(new QStackedWidget)
{
    setWindowTitle(tr("Output"));
    connect(m_widgetComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changePage()));

    m_clearButton->setIcon(QIcon(Constants::ICON_CLEAN_PANE));
    m_clearButton->setToolTip(tr("Clear"));
    connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clearPage()));

179
    m_closeButton->setIcon(QIcon(":/core/images/closebutton.png"));
con's avatar
con committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    m_closeButton->setProperty("type", QLatin1String("dockbutton"));
    connect(m_closeButton, SIGNAL(clicked()), this, SLOT(slotHide()));

    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->setSpacing(0);
    mainlayout->setMargin(0);
    QToolBar *toolBar = new QToolBar;
    toolBar->addWidget(m_widgetComboBox);
    toolBar->addWidget(m_clearButton);
    toolBar->addWidget(m_opToolBarWidgets);
    m_closeAction = toolBar->addWidget(m_closeButton);
    mainlayout->addWidget(toolBar);
    mainlayout->addWidget(m_outputWidgetPane, 10);
    setLayout(mainlayout);

    m_buttonsWidget = new QWidget;
    m_buttonsWidget->setLayout(new QHBoxLayout);
    m_buttonsWidget->layout()->setContentsMargins(5,0,0,0);
#ifdef Q_OS_MAC
    m_buttonsWidget->layout()->setSpacing(16);
#else
    m_buttonsWidget->layout()->setSpacing(4);
#endif
}

205
OutputPaneManager::~OutputPaneManager()
con's avatar
con committed
206
207
208
{
}

209
QWidget *OutputPaneManager::buttonsWidget()
con's avatar
con committed
210
211
212
213
{
    return m_buttonsWidget;
}

214
void OutputPaneManager::init()
con's avatar
con committed
215
{
hjk's avatar
hjk committed
216
    ActionManager *am = Core::ICore::instance()->actionManager();
217
    ActionContainer *mwindow = am->actionContainer(Constants::M_WINDOW);
con's avatar
con committed
218
219

    // Window->Output Panes
220
    ActionContainer *mpanes = am->createMenu(Constants::M_WINDOW_PANES);
con's avatar
con committed
221
222
223
    mwindow->addMenu(mpanes, Constants::G_WINDOW_PANES);
    mpanes->menu()->setTitle(tr("Output &Panes"));

224
225
    QList<IOutputPane*> panes = ExtensionSystem::PluginManager::instance()
        ->getObjects<IOutputPane>();
con's avatar
con committed
226
227
228
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
256
257
258
    QMultiMap<int, IOutputPane*> sorted;
    foreach (IOutputPane* outPane, panes)
        sorted.insertMulti(outPane->priorityInStatusBar(), outPane);

    QMultiMap<int, IOutputPane*>::const_iterator it, begin;
    begin = sorted.constBegin();
    it = sorted.constEnd();
    int shortcutNumber = 1;
    do {
        --it;
        IOutputPane* outPane = it.value();
        const int idx = m_outputWidgetPane->addWidget(outPane->outputWidget(this));

        m_pageMap.insert(idx, outPane);
        connect(outPane, SIGNAL(showPage(bool)), this, SLOT(showPage(bool)));
        connect(outPane, SIGNAL(hidePage()), this, SLOT(slotHide()));
        connect(outPane, SIGNAL(togglePage(bool)), this, SLOT(togglePage(bool)));

        QWidget *toolButtonsContainer = new QWidget(m_opToolBarWidgets);
        QHBoxLayout *toolButtonsLayout = new QHBoxLayout;
        toolButtonsLayout->setMargin(0);
        toolButtonsLayout->setSpacing(0);
        foreach (QWidget *toolButton, outPane->toolBarWidgets())
            toolButtonsLayout->addWidget(toolButton);
        toolButtonsLayout->addStretch(5);
        toolButtonsContainer->setLayout(toolButtonsLayout);

        m_opToolBarWidgets->addWidget(toolButtonsContainer);

        QString actionId = QString("QtCreator.Pane.%1").arg(outPane->name().simplified());
        actionId.remove(QLatin1Char(' '));
        QAction *action = new QAction(outPane->name(), this);

259
        Command *cmd = am->registerAction(action, actionId, QList<int>() << Constants::C_GLOBAL_ID);
con's avatar
con committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
        if (outPane->priorityInStatusBar() != -1) {
#ifdef Q_OS_MAC
            cmd->setDefaultKeySequence(QKeySequence("Ctrl+" + QString::number(shortcutNumber)));
#else
            cmd->setDefaultKeySequence(QKeySequence("Alt+" + QString::number(shortcutNumber)));
#endif
        }
        mpanes->addAction(cmd);
        m_actions.insert(cmd->action(), idx);

        // TODO priority -1
        if (outPane->priorityInStatusBar() != -1) {
            QPushButton *button = new OutputPaneToggleButton(shortcutNumber, outPane->name());
            ++shortcutNumber;
            m_buttonsWidget->layout()->addWidget(button);
            connect(button, SIGNAL(clicked()), this, SLOT(buttonTriggered()));
            m_buttons.insert(idx, button);
        }

        // Now add the entry to the combobox, since the first item we add sets the currentIndex, thus we need to be set up for that
        m_widgetComboBox->addItem(outPane->name(), idx);

        connect(cmd->action(), SIGNAL(triggered()), this, SLOT(shortcutTriggered()));
        connect(cmd->action(), SIGNAL(changed()), this, SLOT(updateToolTip()));
hjk's avatar
hjk committed
284
    } while (it != begin);
con's avatar
con committed
285
286
287
288

    changePage();
}

289
void OutputPaneManager::shortcutTriggered()
con's avatar
con committed
290
291
292
293
294
295
296
297
298
299
{
    QAction *action = qobject_cast<QAction*>(sender());
    if (action && m_actions.contains(action)) {
        int idx = m_actions.value(action);
        Core::IOutputPane *outputPane = m_pageMap.value(idx);
        // Now check the special case, the output window is already visible,
        // we are already on that page
        // but the outputpane doesn't have focus
        // then just give it focus
        // else do the same as clicking on the button does
hjk's avatar
hjk committed
300
        if (OutputPanePlaceHolder::m_current
con's avatar
con committed
301
302
303
304
305
306
307
308
309
310
311
312
           && OutputPanePlaceHolder::m_current->isVisible()
           && m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt() == idx) {
            if (!outputPane->hasFocus() && outputPane->canFocus())
                outputPane->setFocus();
            else
                slotHide();
        } else {
            outputPane->popup(true);
        }
    }
}

313
void OutputPaneManager::buttonTriggered()
con's avatar
con committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
{
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    QMap<int, QPushButton *>::const_iterator it, end;
    end = m_buttons.constEnd();
    for (it = m_buttons.begin(); it != end; ++it) {
        if (it.value() == button)
            break;
    }
    int idx = it.key();

    if (m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt() == idx &&
        OutputPanePlaceHolder::m_current &&
        OutputPanePlaceHolder::m_current->isVisible() &&
        OutputPanePlaceHolder::m_current->closeable()) {
        // we should toggle and the page is already visible and we are actually closeable
        slotHide();
    } else {
        showPage(idx, true);
    }
}

335
void OutputPaneManager::updateToolTip()
con's avatar
con committed
336
337
338
339
340
341
342
343
344
{
    QAction *action = qobject_cast<QAction*>(sender());
    if (action) {
        QPushButton *button = m_buttons.value(m_actions.value(action));
        if (button)
            button->setToolTip(action->toolTip());
    }
}

345
void OutputPaneManager::slotHide()
con's avatar
con committed
346
347
348
349
350
351
352
353
354
355
356
357
{
    if (OutputPanePlaceHolder::m_current) {
        OutputPanePlaceHolder::m_current->setVisible(false);
        int idx = m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt();
        if (m_buttons.value(idx))
            m_buttons.value(idx)->setChecked(false);
        EditorGroup *group = Core::EditorManager::instance()->currentEditorGroup();
        if (group && group->widget())
            group->widget()->setFocus();
    }
}

358
int OutputPaneManager::findIndexForPage(IOutputPane *out)
con's avatar
con committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
{
    if (!out)
        return -1;

    int stackIndex = -1;
    QMap<int, IOutputPane*>::const_iterator it = m_pageMap.constBegin();
    while (it != m_pageMap.constEnd()) {
        if (it.value() == out) {
            stackIndex = it.key();
            break;
        }
        ++it;
    }
    if (stackIndex > -1)
        return m_widgetComboBox->findData(stackIndex);
    else
        return -1;
}

378
void OutputPaneManager::ensurePageVisible(int idx)
con's avatar
con committed
379
380
381
382
383
384
385
386
387
{
    if (m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt() != idx) {
        m_widgetComboBox->setCurrentIndex(m_widgetComboBox->findData(idx));
    } else {
        changePage();
    }
}


388
void OutputPaneManager::showPage(bool focus)
con's avatar
con committed
389
390
391
392
393
{
    int idx = findIndexForPage(qobject_cast<IOutputPane*>(sender()));
    showPage(idx, focus);
}

394
void OutputPaneManager::showPage(int idx, bool focus)
con's avatar
con committed
395
396
397
398
399
400
{
    IOutputPane *out = m_pageMap.value(idx);
    if (idx > -1) {
        if (!OutputPanePlaceHolder::m_current) {
            // In this mode we don't have a placeholder
            // switch to the output mode and switch the page
401
            ICore::instance()->modeManager()->activateMode(Constants::MODE_OUTPUT);
con's avatar
con committed
402
403
404
405
406
407
408
409
410
411
412
            ensurePageVisible(idx);
        } else {
            // else we make that page visible
            OutputPanePlaceHolder::m_current->setVisible(true);
            ensurePageVisible(idx);
            if (focus && out->canFocus())
                out->setFocus();
        }
    }
}

413
void OutputPaneManager::togglePage(bool focus)
con's avatar
con committed
414
415
{
    int idx = findIndexForPage(qobject_cast<IOutputPane*>(sender()));
416
    if (OutputPanePlaceHolder::m_current
con's avatar
con committed
417
418
419
420
421
422
423
424
       && OutputPanePlaceHolder::m_current->isVisible()
       && m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt() == idx) {
         slotHide();
    } else {
         showPage(idx, focus);
    }
}

425
void OutputPaneManager::setCloseable(bool b)
con's avatar
con committed
426
427
428
429
{
    m_closeAction->setVisible(b);
}

430
bool OutputPaneManager::closeable()
con's avatar
con committed
431
432
433
434
{
    return m_closeButton->isVisibleTo(m_closeButton->parentWidget());
}

435
void OutputPaneManager::focusInEvent(QFocusEvent *e)
con's avatar
con committed
436
437
438
439
440
{
    if (m_outputWidgetPane->currentWidget())
        m_outputWidgetPane->currentWidget()->setFocus(e->reason());
}

441
void OutputPaneManager::changePage()
con's avatar
con committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
{
    if (m_outputWidgetPane->count() <= 0)
        return;

    if (!m_pageMap.contains(m_lastIndex)) {
        int idx = m_outputWidgetPane->currentIndex();
        m_pageMap.value(idx)->visibilityChanged(true);
        if (m_buttons.value(idx)) {
            if (OutputPanePlaceHolder::m_current)
                m_buttons.value(idx)->setChecked(OutputPanePlaceHolder::m_current->isVisible());
            else
                m_buttons.value(idx)->setChecked(false);
        }
        m_lastIndex = idx;
        return;
    }

    int idx = m_widgetComboBox->itemData(m_widgetComboBox->currentIndex()).toInt();
    m_outputWidgetPane->setCurrentIndex(idx);
    m_opToolBarWidgets->setCurrentIndex(idx);
    m_pageMap.value(idx)->visibilityChanged(true);
    m_pageMap.value(m_lastIndex)->visibilityChanged(false);

    if (m_buttons.value(m_lastIndex))
        m_buttons.value(m_lastIndex)->setChecked(false);

    if (m_buttons.value(idx)) {
        if (OutputPanePlaceHolder::m_current)
            m_buttons.value(idx)->setChecked(OutputPanePlaceHolder::m_current->isVisible());
        else
            m_buttons.value(idx)->setChecked(false);
    }

    m_lastIndex = idx;
}

478
void OutputPaneManager::clearPage()
con's avatar
con committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
{
    if (m_pageMap.contains(m_outputWidgetPane->currentIndex()))
        m_pageMap.value(m_outputWidgetPane->currentIndex())->clearContents();
}


OutputPaneToggleButton::OutputPaneToggleButton(int number, const QString &text, QWidget *parent)
    : QPushButton(parent)
    , m_number(QString::number(number))
    , m_text(text)
{
    setFocusPolicy(Qt::NoFocus);
    setCheckable(true);
    setStyleSheet(
493
            "QPushButton { border-image: url(:/core/images/panel_button.png) 2 2 2 19;"
con's avatar
con committed
494
                         " border-width: 2px 2px 2px 19px; padding-left: -17; padding-right: 4 } "
495
            "QPushButton:checked { border-image: url(:/core/images/panel_button_checked.png) 2 2 2 19 } "
con's avatar
con committed
496
#ifndef Q_WS_MAC // Mac UI's dont usually do hover
497
498
499
            "QPushButton:checked:hover { border-image: url(:/core/images/panel_button_checked_hover.png) 2 2 2 19 } "
            "QPushButton:pressed:hover { border-image: url(:/core/images/panel_button_pressed.png) 2 2 2 19 } "
            "QPushButton:hover { border-image: url(:/core/images/panel_button_hover.png) 2 2 2 19 } "
con's avatar
con committed
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#endif
            );
}

QSize OutputPaneToggleButton::sizeHint() const
{
    ensurePolished();

    QSize s = fontMetrics().size(Qt::TextSingleLine, m_text);

    // Expand to account for border image set by stylesheet above
    s.rwidth() += 19 + 5 + 2;
    s.rheight() += 2 + 2;

    return s.expandedTo(QApplication::globalStrut());
}

void OutputPaneToggleButton::paintEvent(QPaintEvent *event)
{
    // For drawing the style sheet stuff
    QPushButton::paintEvent(event);

    const QFontMetrics fm = fontMetrics();
523
    const int baseLine = (height() - fm.height() + 1) / 2 + fm.ascent();
con's avatar
con committed
524
525
526
527
528
529
530
531
532
533
534
    const int numberWidth = fm.width(m_number);

    QPainter p(this);
    p.setFont(font());
    p.setPen(Qt::white);
    p.drawText((20 - numberWidth) / 2, baseLine, m_number);
    if (!isChecked())
        p.setPen(Qt::black);
    int leftPart = 22;
    p.drawText(leftPart, baseLine, fm.elidedText(m_text, Qt::ElideRight, width() - leftPart - 1));
}