2007-03-12 14:50:30 +01:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 by Siraj Razick <siraj@kde.org>
|
2007-03-12 14:50:30 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
*
|
|
|
|
* 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 "checkbox.h"
|
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
#include <QStyleOption>
|
|
|
|
#include <QStyle>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class CheckBox::Private
|
|
|
|
{
|
|
|
|
public:
|
2007-03-20 15:21:56 +01:00
|
|
|
Private() { }
|
|
|
|
~Private() { }
|
2007-03-12 14:50:30 +01:00
|
|
|
QString labelText;
|
|
|
|
QString labelIcon;
|
|
|
|
QColor labelTextColor;
|
|
|
|
QIcon icon;
|
|
|
|
QSize iconSize;
|
|
|
|
bool hasIcon;
|
|
|
|
bool hasMouse;
|
|
|
|
int labelTextOpacity;
|
|
|
|
int height;
|
|
|
|
int width;
|
|
|
|
int maxWidth;
|
|
|
|
int radius;
|
|
|
|
QTimer * updateTimer;
|
|
|
|
Qt::CheckState state;
|
|
|
|
bool down;
|
|
|
|
bool hovering;
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckBox::CheckBox(QGraphicsItem *parent)
|
2007-08-03 18:00:10 +02:00
|
|
|
: Plasma::Widget(parent),
|
2007-05-25 04:27:33 +02:00
|
|
|
d(new Private)
|
2007-08-03 18:00:10 +02:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CheckBox::CheckBox(const QString &text, QGraphicsItem *parent)
|
|
|
|
: Plasma::Widget(parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
setText(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::init()
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setEnabled(true);
|
2007-08-03 18:00:10 +02:00
|
|
|
|
|
|
|
setPos(QPointF(0.0,0.0));
|
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
d->height = 40;
|
|
|
|
d->width = 100 ;
|
|
|
|
d->maxWidth = 600;
|
|
|
|
d->state= Qt::Unchecked;
|
2007-08-03 18:00:10 +02:00
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
d->hasIcon = false;
|
2007-08-03 18:00:10 +02:00
|
|
|
d->iconSize = QSize(32,32);
|
2007-03-12 14:50:30 +01:00
|
|
|
d->hasMouse = false;
|
|
|
|
d->down = false;
|
|
|
|
d->hovering = false;
|
|
|
|
d->down = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckBox::~CheckBox()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2007-08-03 18:00:10 +02:00
|
|
|
void CheckBox::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
2007-05-20 22:36:59 +02:00
|
|
|
Q_UNUSED(option)
|
2007-08-03 18:00:10 +02:00
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
QStyleOptionButton options;
|
2007-03-13 02:53:38 +01:00
|
|
|
options.rect = boundingRect().toRect();
|
2007-03-12 14:50:30 +01:00
|
|
|
options.text = text();
|
2007-08-03 18:00:10 +02:00
|
|
|
options.state |= (d->state == Qt::Checked) ? QStyle::State_On : QStyle::State_Off;
|
2007-05-25 04:27:33 +02:00
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
//if (d->down) {
|
|
|
|
// options.state |= QStyle::State_Sunken;
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (d->hasMouse) {
|
|
|
|
options.state |= QStyle::State_MouseOver;
|
|
|
|
options.state |= QStyle::State_HasFocus;
|
|
|
|
options.state |= QStyle::State_Sunken;
|
|
|
|
options.state |= QStyle::State_Raised;
|
|
|
|
options.state |= QStyle::State_On;
|
|
|
|
}
|
2007-08-03 18:00:10 +02:00
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
widget-> style()->drawControl(QStyle::CE_CheckBox, &options, painter, widget);
|
|
|
|
}
|
|
|
|
|
2007-11-06 08:20:08 +01:00
|
|
|
void CheckBox::dataUpdated(const QString&, const DataEngine::Data& data)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
2007-07-28 12:06:03 +02:00
|
|
|
foreach (const QVariant& variant, data) {
|
|
|
|
if (variant.canConvert(QVariant::Bool)) {
|
|
|
|
setChecked(variant.toBool());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-03-12 14:50:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::setText(const QString& text)
|
|
|
|
{
|
|
|
|
d->labelText = text;
|
2007-03-13 17:30:48 +01:00
|
|
|
/*
|
2007-03-12 14:50:30 +01:00
|
|
|
QFont * _font = new QFont ( text );
|
|
|
|
QFontMetrics fm ( *_font );
|
|
|
|
if ( fm.width(text) >= d->width ) {
|
|
|
|
setWidth(fm.width(text)+4);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete _font;
|
2007-03-13 17:30:48 +01:00
|
|
|
*/
|
2007-03-12 14:50:30 +01:00
|
|
|
}
|
|
|
|
|
2007-03-13 08:46:12 +01:00
|
|
|
QString CheckBox::text() const
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
return d->labelText;
|
|
|
|
}
|
|
|
|
|
2007-07-28 12:06:03 +02:00
|
|
|
Qt::CheckState CheckBox::checkState() const
|
|
|
|
{
|
|
|
|
return d->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::setChecked(bool checked)
|
|
|
|
{
|
|
|
|
d->state = checked ? Qt::Checked : Qt::Unchecked;
|
|
|
|
}
|
|
|
|
|
2007-08-03 21:17:30 +02:00
|
|
|
bool CheckBox::isChecked() const
|
|
|
|
{
|
|
|
|
return (d->state == Qt::Checked);
|
|
|
|
}
|
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
void CheckBox::setCheckState(Qt::CheckState state)
|
|
|
|
{
|
|
|
|
d->state = state;
|
|
|
|
}
|
|
|
|
|
2007-07-28 12:06:03 +02:00
|
|
|
void CheckBox::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->hasMouse= false;
|
|
|
|
}
|
2007-07-28 12:06:03 +02:00
|
|
|
|
|
|
|
void CheckBox::mousePressEvent(QGraphicsSceneMouseEvent * event)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->down = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2007-07-28 12:06:03 +02:00
|
|
|
void CheckBox::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
event->accept();
|
2007-07-28 12:06:03 +02:00
|
|
|
|
2007-03-12 14:50:30 +01:00
|
|
|
if (d->hasMouse) {
|
2007-07-28 12:06:03 +02:00
|
|
|
if (d->state == Qt::Checked) {
|
|
|
|
d->state = Qt::Unchecked;
|
|
|
|
} else {
|
|
|
|
d->state = Qt::Checked;
|
2007-03-12 14:50:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
emit clicked();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-28 12:06:03 +02:00
|
|
|
void CheckBox::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->hasMouse= true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckBox::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->hasMouse= false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::hoverEnterEvent ( QGraphicsSceneHoverEvent * event)
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->hasMouse = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void CheckBox::dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Plasma
|
2007-05-25 04:27:33 +02:00
|
|
|
|
|
|
|
#include "checkbox.moc"
|
|
|
|
|
|
|
|
|