Repository: FreeCAD/FreeCAD
File: src/Mod/Measure/Gui/TaskMeasure.cpp (C++)

Code before:
{
    if (this->hasSelection()) {
        this->reset();
    }
    else {
        this->reject();
    }
}

void TaskMeasure::onObjectDeleted(const App::DocumentObject& obj)
{
    if (&obj == _mMeasureObject) {
        _mMeasureObject = nullptr;
    }
}

void TaskMeasure::setDeltaPossible(bool possible)
{
    showDelta->setVisible(possible);
    showDeltaLabel->setVisible(possible);
}

void TaskMeasure::onModeChanged(int index)
{
    explicitMode = (index != 0);

    this->update();
}

void TaskMeasure::onUnitChanged(int index)
{
    QString currentUnit = unitSwitch->itemText(index);

    if (mLastUnitSelection != QLatin1String("-") && currentUnit != mLastUnitSelection) {
        updateResultWithUnit();
    }
    else if (mLastUnitSelection == QLatin1String("-") && currentUnit != QLatin1String("-")) {
        updateResultWithUnit();
    }

    mLastUnitSelection = currentUnit;
}

void TaskMeasure::showDeltaChanged(int checkState)
{
    delta = checkState == Qt::CheckState::Checked;

    QSettings settings;
    settings.beginGroup(QLatin1String(taskMeasureSettingsGroup));
    settings.setValue(QLatin1String(taskMeasureShowDeltaSettingsName), delta);
    settings.endGroup();

Review comment (bug):
Correct me if I'm wrong, but what if we rewrite this as:

```c++
const auto dash = QLatin1String("-");

if (
    (mLastUnitSelection != dash && currentUnit != mLastUnitSelection) ||
    (mLastUnitSelection == dash && currentUnit != dash)
) {
    updateResultWithUnit();
}
```

now notice that in the second clause, `mLastUnitSelection == "-"` and `currentUnit != "-"` implies `currentUnit != mLastUnitSelection` (since they can’t be equal if one is exactly `"-"` and the other isn’t). so we can safely add that extra check without changing behavior:

```c++
if (
    (mLastUnitSelection != dash && currentUnit != mLastUnitSelection) ||
    (mLastUnitSelection == dash && currentUnit != dash && currentUnit != mLastUnitSelection)
) {
    updateResultWithUnit();
}
```

and then we can factor out the common `currentUnit != mLastUnitSelection`:

```c++
if (
    (currentUnit != mLastUnitSelection) &&
    ((mLastUnitSelection != dash) || (mLastUnitSelection == dash && currentUnit != dash))
) {
    updateResultWithUnit();
}
```

the remaining part simplifies because:

* if `mLastUnitSelection != dash`, it’s true
* else (`mLastUnitSelection == dash`), it’s true iff `currentUnit != dash`

which is just: “at least one of them isn’t `"-"`”:

```c++
if (
    (currentUnit != mLastUnitSelection) &&
    (mLastUnitSelection != dash || currentUnit != dash)
) {
    updateResultWithUnit();
}
```

so the whole handler becomes:

```c++
void TaskMeasure::onUnitChanged(int index)
{
    const QString currentUnit = unitSwitch->itemText(index);
    const auto dash = QLatin1String("-");

    if (currentUnit != mLastUnitSelection &&
        (mLastUnitSelection != dash || currentUnit != dash))
    {
        updateResultWithUnit();
    }

    mLastUnitSelection = currentUnit;
}
```

or did I miss something? 🤔 

Code after:
    if (&obj == _mMeasureObject) {
        _mMeasureObject = nullptr;
    }
}

void TaskMeasure::setDeltaPossible(bool possible)
{
    showDelta->setVisible(possible);
    showDeltaLabel->setVisible(possible);
}

void TaskMeasure::onModeChanged(int index)
{
    explicitMode = (index != 0);

    this->update();
}

void TaskMeasure::onUnitChanged(int index)
{
    const QString currentUnit = unitSwitch->itemText(index);
    const auto dash = QLatin1String("-");

    if (currentUnit != mLastUnitSelection && (mLastUnitSelection != dash || currentUnit != dash)) {
        updateResultWithUnit();
    }

    mLastUnitSelection = currentUnit;
}

void TaskMeasure::showDeltaChanged(int checkState)
{
    delta = checkState == Qt::CheckState::Checked;

    QSettings settings;
    settings.beginGroup(QLatin1String(taskMeasureSettingsGroup));
    settings.setValue(QLatin1String(taskMeasureShowDeltaSettingsName), delta);
    settings.endGroup();
    settings.sync();  // immediate write to the settings file

    this->update();
}

void TaskMeasure::autoSaveChanged(bool checked)
{
    mAutoSave = checked;

    QSettings settings;
    settings.beginGroup(QLatin1String(taskMeasureSettingsGroup));
    settings.setValue(QLatin1String(taskMeasureAutoSaveSettingsName), mAutoSave);
    settings.endGroup();