2007-03-12 14:50:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 by Siraj Razick siraj@kdemail.net
|
|
|
|
*
|
|
|
|
* 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 "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-05-25 04:27:33 +02:00
|
|
|
: QObject(),
|
|
|
|
QGraphicsItem(parent),
|
|
|
|
d(new Private)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton);
|
|
|
|
setAcceptsHoverEvents(true);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setEnabled(true);
|
|
|
|
d->height = 40;
|
|
|
|
d->width = 100 ;
|
|
|
|
d->maxWidth = 600;
|
|
|
|
setPos(QPointF(0.0,0.0));
|
|
|
|
d->state= Qt::Unchecked;
|
2007-03-13 08:46:12 +01:00
|
|
|
d->labelText=QString("Plasma");
|
2007-03-12 14:50:30 +01:00
|
|
|
d->labelTextColor= QColor(201,201,255);
|
|
|
|
d->hasIcon = false;
|
|
|
|
d->iconSize=QSize(32,32);
|
|
|
|
d->hasMouse = false;
|
|
|
|
d->down = false;
|
|
|
|
d->hovering = false;
|
|
|
|
d->down = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CheckBox::~CheckBox()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF CheckBox::boundingRect() const
|
|
|
|
{
|
2007-07-21 16:04:37 +02:00
|
|
|
return QRectF(0,0,d->width,d->height);
|
2007-03-12 14:50:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2007-05-20 22:36:59 +02:00
|
|
|
Q_UNUSED(option)
|
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();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
widget-> style()->drawControl(QStyle::CE_CheckBox, &options, painter, widget);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-22 04:49:54 +02:00
|
|
|
void CheckBox::updated(const QString&, const DataEngine::Data&)
|
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-03-13 08:46:12 +01:00
|
|
|
int CheckBox::height() const
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
return d->height;
|
|
|
|
}
|
|
|
|
|
2007-03-13 08:46:12 +01:00
|
|
|
int CheckBox::width() const
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
return d->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::setHeight(int h)
|
|
|
|
{
|
|
|
|
prepareGeometryChange ();
|
|
|
|
d->height = h;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::setWidth(int w)
|
|
|
|
{
|
|
|
|
if (!(w >= d->maxWidth)) {
|
|
|
|
prepareGeometryChange ();
|
|
|
|
d->width = w;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-13 08:46:12 +01:00
|
|
|
QSize CheckBox::size() const
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
return QSize(d->width,d->height);
|
|
|
|
}
|
|
|
|
|
2007-03-13 08:46:12 +01:00
|
|
|
void CheckBox::setSize(const QSize &s)
|
2007-03-12 14:50:30 +01:00
|
|
|
{
|
|
|
|
prepareGeometryChange ();
|
|
|
|
if (!d->maxWidth >= s.width() ) {
|
|
|
|
d->width = s.width();
|
|
|
|
}
|
|
|
|
d->height = s.height();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::setMaximumWidth(int w)
|
|
|
|
{
|
|
|
|
d->maxWidth= w;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::setCheckState(Qt::CheckState state)
|
|
|
|
{
|
|
|
|
d->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckBox::mouseMoveEvent (QGraphicsSceneMouseEvent * event)
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->hasMouse= false;
|
|
|
|
|
|
|
|
}
|
|
|
|
void CheckBox::mousePressEvent ( QGraphicsSceneMouseEvent * event )
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
d->down = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckBox::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
|
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
|
|
|
|
if (d->hasMouse) {
|
|
|
|
if (d->state == Qt::Checked) {
|
|
|
|
d->state = Qt::Unchecked;
|
|
|
|
} else {
|
|
|
|
d->state = Qt::Checked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
emit clicked();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckBox::hoverMoveEvent ( QGraphicsSceneHoverEvent * event )
|
|
|
|
{
|
|
|
|
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"
|
|
|
|
|
|
|
|
|