allow custom image, html and css resources in tooltips
svn path=/trunk/KDE/kdelibs/; revision=884026
This commit is contained in:
parent
a387ff3a29
commit
0bc65a788d
@ -27,6 +27,7 @@
|
|||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPalette>
|
#include <QPalette>
|
||||||
|
#include <QTextDocument>
|
||||||
#include <QTimeLine>
|
#include <QTimeLine>
|
||||||
#ifdef Q_WS_X11
|
#ifdef Q_WS_X11
|
||||||
#include <QX11Info>
|
#include <QX11Info>
|
||||||
@ -43,11 +44,52 @@
|
|||||||
|
|
||||||
namespace Plasma {
|
namespace Plasma {
|
||||||
|
|
||||||
|
class TipTextWidget : public QWidget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
TipTextWidget(QWidget *parent)
|
||||||
|
: QWidget(parent),
|
||||||
|
document(new QTextDocument(this))
|
||||||
|
{
|
||||||
|
//d->text->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
||||||
|
QTextOption op;
|
||||||
|
op.setWrapMode(QTextOption::WordWrap);
|
||||||
|
document->setDefaultTextOption(op);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setStyleSheet(const QString &css)
|
||||||
|
{
|
||||||
|
document->setDefaultStyleSheet(css);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setContent(const ToolTipContent &data)
|
||||||
|
{
|
||||||
|
document->clear();
|
||||||
|
data.registerResources(document);
|
||||||
|
document->setHtml("<p><b>" + data.mainText() + "</b><br>" + data.subText() + "</p>");
|
||||||
|
document->adjustSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize minimumSizeHint() const
|
||||||
|
{
|
||||||
|
return document->size().toSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void paintEvent(QPaintEvent *event)
|
||||||
|
{
|
||||||
|
QPainter p(this);
|
||||||
|
document->drawContents(&p, event->rect());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QTextDocument *document;
|
||||||
|
};
|
||||||
|
|
||||||
class ToolTipPrivate
|
class ToolTipPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ToolTipPrivate()
|
ToolTipPrivate()
|
||||||
: label(0),
|
: text(0),
|
||||||
imageLabel(0),
|
imageLabel(0),
|
||||||
preview(0),
|
preview(0),
|
||||||
source(0),
|
source(0),
|
||||||
@ -55,7 +97,7 @@ class ToolTipPrivate
|
|||||||
autohide(true)
|
autohide(true)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
QLabel *label;
|
TipTextWidget *text;
|
||||||
QLabel *imageLabel;
|
QLabel *imageLabel;
|
||||||
WindowPreview *preview;
|
WindowPreview *preview;
|
||||||
FrameSvg *background;
|
FrameSvg *background;
|
||||||
@ -94,9 +136,7 @@ ToolTip::ToolTip(QWidget *parent)
|
|||||||
setWindowFlags(Qt::ToolTip);
|
setWindowFlags(Qt::ToolTip);
|
||||||
QGridLayout *l = new QGridLayout;
|
QGridLayout *l = new QGridLayout;
|
||||||
d->preview = new WindowPreview(this);
|
d->preview = new WindowPreview(this);
|
||||||
d->label = new QLabel(this);
|
d->text = new TipTextWidget(this);
|
||||||
d->label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
||||||
d->label->setWordWrap(true);
|
|
||||||
d->imageLabel = new QLabel(this);
|
d->imageLabel = new QLabel(this);
|
||||||
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
||||||
|
|
||||||
@ -108,7 +148,7 @@ ToolTip::ToolTip(QWidget *parent)
|
|||||||
|
|
||||||
l->addWidget(d->preview, 0, 0, 1, 2);
|
l->addWidget(d->preview, 0, 0, 1, 2);
|
||||||
l->addWidget(d->imageLabel, 1, 0);
|
l->addWidget(d->imageLabel, 1, 0);
|
||||||
l->addWidget(d->label, 1, 1);
|
l->addWidget(d->text, 1, 1);
|
||||||
setLayout(l);
|
setLayout(l);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +189,7 @@ void ToolTip::checkSize()
|
|||||||
void ToolTip::setContent(QObject *tipper, const ToolTipContent &data)
|
void ToolTip::setContent(QObject *tipper, const ToolTipContent &data)
|
||||||
{
|
{
|
||||||
//reset our size
|
//reset our size
|
||||||
d->label->setText("<qt><b>" + data.mainText() + "</b><br>" + data.subText() + "</qt>");
|
d->text->setContent(data);
|
||||||
d->imageLabel->setPixmap(data.image());
|
d->imageLabel->setPixmap(data.image());
|
||||||
d->preview->setWindowId(data.windowToPreview());
|
d->preview->setWindowId(data.windowToPreview());
|
||||||
d->autohide = data.autohide();
|
d->autohide = data.autohide();
|
||||||
@ -250,13 +290,14 @@ void ToolTip::updateTheme()
|
|||||||
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
||||||
|
|
||||||
// Make the tooltip use Plasma's colorscheme
|
// Make the tooltip use Plasma's colorscheme
|
||||||
|
QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
|
||||||
QPalette plasmaPalette = QPalette();
|
QPalette plasmaPalette = QPalette();
|
||||||
plasmaPalette.setColor(QPalette::Window,
|
plasmaPalette.setColor(QPalette::Window,
|
||||||
Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
|
Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
|
||||||
plasmaPalette.setColor(QPalette::WindowText,
|
plasmaPalette.setColor(QPalette::WindowText, textColor);
|
||||||
Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
|
|
||||||
setAutoFillBackground(true);
|
setAutoFillBackground(true);
|
||||||
setPalette(plasmaPalette);
|
setPalette(plasmaPalette);
|
||||||
|
d->text->setStyleSheet(QString("p { color: %1; }").arg(textColor.name()));
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,11 +19,29 @@
|
|||||||
|
|
||||||
#include "tooltipcontent.h"
|
#include "tooltipcontent.h"
|
||||||
|
|
||||||
|
#include <QHash>
|
||||||
|
#include <QTextDocument>
|
||||||
|
|
||||||
#include <kiconloader.h>
|
#include <kiconloader.h>
|
||||||
|
|
||||||
namespace Plasma
|
namespace Plasma
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct ToolTipResource
|
||||||
|
{
|
||||||
|
ToolTipResource()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ToolTipResource(ToolTipContent::ResourceType t, const QVariant &v)
|
||||||
|
: type(t),
|
||||||
|
data(v)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ToolTipContent::ResourceType type;
|
||||||
|
QVariant data;
|
||||||
|
};
|
||||||
class ToolTipContentPrivate
|
class ToolTipContentPrivate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -37,6 +55,7 @@ public:
|
|||||||
QString subText;
|
QString subText;
|
||||||
QPixmap image;
|
QPixmap image;
|
||||||
WId windowToPreview;
|
WId windowToPreview;
|
||||||
|
QHash<QString, ToolTipResource> resources;
|
||||||
bool autohide;
|
bool autohide;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -144,6 +163,39 @@ bool ToolTipContent::autohide() const
|
|||||||
return d->autohide;
|
return d->autohide;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ToolTipContent::addResource(ResourceType type, const QUrl &path, const QVariant &resource)
|
||||||
|
{
|
||||||
|
d->resources.insert(path.toString(), ToolTipResource(type, resource));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToolTipContent::registerResources(QTextDocument *document) const
|
||||||
|
{
|
||||||
|
if (!document) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QHashIterator<QString, ToolTipResource> it(d->resources);
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next();
|
||||||
|
const ToolTipResource &r = it.value();
|
||||||
|
QTextDocument::ResourceType t;
|
||||||
|
|
||||||
|
switch (r.type) {
|
||||||
|
case ImageResource:
|
||||||
|
t = QTextDocument::ImageResource;
|
||||||
|
break;
|
||||||
|
case HtmlResource:
|
||||||
|
t = QTextDocument::HtmlResource;
|
||||||
|
break;
|
||||||
|
case CssResource:
|
||||||
|
t = QTextDocument::StyleSheetResource;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
document->addResource(t, it.key(), r.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Plasma
|
} // namespace Plasma
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,11 +21,15 @@
|
|||||||
#define PLASMA_TOOLTIPCONTENT_H
|
#define PLASMA_TOOLTIPCONTENT_H
|
||||||
|
|
||||||
#include <QtCore/QString>
|
#include <QtCore/QString>
|
||||||
|
#include <QtCore/QUrl>
|
||||||
|
#include <QtCore/QVariant>
|
||||||
#include <QtGui/QPixmap>
|
#include <QtGui/QPixmap>
|
||||||
#include <QtGui/QIcon>
|
#include <QtGui/QIcon>
|
||||||
|
|
||||||
#include <plasma/plasma_export.h>
|
#include <plasma/plasma_export.h>
|
||||||
|
|
||||||
|
class QTextDocument;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This provides the content for a tooltip.
|
* This provides the content for a tooltip.
|
||||||
*
|
*
|
||||||
@ -41,6 +45,8 @@ class ToolTipContentPrivate;
|
|||||||
class PLASMA_EXPORT ToolTipContent
|
class PLASMA_EXPORT ToolTipContent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum ResourceType { ImageResource = 0, HtmlResource, CssResource };
|
||||||
|
|
||||||
/** Creates an empty Content */
|
/** Creates an empty Content */
|
||||||
ToolTipContent();
|
ToolTipContent();
|
||||||
|
|
||||||
@ -97,6 +103,13 @@ public:
|
|||||||
/** Whether or not to autohide the tooltip, defaults to true */
|
/** Whether or not to autohide the tooltip, defaults to true */
|
||||||
bool autohide() const;
|
bool autohide() const;
|
||||||
|
|
||||||
|
/** Adds a resource that can then be referenced from the text elements
|
||||||
|
using rich text **/
|
||||||
|
void addResource(ResourceType type, const QUrl &path, const QVariant &resource);
|
||||||
|
|
||||||
|
/** Registers all resources with a given document */
|
||||||
|
void registerResources(QTextDocument *document) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ToolTipContentPrivate * const d;
|
ToolTipContentPrivate * const d;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user