File: pickingMode.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (173 lines) | stat: -rw-r--r-- 4,128 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <BALL/VIEW/KERNEL/MODES/pickingMode.h>

#include <QtGui/QMouseEvent>
#include <QtGui/QKeyEvent>

#include <BALL/VIEW/WIDGETS/scene.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/VIEW/KERNEL/message.h>

namespace BALL
{
	namespace VIEW
	{

		PickingMode::PickingMode(Scene* scene)
			: InteractionMode(scene),
			  pick_select_(false),
			  ignore_pick_(false)
		{
			String description = "Shortcut|Display|Picking_Mode";
			main_action_ = scene_->insertMenuEntry(MainControl::DISPLAY, tr("&Picking Mode"), this,
			                                       0, description, QKeySequence("Ctrl+P"),
                                             tr("Switch to picking mode, e.g. to identify single atoms or groups"),
																						 UIOperationMode::MODE_ADVANCED);

			if (main_action_)
			{
				scene_->setIcon(main_action_, "actions/select-rectangular", false);
				main_action_->setCheckable(true);
				connect(main_action_, SIGNAL(triggered()), SLOT(modeChangeSlot_()));
			}
		}

		void PickingMode::mouseDoubleClickEvent(QMouseEvent* evt)
		{
			QPoint p = scene_->mapFromGlobal(evt->pos());
			pickParent_(p);
		}

		void PickingMode::mouseMoveEventImpl_(QMouseEvent* evt)
		{
			if (evt->buttons() & (Qt::LeftButton | Qt::RightButton))
			{
				selectionPressedMoved_();
			}
		}

		void PickingMode::mousePressEventImpl_(QMouseEvent* evt)
		{
			std::cout << "Starting pick" << std::endl;
			if (evt->modifiers() == Qt::NoModifier && (evt->buttons() & (Qt::LeftButton | Qt::RightButton)))
			{
				pick_select_ = (evt->buttons() == Qt::LeftButton);
				mouse_pos_pick_ = mouse_pos_new_;
			}
			else
			{
				pickParent_(evt->pos());
			}

		}

		void PickingMode::mouseReleaseEventImpl_(QMouseEvent* /*evt*/)
		{
			if (ignore_pick_)
			{
				ignore_pick_ = false;

				return;
			}

			selectObjects_();
		}

		Qt::Key PickingMode::getKey() const
		{
			return Qt::Key_Q;
		}

		void PickingMode::activate()
		{
			InteractionMode::activate();

			scene_->enterPickingMode();
			scene_->setCursor(QCursor(Qt::CrossCursor));
		}

		void PickingMode::deactivate()
		{
			InteractionMode::deactivate();

			scene_->exitPickingMode();
		}

		void PickingMode::selectionPressedMoved_()
		{
			auto x0 = std::min(mouse_pos_new_.x(), mouse_pos_pick_.x());
			auto x1 = std::max(mouse_pos_new_.x(), mouse_pos_pick_.x());
			auto y0 = std::min(mouse_pos_new_.y(), mouse_pos_pick_.y());
			auto y1 = std::max(mouse_pos_new_.y(), mouse_pos_pick_.y());

			QPoint p0 = scene_->mapFromGlobal(QPoint(x0, y0));
			QPoint p1 = scene_->mapFromGlobal(QPoint(x1, y1));

			scene_->setRubberBandGeometry(QRect(p0, p1));
			scene_->setRubberBandVisible(true);
		}

		void PickingMode::pickParent_(const QPoint& p)
		{
			ignore_pick_ = true;
			list<GeometricObject*> objects;
			scene_->pickObjects(p, objects);

			if (objects.empty()) return;

			Composite* composite = (Composite*)(**objects.begin()).getComposite();
			if (composite == 0) return;

			Composite* to_select = 0;
			Atom* atom = dynamic_cast<Atom*>(composite);
			if (atom != 0)
			{
				to_select = atom->getParent();
			}
			else
			{
				Bond* bond = dynamic_cast<Bond*>(composite);
				if (bond!= 0)
				{
					to_select = (Composite*)bond->getFirstAtom()->getParent();
				}
				else
				{
					to_select = composite;
				}
			}

			if (to_select != 0)
			{
				if (to_select->isSelected())
				{
					getMainControl()->deselectCompositeRecursive(to_select, true);
				}
				else
				{
					getMainControl()->selectCompositeRecursive(to_select, true);
				}
				getMainControl()->update(*to_select, true);
			}
		}

		void PickingMode::selectObjects_()
		{
			scene_->setRubberBandVisible(false);

			QPoint p0 = scene_->mapFromGlobal(mouse_pos_pick_);
			QPoint p1 = scene_->mapFromGlobal(mouse_pos_new_);

			list<GeometricObject*> objects;

			// draw the representations
			scene_->pickObjects(p0, p1, objects);

			// sent collected objects
			GeometricObjectSelectionMessage* message = new GeometricObjectSelectionMessage;
			message->setSelection(objects);
			message->setSelected(pick_select_);
			scene_->notify(message);
		}

	}
}