|
- #include "button.h"
- #include <QPainter>
- #include <QStyleOptionGraphicsItem>
- #include <QMenu>
-
- Button::Button(const QString &type) : Item(type)
- {
-
- }
-
- QRectF Button::boundingRect() const
- {
- return QRectF(0, 0, 50, 50);
- }
-
- void Button::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
- {
- painter->setRenderHint(QPainter::Antialiasing);
-
- if (type_ == "按钮") {
- painter->drawRect(0,0,50,50);
- }
- if (!registerId_.isEmpty()) {
- painter->save();
- painter->setFont(QFont("Arial", 8));
- painter->setPen(Qt::black);
- QString text;
- if (registerValue_ > 0)
- {
- text = QString("%1\nON").arg(registerId());
- }
- else
- {
- text = QString("%1\nOFF").arg(registerId());
- }
- painter->drawText(QRectF(13, 13, 26, 26), Qt::AlignCenter, text);
- painter->restore();
- }
- if (option->state & QStyle::State_Selected) {
- QPen pen(Qt::DashLine);
- pen.setColor(Qt::blue);
- pen.setWidth(2);
- painter->setPen(pen);
- painter->setBrush(Qt::NoBrush);
- painter->drawRect(boundingRect());
- }
- }
-
- void Button::addMenuActions(QMenu *menu)
- {
- menu->addAction("置ON");
- menu->addAction("置OFF");
- }
-
- void Button::handleMenuAction(QAction *action)
- {
- if (action->text() == "复制") {
- emit requestCopy(this);
- }
- if (action->text() == "删除") {
- emit requestDelete(this);
- }
- if (action->text() == "对象"){
- emit requestBindRegister(this);
- }
- if (action->text() == "重置"){
- emit requestReset(this);
- }
- if (action->text() == "置ON"){
- emit requestSetON(this, true);
- }
- if (action->text() == "置OFF"){
- emit requestSetON(this, false);
- }
- }
|