Commit 882e34ee authored by Oswald Buddenhagen's avatar Oswald Buddenhagen

rewrite editor info bar handling

the info about the bars is now stored in the IFile, not in the
EditorView. this is somewhat more expensive for the bars which
identically apply to all editors of one type, but fixes consistency
issues between views.

additionally, it is now possible to set several simultaneous
info bars per file, which ensures that no information is lost.

Co-authored-by: mae
parent 46c09e77
......@@ -37,6 +37,7 @@
#include "cmakeprojectconstants.h"
#include "cmakeproject.h"
#include <coreplugin/infobar.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <texteditor/fontsettings.h>
......@@ -77,11 +78,10 @@ QString CMakeEditor::id() const
void CMakeEditor::markAsChanged()
{
Core::EditorManager::instance()->
showEditorInfoBar(QLatin1String("CMakeEditor.RunCMake"),
tr("Changes to cmake files are shown in the project tree after building."),
tr("Build now"),
this, SLOT(build()));
Core::InfoBarEntry info(QLatin1String("CMakeEditor.RunCMake"),
tr("Changes to cmake files are shown in the project tree after building."));
info.setCustomButtonInfo(tr("Build now"), this, SLOT(build()));
file()->infoBar()->addInfo(info);
}
void CMakeEditor::build()
......
......@@ -51,6 +51,7 @@
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
#include <coreplugin/icore.h>
#include <coreplugin/infobar.h>
#include <coreplugin/editormanager/editormanager.h>
#include <QtCore/QMap>
......@@ -195,7 +196,9 @@ bool CMakeProject::parseCMakeLists()
!activeTarget()->activeBuildConfiguration())
return false;
Core::EditorManager::instance()->hideEditorInfoBar("CMakeEditor.RunCMake");
foreach (Core::IEditor *editor, Core::EditorManager::instance()->openedEditors())
if (isProjectFile(editor->file()->fileName()))
editor->file()->infoBar()->removeInfo(QLatin1String("CMakeEditor.RunCMake"));
// Find cbp file
CMakeBuildConfiguration *activeBC = activeTarget()->activeBuildConfiguration();
......
......@@ -71,6 +71,7 @@ SOURCES += mainwindow.cpp \
mimedatabase.cpp \
icore.cpp \
ifile.cpp \
infobar.cpp \
editormanager/ieditor.cpp \
dialogs/ioptionspage.cpp \
dialogs/iwizard.cpp \
......@@ -139,6 +140,7 @@ HEADERS += mainwindow.h \
icontext.h \
icore.h \
ifile.h \
infobar.h \
ifilefactory.h \
imode.h \
ioutputpane.h \
......
......@@ -53,6 +53,7 @@
#include <coreplugin/editormanager/ieditorfactory.h>
#include <coreplugin/editormanager/iexternaleditor.h>
#include <coreplugin/icorelistener.h>
#include <coreplugin/infobar.h>
#include <coreplugin/imode.h>
#include <coreplugin/settingsdatabase.h>
#include <coreplugin/variablemanager.h>
......@@ -1573,13 +1574,18 @@ void EditorManager::updateActions()
#ifdef Q_WS_MAC
window()->setWindowModified(curEditor->file()->isModified());
#endif
if (curEditor->file()->isModified() && curEditor->file()->isReadOnly()) {
// we are about to change a read-only file, warn user
showEditorInfoBar(QLatin1String("Core.EditorManager.MakeWritable"),
tr("<b>Warning:</b> You are changing a read-only file."),
tr("Make writable"), this, SLOT(makeCurrentEditorWritable()));
} else {
hideEditorInfoBar(QLatin1String("Core.EditorManager.MakeWritable"));
bool ww = curEditor->file()->isModified() && curEditor->file()->isReadOnly();
if (ww != curEditor->file()->hasWriteWarning()) {
curEditor->file()->setWriteWarning(ww);
if (ww) {
// we are about to change a read-only file, warn user
InfoBarEntry info(QLatin1String("Core.EditorManager.MakeWritable"),
tr("<b>Warning:</b> You are changing a read-only file."));
info.setCustomButtonInfo(tr("Make writable"), this, SLOT(makeCurrentEditorWritable()));
curEditor->file()->infoBar()->addInfo(info);
} else {
curEditor->file()->infoBar()->removeInfo(QLatin1String("Core.EditorManager.MakeWritable"));
}
}
#ifdef Q_WS_MAC
} else { // curEditor
......@@ -1843,23 +1849,6 @@ void EditorManager::revertToSaved()
QMessageBox::critical(m_d->m_core->mainWindow(), tr("File Error"), errorString);
}
void EditorManager::showEditorInfoBar(const QString &id,
const QString &infoText,
const QString &buttonText,
QObject *object, const char *buttonPressMember,
const char *cancelButtonPressMember)
{
currentEditorView()->showEditorInfoBar(id, infoText, buttonText, object, buttonPressMember, cancelButtonPressMember);
}
void EditorManager::hideEditorInfoBar(const QString &id)
{
Core::Internal::EditorView *cev = currentEditorView();
if (cev)
cev->hideEditorInfoBar(id);
}
void EditorManager::showEditorStatusBar(const QString &id,
const QString &infoText,
const QString &buttonText,
......
......@@ -165,14 +165,6 @@ public:
Internal::OpenEditorsWindow *windowPopup() const;
void showPopupOrSelectDocument() const;
void showEditorInfoBar(const QString &id,
const QString &infoText,
const QString &buttonText = QString(),
QObject *object = 0, const char *buttonPressMember = 0,
const char *cancelButtonPressMember = 0);
void hideEditorInfoBar(const QString &id);
void showEditorStatusBar(const QString &id,
const QString &infoText,
const QString &buttonText = QString(),
......
......@@ -38,6 +38,7 @@
#include <coreplugin/editortoolbar.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/infobar.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/ieditor.h>
......@@ -79,8 +80,7 @@ EditorView::EditorView(QWidget *parent) :
QWidget(parent),
m_toolBar(EditorManager::createToolBar(this)),
m_container(new QStackedWidget(this)),
m_infoWidget(new QFrame(this)),
m_editorForInfoWidget(0),
m_infoBarDisplay(new InfoBarDisplay(this)),
m_statusHLine(new QFrame(this)),
m_statusWidget(new QFrame(this)),
m_currentNavigationHistoryPosition(0)
......@@ -95,36 +95,8 @@ EditorView::EditorView(QWidget *parent) :
connect(m_toolBar, SIGNAL(listSelectionActivated(int)), this, SLOT(listSelectionActivated(int)));
tl->addWidget(m_toolBar);
}
{
QPalette pal = m_infoWidget->palette();
pal.setColor(QPalette::Window, QColor(255, 255, 225));
pal.setColor(QPalette::WindowText, Qt::black);
m_infoWidget->setPalette(pal);
m_infoWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
m_infoWidget->setLineWidth(1);
m_infoWidget->setAutoFillBackground(true);
QHBoxLayout *hbox = new QHBoxLayout(m_infoWidget);
hbox->setMargin(2);
m_infoWidgetLabel = new QLabel("Placeholder");
m_infoWidgetLabel->setWordWrap(true);
hbox->addWidget(m_infoWidgetLabel);
m_infoWidgetButton = new QToolButton;
m_infoWidgetButton->setText(tr("Placeholder"));
hbox->addWidget(m_infoWidgetButton);
m_infoWidgetCloseButton = new QToolButton;
m_infoWidgetCloseButton->setAutoRaise(true);
m_infoWidgetCloseButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_CLEAR)));
m_infoWidgetCloseButton->setToolTip(tr("Close"));
hbox->addWidget(m_infoWidgetCloseButton);
m_infoWidget->setVisible(false);
tl->addWidget(m_infoWidget);
}
m_infoBarDisplay->setTarget(tl, 1);
tl->addWidget(m_container);
......@@ -169,40 +141,6 @@ void EditorView::closeView()
if (editor)
em->closeEditor(editor);
}
void EditorView::showEditorInfoBar(const QString &id,
const QString &infoText,
const QString &buttonText,
QObject *object, const char *buttonPressMember,
const char *cancelButtonPressMember)
{
m_infoWidgetId = id;
m_infoWidgetLabel->setText(infoText);
m_infoWidgetButton->setText(buttonText);
if (object && !buttonText.isEmpty()) {
m_infoWidgetButton->show();
} else {
m_infoWidgetButton->hide();
}
m_infoWidgetButton->disconnect();
if (object && buttonPressMember)
connect(m_infoWidgetButton, SIGNAL(clicked()), object, buttonPressMember);
m_infoWidgetCloseButton->disconnect();
if (object && cancelButtonPressMember)
connect(m_infoWidgetCloseButton, SIGNAL(clicked()), object, cancelButtonPressMember);
connect(m_infoWidgetCloseButton, SIGNAL(clicked()), m_infoWidget, SLOT(hide()));
m_infoWidget->setVisible(true);
m_editorForInfoWidget = currentEditor();
}
void EditorView::hideEditorInfoBar(const QString &id)
{
if (id == m_infoWidgetId)
m_infoWidget->setVisible(false);
}
void EditorView::showEditorStatusBar(const QString &id,
const QString &infoText,
......@@ -284,15 +222,10 @@ void EditorView::listSelectionActivated(int index)
void EditorView::setCurrentEditor(IEditor *editor)
{
// FIXME: this keeps the editor hidden if switching from A to B and back
if (editor != m_editorForInfoWidget) {
m_infoWidget->hide();
m_editorForInfoWidget = 0;
}
if (!editor || m_container->count() <= 0
|| m_container->indexOf(editor->widget()) == -1) {
m_toolBar->updateEditorStatus(0);
m_infoBarDisplay->setInfoBar(0);
// ### TODO the combo box m_editorList should show an empty item
return;
}
......@@ -306,6 +239,8 @@ void EditorView::setCurrentEditor(IEditor *editor)
m_toolBar->setCurrentEditor(editor);
updateEditorHistory(editor);
m_infoBarDisplay->setInfoBar(editor->file()->infoBar());
}
int EditorView::editorCount() const
......
......@@ -55,6 +55,7 @@ namespace Core {
class IContext;
class IFile;
class IEditor;
class InfoBarDisplay;
class OpenEditorsModel;
class EditorToolBar;
......@@ -84,12 +85,6 @@ public:
bool hasEditor(IEditor *editor) const;
QList<IEditor *> editors() const;
void showEditorInfoBar(const QString &id,
const QString &infoText,
const QString &buttonText,
QObject *object, const char *buttonPressMember,
const char *cancelButtonPressMember = 0);
void hideEditorInfoBar(const QString &id);
void showEditorStatusBar(const QString &id,
const QString &infoText,
......@@ -109,12 +104,7 @@ private:
EditorToolBar *m_toolBar;
QStackedWidget *m_container;
QString m_infoWidgetId;
QFrame *m_infoWidget;
QLabel *m_infoWidgetLabel;
QToolButton *m_infoWidgetButton;
QToolButton *m_infoWidgetCloseButton;
IEditor *m_editorForInfoWidget;
InfoBarDisplay *m_infoBarDisplay;
QString m_statusWidgetId;
QFrame *m_statusHLine;
QFrame *m_statusWidget;
......
......@@ -32,14 +32,17 @@
#include "ifile.h"
#include "infobar.h"
namespace Core {
IFile::IFile(QObject *parent) : QObject(parent)
IFile::IFile(QObject *parent) : QObject(parent), m_infoBar(0), m_hasWriteWarning(false)
{
}
IFile::~IFile()
{
delete m_infoBar;
}
IFile::ReloadBehavior IFile::reloadBehavior(ChangeTrigger state, ChangeType type) const
......@@ -55,4 +58,11 @@ void IFile::checkPermissions()
{
}
InfoBar *IFile::infoBar()
{
if (!m_infoBar)
m_infoBar = new InfoBar;
return m_infoBar;
}
} // namespace Core
......@@ -39,6 +39,7 @@
namespace Core {
class MimeType;
class InfoBar;
class CORE_EXPORT IFile : public QObject
{
......@@ -100,11 +101,20 @@ public:
virtual void checkPermissions();
bool hasWriteWarning() const { return m_hasWriteWarning; }
void setWriteWarning(bool has) { m_hasWriteWarning = has; }
InfoBar *infoBar();
signals:
void changed();
void aboutToReload();
void reloaded();
private:
InfoBar *m_infoBar;
bool m_hasWriteWarning;
};
} // namespace Core
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#include "infobar.h"
#include <coreplugin/coreconstants.h>
#include <QtGui/QFrame>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QToolButton>
#include <QtCore/QVariant>
namespace Core {
InfoBarEntry::InfoBarEntry(const QString &_id, const QString &_infoText)
: id(_id)
, infoText(_infoText)
, object(0)
, buttonPressMember(0)
, cancelObject(0)
, cancelButtonPressMember(0)
{
}
void InfoBarEntry::setCustomButtonInfo(const QString &_buttonText, QObject *_object, const char *_member)
{
buttonText = _buttonText;
object = _object;
buttonPressMember = _member;
}
void InfoBarEntry::setCancelButtonInfo(QObject *_object, const char *_member)
{
cancelObject = _object;
cancelButtonPressMember = _member;
}
void InfoBar::addInfo(const InfoBarEntry &info)
{
m_infoBarEntries << info;
emit changed();
}
void InfoBar::removeInfo(const QString &id)
{
QMutableListIterator<InfoBarEntry> it(m_infoBarEntries);
while (it.hasNext())
if (it.next().id == id) {
it.remove();
emit changed();
return;
}
}
void InfoBar::clear()
{
if (!m_infoBarEntries.isEmpty()) {
m_infoBarEntries.clear();
emit changed();
}
}
InfoBarDisplay::InfoBarDisplay(QObject *parent)
: QObject(parent)
, m_infoBar(0)
, m_boxLayout(0)
, m_boxIndex(0)
{
}
void InfoBarDisplay::setTarget(QBoxLayout *layout, int index)
{
m_boxLayout = layout;
m_boxIndex = index;
}
void InfoBarDisplay::setInfoBar(InfoBar *infoBar)
{
if (m_infoBar == infoBar)
return;
if (m_infoBar)
m_infoBar->disconnect(this);
m_infoBar = infoBar;
if (m_infoBar) {
connect(infoBar, SIGNAL(changed()), SLOT(update()));
connect(infoBar, SIGNAL(destroyed()), SLOT(infoBarDestroyed()));
}
update();
}
void InfoBarDisplay::infoBarDestroyed()
{
m_infoBar = 0;
// Calling update() here causes a complicated crash on shutdown.
// So instead we rely on the view now being either destroyed (in which case it
// will delete the widgets itself) or setInfoBar() being called explicitly.
}
void InfoBarDisplay::update()
{
foreach (QWidget *widget, m_infoWidgets) {
widget->disconnect(this); // We want no destroyed() signal now
delete widget;
}
m_infoWidgets.clear();
if (!m_infoBar)
return;
foreach (const InfoBarEntry &info, m_infoBar->m_infoBarEntries) {
QFrame *infoWidget = new QFrame;
QPalette pal = infoWidget->palette();
pal.setColor(QPalette::Window, QColor(255, 255, 225));
pal.setColor(QPalette::WindowText, Qt::black);
infoWidget->setPalette(pal);
infoWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
infoWidget->setLineWidth(1);
infoWidget->setAutoFillBackground(true);
QHBoxLayout *hbox = new QHBoxLayout(infoWidget);
hbox->setMargin(2);
QLabel *infoWidgetLabel = new QLabel(info.infoText);
infoWidgetLabel->setWordWrap(true);
hbox->addWidget(infoWidgetLabel);
if (!info.buttonText.isEmpty()) {
QToolButton *infoWidgetButton = new QToolButton;
infoWidgetButton->setText(info.buttonText);
connect(infoWidgetButton, SIGNAL(clicked()), info.object, info.buttonPressMember);
hbox->addWidget(infoWidgetButton);
}
QToolButton *infoWidgetCloseButton = new QToolButton;
infoWidgetCloseButton->setAutoRaise(true);
infoWidgetCloseButton->setIcon(QIcon(QLatin1String(Core::Constants::ICON_CLEAR)));
infoWidgetCloseButton->setToolTip(tr("Close"));
infoWidgetCloseButton->setProperty("infoId", info.id);
connect(infoWidgetCloseButton, SIGNAL(clicked()), SLOT(cancelButtonClicked()));
if (info.cancelObject)
connect(infoWidgetCloseButton, SIGNAL(clicked()),
info.cancelObject, info.cancelButtonPressMember);
hbox->addWidget(infoWidgetCloseButton);
connect(infoWidget, SIGNAL(destroyed()), SLOT(widgetDestroyed()));
m_boxLayout->insertWidget(m_boxIndex, infoWidget);
m_infoWidgets << infoWidget;
}
}
void InfoBarDisplay::widgetDestroyed()
{
// This means that the parent is being deleted
m_infoWidgets.clear();
}
void InfoBarDisplay::cancelButtonClicked()
{
m_infoBar->removeInfo(sender()->property("infoId").toString());
}
} // namespace Core
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** 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, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/
#ifndef INFOBAR_H
#define INFOBAR_H
#include "core_global.h"
#include <QtCore/QObject>
QT_BEGIN_NAMESPACE
class QBoxLayout;
QT_END_NAMESPACE
namespace Core {
class InfoBar;
class InfoBarDisplay;
class CORE_EXPORT InfoBarEntry
{
public:
InfoBarEntry(const QString &_id, const QString &_infoText);
InfoBarEntry(const InfoBarEntry &other) { *this = other; }
void setCustomButtonInfo(const QString &_buttonText, QObject *_object, const char *_member);
void setCancelButtonInfo(QObject *_object, const char *_member);
private:
QString id;
QString infoText;
QString buttonText;
QObject *object;
const char *buttonPressMember;
QObject *cancelObject;
const char *cancelButtonPressMember;
friend class InfoBar;
friend class InfoBarDisplay;
};
class CORE_EXPORT InfoBar : public QObject
{
Q_OBJECT
public:
void addInfo(const InfoBarEntry &info);
void removeInfo(const QString &id);
void clear();
signals:
void changed();