diff --git a/CMakeLists.txt b/CMakeLists.txt index ad008ab48..3667a3517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/widgets) ########### next target ############### @@ -12,6 +12,7 @@ set(plasma_LIB_SRCS svg.cpp theme.cpp widgets/checkbox.cpp + widgets/icon.cpp widgets/lineedit.cpp widgets/pushbutton.cpp widgets/radiobutton.cpp @@ -43,11 +44,15 @@ install( FILES plasma_export.h svg.h theme.h - widgets/widget.h + widgets/icon.h + widgets/layout.h + widgets/layoutitem.h widgets/lineedit.h widgets/pushbutton.h widgets/checkbox.h widgets/radiobutton.h + widgets/vboxlayout.h + widgets/widget.h DESTINATION ${INCLUDE_INSTALL_DIR}/plasma ) install( FILES diff --git a/widgets/icon.cpp b/widgets/icon.cpp new file mode 100644 index 000000000..807c5d0a1 --- /dev/null +++ b/widgets/icon.cpp @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2007 by 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 version 2 as + * published by the Free Software Foundation + * + * 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 +#include + +#include +#include + +#include "svg.h" +#include "icon.h" + +namespace Plasma +{ + +class Icon::Private +{ + public: + Private() + : size(40, 40), + state(Icon::None), + background("widgets/iconbg"), + backgroundPressed("widgets/iconbgpressed"), + foreground("widgets/iconfg"), + foregroundHover("widgets/iconfghover"), + foregroundPressed("widgets/iconfgpressed") + { + minSize = size; + maxSize = size; + state = Icon::None; + } + ~Private() {} + + QString text; + QIcon icon; + QSizeF size; + QSizeF minSize; + QSizeF maxSize; + Icon::ButtonState state; + Svg background; + Svg backgroundPressed; + Svg foreground; + Svg foregroundHover; + Svg foregroundPressed; +}; + +Icon::Icon(QGraphicsItem *parent) + : QObject(), + QGraphicsItem(parent), + d(new Private) +{ + setAcceptedMouseButtons(Qt::LeftButton); + setAcceptsHoverEvents(true); + setEnabled(true); + setPos(QPointF(0.0,0.0)); +} + +Icon::~Icon() +{ + delete d; +} + +QRectF Icon::boundingRect() const +{ + return QRectF(pos(), d->size); +} + +void Icon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(option) + Q_UNUSED(widget) + + QRectF rect = boundingRect(); + + switch (d->state) { + case None: + case Hover: + d->background.paint(painter, 0, 0); + break; + case Pressed: + d->backgroundPressed.paint(painter, 0, 0); + break; + } + + if (!d->icon.isNull()) { + int deltaX = d->size.width() * 0.05; + int deltaY = d->size.height() * 0.05; + painter->drawPixmap(deltaX, deltaY, d->icon.pixmap((d->size * 0.9).toSize())); + } + + //TODO: draw text + + switch (d->state) { + case None: + d->foreground.paint(painter, 0, 0); + break; + case Hover: + d->foregroundHover.paint(painter, 0, 0); + break; + case Pressed: + d->foregroundPressed.paint(painter, 0, 0); + break; + } +} + +void Icon::setText(const QString& text) +{ + d->text = text; +//TODO: implement this puppy calculateSize(); +} + +QString Icon::text() const +{ + return d->text; +} + +void Icon::setIcon(const QString& icon) +{ + if (icon.isEmpty()) { + setIcon(QIcon()); + return; + } + + setIcon(KIcon(icon)); +} + +void Icon::setIcon(const QIcon& icon) +{ + d->icon = icon; + update(); +} + +QSizeF Icon::size() const +{ + return d->size; +} + +void Icon::setSize(const QSizeF& s) +{ + prepareGeometryChange(); + d->size = s.boundedTo(d->maxSize); //FIXME: maxSize always == size means it can be changed. wtf. =) + d->background.resize(s); + d->backgroundPressed.resize(s); + d->foreground.resize(s); + d->foregroundHover.resize(s); + d->foregroundPressed.resize(s); + update(); +} + +void Icon::setSize(int w, int h) +{ + setSize(QSizeF(w, h)); +} + +bool Icon::isDown() +{ + return d->state == Icon::Pressed; +} + +void Icon::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + event->accept(); + d->state = Icon::Pressed; + update(); + emit pressed(true); +} + +void Icon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + event->accept(); + bool wasClicked = d->state == Icon::Pressed && boundingRect().contains(event->scenePos()); + d->state = Icon::None; + update(); + + if (wasClicked) { + emit pressed(false); + emit clicked(); + } +} + +QSizeF Icon::sizeHint() const +{ + return d->size; +} + +QSizeF Icon::minimumSize() const +{ + return d->minSize; +} + +QSizeF Icon::maximumSize() const +{ + return d->maxSize; +} + +Qt::Orientations Icon::expandingDirections() const +{ + return Qt::Horizontal; +} + +bool Icon::hasHeightForWidth() const +{ + return true; +} + +qreal Icon::heightForWidth(qreal w) const +{ + return w; //FIXME: we shouldn't assume squareness but actually calculate based on text and what not +} + +bool Icon::hasWidthForHeight() const +{ + return true; +} + +qreal Icon::widthForHeight(qreal h) const +{ + return h; //FIXME: we shouldn't assume squareness but actually calculate based on text and what not +} + +QRectF Icon::geometry() const +{ + return boundingRect().toRect(); +} + +void Icon::setGeometry(const QRectF &r) +{ + setSize(r.size()); + setPos(r.x(),r.y()); +} + +} // namespace Plasma + +#include "icon.moc" diff --git a/widgets/icon.h b/widgets/icon.h new file mode 100644 index 000000000..3db7b14d9 --- /dev/null +++ b/widgets/icon.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2007 by Siraj Razick siraj@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 version 2 as + * published by the Free Software Foundation + * + * 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 PUSHBUTTON_H +#define PUSHBUTTON_H + +#include +#include + +#include +#include +#include + +//TODO +//Please Document this class + +namespace Plasma +{ + +/** + * Icon class, for URIs and menu popups in panels + */ +class PLASMA_EXPORT Icon : public QObject, + public QGraphicsItem, + public LayoutItem +{ + Q_OBJECT + public: + enum ButtonState + { + None, + Hover, + Pressed + }; + + Icon(QGraphicsItem *parent = 0); + virtual ~Icon(); + + QString text() const; + void setText(const QString &name); + + QSizeF size() const; + void setSize(const QSizeF& size); + void setSize(int height, int width); + + void setIcon(const QString& icon); + void setIcon(const QIcon& icon); + + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); + QRectF boundingRect() const; + + //layout stufff + Qt::Orientations expandingDirections() const = 0; + + QSizeF minimumSize() const = 0; + QSizeF maximumSize() const = 0; + + bool hasHeightForWidth() const; + qreal heightForWidth(qreal w) const; + + bool hasWidthForHeight() const; + qreal widthForHeight(qreal h) const; + + QRectF geometry() const = 0; + void setGeometry(const QRectF& r) = 0; + + QSizeF sizeHint() const = 0; + + Q_SIGNALS: + void pressed(bool down); + void clicked(); + + protected: + bool isDown(); + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + + private: + class Private; + Private * const d; +}; + +} // namespace Plasma + +#endif