From e02b9695eb941714598fd6fffcb3f9b301695923 Mon Sep 17 00:00:00 2001 From: kantundpeterpan Date: Mon, 6 Jul 2020 01:25:32 +0200 Subject: [PATCH] highlighting of selected Constraint Signed-off-by: kantundpeterpan --- .../constraints/.#constraintsdialog.cpp | 1 + avogadro/qtplugins/constraints/constraint.cpp | 29 ++-- avogadro/qtplugins/constraints/constraint.h | 46 ++---- .../constraints/constraintsdialog.cpp | 55 +++++++- .../qtplugins/constraints/constraintsdialog.h | 3 + .../constraints/constraintsdialog.ui | 133 +++++++++--------- .../constraints/constraintsextension.cpp | 12 +- .../constraints/constraintsmodel.cpp | 27 ---- .../qtplugins/constraints/constraintsmodel.h | 8 +- 9 files changed, 154 insertions(+), 160 deletions(-) create mode 120000 avogadro/qtplugins/constraints/.#constraintsdialog.cpp diff --git a/avogadro/qtplugins/constraints/.#constraintsdialog.cpp b/avogadro/qtplugins/constraints/.#constraintsdialog.cpp new file mode 120000 index 0000000000..a0ca7487e7 --- /dev/null +++ b/avogadro/qtplugins/constraints/.#constraintsdialog.cpp @@ -0,0 +1 @@ +kantundpeterpan@haina-Ubs19.5479:1593976063 \ No newline at end of file diff --git a/avogadro/qtplugins/constraints/constraint.cpp b/avogadro/qtplugins/constraints/constraint.cpp index 1906893374..379bc6e24c 100644 --- a/avogadro/qtplugins/constraints/constraint.cpp +++ b/avogadro/qtplugins/constraints/constraint.cpp @@ -14,6 +14,7 @@ namespace Avogadro { c_model = model; ConstraintType = type; + //adjusting for 0 indexing a = a-1; b = b-1; c = c-1; @@ -29,29 +30,29 @@ namespace Avogadro { break; case 5: - // AtomUniqueIdA = + //AtomA Atoms << c_model->c_molecule->atomUniqueId(a); - //AtomUniqueIdB = + //AtomB Atoms << c_model->c_molecule->atomUniqueId(b); break; case 6: - //AtomUniqueIdA = + //AtomA Atoms << c_model->c_molecule->atomUniqueId(a); - //AtomUniqueIdB = + //AtomB Atoms << c_model->c_molecule->atomUniqueId(b); - //AtomUniqueIdC = + //AtomC Atoms << c_model->c_molecule->atomUniqueId(c); break; case 7: - //AtomUniqueIdA = + //AtomA Atoms << c_model->c_molecule->atomUniqueId(a); - //AtomUniqueIdB = + //AtomB Atoms << c_model->c_molecule->atomUniqueId(b); - //AtomUniqueIdC = + //AtomC Atoms << c_model->c_molecule->atomUniqueId(c); - //AtomUniqueIdD = + //AtomD Atoms << c_model->c_molecule->atomUniqueId(d); } @@ -83,7 +84,7 @@ namespace Avogadro { const Index Constraint::GetConstraintAtomA() const { - if (Atoms.size() >= 1) + if (Atoms.size() >= 1) //returned for all constraint types { return c_model->c_molecule->atomByUniqueId(Atoms[0]).index()+1; } @@ -95,7 +96,7 @@ namespace Avogadro { const Index Constraint::GetConstraintAtomB() const { - if (Atoms.size() >= 2) + if (Atoms.size() >= 2) //distance, angle and torsion constraints { return c_model->c_molecule->atomByUniqueId(Atoms[1]).index()+1; } @@ -107,7 +108,7 @@ namespace Avogadro { const Index Constraint::GetConstraintAtomC() const { - if (Atoms.size() >= 3) + if (Atoms.size() >= 3) //angle and torsion constraints { return c_model->c_molecule->atomByUniqueId(Atoms[2]).index()+1; } @@ -119,7 +120,7 @@ namespace Avogadro { const Index Constraint::GetConstraintAtomD() const { - if (Atoms.size() >= 4) + if (Atoms.size() >= 4) //torsion constraints only { return c_model->c_molecule->atomByUniqueId(Atoms[3]).index()+1; } @@ -164,4 +165,4 @@ namespace Avogadro { return ConstraintJ; } } //namespace QtPlugins -} //namespace Avogadroe +} //namespace Avogadro diff --git a/avogadro/qtplugins/constraints/constraint.h b/avogadro/qtplugins/constraints/constraint.h index 3f822134a1..966058b0ad 100644 --- a/avogadro/qtplugins/constraints/constraint.h +++ b/avogadro/qtplugins/constraints/constraint.h @@ -1,7 +1,7 @@ #include #include #include -//#include + #include #include "constraintsmodel.h" @@ -17,10 +17,9 @@ namespace Avogadro { { public: /* - Implementation of very simple class for the represenation of constraints. + Implementation of simple class for the represenation of constraints. - At the moment it stores rather naively just some numbers. The ConstraintType - has the following mapping (reflected in the ConstraintDialog combobox): + The ConstraintType has the following mapping (reflected in the ConstraintDialog combobox): 0: Ignore Atom 1: Fix Atom @@ -29,19 +28,13 @@ namespace Avogadro { 4: Fix Atom Z 5: Distance 6: Angle - 7: Torsion angle - - A more sophisticated way would probably be to store pointers to the respective - atoms of the molecule and extend the Atom class by something like : - - void Atom::setConstrained() - bool Atom::isConstrained() + 7: Torsion - Connecting the appropriate signals would enable easy updating of the constraints - upon changing the Molecule. + This implementation makes use of the UniqueID that is assigned to each Atom upon creation, + which can be used to unambigously retrieve the current index of the Atom in molecule. - This constraint representation has to be translated when passing it to whatever - Optimizing/MD/QM code. + This constraint representation has to be translated into package specific instructions when + passing it to whatever Optimizing/MD/QM code. */ explicit Constraint(int ConstraintType, @@ -54,17 +47,10 @@ namespace Avogadro { ~Constraint(); void SetConstraintType(int ConstraintType); - // void SetAtomId(QList atom_ids); void SetValue(double Value); int GetConstraintType() const; double GetConstraintValue() const; - /* - int GetConstraintAtomA() const; - int GetConstraintAtomB() const; - int GetConstraintAtomC() const; - int GetConstraintAtomD() const; - */ const Index GetConstraintAtomA() const; const Index GetConstraintAtomB() const; @@ -74,23 +60,9 @@ namespace Avogadro { QJsonObject toJson(); int ConstraintType; - /* - int AtomIdA; - int AtomIdB; - int AtomIdC; - int AtomIdD; - */ - - /* - Core::Atom* AtomA = nullptr; - Core::Atom* AtomB = nullptr; - Core::Atom* AtomC = nullptr; - Core::Atom* AtomD = nullptr; - */ - - QList Atoms; double ConstraintValue; + QList Atoms; ConstraintsModel* c_model = nullptr; }; } diff --git a/avogadro/qtplugins/constraints/constraintsdialog.cpp b/avogadro/qtplugins/constraints/constraintsdialog.cpp index 2d2dd5e71c..94440e856d 100644 --- a/avogadro/qtplugins/constraints/constraintsdialog.cpp +++ b/avogadro/qtplugins/constraints/constraintsdialog.cpp @@ -1,10 +1,13 @@ #include "constraintsdialog.h" #include "ui_constraintsdialog.h" + #include #include #include #include +using Avogadro::QtGui::Molecule; + //#include //#include //#include @@ -12,9 +15,6 @@ //#include namespace Avogadro { - // namespace QtGui{ - // class Molecule; - // } namespace QtPlugins { ConstraintsDialog::ConstraintsDialog(ConstraintsExtension* plugin, QWidget* parent_, @@ -28,7 +28,8 @@ namespace Avogadro { connect( ui->ConstraintsAdd, SIGNAL( clicked() ), this, SLOT( addConstraint() )); connect( ui->ConstraintsDelete, SIGNAL( clicked() ), this, SLOT( deleteConstraint() )); connect( ui->ConstraintsDeleteAll, SIGNAL( clicked() ), this, SLOT( deleteAllConstraints() )); - + // connect( ui->HighlightButton, SIGNAL( clicked() ), this, SLOT( highlightSelected())); + connect( ui->checkHighlight, SIGNAL( stateChanged(int)), this, SLOT( connectHighlight(int))); } ConstraintsDialog::~ConstraintsDialog() @@ -36,11 +37,55 @@ namespace Avogadro { delete ui; } + void ConstraintsDialog::connectHighlight(int state) + { + if (state) + { + connect(ui->ConstraintsTableView->selectionModel(), + SIGNAL( selectionChanged(QItemSelection, QItemSelection)), + this, SLOT (highlightSelected())); + } + else + { + disconnect(ui->ConstraintsTableView->selectionModel(), + SIGNAL( selectionChanged(QItemSelection, QItemSelection)), + this, SLOT (highlightSelected())); + } + } + + void ConstraintsDialog::highlightSelected() + {// check if highlighting requestd + if (ui->checkHighlight->checkState()) + {//clear all previous selections + for (int i = 0; i < m_plugin->m_molecule->atomCount(); ++i) + { + m_plugin->m_molecule->atom(i).setSelected(false); + } + // get currently selected constraint + QModelIndex idx = ui->ConstraintsTableView->selectionModel()->currentIndex(); + // extract constraint from ConstraintModel + QtPlugins::Constraint* c = &m_plugin->m_molecule->constraints->ConstraintsList[idx.row()]; + // iterate over involved uniqueAtomIDs + for (int i = 0; i < c->Atoms.size(); i++) + { + // get atom by uniqueID and set selected + m_plugin->m_molecule->atomByUniqueId(c->Atoms[i]).setSelected(true); + qDebug() << "Set selected"; + } + m_plugin->m_molecule->emitChanged(Molecule::Atoms); + } + } + void ConstraintsDialog::setModel() { ui->ConstraintsTableView->setModel(m_plugin->m_molecule->constraints); connect( m_plugin->m_molecule, SIGNAL( changed(unsigned int)), m_plugin->m_molecule->constraints, SLOT (emitDataChanged())); + /* + connect( ui->ConstraintsTableView, SIGNAL( selectionChanged(const QItemSelection &selected, + const QItemSelection &deselected)), + this, SLOT(highlightSelected())); + */ } void ConstraintsDialog::acceptConstraints() @@ -58,6 +103,7 @@ namespace Avogadro { void ConstraintsDialog::addConstraint() { + //Parsing user inptu int type = ui->comboType->currentIndex(); double value = ui->editValue->value(); int AtomIdA = ui->editA->value(); @@ -65,6 +111,7 @@ namespace Avogadro { int AtomIdC = ui->editC->value(); int AtomIdD = ui->editD->value(); + //adding the constraint to the molecule's CosntraintsModel m_plugin->m_molecule->constraints->addConstraint(type, AtomIdA, AtomIdB, diff --git a/avogadro/qtplugins/constraints/constraintsdialog.h b/avogadro/qtplugins/constraints/constraintsdialog.h index 43397a7abd..560663bfae 100644 --- a/avogadro/qtplugins/constraints/constraintsdialog.h +++ b/avogadro/qtplugins/constraints/constraintsdialog.h @@ -30,10 +30,13 @@ namespace Avogadro { void addConstraint(); void deleteConstraint(); void deleteAllConstraints(); + void highlightSelected(); + void connectHighlight(int state); private: Ui::ConstraintsDialog* ui; ConstraintsExtension* m_plugin; + }; } } diff --git a/avogadro/qtplugins/constraints/constraintsdialog.ui b/avogadro/qtplugins/constraints/constraintsdialog.ui index 42a3c2186d..ee526432cb 100644 --- a/avogadro/qtplugins/constraints/constraintsdialog.ui +++ b/avogadro/qtplugins/constraints/constraintsdialog.ui @@ -1,122 +1,123 @@ - + + Avogadro::QtPlugins::ConstraintsDialog - - + + 0 0 - 750 + 798 357 - + Constraints - + - - + + QAbstractItemView::SelectRows - - + + Add Constraints - + - + - - + + 40 16777215 - + Type - + - + Ignore Atom - + Fix Atom - + Fix Atom X - + Fix Atom Y - + Fix Atom Z - + Distance - + Angle - + Torsion angle - - + + Constraint Value - + - - + + Atom Indices - + - + - + - + - - + + Add @@ -127,39 +128,39 @@ - - + + Options - + - + - - + + false - + Save - - + + false - + Load - + Qt::Horizontal - + 40 20 @@ -168,47 +169,41 @@ - - - + + + Highlight selected Constraint + + + + + + + 0 0 - + 100 0 - + Delete Selected - - + + Delete All - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - + + OK diff --git a/avogadro/qtplugins/constraints/constraintsextension.cpp b/avogadro/qtplugins/constraints/constraintsextension.cpp index 67a326dba6..6f4af1aa3d 100644 --- a/avogadro/qtplugins/constraints/constraintsextension.cpp +++ b/avogadro/qtplugins/constraints/constraintsextension.cpp @@ -46,11 +46,15 @@ namespace Avogadro { void ConstraintsExtension::setMolecule(QtGui::Molecule* mol) { if (mol != m_molecule) - m_molecule = mol; + { + m_molecule = mol; + dialog->setModel(); + dialog->connectHighlight(0); + } if (!m_molecule->constraints) - m_molecule->constraints = new ConstraintsModel(m_molecule); - dialog->setModel(); - + { + m_molecule->constraints = new ConstraintsModel(m_molecule); + } } bool ConstraintsExtension::readMolecule(QtGui::Molecule& mol){ diff --git a/avogadro/qtplugins/constraints/constraintsmodel.cpp b/avogadro/qtplugins/constraints/constraintsmodel.cpp index 9697a78a22..f1a7fe78e3 100644 --- a/avogadro/qtplugins/constraints/constraintsmodel.cpp +++ b/avogadro/qtplugins/constraints/constraintsmodel.cpp @@ -1,24 +1,3 @@ -/********************************************************************** - constraintsmodel.cpp - Model to hold constraints - - Copyright (C) 2007 by Tim Vandermeersch - - This file is part of the Avogadro molecular editor project. - For more information, see - - Some code is based on Open Babel - For more information, see - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation version 2 of the License. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - ***********************************************************************/ - #include "constraintsmodel.h" #include #include @@ -200,13 +179,7 @@ namespace Avogadro ConstraintsMJ.insert(QString::number(i), ConstraintsList[i].toJson()); } } - - /* - QJsonDocument json_doc(ConstraintsMJ); - QString json_string = json_doc.toJson(); - qDebug() << json_string << endl; - */ return ConstraintsMJ; } } diff --git a/avogadro/qtplugins/constraints/constraintsmodel.h b/avogadro/qtplugins/constraints/constraintsmodel.h index 97bfeea852..b2d12cba31 100644 --- a/avogadro/qtplugins/constraints/constraintsmodel.h +++ b/avogadro/qtplugins/constraints/constraintsmodel.h @@ -11,9 +11,6 @@ #include #include "constraint.h" -#ifndef BUFF_SIZE -#define BUFF_SIZE 256 -#endif namespace Avogadro { namespace QtPlugins { @@ -45,13 +42,14 @@ namespace Avogadro { QList ConstraintsList; - // reference to associated molecule + + // pointer to the associated molecule QtGui::Molecule* c_molecule = nullptr; public slots: void emitDataChanged(); -}; //ConstraintsModel + }; //ConstraintsModel } // QtPlugins } // end namespace Avogadro