Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

display qml scale charges dialog #1763

Merged
merged 24 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e509bbc
display qml scale charges dialog
RobBuchananCompPhys Jan 16, 2024
1e79a61
removed ui file
RobBuchananCompPhys Jan 16, 2024
169ae2d
applied clang formatting
RobBuchananCompPhys Jan 16, 2024
11cee45
QC
RobBuchananCompPhys Jan 16, 2024
b93dca8
qml imports custom widgets, uses scale charge option enum as defined …
RobBuchananCompPhys Jan 17, 2024
27bf1cc
applied clang formatting
RobBuchananCompPhys Jan 17, 2024
9dab116
applied qml formatting
RobBuchananCompPhys Jan 17, 2024
e781977
use auto
RobBuchananCompPhys Jan 17, 2024
36f7407
reset spin box when cancelled, modularised code for processing selection
RobBuchananCompPhys Jan 18, 2024
ec688b2
added padding, button icons and alignment
RobBuchananCompPhys Jan 22, 2024
9d72b81
qml format
RobBuchananCompPhys Jan 22, 2024
39ccff6
added unit test for model
RobBuchananCompPhys Jan 22, 2024
4a6141f
clang formatting
RobBuchananCompPhys Jan 22, 2024
1756a33
renamed test, renamed property scale_type_ to scaleType_
RobBuchananCompPhys Jan 22, 2024
3c7f4ce
fixed scale dialog tests
RobBuchananCompPhys Jan 23, 2024
8b45cd1
WordWrap
RobBuchananCompPhys Jan 23, 2024
a37dab6
tweaks to dialog
RobBuchananCompPhys Jan 23, 2024
68981f5
format qml
RobBuchananCompPhys Jan 23, 2024
0fba013
increased comment coverage
RobBuchananCompPhys Jan 24, 2024
98004cc
use auto
RobBuchananCompPhys Jan 24, 2024
7591342
moved scale error message from menu actions to model, scale method no…
RobBuchananCompPhys Jan 25, 2024
678c3d7
updated to fix the gui header issue
RobBuchananCompPhys Jan 25, 2024
68e839a
make dialog resizable
RobBuchananCompPhys Jan 25, 2024
65a49b1
applied formatting
RobBuchananCompPhys Jan 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
bool ScaleChargesDialogModel::scaleCharges(Species *species, bool showDialogOnError = true)
bool ScaleChargesDialogModel::scaleCharges(Species *species, bool showDialogOnError)

{
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
RobBuchananCompPhys marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
bool scaleCharges(Species *, bool);
bool scaleCharges(Species *species, bool showDialogOnError = true);


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
Original file line number Diff line number Diff line change
@@ -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"
RobBuchananCompPhys marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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.