Skip to content

Commit

Permalink
Fix #1755 - rotate around origin, molecule center, selection center
Browse files Browse the repository at this point in the history
Also allows rotating everything else if you want to fix the selection

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Nov 11, 2024
1 parent e956cd4 commit 793448d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
58 changes: 49 additions & 9 deletions avogadro/qtplugins/manipulator/manipulatewidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>337</width>
<height>247</height>
<width>371</width>
<height>292</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -103,6 +103,39 @@
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Rotate around:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="rotateComboBox">
<item>
<property name="text">
<string>Origin</string>
</property>
</item>
<item>
<property name="text">
<string>Center of Molecule</string>
</property>
</item>
<item>
<property name="text">
<string>Center of Selection</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="0">
Expand Down Expand Up @@ -174,23 +207,30 @@
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>Rotate around:</string>
<string>Move:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="rotateComboBox">
<item>
<widget class="QComboBox" name="moveComboBox">
<item>
<property name="text">
<string>Origin</string>
<string>Selected Atoms</string>
</property>
</item>
<item>
<property name="text">
<string>Geometry</string>
<string>Everything Else</string>
</property>
</item>
</widget>
Expand Down
30 changes: 19 additions & 11 deletions avogadro/qtplugins/manipulator/manipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Manipulator::Manipulator(QObject* parent_)
m_activateAction->setToolTip(
tr("Manipulation Tool\n\n"
"Left Mouse: \tClick and drag to move atoms\n"
"Right Mouse: \tClick and drag to rotate selected atoms.\n"));
"Right Mouse: \tClick and drag to rotate atoms.\n"));
setIcon();
connect(m_toolWidget->buttonBox, SIGNAL(clicked(QAbstractButton*)), this,
SLOT(buttonClicked(QAbstractButton*)));
Expand Down Expand Up @@ -99,20 +99,27 @@ void Manipulator::buttonClicked(QAbstractButton* button)
return;
}

bool moveSelected = (m_toolWidget->moveComboBox->currentIndex() == 0);

// apply values
Vector3 delta(m_toolWidget->xTranslateSpinBox->value(),
m_toolWidget->yTranslateSpinBox->value(),
m_toolWidget->zTranslateSpinBox->value());

translate(delta);
translate(delta, moveSelected);

Vector3 rotation(m_toolWidget->xRotateSpinBox->value(),
m_toolWidget->yRotateSpinBox->value(),
m_toolWidget->zRotateSpinBox->value());
Vector3 center(0.0, 0.0, 0.0);

// Check if we're rotating around the origin or the centroid
// Check if we're rotating around the origin, the molecule centroid
// or the center of selected atoms
// == 0 is the default = origin
if (m_toolWidget->rotateComboBox->currentIndex() == 1) {
// molecule centroid
center = m_molecule->molecule().centerOfGeometry();
} else if (m_toolWidget->rotateComboBox->currentIndex() == 2) {
// center of selected atoms
unsigned long selectedAtomCount = 0;
for (Index i = 0; i < m_molecule->atomCount(); ++i) {
Expand All @@ -124,16 +131,13 @@ void Manipulator::buttonClicked(QAbstractButton* button)
}
if (selectedAtomCount > 0)
center /= selectedAtomCount;

} else {
center = m_molecule->molecule().centerOfGeometry();
}

// Settings are in degrees
#ifndef DEG_TO_RAD
#define DEG_TO_RAD 0.0174532925
#endif
rotate(rotation * DEG_TO_RAD, center);
rotate(rotation * DEG_TO_RAD, center, moveSelected);

m_molecule->emitChanged(Molecule::Atoms | Molecule::Modified);
}
Expand Down Expand Up @@ -280,18 +284,20 @@ QUndoCommand* Manipulator::mouseMoveEvent(QMouseEvent* e)
return nullptr;
}

void Manipulator::translate(Vector3 delta)
void Manipulator::translate(Vector3 delta, bool moveSelected)
{
for (Index i = 0; i < m_molecule->atomCount(); ++i) {
if (!m_molecule->atomSelected(i))
if (moveSelected && !m_molecule->atomSelected(i))
continue;
else if (!moveSelected && m_molecule->atomSelected(i))
continue;

Vector3 currentPos = m_molecule->atomPosition3d(i);
m_molecule->setAtomPosition3d(i, currentPos + delta.cast<double>());
}
}

void Manipulator::rotate(Vector3 delta, Vector3 centroid)
void Manipulator::rotate(Vector3 delta, Vector3 centroid, bool moveSelected)
{
// Rotate the selected atoms about the center
// rotate only selected primitives
Expand All @@ -311,7 +317,9 @@ void Manipulator::rotate(Vector3 delta, Vector3 centroid)
fragmentRotation.translate(-centroid);

for (Index i = 0; i < m_molecule->atomCount(); ++i) {
if (!m_molecule->atomSelected(i))
if (moveSelected && !m_molecule->atomSelected(i))
continue;
else if (!moveSelected && m_molecule->atomSelected(i))
continue;

Vector3 currentPos = m_molecule->atomPosition3d(i);
Expand Down
4 changes: 2 additions & 2 deletions avogadro/qtplugins/manipulator/manipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public slots:
void updatePressedButtons(QMouseEvent*, bool release);

void resetObject() { m_object = Rendering::Identifier(); }
void translate(Vector3 delta);
void rotate(Vector3 delta, Vector3 centroid);
void translate(Vector3 delta, bool moveSelected = true);
void rotate(Vector3 delta, Vector3 centroid, bool moveSelected = true);
void tilt(Vector3 delta, Vector3 centroid);

QAction* m_activateAction;
Expand Down

0 comments on commit 793448d

Please sign in to comment.