All: Reformat according to new standard

This commit is contained in:
pre-commit-ci[bot]
2025-11-11 13:49:01 +01:00
committed by Kacper Donat
parent eafd18dac0
commit 25c3ba7338
2390 changed files with 154630 additions and 115818 deletions

View File

@@ -20,12 +20,12 @@
* *
***************************************************************************/
# include <limits>
# include <QContextMenuEvent>
# include <QMenu>
# include <QPixmapCache>
# include <QRegularExpression>
# include <QRegularExpressionMatch>
#include <limits>
#include <QContextMenuEvent>
#include <QMenu>
#include <QPixmapCache>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <App/Application.h>
#include <App/DocumentObject.h>
@@ -46,8 +46,9 @@ using namespace Base;
// --------------------------------------------------------------------
namespace Gui {
class InputValidator : public QValidator
namespace Gui
{
class InputValidator: public QValidator
{
public:
explicit InputValidator(InputField* parent);
@@ -59,23 +60,25 @@ public:
private:
InputField* dptr;
};
}
} // namespace Gui
// --------------------------------------------------------------------
InputField::InputField(QWidget * parent)
: ExpressionLineEdit(parent),
ExpressionWidget(),
validInput(true),
actUnitValue(0),
Maximum(std::numeric_limits<double>::max()),
Minimum(-std::numeric_limits<double>::max()),
StepSize(1.0),
HistorySize(5),
SaveSize(5)
InputField::InputField(QWidget* parent)
: ExpressionLineEdit(parent)
, ExpressionWidget()
, validInput(true)
, actUnitValue(0)
, Maximum(std::numeric_limits<double>::max())
, Minimum(-std::numeric_limits<double>::max())
, StepSize(1.0)
, HistorySize(5)
, SaveSize(5)
{
setValidator(new InputValidator(this));
if (!App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")->GetBool("ComboBoxWheelEventFilter",false)) {
if (!App::GetApplication()
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/General")
->GetBool("ComboBoxWheelEventFilter", false)) {
setFocusPolicy(Qt::WheelFocus);
}
else {
@@ -91,8 +94,8 @@ InputField::InputField(QWidget * parent)
connect(this, &QLineEdit::textChanged, this, &InputField::updateIconLabel);
// Set Margins
// vertical margin, such that `,` won't be clipped to a `.` and similar font descents. Relevant on some OSX versions
// horizontal margin, such that text will not be behind `fx` icon
// vertical margin, such that `,` won't be clipped to a `.` and similar font descents. Relevant
// on some OSX versions horizontal margin, such that text will not be behind `fx` icon
int margin = getMargin();
setTextMargins(margin, margin, margin + iconSize, margin);
@@ -112,36 +115,39 @@ int InputField::getMargin()
InputField::~InputField() = default;
void InputField::bind(const App::ObjectIdentifier &_path)
void InputField::bind(const App::ObjectIdentifier& _path)
{
ExpressionBinding::bind(_path);
auto * prop = freecad_cast<PropertyQuantity*>(getPath().getProperty());
auto* prop = freecad_cast<PropertyQuantity*>(getPath().getProperty());
if (prop)
if (prop) {
actQuantity = Base::Quantity(prop->getValue());
}
DocumentObject * docObj = getPath().getDocumentObject();
DocumentObject* docObj = getPath().getDocumentObject();
if (docObj) {
std::shared_ptr<const Expression> expr(docObj->getExpression(getPath()).expression);
if (expr)
if (expr) {
newInput(QString::fromStdString(expr->toString()));
}
}
// Create document object, to initialize completer
setDocumentObject(docObj);
}
bool InputField::apply(const std::string &propName)
bool InputField::apply(const std::string& propName)
{
if (!ExpressionBinding::apply(propName)) {
Gui::Command::doCommand(Gui::Command::Doc,"%s = %f", propName.c_str(), getQuantity().getValue());
Gui::Command::doCommand(Gui::Command::Doc, "%s = %f", propName.c_str(), getQuantity().getValue());
return true;
}
else
else {
return false;
}
}
bool InputField::apply()
@@ -151,24 +157,26 @@ bool InputField::apply()
QPixmap InputField::getValidationIcon(const char* name, const QSize& size) const
{
QString key = QStringLiteral("%1_%2x%3")
.arg(QString::fromLatin1(name))
.arg(size.width())
.arg(size.height());
QString key
= QStringLiteral("%1_%2x%3").arg(QString::fromLatin1(name)).arg(size.width()).arg(size.height());
QPixmap icon;
if (QPixmapCache::find(key, &icon))
if (QPixmapCache::find(key, &icon)) {
return icon;
}
icon = BitmapFactory().pixmapFromSvg(name, size);
if (!icon.isNull())
if (!icon.isNull()) {
QPixmapCache::insert(key, icon);
}
return icon;
}
void InputField::updateText(const Base::Quantity& quant)
{
if (isBound()) {
std::shared_ptr<const Expression> e(getPath().getDocumentObject()->getExpression(getPath()).expression);
std::shared_ptr<const Expression> e(
getPath().getDocumentObject()->getExpression(getPath()).expression
);
if (e) {
setText(QString::fromStdString(e->toString()));
@@ -179,11 +187,11 @@ void InputField::updateText(const Base::Quantity& quant)
double dFactor;
std::string unitStr;
std::string txt = quant.getUserString(dFactor, unitStr);
actUnitValue = quant.getValue()/dFactor;
actUnitValue = quant.getValue() / dFactor;
setText(QString::fromStdString(txt));
}
void InputField::resizeEvent(QResizeEvent * /*event*/)
void InputField::resizeEvent(QResizeEvent* /*event*/)
{
QSize iconSize = iconLabel->sizeHint();
iconLabel->move(width() - (iconSize.width() + 2 * getMargin()), (height() - iconSize.height()) / 2);
@@ -194,9 +202,9 @@ void InputField::updateIconLabel(const QString& text)
iconLabel->setVisible(text.isEmpty());
}
void InputField::contextMenuEvent(QContextMenuEvent *event)
void InputField::contextMenuEvent(QContextMenuEvent* event)
{
QMenu *editMenu = createStandardContextMenu();
QMenu* editMenu = createStandardContextMenu();
editMenu->setTitle(tr("Edit"));
auto menu = new QMenu(QStringLiteral("InputFieldContextmenu"));
@@ -205,43 +213,46 @@ void InputField::contextMenuEvent(QContextMenuEvent *event)
// datastructure to remember actions for values
std::vector<QString> values;
std::vector<QAction *> actions;
std::vector<QAction*> actions;
// add the history menu part...
std::vector<QString> history = getHistory();
for(const auto & it : history){
for (const auto& it : history) {
actions.push_back(menu->addAction(it));
values.push_back(it);
}
// add the save value portion of the menu
menu->addSeparator();
QAction *SaveValueAction = menu->addAction(tr("Save Value"));
QAction* SaveValueAction = menu->addAction(tr("Save Value"));
std::vector<QString> savedValues = getSavedValues();
for(const auto & savedValue : savedValues){
for (const auto& savedValue : savedValues) {
actions.push_back(menu->addAction(savedValue));
values.push_back(savedValue);
}
// call the menu and wait until its back
QAction *saveAction = menu->exec(event->globalPos());
QAction* saveAction = menu->exec(event->globalPos());
// look what the user has chosen
if(saveAction == SaveValueAction)
if (saveAction == SaveValueAction) {
pushToSavedValues();
else{
int i=0;
for(auto it = actions.begin();it!=actions.end();++it,i++)
if(*it == saveAction)
}
else {
int i = 0;
for (auto it = actions.begin(); it != actions.end(); ++it, i++) {
if (*it == saveAction) {
this->setText(values[i]);
}
}
}
delete menu;
}
void InputField::newInput(const QString & text)
void InputField::newInput(const QString& text)
{
Quantity res;
try {
@@ -249,22 +260,25 @@ void InputField::newInput(const QString & text)
fixup(input);
if (isBound()) {
std::shared_ptr<Expression> e(ExpressionParser::parse(getPath().getDocumentObject(), input.toUtf8()));
std::shared_ptr<Expression> e(
ExpressionParser::parse(getPath().getDocumentObject(), input.toUtf8())
);
setExpression(e);
std::unique_ptr<Expression> evalRes(getExpression()->eval());
auto * value = freecad_cast<NumberExpression*>(evalRes.get());
auto* value = freecad_cast<NumberExpression*>(evalRes.get());
if (value) {
res.setValue(value->getValue());
res.setUnit(value->getUnit());
}
}
else
else {
res = Quantity::parse(input.toStdString());
}
}
catch(Base::Exception &e){
catch (Base::Exception& e) {
QString errorText = QString::fromLatin1(e.what());
if (iconLabel->isHidden()) {
iconLabel->setVisible(true);
@@ -274,11 +288,12 @@ void InputField::newInput(const QString & text)
return;
}
if (res.isDimensionless())
if (res.isDimensionless()) {
res.setUnit(this->actUnit);
}
// check if unit fits!
if (actUnit != Unit::One && !res.isDimensionless() && actUnit != res.getUnit()){
if (actUnit != Unit::One && !res.isDimensionless() && actUnit != res.getUnit()) {
if (iconLabel->isHidden()) {
iconLabel->setVisible(true);
}
@@ -292,17 +307,17 @@ void InputField::newInput(const QString & text)
}
validInput = true;
if (res.getValue() > Maximum){
if (res.getValue() > Maximum) {
res.setValue(Maximum);
}
if (res.getValue() < Minimum){
if (res.getValue() < Minimum) {
res.setValue(Minimum);
}
double dFactor;
std::string unitStr;
res.getUserString(dFactor, unitStr);
actUnitValue = res.getValue()/dFactor;
actUnitValue = res.getValue() / dFactor;
// Preserve previous format
res.setFormat(this->actQuantity.getFormat());
actQuantity = res;
@@ -312,32 +327,37 @@ void InputField::newInput(const QString & text)
Q_EMIT valueChanged(res.getValue());
}
void InputField::pushToHistory(const QString &valueq)
void InputField::pushToHistory(const QString& valueq)
{
QString val;
if(valueq.isEmpty())
if (valueq.isEmpty()) {
val = this->text();
else
}
else {
val = valueq;
}
// check if already in:
std::vector<QString> hist = InputField::getHistory();
for(const auto & it : hist)
if( it == val)
for (const auto& it : hist) {
if (it == val) {
return;
}
}
std::string value(val.toUtf8());
if(_handle.isValid()){
if (_handle.isValid()) {
char hist1[21];
char hist0[21];
for(int i = HistorySize -1 ; i>=0 ;i--){
snprintf(hist1,20,"Hist%i",i+1);
snprintf(hist0,20,"Hist%i",i);
std::string tHist = _handle->GetASCII(hist0,"");
if(!tHist.empty())
_handle->SetASCII(hist1,tHist.c_str());
for (int i = HistorySize - 1; i >= 0; i--) {
snprintf(hist1, 20, "Hist%i", i + 1);
snprintf(hist0, 20, "Hist%i", i);
std::string tHist = _handle->GetASCII(hist0, "");
if (!tHist.empty()) {
_handle->SetASCII(hist1, tHist.c_str());
}
}
_handle->SetASCII("Hist0",value.c_str());
_handle->SetASCII("Hist0", value.c_str());
}
}
@@ -345,16 +365,18 @@ std::vector<QString> InputField::getHistory()
{
std::vector<QString> res;
if(_handle.isValid()){
if (_handle.isValid()) {
std::string tmp;
char hist[21];
for(int i = 0 ; i< HistorySize ;i++){
snprintf(hist,20,"Hist%i",i);
tmp = _handle->GetASCII(hist,"");
if( !tmp.empty())
for (int i = 0; i < HistorySize; i++) {
snprintf(hist, 20, "Hist%i", i);
tmp = _handle->GetASCII(hist, "");
if (!tmp.empty()) {
res.push_back(QString::fromUtf8(tmp.c_str()));
else
break; // end of history reached
}
else {
break; // end of history reached
}
}
}
return res;
@@ -363,29 +385,33 @@ std::vector<QString> InputField::getHistory()
void InputField::setToLastUsedValue()
{
std::vector<QString> hist = getHistory();
if(!hist.empty())
if (!hist.empty()) {
this->setText(hist[0]);
}
}
void InputField::pushToSavedValues(const QString &valueq)
void InputField::pushToSavedValues(const QString& valueq)
{
std::string value;
if(valueq.isEmpty())
if (valueq.isEmpty()) {
value = this->text().toUtf8().constData();
else
}
else {
value = valueq.toUtf8().constData();
}
if(_handle.isValid()){
if (_handle.isValid()) {
char hist1[21];
char hist0[21];
for(int i = SaveSize -1 ; i>=0 ;i--){
snprintf(hist1,20,"Save%i",i+1);
snprintf(hist0,20,"Save%i",i);
std::string tHist = _handle->GetASCII(hist0,"");
if(!tHist.empty())
_handle->SetASCII(hist1,tHist.c_str());
for (int i = SaveSize - 1; i >= 0; i--) {
snprintf(hist1, 20, "Save%i", i + 1);
snprintf(hist0, 20, "Save%i", i);
std::string tHist = _handle->GetASCII(hist0, "");
if (!tHist.empty()) {
_handle->SetASCII(hist1, tHist.c_str());
}
}
_handle->SetASCII("Save0",value.c_str());
_handle->SetASCII("Save0", value.c_str());
}
}
@@ -393,34 +419,38 @@ std::vector<QString> InputField::getSavedValues()
{
std::vector<QString> res;
if(_handle.isValid()){
if (_handle.isValid()) {
std::string tmp;
char hist[21];
for(int i = 0 ; i< SaveSize ;i++){
snprintf(hist,20,"Save%i",i);
tmp = _handle->GetASCII(hist,"");
if( !tmp.empty())
for (int i = 0; i < SaveSize; i++) {
snprintf(hist, 20, "Save%i", i);
tmp = _handle->GetASCII(hist, "");
if (!tmp.empty()) {
res.push_back(QString::fromUtf8(tmp.c_str()));
else
break; // end of history reached
}
else {
break; // end of history reached
}
}
}
return res;
}
/** Sets the preference path to \a path. */
void InputField::setParamGrpPath( const QByteArray& path )
void InputField::setParamGrpPath(const QByteArray& path)
{
_handle = App::GetApplication().GetParameterGroupByPath( path);
if (_handle.isValid())
_handle = App::GetApplication().GetParameterGroupByPath(path);
if (_handle.isValid()) {
sGroupString = (const char*)path;
}
}
/** Returns the widget's preferences path. */
QByteArray InputField::paramGrpPath() const
{
if(_handle.isValid())
if (_handle.isValid()) {
return sGroupString.c_str();
}
return {};
}
@@ -429,10 +459,12 @@ void InputField::setValue(const Base::Quantity& quant)
{
actQuantity = quant;
// check limits
if (actQuantity.getValue() > Maximum)
if (actQuantity.getValue() > Maximum) {
actQuantity.setValue(Maximum);
if (actQuantity.getValue() < Minimum)
}
if (actQuantity.getValue() < Minimum) {
actQuantity.setValue(Minimum);
}
actUnit = quant.getUnit();
@@ -478,7 +510,7 @@ void InputField::setQuantityString(const QString& text)
/// return the quantity in C locale, i.e. decimal separator is a dot.
QString InputField::rawText() const
{
double factor;
double factor;
std::string unit;
double value = actQuantity.getValue();
actQuantity.getUserString(factor, unit);
@@ -495,7 +527,7 @@ void InputField::setRawText(const QString& text)
}
/// get the value of the singleStep property
double InputField::singleStep()const
double InputField::singleStep() const
{
return StepSize;
}
@@ -507,7 +539,7 @@ void InputField::setSingleStep(double s)
}
/// get the value of the maximum property
double InputField::maximum()const
double InputField::maximum() const
{
return Maximum;
}
@@ -523,7 +555,7 @@ void InputField::setMaximum(double m)
}
/// get the value of the minimum property
double InputField::minimum()const
double InputField::minimum() const
{
return Minimum;
}
@@ -577,8 +609,9 @@ QString InputField::getFormat() const
void InputField::setFormat(const QString& format)
{
if (format.isEmpty())
if (format.isEmpty()) {
return;
}
QChar c = format[0];
Base::QuantityFormat f = this->actQuantity.getFormat();
f.format = Base::QuantityFormat::toFormat(c.toLatin1());
@@ -587,7 +620,7 @@ void InputField::setFormat(const QString& format)
}
// get the value of the minimum property
int InputField::historySize()const
int InputField::historySize() const
{
return HistorySize;
}
@@ -595,8 +628,8 @@ int InputField::historySize()const
// set the value of the minimum property
void InputField::setHistorySize(int i)
{
assert(i>=0);
assert(i<100);
assert(i >= 0);
assert(i < 100);
HistorySize = i;
}
@@ -604,40 +637,41 @@ void InputField::setHistorySize(int i)
void InputField::selectNumber()
{
QString expr = QStringLiteral("^([%1%2]?[0-9\\%3]*)\\%4?([0-9]+(%5[%1%2]?[0-9]+)?)")
.arg(locale().negativeSign())
.arg(locale().positiveSign())
.arg(locale().groupSeparator())
.arg(locale().decimalPoint())
.arg(locale().exponential());
.arg(locale().negativeSign())
.arg(locale().positiveSign())
.arg(locale().groupSeparator())
.arg(locale().decimalPoint())
.arg(locale().exponential());
auto rmatch = QRegularExpression(expr).match(text());
if (rmatch.hasMatch()) {
setSelection(0, rmatch.capturedLength());
}
}
void InputField::showEvent(QShowEvent * event)
void InputField::showEvent(QShowEvent* event)
{
QLineEdit::showEvent(event);
bool selected = this->hasSelectedText();
updateText(actQuantity);
if (selected)
if (selected) {
selectNumber();
}
}
void InputField::focusInEvent(QFocusEvent *event)
void InputField::focusInEvent(QFocusEvent* event)
{
if (event->reason() == Qt::TabFocusReason ||
event->reason() == Qt::BacktabFocusReason ||
event->reason() == Qt::ShortcutFocusReason) {
if (!this->hasSelectedText())
if (event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason
|| event->reason() == Qt::ShortcutFocusReason) {
if (!this->hasSelectedText()) {
selectNumber();
}
}
QLineEdit::focusInEvent(event);
}
void InputField::focusOutEvent(QFocusEvent *event)
void InputField::focusOutEvent(QFocusEvent* event)
{
try {
if (Quantity::parse(this->text().toStdString()).isDimensionless()) {
@@ -656,7 +690,7 @@ void InputField::focusOutEvent(QFocusEvent *event)
QLineEdit::focusOutEvent(event);
}
void InputField::keyPressEvent(QKeyEvent *event)
void InputField::keyPressEvent(QKeyEvent* event)
{
if (isReadOnly()) {
QLineEdit::keyPressEvent(event);
@@ -666,19 +700,21 @@ void InputField::keyPressEvent(QKeyEvent *event)
double val = actUnitValue;
switch (event->key()) {
case Qt::Key_Up:
val += StepSize;
if (val > Maximum)
val = Maximum;
break;
case Qt::Key_Down:
val -= StepSize;
if (val < Minimum)
val = Minimum;
break;
default:
QLineEdit::keyPressEvent(event);
return;
case Qt::Key_Up:
val += StepSize;
if (val > Maximum) {
val = Maximum;
}
break;
case Qt::Key_Down:
val -= StepSize;
if (val < Minimum) {
val = Minimum;
}
break;
default:
QLineEdit::keyPressEvent(event);
return;
}
double dFactor;
@@ -688,10 +724,11 @@ void InputField::keyPressEvent(QKeyEvent *event)
event->accept();
}
void InputField::wheelEvent (QWheelEvent * event)
void InputField::wheelEvent(QWheelEvent* event)
{
if (!hasFocus())
if (!hasFocus()) {
return;
}
if (isReadOnly()) {
QLineEdit::wheelEvent(event);
@@ -701,10 +738,12 @@ void InputField::wheelEvent (QWheelEvent * event)
double factor = event->modifiers() & Qt::ControlModifier ? 10 : 1;
double step = event->angleDelta().y() > 0 ? StepSize : -StepSize;
double val = actUnitValue + factor * step;
if (val > Maximum)
if (val > Maximum) {
val = Maximum;
else if (val < Minimum)
}
else if (val < Minimum) {
val = Minimum;
}
double dFactor;
std::string unitStr;
@@ -744,12 +783,13 @@ QValidator::State InputField::validate(QString& input, int& pos) const
double factor;
std::string unitStr;
res.getUserString(factor, unitStr);
double value = res.getValue()/factor;
double value = res.getValue() / factor;
// disallow one to enter numbers out of range
if (value > this->Maximum || value < this->Minimum)
if (value > this->Maximum || value < this->Minimum) {
return QValidator::Invalid;
}
}
catch(Base::Exception&) {
catch (Base::Exception&) {
// Actually invalid input but the newInput slot gives
// some feedback
return QValidator::Intermediate;
@@ -760,9 +800,10 @@ QValidator::State InputField::validate(QString& input, int& pos) const
// --------------------------------------------------------------------
InputValidator::InputValidator(InputField* parent) : QValidator(parent), dptr(parent)
{
}
InputValidator::InputValidator(InputField* parent)
: QValidator(parent)
, dptr(parent)
{}
InputValidator::~InputValidator() = default;