Files
qt-creator/src/plugins/coreplugin/outputpane.cpp

219 lines
6.7 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
2010-12-17 16:01:08 +01:00
**
****************************************************************************/
2008-12-02 14:09:21 +01:00
#include "imode.h"
#include "modemanager.h"
2008-12-02 12:01:29 +01:00
#include "outputpane.h"
2010-09-16 12:26:28 +02:00
#include "outputpanemanager.h"
#include <QResizeEvent>
#include <QSplitter>
#include <QVBoxLayout>
2008-12-02 12:01:29 +01:00
2010-09-16 12:26:28 +02:00
namespace Core {
class OutputPanePlaceHolderPrivate {
public:
explicit OutputPanePlaceHolderPrivate(Id mode, QSplitter *parent);
2009-07-16 16:30:00 +02:00
Id m_mode;
2010-09-16 12:26:28 +02:00
QSplitter *m_splitter;
int m_nonMaximizedSize = 0;
bool m_isMaximized = false;
bool m_initialized = false;
2010-09-16 12:26:28 +02:00
static OutputPanePlaceHolder* m_current;
};
OutputPanePlaceHolderPrivate::OutputPanePlaceHolderPrivate(Id mode, QSplitter *parent) :
m_mode(mode), m_splitter(parent)
2010-09-16 12:26:28 +02:00
{
}
2008-12-02 12:01:29 +01:00
OutputPanePlaceHolder *OutputPanePlaceHolderPrivate::m_current = nullptr;
2008-12-02 12:01:29 +01:00
OutputPanePlaceHolder::OutputPanePlaceHolder(Id mode, QSplitter *parent)
2010-09-16 12:26:28 +02:00
: QWidget(parent), d(new OutputPanePlaceHolderPrivate(mode, parent))
2008-12-02 12:01:29 +01:00
{
setVisible(false);
setLayout(new QVBoxLayout);
QSizePolicy sp;
sp.setHorizontalPolicy(QSizePolicy::Preferred);
sp.setVerticalPolicy(QSizePolicy::Preferred);
sp.setHorizontalStretch(0);
setSizePolicy(sp);
layout()->setContentsMargins(0, 0, 0, 0);
connect(ModeManager::instance(), &ModeManager::currentModeChanged,
this, &OutputPanePlaceHolder::currentModeChanged);
// if this is part of a lazily created mode widget,
// we need to check if this is the current placeholder
currentModeChanged(ModeManager::currentModeId());
2008-12-02 12:01:29 +01:00
}
OutputPanePlaceHolder::~OutputPanePlaceHolder()
{
2010-09-16 12:26:28 +02:00
if (d->m_current == this) {
if (Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance()) {
om->setParent(nullptr);
om->hide();
}
OutputPanePlaceHolderPrivate::m_current = nullptr;
2008-12-02 12:01:29 +01:00
}
delete d;
2008-12-02 12:01:29 +01:00
}
void OutputPanePlaceHolder::currentModeChanged(Id mode)
2008-12-02 12:01:29 +01:00
{
2010-09-16 12:26:28 +02:00
if (d->m_current == this) {
d->m_current = nullptr;
if (d->m_initialized)
Internal::OutputPaneManager::setOutputPaneHeightSetting(d->m_nonMaximizedSize);
2010-09-16 12:26:28 +02:00
Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance();
om->hide();
om->setParent(nullptr);
2010-09-16 12:26:28 +02:00
om->updateStatusButtons(false);
2008-12-02 12:01:29 +01:00
}
2010-09-16 12:26:28 +02:00
if (d->m_mode == mode) {
if (d->m_current && d->m_current->d->m_initialized)
Internal::OutputPaneManager::setOutputPaneHeightSetting(d->m_current->d->m_nonMaximizedSize);
2010-09-16 12:26:28 +02:00
d->m_current = this;
Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance();
layout()->addWidget(om);
om->show();
om->updateStatusButtons(isVisible());
Internal::OutputPaneManager::updateMaximizeButton(d->m_isMaximized);
2008-12-02 12:01:29 +01:00
}
}
void OutputPanePlaceHolder::setMaximized(bool maximize)
{
if (d->m_isMaximized == maximize)
return;
2010-09-16 12:26:28 +02:00
if (!d->m_splitter)
return;
2010-09-16 12:26:28 +02:00
int idx = d->m_splitter->indexOf(this);
if (idx < 0)
return;
d->m_isMaximized = maximize;
if (d->m_current == this)
Internal::OutputPaneManager::updateMaximizeButton(d->m_isMaximized);
2010-09-16 12:26:28 +02:00
QList<int> sizes = d->m_splitter->sizes();
if (maximize) {
d->m_nonMaximizedSize = sizes[idx];
int sum = 0;
foreach (int s, sizes)
sum += s;
for (int i = 0; i < sizes.count(); ++i) {
sizes[i] = 32;
}
sizes[idx] = sum - (sizes.count()-1) * 32;
} else {
int target = d->m_nonMaximizedSize > 0 ? d->m_nonMaximizedSize : sizeHint().height();
int space = sizes[idx] - target;
if (space > 0) {
for (int i = 0; i < sizes.count(); ++i) {
sizes[i] += space / (sizes.count()-1);
}
sizes[idx] = target;
}
}
2010-09-16 12:26:28 +02:00
d->m_splitter->setSizes(sizes);
}
bool OutputPanePlaceHolder::isMaximized() const
{
return d->m_isMaximized;
}
void OutputPanePlaceHolder::setHeight(int height)
{
if (height == 0)
return;
if (!d->m_splitter)
return;
const int idx = d->m_splitter->indexOf(this);
if (idx < 0)
return;
d->m_splitter->refresh();
QList<int> sizes = d->m_splitter->sizes();
const int difference = height - sizes.at(idx);
if (difference == 0)
return;
const int adaption = difference / (sizes.count()-1);
for (int i = 0; i < sizes.count(); ++i) {
sizes[i] -= adaption;
}
sizes[idx] = height;
d->m_splitter->setSizes(sizes);
}
void OutputPanePlaceHolder::ensureSizeHintAsMinimum()
{
Internal::OutputPaneManager *om = Internal::OutputPaneManager::instance();
int minimum = (d->m_splitter->orientation() == Qt::Vertical
? om->sizeHint().height() : om->sizeHint().width());
if (nonMaximizedSize() < minimum && !d->m_isMaximized)
setHeight(minimum);
}
int OutputPanePlaceHolder::nonMaximizedSize() const
{
if (!d->m_initialized)
return Internal::OutputPaneManager::outputPaneHeightSetting();
return d->m_nonMaximizedSize;
}
void OutputPanePlaceHolder::resizeEvent(QResizeEvent *event)
2008-12-02 12:01:29 +01:00
{
if (d->m_isMaximized || event->size().height() == 0)
return;
d->m_nonMaximizedSize = event->size().height();
2008-12-02 12:01:29 +01:00
}
void OutputPanePlaceHolder::showEvent(QShowEvent *)
2008-12-02 12:01:29 +01:00
{
if (!d->m_initialized) {
d->m_initialized = true;
setHeight(Internal::OutputPaneManager::outputPaneHeightSetting());
}
}
OutputPanePlaceHolder *OutputPanePlaceHolder::getCurrent()
{
return OutputPanePlaceHolderPrivate::m_current;
2008-12-02 12:01:29 +01:00
}
2010-09-16 12:26:28 +02:00
bool OutputPanePlaceHolder::isCurrentVisible()
2008-12-02 12:01:29 +01:00
{
2010-09-16 12:26:28 +02:00
return OutputPanePlaceHolderPrivate::m_current && OutputPanePlaceHolderPrivate::m_current->isVisible();
2008-12-02 12:01:29 +01:00
}
2010-09-16 12:26:28 +02:00
} // namespace Core
2008-12-02 12:01:29 +01:00