Skip to content

Commit

Permalink
Fix component's property value editbox position and size
Browse files Browse the repository at this point in the history
  • Loading branch information
wawuwo committed Jul 14, 2024
1 parent 728e557 commit 9d72037
Showing 1 changed file with 60 additions and 76 deletions.
136 changes: 60 additions & 76 deletions qucs/qucs_actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,110 +1219,94 @@ void QucsApp::slotCursorUp(bool up)
// In "view->MAx3" is the number of the current property.
void QucsApp::slotApplyCompText()
{
QString s;
QFont f = QucsSettings.font;
Schematic *Doc = (Schematic*)DocumentTab->currentWidget();
f.setPointSizeF( Doc->Scale * float(f.pointSize()) );
editText->setFont(f);

Property *pp = 0;
Component *pc = (Component*)view->focusElement;
if(!pc) return; // should never happen
view->MAx1 = pc->cx + pc->tx;
view->MAy1 = pc->cy + pc->ty;

int z, n=0; // "n" is number of property on screen
pp = pc->Props.first();
for(z=view->MAx3; z>0; z--) { // calculate "n"
if(!pp) { // should never happen
slotHideEdit();
return;
}
if(pp->display) n++; // is visible ?
pp = pc->Props.next();
}

pp = 0;
if(view->MAx3 > 0) pp = pc->Props.at(view->MAx3-1); // current property
else s = pc->Name;

if(!editText->isHidden()) { // is called the first time ?
// no -> apply value to current property
if(view->MAx3 == 0) { // component name ?
Component *pc2;
if(!editText->text().isEmpty())
if(pc->Name != editText->text()) {
for(pc2 = Doc->Components->first(); pc2!=0; pc2 = Doc->Components->next())
if(pc2->Name == editText->text())
break; // found component with the same name ?
if(!pc2) {
pc->Name = editText->text();
Doc->setChanged(true, true); // only one undo state
Component *const component = dynamic_cast<Component*>(view->focusElement);
if(!component) return; // should never happen
view->MAx1 = component->cx + component->tx;
view->MAy1 = component->cy + component->ty;

// Here is a bit of *magic* and implicit coupling: the value of view->MAx3
// comes from Component::getTextSelected, and it's equal 0 when component
// name is clicked, N + 1 when Nth component property is clicked.
const int component_text_index = view->MAx3;
const bool is_name = component_text_index == 0;

Property *const component_property = !is_name
? component->Props.at(component_text_index - 1)
: nullptr;

if (editText->isVisible()) { // is called the first time ?
if (is_name) {
const auto new_name{editText->text()};

if (!new_name.isEmpty() && component->Name != new_name) {
// TODO: rewrite with std::none_of after replacing Q3PtrList
// with modern container
bool is_unique = true;
for (auto* other : *Doc->Components) {
if (other->Name == new_name) {
is_unique = false;
break;
}
}
}
else if(pp) { // property was applied
if(pp->Value != editText->text()) {
pp->Value = editText->text();
Doc->recreateComponent(pc); // because of "Num" and schematic symbol
Doc->setChanged(true, true); // only one undo state
}
}

n++; // next row on screen
(view->MAx3)++; // next property
pp = pc->Props.at(view->MAx3-1); // search for next property

Doc->viewport()->update();
view->drawn = false;
if (is_unique) {
component->Name = new_name;
Doc->setChanged(true, true); // only one undo state
}
}

if(!pp) { // was already last property ?
slotHideEdit();
return;
}


while(!pp->display) { // search for next visible property
(view->MAx3)++; // next property
pp = pc->Props.next();
if(!pp) { // was already last property ?
slotHideEdit();
return;
else if (component_property) { // property was applied
if (component_property->Value != editText->text()) {
component_property->Value = editText->text();
Doc->recreateComponent(component); // because of "Num" and schematic symbol
Doc->setChanged(true, true); // only one undo state
}
}
}

// avoid seeing the property text behind the line edit
if(pp) // Is it first property or component name ?
s = pp->Value;
editText->setMinimumWidth(editText->fontMetrics().boundingRect(s).width()+4);

const QString s = is_name
? component->Name
: component_property->Value;

Doc->contentsToViewport(int(Doc->Scale * float(view->MAx1 - Doc->ViewX1)),
int(Doc->Scale * float(view->MAy1 - Doc->ViewY1)),
view->MAx2, view->MAy2);
editText->setReadOnly(false);
if(pp) { // is it a property ?
s = pp->Value;
view->MAx2 += editText->fontMetrics().boundingRect(pp->Name+"=").width();
if(pp->Description.indexOf('[') >= 0) // is selection list ?
QPoint editTextTopLeft;
if (component_property) { // is it a property ?
editTextTopLeft = Doc->modelToViewport(QPoint{component->cx, component->cy} + component_property->boundingRect().topLeft());
editTextTopLeft.rx() += editText->fontMetrics().boundingRect(component_property->Name + "=" + '\u0020').width();

if(component_property->Description.indexOf('[') >= 0) // is selection list ?
editText->setReadOnly(true);
Expr_CompProp.setPattern("[^\"]*");
if(!pc->showName) n--;
}
else // it is the component name
else { // it is the component name
Expr_CompProp.setPattern("[\\w_]+");
editTextTopLeft = Doc->modelToViewport(QPoint{component->cx + component->tx, component->cy + component->ty});
}

{
auto size = editText->fontMetrics().boundingRect(s ).size();
size.rwidth() += editText->fontMetrics().averageCharWidth();
editText->setFixedSize(size);
}

view->MAx2 = editTextTopLeft.x();
view->MAy2 = editTextTopLeft.y();

Val_CompProp.setRegularExpression(Expr_CompProp);
editText->setValidator(&Val_CompProp);

z = editText->fontMetrics().lineSpacing();
view->MAy2 += n*z;
editText->setText(s);
editText->setStyleSheet("color: black; background-color: " + QucsSettings.BGColor.name());
editText->setFocus();
editText->selectAll();
editText->setParent(Doc->viewport());
editText->move(QPoint(view->MAx2, view->MAy2));
editText->move(view->MAx2, view->MAy2);
editText->show();
//editText->reparent(Doc->viewport(), 0, QPoint(view->MAx2, view->MAy2), true);
}
Expand Down

0 comments on commit 9d72037

Please sign in to comment.