2008-08-02 14:01:34 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Marco Martin <notmart@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 "frame.h"
|
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneResizeEvent>
|
|
|
|
#include <QWidget>
|
2008-08-02 14:51:50 +02:00
|
|
|
#include <QDir>
|
2008-08-02 14:01:34 +02:00
|
|
|
#include <QApplication>
|
|
|
|
|
|
|
|
//KDE
|
|
|
|
#include <KMimeType>
|
|
|
|
|
|
|
|
//Plasma
|
|
|
|
#include "plasma/theme.h"
|
2008-10-29 18:46:33 +01:00
|
|
|
#include "plasma/framesvg.h"
|
2008-08-02 14:01:34 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class FramePrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FramePrivate(Frame *parent)
|
|
|
|
: q(parent),
|
|
|
|
svg(0),
|
|
|
|
image(0),
|
|
|
|
pixmap(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~FramePrivate()
|
|
|
|
{
|
2008-08-12 01:26:13 +02:00
|
|
|
delete pixmap;
|
2008-08-02 14:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void syncBorders();
|
|
|
|
|
|
|
|
Frame *q;
|
2008-10-29 18:46:33 +01:00
|
|
|
FrameSvg *svg;
|
2008-08-02 14:01:34 +02:00
|
|
|
Frame::Shadow shadow;
|
|
|
|
QString text;
|
|
|
|
QString styleSheet;
|
|
|
|
QString imagePath;
|
|
|
|
QString absImagePath;
|
|
|
|
Svg *image;
|
|
|
|
QPixmap *pixmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
void FramePrivate::syncBorders()
|
|
|
|
{
|
|
|
|
//set margins from the normal element
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
|
|
|
|
svg->getMargins(left, top, right, bottom);
|
2008-08-02 15:01:16 +02:00
|
|
|
|
2008-08-02 14:01:34 +02:00
|
|
|
if (!text.isNull()) {
|
|
|
|
QFontMetricsF fm(QApplication::font());
|
2008-08-02 15:01:16 +02:00
|
|
|
top += fm.height();
|
2008-08-02 14:01:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
q->setContentsMargins(left, top, right, bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
Frame::Frame(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new FramePrivate(this))
|
|
|
|
{
|
2008-10-29 18:46:33 +01:00
|
|
|
d->svg = new Plasma::FrameSvg(this);
|
2008-08-02 14:01:34 +02:00
|
|
|
d->svg->setImagePath("widgets/frame");
|
|
|
|
d->svg->setElementPrefix("plain");
|
|
|
|
d->syncBorders();
|
|
|
|
|
|
|
|
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Frame::~Frame()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::setFrameShadow(Shadow shadow)
|
|
|
|
{
|
|
|
|
d->shadow = shadow;
|
|
|
|
|
|
|
|
switch (d->shadow) {
|
|
|
|
case Raised:
|
|
|
|
d->svg->setElementPrefix("raised");
|
|
|
|
break;
|
|
|
|
case Sunken:
|
|
|
|
d->svg->setElementPrefix("sunken");
|
|
|
|
break;
|
|
|
|
case Plain:
|
|
|
|
default:
|
|
|
|
d->svg->setElementPrefix("plain");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->syncBorders();
|
|
|
|
}
|
|
|
|
|
|
|
|
Frame::Shadow Frame::frameShadow() const
|
|
|
|
{
|
|
|
|
return d->shadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::setText(QString text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
|
|
|
d->syncBorders();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Frame::text() const
|
|
|
|
{
|
|
|
|
return d->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::setImage(const QString &path)
|
|
|
|
{
|
|
|
|
if (d->imagePath == path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete d->image;
|
|
|
|
d->image = 0;
|
|
|
|
d->imagePath = path;
|
|
|
|
delete d->pixmap;
|
|
|
|
d->pixmap = 0;
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
bool absolutePath = !path.isEmpty() &&
|
2008-08-02 14:01:34 +02:00
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
!QDir::isRelativePath(path)
|
|
|
|
#else
|
|
|
|
(path[0] == '/' || path.startsWith(":/"))
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
if (absolutePath) {
|
|
|
|
d->absImagePath = path;
|
|
|
|
} else {
|
|
|
|
//TODO: package support
|
|
|
|
d->absImagePath = Theme::defaultTheme()->imagePath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
KMimeType::Ptr mime = KMimeType::findByPath(d->absImagePath);
|
|
|
|
|
|
|
|
if (!mime->is("image/svg+xml") && !mime->is("application/x-gzip")) {
|
|
|
|
d->pixmap = new QPixmap(d->absImagePath);
|
|
|
|
} else {
|
|
|
|
d->image = new Plasma::Svg(this);
|
|
|
|
d->image->setImagePath(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Frame::image() const
|
|
|
|
{
|
|
|
|
return d->imagePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::setStyleSheet(const QString &styleSheet)
|
|
|
|
{
|
|
|
|
//TODO: implement stylesheets painting
|
|
|
|
d->styleSheet = styleSheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Frame::styleSheet() const
|
|
|
|
{
|
|
|
|
return d->styleSheet;
|
|
|
|
}
|
|
|
|
|
2008-10-12 03:31:54 +02:00
|
|
|
QWidget *Frame::nativeWidget() const
|
2008-08-02 14:01:34 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::paint(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
2008-10-29 18:46:33 +01:00
|
|
|
d->svg->paintFrame(painter);
|
2008-08-02 14:01:34 +02:00
|
|
|
|
|
|
|
if (!d->text.isNull()) {
|
|
|
|
QFontMetricsF fm(QApplication::font());
|
2008-10-02 22:07:49 +02:00
|
|
|
QRectF textRect = d->svg->contentsRect();
|
|
|
|
textRect.setHeight(fm.height());
|
2008-08-02 14:01:34 +02:00
|
|
|
painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::TextColor));
|
|
|
|
painter->drawText(textRect, Qt::AlignHCenter|Qt::AlignTop, d->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->imagePath.isNull()) {
|
|
|
|
if (d->pixmap && !d->pixmap->isNull()) {
|
|
|
|
painter->drawPixmap(contentsRect(), *d->pixmap, d->pixmap->rect());
|
|
|
|
} else if (d->image) {
|
|
|
|
d->image->paint(painter, contentsRect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Frame::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
2008-10-29 18:46:33 +01:00
|
|
|
d->svg->resizeFrame(event->newSize());
|
2008-08-02 14:01:34 +02:00
|
|
|
|
|
|
|
if (d->image) {
|
|
|
|
d->image->resize(contentsRect().size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-02 22:07:49 +02:00
|
|
|
QSizeF Frame::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
QSizeF hint = QGraphicsWidget::sizeHint(which, constraint);
|
|
|
|
|
|
|
|
if (!d->image && !layout()) {
|
|
|
|
QFontMetricsF fm(QApplication::font());
|
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
d->svg->getMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
hint.setHeight(fm.height() + top + bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2008-08-02 14:01:34 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <frame.moc>
|
|
|
|
|