From 313f5ef0c7b30b64c264c7a332297ebb2d3d77c2 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Fri, 8 May 2015 19:19:14 -0400 Subject: [PATCH] Add distance label while drawing occurs for immediate feedback. Gives the bond distance between the atoms. Change-Id: Ia26ea632a4a32fd63d6c495f7b74206ca60949d5 --- avogadro/qtplugins/editor/editor.cpp | 54 +++++++++++++++++++++++++++- avogadro/qtplugins/editor/editor.h | 5 +++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/avogadro/qtplugins/editor/editor.cpp b/avogadro/qtplugins/editor/editor.cpp index f451fa1d9..a1a58c0ee 100644 --- a/avogadro/qtplugins/editor/editor.cpp +++ b/avogadro/qtplugins/editor/editor.cpp @@ -31,6 +31,13 @@ #include #include +#include + +#include +#include +#include +#include + #include #include @@ -57,9 +64,15 @@ using QtGui::RWAtom; using QtGui::RWBond; using QtGui::Molecule; using QtGui::RWMolecule; -using Rendering::Identifier; using QtOpenGL::GLWidget; +using Avogadro::Rendering::GeometryNode; +using Avogadro::Rendering::GroupNode; +using Avogadro::Rendering::Identifier; +using Avogadro::Rendering::TextLabel2D; +using Avogadro::Rendering::TextLabel3D; +using Avogadro::Rendering::TextProperties; + Editor::Editor(QObject *parent_) : QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), @@ -208,6 +221,36 @@ void Editor::adjustHydrogens() } } +void Editor::draw(Rendering::GroupNode &node) +{ + if (fabs(m_bondDistance) < 0.3) + return; + + GeometryNode *geo = new GeometryNode; + node.addChild(geo); + + // Determine the field width. Negate it to indicate left-alignment. + QString distanceLabel = tr("Distance:"); + int labelWidth = -1*distanceLabel.size(); + + QString overlayText = QString("%1 %L2") + .arg(tr("Distance:"), labelWidth) + .arg(m_bondDistance, 10, 'f', 3); + + TextProperties overlayTProp; + overlayTProp.setFontFamily(TextProperties::Mono); + overlayTProp.setColorRgb(64, 255, 220); + overlayTProp.setAlign(TextProperties::HLeft, TextProperties::VBottom); + + TextLabel2D *label = new TextLabel2D; + label->setText(overlayText.toStdString()); + label->setTextProperties(overlayTProp); + label->setRenderPass(Rendering::Overlay2DPass); + label->setAnchor(Vector2i(10, 10)); + + geo->addDrawable(label); +} + void Editor::updatePressedButtons(QMouseEvent *e, bool release) { /// @todo Use modifier keys on mac @@ -226,6 +269,9 @@ void Editor::reset() m_pressedButtons = Qt::NoButton; m_clickedAtomicNumber = INVALID_ATOMIC_NUMBER; m_bondAdded = false; + + m_bondDistance = 0.0f; + emit drawablesChanged(); } void Editor::emptyLeftClick(QMouseEvent *e) @@ -449,6 +495,12 @@ void Editor::atomLeftDrag(QMouseEvent *e) Vector3f newPos = m_renderer->camera().unProject(windowPos, oldPos); newAtom.setPosition3d(newPos.cast()); changes |= Molecule::Atoms | Molecule::Modified; + + RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index); + if (clickedAtom.isValid()) { + Vector3f bondVector = clickedAtom.position3d().cast() - newPos; + m_bondDistance = bondVector.norm(); + } } m_molecule->emitChanged(changes); diff --git a/avogadro/qtplugins/editor/editor.h b/avogadro/qtplugins/editor/editor.h index b81463ac3..f88cc86a2 100644 --- a/avogadro/qtplugins/editor/editor.h +++ b/avogadro/qtplugins/editor/editor.h @@ -23,6 +23,7 @@ #include #include + #include namespace Avogadro { @@ -72,6 +73,8 @@ class Editor : public QtGui::ToolPlugin QUndoCommand * mouseMoveEvent(QMouseEvent *e) AVO_OVERRIDE; QUndoCommand * keyPressEvent(QKeyEvent *e) AVO_OVERRIDE; + void draw(Rendering::GroupNode &node) AVO_OVERRIDE; + private slots: void clearKeyPressBuffer() { m_keyPressBuffer.clear(); } void adjustHydrogens(); @@ -110,6 +113,8 @@ private slots: unsigned char m_clickedAtomicNumber; bool m_bondAdded; QString m_keyPressBuffer; + + Real m_bondDistance; }; } // namespace QtPlugins