2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Dan Meltzer <hydrogen@notyetimplemented.com>
|
|
|
|
* Copyright (C) 2008 by Alexis Ménard <darktears31@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tooltip_p.h"
|
|
|
|
#include "windowpreview_p.h"
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
#include <QAbstractTextDocumentLayout>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QBitmap>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPalette>
|
2008-11-14 01:52:29 +01:00
|
|
|
#include <QTextDocument>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QTimeLine>
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#include <QX11Info>
|
2008-11-04 03:39:56 +01:00
|
|
|
#include <netwm.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-04 03:39:56 +01:00
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kglobal.h>
|
|
|
|
#include <kglobalsettings.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include <plasma/plasma.h>
|
|
|
|
#include <plasma/theme.h>
|
|
|
|
#include <plasma/framesvg.h>
|
2009-10-31 13:33:08 +01:00
|
|
|
#include <plasma/windoweffects.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
2008-11-14 01:52:29 +01:00
|
|
|
class TipTextWidget : public QWidget
|
|
|
|
{
|
|
|
|
public:
|
2009-06-26 08:40:15 +02:00
|
|
|
TipTextWidget(ToolTip *parent)
|
2008-11-14 01:52:29 +01:00
|
|
|
: QWidget(parent),
|
2009-06-26 08:40:15 +02:00
|
|
|
m_toolTip(parent),
|
|
|
|
m_document(new QTextDocument(this))
|
2008-11-14 01:52:29 +01:00
|
|
|
{
|
|
|
|
//d->text->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
2008-11-14 07:06:15 +01:00
|
|
|
// QTextOption op;
|
|
|
|
// op.setWrapMode(QTextOption::WordWrap);
|
2009-06-26 08:40:15 +02:00
|
|
|
// m_document->setDefaultTextOption(op);
|
2008-11-14 01:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setStyleSheet(const QString &css)
|
|
|
|
{
|
2009-06-26 08:40:15 +02:00
|
|
|
m_document->setDefaultStyleSheet(css);
|
2008-11-14 01:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setContent(const ToolTipContent &data)
|
|
|
|
{
|
2009-03-26 23:08:36 +01:00
|
|
|
QString html;
|
|
|
|
if (!data.mainText().isEmpty()) {
|
|
|
|
html.append("<b>" + data.mainText() + "</b>");
|
|
|
|
|
|
|
|
if (!data.subText().isEmpty()) {
|
|
|
|
html.append("<br>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
html.append(data.subText());
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
m_anchor.clear();
|
|
|
|
m_document->clear();
|
|
|
|
data.registerResources(m_document);
|
|
|
|
m_document->setHtml("<p>" + html + "</p>");
|
|
|
|
m_document->adjustSize();
|
2008-11-14 07:06:15 +01:00
|
|
|
update();
|
2008-11-14 01:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize minimumSizeHint() const
|
|
|
|
{
|
2009-06-26 08:40:15 +02:00
|
|
|
return m_document->size().toSize();
|
2008-11-14 01:52:29 +01:00
|
|
|
}
|
2008-12-23 20:38:52 +01:00
|
|
|
|
2008-12-18 20:11:54 +01:00
|
|
|
QSize maximumSizeHint() const
|
|
|
|
{
|
|
|
|
return minimumSizeHint();
|
|
|
|
}
|
2008-11-14 01:52:29 +01:00
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QPainter p(this);
|
2009-06-26 08:40:15 +02:00
|
|
|
m_document->drawContents(&p, event->rect());
|
|
|
|
}
|
|
|
|
|
|
|
|
void mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractTextDocumentLayout *layout = m_document->documentLayout();
|
|
|
|
if (layout) {
|
|
|
|
m_anchor = layout->anchorAt(event->pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QAbstractTextDocumentLayout *layout = m_document->documentLayout();
|
|
|
|
if (layout) {
|
|
|
|
QString anchor = layout->anchorAt(event->pos());
|
|
|
|
if (anchor == m_anchor) {
|
|
|
|
m_toolTip->linkActivated(m_anchor, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_anchor.clear();
|
|
|
|
}
|
2008-11-14 01:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-06-26 08:40:15 +02:00
|
|
|
ToolTip *m_toolTip;
|
|
|
|
QTextDocument *m_document;
|
|
|
|
QString m_anchor;
|
2008-11-14 01:52:29 +01:00
|
|
|
};
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
class ToolTipPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ToolTipPrivate()
|
2008-11-14 01:52:29 +01:00
|
|
|
: text(0),
|
2008-11-04 00:08:39 +01:00
|
|
|
imageLabel(0),
|
|
|
|
preview(0),
|
|
|
|
timeline(0),
|
2008-12-26 21:38:59 +01:00
|
|
|
direction(Plasma::Up),
|
2008-11-04 00:08:39 +01:00
|
|
|
autohide(true)
|
|
|
|
{ }
|
|
|
|
|
2008-11-14 01:52:29 +01:00
|
|
|
TipTextWidget *text;
|
2008-11-04 00:08:39 +01:00
|
|
|
QLabel *imageLabel;
|
|
|
|
WindowPreview *preview;
|
|
|
|
FrameSvg *background;
|
2009-10-13 23:43:35 +02:00
|
|
|
QWeakPointer<QObject> source;
|
2008-11-04 00:08:39 +01:00
|
|
|
QTimeLine *timeline;
|
|
|
|
QPoint to;
|
|
|
|
QPoint from;
|
2008-12-26 21:38:59 +01:00
|
|
|
Plasma::Direction direction;
|
2008-11-04 00:08:39 +01:00
|
|
|
bool autohide;
|
|
|
|
};
|
|
|
|
|
|
|
|
ToolTip::ToolTip(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
d(new ToolTipPrivate())
|
|
|
|
{
|
2009-02-16 20:24:05 +01:00
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
2008-11-04 00:08:39 +01:00
|
|
|
setWindowFlags(Qt::ToolTip);
|
|
|
|
d->preview = new WindowPreview(this);
|
2008-11-14 01:52:29 +01:00
|
|
|
d->text = new TipTextWidget(this);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->imageLabel = new QLabel(this);
|
|
|
|
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
|
|
|
|
|
|
|
d->background = new FrameSvg(this);
|
|
|
|
d->background->setImagePath("widgets/tooltip");
|
|
|
|
d->background->setEnabledBorders(FrameSvg::AllBorders);
|
|
|
|
updateTheme();
|
|
|
|
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(updateTheme()));
|
2009-06-26 08:40:15 +02:00
|
|
|
connect(d->preview, SIGNAL(windowPreviewClicked(WId,Qt::MouseButtons,Qt::KeyboardModifiers,QPoint)),
|
|
|
|
this, SIGNAL(activateWindowByWId(WId,Qt::MouseButtons,Qt::KeyboardModifiers,QPoint)));
|
2009-07-08 15:17:55 +02:00
|
|
|
|
|
|
|
QHBoxLayout *previewHBoxLayout = new QHBoxLayout;
|
|
|
|
previewHBoxLayout->addWidget(d->preview);
|
|
|
|
|
|
|
|
QHBoxLayout *iconTextHBoxLayout = new QHBoxLayout;
|
|
|
|
iconTextHBoxLayout->addWidget(d->imageLabel);
|
|
|
|
iconTextHBoxLayout->setAlignment(d->imageLabel, Qt::AlignCenter);
|
|
|
|
iconTextHBoxLayout->addWidget(d->text);
|
|
|
|
iconTextHBoxLayout->setAlignment(d->text, Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
iconTextHBoxLayout->setStretchFactor(d->text, 1);
|
|
|
|
|
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
|
|
|
mainLayout->addLayout(previewHBoxLayout);
|
|
|
|
mainLayout->addLayout(iconTextHBoxLayout);
|
|
|
|
|
|
|
|
setLayout(mainLayout);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTip::~ToolTip()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
void ToolTip::showEvent(QShowEvent *e)
|
|
|
|
{
|
|
|
|
checkSize();
|
|
|
|
QWidget::showEvent(e);
|
|
|
|
d->preview->setInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::hideEvent(QHideEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::hideEvent(e);
|
|
|
|
if (d->source) {
|
2009-10-13 23:43:35 +02:00
|
|
|
QMetaObject::invokeMethod(d->source.data(), "toolTipHidden");
|
2009-06-26 08:40:15 +02:00
|
|
|
}
|
2009-10-31 13:33:08 +01:00
|
|
|
WindowEffects::highlightWindows(winId(), QList<WId>());
|
2009-06-26 08:40:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (rect().contains(event->pos()) &&
|
|
|
|
(!d->preview || !d->preview->geometry().contains(event->pos()))) {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::enterEvent(QEvent *)
|
|
|
|
{
|
|
|
|
emit hovered(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::leaveEvent(QEvent *)
|
|
|
|
{
|
|
|
|
emit hovered(false);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void ToolTip::checkSize()
|
|
|
|
{
|
2008-12-05 23:48:58 +01:00
|
|
|
//FIXME: layout bugs even on qlayouts? oh, please, no.
|
2009-01-15 06:59:07 +01:00
|
|
|
d->text->setMinimumSize(0, 0);
|
|
|
|
d->text->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
|
2008-12-05 23:48:58 +01:00
|
|
|
d->text->setMinimumSize(d->text->minimumSizeHint());
|
2008-12-18 20:11:54 +01:00
|
|
|
d->text->setMaximumSize(d->text->maximumSizeHint());
|
2009-01-06 16:48:54 +01:00
|
|
|
|
|
|
|
adjustSize();
|
2009-04-17 18:55:42 +02:00
|
|
|
}
|
2008-12-26 21:38:59 +01:00
|
|
|
|
2009-04-17 18:55:42 +02:00
|
|
|
void ToolTip::adjustPosition(const QSize &previous, const QSize ¤t)
|
|
|
|
{
|
|
|
|
if (previous != current) {
|
2008-12-26 21:38:59 +01:00
|
|
|
//offsets to stop tooltips from jumping when they resize
|
2008-12-23 20:38:52 +01:00
|
|
|
int deltaX = 0;
|
|
|
|
int deltaY = 0;
|
2008-12-26 21:38:59 +01:00
|
|
|
if (d->direction == Plasma::Up) {
|
2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
kDebug() << "resizing from" << current << "to" << hint
|
|
|
|
<< "and moving from" << pos() << "to"
|
|
|
|
<< x() << y() + (current.height() - hint.height())
|
|
|
|
<< current.height() - hint.height();
|
|
|
|
*/
|
2009-01-06 16:48:54 +01:00
|
|
|
deltaY = previous.height() - current.height();
|
2008-12-26 21:38:59 +01:00
|
|
|
} else if (d->direction == Plasma::Left) {
|
2008-12-23 20:38:52 +01:00
|
|
|
/*
|
|
|
|
kDebug() << "vertical resizing from" << current << "to" << hint
|
|
|
|
<< "and moving from" << pos() << "to"
|
|
|
|
<< x() + (current.width() - hint.width()) << y()
|
|
|
|
<< current.width() - hint.width(); */
|
2009-01-06 16:48:54 +01:00
|
|
|
deltaX = previous.width() - current.width();
|
2008-12-23 20:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// resize then move if we're getting smaller, vice versa when getting bigger
|
|
|
|
// this prevents overlap with the item in the smaller case, and a repaint of
|
|
|
|
// the tipped item when getting bigger
|
|
|
|
|
|
|
|
move(x() + deltaX, y() + deltaY);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-11 00:30:19 +01:00
|
|
|
void ToolTip::setContent(QObject *tipper, const ToolTipContent &data)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
//reset our size
|
2008-11-14 01:52:29 +01:00
|
|
|
d->text->setContent(data);
|
2008-11-04 00:08:39 +01:00
|
|
|
d->imageLabel->setPixmap(data.image());
|
2009-10-31 13:33:08 +01:00
|
|
|
|
|
|
|
if (data.windowsToPreview().size() > 1 || data.windowToPreview() != 0) {
|
2009-11-11 16:18:52 +01:00
|
|
|
WindowEffects::highlightWindows(winId(), QList<WId>()<<winId()<<data.windowsToPreview());
|
2009-10-31 13:33:08 +01:00
|
|
|
}
|
2009-03-19 23:15:29 +01:00
|
|
|
if (data.windowsToPreview().size() > 1) {
|
|
|
|
d->preview->setWindowIds(data.windowsToPreview());
|
|
|
|
} else {
|
|
|
|
QList<WId>ids;
|
|
|
|
ids.append(data.windowToPreview());
|
|
|
|
d->preview->setWindowIds(ids);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
d->autohide = data.autohide();
|
2008-11-11 00:30:19 +01:00
|
|
|
d->source = tipper;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
if (isVisible()) {
|
|
|
|
d->preview->setInfo();
|
|
|
|
//kDebug() << "about to check size";
|
|
|
|
checkSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-30 03:04:35 +01:00
|
|
|
void ToolTip::prepareShowing()
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
if (!d->preview->isEmpty()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
// show/hide the preview area
|
|
|
|
d->preview->show();
|
|
|
|
} else {
|
|
|
|
d->preview->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
layout()->activate();
|
|
|
|
d->preview->setInfo();
|
|
|
|
//kDebug() << "about to check size";
|
|
|
|
checkSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::moveTo(const QPoint &to)
|
|
|
|
{
|
|
|
|
if (!isVisible() ||
|
|
|
|
!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
|
|
|
|
move(to);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->from = QPoint();
|
|
|
|
d->to = to;
|
|
|
|
|
|
|
|
if (!d->timeline) {
|
|
|
|
d->timeline = new QTimeLine(250, this);
|
|
|
|
d->timeline->setFrameRange(0, 10);
|
|
|
|
d->timeline->setCurveShape(QTimeLine::EaseInCurve);
|
|
|
|
connect(d->timeline, SIGNAL(valueChanged(qreal)), this, SLOT(animateMove(qreal)));
|
|
|
|
}
|
|
|
|
|
|
|
|
d->timeline->stop();
|
|
|
|
d->timeline->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::animateMove(qreal progress)
|
|
|
|
{
|
|
|
|
if (d->from.isNull()) {
|
|
|
|
d->from = pos();
|
|
|
|
}
|
|
|
|
|
2009-01-01 20:17:53 +01:00
|
|
|
if (qFuzzyCompare(progress, qreal(1.0))) {
|
2008-11-04 00:08:39 +01:00
|
|
|
move(d->to);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
move(d->from.x() + ((d->to.x() - d->from.x()) * progress),
|
|
|
|
d->from.y() + ((d->to.y() - d->from.y()) * progress));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(e);
|
|
|
|
d->background->resizeFrame(size());
|
|
|
|
setMask(d->background->mask());
|
2008-12-13 19:17:36 +01:00
|
|
|
d->preview->setInfo();
|
2009-04-17 18:55:42 +02:00
|
|
|
|
|
|
|
if (isVisible()) {
|
|
|
|
adjustPosition(e->oldSize(), e->size());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setClipRect(e->rect());
|
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
painter.fillRect(rect(), Qt::transparent);
|
|
|
|
|
|
|
|
d->background->paintFrame(&painter);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTip::autohide() const
|
|
|
|
{
|
|
|
|
return d->autohide;
|
|
|
|
}
|
|
|
|
|
2008-12-26 21:38:59 +01:00
|
|
|
void ToolTip::setDirection(Plasma::Direction direction)
|
2008-12-23 20:38:52 +01:00
|
|
|
{
|
2008-12-26 21:38:59 +01:00
|
|
|
d->direction = direction;
|
2008-12-23 20:38:52 +01:00
|
|
|
}
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
void ToolTip::linkActivated(const QString &anchor, QMouseEvent *event)
|
|
|
|
{
|
|
|
|
emit linkActivated(anchor, event->buttons(), event->modifiers(), event->globalPos());
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void ToolTip::updateTheme()
|
|
|
|
{
|
|
|
|
const int topHeight = d->background->marginSize(Plasma::TopMargin);
|
|
|
|
const int leftWidth = d->background->marginSize(Plasma::LeftMargin);
|
|
|
|
const int rightWidth = d->background->marginSize(Plasma::RightMargin);
|
|
|
|
const int bottomHeight = d->background->marginSize(Plasma::BottomMargin);
|
|
|
|
setContentsMargins(leftWidth, topHeight, rightWidth, bottomHeight);
|
|
|
|
|
|
|
|
// Make the tooltip use Plasma's colorscheme
|
2008-11-14 01:52:29 +01:00
|
|
|
QColor textColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
|
2008-11-04 00:08:39 +01:00
|
|
|
QPalette plasmaPalette = QPalette();
|
|
|
|
plasmaPalette.setColor(QPalette::Window,
|
|
|
|
Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
|
2008-11-14 01:52:29 +01:00
|
|
|
plasmaPalette.setColor(QPalette::WindowText, textColor);
|
2008-11-04 00:08:39 +01:00
|
|
|
setAutoFillBackground(true);
|
|
|
|
setPalette(plasmaPalette);
|
2008-11-14 01:52:29 +01:00
|
|
|
d->text->setStyleSheet(QString("p { color: %1; }").arg(textColor.name()));
|
2008-11-04 00:08:39 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "tooltip_p.moc"
|