Skip to content

Commit

Permalink
refactor: Modify charge dialogs qml (#1787)
Browse files Browse the repository at this point in the history
Co-authored-by: Tristan Youngs <tristan.youngs@stfc.ac.uk>
  • Loading branch information
2 people authored and rprospero committed Mar 11, 2024
1 parent fdce223 commit 7fa5fba
Show file tree
Hide file tree
Showing 22 changed files with 567 additions and 336 deletions.
6 changes: 2 additions & 4 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ set(gui_MOC_HDRS
# Dialogs
addConfigurationDialog.h
addForcefieldTermsDialog.h
scaleChargesDialog.h
copySpeciesTermsDialog.h
dataManagerDialog.h
editSpeciesDialog.h
Expand All @@ -39,7 +38,7 @@ set(gui_MOC_HDRS
importCIFDialog.h
importSpeciesDialog.h
intramolecularTermsDialog.h
scaleChargesDialog.h
modifyChargesDialog.h
selectAtomTypeDialog.h
selectElementDialog.h
selectGenericItemDialog.h
Expand Down Expand Up @@ -149,7 +148,6 @@ set(gui_SRCS
# Dialogs
addConfigurationDialogFuncs.cpp
addForcefieldTermsDialogFuncs.cpp
scaleChargesDialogFuncs.cpp
copySpeciesTermsDialogFuncs.cpp
dataManagerDialogFuncs.cpp
editSpeciesDialogFuncs.cpp
Expand All @@ -161,7 +159,7 @@ set(gui_SRCS
importCIFDialogFuncs.cpp
importSpeciesDialogFuncs.cpp
intramolecularTermsDialogFuncs.cpp
scaleChargesDialogFuncs.cpp
modifyChargesDialog.cpp
selectAtomTypeDialogFuncs.cpp
selectElementDialogFuncs.cpp
selectGenericItemDialogFuncs.cpp
Expand Down
8 changes: 8 additions & 0 deletions src/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "base/timer.h"
#include "gui/mainTab.h"
#include "gui/models/modifyChargesModel.h"
#include "gui/outputHandler.hui"
#include "gui/signals.h"
#include "gui/thread.hui"
Expand Down Expand Up @@ -108,6 +109,13 @@ class DissolveWindow : public QMainWindow
void updateWhileRunning(int iterationsRemaining);
// Clear the messages window
void clearMessages();

/*
* Shared Dialog Models
*/
private:
ModifyChargesModel modifyChargesModel_;

/*
* Main Menu
*/
Expand Down
4 changes: 3 additions & 1 deletion src/gui/main.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@
<file>qml/widgets/Text.qml</file>
<file>qml/widgets/PrettyListView.qml</file>
<file>qml/AddForcefieldTermsDialog.qml</file>
<file>qml/ScaleChargesDialog.qml</file>
<file>qml/modifyCharges/Loader.qml</file>
<file>qml/modifyCharges/Layout.qml</file>
<file>qml/modifyCharges/ScaleLayout.qml</file>
<file>qml/DropDownLabel.qml</file>
<file>qml/ForceFieldAssign.qml</file>
<file>qml/ForceFieldAtomTypes.qml</file>
Expand Down
48 changes: 18 additions & 30 deletions src/gui/menu_species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include "gui/importCIFDialog.h"
#include "gui/importLigParGenDialog.h"
#include "gui/importSpeciesDialog.h"
#include "gui/scaleChargesDialog.h"
#include "gui/models/modifyChargesModel.h"
#include "gui/modifyChargesDialog.h"
#include "gui/selectAtomTypeDialog.h"
#include "gui/selectElementDialog.h"
#include "gui/selectSpeciesDialog.h"
Expand Down Expand Up @@ -434,10 +435,10 @@ void DissolveWindow::on_SpeciesScaleChargesAction_triggered(bool checked)
if (!species)
return;

static ScaleChargesDialog dialog(this);
ModifyChargesDialog dialog(this, &modifyChargesModel_, ModifyChargesModel::Scaling);
if (dialog.exec() == QDialog::Accepted)
{
auto success = dialog.model->scaleCharges(species, true);
auto success = modifyChargesModel_.scale(species, true);
setModified();
fullUpdate();
}
Expand All @@ -450,20 +451,13 @@ void DissolveWindow::on_SpeciesSmoothChargesAction_triggered(bool checked)
if (!species)
return;

auto ok = false;
auto targetSum = QInputDialog::getDouble(this, "Smooth atom charges", "Enter the target sum to smooth atom charges to.",
0.0, -100.0, 100.0, 5, &ok);

if (!ok)
return;

auto sum = species->totalCharge(false), shiftVal = 0.0;
shiftVal = (sum - targetSum) / species->nAtoms();
for (auto &atom : species->atoms())
atom.setCharge(atom.charge() - shiftVal);

setModified();
fullUpdate();
ModifyChargesDialog dialog(this, &modifyChargesModel_, ModifyChargesModel::Smoothing);
if (dialog.exec() == QDialog::Accepted)
{
modifyChargesModel_.smooth(species);
setModified();
fullUpdate();
}
}

void DissolveWindow::on_SpeciesReduceChargesSigFigsAction_triggered(bool checked)
Expand All @@ -473,17 +467,11 @@ void DissolveWindow::on_SpeciesReduceChargesSigFigsAction_triggered(bool checked
if (!species)
return;

auto ok = false;
auto significantFigures =
QInputDialog::getInt(this, "Reduce Signficant Figures in Charges",
"Enter the number of significant figures to use for all atoms", 3, 1, 100, 1, &ok);
if (!ok)
return;

for (auto &atom : species->atoms())
atom.setCharge(std::round(atom.charge() * std::pow(10, significantFigures)) / std::pow(10, significantFigures));

setModified();

fullUpdate();
ModifyChargesDialog dialog(this, &modifyChargesModel_, ModifyChargesModel::ReduceSigFig);
if (dialog.exec() == QDialog::Accepted)
{
modifyChargesModel_.reduceSignificantFigures(species);
setModified();
fullUpdate();
}
}
4 changes: 2 additions & 2 deletions src/gui/models/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(models_MOC_HDRS
masterTermModel.h
masterTermTreeModel.h
masterTorsionModel.h
modifyChargesModel.h
moduleLayerModel.h
moduleLayersModel.h
moduleModel.h
Expand All @@ -28,7 +29,6 @@ set(models_MOC_HDRS
procedureNodeModel.h
rangeVectorModel.h
renderableGroupManagerModel.h
scaleChargesDialogModel.h
sitesFilterProxy.h
sitesModel.h
speciesFilterProxy.h
Expand Down Expand Up @@ -79,6 +79,7 @@ set(models_SRCS
masterTermModel.cpp
masterTermTreeModel.cpp
masterTorsionModel.cpp
modifyChargesModel.cpp
moduleLayerModel.cpp
moduleLayersModel.cpp
moduleModel.cpp
Expand All @@ -92,7 +93,6 @@ set(models_SRCS
procedureNodeModel.cpp
rangeVectorModel.cpp
renderableGroupManagerModel.cpp
scaleChargesDialogModel.cpp
sitesFilterProxy.cpp
sitesModel.cpp
speciesAngleModel.cpp
Expand Down
72 changes: 72 additions & 0 deletions src/gui/models/modifyChargesModel.cpp
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));
}
85 changes: 85 additions & 0 deletions src/gui/models/modifyChargesModel.h
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();
};
44 changes: 0 additions & 44 deletions src/gui/models/scaleChargesDialogModel.cpp

This file was deleted.

0 comments on commit 7fa5fba

Please sign in to comment.