Commit f72c259d authored by mae's avatar mae
Browse files

the group is no longer necessary

parent 1b3e3fb7
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** 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
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** 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
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include "editorgroup.h"
#include "editormanager.h"
#include <coreplugin/coreconstants.h>
#include <utils/qtcassert.h>
#include <QtCore/QDir>
#include <QtCore/QDebug>
#include <QtGui/QPainter>
#include <QtGui/QStyle>
#include <QtGui/QStyleOption>
#ifdef Q_WS_MAC
#include <QtGui/QMacStyle>
#endif
Q_DECLARE_METATYPE(Core::IEditor*)
using namespace Core;
using namespace Core::Internal;
namespace Core {
namespace Internal {
class EditorList;
}
}
QDataStream &operator<<(QDataStream &out, const Core::Internal::EditorList &list);
QDataStream &operator>>(QDataStream &in, Core::Internal::EditorList &list);
namespace Core {
namespace Internal {
class EditorList
{
public:
quint32 currentEditorIndex;
void append(Core::IEditor *editor);
QString fileNameAt(int index);
QString editorKindAt(int index);
int count();
private:
QList<QPair<QString,QString> > editorinfo;
friend QDataStream &::operator<<(QDataStream &out, const EditorList &list);
friend QDataStream &::operator>>(QDataStream &in, EditorList &list);
};
} // namespace Internal
} // namespace Core
//================EditorGroupContext===============
EditorGroupContext::EditorGroupContext(EditorGroup *editorGroup)
: IContext(editorGroup),
m_context(QList<int>() << Constants::C_GLOBAL_ID),
m_editorGroup(editorGroup)
{
}
QList<int> EditorGroupContext::context() const
{
return m_context;
}
QWidget *EditorGroupContext::widget()
{
return m_editorGroup;
}
EditorGroup *EditorGroupContext::editorGroup()
{
return m_editorGroup;
}
//================EditorGroup=================
EditorGroup::EditorGroup(QWidget *parent)
: QFrame(parent),
m_contextObject(new EditorGroupContext(this))
{
setFocusPolicy(Qt::StrongFocus);
m_model = new EditorModel(this);
}
QSize EditorGroup::minimumSizeHint() const
{
return QSize(10, 10);
}
void EditorGroup::focusInEvent(QFocusEvent *)
{
update();
}
void EditorGroup::focusOutEvent(QFocusEvent *)
{
update();
}
void EditorGroup::paintEvent(QPaintEvent *e)
{
QFrame::paintEvent(e);
if (editorCount() == 0) {
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
}
}
}
void EditorGroup::moveEditorsFromGroup(EditorGroup *group)
{
foreach (IEditor *editor, group->editors()) {
group->removeEditor(editor);
addEditor(editor);
}
}
void EditorGroup::moveEditorFromGroup(EditorGroup *group, IEditor *editor)
{
group->removeEditor(editor);
addEditor(editor);
}
QByteArray EditorGroup::saveState() const
{
QByteArray bytes;
QDataStream stream(&bytes, QIODevice::WriteOnly);
EditorList editorinfo;
IEditor *curr = currentEditor();
QList<IEditor *> editors = editorsInNaturalOrder();
for (int j = 0; j < editors.count(); ++j) {
IEditor *editor = editors.at(j);
if (editor == curr)
editorinfo.currentEditorIndex = j;
editorinfo.append(editor);
}
stream << editorinfo;
return bytes;
}
bool EditorGroup::restoreState(const QByteArray &state)
{
QDataStream in(state);
EditorManager *em = EditorManager::instance();
EditorList editors;
in >> editors;
int savedIndex = editors.currentEditorIndex;
if (savedIndex >= 0 && savedIndex < editors.count())
em->restoreEditor(editors.fileNameAt(savedIndex), editors.editorKindAt(savedIndex), this);
for (int j = 0; j < editors.count(); ++j) {
if (j == savedIndex)
continue;
em->restoreEditor(editors.fileNameAt(j), editors.editorKindAt(j), this);
}
return true;
}
void EditorGroup::addEditor(IEditor *editor)
{
m_model->addEditor(editor);
}
void EditorGroup::insertEditor(int i, IEditor *editor)
{
m_model->insertEditor(i, editor);
}
void EditorGroup::removeEditor(IEditor *editor)
{
m_model->removeEditor(editor);
}
void EditorGroup::showEditorInfoBar(const QString &, const QString &, const QString &, QObject *, const char *)
{
}
void EditorGroup::hideEditorInfoBar(const QString &)
{
}
void EditorList::append(IEditor *editor)
{
if (editor->file()->fileName().isEmpty())
return;
editorinfo << qMakePair(editor->file()->fileName(), QString(editor->kind()));
}
QDataStream &operator<<(QDataStream &out, const EditorList &list)
{
//todo: versioning
out << list.currentEditorIndex << list.editorinfo;
return out;
}
QDataStream &operator>>(QDataStream &in, EditorList &list)
{
//todo: versioning
in >> list.currentEditorIndex;
in >> list.editorinfo;
return in;
}
QString EditorList::fileNameAt(int index)
{
return editorinfo.at(index).first;
}
QString EditorList::editorKindAt(int index)
{
return editorinfo.at(index).second;
}
int EditorList::count()
{
return editorinfo.count();
}
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** 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
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** 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
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#ifndef EDITORGROUP_H
#define EDITORGROUP_H
#include <coreplugin/icontext.h>
#include <QtCore/QEvent>
#include <QtCore/QAbstractListModel>
#include <QtGui/QFrame>
#include <QtGui/QAbstractButton>
#include <QtGui/QPen>
namespace Core {
class IEditor;
class EditorGroup;
namespace Internal {
class EditorModel;
// Also used by the EditorManager
class EditorGroupContext : public IContext
{
Q_OBJECT
public:
EditorGroupContext(EditorGroup *editorGroup);
EditorGroup *editorGroup();
// IContext
QList<int> context() const;
QWidget *widget();
private:
QList<int> m_context;
EditorGroup *m_editorGroup;
};
} // namespace Internal
class CORE_EXPORT EditorGroup : public QFrame
{
Q_OBJECT
public:
EditorGroup(QWidget *parent);
virtual ~EditorGroup() {};
virtual IContext *contextObject() { return m_contextObject; }
virtual QWidget *widget() { return this; }
virtual int editorCount() const = 0;
virtual void addEditor(IEditor *editor);
virtual void insertEditor(int i, IEditor *editor);
virtual void removeEditor(IEditor *editor);
virtual QList<IEditor*> editors() const = 0;
virtual IEditor *currentEditor() const = 0;
virtual void setCurrentEditor(IEditor *editor) = 0;
virtual void moveEditorsFromGroup(EditorGroup *group);
virtual void moveEditorFromGroup(EditorGroup *group, IEditor *editor);
virtual QByteArray saveState() const;
virtual bool restoreState(const QByteArray &state);
virtual void showEditorInfoBar(const QString &kind,
const QString &infoText,
const QString &buttonText,
QObject *object, const char *member);
virtual void hideEditorInfoBar(const QString &kind);
QSize minimumSizeHint() const;
void focusInEvent(QFocusEvent *e);
void focusOutEvent(QFocusEvent *e);
void paintEvent(QPaintEvent *e);
signals:
void closeRequested(Core::IEditor *editor);
void editorRemoved(Core::IEditor *editor);
void editorAdded(Core::IEditor *editor);
protected:
virtual QList<IEditor *> editorsInNaturalOrder() const { return editors(); }
Internal::EditorModel *model() const { return m_model; }
private:
Internal::EditorGroupContext *m_contextObject;
Internal::EditorModel *m_model;
};
namespace Internal {
} // namespace Internal
} // namespace Core
#endif // EDITORGROUP_H
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** 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
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** 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
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
#include "stackededitorgroup.h"
#include "editormanager.h"
#include "editorview.h"
#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;
StackedEditorGroup::StackedEditorGroup(QWidget *parent) :
EditorGroup(parent),
m_toplevel(new QWidget),
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)),
m_editorForInfoWidget(0)
{
QVBoxLayout *tl = new QVBoxLayout(m_toplevel);
tl->setSpacing(0);
tl->setMargin(0);
{
m_editorList->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_editorList->setSizeAdjustPolicy(QComboBox::AdjustToContents);
m_editorList->setMinimumContentsLength(20);
m_proxyModel.setSourceModel(model());
m_proxyModel.sort(0);
m_editorList->setModel(&m_proxyModel);
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);
connect(m_editorList, SIGNAL(currentIndexChanged(int)), this, SLOT(listSelectionChanged(int)));
connect(m_lockButton, SIGNAL(clicked()), this, SLOT(makeEditorWritable()));
connect(m_closeButton, SIGNAL(clicked()), this, SLOT(sendCloseRequest()));
}
{
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);
}
tl->addWidget(m_container);
QHBoxLayout *l = new QHBoxLayout;
l->setSpacing(0);
l->setMargin(0);
l->addWidget(m_toplevel);
setLayout(l);
}
void StackedEditorGroup::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);