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.
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:28:03 +02:00
|
|
|
#include "pushbutton.h"
|
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
#include <QStyleOptionFrameV2>
|
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2007-07-23 00:16:40 +02:00
|
|
|
#include <QFontMetricsF>
|
|
|
|
#include <QApplication>
|
|
|
|
|
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 radius;
|
2007-07-23 00:16:40 +02:00
|
|
|
QTimer *updateTimer;
|
2007-03-13 04:00:09 +01:00
|
|
|
PushButton::ButtonState state;
|
2007-03-05 05:13:22 +01:00
|
|
|
};
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
PushButton::PushButton(Widget *parent)
|
2007-05-25 04:27:33 +02:00
|
|
|
: QObject(),
|
2007-07-23 00:16:40 +02:00
|
|
|
Widget(parent),
|
2007-05-25 04:27:33 +02:00
|
|
|
d(new Private)
|
2007-03-05 05:13:22 +01:00
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setEnabled(true);
|
2007-03-13 04:00:09 +01:00
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
resize(40.0f, 100.0f);
|
2007-03-05 05:13:22 +01:00
|
|
|
setPos(QPointF(0.0,0.0));
|
2007-07-23 00:16:40 +02:00
|
|
|
|
|
|
|
/*FIXME: Don't use hardcoded strings and colors. */
|
|
|
|
|
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-07-23 00:16:40 +02: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-05-22 04:49:54 +02:00
|
|
|
void PushButton::updated(const QString&, const DataEngine::Data &data)
|
2007-03-13 04:25:19 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(data)
|
|
|
|
}
|
|
|
|
|
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);
|
2007-07-23 00:16:40 +02:00
|
|
|
widget->style()->drawControl(QStyle::CE_PushButton, &options, painter, widget);
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->labelText = text;
|
2007-03-13 17:16:26 +01:00
|
|
|
/*
|
|
|
|
QFont * font = new QFont (text);
|
|
|
|
QFontMetrics * fm = new QFontMetrics(*font);
|
|
|
|
if (fm->width(text) >= d->width)
|
|
|
|
setWidth(fm->width(d->labelText) + 4);
|
|
|
|
delete fm;
|
|
|
|
delete font;
|
|
|
|
*/
|
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
|
|
|
|
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
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
void PushButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2007-03-05 05:13:22 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
void PushButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2007-03-13 03:51:40 +01:00
|
|
|
{
|
|
|
|
event->accept();
|
2007-06-20 21:13:30 +02:00
|
|
|
if (d->state == PushButton::Pressed)
|
|
|
|
emit clicked();
|
2007-03-13 04:25:19 +01:00
|
|
|
d->state = PushButton::Released;
|
2007-03-13 03:51:40 +01:00
|
|
|
update();
|
2007-03-05 05:13:22 +01:00
|
|
|
}
|
2007-03-12 20:16:17 +01:00
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
QSizeF PushButton::sizeHint() const
|
2007-03-12 20:16:17 +01:00
|
|
|
{
|
2007-07-23 00:16:40 +02:00
|
|
|
return minimumSize();
|
2007-03-12 20:16:17 +01:00
|
|
|
}
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
QSizeF PushButton::minimumSize() const
|
2007-03-12 20:16:17 +01:00
|
|
|
{
|
2007-07-23 00:16:40 +02:00
|
|
|
QFontMetricsF m = qApp->fontMetrics();
|
2007-03-12 20:16:17 +01:00
|
|
|
|
2007-07-23 11:32:54 +02:00
|
|
|
return m.boundingRect(text()).size() + QSizeF(5.0f, 5.0f);
|
2007-03-12 20:16:17 +01:00
|
|
|
}
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
QSizeF PushButton::maximumSize() const
|
2007-03-12 20:16:17 +01:00
|
|
|
{
|
2007-07-23 00:16:40 +02:00
|
|
|
return QSizeF();
|
2007-03-12 20:16:17 +01:00
|
|
|
}
|
|
|
|
|
2007-07-23 00:16:40 +02:00
|
|
|
Qt::Orientations PushButton::expandingDirections() const
|
2007-03-12 20:16:17 +01:00
|
|
|
{
|
2007-07-23 00:16:40 +02:00
|
|
|
return Qt::Horizontal;
|
2007-03-12 20:16:17 +01:00
|
|
|
}
|
2007-03-13 04:00:09 +01:00
|
|
|
|
2007-07-20 10:06:27 +02:00
|
|
|
bool PushButton::isEmpty() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-05 05:13:22 +01:00
|
|
|
} // namespace Plasma
|