File indexing completed on 2024-05-05 05:46:09

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "toggleswitch.h"
0012 
0013 #include "canvasitemparts.h"
0014 #include "ecnode.h"
0015 #include "libraryitem.h"
0016 #include "switch.h"
0017 
0018 #include <KLocalizedString>
0019 #include <QPainter>
0020 
0021 // BEGIN class ECDPDT
0022 Item *ECDPDT::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0023 {
0024     return new ECDPDT(static_cast<ICNDocument *>(itemDocument), newItem, id);
0025 }
0026 
0027 LibraryItem *ECDPDT::libraryItem()
0028 {
0029     return new LibraryItem(QStringList(QString("ec/dpdt_toggle")), i18n("DPDT"), i18n("Switches"), "dpdt.png", LibraryItem::lit_component, ECDPDT::construct);
0030 }
0031 
0032 ECDPDT::ECDPDT(ICNDocument *icnDocument, bool newItem, const char *id)
0033     : Component(icnDocument, newItem, id ? id : "dpdt_toggle")
0034 {
0035     m_name = i18n("DPDT Toggle");
0036     setSize(-16, -32, 32, 64);
0037 
0038     addButton("button", QRect(-16, 32, 32, 20), "", true);
0039 
0040     createProperty("button_text", Variant::Type::String);
0041     property("button_text")->setCaption(i18n("Button Text"));
0042 
0043     Variant *v = createProperty("bounce", Variant::Type::Bool);
0044     v->setCaption("Bounce");
0045     v->setAdvanced(true);
0046     v->setValue(false);
0047 
0048     v = createProperty("bounce_period", Variant::Type::Double);
0049     v->setCaption("Bounce Period");
0050     v->setAdvanced(true);
0051     v->setUnit("s");
0052     v->setValue(5e-3);
0053 
0054     init4PinRight(-24, -8, 8, 24);
0055 
0056     init2PinLeft(-16, 16);
0057 
0058     m_switch1 = createSwitch(m_pNNode[0], m_pPNode[0], false);
0059     m_switch2 = createSwitch(m_pNNode[0], m_pPNode[1], true);
0060     m_switch3 = createSwitch(m_pNNode[1], m_pPNode[2], false);
0061     m_switch4 = createSwitch(m_pNNode[1], m_pPNode[3], true);
0062     pressed = false;
0063 }
0064 
0065 ECDPDT::~ECDPDT()
0066 {
0067 }
0068 
0069 void ECDPDT::dataChanged()
0070 {
0071     button("button")->setText(dataString("button_text"));
0072 
0073     bool bounce = dataBool("bounce");
0074     int bouncePeriod_ms = int(dataDouble("bounce_period") * 1e3);
0075 
0076     m_switch1->setBounce(bounce, bouncePeriod_ms);
0077     m_switch2->setBounce(bounce, bouncePeriod_ms);
0078     m_switch3->setBounce(bounce, bouncePeriod_ms);
0079     m_switch4->setBounce(bounce, bouncePeriod_ms);
0080 }
0081 
0082 void ECDPDT::drawShape(QPainter &p)
0083 {
0084     initPainter(p);
0085 
0086     int _x = int(x()) - 16;
0087     int _y = int(y()) - 32;
0088     const int radius = 2;
0089 
0090     p.drawEllipse(_x, _y + 15, 2 * radius, 2 * radius);
0091     p.drawEllipse(_x, _y + 47, 2 * radius, 2 * radius);
0092     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 7, 2 * radius, 2 * radius);
0093     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 23, 2 * radius, 2 * radius);
0094     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 39, 2 * radius, 2 * radius);
0095     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 55, 2 * radius, 2 * radius);
0096 
0097     const int dy = pressed ? 6 : -6;
0098 
0099     p.drawLine(_x + 2 * radius, _y + 16, _x + width() - 2 * radius + 2, _y + 16 + dy);
0100     p.drawLine(_x + 2 * radius, _y + 48, _x + width() - 2 * radius + 2, _y + 48 + dy);
0101 
0102     deinitPainter(p);
0103 }
0104 
0105 void ECDPDT::buttonStateChanged(const QString &, bool state)
0106 {
0107     pressed = state;
0108     m_switch1->setState(state ? Switch::Open : Switch::Closed);
0109     m_switch2->setState(state ? Switch::Closed : Switch::Open);
0110     m_switch3->setState(state ? Switch::Open : Switch::Closed);
0111     m_switch4->setState(state ? Switch::Closed : Switch::Open);
0112 }
0113 // END class ECDPDT
0114 
0115 // BEGIN class ECDPST
0116 Item *ECDPST::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0117 {
0118     return new ECDPST(static_cast<ICNDocument *>(itemDocument), newItem, id);
0119 }
0120 
0121 LibraryItem *ECDPST::libraryItem()
0122 {
0123     return new LibraryItem(QStringList(QString("ec/dpst_toggle")), i18n("DPST"), i18n("Switches"), "dpst.png", LibraryItem::lit_component, ECDPST::construct);
0124 }
0125 
0126 ECDPST::ECDPST(ICNDocument *icnDocument, bool newItem, const char *id)
0127     : Component(icnDocument, newItem, id ? id : "dpst_toggle")
0128 {
0129     m_name = i18n("DPST Toggle");
0130     setSize(-16, -16, 32, 32);
0131 
0132     addButton("button", QRect(-16, 16, 32, 20), "", true);
0133 
0134     createProperty("button_text", Variant::Type::String);
0135     property("button_text")->setCaption(i18n("Button Text"));
0136 
0137     Variant *v = createProperty("bounce", Variant::Type::Bool);
0138     v->setCaption("Bounce");
0139     v->setAdvanced(true);
0140     v->setValue(false);
0141 
0142     v = createProperty("bounce_period", Variant::Type::Double);
0143     v->setCaption("Bounce Period");
0144     v->setAdvanced(true);
0145     v->setUnit("s");
0146     v->setValue(5e-3);
0147 
0148     init2PinLeft(-8, 8);
0149     init2PinRight(-8, 8);
0150 
0151     m_switch1 = createSwitch(m_pPNode[0], m_pNNode[0], true);
0152     m_switch2 = createSwitch(m_pPNode[1], m_pNNode[1], true);
0153     pressed = false;
0154 }
0155 
0156 ECDPST::~ECDPST()
0157 {
0158 }
0159 
0160 void ECDPST::dataChanged()
0161 {
0162     button("button")->setText(dataString("button_text"));
0163 
0164     bool bounce = dataBool("bounce");
0165     int bouncePeriod_ms = int(dataDouble("bounce_period") * 1e3);
0166 
0167     m_switch1->setBounce(bounce, bouncePeriod_ms);
0168     m_switch2->setBounce(bounce, bouncePeriod_ms);
0169 }
0170 
0171 void ECDPST::drawShape(QPainter &p)
0172 {
0173     initPainter(p);
0174 
0175     int _x = int(x()) - 16;
0176     int _y = int(y()) - 16;
0177     const int radius = 2;
0178 
0179     p.drawEllipse(_x, _y + 6, 2 * radius, 2 * radius);
0180     p.drawEllipse(_x, _y + 22, 2 * radius, 2 * radius);
0181     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 6, 2 * radius, 2 * radius);
0182     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 22, 2 * radius, 2 * radius);
0183 
0184     const int dy = pressed ? 6 : 0;
0185 
0186     p.drawLine(_x + 2 * radius, _y + 7, _x + width() - 2 * radius, _y + 1 + dy);
0187     p.drawLine(_x + 2 * radius, _y + 24, _x + width() - 2 * radius, _y + 18 + dy);
0188 
0189     deinitPainter(p);
0190 }
0191 
0192 void ECDPST::buttonStateChanged(const QString &, bool state)
0193 {
0194     m_switch1->setState(state ? Switch::Closed : Switch::Open);
0195     m_switch2->setState(state ? Switch::Closed : Switch::Open);
0196     pressed = state;
0197 }
0198 // END class ECDPST
0199 
0200 // BEGIN class ECSPDT
0201 Item *ECSPDT::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0202 {
0203     return new ECSPDT(static_cast<ICNDocument *>(itemDocument), newItem, id);
0204 }
0205 
0206 LibraryItem *ECSPDT::libraryItem()
0207 {
0208     return new LibraryItem(QStringList(QString("ec/spdt_toggle")), i18n("SPDT"), i18n("Switches"), "spdt.png", LibraryItem::lit_component, ECSPDT::construct);
0209 }
0210 
0211 ECSPDT::ECSPDT(ICNDocument *icnDocument, bool newItem, const char *id)
0212     : Component(icnDocument, newItem, id ? id : "spdt_toggle")
0213 {
0214     m_name = i18n("SPDT Toggle");
0215     setSize(-16, -16, 32, 32);
0216 
0217     addButton("button", QRect(-16, 16, width(), 20), "", true);
0218 
0219     createProperty("button_text", Variant::Type::String);
0220     property("button_text")->setCaption(i18n("Button Text"));
0221 
0222     Variant *v = createProperty("bounce", Variant::Type::Bool);
0223     v->setCaption("Bounce");
0224     v->setAdvanced(true);
0225     v->setValue(false);
0226 
0227     v = createProperty("bounce_period", Variant::Type::Double);
0228     v->setCaption("Bounce Period");
0229     v->setAdvanced(true);
0230     v->setUnit("s");
0231     v->setValue(5e-3);
0232 
0233     init1PinLeft(0);
0234     init2PinRight(-8, 8);
0235 
0236     m_switch1 = createSwitch(m_pNNode[0], m_pPNode[0], false);
0237     m_switch2 = createSwitch(m_pNNode[0], m_pPNode[1], true);
0238 
0239     pressed = false;
0240 }
0241 
0242 ECSPDT::~ECSPDT()
0243 {
0244 }
0245 
0246 void ECSPDT::dataChanged()
0247 {
0248     button("button")->setText(dataString("button_text"));
0249 
0250     bool bounce = dataBool("bounce");
0251     int bouncePeriod_ms = int(dataDouble("bounce_period") * 1e3);
0252 
0253     m_switch1->setBounce(bounce, bouncePeriod_ms);
0254     m_switch2->setBounce(bounce, bouncePeriod_ms);
0255 }
0256 
0257 void ECSPDT::drawShape(QPainter &p)
0258 {
0259     initPainter(p);
0260 
0261     int _x = int(x()) - 16;
0262     int _y = int(y()) - 16;
0263     const int radius = 2;
0264 
0265     p.drawEllipse(_x, _y + 15, 2 * radius, 2 * radius);
0266     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 6, 2 * radius, 2 * radius);
0267     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 22, 2 * radius, 2 * radius);
0268 
0269     const int dy = pressed ? 21 : 10;
0270     p.drawLine(_x + 2 * radius, _y + 16, _x + width() - 2 * radius + 2, _y + dy);
0271 
0272     deinitPainter(p);
0273 }
0274 
0275 void ECSPDT::buttonStateChanged(const QString &, bool state)
0276 {
0277     pressed = state;
0278     m_switch1->setState(state ? Switch::Open : Switch::Closed);
0279     m_switch2->setState(state ? Switch::Closed : Switch::Open);
0280 }
0281 // END class ECSPDT
0282 
0283 // BEGIN class ECSPST
0284 Item *ECSPST::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0285 {
0286     return new ECSPST(static_cast<ICNDocument *>(itemDocument), newItem, id);
0287 }
0288 
0289 LibraryItem *ECSPST::libraryItem()
0290 {
0291     return new LibraryItem(QStringList(QString("ec/spst_toggle")), i18n("SPST"), i18n("Switches"), "spst.png", LibraryItem::lit_component, ECSPST::construct);
0292 }
0293 
0294 ECSPST::ECSPST(ICNDocument *icnDocument, bool newItem, const char *id)
0295     : Component(icnDocument, newItem, id ? id : "spst_toggle")
0296 {
0297     m_name = i18n("SPST Toggle");
0298     setSize(-16, -8, 32, 16);
0299     pressed = false;
0300 
0301     addButton("button", QRect(-16, 8, width(), 20), "", true);
0302 
0303     createProperty("button_text", Variant::Type::String);
0304     property("button_text")->setCaption(i18n("Button Text"));
0305 
0306     Variant *v = createProperty("bounce", Variant::Type::Bool);
0307     v->setCaption("Bounce");
0308     v->setAdvanced(true);
0309     v->setValue(false);
0310 
0311     v = createProperty("bounce_period", Variant::Type::Double);
0312     v->setCaption("Bounce Period");
0313     v->setAdvanced(true);
0314     v->setUnit("s");
0315     v->setValue(5e-3);
0316 
0317     button("button")->setState(pressed);
0318 
0319     init1PinLeft();
0320     init1PinRight();
0321 
0322     m_switch = createSwitch(m_pNNode[0], m_pPNode[0], !pressed);
0323 }
0324 
0325 ECSPST::~ECSPST()
0326 {
0327 }
0328 
0329 void ECSPST::dataChanged()
0330 {
0331     button("button")->setText(dataString("button_text"));
0332 
0333     bool bounce = dataBool("bounce");
0334     int bouncePeriod_ms = int(dataDouble("bounce_period") * 1e3);
0335     m_switch->setBounce(bounce, bouncePeriod_ms);
0336 }
0337 
0338 void ECSPST::drawShape(QPainter &p)
0339 {
0340     initPainter(p);
0341 
0342     int _x = int(x()) - 16;
0343     int _y = int(y()) - 8;
0344     const int radius = 2;
0345 
0346     p.drawEllipse(_x, _y + 7, 2 * radius, 2 * radius);
0347     p.drawEllipse(_x + width() - 2 * radius + 1, _y + 7, 2 * radius, 2 * radius);
0348     const int dy = pressed ? 0 : -6;
0349     p.drawLine(_x + 2 * radius, _y + 8, _x + width() - 2 * radius, _y + 8 + dy);
0350 
0351     deinitPainter(p);
0352 }
0353 
0354 void ECSPST::buttonStateChanged(const QString &, bool state)
0355 {
0356     pressed = state;
0357     m_switch->setState(state ? Switch::Closed : Switch::Open);
0358 }
0359 // END class ECSPST