Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix simulator bugs and a few improvements #3966

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions resources/properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@

<property name="power" symbol="W" minValue="0" maxValue="1000000000000" defaultValue="" numeric="yes" editable="yes">
<suffix suffix="ResistorModuleID"/>
<suffix suffix="PhotocellModuleID"/>
<suffix suffix="PotentiometerModuleID"/>
<suffix suffix="alps-starter-pot9mm" />
<suffix suffix="SparkFun-Electromechanical-TRIMPOT-3MM-2"/>
Expand Down Expand Up @@ -315,4 +316,16 @@
<suffix suffix="beab33bd7feee277622ef76dd6e79e4d"/>
</property>

<property name="dark resistance @0.01lx" symbol="&#937;" minValue="0" maxValue="1000000000000" defaultValue="300000" numeric="yes" editable="yes">
<suffix suffix="PhotocellModuleID"/>
</property>

<property name="light resistance @100lx" symbol="&#937;" minValue="0" maxValue="1000000000000" defaultValue="400" numeric="yes" editable="yes">
<suffix suffix="PhotocellModuleID"/>
</property>

<property name="luminance" symbol="lx" minValue="0.001" maxValue="10000" defaultValue="10" numeric="yes" editable="yes">
<suffix suffix="PhotocellModuleID"/>
</property>

