Skip to content

Commit

Permalink
display qml scale charges dialog (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobBuchananCompPhys committed Jan 26, 2024
1 parent 36447f1 commit 4e67ebe
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 133 deletions.
3 changes: 2 additions & 1 deletion src/gui/CMakeLists.txt
Expand Up @@ -27,6 +27,7 @@ set(gui_MOC_HDRS
# Dialogs
addConfigurationDialog.h
addForcefieldTermsDialog.h
scaleChargesDialog.h
copySpeciesTermsDialog.h
dataManagerDialog.h
editSpeciesDialog.h
Expand Down Expand Up @@ -92,7 +93,6 @@ set(gui_UIS
importCIFDialog.ui
importSpeciesDialog.ui
intramolecularTermsDialog.ui
scaleChargesDialog.ui
selectAtomTypeDialog.ui
selectElementDialog.ui
selectGenericItemDialog.ui
Expand Down Expand Up @@ -149,6 +149,7 @@ set(gui_SRCS
# Dialogs
addConfigurationDialogFuncs.cpp
addForcefieldTermsDialogFuncs.cpp
scaleChargesDialogFuncs.cpp
copySpeciesTermsDialogFuncs.cpp
dataManagerDialogFuncs.cpp
editSpeciesDialogFuncs.cpp
Expand Down
1 change: 1 addition & 0 deletions src/gui/main.qrc
Expand Up @@ -191,6 +191,7 @@
<file>qml/widgets/Text.qml</file>
<file>qml/widgets/PrettyListView.qml</file>
<file>qml/AddForcefieldTermsDialog.qml</file>
<file>qml/ScaleChargesDialog.qml</file>
<file>qml/DropDownLabel.qml</file>
<file>qml/ForceFieldAssign.qml</file>
<file>qml/ForceFieldAtomTypes.qml</file>
Expand Down
25 changes: 3 additions & 22 deletions src/gui/menu_species.cpp
Expand Up @@ -434,29 +434,10 @@ void DissolveWindow::on_SpeciesScaleChargesAction_triggered(bool checked)
if (!species)
return;

static ScaleChargesDialog scaleChargesDialog(this);
double scaleFactor = 1.0;
if (scaleChargesDialog.exec() == QDialog::Accepted)
static ScaleChargesDialog dialog(this);
if (dialog.exec() == QDialog::Accepted)
{
if (scaleChargesDialog.scale_)
scaleFactor = scaleChargesDialog.scaleValue();
else
{
double scaleTarget = scaleChargesDialog.scaleValue();
if (scaleTarget == 0.0)
{
QMessageBox::warning(this, "Scale atom charges", "Cannot scale atom charges so they sum to 0.",
QMessageBox::StandardButton::Ok);
return;
}

double sum = 0.0;
for (auto &atom : species->atoms())
sum += atom.charge();
scaleFactor = scaleTarget / sum;
}
for (auto &atom : species->atoms())
atom.setCharge(atom.charge() * scaleFactor);
auto success = dialog.model->scaleCharges(species, true);
setModified();
fullUpdate();
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/models/CMakeLists.txt
Expand Up @@ -28,6 +28,7 @@ set(models_MOC_HDRS
procedureNodeModel.h
rangeVectorModel.h
renderableGroupManagerModel.h
scaleChargesDialogModel.h
sitesFilterProxy.h
sitesModel.h
speciesFilterProxy.h
Expand Down Expand Up @@ -91,6 +92,7 @@ set(models_SRCS
procedureNodeModel.cpp
rangeVectorModel.cpp
renderableGroupManagerModel.cpp
scaleChargesDialogModel.cpp
sitesFilterProxy.cpp
sitesModel.cpp
speciesAngleModel.cpp
Expand Down
44 changes: 44 additions & 0 deletions src/gui/models/scaleChargesDialogModel.cpp
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2024 Team Dissolve and contributors

#include "gui/models/scaleChargesDialogModel.h"
#include <QMessageBox>

ScaleChargesDialogModel::ScaleChargesDialogModel() {}

ScaleChargesDialogModel::~ScaleChargesDialogModel() {}

double ScaleChargesDialogModel::value() const { return currentValue_; }

void ScaleChargesDialogModel::updateValue(double newVal) { currentValue_ = newVal; }

void ScaleChargesDialogModel::setOption(Option option) { scaleType_ = option; }

ScaleChargesDialogModel::Option ScaleChargesDialogModel::getOption() { return scaleType_; }

bool ScaleChargesDialogModel::scaleCharges(Species *species, bool showDialogOnError = true)
{
auto scaleFactor = value();
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;
}
49 changes: 49 additions & 0 deletions src/gui/models/scaleChargesDialogModel.h
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2024 Team Dissolve and contributors

#pragma once

#include "classes/species.h"
#include <QObject>

class ScaleChargesDialogModel : public QObject
{
Q_OBJECT

Q_PROPERTY(double value READ value NOTIFY valueSet)

public:
ScaleChargesDialogModel();
~ScaleChargesDialogModel();

// Enum type to differentiate between usage options for dialog
// Can either "Scale" the current value, or "ScaleTo" a given value
typedef enum
{
Scale,
ScaleTo
} Option;
Q_ENUM(Option)

// User has chosen either to apply Scale or ScaleTo to the charges
Q_INVOKABLE void setOption(Option);
// Sets the new selected scale value
Q_INVOKABLE void updateValue(double);

// Returns the current option
Option getOption();

// Returns the current scale value
double value() const;
// Apply scaling operation to species atoms
bool scaleCharges(Species *, bool);

private:
double currentValue_{1.0};
Option scaleType_;

Q_SIGNALS:
void valueSet();
void cancelSelection();
void acceptSelection();
};
88 changes: 88 additions & 0 deletions src/gui/qml/ScaleChargesDialog.qml
@@ -0,0 +1,88 @@
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Dissolve
import "widgets" as D

Page {
id: root
property double value

height: 116
title: "Scale Charges"
visible: true
width: 400

ScaleChargesDialogModel {
id: dialogModel
objectName: "dialogModel"

function processSelection(mode, x) {
dialogModel.setOption(mode);
dialogModel.updateValue(x);
}
}
Item {
anchors.fill: parent
anchors.margins: 10

ColumnLayout {
anchors.fill: parent
spacing: 10

D.Text {
Layout.fillHeight: true
Layout.fillWidth: true
font.pixelSize: 11
text: "Enter the scaling factor to apply to all atoms / the target sum to determine scaling factor from"
width: parent.width - 2 * parent.spacing
wrapMode: Text.WordWrap
}
SpinBox {
id: scaleSpinBox
Layout.alignment: Qt.AlignRight
Layout.fillWidth: true
editable: true
from: -100
stepSize: 1
to: 100
value: dialogModel.value
}
RowLayout {
Layout.alignment: Qt.AlignRight
spacing: 10

D.Button {
id: cancelButton
icon.source: "qrc:/general/icons/false.svg"
text: "Cancel"

onClicked: {
dialogModel.cancelSelection();
scaleSpinBox.value = dialogModel.value;
}
}
D.Button {
id: scaleButton
icon.source: "qrc:/general/icons/true.svg"
text: "Scale"

onClicked: {
dialogModel.processSelection(dialogModel.Scale, scaleSpinBox.value);
dialogModel.acceptSelection();
}
}
D.Button {
id: scaleToButton
icon.source: "qrc:/general/icons/true.svg"
text: "Scale To"

onClicked: {
dialogModel.processSelection(dialogModel.ScaleTo, scaleSpinBox.value);
dialogModel.acceptSelection();
}
}
}
}
}
}
18 changes: 4 additions & 14 deletions src/gui/scaleChargesDialog.h
@@ -1,9 +1,9 @@
// spdx-license-identifier: gpl-3.0-or-later
// copyright (c) 2023 team dissolve and contributors
// copyright (c) 2024 team dissolve and contributors

#pragma once

#include "gui/ui_scaleChargesDialog.h"
#include "gui/models/scaleChargesDialogModel.h"
#include <QDialog>

class ScaleChargesDialog : public QDialog
Expand All @@ -12,17 +12,7 @@ class ScaleChargesDialog : public QDialog

public:
ScaleChargesDialog(QWidget *parent);
~ScaleChargesDialog();
bool scale_, scaleTo_;
~ScaleChargesDialog() = default;

private:
Ui::ScaleChargesDialog ui_;

public:
double scaleValue() const;

private Q_SLOTS:
void on_CancelButton_clicked(bool checked);
void on_ScaleButton_clicked(bool checked);
void on_ScaleToButton_clicked(bool checked);
ScaleChargesDialogModel *model;
};
82 changes: 0 additions & 82 deletions src/gui/scaleChargesDialog.ui

This file was deleted.

1 comment on commit 4e67ebe

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 4e67ebe Previous: 36447f1 Ratio
BM_Box_MinimumVector<OrthorhombicBox> 11.899363049760147 ns/iter 5.30059682235742 ns/iter 2.24

This comment was automatically generated by workflow using github-action-benchmark.

CC: @disorderedmaterials/dissolve-devs

Please sign in to comment.