| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "selectiontool.h" |
| 7 |
|
| 8 |
#include "selectiontoolwidget.h" |
| 9 |
|
| 10 |
#include <avogadro/qtopengl/glwidget.h> |
| 11 |
|
| 12 |
#include <avogadro/rendering/geometrynode.h> |
| 13 |
#include <avogadro/rendering/glrenderer.h> |
| 14 |
#include <avogadro/rendering/groupnode.h> |
| 15 |
#include <avogadro/rendering/meshgeometry.h> |
| 16 |
#include <avogadro/rendering/scene.h> |
| 17 |
|
| 18 |
#include <avogadro/core/array.h> |
| 19 |
#include <avogadro/core/atom.h> |
| 20 |
#include <avogadro/core/vector.h> |
| 21 |
#include <avogadro/qtgui/molecule.h> |
| 22 |
#include <avogadro/qtgui/rwlayermanager.h> |
| 23 |
#include <avogadro/qtgui/rwmolecule.h> |
| 24 |
|
| 25 |
#include <QAction> |
| 26 |
#include <QtCore/QDebug> |
| 27 |
#include <QtGui/QIcon> |
| 28 |
#include <QtGui/QMouseEvent> |
| 29 |
|
| 30 |
#include <queue> |
| 31 |
#include <set> |
| 32 |
|
| 33 |
using Avogadro::Core::Array; |
| 34 |
using Avogadro::Core::Atom; |
| 35 |
using Avogadro::QtGui::Molecule; |
| 36 |
using Avogadro::QtGui::RWMolecule; |
| 37 |
using Avogadro::Rendering::GeometryNode; |
| 38 |
using Avogadro::Rendering::GroupNode; |
| 39 |
using Avogadro::Rendering::Identifier; |
| 40 |
using Avogadro::Rendering::MeshGeometry; |
| 41 |
|
| 42 |
namespace Avogadro::QtPlugins { |
| 43 |
|
| 44 |
SelectionTool::SelectionTool(QObject* parent_) |
| 45 |
: QtGui::ToolPlugin(parent_), m_activateAction(new QAction(this)), |
| 46 |
m_molecule(nullptr), m_renderer(nullptr), m_toolWidget(nullptr), |
| 47 |
m_drawSelectionBox(false), m_doubleClick(false), m_initSelectionBox(false), |
| 48 |
m_layerManager("Selection Tool") |
| 49 |
{ |
| 50 |
QString shortcut = tr("Ctrl+4", "control-key 4"); |
| 51 |
m_activateAction->setText(tr("Selection")); |
| 52 |
m_activateAction->setToolTip( |
| 53 |
tr("Selection Tool\t(%1)\n\n" |
| 54 |
"Left Mouse:\tClick to pick individual atoms, residues, or fragments\n" |
| 55 |
"\tDrag to select a range of atoms\n" |
| 56 |
"Right Mouse:\tClick outside the molecule to clear selection\n" |
| 57 |
"Use Ctrl to toggle the selection and shift to add to the selection.\n" |
| 58 |
"Double-Click:\tSelect an entire fragment.") |
| 59 |
.arg(shortcut)); |
| 60 |
setIcon(); |
| 61 |
} |
| 62 |
|
| 63 |
SelectionTool::~SelectionTool() {} |
| 64 |
|
| 65 |
void SelectionTool::setIcon(bool darkTheme) |
| 66 |
{ |
| 67 |
if (darkTheme) |
| 68 |
m_activateAction->setIcon(QIcon(":/icons/selection_dark.svg")); |
| 69 |
else |
| 70 |
m_activateAction->setIcon(QIcon(":/icons/selection_light.svg")); |
| 71 |
} |
| 72 |
|
| 73 |
QWidget* SelectionTool::toolWidget() const |
| 74 |
{ |
| 75 |
if (m_toolWidget == nullptr) { |
| 76 |
m_toolWidget = new SelectionToolWidget(qobject_cast<QWidget*>(parent())); |
| 77 |
connect(m_toolWidget, SIGNAL(colorApplied(Vector3ub)), this, |
| 78 |
SLOT(applyColor(Vector3ub))); |
| 79 |
connect(m_toolWidget, SIGNAL(changeLayer(int)), this, |
| 80 |
SLOT(applyLayer(int))); |
| 81 |
} |
| 82 |
return m_toolWidget; |
| 83 |
} |
| 84 |
|
| 85 |
QUndoCommand* SelectionTool::mousePressEvent(QMouseEvent* e) |
| 86 |
{ |
| 87 |
if (e->button() != Qt::LeftButton || !m_renderer) { |
| 88 |
m_initSelectionBox = false; |
| 89 |
return nullptr; |
| 90 |
} |
| 91 |
|
| 92 |
m_drawSelectionBox = false; |
| 93 |
m_initSelectionBox = true; |
| 94 |
m_start = Vector2(e->pos().x(), e->pos().y()); |
| 95 |
m_end = m_start; |
| 96 |
e->accept(); |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
QUndoCommand* SelectionTool::mouseReleaseEvent(QMouseEvent* e) |
| 101 |
{ |
| 102 |
// If the click is released on an atom, add it to the list |
| 103 |
if (e->button() != Qt::LeftButton || !m_renderer || m_doubleClick) { |
| 104 |
m_doubleClick = false; |
| 105 |
return nullptr; |
| 106 |
} |
| 107 |
// Assess whether the selection box is big enough to use, or a mis-click. |
| 108 |
m_end = Vector2(e->pos().x(), e->pos().y()); |
| 109 |
Vector2f start(m_start.x() < m_end.x() ? m_start.x() : m_end.x(), |
| 110 |
m_start.y() < m_end.y() ? m_start.y() : m_end.y()); |
| 111 |
Vector2f end(m_start.x() > m_end.x() ? m_start.x() : m_end.x(), |
| 112 |
m_start.y() > m_end.y() ? m_start.y() : m_end.y()); |
| 113 |
bool bigEnough = |
| 114 |
fabs(start.x() - end.x()) > 2 && fabs(start.y() - end.y()) > 2; |
| 115 |
|
| 116 |
bool anySelect = false; |
| 117 |
Index selectedIndex = MaxIndex; |
| 118 |
if (m_drawSelectionBox && bigEnough) { |
| 119 |
shouldClean(e); |
| 120 |
m_initSelectionBox = false; |
| 121 |
auto hits = m_renderer->hits(start.x(), start.y(), end.x(), end.y()); |
| 122 |
for (const auto& hit : hits) { |
| 123 |
if (hit.type == Rendering::AtomType) { |
| 124 |
anySelect = selectAtom(e, hit.index) || anySelect; |
| 125 |
selectedIndex = hit.index; |
| 126 |
} |
| 127 |
} |
| 128 |
} else { |
| 129 |
// Single click |
| 130 |
m_start = Vector2(e->pos().x(), e->pos().y()); |
| 131 |
m_end = m_start; |
| 132 |
Identifier hit = m_renderer->hit(e->pos().x(), e->pos().y()); |
| 133 |
// Now add the atom on release. |
| 134 |
if (hit.type == Rendering::AtomType) { |
| 135 |
// store the result in case it's a toggle |
| 136 |
bool selected = selectAtom(e, hit.index); |
| 137 |
shouldClean(e); |
| 138 |
if (selected) { |
| 139 |
anySelect = addAtom(hit.index); |
| 140 |
selectedIndex = hit.index; |
| 141 |
} else { |
| 142 |
anySelect = removeAtom(hit.index); |
| 143 |
selectedIndex = hit.index; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
if (anySelect && m_toolWidget != nullptr) { |
| 148 |
m_toolWidget->setDropDown(m_layerManager.getLayerID(selectedIndex), |
| 149 |
m_layerManager.layerCount()); |
| 150 |
} |
| 151 |
m_drawSelectionBox = false; |
| 152 |
// Disable this code until rectangle selection is ready. |
| 153 |
emit drawablesChanged(); |
| 154 |
e->accept(); |
| 155 |
return nullptr; |
| 156 |
} |
| 157 |
|
| 158 |
QUndoCommand* SelectionTool::mouseDoubleClickEvent(QMouseEvent* e) |
| 159 |
{ |
| 160 |
if (e->button() == Qt::LeftButton) { |
| 161 |
m_doubleClick = true; |
| 162 |
m_initSelectionBox = false; |
| 163 |
Vector2 select = Vector2(e->pos().x(), e->pos().y()); |
| 164 |
Identifier hit = m_renderer->hit(select.x(), select.y()); |
| 165 |
// Reset the atom list |
| 166 |
if (!hit.isValid()) { |
| 167 |
clearAtoms(); |
| 168 |
} else { |
| 169 |
shouldClean(e); |
| 170 |
m_drawSelectionBox = false; |
| 171 |
// resync the select from simple click only on control |
| 172 |
if (e->modifiers() & Qt::ControlModifier) { |
| 173 |
toggleAtom(hit.index); |
| 174 |
} |
| 175 |
selectLinkedMolecule(e, hit.index); |
| 176 |
emit drawablesChanged(); |
| 177 |
e->accept(); |
| 178 |
} |
| 179 |
} |
| 180 |
return nullptr; |
| 181 |
} // namespace QtPlugins |
| 182 |
|
| 183 |
QUndoCommand* SelectionTool::mouseMoveEvent(QMouseEvent* e) |
| 184 |
{ |
| 185 |
// Disable this code until rectangle selection is ready. |
| 186 |
if (m_initSelectionBox) { |
| 187 |
m_drawSelectionBox = true; |
| 188 |
m_end = Vector2(e->pos().x(), e->pos().y()); |
| 189 |
emit drawablesChanged(); |
| 190 |
e->accept(); |
| 191 |
} |
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
QUndoCommand* SelectionTool::keyPressEvent(QKeyEvent*) |
| 196 |
{ |
| 197 |
return nullptr; |
| 198 |
} |
| 199 |
|
| 200 |
void SelectionTool::draw(Rendering::GroupNode& node) |
| 201 |
{ |
| 202 |
if (!m_drawSelectionBox || !m_initSelectionBox) { |
| 203 |
node.clear(); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
auto* geo = new GeometryNode; |
| 208 |
node.addChild(geo); |
| 209 |
auto* mesh = new MeshGeometry; |
| 210 |
|
| 211 |
mesh->setRenderPass(Rendering::Overlay2DPass); |
| 212 |
|
| 213 |
Array<Vector3f> verts(4); |
| 214 |
Vector3f start(m_start.x() < m_end.x() ? m_start.x() : m_end.x(), |
| 215 |
m_start.y() < m_end.y() ? m_start.y() : m_end.y(), 0.0f); |
| 216 |
Vector3f end(m_start.x() > m_end.x() ? m_start.x() : m_end.x(), |
| 217 |
m_start.y() > m_end.y() ? m_start.y() : m_end.y(), 0.0f); |
| 218 |
start.y() = m_renderer->overlayCamera().height() - start.y(); |
| 219 |
end.y() = m_renderer->overlayCamera().height() - end.y(); |
| 220 |
|
| 221 |
verts[0] = Vector3f(start.x(), end.y(), 0.0f); |
| 222 |
verts[1] = Vector3f(end.x(), end.y(), 0.0f); |
| 223 |
verts[2] = Vector3f(start.x(), start.y(), 0.0f); |
| 224 |
verts[3] = Vector3f(end.x(), start.y(), 0.0f); |
| 225 |
|
| 226 |
const Vector3f normal = verts[0].cross(verts[1]).normalized(); |
| 227 |
Array<Vector3f> norms(4, normal); |
| 228 |
|
| 229 |
Array<unsigned int> indices(6); |
| 230 |
indices[0] = 0; |
| 231 |
indices[1] = 1; |
| 232 |
indices[2] = 2; |
| 233 |
indices[3] = 2; |
| 234 |
indices[4] = 1; |
| 235 |
indices[5] = 3; |
| 236 |
|
| 237 |
mesh->setColor(Vector3ub(200, 200, 0)); |
| 238 |
mesh->setOpacity(180); |
| 239 |
mesh->addVertices(verts, norms); |
| 240 |
mesh->addTriangles(indices); |
| 241 |
|
| 242 |
geo->addDrawable(mesh); |
| 243 |
} |
| 244 |
|
| 245 |
void SelectionTool::applyColor(Vector3ub color) |
| 246 |
{ |
| 247 |
RWMolecule* rwmol = m_molecule->undoMolecule(); |
| 248 |
rwmol->beginMergeMode(tr("Paint Atoms")); |
| 249 |
for (Index i = 0; i < rwmol->atomCount(); ++i) { |
| 250 |
auto a = rwmol->atom(i); |
| 251 |
if (a.selected()) |
| 252 |
a.setColor(color); |
| 253 |
} |
| 254 |
rwmol->endMergeMode(); |
| 255 |
rwmol->emitChanged(Molecule::Atoms | Molecule::Modified); |
| 256 |
} |
| 257 |
|
| 258 |
void SelectionTool::applyLayer(int layer) |
| 259 |
{ |
| 260 |
if (layer <= 0 || m_molecule == nullptr) { |
| 261 |
return; |
| 262 |
} |
| 263 |
RWMolecule* rwmol = m_molecule->undoMolecule(); |
| 264 |
rwmol->beginMergeMode(tr("Change Layer")); |
| 265 |
Molecule::MoleculeChanges changes = Molecule::Atoms | Molecule::Modified; |
| 266 |
|
| 267 |
// qDebug() << "SelectionTool::applyLayer" << layer << " layerCount " << |
| 268 |
// m_layerManager.layerCount(); |
| 269 |
if (layer >= static_cast<int>(m_layerManager.layerCount())) { |
| 270 |
// add a new layer |
| 271 |
auto& layerInfo = Core::LayerManager::getMoleculeInfo(m_molecule)->layer; |
| 272 |
QtGui::RWLayerManager rwLayerManager; |
| 273 |
rwLayerManager.addLayer(rwmol); |
| 274 |
layer = layerInfo.maxLayer(); |
| 275 |
|
| 276 |
// update the menu too |
| 277 |
if (m_toolWidget != nullptr) |
| 278 |
m_toolWidget->setDropDown(layer, m_layerManager.layerCount()); |
| 279 |
changes |= Molecule::Layers | Molecule::Added; |
| 280 |
} |
| 281 |
|
| 282 |
for (Index i = 0; i < rwmol->atomCount(); ++i) { |
| 283 |
auto a = rwmol->atom(i); |
| 284 |
if (a.selected()) { |
| 285 |
a.setLayer(layer); |
| 286 |
} |
| 287 |
} |
| 288 |
rwmol->endMergeMode(); |
| 289 |
rwmol->emitChanged(changes); |
| 290 |
} |
| 291 |
|
| 292 |
void SelectionTool::selectLinkedMolecule(QMouseEvent* e, Index atom) |
| 293 |
{ |
| 294 |
auto connectedAtoms = m_molecule->graph().connectedComponent(atom); |
| 295 |
for (auto a : connectedAtoms) { |
| 296 |
selectAtom(e, a); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
void SelectionTool::clearAtoms() |
| 301 |
{ |
| 302 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) |
| 303 |
m_molecule->undoMolecule()->setAtomSelected(i, false); |
| 304 |
} |
| 305 |
|
| 306 |
bool SelectionTool::addAtom(const Index& atom) |
| 307 |
{ |
| 308 |
m_molecule->undoMolecule()->setAtomSelected(atom, true); |
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
bool SelectionTool::removeAtom(const Index& atom) |
| 313 |
{ |
| 314 |
m_molecule->undoMolecule()->setAtomSelected(atom, false); |
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
bool SelectionTool::toggleAtom(const Index& atom) |
| 319 |
{ |
| 320 |
Atom a = m_molecule->atom(atom); |
| 321 |
m_molecule->undoMolecule()->setAtomSelected(atom, !a.selected()); |
| 322 |
return a.selected(); |
| 323 |
} |
| 324 |
|
| 325 |
bool SelectionTool::shouldClean(QMouseEvent* e) |
| 326 |
{ |
| 327 |
// accumulate the selection if shift or ctrl are presset |
| 328 |
if (!(e->modifiers() & Qt::ControlModifier) && |
| 329 |
!(e->modifiers() & Qt::ShiftModifier)) { |
| 330 |
clearAtoms(); |
| 331 |
return true; |
| 332 |
} |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
bool SelectionTool::selectAtom(QMouseEvent* e, const Index& index) |
| 337 |
{ |
| 338 |
if (m_layerManager.atomLocked(index)) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
// control toggles the selection |
| 342 |
if (e->modifiers() & Qt::ControlModifier) { |
| 343 |
return toggleAtom(index); |
| 344 |
} |
| 345 |
// shift and default selection adds |
| 346 |
else if (e->modifiers() & Qt::ShiftModifier || m_drawSelectionBox) { |
| 347 |
return addAtom(index); |
| 348 |
} |
| 349 |
// default toggle |
| 350 |
else { |
| 351 |
return toggleAtom(index); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
void SelectionTool::setMolecule(QtGui::Molecule* mol) |
| 356 |
{ |
| 357 |
if (m_molecule != mol) { |
| 358 |
m_molecule = mol; |
| 359 |
} |
| 360 |
|
| 361 |
size_t currentLayer = 0; |
| 362 |
size_t maxLayers = 1; |
| 363 |
if (m_molecule && !m_molecule->isSelectionEmpty()) { |
| 364 |
// find a selected atom |
| 365 |
Index selectedIndex = 0; |
| 366 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) { |
| 367 |
auto a = m_molecule->atom(i); |
| 368 |
if (a.selected()) |
| 369 |
selectedIndex = i; |
| 370 |
break; |
| 371 |
} |
| 372 |
currentLayer = m_layerManager.getLayerID(selectedIndex); |
| 373 |
maxLayers = m_layerManager.layerCount(); |
| 374 |
} |
| 375 |
|
| 376 |
if (m_toolWidget != nullptr) |
| 377 |
m_toolWidget->setDropDown(currentLayer, maxLayers); |
| 378 |
} |
| 379 |
|
| 380 |
} // namespace Avogadro::QtPlugins |