Skip to content

Commit

Permalink
Merge pull request cryos#10 from ghutchis/ENH_draw_show_distance
Browse files Browse the repository at this point in the history
Add distance label while drawing occurs for immediate feedback.
  • Loading branch information
cryos committed May 25, 2015
2 parents e34ab63 + 313f5ef commit eb810c7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
54 changes: 53 additions & 1 deletion avogadro/qtplugins/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

#include <avogadro/rendering/camera.h>
#include <avogadro/rendering/glrenderer.h>
#include <avogadro/rendering/geometrynode.h>

#include <avogadro/rendering/groupnode.h>
#include <avogadro/rendering/textlabel2d.h>
#include <avogadro/rendering/textlabel3d.h>
#include <avogadro/rendering/textproperties.h>


#include <QtWidgets/QAction>
#include <QtWidgets/QComboBox>
Expand All @@ -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)),
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -449,6 +495,12 @@ void Editor::atomLeftDrag(QMouseEvent *e)
Vector3f newPos = m_renderer->camera().unProject(windowPos, oldPos);
newAtom.setPosition3d(newPos.cast<double>());
changes |= Molecule::Atoms | Molecule::Modified;

RWAtom clickedAtom = m_molecule->atom(m_clickedObject.index);
if (clickedAtom.isValid()) {
Vector3f bondVector = clickedAtom.position3d().cast<float>() - newPos;
m_bondDistance = bondVector.norm();
}
}

m_molecule->emitChanged(changes);
Expand Down
5 changes: 5 additions & 0 deletions avogadro/qtplugins/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <avogadro/core/avogadrocore.h>
#include <avogadro/rendering/primitive.h>


#include <QtCore/QPoint>

namespace Avogadro {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -110,6 +113,8 @@ private slots:
unsigned char m_clickedAtomicNumber;
bool m_bondAdded;
QString m_keyPressBuffer;

Real m_bondDistance;
};

} // namespace QtPlugins
Expand Down

0 comments on commit eb810c7

Please sign in to comment.