Commit 14ffff31 authored by Leandro Melo's avatar Leandro Melo
Browse files

Better flexibility and extensibility in text editor tooltip's internals.

parent 42d15fcf
......@@ -41,6 +41,7 @@
#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/tooltip/tooltip.h>
#include <texteditor/tooltip/tipcontents.h>
using namespace Core;
using namespace QmlJS;
......@@ -235,12 +236,16 @@ void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint
TextEditor::ToolTip::instance()->hide();
else {
if (m_colorTip.isValid()) {
TextEditor::ToolTip::instance()->showColor(point, m_colorTip, editor->widget());
TextEditor::ToolTip::instance()->show(point,
TextEditor::ColorContent(m_colorTip),
editor->widget());
} else {
if (matchingHelpCandidate() != -1)
addF1ToToolTip();
TextEditor::ToolTip::instance()->showText(point, toolTip(), editor->widget());
TextEditor::ToolTip::instance()->show(point,
TextEditor::TextContent(toolTip()),
editor->widget());
}
}
}
......
......@@ -32,6 +32,7 @@
#include "basetexteditor.h"
#include "displaysettings.h"
#include "tooltip.h"
#include "tipcontents.h"
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
......@@ -167,7 +168,7 @@ void BaseHoverHandler::operateTooltip(ITextEditor *editor, const QPoint &point)
if (m_matchingHelpCandidate != -1)
addF1ToToolTip();
TextEditor::ToolTip::instance()->showText(point, m_toolTip, editor->widget());
ToolTip::instance()->show(point, TextContent(m_toolTip), editor->widget());
}
}
......
......@@ -40,6 +40,8 @@
#include "texteditorconstants.h"
#include "texteditorplugin.h"
#include "syntaxhighlighter.h"
#include "tooltip.h"
#include "tipcontents.h"
#include <aggregation/aggregate.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -79,7 +81,6 @@
#include <QtGui/QTextBlock>
#include <QtGui/QTextLayout>
#include <QtGui/QToolBar>
#include <QtGui/QToolTip>
#include <QtGui/QInputDialog>
#include <QtGui/QMenu>
......@@ -1087,7 +1088,7 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
Locker inKeyPressEvent(&d->m_inKeyPressEvent);
viewport()->setCursor(Qt::BlankCursor);
QToolTip::hideText();
ToolTip::instance()->hide();
d->m_moveLineUndoHack = false;
d->clearVisibleFoldedBlock();
......@@ -2041,7 +2042,10 @@ bool BaseTextEditor::viewportEvent(QEvent *event)
RefactorMarker refactorMarker = d->m_refactorOverlay->markerAt(pos);
if (refactorMarker.isValid() && !refactorMarker.tooltip.isEmpty()) {
QToolTip::showText(he->globalPos(), refactorMarker.tooltip, viewport(), refactorMarker.rect);
ToolTip::instance()->show(he->globalPos(),
TextContent(refactorMarker.tooltip),
viewport(),
refactorMarker.rect);
return true;
}
......
......@@ -67,7 +67,8 @@ SOURCES += texteditorplugin.cpp \
tooltip/tooltip.cpp \
tooltip/tips.cpp \
tooltip/tipcontents.cpp \
basehoverhandler.cpp
basehoverhandler.cpp \
tooltip/tipfactory.cpp
HEADERS += texteditorplugin.h \
textfilewizard.h \
......@@ -136,7 +137,9 @@ HEADERS += texteditorplugin.h \
tooltip/tooltip.h \
tooltip/tips.h \
tooltip/tipcontents.h \
basehoverhandler.h
basehoverhandler.h \
tooltip/effects.h \
tooltip/tipfactory.h
FORMS += behaviorsettingspage.ui \
displaysettingspage.ui \
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef EFFECTS_H
#define EFFECTS_H
#include <QtCore/QtGlobal>
// This is a copy of a Qt private header. Please read comments in tooltip.h.
QT_BEGIN_NAMESPACE
class QWidget;
struct QEffects
{
enum Direction {
LeftScroll = 0x0001,
RightScroll = 0x0002,
UpScroll = 0x0004,
DownScroll = 0x0008
};
typedef uint DirFlags;
};
extern void Q_GUI_EXPORT qScrollEffect(QWidget*, QEffects::DirFlags dir = QEffects::DownScroll, int time = -1);
extern void Q_GUI_EXPORT qFadeEffect(QWidget*, int time = -1);
QT_END_NAMESPACE
#endif // EFFECTS_H
......@@ -29,8 +29,9 @@
#include "tipcontents.h"
#include <QtCore/QtGlobal>
using namespace TextEditor;
using namespace Internal;
TipContent::TipContent()
{}
......@@ -38,39 +39,82 @@ TipContent::TipContent()
TipContent::~TipContent()
{}
QColorContent::QColorContent(const QColor &color) : m_color(color)
ColorContent::ColorContent(const QColor &color) : m_color(color)
{}
QColorContent::~QColorContent()
ColorContent::~ColorContent()
{}
bool TipContent::equals(const QColorContent *colorContent) const
TipContent *ColorContent::clone() const
{
Q_UNUSED(colorContent)
return false;
return new ColorContent(*this);
}
bool QColorContent::isValid() const
int ColorContent::typeId() const
{
return COLOR_CONTENT_ID;
}
bool ColorContent::isValid() const
{
return m_color.isValid();
}
int QColorContent::showTime() const
int ColorContent::showTime() const
{
return 4000;
}
bool QColorContent::equals(const TipContent *tipContent) const
bool ColorContent::equals(const TipContent &tipContent) const
{
return tipContent->equals(this);
if (typeId() == tipContent.typeId()) {
if (m_color == static_cast<const ColorContent &>(tipContent).m_color)
return true;
}
return false;
}
bool QColorContent::equals(const QColorContent *colorContent) const
const QColor &ColorContent::color() const
{
return m_color == colorContent->color();
return m_color;
}
const QColor &QColorContent::color() const
TextContent::TextContent(const QString &text) : m_text(text)
{}
TextContent::~TextContent()
{}
TipContent *TextContent::clone() const
{
return m_color;
return new TextContent(*this);
}
int TextContent::typeId() const
{
return TEXT_CONTENT_ID;
}
bool TextContent::isValid() const
{
return !m_text.isEmpty();
}
int TextContent::showTime() const
{
return 10000 + 40 * qMax(0, m_text.length() - 100);
}
bool TextContent::equals(const TipContent &tipContent) const
{
if (typeId() == tipContent.typeId()) {
if (m_text == static_cast<const TextContent &>(tipContent).m_text)
return true;
}
return false;
}
const QString &TextContent::text() const
{
return m_text;
}
......@@ -30,43 +30,68 @@
#ifndef TIPCONTENTS_H
#define TIPCONTENTS_H
#include "texteditor/texteditor_global.h"
#include <QtCore/QString>
#include <QtGui/QColor>
namespace TextEditor {
namespace Internal {
class QColorContent;
class TipContent
class TEXTEDITOR_EXPORT TipContent
{
public:
protected:
TipContent();
public:
virtual ~TipContent();
virtual TipContent *clone() const = 0;
virtual int typeId() const = 0;
virtual bool isValid() const = 0;
virtual int showTime() const = 0;
virtual bool equals(const TipContent *tipContent) const = 0;
virtual bool equals(const QColorContent *colorContent) const;
virtual bool equals(const TipContent &tipContent) const = 0;
};
class QColorContent : public TipContent
class TEXTEDITOR_EXPORT ColorContent : public TipContent
{
public:
QColorContent(const QColor &color);
virtual ~QColorContent();
ColorContent(const QColor &color);
virtual ~ColorContent();
virtual TipContent *clone() const;
virtual int typeId() const;
virtual bool isValid() const;
virtual int showTime() const;
virtual bool equals(const TipContent *tipContent) const;
virtual bool equals(const QColorContent *colorContent) const;
virtual bool equals(const TipContent &tipContent) const;
const QColor &color() const;
static const int COLOR_CONTENT_ID = 0;
private:
QColor m_color;
};
} // namespace Internal
class TEXTEDITOR_EXPORT TextContent : public TipContent
{
public:
TextContent(const QString &text);
virtual ~TextContent();
virtual TipContent *clone() const;
virtual int typeId() const;
virtual bool isValid() const;
virtual int showTime() const;
virtual bool equals(const TipContent &tipContent) const;
const QString &text() const;
static const int TEXT_CONTENT_ID = 1;
private:
QString m_text;
};
} // namespace TextEditor
#endif // TIPCONTENTS_H
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "tipfactory.h"
#include "tipcontents.h"
#include "tips.h"
using namespace TextEditor;
using namespace Internal;
TipFactory::TipFactory()
{}
TipFactory::~TipFactory()
{}
QTipLabel *TipFactory::createTip(const TipContent &content, QWidget *w)
{
QTipLabel *tip = 0;
if (content.typeId() == TextContent::TEXT_CONTENT_ID)
tip = new TextTip(w);
else if (content.typeId() == ColorContent::COLOR_CONTENT_ID)
tip = new ColorTip(w);
if (tip)
tip->setContent(content);
return tip;
}
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef TIPFACTORY_H
#define TIPFACTORY_H
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class QTipLabel;
QT_END_NAMESPACE
namespace TextEditor {
class TipContent;
namespace Internal {
class TipFactory
{
public:
TipFactory();
virtual ~TipFactory();
Q_DISABLE_COPY(TipFactory)
virtual QTipLabel *createTip(const TipContent &content, QWidget *w);
};
} // namespace Internal
} // namespace TextEditor
#endif // TIPFACTORY_H
......@@ -33,7 +33,13 @@
#include <QtCore/QRect>
#include <QtGui/QColor>
#include <QtGui/QPainter>
#include <QtGui/QPen>
#include <QtGui/QPixmap>
#include <QtGui/QStyle>
#include <QtGui/QFontMetrics>
#include <QtGui/QTextDocument>
#include <QtGui/QStylePainter>
#include <QtGui/QStyleOptionFrame>
using namespace TextEditor;
using namespace Internal;
......@@ -53,31 +59,36 @@ namespace {
}
}
Tip::Tip(QWidget *parent) : QFrame(parent)
{
setWindowFlags(Qt::ToolTip);
setAutoFillBackground(true);
ensurePolished();
}
QT_BEGIN_NAMESPACE
Tip::~Tip()
QTipLabel::QTipLabel(QWidget *parent) :
QLabel(parent, Qt::ToolTip | Qt::BypassGraphicsProxyWidget),
m_tipContent(0)
{}
void Tip::setContent(const QSharedPointer<TipContent> &content)
QTipLabel::~QTipLabel()
{
m_content = content;
if (m_tipContent)
delete m_tipContent;
}
void QTipLabel::setContent(const TipContent &content)
{
if (m_tipContent)
delete m_tipContent;
m_tipContent = content.clone();
configure();
}
const QSharedPointer<TipContent> &Tip::content() const
{ return m_content; }
const TipContent &QTipLabel::content() const
{ return *m_tipContent; }
QT_END_NAMESPACE
ColorTip::ColorTip(QWidget *parent) : Tip(parent)
ColorTip::ColorTip(QWidget *parent) : QTipLabel(parent)
{
setFrameStyle(QFrame::Box);
resize(QSize(40, 40));
m_tilePixMap = tilePixMap(9);
m_tilePixMap = tilePixMap(10);
}
ColorTip::~ColorTip()
......@@ -88,13 +99,87 @@ void ColorTip::configure()
update();
}
bool ColorTip::handleContentReplacement(const TipContent &content) const
{
if (content.typeId() == ColorContent::COLOR_CONTENT_ID)
return true;
return false;
}
void ColorTip::paintEvent(QPaintEvent *event)
{
const QColor &color = static_cast<const ColorContent &>(content()).color();
QPen pen;
pen.setWidth(1);
if (color.value() > 100)
pen.setColor(color.darker());
else
pen.setColor(color.lighter());
QPainter painter(this);
QRect r(1, 1, width() - 2, height() - 2);
painter.setPen(pen);
painter.setBrush(color);
QRect r(1, 1, rect().width() - 2, rect().height() - 2);
painter.drawTiledPixmap(r, m_tilePixMap);
painter.setBrush(static_cast<QColorContent *>(content().data())->color());
painter.drawRect(rect());
painter.drawRect(r);
QLabel::paintEvent(event);
}
TextTip::TextTip(QWidget *parent) : QTipLabel(parent)
{
setForegroundRole(QPalette::ToolTipText);
setBackgroundRole(QPalette::ToolTipBase);
ensurePolished();
setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, 0, this));
setFrameStyle(QFrame::NoFrame);
setAlignment(Qt::AlignLeft);
setIndent(1);
setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, 0, this) / 255.0);
}
TextTip::~TextTip()
{}
void TextTip::configure()
{
const QString &text = static_cast<const TextContent &>(content()).text();
setWordWrap(Qt::mightBeRichText(text));
setText(text);
QFontMetrics fm(font());
QSize extra(1, 0);
// Make it look good with the default ToolTip font on Mac, which has a small descent.
if (fm.descent() == 2 && fm.ascent() >= 11)
++extra.rheight();
resize(sizeHint() + extra);
}
bool TextTip::handleContentReplacement(const TipContent &content) const
{
if (content.typeId() == TextContent::TEXT_CONTENT_ID)
return true;
return false;
}
void TextTip::paintEvent(QPaintEvent *event)
{
QStylePainter p(this);
QStyleOptionFrame opt;
opt.init(this);
p.drawPrimitive(QStyle::PE_PanelTipLabel, opt);
p.end();