| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 3 |
* Copyright (c) 2025 Pieter Hijma <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 51 Franklin Street, * |
| 20 |
* Fifth Floor, Boston, MA 02110-1301, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QMenu> |
| 26 |
#include <QMouseEvent> |
| 27 |
#include <QPushButton> |
| 28 |
#include <QTreeWidget> |
| 29 |
#include <QStyledItemDelegate> |
| 30 |
|
| 31 |
#include <fmt/format.h> |
| 32 |
|
| 33 |
#include <App/Application.h> |
| 34 |
#include <App/Document.h> |
| 35 |
#include <App/DocumentObject.h> |
| 36 |
#include <App/ExpressionParser.h> |
| 37 |
#include <App/VarSet.h> |
| 38 |
#include <Base/Console.h> |
| 39 |
#include <Base/Tools.h> |
| 40 |
#include <regex> |
| 41 |
|
| 42 |
#include "Dialogs/DlgExpressionInput.h" |
| 43 |
#include "ui_DlgExpressionInput.h" |
| 44 |
#include "Application.h" |
| 45 |
#include "Command.h" |
| 46 |
#include "Tools.h" |
| 47 |
#include "ExpressionBinding.h" |
| 48 |
#include "BitmapFactory.h" |
| 49 |
#include "ViewProviderDocumentObject.h" |
| 50 |
|
| 51 |
using namespace App; |
| 52 |
using namespace Gui::Dialog; |
| 53 |
|
| 54 |
FC_LOG_LEVEL_INIT("DlgExpressionInput", true, true) |
| 55 |
|
| 56 |
DlgExpressionInput::DlgExpressionInput( |
| 57 |
const App::ObjectIdentifier& _path, |
| 58 |
std::shared_ptr<const Expression> _expression, |
| 59 |
const Base::Unit& _impliedUnit, |
| 60 |
QWidget* parent |
| 61 |
) |
| 62 |
: QDialog(parent) |
| 63 |
, ui(new Ui::DlgExpressionInput) |
| 64 |
, expression(_expression ? _expression->copy() : nullptr) |
| 65 |
, path(_path) |
| 66 |
, discarded(false) |
| 67 |
, impliedUnit(_impliedUnit) |
| 68 |
, varSetsVisible(false) |
| 69 |
, comboBoxGroup(this) |
| 70 |
{ |
| 71 |
assert(path.getDocumentObject()); |
| 72 |
|
| 73 |
// Setup UI |
| 74 |
ui->setupUi(this); |
| 75 |
okBtn = ui->buttonBox->button(QDialogButtonBox::Ok); |
| 76 |
discardBtn = ui->buttonBox->button(QDialogButtonBox::Reset); |
| 77 |
discardBtn->setToolTip(tr("Revert to last calculated value (as constant)")); |
| 78 |
|
| 79 |
initializeVarSets(); |
| 80 |
|
| 81 |
// Connect signal(s) |
| 82 |
connect(ui->expression, &ExpressionTextEdit::textChanged, this, &DlgExpressionInput::textChanged); |
| 83 |
connect(discardBtn, &QPushButton::clicked, this, &DlgExpressionInput::setDiscarded); |
| 84 |
|
| 85 |
if (expression) { |
| 86 |
ui->expression->setPlainText(QString::fromStdString(expression->toString())); |
| 87 |
} |
| 88 |
else { |
| 89 |
QVariant text = parent->property("text"); |
| 90 |
if (text.canConvert<QString>()) { |
| 91 |
ui->expression->setPlainText(text.toString()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Set document object on text edit to create auto completer |
| 96 |
DocumentObject* docObj = path.getDocumentObject(); |
| 97 |
ui->expression->setDocumentObject(docObj); |
| 98 |
|
| 99 |
// There are some platforms where setting no system background causes a black |
| 100 |
// rectangle to appear. To avoid this the 'NoSystemBackground' parameter can be |
| 101 |
// set to false. Then a normal non-modal dialog will be shown instead (#0002440). |
| 102 |
bool noBackground = App::GetApplication() |
| 103 |
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/Expression") |
| 104 |
->GetBool("NoSystemBackground", false); |
| 105 |
|
| 106 |
if (noBackground) { |
| 107 |
#if defined(Q_OS_MACOS) |
| 108 |
setWindowFlags(Qt::Widget | Qt::Popup | Qt::FramelessWindowHint); |
| 109 |
#else |
| 110 |
setWindowFlags(Qt::SubWindow | Qt::Widget | Qt::Popup | Qt::FramelessWindowHint); |
| 111 |
#endif |
| 112 |
setAttribute(Qt::WA_NoSystemBackground, true); |
| 113 |
setAttribute(Qt::WA_TranslucentBackground, true); |
| 114 |
} |
| 115 |
else { |
| 116 |
ui->expression->setMinimumWidth(300); |
| 117 |
ui->expression->setMinimumHeight(80); |
| 118 |
ui->msg->setWordWrap(true); |
| 119 |
ui->msg->setMaximumHeight(200); |
| 120 |
ui->msg->setMinimumWidth(280); |
| 121 |
ui->verticalLayout->setContentsMargins(9, 9, 9, 9); |
| 122 |
this->adjustSize(); |
| 123 |
// It is strange that (at least on Linux) DlgExpressionInput will shrink |
| 124 |
// to be narrower than ui->expression after calling adjustSize() above. |
| 125 |
// Why? |
| 126 |
if (this->width() < ui->expression->width() + 18) { |
| 127 |
this->resize(ui->expression->width() + 18, this->height()); |
| 128 |
} |
| 129 |
} |
| 130 |
ui->expression->setFocus(); |
| 131 |
} |
| 132 |
|
| 133 |
DlgExpressionInput::~DlgExpressionInput() |
| 134 |
{ |
| 135 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) |
| 136 |
disconnect( |
| 137 |
ui->checkBoxVarSets, |
| 138 |
&QCheckBox::checkStateChanged, |
| 139 |
this, |
| 140 |
&DlgExpressionInput::onCheckVarSets |
| 141 |
); |
| 142 |
#else |
| 143 |
disconnect(ui->checkBoxVarSets, &QCheckBox::stateChanged, this, &DlgExpressionInput::onCheckVarSets); |
| 144 |
#endif |
| 145 |
disconnect( |
| 146 |
ui->comboBoxVarSet, |
| 147 |
qOverload<int>(&QComboBox::currentIndexChanged), |
| 148 |
this, |
| 149 |
&DlgExpressionInput::onVarSetSelected |
| 150 |
); |
| 151 |
disconnect( |
| 152 |
&comboBoxGroup, |
| 153 |
&EditFinishedComboBox::currentTextChanged, |
| 154 |
this, |
| 155 |
&DlgExpressionInput::onTextChangedGroup |
| 156 |
); |
| 157 |
disconnect(ui->lineEditPropNew, &QLineEdit::textChanged, this, &DlgExpressionInput::namePropChanged); |
| 158 |
|
| 159 |
delete ui; |
| 160 |
} |
| 161 |
|
| 162 |
static void getVarSetsDocument(std::vector<App::VarSet*>& varSets, App::Document* doc) |
| 163 |
{ |
| 164 |
for (auto obj : doc->getObjects()) { |
| 165 |
auto varSet = dynamic_cast<App::VarSet*>(obj); |
| 166 |
if (varSet) { |
| 167 |
varSets.push_back(varSet); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
static std::vector<App::VarSet*> getAllVarSets() |
| 173 |
{ |
| 174 |
std::vector<App::Document*> docs = App::GetApplication().getDocuments(); |
| 175 |
std::vector<App::VarSet*> varSets; |
| 176 |
|
| 177 |
for (auto doc : docs) { |
| 178 |
getVarSetsDocument(varSets, doc); |
| 179 |
} |
| 180 |
|
| 181 |
return varSets; |
| 182 |
} |
| 183 |
|
| 184 |
Base::Type DlgExpressionInput::getTypePath() |
| 185 |
{ |
| 186 |
return path.getProperty()->getTypeId(); |
| 187 |
} |
| 188 |
|
| 189 |
Base::Type DlgExpressionInput::determineTypeVarSet() |
| 190 |
{ |
| 191 |
Base::Type typePath = getTypePath(); |
| 192 |
|
| 193 |
// The type of the path is leading. If it is one of the types below, we |
| 194 |
// can create a property in the varset. |
| 195 |
if (typePath == App::PropertyString::getClassTypeId() |
| 196 |
|| typePath.isDerivedFrom(App::PropertyFloat::getClassTypeId()) |
| 197 |
|| typePath.isDerivedFrom(App::PropertyInteger::getClassTypeId())) { |
| 198 |
return typePath; |
| 199 |
} |
| 200 |
|
| 201 |
// If we cannot determine the type by means of the path, for example when |
| 202 |
// dealing with a sketcher constraint list or with the x, y, or z of a |
| 203 |
// Placement, the type of the unit allows us to create a property in the |
| 204 |
// varset. Since unit properties are derived from App::PropertyFloat, it |
| 205 |
// allows us to create a property and set the value. |
| 206 |
|
| 207 |
std::string unitTypeString = impliedUnit.getTypeString(); |
| 208 |
if (unitTypeString.empty()) { |
| 209 |
// no type was provided |
| 210 |
return Base::Type::BadType; |
| 211 |
} |
| 212 |
|
| 213 |
std::string typeString = "App::Property" + unitTypeString; |
| 214 |
// may return badType |
| 215 |
return Base::Type::fromName(typeString.c_str()); |
| 216 |
} |
| 217 |
|
| 218 |
bool DlgExpressionInput::typeOkForVarSet() |
| 219 |
{ |
| 220 |
std::string unitType = impliedUnit.getTypeString(); |
| 221 |
return !determineTypeVarSet().isBad(); |
| 222 |
} |
| 223 |
|
| 224 |
void DlgExpressionInput::initializeErrorFrame() |
| 225 |
{ |
| 226 |
ui->errorFrame->setVisible(false); |
| 227 |
const int size = style()->pixelMetric(QStyle::PM_LargeIconSize); |
| 228 |
QIcon icon = Gui::BitmapFactory().iconFromTheme("overlay_error"); |
| 229 |
if (icon.isNull()) { |
| 230 |
icon = style()->standardIcon(QStyle::SP_MessageBoxCritical); |
| 231 |
} |
| 232 |
ui->errorIconLabel->setPixmap(icon.pixmap(QSize(size, size))); |
| 233 |
} |
| 234 |
|
| 235 |
void DlgExpressionInput::initializeVarSets() |
| 236 |
{ |
| 237 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0) |
| 238 |
connect(ui->checkBoxVarSets, &QCheckBox::checkStateChanged, this, &DlgExpressionInput::onCheckVarSets); |
| 239 |
#else |
| 240 |
connect(ui->checkBoxVarSets, &QCheckBox::stateChanged, this, &DlgExpressionInput::onCheckVarSets); |
| 241 |
#endif |
| 242 |
connect( |
| 243 |
ui->comboBoxVarSet, |
| 244 |
qOverload<int>(&QComboBox::currentIndexChanged), |
| 245 |
this, |
| 246 |
&DlgExpressionInput::onVarSetSelected |
| 247 |
); |
| 248 |
connect( |
| 249 |
&comboBoxGroup, |
| 250 |
&EditFinishedComboBox::currentTextChanged, |
| 251 |
this, |
| 252 |
&DlgExpressionInput::onTextChangedGroup |
| 253 |
); |
| 254 |
connect(ui->lineEditPropNew, &QLineEdit::textChanged, this, &DlgExpressionInput::namePropChanged); |
| 255 |
|
| 256 |
comboBoxGroup.setObjectName(QStringLiteral("comboBoxGroup")); |
| 257 |
comboBoxGroup.setInsertPolicy(QComboBox::InsertAtTop); |
| 258 |
comboBoxGroup.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 259 |
DlgAddProperty::setWidgetForLabel("labelGroup", &comboBoxGroup, ui->formLayout); |
| 260 |
setTabOrder(ui->comboBoxVarSet, &comboBoxGroup); |
| 261 |
setTabOrder(&comboBoxGroup, ui->lineEditPropNew); |
| 262 |
|
| 263 |
std::vector<App::VarSet*> varSets = getAllVarSets(); |
| 264 |
if (!varSets.empty() && typeOkForVarSet()) { |
| 265 |
ui->checkBoxVarSets->setVisible(true); |
| 266 |
ui->checkBoxVarSets->setCheckState(Qt::Unchecked); |
| 267 |
ui->groupBoxVarSets->setVisible(false); |
| 268 |
} |
| 269 |
else { |
| 270 |
// The dialog is shown without any VarSet options. |
| 271 |
ui->checkBoxVarSets->setVisible(false); |
| 272 |
ui->groupBoxVarSets->setVisible(false); |
| 273 |
} |
| 274 |
initializeErrorFrame(); |
| 275 |
} |
| 276 |
|
| 277 |
void NumberRange::setRange(double min, double max) |
| 278 |
{ |
| 279 |
minimum = min; |
| 280 |
maximum = max; |
| 281 |
defined = true; |
| 282 |
} |
| 283 |
|
| 284 |
void NumberRange::clearRange() |
| 285 |
{ |
| 286 |
defined = false; |
| 287 |
} |
| 288 |
|
| 289 |
void NumberRange::throwIfOutOfRange(const Base::Quantity& value) const |
| 290 |
{ |
| 291 |
if (!defined) { |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
auto toQString = [](const Base::Quantity& v) { |
| 296 |
return QString::fromStdString(v.getUserString()); |
| 297 |
}; |
| 298 |
|
| 299 |
if (value.getValue() < minimum || value.getValue() > maximum) { |
| 300 |
Base::Quantity minVal(minimum, value.getUnit()); |
| 301 |
Base::Quantity maxVal(maximum, value.getUnit()); |
| 302 |
|
| 303 |
const QString fmt |
| 304 |
= QCoreApplication::translate("Exceptions", "Value out of range (%1 out of [%2, %3])"); |
| 305 |
const QString msg = fmt.arg(toQString(value), toQString(minVal), toQString(maxVal)); |
| 306 |
THROWM(Base::ValueError, msg.toStdString()); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
void DlgExpressionInput::setRange(double minimum, double maximum) |
| 311 |
{ |
| 312 |
numberRange.setRange(minimum, maximum); |
| 313 |
} |
| 314 |
|
| 315 |
void DlgExpressionInput::clearRange() |
| 316 |
{ |
| 317 |
numberRange.clearRange(); |
| 318 |
} |
| 319 |
|
| 320 |
QPoint DlgExpressionInput::expressionPosition() const |
| 321 |
{ |
| 322 |
return ui->expression->pos(); |
| 323 |
} |
| 324 |
|
| 325 |
bool DlgExpressionInput::checkCyclicDependencyVarSet(const QString& text) |
| 326 |
{ |
| 327 |
std::shared_ptr<Expression> expr( |
| 328 |
ExpressionParser::parse(path.getDocumentObject(), text.toUtf8().constData()) |
| 329 |
); |
| 330 |
|
| 331 |
if (expr) { |
| 332 |
DocumentObject* obj = path.getDocumentObject(); |
| 333 |
auto ids = expr->getIdentifiers(); |
| 334 |
|
| 335 |
for (const auto& id : ids) { |
| 336 |
if (id.first.getDocumentObject() == obj) { |
| 337 |
// This string is not translated. It is based on a string that |
| 338 |
// originates from the expression validator in App that is also |
| 339 |
// not translated. |
| 340 |
ui->msg->setText( |
| 341 |
QString::fromStdString(id.first.toString() + " reference causes a cyclic dependency") |
| 342 |
); |
| 343 |
return true; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return false; |
| 349 |
} |
| 350 |
|
| 351 |
void DlgExpressionInput::checkExpression(const QString& text) |
| 352 |
{ |
| 353 |
// now handle expression |
| 354 |
std::shared_ptr<Expression> expr( |
| 355 |
ExpressionParser::parse(path.getDocumentObject(), text.toUtf8().constData()) |
| 356 |
); |
| 357 |
|
| 358 |
if (expr) { |
| 359 |
std::string error = path.getDocumentObject()->ExpressionEngine.validateExpression(path, expr); |
| 360 |
|
| 361 |
if (!error.empty()) { |
| 362 |
throw Base::RuntimeError(error.c_str()); |
| 363 |
} |
| 364 |
|
| 365 |
std::unique_ptr<Expression> result(expr->eval()); |
| 366 |
|
| 367 |
expression = expr; |
| 368 |
okBtn->setEnabled(true); |
| 369 |
ui->msg->clear(); |
| 370 |
|
| 371 |
// set default palette as we may have read text right now |
| 372 |
ui->msg->setPalette(okBtn->palette()); |
| 373 |
|
| 374 |
auto* n = freecad_cast<NumberExpression*>(result.get()); |
| 375 |
if (n) { |
| 376 |
Base::Quantity value = n->getQuantity(); |
| 377 |
if (!value.isValid()) { |
| 378 |
THROWMT(Base::ValueError, QT_TRANSLATE_NOOP("Exceptions", "Not a number")); |
| 379 |
} |
| 380 |
|
| 381 |
QString msg = QString::fromStdString(value.getUserString()); |
| 382 |
if (impliedUnit != Base::Unit::One) { |
| 383 |
if (!value.isDimensionless() && value.getUnit() != impliedUnit) { |
| 384 |
THROWMT( |
| 385 |
Base::UnitsMismatchError, |
| 386 |
QT_TRANSLATE_NOOP("Exceptions", "Unit mismatch between result and required unit") |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
value.setUnit(impliedUnit); |
| 391 |
} |
| 392 |
else if (!value.isDimensionless()) { |
| 393 |
msg += tr(" (Warning: unit discarded)"); |
| 394 |
|
| 395 |
QPalette p(ui->msg->palette()); |
| 396 |
p.setColor(QPalette::WindowText, Qt::red); |
| 397 |
ui->msg->setPalette(p); |
| 398 |
} |
| 399 |
|
| 400 |
numberRange.throwIfOutOfRange(value); |
| 401 |
message = msg.toStdString(); |
| 402 |
} |
| 403 |
else { |
| 404 |
message = result->toString(); |
| 405 |
} |
| 406 |
setMsgText(); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static const bool NoCheckExpr = false; |
| 411 |
|
| 412 |
void DlgExpressionInput::textChanged() |
| 413 |
{ |
| 414 |
const QString& text = ui->expression->toPlainText(); |
| 415 |
|
| 416 |
if (text.isEmpty()) { |
| 417 |
okBtn->setDisabled(true); |
| 418 |
discardBtn->setDefault(true); |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
okBtn->setDefault(true); |
| 423 |
|
| 424 |
try { |
| 425 |
checkExpression(text); |
| 426 |
if (varSetsVisible) { |
| 427 |
// If varsets are visible, check whether the varset info also |
| 428 |
// agrees that the button should be enabled. |
| 429 |
// No need to check the expression in that function. |
| 430 |
updateVarSetInfo(NoCheckExpr); |
| 431 |
} |
| 432 |
} |
| 433 |
catch (Base::Exception& e) { |
| 434 |
message = e.what(); |
| 435 |
setMsgText(); |
| 436 |
QPalette p(ui->msg->palette()); |
| 437 |
p.setColor(QPalette::WindowText, Qt::red); |
| 438 |
ui->msg->setPalette(p); |
| 439 |
okBtn->setDisabled(true); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
void DlgExpressionInput::setDiscarded() |
| 444 |
{ |
| 445 |
discarded = true; |
| 446 |
reject(); |
| 447 |
} |
| 448 |
|
| 449 |
void DlgExpressionInput::mouseReleaseEvent(QMouseEvent* event) |
| 450 |
{ |
| 451 |
Q_UNUSED(event); |
| 452 |
} |
| 453 |
|
| 454 |
void DlgExpressionInput::mousePressEvent(QMouseEvent* event) |
| 455 |
{ |
| 456 |
Q_UNUSED(event); |
| 457 |
|
| 458 |
// The 'FramelessWindowHint' is also set when the background is transparent. |
| 459 |
if (windowFlags() & Qt::FramelessWindowHint) { |
| 460 |
// we need to reject the dialog when clicked on the background. As the background is |
| 461 |
// transparent this is the expected behaviour for the user |
| 462 |
bool on = ui->expression->completerActive(); |
| 463 |
if (!on) { |
| 464 |
this->reject(); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
void DlgExpressionInput::show() |
| 470 |
{ |
| 471 |
QDialog::show(); |
| 472 |
this->activateWindow(); |
| 473 |
ui->expression->selectAll(); |
| 474 |
} |
| 475 |
|
| 476 |
class Binding: public Gui::ExpressionBinding |
| 477 |
{ |
| 478 |
// helper class to compensate for the fact that |
| 479 |
// ExpressionBinding::setExpression is protected. |
| 480 |
public: |
| 481 |
Binding() = default; |
| 482 |
|
| 483 |
void setExpression(std::shared_ptr<App::Expression> expr) override |
| 484 |
{ |
| 485 |
ExpressionBinding::setExpression(expr); |
| 486 |
} |
| 487 |
}; |
| 488 |
|
| 489 |
static constexpr const char* InvalidIdentifierMessage = QT_TR_NOOP( |
| 490 |
"must contain only alphanumeric characters, underscore, and must not start with a digit" |
| 491 |
); |
| 492 |
|
| 493 |
bool DlgExpressionInput::isPropertyNameValid( |
| 494 |
const QString& nameProp, |
| 495 |
const App::DocumentObject* obj, |
| 496 |
QString& message |
| 497 |
) const |
| 498 |
{ |
| 499 |
auto withPrefix = [&](const QString& detail) { |
| 500 |
return tr("Invalid property name: %1").arg(detail); |
| 501 |
}; |
| 502 |
|
| 503 |
if (!obj) { |
| 504 |
message = tr("Unknown object"); |
| 505 |
return false; |
| 506 |
} |
| 507 |
|
| 508 |
std::string name = nameProp.toStdString(); |
| 509 |
if (name.empty()) { |
| 510 |
message = withPrefix(tr("the name cannot be empty")); |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
if (name != Base::Tools::getIdentifier(name)) { |
| 515 |
message = withPrefix(tr(InvalidIdentifierMessage)); |
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
if (ExpressionParser::isTokenAUnit(name)) { |
| 520 |
message = withPrefix(tr("%1 is a unit").arg(nameProp)); |
| 521 |
return false; |
| 522 |
} |
| 523 |
|
| 524 |
if (ExpressionParser::isTokenAConstant(name)) { |
| 525 |
message = withPrefix(tr("%1 is a constant").arg(nameProp)); |
| 526 |
return false; |
| 527 |
} |
| 528 |
|
| 529 |
auto prop = obj->getPropertyByName(name.c_str()); |
| 530 |
if (prop && prop->getContainer() == obj) { |
| 531 |
message = withPrefix(tr("%1 already exists").arg(nameProp)); |
| 532 |
return false; |
| 533 |
} |
| 534 |
|
| 535 |
return true; |
| 536 |
} |
| 537 |
|
| 538 |
static const int DocRole = Qt::UserRole; |
| 539 |
static const int VarSetNameRole = Qt::UserRole + 1; |
| 540 |
static const int VarSetLabelRole = Qt::UserRole + 2; |
| 541 |
static const int LevelRole = Qt::UserRole + 3; |
| 542 |
|
| 543 |
static QString getValue(QComboBox* comboBox, int role) |
| 544 |
{ |
| 545 |
QVariant variant = comboBox->currentData(role); |
| 546 |
return variant.toString(); |
| 547 |
} |
| 548 |
|
| 549 |
static void storePreferences( |
| 550 |
const std::string& nameDoc, |
| 551 |
const std::string& nameVarSet, |
| 552 |
const std::string& nameGroup |
| 553 |
) |
| 554 |
{ |
| 555 |
auto paramExpressionEditor = App::GetApplication().GetParameterGroupByPath( |
| 556 |
"User parameter:BaseApp/Preferences/ExpressionEditor" |
| 557 |
); |
| 558 |
paramExpressionEditor->SetASCII("LastDocument", nameDoc); |
| 559 |
paramExpressionEditor->SetASCII("LastVarSet", nameVarSet); |
| 560 |
paramExpressionEditor->SetASCII("LastGroup", nameGroup); |
| 561 |
} |
| 562 |
|
| 563 |
static const App::NumberExpression* toNumberExpr(const App::Expression* expr) |
| 564 |
{ |
| 565 |
return freecad_cast<const App::NumberExpression*>(expr); |
| 566 |
} |
| 567 |
|
| 568 |
static const App::StringExpression* toStringExpr(const App::Expression* expr) |
| 569 |
{ |
| 570 |
return freecad_cast<const App::StringExpression*>(expr); |
| 571 |
} |
| 572 |
|
| 573 |
static const App::OperatorExpression* toUnitNumberExpr(const App::Expression* expr) |
| 574 |
{ |
| 575 |
auto* opExpr = freecad_cast<const App::OperatorExpression*>(expr); |
| 576 |
if (opExpr && opExpr->getOperator() == App::OperatorExpression::Operator::UNIT |
| 577 |
&& toNumberExpr(opExpr->getLeft())) { |
| 578 |
return opExpr; |
| 579 |
} |
| 580 |
return nullptr; |
| 581 |
} |
| 582 |
|
| 583 |
void DlgExpressionInput::createBindingVarSet(App::Property* propVarSet, App::DocumentObject* varSet) |
| 584 |
{ |
| 585 |
ObjectIdentifier varSetId(*propVarSet); |
| 586 |
|
| 587 |
// rewrite the identifiers of the expression to be relative to the VarSet |
| 588 |
std::map<App::ObjectIdentifier, bool> identifiers = expression->getIdentifiers(); |
| 589 |
|
| 590 |
std::map<ObjectIdentifier, ObjectIdentifier> idsFromObjToVarSet; |
| 591 |
for (const auto& idPair : identifiers) { |
| 592 |
ObjectIdentifier exprId = idPair.first; |
| 593 |
ObjectIdentifier relativeId = exprId.relativeTo(varSetId); |
| 594 |
idsFromObjToVarSet[exprId] = relativeId; |
| 595 |
} |
| 596 |
|
| 597 |
Binding binding; |
| 598 |
binding.bind(*propVarSet); |
| 599 |
binding.setExpression(expression); |
| 600 |
binding.apply(); |
| 601 |
|
| 602 |
varSet->renameObjectIdentifiers(idsFromObjToVarSet); |
| 603 |
} |
| 604 |
|
| 605 |
void DlgExpressionInput::acceptWithVarSet() |
| 606 |
{ |
| 607 |
// all checks have been performed in updateVarSetInfo and textChanged that |
| 608 |
// decide to enable the button |
| 609 |
|
| 610 |
// create a property in the VarSet |
| 611 |
QString nameVarSet = getValue(ui->comboBoxVarSet, VarSetNameRole); |
| 612 |
QString nameGroup = comboBoxGroup.currentText(); |
| 613 |
QString nameProp = ui->lineEditPropNew->text(); |
| 614 |
|
| 615 |
QString nameDoc = getValue(ui->comboBoxVarSet, DocRole); |
| 616 |
App::Document* doc = App::GetApplication().getDocument(nameDoc.toUtf8()); |
| 617 |
App::DocumentObject* obj = doc->getObject(nameVarSet.toUtf8()); |
| 618 |
|
| 619 |
std::string name = nameProp.toStdString(); |
| 620 |
std::string group = nameGroup.toStdString(); |
| 621 |
std::string type = getType(); |
| 622 |
auto prop = obj->addDynamicProperty(type.c_str(), name.c_str(), group.c_str()); |
| 623 |
|
| 624 |
// Set the value of the property in the VarSet |
| 625 |
// |
| 626 |
// The value of the property is going to be the value that was originally |
| 627 |
// meant to be the value for the property that this dialog is targeting. |
| 628 |
const Expression* expr = expression.get(); |
| 629 |
if (const NumberExpression* ne = toNumberExpr(expr)) { |
| 630 |
// the value is a number: directly assign it to the property instead of |
| 631 |
// making it an expression in the variable set |
| 632 |
Gui::Command::doCommand( |
| 633 |
Gui::Command::Doc, |
| 634 |
"App.getDocument('%s').getObject('%s').%s = %f", |
| 635 |
obj->getDocument()->getName(), |
| 636 |
obj->getNameInDocument(), |
| 637 |
prop->getName(), |
| 638 |
ne->getValue() |
| 639 |
); |
| 640 |
} |
| 641 |
else if (const StringExpression* se = toStringExpr(expr)) { |
| 642 |
// the value is a string: directly assign it to the property. |
| 643 |
Gui::Command::doCommand( |
| 644 |
Gui::Command::Doc, |
| 645 |
"App.getDocument('%s').getObject('%s').%s = \"%s\"", |
| 646 |
obj->getDocument()->getName(), |
| 647 |
obj->getNameInDocument(), |
| 648 |
prop->getName(), |
| 649 |
se->getText().c_str() |
| 650 |
); |
| 651 |
} |
| 652 |
else if (const OperatorExpression* une = toUnitNumberExpr(expr)) { |
| 653 |
// the value is a unit number: directly assign it to the property. |
| 654 |
Gui::Command::doCommand( |
| 655 |
Gui::Command::Doc, |
| 656 |
"App.getDocument('%s').getObject('%s').%s = \"%s\"", |
| 657 |
obj->getDocument()->getName(), |
| 658 |
obj->getNameInDocument(), |
| 659 |
prop->getName(), |
| 660 |
une->toString().c_str() |
| 661 |
); |
| 662 |
} |
| 663 |
else { |
| 664 |
// the value is an expression: make an expression binding in the VarSet |
| 665 |
createBindingVarSet(prop, obj); |
| 666 |
} |
| 667 |
|
| 668 |
// Create a new expression that refers to the property in the VarSet |
| 669 |
// for the original property that is the target of this dialog. |
| 670 |
expression.reset(ExpressionParser::parse(path.getDocumentObject(), prop->getFullName().c_str())); |
| 671 |
|
| 672 |
storePreferences(nameDoc.toStdString(), nameVarSet.toStdString(), group); |
| 673 |
} |
| 674 |
|
| 675 |
void DlgExpressionInput::accept() |
| 676 |
{ |
| 677 |
if (varSetsVisible) { |
| 678 |
if (needReportOnVarSet()) { |
| 679 |
return; |
| 680 |
} |
| 681 |
acceptWithVarSet(); |
| 682 |
} |
| 683 |
QDialog::accept(); |
| 684 |
} |
| 685 |
|
| 686 |
static App::Document* getPreselectedDocument() |
| 687 |
{ |
| 688 |
auto paramExpressionEditor = App::GetApplication().GetParameterGroupByPath( |
| 689 |
"User parameter:BaseApp/Preferences/ExpressionEditor" |
| 690 |
); |
| 691 |
std::string lastDoc = paramExpressionEditor->GetASCII("LastDocument", ""); |
| 692 |
|
| 693 |
if (lastDoc.empty()) { |
| 694 |
return App::GetApplication().getActiveDocument(); |
| 695 |
} |
| 696 |
|
| 697 |
App::Document* doc = App::GetApplication().getDocument(lastDoc.c_str()); |
| 698 |
if (doc == nullptr) { |
| 699 |
return App::GetApplication().getActiveDocument(); |
| 700 |
} |
| 701 |
|
| 702 |
return doc; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
int DlgExpressionInput::getVarSetIndex(const App::Document* doc) const |
| 707 |
{ |
| 708 |
auto paramExpressionEditor = App::GetApplication().GetParameterGroupByPath( |
| 709 |
"User parameter:BaseApp/Preferences/ExpressionEditor" |
| 710 |
); |
| 711 |
std::string lastVarSet = paramExpressionEditor->GetASCII("LastVarSet", "VarSet"); |
| 712 |
|
| 713 |
auto* model = qobject_cast<QStandardItemModel*>(ui->comboBoxVarSet->model()); |
| 714 |
for (int i = 0; i < model->rowCount(); ++i) { |
| 715 |
QStandardItem* item = model->item(i); |
| 716 |
if (item->data(DocRole).toString() == QString::fromUtf8(doc->getName()) |
| 717 |
&& item->data(VarSetNameRole).toString() == QString::fromStdString(lastVarSet)) { |
| 718 |
return i; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
// Select the first varset of the first document (the document is item 0) |
| 723 |
return 1; |
| 724 |
} |
| 725 |
|
| 726 |
void DlgExpressionInput::preselectVarSet() |
| 727 |
{ |
| 728 |
const App::Document* doc = getPreselectedDocument(); |
| 729 |
if (doc == nullptr) { |
| 730 |
FC_ERR("No active document found"); |
| 731 |
} |
| 732 |
ui->comboBoxVarSet->setCurrentIndex(getVarSetIndex(doc)); |
| 733 |
} |
| 734 |
|
| 735 |
// Custom delegate to add indentation |
| 736 |
class IndentedItemDelegate: public QStyledItemDelegate |
| 737 |
{ |
| 738 |
public: |
| 739 |
explicit IndentedItemDelegate(QObject* parent = nullptr) |
| 740 |
: QStyledItemDelegate(parent) |
| 741 |
{} |
| 742 |
|
| 743 |
void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const override |
| 744 |
{ |
| 745 |
QStyledItemDelegate::initStyleOption(option, index); |
| 746 |
|
| 747 |
if (index.data(LevelRole) == 1) { |
| 748 |
int indentWidth = 20; |
| 749 |
option->rect.adjust(indentWidth, 0, 0, 0); |
| 750 |
} |
| 751 |
} |
| 752 |
}; |
| 753 |
|
| 754 |
static void addVarSetsVarSetComboBox( |
| 755 |
std::vector<App::VarSet*>& varSets, |
| 756 |
QStandardItem* docItem, |
| 757 |
QStandardItemModel* model |
| 758 |
) |
| 759 |
{ |
| 760 |
for (auto* varSet : varSets) { |
| 761 |
auto* vp = freecad_cast<Gui::ViewProviderDocumentObject*>( |
| 762 |
Gui::Application::Instance->getViewProvider(varSet) |
| 763 |
); |
| 764 |
if (vp == nullptr) { |
| 765 |
FC_ERR("No ViewProvider found for VarSet: " << varSet->getNameInDocument()); |
| 766 |
continue; |
| 767 |
} |
| 768 |
|
| 769 |
// The item will be owned by the model, so no need to delete it manually. |
| 770 |
auto item = new QStandardItem(); |
| 771 |
item->setIcon(vp->getIcon()); |
| 772 |
item->setText(QString::fromUtf8(varSet->Label.getValue())); |
| 773 |
item->setData(QString::fromUtf8(varSet->Label.getValue()), VarSetLabelRole); |
| 774 |
item->setData(QString::fromUtf8(varSet->getNameInDocument()), VarSetNameRole); |
| 775 |
item->setData(docItem->data(DocRole), DocRole); |
| 776 |
item->setData(1, LevelRole); |
| 777 |
model->appendRow(item); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
static void addDocVarSetComboBox(App::Document* doc, QPixmap& docIcon, QStandardItemModel* model) |
| 782 |
{ |
| 783 |
if (doc->testStatus(App::Document::TempDoc)) { |
| 784 |
// Do not add temporary documents to the VarSet combo box |
| 785 |
return; |
| 786 |
} |
| 787 |
|
| 788 |
std::vector<App::VarSet*> varSets; |
| 789 |
getVarSetsDocument(varSets, doc); |
| 790 |
if (varSets.empty()) { |
| 791 |
return; |
| 792 |
} |
| 793 |
|
| 794 |
// The item will be owned by the model, so no need to delete it manually. |
| 795 |
auto* item = new QStandardItem(); |
| 796 |
item->setIcon(docIcon); |
| 797 |
item->setText(QString::fromUtf8(doc->Label.getValue())); |
| 798 |
item->setData(QByteArray(doc->getName()), DocRole); |
| 799 |
item->setFlags(Qt::ItemIsEnabled); // Make sure this item cannot be selected |
| 800 |
item->setData(0, LevelRole); |
| 801 |
model->appendRow(item); |
| 802 |
|
| 803 |
addVarSetsVarSetComboBox(varSets, item, model); |
| 804 |
} |
| 805 |
|
| 806 |
QStandardItemModel* DlgExpressionInput::createVarSetModel() |
| 807 |
{ |
| 808 |
// Create the model |
| 809 |
auto* model = new QStandardItemModel(ui->comboBoxVarSet); |
| 810 |
model->setColumnCount(1); |
| 811 |
|
| 812 |
// Add items to the model |
| 813 |
QPixmap docIcon(Gui::BitmapFactory().pixmap("Document")); |
| 814 |
std::vector<App::Document*> docs = App::GetApplication().getDocuments(); |
| 815 |
|
| 816 |
for (auto doc : docs) { |
| 817 |
addDocVarSetComboBox(doc, docIcon, model); |
| 818 |
} |
| 819 |
|
| 820 |
return model; |
| 821 |
} |
| 822 |
|
| 823 |
void DlgExpressionInput::setupVarSets() |
| 824 |
{ |
| 825 |
QStandardItemModel* model = createVarSetModel(); |
| 826 |
{ |
| 827 |
QSignalBlocker blocker(ui->comboBoxVarSet); |
| 828 |
ui->comboBoxVarSet->clear(); |
| 829 |
auto* listView = new QListView(this); |
| 830 |
listView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 831 |
listView->setModel(model); |
| 832 |
ui->comboBoxVarSet->setView(listView); |
| 833 |
ui->comboBoxVarSet->setModel(model); |
| 834 |
ui->comboBoxVarSet->setItemDelegate(new IndentedItemDelegate(ui->comboBoxVarSet)); |
| 835 |
} |
| 836 |
preselectVarSet(); |
| 837 |
|
| 838 |
okBtn->setEnabled(false); |
| 839 |
} |
| 840 |
|
| 841 |
std::string DlgExpressionInput::getType() |
| 842 |
{ |
| 843 |
return determineTypeVarSet().getName(); |
| 844 |
} |
| 845 |
|
| 846 |
void DlgExpressionInput::onCheckVarSets(int state) |
| 847 |
{ |
| 848 |
varSetsVisible = state == Qt::Checked; |
| 849 |
ui->groupBoxVarSets->setVisible(varSetsVisible); |
| 850 |
if (varSetsVisible) { |
| 851 |
setupVarSets(); |
| 852 |
} |
| 853 |
else { |
| 854 |
try { |
| 855 |
checkExpression(ui->expression->toPlainText()); |
| 856 |
} |
| 857 |
catch (Base::Exception&) { |
| 858 |
okBtn->setEnabled(false); |
| 859 |
} |
| 860 |
adjustSize(); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
void DlgExpressionInput::preselectGroup() |
| 865 |
{ |
| 866 |
auto paramExpressionEditor = App::GetApplication().GetParameterGroupByPath( |
| 867 |
"User parameter:BaseApp/Preferences/ExpressionEditor" |
| 868 |
); |
| 869 |
std::string lastGroup = paramExpressionEditor->GetASCII("LastGroup", ""); |
| 870 |
|
| 871 |
if (lastGroup.empty()) { |
| 872 |
return; |
| 873 |
} |
| 874 |
|
| 875 |
if (int index = comboBoxGroup.findText(QString::fromStdString(lastGroup)); index != -1) { |
| 876 |
comboBoxGroup.setCurrentIndex(index); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
void DlgExpressionInput::onVarSetSelected(int /*index*/) |
| 881 |
{ |
| 882 |
QString docName = getValue(ui->comboBoxVarSet, DocRole); |
| 883 |
QString varSetName = getValue(ui->comboBoxVarSet, VarSetNameRole); |
| 884 |
if (docName.isEmpty() || varSetName.isEmpty()) { |
| 885 |
FC_ERR("No document or variable set selected"); |
| 886 |
return; |
| 887 |
} |
| 888 |
|
| 889 |
App::Document* doc = App::GetApplication().getDocument(docName.toUtf8()); |
| 890 |
if (doc == nullptr) { |
| 891 |
FC_ERR("Document not found: " << docName.toStdString()); |
| 892 |
return; |
| 893 |
} |
| 894 |
|
| 895 |
App::DocumentObject* varSet = doc->getObject(varSetName.toUtf8()); |
| 896 |
if (varSet == nullptr) { |
| 897 |
FC_ERR("Variable set not found: " << varSetName.toStdString()); |
| 898 |
return; |
| 899 |
} |
| 900 |
|
| 901 |
DlgAddProperty::populateGroup(comboBoxGroup, varSet); |
| 902 |
preselectGroup(); |
| 903 |
updateVarSetInfo(); |
| 904 |
ui->lineEditPropNew->setFocus(); |
| 905 |
} |
| 906 |
|
| 907 |
void DlgExpressionInput::onTextChangedGroup(const QString&) |
| 908 |
{ |
| 909 |
updateVarSetInfo(); |
| 910 |
} |
| 911 |
|
| 912 |
void DlgExpressionInput::namePropChanged(const QString&) |
| 913 |
{ |
| 914 |
updateVarSetInfo(); |
| 915 |
} |
| 916 |
|
| 917 |
bool DlgExpressionInput::isGroupNameValid(const QString& nameGroup, QString& message) const |
| 918 |
{ |
| 919 |
auto withPrefix = [&](const QString& detail) { |
| 920 |
return tr("Invalid group name: %1").arg(detail); |
| 921 |
}; |
| 922 |
|
| 923 |
if (nameGroup.isEmpty()) { |
| 924 |
message = withPrefix(tr("the name cannot be empty")); |
| 925 |
return false; |
| 926 |
} |
| 927 |
std::string name = nameGroup.toStdString(); |
| 928 |
|
| 929 |
if (name != Base::Tools::getIdentifier(name)) { |
| 930 |
message = withPrefix(tr(InvalidIdentifierMessage)); |
| 931 |
return false; |
| 932 |
} |
| 933 |
|
| 934 |
return true; |
| 935 |
} |
| 936 |
|
| 937 |
void DlgExpressionInput::reportVarSetInfo(const QString& message) |
| 938 |
{ |
| 939 |
if (!message.isEmpty()) { |
| 940 |
ui->errorFrame->setVisible(true); |
| 941 |
ui->errorTextLabel->setText(message); |
| 942 |
ui->errorTextLabel->updateGeometry(); |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
static void setErrorState(QWidget* widget, bool on) |
| 947 |
{ |
| 948 |
widget->setProperty("validationState", on ? QStringLiteral("error") : QVariant()); |
| 949 |
|
| 950 |
widget->style()->unpolish(widget); |
| 951 |
widget->style()->polish(widget); |
| 952 |
} |
| 953 |
|
| 954 |
bool DlgExpressionInput::reportGroup(const QString& nameGroup) |
| 955 |
{ |
| 956 |
QString message; |
| 957 |
if (!isGroupNameValid(nameGroup, message)) { |
| 958 |
setErrorState(&comboBoxGroup, true); |
| 959 |
reportVarSetInfo(message); |
| 960 |
return true; |
| 961 |
} |
| 962 |
|
| 963 |
return false; |
| 964 |
} |
| 965 |
|
| 966 |
bool DlgExpressionInput::reportName() |
| 967 |
{ |
| 968 |
QString nameProp = ui->lineEditPropNew->text(); |
| 969 |
QString nameVarSet = getValue(ui->comboBoxVarSet, VarSetNameRole); |
| 970 |
QString nameDoc = getValue(ui->comboBoxVarSet, DocRole); |
| 971 |
App::Document* doc = App::GetApplication().getDocument(nameDoc.toUtf8()); |
| 972 |
App::DocumentObject* obj = doc->getObject(nameVarSet.toUtf8()); |
| 973 |
QString message; |
| 974 |
if (!isPropertyNameValid(nameProp, obj, message)) { |
| 975 |
setErrorState(ui->lineEditPropNew, true); |
| 976 |
reportVarSetInfo(message); |
| 977 |
return true; |
| 978 |
} |
| 979 |
|
| 980 |
return false; |
| 981 |
} |
| 982 |
|
| 983 |
void DlgExpressionInput::updateVarSetInfo(bool checkExpr) |
| 984 |
{ |
| 985 |
if (ui->lineEditPropNew->text().isEmpty()) { |
| 986 |
okBtn->setEnabled(false); |
| 987 |
return; |
| 988 |
} |
| 989 |
|
| 990 |
if (comboBoxGroup.currentText().isEmpty()) { |
| 991 |
okBtn->setEnabled(false); |
| 992 |
return; |
| 993 |
} |
| 994 |
|
| 995 |
if (checkCyclicDependencyVarSet(ui->expression->toPlainText())) { |
| 996 |
okBtn->setEnabled(false); |
| 997 |
return; |
| 998 |
} |
| 999 |
|
| 1000 |
if (checkExpr) { |
| 1001 |
// We have to check the text of the expression as well |
| 1002 |
try { |
| 1003 |
checkExpression(ui->expression->toPlainText()); |
| 1004 |
} |
| 1005 |
catch (Base::Exception&) { |
| 1006 |
okBtn->setEnabled(false); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
okBtn->setEnabled(true); |
| 1011 |
} |
| 1012 |
|
| 1013 |
bool DlgExpressionInput::needReportOnVarSet() |
| 1014 |
{ |
| 1015 |
setErrorState(ui->lineEditPropNew, false); |
| 1016 |
setErrorState(&comboBoxGroup, false); |
| 1017 |
|
| 1018 |
return reportGroup(comboBoxGroup.currentText()) || reportName(); |
| 1019 |
} |
| 1020 |
|
| 1021 |
void DlgExpressionInput::resizeEvent(QResizeEvent* event) |
| 1022 |
{ |
| 1023 |
// When the dialog is resized, message text may need to be re-wrapped |
| 1024 |
if (!this->message.empty() && event->size() != event->oldSize()) { |
| 1025 |
setMsgText(); |
| 1026 |
} |
| 1027 |
QDialog::resizeEvent(event); |
| 1028 |
} |
| 1029 |
|
| 1030 |
void DlgExpressionInput::setMsgText() |
| 1031 |
{ |
| 1032 |
if (!this->message.size()) { |
| 1033 |
return; |
| 1034 |
} |
| 1035 |
|
| 1036 |
const QFontMetrics msgFontMetrics {ui->msg->font()}; |
| 1037 |
|
| 1038 |
// find words longer than length of msg widget |
| 1039 |
// then insert newline to wrap it |
| 1040 |
std::string wrappedMsg {}; |
| 1041 |
const int msgContentWidth = ui->msg->width() * 0.85; // 0.85 is a magic number for some padding |
| 1042 |
const int maxWordLength = msgContentWidth / msgFontMetrics.averageCharWidth(); |
| 1043 |
|
| 1044 |
const auto wrappableWordPattern = std::regex {"\\S{" + std::to_string(maxWordLength) + "}"}; |
| 1045 |
auto it = std::sregex_iterator {this->message.cbegin(), this->message.cend(), wrappableWordPattern}; |
| 1046 |
const auto itEnd = std::sregex_iterator {}; |
| 1047 |
|
| 1048 |
int lastPos = 0; |
| 1049 |
for (; it != itEnd; ++it) { |
| 1050 |
wrappedMsg += this->message.substr(lastPos, it->position() - lastPos); |
| 1051 |
wrappedMsg += it->str() + "\n"; |
| 1052 |
lastPos = it->position() + it->length(); |
| 1053 |
} |
| 1054 |
wrappedMsg += this->message.substr(lastPos); |
| 1055 |
|
| 1056 |
ui->msg->setText(QString::fromStdString(wrappedMsg)); |
| 1057 |
|
| 1058 |
// elide text if it is going out of widget bounds |
| 1059 |
// note: this is only 'rough elide', as this text is usually not very long; |
| 1060 |
const int msgLinesLimit = 3; |
| 1061 |
if (static_cast<int>(wrappedMsg.size()) |
| 1062 |
> msgContentWidth / msgFontMetrics.averageCharWidth() * msgLinesLimit) { |
| 1063 |
const QString elidedMsg = msgFontMetrics.elidedText( |
| 1064 |
QString::fromStdString(wrappedMsg), |
| 1065 |
Qt::ElideRight, |
| 1066 |
msgContentWidth * msgLinesLimit |
| 1067 |
); |
| 1068 |
ui->msg->setText(elidedMsg); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
#include "moc_DlgExpressionInput.cpp" |