2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 by Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tooltipcontent.h"
|
|
|
|
|
2010-08-18 18:33:32 +02:00
|
|
|
#include <QGraphicsWidget>
|
2008-11-14 01:52:29 +01:00
|
|
|
#include <QHash>
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kiconloader.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2008-11-14 01:52:29 +01:00
|
|
|
struct ToolTipResource
|
|
|
|
{
|
|
|
|
ToolTipResource()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipResource(ToolTipContent::ResourceType t, const QVariant &v)
|
|
|
|
: type(t),
|
|
|
|
data(v)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent::ResourceType type;
|
|
|
|
QVariant data;
|
|
|
|
};
|
2009-05-11 11:32:19 +02:00
|
|
|
|
2010-09-22 01:50:57 +02:00
|
|
|
const int MAXIMUM_TEXT_LENGTH = 5000;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
class ToolTipContentPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ToolTipContentPrivate()
|
2009-06-26 08:40:15 +02:00
|
|
|
: autohide(true),
|
2011-04-26 20:43:40 +02:00
|
|
|
instantPopup(false),
|
2009-12-24 15:54:48 +01:00
|
|
|
clickable(false),
|
|
|
|
highlightWindows(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString mainText;
|
|
|
|
QString subText;
|
|
|
|
QPixmap image;
|
2009-03-19 23:15:29 +01:00
|
|
|
QList<WId> windowsToPreview;
|
2008-11-14 01:52:29 +01:00
|
|
|
QHash<QString, ToolTipResource> resources;
|
2010-08-18 18:33:32 +02:00
|
|
|
QWeakPointer<QGraphicsWidget> graphicsWidget;
|
2009-06-26 08:40:15 +02:00
|
|
|
bool autohide : 1;
|
2011-04-26 20:43:40 +02:00
|
|
|
bool instantPopup : 1;
|
2009-06-26 08:40:15 +02:00
|
|
|
bool clickable : 1;
|
2009-12-24 15:54:48 +01:00
|
|
|
bool highlightWindows : 1;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ToolTipContent::ToolTipContent()
|
|
|
|
: d(new ToolTipContentPrivate)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent::ToolTipContent(const ToolTipContent &other)
|
|
|
|
: d(new ToolTipContentPrivate(*other.d))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent::~ToolTipContent()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent &ToolTipContent::operator=(const ToolTipContent &other)
|
|
|
|
{
|
|
|
|
*d = *other.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent::ToolTipContent(const QString &mainText,
|
|
|
|
const QString &subText,
|
|
|
|
const QPixmap &image)
|
|
|
|
: d(new ToolTipContentPrivate)
|
|
|
|
{
|
2011-05-05 12:44:35 +02:00
|
|
|
setMainText(mainText);
|
|
|
|
setSubText(subText);
|
|
|
|
setImage(image);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ToolTipContent::ToolTipContent(const QString &mainText,
|
|
|
|
const QString &subText,
|
|
|
|
const QIcon &icon)
|
|
|
|
: d(new ToolTipContentPrivate)
|
|
|
|
{
|
2011-05-05 12:44:35 +02:00
|
|
|
setMainText(mainText);
|
|
|
|
setSubText(subText);
|
|
|
|
setImage(icon);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipContent::isEmpty() const
|
|
|
|
{
|
|
|
|
return d->mainText.isEmpty() &&
|
|
|
|
d->subText.isEmpty() &&
|
|
|
|
d->image.isNull() &&
|
2009-03-19 23:15:29 +01:00
|
|
|
(d->windowsToPreview.size() == 0);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipContent::setMainText(const QString &text)
|
|
|
|
{
|
2011-05-05 12:44:35 +02:00
|
|
|
d->mainText = text.trimmed();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ToolTipContent::mainText() const
|
|
|
|
{
|
2010-09-22 01:50:57 +02:00
|
|
|
QString text = d->mainText;
|
|
|
|
text.truncate(MAXIMUM_TEXT_LENGTH);
|
|
|
|
return text;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipContent::setSubText(const QString &text)
|
|
|
|
{
|
2011-05-05 12:44:35 +02:00
|
|
|
d->subText = text.trimmed();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ToolTipContent::subText() const
|
|
|
|
{
|
2010-09-22 01:50:57 +02:00
|
|
|
QString text = d->subText;
|
|
|
|
text.truncate(MAXIMUM_TEXT_LENGTH);
|
|
|
|
return text;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipContent::setImage(const QPixmap &image)
|
|
|
|
{
|
|
|
|
d->image = image;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToolTipContent::setImage(const QIcon &icon)
|
|
|
|
{
|
|
|
|
d->image = icon.pixmap(IconSize(KIconLoader::Desktop));
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap ToolTipContent::image() const
|
|
|
|
{
|
|
|
|
return d->image;
|
|
|
|
}
|
|
|
|
|
2009-07-25 09:21:14 +02:00
|
|
|
void ToolTipContent::setWindowToPreview(WId id)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
d->windowsToPreview.clear();
|
|
|
|
d->windowsToPreview.append(id);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WId ToolTipContent::windowToPreview() const
|
|
|
|
{
|
2009-03-19 23:15:29 +01:00
|
|
|
if (d->windowsToPreview.size() == 1) {
|
|
|
|
return d->windowsToPreview.first();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-19 23:45:03 +01:00
|
|
|
void ToolTipContent::setWindowsToPreview(const QList<WId> & ids)
|
2009-03-19 23:15:29 +01:00
|
|
|
{
|
|
|
|
d->windowsToPreview = ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<WId> ToolTipContent::windowsToPreview() const
|
|
|
|
{
|
|
|
|
return d->windowsToPreview;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-12-24 15:54:48 +01:00
|
|
|
void ToolTipContent::setHighlightWindows(bool highlight)
|
|
|
|
{
|
|
|
|
d->highlightWindows = highlight;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipContent::highlightWindows() const
|
|
|
|
{
|
|
|
|
return d->highlightWindows;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void ToolTipContent::setAutohide(bool autohide)
|
|
|
|
{
|
|
|
|
d->autohide = autohide;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipContent::autohide() const
|
|
|
|
{
|
|
|
|
return d->autohide;
|
|
|
|
}
|
|
|
|
|
2011-04-26 20:43:40 +02:00
|
|
|
void ToolTipContent::setInstantPopup(bool enabled)
|
|
|
|
{
|
|
|
|
d->instantPopup = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipContent::isInstantPopup() const
|
|
|
|
{
|
|
|
|
return d->instantPopup;
|
|
|
|
}
|
|
|
|
|
2008-11-14 01:52:29 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-26 08:40:15 +02:00
|
|
|
void ToolTipContent::setClickable(bool clickable)
|
|
|
|
{
|
|
|
|
d->clickable = clickable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ToolTipContent::isClickable() const
|
|
|
|
{
|
|
|
|
return d->clickable;
|
|
|
|
}
|
|
|
|
|
2010-08-18 18:33:32 +02:00
|
|
|
void ToolTipContent::setGraphicsWidget(QGraphicsWidget *widget)
|
|
|
|
{
|
|
|
|
d->graphicsWidget = widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *ToolTipContent::graphicsWidget() const
|
|
|
|
{
|
|
|
|
return d->graphicsWidget.data();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
|