new widget Plasma::Frame:

Can act as a qframe like or as a groupbox (with label) supports
also arbitrary pixmap/svg backgrounds
stylesheet support is still todo and the graphics for the raised version
is still temporary

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=840986
This commit is contained in:
Marco Martin 2008-08-02 12:01:34 +00:00
parent a6293d363f
commit 0337120602
3 changed files with 366 additions and 0 deletions

View File

@ -66,6 +66,7 @@ set(plasma_LIB_SRCS
widgets/extender.cpp widgets/extender.cpp
widgets/extenderitem.cpp widgets/extenderitem.cpp
widgets/flash.cpp widgets/flash.cpp
widgets/frame.cpp
widgets/groupbox.cpp widgets/groupbox.cpp
widgets/icon.cpp widgets/icon.cpp
widgets/label.cpp widgets/label.cpp
@ -168,6 +169,7 @@ install(FILES
widgets/extender.h widgets/extender.h
widgets/extenderitem.h widgets/extenderitem.h
widgets/flash.h widgets/flash.h
widgets/frame.h
widgets/groupbox.h widgets/groupbox.h
widgets/icon.h widgets/icon.h
widgets/label.h widgets/label.h

237
widgets/frame.cpp Normal file
View File

@ -0,0 +1,237 @@
/*
* 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>
#include <QApplication>
//KDE
#include <KMimeType>
//Plasma
#include "plasma/theme.h"
#include "plasma/panelsvg.h"
namespace Plasma
{
class FramePrivate
{
public:
FramePrivate(Frame *parent)
: q(parent),
svg(0),
image(0),
pixmap(0)
{
}
~FramePrivate()
{
delete svg;
}
void syncBorders();
Frame *q;
PanelSvg *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()) {
QFontMetricsF fm(QApplication::font());
top += fm.height();
}
q->setContentsMargins(left, top, right, bottom);
}
Frame::Frame(QGraphicsWidget *parent)
: QGraphicsWidget(parent),
d(new FramePrivate(this))
{
d->svg = new Plasma::PanelSvg(this);
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;
bool absolutePath = !path.isEmpty() &&
#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;
}
QWidget* Frame::nativeWidget() const
{
return 0;
}
void Frame::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
d->svg->paintPanel(painter);
if (!d->text.isNull()) {
QFontMetricsF fm(QApplication::font());
QRectF textRect = contentsRect();
textRect.translate(0, -fm.height());
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->resizePanel(event->newSize());
if (d->image) {
d->image->resize(contentsRect().size());
}
}
} // namespace Plasma
#include <frame.moc>

127
widgets/frame.h Normal file
View File

@ -0,0 +1,127 @@
/*
* 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.
*/
#ifndef PLASMA_FRAME_H
#define PLASMA_FRAME_H
#include <QtGui/QGraphicsWidget>
#include <plasma/plasma_export.h>
class QFrame;
namespace Plasma
{
class FramePrivate;
/**
* @short A widget that provides a simple frame
* A simple frame to group widgets, it can have a plain, sunken or raise aspect
* the default aspect is plain
*/
class PLASMA_EXPORT Frame : public QGraphicsWidget
{
Q_OBJECT
public:
enum Shadow {
Plain = 1,
Raised,
Sunken
};
/**
* Constructs a new Frame
*
* @arg parent the parent of this widget
*/
explicit Frame(QGraphicsWidget *parent = 0);
~Frame();
/**
* Sets the Frame's shadow style
*
* @arg shadow plain, raised or sunken
*/
void setFrameShadow(Shadow shadow);
/**
* @return the Frame's shadow style
*/
Shadow frameShadow() const;
/**
* Set the text to display by this Frame
*
* @arg text the text
*/
void setText(QString text);
/**
* @return text displayed from this Frame
*/
QString text() const;
/**
* Sets the path to an image to display.
*
* @arg path the path to the image; if a relative path, then a themed image will be loaded.
*/
void setImage(const QString &path);
/**
* @return the image path being displayed currently, or an empty string if none.
*/
QString image() const;
/**
* Sets the stylesheet used to control the visual display of this Frame
*
* @arg stylesheet a CSS string
*/
void setStyleSheet(const QString &stylesheet);
/**
* @return the stylesheet currently used with this widget
*/
QString styleSheet() const;
/**
* @return the native widget wrapped by this Label
*/
QWidget* nativeWidget() const;
protected:
void paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget = 0);
void resizeEvent(QGraphicsSceneResizeEvent *event);
private:
FramePrivate * const d;
Q_PRIVATE_SLOT(d, void syncBorders())
};
} // namespace Plasma
#endif // multiple inclusion guard