File indexing completed on 2024-05-05 05:46:05
0001 /*************************************************************************** 0002 * Copyright (C) 2003-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 "ecpotentiometer.h" 0012 #include "canvasitemparts.h" 0013 #include "ecnode.h" 0014 #include "libraryitem.h" 0015 #include "resistance.h" 0016 0017 #include <KLocalizedString> 0018 #include <QPainter> 0019 #include <QStyle> 0020 0021 Item *ECPotentiometer::construct(ItemDocument *itemDocument, bool newItem, const char *id) 0022 { 0023 return new ECPotentiometer(static_cast<ICNDocument *>(itemDocument), newItem, id); 0024 } 0025 0026 LibraryItem *ECPotentiometer::libraryItem() 0027 { 0028 return new LibraryItem(QStringList(QString("ec/potentiometer")), i18n("Potentiometer"), i18n("Passive"), "potentiometer.png", LibraryItem::lit_component, ECPotentiometer::construct); 0029 } 0030 0031 ECPotentiometer::ECPotentiometer(ICNDocument *icnDocument, bool newItem, const char *id) 0032 : Component(icnDocument, newItem, id ? id : "potentiometer") 0033 { 0034 m_name = i18n("Potentiometer"); 0035 setSize(-16, -16, 40, 32); 0036 0037 m_p1 = createPin(32, 0, 180, "p1"); 0038 0039 m_sliderProp = 0.0; 0040 m_resistance = 5000.; 0041 m_r1 = createResistance(createPin(-8, -24, 90, "n1"), m_p1, 1.); 0042 m_r2 = createResistance(createPin(-8, 24, 270, "n2"), m_p1, 1.); 0043 0044 Slider *s = addSlider("slider", 0, 100, 5, 50, Qt::Vertical, QRect(0, -16, 16, 32)); 0045 m_pSlider = static_cast<QSlider *>(s->widget()); 0046 0047 createProperty("resistance", Variant::Type::Double); 0048 property("resistance")->setCaption(i18n("Resistance")); 0049 property("resistance")->setUnit(QChar(0x3a9)); 0050 property("resistance")->setMinValue(1e-6); 0051 property("resistance")->setValue(1e5); 0052 0053 addDisplayText("res", QRect(-56, -8, 40, 16), ""); 0054 } 0055 0056 ECPotentiometer::~ECPotentiometer() 0057 { 0058 } 0059 0060 void ECPotentiometer::dataChanged() 0061 { 0062 m_resistance = dataDouble("resistance"); 0063 0064 QString display = QString::number(m_resistance / getMultiplier(m_resistance), 'g', 3) + getNumberMag(m_resistance) + QChar(0x3a9); 0065 setDisplayText("res", display); 0066 0067 sliderValueChanged("slider", slider("slider")->value()); 0068 } 0069 0070 void ECPotentiometer::sliderValueChanged(const QString &id, int newValue) 0071 { 0072 if (id != "slider") 0073 return; 0074 0075 m_sliderProp = (newValue - 50.0) / 100.0; 0076 0077 m_r1->setResistance(m_resistance * double(newValue) / 100.); 0078 m_r2->setResistance(m_resistance * double(100. - newValue) / 100.); 0079 } 0080 0081 void ECPotentiometer::drawShape(QPainter &p) 0082 { 0083 initPainter(p); 0084 int _x = int(x()); 0085 int _y = int(y()); 0086 0087 p.drawRect(_x - 14, _y - 16, 12, 32); 0088 0089 QPolygon pa(3); 0090 pa[0] = QPoint(0, 0); 0091 pa[1] = QPoint(4, -3); 0092 pa[2] = QPoint(4, 3); 0093 0094 int space = m_pSlider->style()->pixelMetric(QStyle::PM_SliderSpaceAvailable /*, m_pSlider TODO investigate parameter */); 0095 int base_y = _y + int(space * m_sliderProp); 0096 0097 pa.translate(_x + 16, base_y); 0098 0099 QColor c = m_p1->isSelected() ? m_selectedCol : Qt::black; 0100 0101 p.setPen(c); 0102 p.setBrush(c); 0103 p.drawPolygon(pa); 0104 0105 p.drawLine(_x + 20, base_y, _x + 24, base_y); 0106 p.drawLine(_x + 24, base_y, _x + 24, _y); 0107 0108 deinitPainter(p); 0109 }