Core: Fix 'Result' message text not wrapped in Expression editor

This commit is contained in:
B0cho
2025-08-04 22:13:17 +02:00
parent c158fd9cf6
commit 05517d2ff4
6 changed files with 69 additions and 26 deletions

View File

@@ -40,6 +40,7 @@
#include <App/VarSet.h>
#include <Base/Console.h>
#include <Base/Tools.h>
#include <regex>
#include "Dialogs/DlgExpressionInput.h"
#include "ui_DlgExpressionInput.h"
@@ -64,7 +65,6 @@ DlgExpressionInput::DlgExpressionInput(const App::ObjectIdentifier & _path,
, path(_path)
, discarded(false)
, impliedUnit(_impliedUnit)
, minimumWidth(10)
, varSetsVisible(false)
, comboBoxGroup(this)
{
@@ -114,7 +114,11 @@ DlgExpressionInput::DlgExpressionInput(const App::ObjectIdentifier & _path,
setAttribute(Qt::WA_TranslucentBackground, true);
}
else {
ui->expression->setMinimumWidth(300);
ui->expression->setMinimumHeight(50);
ui->msg->setWordWrap(true);
ui->msg->setMaximumHeight(200);
ui->msg->setMinimumWidth(280);
ui->horizontalSpacer_3->changeSize(0, 2);
ui->verticalLayout->setContentsMargins(9, 9, 9, 9);
this->adjustSize();
@@ -348,12 +352,12 @@ void DlgExpressionInput::checkExpression(const QString& text)
}
numberRange.throwIfOutOfRange(value);
ui->msg->setText(msg);
message = msg.toStdString();
}
else {
ui->msg->setText(QString::fromStdString(result->toString()));
message = result->toString();
}
setMsgText();
}
}
@@ -382,7 +386,8 @@ void DlgExpressionInput::textChanged()
}
}
catch (Base::Exception & e) {
ui->msg->setText(QString::fromUtf8(e.what()));
message = e.what();
setMsgText();
QPalette p(ui->msg->palette());
p.setColor(QPalette::WindowText, Qt::red);
ui->msg->setPalette(p);
@@ -396,19 +401,6 @@ void DlgExpressionInput::setDiscarded()
reject();
}
void DlgExpressionInput::setExpressionInputSize(int width, int height)
{
if (ui->expression->minimumHeight() < height) {
ui->expression->setMinimumHeight(height);
}
if (ui->expression->minimumWidth() < width) {
ui->expression->setMinimumWidth(width);
}
minimumWidth = width;
}
void DlgExpressionInput::mouseReleaseEvent(QMouseEvent* event)
{
Q_UNUSED(event);
@@ -926,4 +918,42 @@ bool DlgExpressionInput::needReportOnVarSet()
return reportGroup(comboBoxGroup.currentText()) || reportName();
}
void DlgExpressionInput::resizeEvent(QResizeEvent *event)
{
// When the dialog is resized, message text may need to be re-wrapped
if (!this->message.empty() && event->size() != event->oldSize()) {
setMsgText();
}
QDialog::resizeEvent(event);
}
void DlgExpressionInput::setMsgText()
{
if (!this->message.size()) {
return;
}
const QFontMetrics msgFontMetrics{ ui->msg->font() };
// find words longer than length of msg widget
// then insert newline to wrap it
std::string wrappedMsg{};
static constexpr int msgContentMargins = 50;
const int maxWordLength = (ui->msg->width() - msgContentMargins) / msgFontMetrics.averageCharWidth();
const auto wrappableWordPattern = std::regex{ "\\S{" + std::to_string(maxWordLength) + "}" };
auto it = std::sregex_iterator{ this->message.cbegin(), this->message.cend(), wrappableWordPattern };
const auto itEnd = std::sregex_iterator{};
int lastPos = 0;
for (; it != itEnd; ++it) {
wrappedMsg += this->message.substr(lastPos, it->position() - lastPos);
wrappedMsg += it->str() + "\n";
lastPos = it->position() + it->length();
}
wrappedMsg += this->message.substr(lastPos);
ui->msg->setText(QString::fromStdString(wrappedMsg));
}
#include "moc_DlgExpressionInput.cpp"

View File

@@ -77,7 +77,6 @@ public:
bool discardedFormula() const { return discarded; }
QPoint expressionPosition() const;
void setExpressionInputSize(int width, int height);
public Q_SLOTS:
void show();
@@ -86,6 +85,7 @@ public Q_SLOTS:
protected:
void mouseReleaseEvent(QMouseEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void resizeEvent(QResizeEvent* event) override;
private:
Base::Type getTypePath();
@@ -109,6 +109,7 @@ private:
const App::DocumentObject* obj, QString& message) const;
bool isGroupNameValid(const QString& nameGroup,
QString& message) const;
void setMsgText();
private Q_SLOTS:
void textChanged();
@@ -127,7 +128,7 @@ private:
const Base::Unit impliedUnit;
NumberRange numberRange;
int minimumWidth;
std::string message;
bool varSetsVisible;
QPushButton* okBtn = nullptr;

View File

@@ -18,7 +18,7 @@
</property>
<property name="minimumSize">
<size>
<width>300</width>
<width>350</width>
<height>0</height>
</size>
</property>
@@ -48,7 +48,7 @@
<item>
<widget class="QFrame" name="ctrlArea">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
@@ -71,6 +71,12 @@
</property>
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Result</string>
</property>
@@ -78,6 +84,12 @@
</item>
<item>
<widget class="QLabel" name="msg">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="palette">
<palette>
<active>
@@ -130,6 +142,9 @@
<property name="orientation">
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Policy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
@@ -145,7 +160,7 @@
<item>
<widget class="Gui::ExpressionTextEdit" name="expression">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Expanding">
<sizepolicy hsizetype="Ignored" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>

View File

@@ -618,7 +618,6 @@ void QuantitySpinBox::openFormulaDialog()
QPoint pos = mapToGlobal(QPoint(0,0));
box->move(pos-box->expressionPosition());
box->setExpressionInputSize(width(), height());
Gui::adjustDialogPosition(box);
Q_EMIT showFormulaDialog(true);

View File

@@ -206,7 +206,6 @@ void ExpressionSpinBox::openFormulaDialog()
QPoint pos = spinbox->mapToGlobal(QPoint(0,0));
box->move(pos-box->expressionPosition());
box->setExpressionInputSize(spinbox->width(), spinbox->height());
Gui::adjustDialogPosition(box);
}

View File

@@ -1606,7 +1606,6 @@ void ExpLineEdit::openFormulaDialog()
QPoint pos = mapToGlobal(QPoint(0,0));
box->move(pos-box->expressionPosition());
box->setExpressionInputSize(width(), height());
Gui::adjustDialogPosition(box);
}