File indexing completed on 2024-05-05 05:46:08
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 "pushswitch.h" 0012 #include "canvasitemparts.h" 0013 #include "libraryitem.h" 0014 #include "switch.h" 0015 0016 #include <KLocalizedString> 0017 #include <QPainter> 0018 #include <QPixmap> 0019 #include <QPoint> 0020 //#include <q3pointarray.h> 0021 0022 // BEGIN class ECPTBSwitch 0023 Item *ECPTBSwitch::construct(ItemDocument *itemDocument, bool newItem, const char *id) 0024 { 0025 return new ECPTBSwitch(static_cast<ICNDocument *>(itemDocument), newItem, id); 0026 } 0027 0028 LibraryItem *ECPTBSwitch::libraryItem() 0029 { 0030 return new LibraryItem(QStringList(QString("ec/ptb_switch")), i18n("Push-to-Break"), i18n("Switches"), "ptb.png", LibraryItem::lit_component, ECPTBSwitch::construct); 0031 } 0032 0033 ECPTBSwitch::ECPTBSwitch(ICNDocument *icnDocument, bool newItem, const char *id) 0034 : Component(icnDocument, newItem, (id) ? id : "ptb_switch") 0035 { 0036 m_name = i18n("Push to Break"); 0037 setSize(-16, -16, 32, 24); 0038 0039 addButton("button", QRect(-16, 8, 32, 20), ""); 0040 0041 createProperty("button_text", Variant::Type::String); 0042 property("button_text")->setCaption(i18n("Button Text")); 0043 0044 Variant *v = createProperty("bounce", Variant::Type::Bool); 0045 v->setCaption(i18n("Bounce")); 0046 v->setAdvanced(true); 0047 v->setValue(false); 0048 0049 v = createProperty("bounce_period", Variant::Type::Double); 0050 v->setCaption(i18n("Bounce Period")); 0051 v->setAdvanced(true); 0052 v->setUnit("s"); 0053 v->setValue(5e-3); 0054 0055 init1PinLeft(0); 0056 init1PinRight(0); 0057 0058 m_switch = createSwitch(m_pPNode[0], m_pNNode[0], false); 0059 pressed = false; 0060 } 0061 0062 ECPTBSwitch::~ECPTBSwitch() 0063 { 0064 } 0065 0066 void ECPTBSwitch::dataChanged() 0067 { 0068 button("button")->setText(dataString("button_text")); 0069 0070 bool bounce = dataBool("bounce"); 0071 int bouncePeriod_ms = int(dataDouble("bounce_period") * 1e3); 0072 m_switch->setBounce(bounce, bouncePeriod_ms); 0073 } 0074 0075 void ECPTBSwitch::drawShape(QPainter &p) 0076 { 0077 initPainter(p); 0078 0079 int _x = int(x()) - 16; 0080 int _y = int(y()) - 8; 0081 const int radius = 2; 0082 const int _height = height() - 8; 0083 0084 int dy = pressed ? 6 : 4; 0085 0086 p.drawLine(_x + width() / 4, _y + dy, _x + (3 * width()) / 4, _y + dy); // Top horizontal line 0087 p.drawLine(_x, _y + (_height / 2) - radius + dy, _x + width(), _y + (_height / 2) - radius + dy); // Bottom horizontal line 0088 p.drawLine(_x + width() / 2, _y + dy, _x + width() / 2, _y + (_height / 2) - radius + dy); // Vertical line 0089 0090 p.drawEllipse(_x, _y + (_height / 2) - radius, 2 * radius, 2 * radius); // Left circle 0091 p.drawEllipse(_x + width() - 2 * radius + 1, _y + (_height / 2) - radius, 2 * radius, 2 * radius); // Right circle 0092 0093 deinitPainter(p); 0094 } 0095 0096 void ECPTBSwitch::buttonStateChanged(const QString &id, bool state) 0097 { 0098 if (id != "button") 0099 return; 0100 m_switch->setState(state ? Switch::Open : Switch::Closed); 0101 pressed = state; 0102 } 0103 // END class ECPTBSwitch 0104 0105 // BEGIN class ECPTMSwitch 0106 Item *ECPTMSwitch::construct(ItemDocument *itemDocument, bool newItem, const char *id) 0107 { 0108 return new ECPTMSwitch(static_cast<ICNDocument *>(itemDocument), newItem, id); 0109 } 0110 0111 LibraryItem *ECPTMSwitch::libraryItem() 0112 { 0113 return new LibraryItem(QStringList(QString("ec/ptm_switch")), i18n("Push-to-Make"), i18n("Switches"), "ptm.png", LibraryItem::lit_component, ECPTMSwitch::construct); 0114 } 0115 0116 ECPTMSwitch::ECPTMSwitch(ICNDocument *icnDocument, bool newItem, const char *id) 0117 : Component(icnDocument, newItem, (id) ? id : "ptm_switch") 0118 { 0119 m_name = i18n("Push to Make"); 0120 setSize(-16, -16, 32, 24); 0121 0122 addButton("button", QRect(-16, 8, 32, 20), ""); 0123 0124 createProperty("button_text", Variant::Type::String); 0125 property("button_text")->setCaption(i18n("Button Text")); 0126 0127 Variant *v = createProperty("bounce", Variant::Type::Bool); 0128 v->setCaption("Bounce"); 0129 v->setAdvanced(true); 0130 v->setValue(false); 0131 0132 v = createProperty("bounce_period", Variant::Type::Double); 0133 v->setCaption("Bounce Period"); 0134 v->setAdvanced(true); 0135 v->setUnit("s"); 0136 v->setValue(5e-3); 0137 0138 init1PinLeft(0); 0139 init1PinRight(0); 0140 0141 m_switch = createSwitch(m_pPNode[0], m_pNNode[0], true); 0142 pressed = false; 0143 } 0144 0145 ECPTMSwitch::~ECPTMSwitch() 0146 { 0147 } 0148 0149 void ECPTMSwitch::dataChanged() 0150 { 0151 button("button")->setText(dataString("button_text")); 0152 0153 bool bounce = dataBool("bounce"); 0154 int bouncePeriod_ms = int(dataDouble("bounce_period") * 1e3); 0155 m_switch->setBounce(bounce, bouncePeriod_ms); 0156 } 0157 0158 void ECPTMSwitch::drawShape(QPainter &p) 0159 { 0160 initPainter(p); 0161 0162 int _x = int(x()) - 16; 0163 int _y = int(y()) - 8; 0164 const int radius = 2; 0165 const int _height = height() - 8; 0166 0167 int dy = pressed ? 1 : 3; 0168 0169 p.drawLine(_x + width() / 4, _y - dy, _x + (3 * width()) / 4, _y - dy); // Top horizontal line 0170 p.drawLine(_x, _y + (_height / 2) - radius - dy, _x + width(), _y + (_height / 2) - radius - dy); // Bottom horizontal line 0171 p.drawLine(_x + width() / 2, _y - dy, _x + width() / 2, _y + (_height / 2) - radius - dy); // Vertical line 0172 0173 p.drawEllipse(_x, _y + (_height / 2) - radius, 2 * radius, 2 * radius); // Left circle 0174 p.drawEllipse(_x + width() - 2 * radius + 1, _y + (_height / 2) - radius, 2 * radius, 2 * radius); // Right circle 0175 0176 deinitPainter(p); 0177 } 0178 0179 void ECPTMSwitch::buttonStateChanged(const QString &id, bool state) 0180 { 0181 if (id != "button") 0182 return; 0183 m_switch->setState(state ? Switch::Closed : Switch::Open); 0184 pressed = state; 0185 } 0186 // END class ECPTMSwitch