2007-03-05 05:13:22 +01:00
|
|
|
/*
|
2007-03-13 01:32:17 +01:00
|
|
|
* Copyright (C) 2007 by Siraj Razick siraj@kde.org
|
2007-03-05 05:13:22 +01:00
|
|
|
*
|
|
|
|
* 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 <QStyleOptionFrameV2>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2007-03-13 04:00:09 +01:00
|
|
|
#include <QDebug>
|
2007-03-05 05:13:22 +01:00
|
|
|
|
|
|
|
#include "pushbutton.h"
|
2007-03-05 10:00:26 +01:00
|
|
|
#include "pushbutton.moc"
|
2007-03-05 05:13:22 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class PushButton::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-03-13 04:00:09 +01:00
|
|
|
Private() {}
|
|
|
|
~Private() {}
|
|
|
|
|
|
|
|
QString labelText;
|
|
|
|
QString labelIcon;
|
|
|
|
QColor labelTextColor;
|
|
|
|
QIcon icon;
|
|
|
|
QSize iconSize;
|
|
|
|
bool hasIcon;
|
|
|
|
int labelTextOpacity;
|
|
|
|
int height;
|
|
|
|
int width;
|
|
|
|
int maxWidth;
|
|
|
|
int minWidth;
|
|
|
|
int minHeight;
|
|
|
|
int maxHeight;
|
|
|
|
int radius;
|
|
|
|
QTimer * updateTimer;
|
|
|
|
PushButton::ButtonState state;
|
2007-03-05 05:13:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
PushButton::PushButton(QGraphicsItem *parent)
|
2007-03-13 04:25:19 +01:00
|
|
|
: QGraphicsItem(parent),
|
2007-03-12 20:16:17 +01:00
|
|
|
QLayoutItem (Qt::AlignHCenter),
|
2007-03-05 05:13:22 +01:00
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setEnabled(true);
|
2007-03-13 04:00:09 +01:00
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
d->height = 40;
|
|
|
|
d->width = 100 ;
|
2007-03-12 20:16:17 +01:00
|
|
|
d->minWidth = d->width;
|
|
|
|
d->maxWidth = d->width;
|
|
|
|
d->minHeight = d->height;
|
|
|
|
d->maxHeight = d->height;
|
2007-03-05 05:13:22 +01:00
|
|
|
setPos(QPointF(0.0,0.0));
|
2007-03-13 04:00:09 +01:00
|
|
|
d->state = PushButton::None;
|
2007-03-13 04:25:19 +01:00
|
|
|
d->labelText = QString("Plasma");
|
2007-03-13 04:00:09 +01:00
|
|
|
d->labelTextColor = QColor(201,201,255);
|
2007-03-05 13:54:54 +01:00
|
|
|
d->hasIcon = false;
|
2007-03-13 04:00:09 +01:00
|
|
|
d->iconSize = QSize(32,32);
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PushButton::~PushButton()
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
delete d;
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
2007-03-13 04:25:19 +01:00
|
|
|
void PushButton::data(const DataSource::Data &data)
|
|
|
|
{
|
|
|
|
Q_UNUSED(data)
|
|
|
|
}
|
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
QRectF PushButton::boundingRect() const
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
return QRectF(x(), y(), d->width, d->height);
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
QStyleOptionButton options;
|
|
|
|
options.initFrom(widget);
|
|
|
|
options.state = option->state;
|
2007-03-13 03:51:40 +01:00
|
|
|
options.state |= isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
|
2007-03-05 05:13:22 +01:00
|
|
|
options.rect = boundingRect().toRect();
|
|
|
|
options.text = text();
|
2007-03-13 04:00:09 +01:00
|
|
|
|
|
|
|
if (d->hasIcon)
|
|
|
|
{
|
2007-03-05 13:54:54 +01:00
|
|
|
options.icon= d->icon;
|
|
|
|
options.iconSize = d->iconSize;
|
|
|
|
}
|
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
widget->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &options, painter, widget);
|
|
|
|
widget->style()->drawPrimitive(QStyle::PE_FrameFocusRect, &options, painter, widget);
|
|
|
|
widget-> style()->drawControl(QStyle::CE_PushButton, &options, painter, widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->labelText = text;
|
|
|
|
|
2007-03-13 04:00:09 +01:00
|
|
|
QFont font(text);
|
|
|
|
QFontMetrics fm(font);
|
|
|
|
if (fm.width(text) >= d->width)
|
|
|
|
setWidth(fm.width(text) + 4);
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
2007-03-13 04:00:09 +01:00
|
|
|
QString PushButton::text() const
|
2007-03-05 05:13:22 +01:00
|
|
|
{
|
|
|
|
return d->labelText;
|
|
|
|
}
|
2007-03-13 04:00:09 +01:00
|
|
|
|
|
|
|
int PushButton::height() const
|
2007-03-05 05:13:22 +01:00
|
|
|
{
|
|
|
|
return d->height;
|
|
|
|
}
|
|
|
|
|
2007-03-13 04:00:09 +01:00
|
|
|
int PushButton::width() const
|
2007-03-05 05:13:22 +01:00
|
|
|
{
|
|
|
|
return d->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setHeight(int h)
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
prepareGeometryChange();
|
2007-03-05 05:13:22 +01:00
|
|
|
d->height = h;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setWidth(int w)
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
if (!(w >= d->maxWidth))
|
|
|
|
{
|
|
|
|
prepareGeometryChange ();
|
|
|
|
d->width = w;
|
|
|
|
update();
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-05 13:54:54 +01:00
|
|
|
void PushButton::setIcon(const QString& path)
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
if (!path.isNull())
|
|
|
|
{
|
|
|
|
QPixmap iconPixmap(path);
|
|
|
|
d->icon = QIcon(iconPixmap);
|
|
|
|
d->iconSize = iconPixmap.size();
|
|
|
|
d->hasIcon=true;
|
2007-03-05 13:54:54 +01:00
|
|
|
}
|
2007-03-13 04:00:09 +01:00
|
|
|
else
|
|
|
|
d->hasIcon = false;
|
2007-03-05 13:54:54 +01:00
|
|
|
}
|
2007-03-13 04:00:09 +01:00
|
|
|
|
|
|
|
QSize PushButton::size() const
|
2007-03-05 05:13:22 +01:00
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
return QSize(d->width, d->height);
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setSize(QSize s)
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
prepareGeometryChange();
|
|
|
|
if (!d->maxWidth >= s.width())
|
2007-03-05 05:13:22 +01:00
|
|
|
d->width = s.width();
|
|
|
|
d->height = s.height();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setMaximumWidth(int w)
|
|
|
|
{
|
|
|
|
d->maxWidth= w;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PushButton::isDown()
|
|
|
|
{
|
2007-03-13 04:00:09 +01:00
|
|
|
if (d->state == PushButton::Pressed)
|
|
|
|
return true;
|
2007-03-05 05:13:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::mousePressEvent ( QGraphicsSceneMouseEvent * event )
|
|
|
|
{
|
|
|
|
event->accept();
|
2007-03-13 04:00:09 +01:00
|
|
|
d->state = PushButton::Pressed;
|
2007-03-05 05:13:22 +01:00
|
|
|
update();
|
2007-03-13 04:25:19 +01:00
|
|
|
// emit clicked();
|
2007-03-13 03:51:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
|
|
|
|
{
|
|
|
|
event->accept();
|
2007-03-13 04:25:19 +01:00
|
|
|
d->state = PushButton::Released;
|
2007-03-13 03:51:40 +01:00
|
|
|
update();
|
2007-03-13 04:25:19 +01:00
|
|
|
// emit clicked();
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
2007-03-12 20:16:17 +01:00
|
|
|
|
|
|
|
QSize PushButton::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(d->width,d->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize PushButton::minimumSize() const
|
|
|
|
{
|
|
|
|
return QSize(d->minWidth,d->minHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize PushButton::maximumSize() const
|
|
|
|
{
|
|
|
|
return QSize(d->maxWidth,d->maxHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientations PushButton::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setGeometry(const QRect & r)
|
|
|
|
{
|
|
|
|
setSize(r.size());
|
|
|
|
setPos(r.x(),r.y());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect PushButton::geometry() const
|
|
|
|
{
|
|
|
|
return boundingRect().toRect();
|
|
|
|
}
|
2007-03-13 04:00:09 +01:00
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
} // namespace Plasma
|