-
Notifications
You must be signed in to change notification settings - Fork 912
Expand file tree
/
Copy pathcapacitor.cpp
More file actions
244 lines (210 loc) · 9.35 KB
/
capacitor.cpp
File metadata and controls
244 lines (210 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*******************************************************************
Part of the Fritzing project - http://fritzing.org
Copyright (c) 2007-2019 Fritzing
Fritzing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Fritzing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
#include "capacitor.h"
#include "../utils/textutils.h"
#include "../utils/focusoutcombobox.h"
#include "../utils/boundedregexpvalidator.h"
#include "../sketch/infographicsview.h"
#include "partlabel.h"
#include "../sketch/sketchwidget.h"
// TODO
// save into parts bin
Capacitor::Capacitor( ModelPart * modelPart, ViewLayer::ViewID viewID, const ViewGeometry & viewGeometry, long id, QMenu * itemMenu, bool doLabel)
: PaletteItem(modelPart, viewID, viewGeometry, id, itemMenu, doLabel)
{
PropertyDefMaster::initPropertyDefs(modelPart, m_propertyDefs);
}
Capacitor::~Capacitor() {
}
ItemBase::PluralType Capacitor::isPlural() {
return Plural;
}
bool Capacitor::collectExtraInfo(QWidget * parent, const QString & family, const QString & prop, const QString & value, bool swappingEnabled, QString & returnProp, QString & returnValue, QWidget * & returnWidget, bool & hide)
{
foreach (PropertyDef * propertyDef, m_propertyDefs.keys()) {
if (prop.compare(propertyDef->name, Qt::CaseInsensitive) == 0) {
returnProp = TranslatedPropertyNames.value(prop);
if (returnProp.isEmpty()) {
returnProp = propertyDef->name;
}
FocusOutComboBox * focusOutComboBox = new FocusOutComboBox();
focusOutComboBox->setEnabled(swappingEnabled);
focusOutComboBox->setEditable(propertyDef->editable);
focusOutComboBox->setObjectName("infoViewComboBox");
QString current = m_propertyDefs.value(propertyDef);
if (current.isEmpty() && !propertyDef->defaultValue.isEmpty()) {
current = propertyDef->defaultValue + propertyDef->symbol;
setProp(propertyDef->name, propertyDef->defaultValue + propertyDef->symbol);
}
if (propertyDef->editable) {
focusOutComboBox->setToolTip(tr("Select from the dropdown, or type in a %1 value").arg(returnProp));
}
if (propertyDef->numeric) {
focusOutComboBox->setToolTip(tr("Select from the dropdown, or type in a %1 value\n"
"Range: [%2 - %3] %4\n"
"Background: Green = ok, Red = incorrect value, Grey = current value").
arg(returnProp).
arg(TextUtils::convertToPowerPrefix(propertyDef->minValue)).
arg(TextUtils::convertToPowerPrefix(propertyDef->maxValue)).
arg(propertyDef->symbol));
if (!current.isEmpty()) {
double val = TextUtils::convertFromPowerPrefixU(current, propertyDef->symbol);
if (!propertyDef->menuItems.contains(val)) {
propertyDef->menuItems.append(val);
}
}
foreach(double q, propertyDef->menuItems) {
QString s = TextUtils::convertToPowerPrefix(q) + propertyDef->symbol;
focusOutComboBox->addItem(s);
}
}
else {
if (!current.isEmpty()) {
if (!propertyDef->sMenuItems.contains(current)) {
propertyDef->sMenuItems.append(current);
}
}
focusOutComboBox->addItems(propertyDef->sMenuItems);
}
if (!current.isEmpty()) {
int ix = focusOutComboBox->findText(current);
if (ix < 0) {
focusOutComboBox->addItem(current);
ix = focusOutComboBox->findText(current);
}
focusOutComboBox->setCurrentIndex(ix);
}
if (propertyDef->editable) {
BoundedRegExpValidator * validator = new BoundedRegExpValidator(focusOutComboBox);
validator->setSymbol(propertyDef->symbol);
validator->setConverter(TextUtils::convertFromPowerPrefix);
if (propertyDef->maxValue > propertyDef->minValue) {
validator->setBounds(propertyDef->minValue, propertyDef->maxValue);
}
// QString pattern = QString("((\\d{0,10})|(\\d{0,10}\\.)|(\\d{0,10}\\.\\d{1,10}))[%1]{0,1}[%2]{0,1}")
QString pattern = QString("((\\d{1,3})|(\\d{1,3}\\.)|(\\d{1,3}\\.\\d{1,2}))[%1]{0,1}[%2]{0,1}").arg(
// QString pattern = QString("((\\d{0,3})|(\\d{0,3}\\.)|(\\d{0,3}\\.\\d{1,3}))[%1]{0,1}[%2]{0,1}")
TextUtils::PowerPrefixesString,
propertyDef->symbol
);
validator->setRegExp(QRegExp(pattern));
focusOutComboBox->setValidator(validator);
connect(focusOutComboBox->validator(), SIGNAL(sendState(QValidator::State)), this, SLOT(textModified(QValidator::State)));
connect(focusOutComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(propertyEntry(const QString &)));
}
else {
connect(focusOutComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(simplePropertyEntry(const QString &)));
}
this->m_comboBoxes.insert(propertyDef, focusOutComboBox);
returnValue = focusOutComboBox->currentText();
returnWidget = focusOutComboBox;
return true;
}
}
return PaletteItem::collectExtraInfo(parent, family, prop, value, swappingEnabled, returnProp, returnValue, returnWidget, hide);
}
/**
* Sets the appropriate background colour in the combo box when the text is modified. When a user changes the text of an editable
* property in a combo box, this function is called. The function checks the validator of the combo box and sets a red background if the
* state is Intermediate (the value does is not within the limits of the property) or green if the state is Acceptable. Invalid states
* are not possible as the characters that make the string invalid are deleted if introduced.
* not changed in this function.
* @param[in] text The new string in the combo box.
*/
void Capacitor::textModified(QValidator::State state) {
BoundedRegExpValidator * validator = qobject_cast<BoundedRegExpValidator *>(sender());
if (validator == NULL) return;
FocusOutComboBox * focusOutComboBox = qobject_cast<FocusOutComboBox *>(validator->parent());
if (focusOutComboBox == NULL) return;
if (state == QValidator::Acceptable) {
QColor backColor = QColor(210, 246, 210);
QLineEdit *lineEditor = focusOutComboBox->lineEdit();
QPalette pal = lineEditor->palette();
pal.setColor(QPalette::Base, backColor);
lineEditor->setPalette(pal);
} else if (state == QValidator::Intermediate) {
QColor backColor = QColor(246, 210, 210);
QLineEdit *lineEditor = focusOutComboBox->lineEdit();
QPalette pal = lineEditor->palette();
pal.setColor(QPalette::Base, backColor);
lineEditor->setPalette(pal);
}
}
void Capacitor::propertyEntry(const QString & text) {
FocusOutComboBox * focusOutComboBox = qobject_cast<FocusOutComboBox *>(sender());
if (focusOutComboBox == NULL) return;
foreach (PropertyDef * propertyDef, m_comboBoxes.keys()) {
if (m_comboBoxes.value(propertyDef) == focusOutComboBox) {
QString utext = text;
if (propertyDef->numeric) {
double val = TextUtils::convertFromPowerPrefixU(utext, propertyDef->symbol);
if (!propertyDef->menuItems.contains(val)) {
// info view is redrawn, so combobox is recreated, so the new item is added to the combo box menu
propertyDef->menuItems.append(val);
}
}
else {
if (!propertyDef->sMenuItems.contains(text)) {
// info view is redrawn, so combobox is recreated, so the new item is added to the combo box menu
propertyDef->sMenuItems.append(text);
}
}
InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
if (infoGraphicsView) {
infoGraphicsView->setProp(this, propertyDef->name, "", m_propertyDefs.value(propertyDef, ""), utext, true);
}
break;
}
}
}
void Capacitor::setProp(const QString & prop, const QString & value) {
foreach (PropertyDef * propertyDef, m_propertyDefs.keys()) {
if (prop.compare(propertyDef->name, Qt::CaseInsensitive) == 0) {
m_propertyDefs.insert(propertyDef, value);
modelPart()->setLocalProp(propertyDef->name, value);
if (m_partLabel) m_partLabel->displayTextsIf();
return;
}
}
PaletteItem::setProp(prop, value);
}
void Capacitor::simplePropertyEntry(const QString & text) {
FocusOutComboBox * focusOutComboBox = qobject_cast<FocusOutComboBox *>(sender());
if (focusOutComboBox == NULL) return;
foreach (PropertyDef * propertyDef, m_comboBoxes.keys()) {
if (m_comboBoxes.value(propertyDef) == focusOutComboBox) {
InfoGraphicsView * infoGraphicsView = InfoGraphicsView::getInfoGraphicsView(this);
if (infoGraphicsView) {
infoGraphicsView->setProp(this, propertyDef->name, "", m_propertyDefs.value(propertyDef, ""), text, true);
}
break;
}
}
}
void Capacitor::getProperties(QHash<QString, QString> & hash) {
foreach (PropertyDef * propertyDef, m_propertyDefs.keys()) {
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;
}