</properties>
2 changes: 2 additions & 0 deletions src/infoview/htmlinfoview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ void HtmlInfoView::init(bool tinyMode) {
m_spiceTextLabel = new TagLabel(this);
m_spiceTextLabel->setWordWrap(true);
m_spiceTextLabel->setObjectName("tagsValue");
m_spiceTextLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
vlo->addWidget(m_spiceTextLabel);

m_tagLabel = new QLabel(tr("Tags"), NULL);
Expand All @@ -295,6 +296,7 @@ void HtmlInfoView::init(bool tinyMode) {
m_tagsTextLabel = new TagLabel(this);
m_tagsTextLabel->setWordWrap(true);
m_tagsTextLabel->setObjectName("tagsValue");
m_tagsTextLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
vlo->addWidget(m_tagsTextLabel);
if (tinyMode) m_tagsTextLabel->setVisible(false);

Expand Down
13 changes: 13 additions & 0 deletions src/items/capacitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,16 @@ void Capacitor::getProperties(QHash<QString, QString> & hash) {
hash.insert(propertyDef->name, m_propertyDefs.value(propertyDef));
}
}

QHash<QString, QString> Capacitor::prepareProps(ModelPart * modelPart, bool wantDebug, QStringList & keys)
{
QHash<QString, QString> props = ItemBase::prepareProps(modelPart, wantDebug, keys);

// ensure capacitance and other properties are after family, if it is a capacitor;
if (keys.removeOne("capacitance")) {
keys.insert(1, "capacitance");
if (keys.removeOne("voltage")) keys.insert(2, "voltage");
}

return props;
}
1 change: 1 addition & 0 deletions src/items/capacitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Capacitor : public PaletteItem
bool collectExtraInfo(QWidget * parent, const QString & family, const QString & prop, const QString & value, bool swappingEnabled, QString & returnProp, QString & returnValue, QWidget * & returnWidget, bool & hide);
void setProp(const QString & prop, const QString & value);
void getProperties(QHash<QString, QString> &);
QHash<QString, QString> prepareProps(ModelPart * modelPart, bool wantDebug, QStringList & keys);

public slots:
void textModified(QValidator::State state);
Expand Down
5 changes: 3 additions & 2 deletions src/items/itembase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,15 +1303,16 @@ FSvgRenderer * ItemBase::setUpImage(ModelPart * modelPart, LayerAttributes & lay


//DebugDialog::debug(QString("set up image elapsed (1) %1").arg(t.elapsed()) );
QString filename = PartFactory::getSvgFilename(modelPart, modelPartShared->imageFileName(layerAttributes.viewID, layerAttributes.viewLayerID), true, true);
QString imageFilename = modelPartShared->imageFileName(layerAttributes.viewID, layerAttributes.viewLayerID);
QString filename = PartFactory::getSvgFilename(modelPart, imageFilename, true, true);

//#ifndef QT_NO_DEBUG
//DebugDialog::debug(QString("set up image elapsed (2) %1").arg(t.elapsed()) );
//#endif

if (filename.isEmpty()) {
//QString deleteme = modelPartShared->domDocument()->toString();
layerAttributes.error = tr("file for %1 %2 not found").arg(modelPartShared->title()).arg(modelPartShared->moduleID());
layerAttributes.error = tr("file %1 for %2 not found").arg(imageFilename).arg(modelPartShared->moduleID());
return nullptr;
}

Expand Down
98 changes: 86 additions & 12 deletions src/items/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ QString LED::retrieveSvg(ViewLayer::ViewLayerID viewLayerID, QHash<QString, QStr

void LED::addedToScene(bool temporary)
{
if (this->scene()) {
//Check if this an RGB led
QString rgbString = this->getProperty("rgb");
if (this->scene() && rgbString.isEmpty()) {
//Set the color for the LED, does not apply if is is an RGB led
setColor(prop("color"));
}

Expand Down Expand Up @@ -140,7 +143,7 @@ void LED::setColor(const QString & color)
* @brief Restores the original brightness of an LED.
*/
void LED::resetBrightness() {
setBrightness(1-offColor);
setBrightness((1-offColor)/brigtnessMultiplier);
if(m_ledLight && this->viewID()==ViewLayer::ViewID::BreadboardView) {
m_ledLight->hide();
}
Expand All @@ -155,7 +158,7 @@ void LED::resetBrightness() {
* @brief Changes the brightness of an LED.
* @param[in] brightness The brightness value, its range is from 0 (off) to 1 (on)
*/
void LED::setBrightness(double brightness){
void LED::setBrightness(double brightness) {
QString errorStr;
int errorLine;
int errorColumn;
Expand All @@ -166,14 +169,21 @@ void LED::setBrightness(double brightness){

//get the color of the LED
QString colorString;
QString color = prop("color");
foreach (PropertyDef * propertyDef, m_propertyDefs.keys()) {
if (propertyDef->name.compare("color") == 0) {
colorString = propertyDef->adjuncts.value(color, "");
break;
QString rgbString = this->getProperty("rgb");
if (rgbString.isEmpty()) {
//If it is just one LED, use color property
QString color = prop("color");
foreach (PropertyDef * propertyDef, m_propertyDefs.keys()) {
if (propertyDef->name.compare("color") == 0) {
colorString = propertyDef->adjuncts.value(color, "");
break;
}
}
if (colorString.isEmpty()) return;
} else {
// It is an RGB led, set color to grey
colorString = "#E6E6E6";
}
if (colorString.isEmpty()) return;

int red = colorString.mid(1,2).toInt(nullptr, 16);
int green = colorString.mid(3,2).toInt(nullptr, 16);
Expand All @@ -183,9 +193,12 @@ void LED::setBrightness(double brightness){
if (brightness < 0) brightness = 0;

//Find the new color values
red = offColor*red + brightness*red;
green = offColor*green + brightness*green;
blue = offColor*blue + brightness*blue;
//The color achives maximum intensity when brightness = 1/brigtnessMultiplier
double brightnessColor = brightness * brigtnessMultiplier;
if (brightnessColor > 1) brightnessColor = 1;
red = offColor*red + brightnessColor*red;
green = offColor*green + brightnessColor*green;
blue = offColor*blue + brightnessColor*blue;
if(red > 255) red = 255;
if(green > 255) green = 255;
if(blue > 255) blue = 255;
Expand All @@ -210,6 +223,56 @@ void LED::setBrightness(double brightness){
}
}

/**
* Changes the brightness of an RGB LED based on 3 brightness parameters.
* A brightness of 0 reduces scales the color to 30% of the color.
*
* @brief Changes the brightness of an RGB LED.
* @param[in] brightness The brightness values, its range is from 0 (off) to 1 (on)
*/
void LED::setBrightnessRGB(double brightnessR, double brightnessG, double brightnessB) {
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument domDocument;
if (!domDocument.setContent( BreadboardSvg.value(m_filename), &errorStr, &errorLine, &errorColumn)) {
return;
}

//The color achives maximum intensity when brightness = 0.25
int red = 40 + (brightnessR * brigtnessMultiplier * 200.0);
int green = 40 + (brightnessG * brigtnessMultiplier * 200.0);
int blue = 40 + (brightnessB * brigtnessMultiplier * 200.0);
double brightness = std::max({brightnessR, brightnessG, brightnessB});

int maxColor = 230; //Do not saturate the colors
if (red > 255) red = maxColor;
if (red < 0) red = 0;
if (green > 255) green = maxColor;
if (green < 0) green = 0;
if (blue > 255) blue = maxColor;
if (blue < 0) blue = 0;

QString newColorStr = QString("#%1%2%3")
.arg(red, 2, 16)
.arg(green, 2, 16)
.arg(blue, 2, 16);
newColorStr.replace(' ', '0');

//Change the color of the LED
QDomElement root = domDocument.documentElement();
slamColor(root, newColorStr);
reloadRenderer(domDocument.toString(),true);

//Add light comming out of the LED in BreadboardView
if(this->viewID()==ViewLayer::ViewID::BreadboardView) {
if(!m_ledLight) {
m_ledLight = new LedLight(this);
}
m_ledLight->setLight(brightness, red, green, blue);
}
}

QString LED::getColorSVG(const QString & color, ViewLayer::ViewLayerID viewLayerID)
{
QString errorStr;
Expand Down Expand Up @@ -259,3 +322,14 @@ ViewLayer::ViewID LED::useViewIDForPixmap(ViewLayer::ViewID vid, bool swappingEn

return ItemBase::useViewIDForPixmap(vid, swappingEnabled);
}

QHash<QString, QString> LED::prepareProps(ModelPart * modelPart, bool wantDebug, QStringList & keys)
{
QHash<QString, QString> props = ItemBase::prepareProps(modelPart, wantDebug, keys);

// ensure color and other properties are after family;
if (keys.removeOne("color")) keys.insert(1, "color");
if (keys.removeOne("current")) keys.insert(2, "current");

return props;
}
39 changes: 25 additions & 14 deletions src/items/led.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ class LedLight : public QGraphicsEllipseItem {
~LedLight() {
};
void setLight(double brightness, int red, int green, int blue){
if (brightness < 0.15)
//Only set light coming out if brightness is bigger than 0.25
if (brightness < 0.25)
brightness = 0.0;
double radious = std::min(parentItem()->boundingRect().width()/2,
parentItem()->boundingRect().height()/2) * brightness * 4;
prepareGeometryChange();
setRect(-radious + parentItem()->boundingRect().width()/2,
-radious + parentItem()->boundingRect().width()/5,
radious*2, radious*2);
QRadialGradient gradient = QRadialGradient(0.5, 0.5, 0.5);
gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
gradient.setColorAt(0, QColor(red, green, blue, 255));
gradient.setColorAt(0.3, QColor(red, green, blue, 230));
gradient.setColorAt(1, QColor(red, green, blue, 0));
setBrush(gradient);
this->show();
Capacitor* led = dynamic_cast<Capacitor *>(parentItem());
if (led) {
double radious = std::min(led->boundingRectWithoutLegs().width()/2,
led->boundingRectWithoutLegs().height()/2) * brightness * 4;
prepareGeometryChange();
setRect(-radious + led->boundingRectWithoutLegs().width()/2,
-radious + led->boundingRectWithoutLegs().width()/2,
radious*2, radious*2);
QRadialGradient gradient = QRadialGradient(0.5, 0.5, 0.5);
gradient.setCoordinateMode(QGradient::ObjectBoundingMode);
gradient.setColorAt(0, QColor(red, green, blue, 255));
gradient.setColorAt(0.3, QColor(red, green, blue, 230));
gradient.setColorAt(1, QColor(red, green, blue, 0));
setBrush(gradient);
this->show();
}
};
};

Expand All @@ -62,6 +66,11 @@ class LED : public Capacitor
//The color that remains when the brightness is set to 0
static constexpr double offColor = 0.3;

//The maximum color is achived when brigtness*brigtnessMultiplier=1
//After that, more current increases the light comming out of the LED,
//but it does not change the color
static constexpr double brigtnessMultiplier = 4.0;

public:
// after calling this constructor if you want to render the loaded svg (either from model or from file), MUST call <renderImage>
LED(ModelPart *, ViewLayer::ViewID, const ViewGeometry & viewGeometry, long id, QMenu * itemMenu, bool doLabel);
Expand All @@ -77,7 +86,9 @@ class LED : public Capacitor
const QString & title();
ViewLayer::ViewID useViewIDForPixmap(ViewLayer::ViewID, bool swappingEnabled);
void setBrightness(double);
void setBrightnessRGB(double brightnessR, double brightnessG, double brightnessB);
void resetBrightness();
QHash<QString, QString> prepareProps(ModelPart * modelPart, bool wantDebug, QStringList & keys);

protected:
void setColor(const QString & color);
Expand Down
1 change: 1 addition & 0 deletions src/items/moduleidnames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const QString ModuleIDNames::PowerLabelModuleIDName = "PowerLabelModuleID";
const QString ModuleIDNames::LeftNetLabelModuleIDName = "LeftNetLabelModuleID";
const QString ModuleIDNames::JustPowerModuleIDName = "JustPowerModuleID";
const QString ModuleIDNames::ResistorModuleIDName = "ResistorModuleID";
const QString ModuleIDNames::PhotocellModuleIDName = "PhotocellModuleID";
const QString ModuleIDNames::LogoTextModuleIDName = "LogoTextModuleID";
const QString ModuleIDNames::LogoImageModuleIDName = "LogoImageModuleID";
const QString ModuleIDNames::Silkscreen0LogoImageModuleIDName = "Silkscreen0LogoImageModuleID";
Expand Down
1 change: 1 addition & 0 deletions src/items/moduleidnames.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ModuleIDNames
static const QString LeftNetLabelModuleIDName;
static const QString JustPowerModuleIDName;
static const QString ResistorModuleIDName;
static const QString PhotocellModuleIDName;
static const QString LogoTextModuleIDName;
static const QString LogoImageModuleIDName;
static const QString Silkscreen0LogoImageModuleIDName;
Expand Down
15 changes: 15 additions & 0 deletions src/items/resistor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,18 @@ ViewLayer::ViewID Resistor::useViewIDForPixmap(ViewLayer::ViewID vid, bool swapp

return ItemBase::useViewIDForPixmap(vid, swappingEnabled);
}

QHash<QString, QString> Resistor::prepareProps(ModelPart * modelPart, bool wantDebug, QStringList & keys)
{
QHash<QString, QString> props = ItemBase::prepareProps(modelPart, wantDebug, keys);

// ensure resistance and other properties are after family;
if (keys.removeOne("resistance"))
keys.insert(1, "resistance");
if (keys.removeOne("tolerance"))
keys.insert(2, "tolerance");
if (keys.removeOne("power"))
keys.insert(3, "power");

return props;
}
1 change: 1 addition & 0 deletions src/items/resistor.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Resistor : public Capacitor
void addedToScene(bool temporary);
void setProp(const QString & prop, const QString & value);
bool setUpImage(ModelPart* modelPart, const LayerHash & viewLayers, LayerAttributes &);
QHash<QString, QString> prepareProps(ModelPart * modelPart, bool wantDebug, QStringList & keys);

protected:
QString makeSvg(const QString & ohms, ViewLayer::ViewLayerID viewLayerID);
Expand Down
Loading