2008-07-16 17:17:44 +02: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.
|
|
|
|
*/
|
2008-07-30 22:35:42 +02:00
|
|
|
|
2008-08-23 18:17:04 +02:00
|
|
|
#include "tooltip_p.h"
|
|
|
|
#include "windowpreview_p.h"
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
#include <QBitmap>
|
2008-08-23 18:17:04 +02:00
|
|
|
#include <QGridLayout>
|
2008-07-16 17:17:44 +02:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QMouseEvent>
|
2008-08-23 18:17:04 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QPalette>
|
2008-11-02 08:25:56 +01:00
|
|
|
#include <QTimeLine>
|
2008-11-03 05:52:34 +01:00
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#include <QX11Info>
|
|
|
|
#include <NETRootInfo>
|
|
|
|
#endif
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
#include <KGlobal>
|
2008-11-02 08:25:56 +01:00
|
|
|
#include <KGlobalSettings>
|
2008-08-23 18:17:04 +02:00
|
|
|
|
|
|
|
#include <plasma/plasma.h>
|
2008-07-16 17:17:44 +02:00
|
|
|
#include <plasma/theme.h>
|
2008-10-29 18:46:33 +01:00
|
|
|
#include <plasma/framesvg.h>
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
class ToolTipPrivate
|
|
|
|
{
|
|
|
|
public:
|
2008-11-02 07:55:10 +01:00
|
|
|
ToolTipPrivate()
|
2008-10-12 01:28:20 +02:00
|
|
|
: label(0),
|
|
|
|
imageLabel(0),
|
|
|
|
preview(0),
|
2008-11-02 07:55:10 +01:00
|
|
|
source(0),
|
2008-11-02 08:25:56 +01:00
|
|
|
timeline(0),
|
2008-10-26 17:55:54 +01:00
|
|
|
autohide(true)
|
2008-07-16 17:17:44 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
QLabel *label;
|
|
|
|
QLabel *imageLabel;
|
|
|
|
WindowPreview *preview;
|
2008-10-29 18:46:33 +01:00
|
|
|
FrameSvg *background;
|
2008-10-22 14:54:04 +02:00
|
|
|
QPointer<QObject> source;
|
2008-11-02 08:25:56 +01:00
|
|
|
QTimeLine *timeline;
|
|
|
|
QPoint to;
|
|
|
|
QPoint from;
|
2008-10-26 17:55:54 +01:00
|
|
|
bool autohide;
|
2008-07-16 17:17:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void ToolTip::showEvent(QShowEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::showEvent(e);
|
|
|
|
d->preview->setInfo();
|
|
|
|
}
|
|
|
|
|
2008-07-17 01:27:34 +02:00
|
|
|
void ToolTip::hideEvent(QHideEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::hideEvent(e);
|
|
|
|
if (d->source) {
|
2008-09-18 23:53:52 +02:00
|
|
|
QMetaObject::invokeMethod(d->source, "toolTipHidden");
|
2008-07-17 01:27:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 01:28:20 +02:00
|
|
|
void ToolTip::mouseReleaseEvent(QMouseEvent *event)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
|
|
|
if (rect().contains(event->pos())) {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-02 07:55:10 +01:00
|
|
|
ToolTip::ToolTip(QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
d(new ToolTipPrivate())
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
|
|
|
setWindowFlags(Qt::ToolTip);
|
|
|
|
QGridLayout *l = new QGridLayout;
|
2008-08-23 18:17:04 +02:00
|
|
|
d->preview = new WindowPreview(this);
|
|
|
|
d->label = new QLabel(this);
|
2008-07-16 17:17:44 +02:00
|
|
|
d->label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
|
|
d->label->setWordWrap(true);
|
2008-08-23 18:17:04 +02:00
|
|
|
d->imageLabel = new QLabel(this);
|
2008-07-16 17:17:44 +02:00
|
|
|
d->imageLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
d->background = new FrameSvg(this);
|
2008-11-02 07:55:10 +01:00
|
|
|
d->background->setImagePath("widgets/tooltip");
|
|
|
|
d->background->setEnabledBorders(FrameSvg::AllBorders);
|
|
|
|
updateTheme();
|
|
|
|
connect(d->background, SIGNAL(repaintNeeded()), this, SLOT(updateTheme()));
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
l->addWidget(d->preview, 0, 0, 1, 2);
|
|
|
|
l->addWidget(d->imageLabel, 1, 0);
|
|
|
|
l->addWidget(d->label, 1, 1);
|
|
|
|
setLayout(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTip::~ToolTip()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-11-03 06:03:36 +01:00
|
|
|
void ToolTip::checkSize()
|
|
|
|
{
|
|
|
|
QSize hint = sizeHint();
|
|
|
|
QSize current = size();
|
|
|
|
|
|
|
|
if (hint != current) {
|
|
|
|
/*
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
NETRootInfo i(QX11Info::display(), 0);
|
|
|
|
int flags = NET::BottomLeft;
|
|
|
|
i.moveResizeWindowRequest(winId(), flags,
|
|
|
|
x(), y() + (current.height() - hint.height()),
|
|
|
|
hint.width(), hint.height());
|
|
|
|
#else
|
|
|
|
move(x(), y() + (current.height() - hint.height()));
|
|
|
|
resize(hint);
|
|
|
|
#endif
|
|
|
|
*/
|
2008-11-03 07:14:26 +01:00
|
|
|
/*
|
|
|
|
kDebug() << "resizing from" << current << "to" << hint
|
|
|
|
<< "and moving from" << pos() << "to"
|
|
|
|
<< x() << y() + (current.height() - hint.height())
|
|
|
|
<< current.height() - hint.height();
|
|
|
|
*/
|
2008-11-03 06:03:36 +01:00
|
|
|
resize(hint);
|
2008-11-03 07:14:26 +01:00
|
|
|
move(x(), y() + (current.height() - hint.height()));
|
2008-11-03 06:03:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-02 06:58:42 +01:00
|
|
|
void ToolTip::setContent(const ToolTipContent &data)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
|
|
|
//reset our size
|
2008-11-02 06:58:42 +01:00
|
|
|
d->label->setText("<qt><b>" + data.mainText() + "</b><br>" + data.subText() + "</qt>");
|
|
|
|
d->imageLabel->setPixmap(data.image());
|
|
|
|
d->preview->setWindowId(data.windowToPreview());
|
|
|
|
d->autohide = data.autohide();
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
if (isVisible()) {
|
2008-11-02 07:55:10 +01:00
|
|
|
d->preview->setInfo();
|
2008-11-03 07:14:26 +01:00
|
|
|
//kDebug() << "about to check size";
|
2008-11-03 06:03:36 +01:00
|
|
|
checkSize();
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 23:53:52 +02:00
|
|
|
void ToolTip::prepareShowing(bool cueUpdate)
|
2008-07-16 17:17:44 +02:00
|
|
|
{
|
2008-09-18 23:53:52 +02:00
|
|
|
if (cueUpdate && d->source) {
|
2008-08-12 20:40:45 +02:00
|
|
|
QMetaObject::invokeMethod(d->source, "toolTipAboutToShow");
|
2008-07-17 01:27:34 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 06:58:42 +01:00
|
|
|
if (d->preview->windowId() != 0) {
|
2008-07-16 17:17:44 +02:00
|
|
|
// show/hide the preview area
|
|
|
|
d->preview->show();
|
|
|
|
} else {
|
|
|
|
d->preview->hide();
|
|
|
|
}
|
|
|
|
|
2008-07-17 01:27:34 +02:00
|
|
|
layout()->activate();
|
2008-11-02 07:55:10 +01:00
|
|
|
d->preview->setInfo();
|
2008-11-03 07:14:26 +01:00
|
|
|
//kDebug() << "about to check size";
|
2008-11-03 06:03:36 +01:00
|
|
|
checkSize();
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-11-02 08:25:56 +01:00
|
|
|
void ToolTip::moveTo(const QPoint &to)
|
|
|
|
{
|
|
|
|
if (!isVisible() ||
|
|
|
|
!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
|
|
|
|
move(to);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-03 07:14:26 +01:00
|
|
|
d->from = QPoint();
|
2008-11-02 08:25:56 +01:00
|
|
|
d->to = to;
|
|
|
|
|
|
|
|
if (!d->timeline) {
|
|
|
|
d->timeline = new QTimeLine(250, this);
|
|
|
|
d->timeline->setFrameRange(0, 10);
|
2008-11-02 08:31:51 +01:00
|
|
|
d->timeline->setCurveShape(QTimeLine::EaseInCurve);
|
2008-11-02 08:25:56 +01:00
|
|
|
connect(d->timeline, SIGNAL(valueChanged(qreal)), this, SLOT(animateMove(qreal)));
|
|
|
|
}
|
|
|
|
|
|
|
|
d->timeline->stop();
|
|
|
|
d->timeline->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::animateMove(qreal progress)
|
|
|
|
{
|
2008-11-03 07:14:26 +01:00
|
|
|
if (d->from.isNull()) {
|
|
|
|
d->from = pos();
|
|
|
|
}
|
|
|
|
|
2008-11-02 08:25:56 +01:00
|
|
|
if (qFuzzyCompare(progress, 1.0)) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:17:44 +02:00
|
|
|
void ToolTip::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(e);
|
2008-10-29 18:46:33 +01:00
|
|
|
d->background->resizeFrame(size());
|
2008-07-16 17:17:44 +02:00
|
|
|
setMask(d->background->mask());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTip::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setClipRect(e->rect());
|
2008-10-12 01:28:20 +02:00
|
|
|
painter.setCompositionMode(QPainter::CompositionMode_Source);
|
2008-07-16 17:17:44 +02:00
|
|
|
painter.fillRect(rect(), Qt::transparent);
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
d->background->paintFrame(&painter);
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-10-26 17:55:54 +01:00
|
|
|
bool ToolTip::autohide() const
|
|
|
|
{
|
|
|
|
return d->autohide;
|
|
|
|
}
|
|
|
|
|
2008-07-16 17:17:44 +02: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
|
|
|
|
QPalette plasmaPalette = QPalette();
|
2008-10-12 01:28:20 +02:00
|
|
|
plasmaPalette.setColor(QPalette::Window,
|
|
|
|
Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
|
|
|
|
plasmaPalette.setColor(QPalette::WindowText,
|
|
|
|
Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
|
2008-07-16 17:17:44 +02:00
|
|
|
setAutoFillBackground(true);
|
|
|
|
setPalette(plasmaPalette);
|
2008-11-02 07:55:10 +01:00
|
|
|
update();
|
2008-07-16 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
2008-08-23 18:17:04 +02:00
|
|
|
} // namespace Plasma
|
2008-07-16 17:17:44 +02:00
|
|
|
|
|
|
|
#include "tooltip_p.moc"
|