2008-11-04 00:08:39 +01: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 <QGraphicsSceneResizeEvent>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QDir>
|
2010-10-22 02:25:17 +02:00
|
|
|
#include <QPainter>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
//KDE
|
2012-06-19 15:12:44 +02:00
|
|
|
#include <qmimedatabase.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
//Plasma
|
2010-10-22 02:25:17 +02:00
|
|
|
#include "framesvg.h"
|
|
|
|
#include "private/themedwidgetinterface_p.h"
|
|
|
|
#include "theme.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2010-10-22 14:29:35 +02:00
|
|
|
class FramePrivate : public ThemedWidgetInterface<Frame>
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
public:
|
2010-10-22 14:29:35 +02:00
|
|
|
FramePrivate(Frame *parent)
|
2010-10-22 18:55:46 +02:00
|
|
|
: ThemedWidgetInterface<Frame>(parent),
|
2008-11-04 00:08:39 +01:00
|
|
|
svg(0),
|
|
|
|
image(0),
|
2010-10-22 02:25:17 +02:00
|
|
|
pixmap(0)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~FramePrivate()
|
|
|
|
{
|
|
|
|
delete pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncBorders();
|
|
|
|
|
|
|
|
FrameSvg *svg;
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!text.isNull()) {
|
2010-10-22 02:25:17 +02:00
|
|
|
QFontMetricsF fm(q->font());
|
2008-11-04 00:08:39 +01:00
|
|
|
top += fm.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
q->setContentsMargins(left, top, right, bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
Frame::Frame(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new FramePrivate(this))
|
|
|
|
{
|
2010-01-27 23:22:07 +01:00
|
|
|
d->svg = new Plasma::FrameSvg(this);
|
|
|
|
d->svg->setImagePath("widgets/frame");
|
|
|
|
d->svg->setElementPrefix("plain");
|
|
|
|
d->syncBorders();
|
|
|
|
|
2010-06-24 22:52:14 +02:00
|
|
|
connect(d->svg, SIGNAL(repaintNeeded()), SLOT(syncBorders()));
|
2010-10-22 02:25:17 +02:00
|
|
|
d->initTheming();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-21 07:29:47 +02:00
|
|
|
void Frame::setEnabledBorders(const FrameSvg::EnabledBorders borders)
|
|
|
|
{
|
|
|
|
if (borders != d->svg->enabledBorders()) {
|
|
|
|
d->svg->setEnabledBorders(borders);
|
|
|
|
d->syncBorders();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FrameSvg::EnabledBorders Frame::enabledBorders() const
|
|
|
|
{
|
|
|
|
return d->svg->enabledBorders();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void Frame::setText(QString text)
|
|
|
|
{
|
|
|
|
d->text = text;
|
|
|
|
d->syncBorders();
|
2009-10-31 15:10:11 +01:00
|
|
|
updateGeometry();
|
2009-11-13 12:59:17 +01:00
|
|
|
update();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
bool absolutePath = !path.isEmpty() &&
|
2012-08-03 11:38:59 +02:00
|
|
|
#ifdef Q_OS_WIN
|
2008-11-04 00:08:39 +01:00
|
|
|
!QDir::isRelativePath(path)
|
|
|
|
#else
|
2009-10-13 14:40:13 +02:00
|
|
|
(path[0] == '/' || path.startsWith(QLatin1String(":/")))
|
2008-11-04 00:08:39 +01:00
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
if (absolutePath) {
|
|
|
|
d->absImagePath = path;
|
|
|
|
} else {
|
|
|
|
//TODO: package support
|
|
|
|
d->absImagePath = Theme::defaultTheme()->imagePath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-19 15:12:44 +02:00
|
|
|
QMimeDatabase db;
|
|
|
|
QMimeType mime = db.mimeTypeForFile(d->absImagePath);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2012-06-19 15:12:44 +02:00
|
|
|
if (!mime.inherits("image/svg+xml") && !mime.inherits("application/x-gzip")) {
|
2008-11-04 00:08:39 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *Frame::nativeWidget() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-21 07:29:47 +02:00
|
|
|
void Frame::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
|
|
|
|
d->svg->paintFrame(painter);
|
|
|
|
|
|
|
|
if (!d->text.isNull()) {
|
2010-10-22 02:25:17 +02:00
|
|
|
QFontMetricsF fm(font());
|
2008-11-04 00:08:39 +01:00
|
|
|
QRectF textRect = d->svg->contentsRect();
|
|
|
|
textRect.setHeight(fm.height());
|
2010-10-22 02:25:17 +02:00
|
|
|
painter->setFont(font());
|
2008-11-04 00:08:39 +01: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)
|
|
|
|
{
|
|
|
|
d->svg->resizeFrame(event->newSize());
|
|
|
|
|
|
|
|
if (d->image) {
|
|
|
|
d->image->resize(contentsRect().size());
|
|
|
|
}
|
2010-01-26 16:29:53 +01:00
|
|
|
|
2010-01-26 16:43:23 +01:00
|
|
|
QGraphicsWidget::resizeEvent(event);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Frame::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
QSizeF hint = QGraphicsWidget::sizeHint(which, constraint);
|
|
|
|
|
|
|
|
if (!d->image && !layout()) {
|
2010-10-22 02:25:17 +02:00
|
|
|
QFontMetricsF fm(font());
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
d->svg->getMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
hint.setHeight(fm.height() + top + bottom);
|
2009-10-30 11:11:12 +01:00
|
|
|
if (which == Qt::MinimumSize || which == Qt::PreferredSize) {
|
|
|
|
QRectF rect = fm.boundingRect(d->text);
|
|
|
|
hint.setWidth(rect.width() + left + right);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:13:55 +02:00
|
|
|
void Frame::changeEvent(QEvent *event)
|
|
|
|
{
|
2010-10-22 02:25:17 +02:00
|
|
|
d->changeEvent(event);
|
2009-09-13 15:13:55 +02:00
|
|
|
QGraphicsWidget::changeEvent(event);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
|
2012-02-08 23:33:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include "moc_frame.cpp"
|