add some widgets. it works! no more explicit proxy widget usage, and a nice simplified api that makes the baby jesus happy

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=809503
This commit is contained in:
Aaron J. Seigo 2008-05-19 01:24:12 +00:00
parent b4c0e2ea4d
commit 9d619d61c6
7 changed files with 517 additions and 0 deletions

View File

@ -60,6 +60,8 @@ set(plasma_LIB_SRCS
scripting/runnerscript.cpp
scripting/scriptengine.cpp
widgets/icon.cpp
widgets/label.cpp
widgets/lineedit.cpp
widgets/webcontent.cpp
widgets/meter.cpp
widgets/signalplotter.cpp
@ -141,6 +143,8 @@ install(FILES
install(FILES
widgets/icon.h
widgets/label.h
widgets/lineedit.h
widgets/webcontent.h
widgets/meter.h
widgets/signalplotter.h

1
includes/Label Normal file
View File

@ -0,0 +1 @@
#include ../../plasma/label.h

1
includes/LineEdit Normal file
View File

@ -0,0 +1 @@
#include ../../plasma/lineedit.h

160
widgets/label.cpp Normal file
View File

@ -0,0 +1,160 @@
/*
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
*
* 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 "label.h"
#include <QLabel>
#include <QPainter>
#include <KMimeType>
#include "theme.h"
#include "svg.h"
namespace Plasma
{
class Label::Private
{
public:
Private()
: svg(0)
{
}
~Private()
{
delete svg;
}
void setPixmap(Label *q)
{
if (imagePath.isEmpty()) {
return;
}
KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
QPixmap pm(q->size().toSize());
if (mime->is("image/svg+xml")) {
svg = new Svg();
QPainter p(&pm);
svg->paint(&p, pm.rect());
} else {
pm = QPixmap(absImagePath);
}
static_cast<QLabel*>(q->widget())->setPixmap(pm);
}
QString imagePath;
QString absImagePath;
Svg *svg;
};
Label::Label(QGraphicsWidget *parent)
: QGraphicsProxyWidget(parent),
d(new Private)
{
QLabel* native = new QLabel;
QPalette p = native->palette();
QColor color = Theme::defaultTheme()->color(Theme::TextColor);
p.setColor(QPalette::Normal, QPalette::WindowText, color);
p.setColor(QPalette::Inactive, QPalette::WindowText, color);
native->setPalette(p);
setWidget(native);
native->setAttribute(Qt::WA_NoSystemBackground);
}
Label::~Label()
{
delete d;
}
void Label::setText(const QString &text)
{
static_cast<QLabel*>(widget())->setText(text);
}
QString Label::text() const
{
return static_cast<QLabel*>(widget())->text();
}
void Label::setImage(const QString &path)
{
if (d->imagePath == path) {
return;
}
delete d->svg;
d->svg = 0;
d->imagePath = path;
bool absolutePath = !path.isEmpty() &&
#ifdef Q_WS_WIN
!QDir::isRelativePath(path)
#else
path[0] == '/'
#endif
;
if (absolutePath) {
d->absImagePath = path;
} else {
//TODO: package support
d->absImagePath = Theme::defaultTheme()->imagePath(path);
}
d->setPixmap(this);
}
QString Label::image() const
{
return d->imagePath;
}
void Label::setStylesheet(const QString &stylesheet)
{
widget()->setStyleSheet(stylesheet);
}
QString Label::stylesheet()
{
return widget()->styleSheet();
}
QLabel* Label::nativeWidget() const
{
return static_cast<QLabel*>(widget());
}
void Label::resizeEvent(QGraphicsSceneResizeEvent *event)
{
Q_UNUSED(event)
d->setPixmap(this);
widget()->resize(size().toSize());
}
} // namespace Plasma
#include <label.moc>

98
widgets/label.h Normal file
View File

@ -0,0 +1,98 @@
/*
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
*
* 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__H
#define PLASMA__H
#include <QGraphicsProxyWidget>
class QLabel;
namespace Plasma
{
class Label : public QGraphicsProxyWidget
{
Q_OBJECT
Q_PROPERTY(QGraphicsWidget* parentWidget READ parentWidget)
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(QString image READ image WRITE setImage)
Q_PROPERTY(QString stylesheet READ stylesheet WRITE setStylesheet)
Q_PROPERTY(QLabel* nativeWidget READ nativeWidget)
public:
explicit Label(QGraphicsWidget *parent = 0);
~Label();
/**
* Sets the display text for this Label
*
* @arg text the text to display; should be translated.
*/
void setText(const QString &text);
/**
* @return the display text
*/
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 style sheet used to control the visual display of this Label
*
* @arg stylehseet a CSS string
*/
void setStylesheet(const QString &stylesheet);
/**
* @return the stylesheet currently used with this widget
*/
QString stylesheet();
/**
* @return the native widget wrapped by this Label
*/
QLabel* nativeWidget() const;
Q_SIGNALS:
protected:
void resizeEvent(QGraphicsSceneResizeEvent *event);
private:
class Private;
Private * const d;
};
} // namespace Plasma
#endif // multiple inclusion guard

155
widgets/lineedit.cpp Normal file
View File

@ -0,0 +1,155 @@
/*
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
*
* 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 "lineedit.h"
#include <QLineEdit>
#include <QPainter>
#include <KMimeType>
#include "theme.h"
#include "svg.h"
namespace Plasma
{
class LineEdit::Private
{
public:
Private()
: svg(0)
{
}
~Private()
{
delete svg;
}
void setPixmap(LineEdit *q)
{
if (imagePath.isEmpty()) {
return;
}
KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
QPixmap pm(q->size().toSize());
if (mime->is("image/svg+xml")) {
svg = new Svg();
QPainter p(&pm);
svg->paint(&p, pm.rect());
} else {
pm = QPixmap(absImagePath);
}
//TODO: load image into widget
//static_cast<QLineEdit*>(widget())->setPixmap();
//static_cast<QLineEdit*>(widget())->setIcon(QIcon();
}
QString imagePath;
QString absImagePath;
Svg *svg;
};
LineEdit::LineEdit(QGraphicsWidget *parent)
: QGraphicsProxyWidget(parent),
d(new Private)
{
QLineEdit* native = new QLineEdit;
//TODO: forward signals
setWidget(native);
native->setAttribute(Qt::WA_NoSystemBackground);
}
LineEdit::~LineEdit()
{
delete d;
}
void LineEdit::setText(const QString &text)
{
static_cast<QLineEdit*>(widget())->setText(text);
}
QString LineEdit::text() const
{
return static_cast<QLineEdit*>(widget())->text();
}
void LineEdit::setImage(const QString &path)
{
if (d->imagePath == path) {
return;
}
delete d->svg;
d->svg = 0;
d->imagePath = path;
bool absolutePath = !path.isEmpty() &&
#ifdef Q_WS_WIN
!QDir::isRelativePath(path)
#else
path[0] == '/'
#endif
;
if (absolutePath) {
d->absImagePath = path;
} else {
//TODO: package support
d->absImagePath = Theme::defaultTheme()->imagePath(path);
}
d->setPixmap(this);
}
QString LineEdit::image() const
{
return d->imagePath;
}
void LineEdit::setStylesheet(const QString &stylesheet)
{
widget()->setStyleSheet(stylesheet);
}
QString LineEdit::stylesheet()
{
return widget()->styleSheet();
}
QLineEdit* LineEdit::nativeWidget() const
{
return static_cast<QLineEdit*>(widget());
}
void LineEdit::resizeEvent(QGraphicsSceneResizeEvent *event)
{
Q_UNUSED(event)
d->setPixmap(this);
}
} // namespace Plasma
#include <lineedit.moc>

98
widgets/lineedit.h Normal file
View File

@ -0,0 +1,98 @@
/*
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
*
* 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__H
#define PLASMA__H
#include <QGraphicsProxyWidget>
class QLineEdit;
namespace Plasma
{
class LineEdit : public QGraphicsProxyWidget
{
Q_OBJECT
Q_PROPERTY(QGraphicsWidget* parentWidget READ parentWidget)
Q_PROPERTY(QString text READ text WRITE setText)
Q_PROPERTY(QString image READ image WRITE setImage)
Q_PROPERTY(QString stylesheet READ stylesheet WRITE setStylesheet)
Q_PROPERTY(QLineEdit* nativeWidget READ nativeWidget)
public:
explicit LineEdit(QGraphicsWidget *parent = 0);
~LineEdit();
/**
* Sets the display text for this LineEdit
*
* @arg text the text to display; should be translated.
*/
void setText(const QString &text);
/**
* @return the display text
*/
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 style sheet used to control the visual display of this LineEdit
*
* @arg stylehseet a CSS string
*/
void setStylesheet(const QString &stylesheet);
/**
* @return the stylesheet currently used with this widget
*/
QString stylesheet();
/**
* @return the native widget wrapped by this LineEdit
*/
QLineEdit* nativeWidget() const;
Q_SIGNALS:
protected:
void resizeEvent(QGraphicsSceneResizeEvent *event);
private:
class Private;
Private * const d;
};
} // namespace Plasma
#endif // multiple inclusion guard