File indexing completed on 2024-05-05 05:46:09
0001 /*************************************************************************** 0002 * Copyright (C) 2006 by William Hillerby * 0003 * william.hillerby@ntlworld.com * 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 "variablecapacitor.h" 0012 #include "canvasitemparts.h" 0013 #include "capacitance.h" 0014 #include "ecnode.h" 0015 #include "libraryitem.h" 0016 0017 #include <KLocalizedString> 0018 0019 #include <QDebug> 0020 #include <QPainter> 0021 #include <QStyle> 0022 0023 Item *VariableCapacitor::construct(ItemDocument *itemDocument, bool newItem, const char *id) 0024 { 0025 return new VariableCapacitor(static_cast<ICNDocument *>(itemDocument), newItem, id); 0026 } 0027 0028 LibraryItem *VariableCapacitor::libraryItem() 0029 { 0030 return new LibraryItem(QStringList(QString("ec/variablecapacitor")), i18n("Variable Capacitor"), i18n("Passive"), "variable_capacitor.png", LibraryItem::lit_component, VariableCapacitor::construct); 0031 } 0032 0033 VariableCapacitor::VariableCapacitor(ICNDocument *icnDocument, bool newItem, const QString &id) 0034 : Component(icnDocument, newItem, (!id.isEmpty()) ? id : "variable capacitor") 0035 { 0036 m_name = i18n("Variable Capacitor"); 0037 0038 // Top Left(x,y) from centre point, width, height. 0039 setSize(-16, -8, 32, 16); 0040 0041 // william - you might want to change this value. I added this line as it 0042 // was being used unitialized (in the sliderValueChanged function when 0043 // addSlider is called later on), and causing a crash - david. 0044 m_tickValue = 1; 0045 0046 m_maxCapacitance = 0.0001; 0047 m_minCapacitance = 0.00005; 0048 0049 m_currCapacitance = m_minCapacitance + ((m_maxCapacitance - m_minCapacitance) / 2); 0050 0051 init1PinLeft(); 0052 init1PinRight(); 0053 0054 m_pNNode[0]->setLength(15); 0055 m_pPNode[0]->setLength(15); 0056 0057 m_pCapacitance = createCapacitance(m_pNNode[0], m_pPNode[0], m_currCapacitance); 0058 0059 addDisplayText("capacitance", QRect(-8, -26, 16, 16), "", false); 0060 0061 createProperty("currcapacitance", Variant::Type::Double); 0062 property("currcapacitance")->setCaption(i18n("Capacitance")); 0063 property("currcapacitance")->setUnit("F"); 0064 property("currcapacitance")->setMinValue(1e-12); 0065 property("currcapacitance")->setMaxValue(1e12); 0066 property("currcapacitance")->setValue(m_currCapacitance); 0067 0068 createProperty("maximum capacitance", Variant::Type::Double); 0069 property("maximum capacitance")->setCaption(i18n("Max")); 0070 property("maximum capacitance")->setUnit("F"); 0071 property("maximum capacitance")->setMinValue(1e-12); 0072 property("maximum capacitance")->setMaxValue(1e12); 0073 property("maximum capacitance")->setValue(m_maxCapacitance); 0074 0075 createProperty("minimum capacitance", Variant::Type::Double); 0076 property("minimum capacitance")->setCaption(i18n("Min")); 0077 property("minimum capacitance")->setUnit("F"); 0078 property("minimum capacitance")->setMinValue(1e-12); 0079 property("minimum capacitance")->setMaxValue(1e12); 0080 property("minimum capacitance")->setValue(m_minCapacitance); 0081 0082 Slider *s = addSlider("slider", 0, 100, 1, 50, Qt::Horizontal, QRect(-16, 10, 32, 16)); 0083 m_pSlider = qobject_cast<QSlider *>(s->widget()); 0084 } 0085 0086 VariableCapacitor::~VariableCapacitor() 0087 { 0088 } 0089 0090 void VariableCapacitor::dataChanged() 0091 { 0092 double new_minCapacitance = dataDouble("minimum capacitance"); 0093 double new_maxCapacitance = dataDouble("maximum capacitance"); 0094 0095 if (new_minCapacitance != m_minCapacitance) { 0096 if (new_minCapacitance >= m_maxCapacitance) { 0097 m_minCapacitance = m_maxCapacitance; 0098 property("minimum capacitance")->setValue(m_minCapacitance); 0099 } else 0100 m_minCapacitance = new_minCapacitance; 0101 } 0102 0103 if (new_maxCapacitance != m_maxCapacitance) { 0104 if (new_maxCapacitance <= m_minCapacitance) { 0105 m_maxCapacitance = m_minCapacitance; 0106 property("maximum capacitance")->setValue(m_maxCapacitance); 0107 } else 0108 m_maxCapacitance = new_maxCapacitance; 0109 } 0110 0111 /* Attempt at fixme. 0112 m_currCapacitance = property( "currcapacitance" )->value().asDouble(); 0113 0114 if(m_currCapacitance > m_maxCapacitance) m_currCapacitance = m_maxCapacitance; 0115 else if(m_currCapacitance < m_minCapacitance) m_currCapacitance = m_minCapacitance; 0116 */ 0117 0118 m_tickValue = (m_maxCapacitance - m_minCapacitance) / m_pSlider->maximum(); 0119 0120 property("currcapacitance")->setValue(m_currCapacitance); 0121 0122 // Calculate the capacitance jump per tick of a 100 tick slider. 0123 sliderValueChanged("slider", slider("slider")->value()); 0124 } 0125 0126 void VariableCapacitor::sliderValueChanged(const QString &id, int newValue) 0127 { 0128 if (id != "slider") 0129 return; 0130 0131 /** @todo fix slider so current cap can be set in toolbar and editor and slider updates */ 0132 m_currCapacitance = m_minCapacitance + (newValue * m_tickValue); 0133 0134 // Set the new capacitance value. 0135 m_pCapacitance->setCapacitance(m_currCapacitance); 0136 0137 // Update property. 0138 property("currcapacitance")->setValue(m_currCapacitance); 0139 0140 QString display = QString::number(m_currCapacitance / getMultiplier(m_currCapacitance), 'g', 3) + getNumberMag(m_currCapacitance) + "F"; 0141 0142 setDisplayText("capacitance", display); 0143 } 0144 0145 void VariableCapacitor::drawShape(QPainter &p) 0146 { 0147 initPainter(p); 0148 0149 // Get centre point of component. 0150 int _y = int(y()); 0151 int _x = int(x()); 0152 0153 p.drawRect(_x - 8, _y - 8, 5, 16); 0154 p.drawRect(_x + 3, _y - 8, 5, 16); 0155 0156 // p.drawLine( _x-8, _y, _x-16, _y ); 0157 // p.drawLine( _x+8, _y, _x+16, _y ); 0158 0159 // Diagonally pointing arrow 0160 QPolygon pa(3); 0161 pa[0] = QPoint(-4, 0); 0162 pa[1] = QPoint(-2, 4); 0163 pa[2] = QPoint(0, 0); 0164 0165 pa.translate(_x + 16, _y - 8); 0166 p.setBrush(p.pen().color()); 0167 p.drawPolygon(pa); 0168 0169 p.drawLine(_x - 16, _y + 8, _x + 16, _y - 8); 0170 0171 deinitPainter(p); 0172 }