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

0001 /***************************************************************************
0002  *   Copyright (C) 2006 by William Hillerby - william.hillerby@ntlworld.com*
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #include "variableresistor.h"
0011 #include "canvasitemparts.h"
0012 #include "ecnode.h"
0013 #include "libraryitem.h"
0014 #include "resistance.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QDebug>
0019 #include <QPainter>
0020 #include <QStyle>
0021 
0022 Item *VariableResistor::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0023 {
0024     return new VariableResistor(static_cast<ICNDocument *>(itemDocument), newItem, id);
0025 }
0026 
0027 LibraryItem *VariableResistor::libraryItem()
0028 {
0029     return new LibraryItem(QStringList(QString("ec/variableresistor")), i18n("Variable Resistor"), i18n("Passive"), "variable_resistor.png", LibraryItem::lit_component, VariableResistor::construct);
0030 }
0031 
0032 VariableResistor::VariableResistor(ICNDocument *icnDocument, bool newItem, const QString &id)
0033     : Component(icnDocument, newItem, (!id.isEmpty()) ? id : "variable resistor")
0034 {
0035     m_name = i18n("Resistor");
0036 
0037     // Top Left(x,y) from centre point, width, height.
0038     setSize(-16, -16, 32, 32);
0039 
0040     init1PinLeft();
0041     init1PinRight();
0042 
0043     // (see comment in variablecapacitor.cpp) - david
0044     m_tickValue = 1;
0045 
0046     m_minResistance = 0.5;
0047     m_maxResistance = 1.0;
0048 
0049     m_currResistance = m_minResistance + ((m_maxResistance - m_minResistance) / 2);
0050 
0051     m_pResistance = createResistance(m_pPNode[0], m_pNNode[0], m_currResistance);
0052 
0053     createProperty("resistance", Variant::Type::Double);
0054     property("resistance")->setCaption(i18n("Resistance"));
0055     property("resistance")->setUnit(QChar(0x3a9));
0056     property("resistance")->setMinValue(1e-6);
0057     property("resistance")->setValue(m_currResistance);
0058 
0059     createProperty("minimum resistance", Variant::Type::Double);
0060     property("minimum resistance")->setCaption(i18n("Min"));
0061     property("minimum resistance")->setUnit(QChar(0x3a9));
0062     property("minimum resistance")->setMinValue(1e-6);
0063     property("minimum resistance")->setValue(m_minResistance);
0064 
0065     createProperty("maximum resistance", Variant::Type::Double);
0066     property("maximum resistance")->setCaption(i18n("Max"));
0067     property("maximum resistance")->setUnit(QChar(0x3a9));
0068     property("maximum resistance")->setMinValue(1e-6);
0069     property("maximum resistance")->setValue(m_maxResistance);
0070 
0071     addDisplayText("res", QRect(-16, -26, 32, 12), "", false);
0072 
0073     Slider *s = addSlider("slider", 0, 100, 1, 50, Qt::Horizontal, QRect(-16, 14, width(), 16));
0074     m_pSlider = static_cast<QSlider *>(s->widget());
0075 }
0076 
0077 VariableResistor::~VariableResistor()
0078 {
0079 }
0080 
0081 void VariableResistor::dataChanged()
0082 {
0083     double new_minResistance = dataDouble("minimum resistance");
0084     double new_maxResistance = dataDouble("maximum resistance");
0085 
0086     if (new_minResistance != m_minResistance) {
0087         if (new_minResistance >= m_maxResistance) {
0088             m_minResistance = m_maxResistance;
0089             property("minimum resistance")->setValue(m_minResistance);
0090         } else
0091             m_minResistance = new_minResistance;
0092     }
0093 
0094     if (new_maxResistance != m_maxResistance) {
0095         if (new_maxResistance <= m_minResistance) {
0096             m_maxResistance = m_minResistance;
0097             property("maximum resistance")->setValue(m_maxResistance);
0098         } else
0099             m_maxResistance = new_maxResistance;
0100     }
0101 
0102     m_tickValue = (m_maxResistance - m_minResistance) / m_pSlider->maximum();
0103 
0104     // Calculate the resistance jump per tick of a 100 tick slider.
0105     sliderValueChanged("slider", slider("slider")->value());
0106 }
0107 
0108 void VariableResistor::sliderValueChanged(const QString &id, int newValue)
0109 {
0110     if (id != "slider")
0111         return;
0112 
0113     /** @todo fix slider so current cap can be set in toolbar and editor and slider updates */
0114     m_currResistance = m_minResistance + (newValue * m_tickValue);
0115 
0116     // Set the new capacitance value.
0117     m_pResistance->setResistance(m_currResistance);
0118 
0119     // Update property.
0120     property("resistance")->setValue(m_currResistance);
0121 
0122     QString display = QString::number(m_currResistance / getMultiplier(m_currResistance), 'g', 3) + getNumberMag(m_currResistance) + QChar(0x3a9);
0123 
0124     setDisplayText("res", display);
0125 }
0126 
0127 void VariableResistor::drawShape(QPainter &p)
0128 {
0129     initPainter(p);
0130 
0131     // Get centre point of component.
0132     int _y = int(y());
0133     int _x = int(x());
0134 
0135     p.drawRect(_x - 16, _y - 6, width(), 12);
0136     p.drawLine(_x - 12, _y + 12, _x + 13, _y - 13);
0137 
0138     QPolygon pa(3);
0139 
0140     // Diagonally pointing arrow
0141     pa[0] = QPoint(0, 0);
0142     pa[1] = QPoint(-4, 0);
0143     pa[2] = QPoint(0, 4);
0144 
0145     pa.translate(_x + 13, _y - 13);
0146     p.setBrush(p.pen().color());
0147     p.drawPolygon(pa);
0148 
0149     deinitPainter(p);
0150 }