-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Modify charge dialogs qml (#1787)
Co-authored-by: Tristan Youngs <tristan.youngs@stfc.ac.uk>
- Loading branch information
Showing
22 changed files
with
567 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// Copyright (c) 2024 Team Dissolve and contributors | ||
|
||
#include "gui/models/modifyChargesModel.h" | ||
#include <QMessageBox> | ||
|
||
ModifyChargesModel::ModifyChargesModel() {} | ||
|
||
ModifyChargesModel::~ModifyChargesModel() {} | ||
|
||
double ModifyChargesModel::scaleValue() const { return currentScaleValue_; } | ||
|
||
double ModifyChargesModel::smoothValue() const { return currentSmoothValue_; } | ||
|
||
int ModifyChargesModel::sigFigValue() const { return currentSigFigValue_; } | ||
|
||
void ModifyChargesModel::updateScaleValue(double newValue) { currentScaleValue_ = newValue; } | ||
|
||
void ModifyChargesModel::updateSmoothValue(double newValue) { currentSmoothValue_ = newValue; } | ||
|
||
void ModifyChargesModel::updateSigFigValue(int newValue) { currentSigFigValue_ = newValue; } | ||
|
||
void ModifyChargesModel::setScaleType(ScaleType option) { scaleType_ = option; } | ||
|
||
ModifyChargesModel::ScaleType ModifyChargesModel::getScaleType() { return scaleType_; } | ||
|
||
void ModifyChargesModel::setModifier(ModifyChargesModel::ModifyBy option) { modify_ = option; } | ||
|
||
ModifyChargesModel::ModifyBy ModifyChargesModel::getModifier() { return modify_; } | ||
|
||
bool ModifyChargesModel::scale(Species *species, bool showDialogOnError = true) | ||
{ | ||
auto scaleFactor = scaleValue(); | ||
if (scaleType_ != Scale) | ||
{ | ||
auto scaleTarget = scaleFactor; | ||
if (fabs(scaleTarget) < 1.0e-6) | ||
{ | ||
if (showDialogOnError) | ||
{ | ||
QMessageBox::warning(nullptr, "Scale atom charges", "Cannot scale atom charges so they sum to 0.", | ||
QMessageBox::StandardButton::Ok); | ||
} | ||
return false; | ||
} | ||
|
||
auto sum = 0.0; | ||
for (auto &atom : species->atoms()) | ||
sum += atom.charge(); | ||
scaleFactor = scaleTarget / sum; | ||
} | ||
for (auto &atom : species->atoms()) | ||
atom.setCharge(atom.charge() * scaleFactor); | ||
|
||
return true; | ||
} | ||
|
||
void ModifyChargesModel::smooth(Species *species) | ||
{ | ||
auto targetSum = smoothValue(); | ||
auto sum = species->totalCharge(false), shiftVal = 0.0; | ||
shiftVal = (sum - targetSum) / species->nAtoms(); | ||
for (auto &atom : species->atoms()) | ||
atom.setCharge(atom.charge() - shiftVal); | ||
} | ||
|
||
void ModifyChargesModel::reduceSignificantFigures(Species *species) | ||
{ | ||
auto significantFigures = sigFigValue(); | ||
for (auto &atom : species->atoms()) | ||
atom.setCharge(std::round(atom.charge() * std::pow(10, significantFigures)) / std::pow(10, significantFigures)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
// Copyright (c) 2024 Team Dissolve and contributors | ||
|
||
#pragma once | ||
|
||
#include "classes/species.h" | ||
#include <QObject> | ||
|
||
class ModifyChargesModel : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY(double scaleValue READ scaleValue NOTIFY valueSet) | ||
Q_PROPERTY(double smoothValue READ smoothValue NOTIFY valueSet) | ||
Q_PROPERTY(int sigFigValue READ sigFigValue NOTIFY valueSet) | ||
|
||
public: | ||
ModifyChargesModel(); | ||
~ModifyChargesModel(); | ||
|
||
// Enum type to differentiate between operations for mofiying charges | ||
enum ModifyBy | ||
{ | ||
Scaling, | ||
Smoothing, | ||
ReduceSigFig | ||
}; | ||
Q_ENUM(ModifyBy) | ||
|
||
// Enum type to differentiate between usage options for dialog | ||
// Can either "Scale" the current value, or "ScaleTo" a given value | ||
enum ScaleType | ||
{ | ||
Scale, | ||
ScaleTo | ||
}; | ||
Q_ENUM(ScaleType) | ||
|
||
private: | ||
// Contains the current value to be used for scaling charges | ||
double currentScaleValue_{1.0}; | ||
// Contains the current value to be used for smoothing charges | ||
double currentSmoothValue_{0.0}; | ||
// Contains the current value to be used for reducing significant figures on charges | ||
int currentSigFigValue_{3}; | ||
|
||
// Contains the current setting for scale type (Scale or ScaleTo) | ||
ScaleType scaleType_; | ||
// Contains the attribute (scaling, smoothing, sig fig) that will currently be modified by the model | ||
ModifyBy modify_; | ||
|
||
public: | ||
// Sets the new selected scale value | ||
Q_INVOKABLE void updateScaleValue(double); | ||
// Sets the new selected smooth value | ||
Q_INVOKABLE void updateSmoothValue(double); | ||
// Sets the new selected significant figures | ||
Q_INVOKABLE void updateSigFigValue(int); | ||
|
||
// User has chosen either to apply Scale or ScaleTo to the charges | ||
Q_INVOKABLE void setScaleType(ScaleType); | ||
// Returns the current scale type option | ||
ScaleType getScaleType(); | ||
// Returns the current modifier option | ||
ModifyBy getModifier(); | ||
// Sets the current modifier option | ||
void setModifier(ModifyBy); | ||
// Returns the current scale value | ||
double scaleValue() const; | ||
// Returns the current smooth value | ||
double smoothValue() const; | ||
// Returns the current significant figures | ||
int sigFigValue() const; | ||
// Apply scaling operation to species atoms charges | ||
bool scale(Species *, bool); | ||
// Apply smoothing operation to species atoms charges | ||
void smooth(Species *); | ||
// Reduce significant figures on species atoms charges | ||
void reduceSignificantFigures(Species *); | ||
|
||
Q_SIGNALS: | ||
void valueSet(); | ||
void cancelSelection(); | ||
void acceptSelection(); | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.