libplasma
checkbox.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "checkbox.h"
00022
00023 #include <QStyleOption>
00024 #include <QStyle>
00025 #include <QWidget>
00026 #include <QPainter>
00027 #include <QGraphicsSceneMouseEvent>
00028
00029 namespace Plasma
00030 {
00031
00032 class CheckBox::Private
00033 {
00034 public:
00035 Private() { }
00036 ~Private() { }
00037 QString labelText;
00038 QString labelIcon;
00039 QColor labelTextColor;
00040 QIcon icon;
00041 QSize iconSize;
00042 bool hasIcon;
00043 bool hasMouse;
00044 int labelTextOpacity;
00045 int height;
00046 int width;
00047 int maxWidth;
00048 int radius;
00049 QTimer * updateTimer;
00050 Qt::CheckState state;
00051 bool down;
00052 bool hovering;
00053 };
00054
00055 CheckBox::CheckBox(QGraphicsItem *parent)
00056 : Plasma::Widget(parent),
00057 d(new Private)
00058 {
00059 init();
00060 }
00061
00062
00063 CheckBox::CheckBox(const QString &text, QGraphicsItem *parent)
00064 : Plasma::Widget(parent),
00065 d(new Private)
00066 {
00067 init();
00068 setText(text);
00069 }
00070
00071 void CheckBox::init()
00072 {
00073 setAcceptedMouseButtons(Qt::LeftButton);
00074 setAcceptsHoverEvents(true);
00075 setAcceptDrops(true);
00076 setEnabled(true);
00077
00078 setPos(QPointF(0.0,0.0));
00079
00080 d->height = 40;
00081 d->width = 100 ;
00082 d->maxWidth = 600;
00083 d->state= Qt::Unchecked;
00084
00085 d->hasIcon = false;
00086 d->iconSize = QSize(32,32);
00087 d->hasMouse = false;
00088 d->down = false;
00089 d->hovering = false;
00090 d->down = false;
00091 }
00092
00093 CheckBox::~CheckBox()
00094 {
00095 delete d;
00096 }
00097
00098 void CheckBox::paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00099 {
00100 Q_UNUSED(option)
00101
00102 QStyleOptionButton options;
00103 options.rect = boundingRect().toRect();
00104 options.text = text();
00105 options.state |= (d->state == Qt::Checked) ? QStyle::State_On : QStyle::State_Off;
00106
00107
00108
00109
00110
00111 if (d->hasMouse) {
00112 options.state |= QStyle::State_MouseOver;
00113 options.state |= QStyle::State_HasFocus;
00114 options.state |= QStyle::State_Sunken;
00115 options.state |= QStyle::State_Raised;
00116 options.state |= QStyle::State_On;
00117 }
00118
00119 widget-> style()->drawControl(QStyle::CE_CheckBox, &options, painter, widget);
00120 }
00121
00122 void CheckBox::dataUpdated(const QString&, const DataEngine::Data& data)
00123 {
00124 foreach (const QVariant& variant, data) {
00125 if (variant.canConvert(QVariant::Bool)) {
00126 setChecked(variant.toBool());
00127 return;
00128 }
00129 }
00130 }
00131
00132 void CheckBox::setText(const QString& text)
00133 {
00134 d->labelText = text;
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 }
00145
00146 QString CheckBox::text() const
00147 {
00148 return d->labelText;
00149 }
00150
00151 Qt::CheckState CheckBox::checkState() const
00152 {
00153 return d->state;
00154 }
00155
00156 void CheckBox::setChecked(bool checked)
00157 {
00158 d->state = checked ? Qt::Checked : Qt::Unchecked;
00159 }
00160
00161 bool CheckBox::isChecked() const
00162 {
00163 return (d->state == Qt::Checked);
00164 }
00165
00166 void CheckBox::setCheckState(Qt::CheckState state)
00167 {
00168 d->state = state;
00169 }
00170
00171 void CheckBox::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
00172 {
00173 event->accept();
00174 d->hasMouse= false;
00175 }
00176
00177 void CheckBox::mousePressEvent(QGraphicsSceneMouseEvent * event)
00178 {
00179 event->accept();
00180 d->down = true;
00181 update();
00182 }
00183
00184 void CheckBox::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
00185 {
00186 event->accept();
00187
00188 if (d->hasMouse) {
00189 if (d->state == Qt::Checked) {
00190 d->state = Qt::Unchecked;
00191 } else {
00192 d->state = Qt::Checked;
00193 }
00194 }
00195
00196 update();
00197
00198 if (sceneBoundingRect().contains(event->scenePos())) {
00199 emit clicked();
00200 }
00201 }
00202
00203
00204 void CheckBox::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
00205 {
00206 event->accept();
00207 d->hasMouse= true;
00208 update();
00209 }
00210
00211
00212 void CheckBox::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
00213 {
00214 event->accept();
00215 d->hasMouse= false;
00216 update();
00217 Widget::hoverEnterEvent(event);
00218 }
00219
00220 void CheckBox::hoverEnterEvent ( QGraphicsSceneHoverEvent * event)
00221 {
00222 event->accept();
00223 d->hasMouse = true;
00224 update();
00225 Widget::hoverLeaveEvent(event);
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 }
00238
00239 #include "checkbox.moc"
00240
00241