forked from SpineML/SpineCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SC_network_2d_visualiser_panel.cpp
404 lines (354 loc) · 13.9 KB
/
SC_network_2d_visualiser_panel.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Authors: Alex Cope, Seb James **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "SC_network_2d_visualiser_panel.h"
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
#define RETINA_SUPPORT 1.0
#else
#ifdef Q_OS_WIN2
#define RETINA_SUPPORT 1.0
#define RETINA_SUPPORT2 1.75
#else
#define RETINA_SUPPORT 1.0
#endif
#endif
#define CHANGED_TIME 10
#ifdef Q_OS_MAC2
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
#else
GLWidget::GLWidget(QWidget *parent):QWidget(parent)
#endif
{
// variable for making sure we don't redraw the openGL when we don't need to
changed = CHANGED_TIME;
currSelType = 0;
currSelInd = 0;
GLscale = 100.0*RETINA_SUPPORT;
targGLscale = 100.0*RETINA_SUPPORT;
viewX = 0.0;
viewY = 0.0;
connectMode = false;
addSelection = false;
gridSelect = false;
gridScale = 0.5;
this->button = Qt::NoButton;
this->LMB_drag = false;
// accept both tab and click focus
this->setFocusPolicy(Qt::StrongFocus);
// Nothing is moving to begin with.
this->itemMoving = false;
}
GLWidget::~GLWidget()
{
}
void GLWidget::redrawGLview()
{
changed = CHANGED_TIME;
}
void GLWidget::animate()
{
float animSpeed = 1.0;
GLscale += (targGLscale - GLscale)*animSpeed;
// only redraw the openGL when we need to
if (changed) {
--changed;
repaint();
}
}
void GLWidget::mousePressEvent(QMouseEvent* event)
{
this->button = event->button();
// convert the incoming x and y into the openGL coordinates
float xGL = float((event->x()*RETINA_SUPPORT)-(this->width()*RETINA_SUPPORT)/2)*2.0/(GLscale)-viewX;
float yGL = -(float((event->y()*RETINA_SUPPORT)-(this->height()*RETINA_SUPPORT)/2)*2.0/(GLscale)-viewY);
// select under the mouse
//cout << "coords " << float(xGL) << " " << float(yGL) << endl;
if (this->connectMode == false) {
if (event->button() == Qt::LeftButton) {
setCursor(Qt::ClosedHandCursor);
// Rather than using the shift modifier, lets use logic in
// onLeftMouseDown to see if we should select the object or drag the group.
bool shiftDown = (QApplication::keyboardModifiers() & Qt::ShiftModifier);
emit onLeftMouseDown(xGL, yGL, this->GLscale, shiftDown);
}
if (event->button() == Qt::RightButton) {
setCursor(Qt::CrossCursor);
emit onRightMouseDown(xGL, yGL, this->GLscale);
}
} else {
if (event->button() == Qt::LeftButton) {
// fix the current edge
emit addBezierOrProjection(xGL, yGL);
}
}
event->setAccepted(true);
LMB_drag = false;
}
void GLWidget::mouseReleaseEvent(QMouseEvent* event)
{
changed = CHANGED_TIME;
this->button = Qt::NoButton;
setCursor(Qt::ArrowCursor);
// convert the incoming x and y into the openGL coordinates
#ifdef NEED_MOUSE_UP_LOGIC
float xGL = float((event->x()*RETINA_SUPPORT)-(this->width()*RETINA_SUPPORT)/2)*2.0/(GLscale)-viewX;
float yGL = -(float((event->y()*RETINA_SUPPORT)-(this->height()*RETINA_SUPPORT)/2)*2.0/(GLscale)-viewY);
//qDebug() << "coords: " << float(xGL) << " " << float(yGL) << endl;
#endif
// select under the mouse
if (this->connectMode == false) {
if (event->button() == Qt::LeftButton && !this->itemMoving) {
#ifdef NEED_MOUSE_UP_LOGIC
emit onLeftMouseUp(xGL, yGL, this->GLscale);
#endif
} else if (event->button() == Qt::LeftButton && this->itemMoving) {
// Item was released after moving.
emit itemWasMoved();
this->itemMoving = false;
}
if (event->button() == Qt::LeftButton && this->LMB_drag) {
emit endDragSelect();
LMB_drag = false;
}
if (event->button() == Qt::RightButton) {
emit endDragSelect();
}
} else {
if (event->button() == Qt::RightButton) {
// abort connection
this->connectMode = false;
//this->setMouseTracking(false);
emit abortProjection();
}
}
event->setAccepted(true);
}
void GLWidget::wheelEvent(QWheelEvent* event)
{
changed = CHANGED_TIME;
float val = float(event->delta()) / 320.0;
val = pow(2.0f,val);
if (event->orientation() == Qt::Vertical) {
this->targGLscale *= (val);
#ifdef Q_OS_LINUX
float xGL = float((event->x()*RETINA_SUPPORT)-(this->width()*RETINA_SUPPORT)/2)*2.0/GLscale;
float yGL = -float((event->y()*RETINA_SUPPORT)-(this->height()*RETINA_SUPPORT)/2)*2.0/GLscale;
// aim the scrolling at the current cursor location (goes bad on osx)
double scaleVal = 5.0;
if (val > 1.0) {
viewX -= xGL/scaleVal;
viewY += yGL/scaleVal;
}
if (val < 1.0) {
viewX += xGL/scaleVal;
viewY -= yGL/scaleVal;
}
#endif
}
event->setAccepted(true);
}
void GLWidget::mouseMoveEvent(QMouseEvent* event)
{
changed = CHANGED_TIME;
// convert mouse event into openGL coordinates
float xGL = float((event->x()*RETINA_SUPPORT)-(this->width()*RETINA_SUPPORT)/2)*2.0/(GLscale)-viewX;
float yGL = -(float((event->y()*RETINA_SUPPORT)-(this->height()*RETINA_SUPPORT)/2)*2.0/(GLscale)-viewY);
if (this->connectMode == false) {
if ((QApplication::keyboardModifiers() & Qt::ShiftModifier)) {
if (this->button == Qt::LeftButton) {
if (LMB_drag == false) {
qDebug() << "Start LMB drag";
emit onRightMouseDown(xGL, yGL, this->GLscale);
} else {
emit dragSelect(xGL, yGL);
}
LMB_drag = true;
}
} else {
if (this->button == Qt::LeftButton) {
this->itemMoving = true;
emit mouseMove(xGL, yGL);
} else {
this->itemMoving = false;
}
}
if (this->button == Qt::RightButton) {
emit dragSelect(xGL, yGL);
}
} else {
emit drawSynapse(xGL, yGL);
}
//repaint();
event->setAccepted(true);
}
void GLWidget::keyPressEvent(QKeyEvent * event)
{
changed = CHANGED_TIME;
if (event->type() == QEvent::KeyPress) {
if (event->key() == Qt::Key_Control) {
this->gridSelect = true;
}
/*
if (event->key() == Qt::Key_Shift) {
this->addSelection = true;
}
*/
}
event->setAccepted(true);
}
void GLWidget::keyReleaseEvent(QKeyEvent * event)
{
changed = CHANGED_TIME;
if (event->type() == QEvent::KeyRelease) {
if (event->key() == Qt::Key_Control) {
this->gridSelect = false;
}
/*
if (event->key() == Qt::Key_Shift) {
this->addSelection = false;
}
*/
}
event->setAccepted(true);
}
void GLWidget::initializeGL()
{
//QColor qtCol = QColor::fromCmykF(0.5, 0.5, 0.5, 0.0);
//qglClearColor(qtCol.light());
}
void GLWidget::resizeGL(int, int)
{
}
void GLWidget::paintEvent(QPaintEvent * event)
{
#ifdef Q_OS_MAC2
makeCurrent();
//QPixmap pix((this->width()*RETINA_SUPPORT), (this->height()*RETINA_SUPPORT))
glViewport (0, 0, (this->width()*RETINA_SUPPORT), (this->height()*RETINA_SUPPORT));
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, (this->width()*RETINA_SUPPORT),0,(this->height()*RETINA_SUPPORT),-1,1);
glMatrixMode (GL_MODELVIEW);
QImage workaround_for_slow_osx_drawing((this->width()*RETINA_SUPPORT), (this->height()*RETINA_SUPPORT), QImage::Format_RGB32);
QPainter painter(&workaround_for_slow_osx_drawing);
workaround_for_slow_osx_drawing.fill(Qt::red); // debug so we can check all is painted
#else
QPainter painter(this);
#endif
//painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setRenderHint( QPainter::TextAntialiasing,true);
painter.setRenderHint(QPainter::SmoothPixmapTransform,true);
QRect fillRectangle(event->rect().x()*RETINA_SUPPORT, event->rect().y()*RETINA_SUPPORT, event->rect().width()*RETINA_SUPPORT, event->rect().height()*RETINA_SUPPORT);
painter.fillRect(fillRectangle, Qt::white);
// painter.beginNativePainting();
// setup the painter
#ifdef Q_OS_MAC2
QFont font("Monospace", GLscale/18.0f*1.3f);
font.setPointSizeF(GLscale/18.0f*1.3f);
#else
QFont font("Monospace", GLscale/18.0f);
font.setPointSizeF(GLscale/18.0f);
#endif
font.setStyleHint(QFont::TypeWriter);
painter.setPen(QCOL_BLACK);
painter.setFont(font);
// if grid then draw it up:
#ifndef Q_OS_MAC2
if (this->gridSelect) {
painter.save();
painter.translate((this->width()*RETINA_SUPPORT)/2, (this->height()*RETINA_SUPPORT)/2);
//painter.scale(,-GLscale/2.0);
painter.translate(viewX*(GLscale/2.0), viewY*(GLscale/2.0));
// too fine a grid looks bad and is of no use
QPen pen = painter.pen();
pen.setWidth(1.5*RETINA_SUPPORT);
painter.setPen(pen);
if (GLscale > 10) {
for (float i = round(-viewX/this->gridScale)*this->gridScale*GLscale; i < round(-viewX/this->gridScale)*this->gridScale+(this->width()*RETINA_SUPPORT); i +=this->gridScale*GLscale/2.0) {
for (float j =round(viewY/this->gridScale)*this->gridScale*GLscale; j < round(viewY/this->gridScale)*this->gridScale+(this->height()*RETINA_SUPPORT); j +=this->gridScale*GLscale/2.0) {
painter.drawPoint(QPointF(i,j));
}
}
for (float i = round(-viewX/this->gridScale)*this->gridScale*GLscale; i > round(-viewX/this->gridScale)*this->gridScale-(this->width()*RETINA_SUPPORT); i -=this->gridScale*GLscale/2.0) {
for (float j =round(viewY/this->gridScale)*this->gridScale*GLscale; j > round(viewY/this->gridScale)*this->gridScale-(this->height()*RETINA_SUPPORT); j -=this->gridScale*GLscale/2.0) {
painter.drawPoint(QPointF(i,j));
}
}
for (float i = round(-viewX/this->gridScale)*this->gridScale*GLscale; i > round(-viewX/this->gridScale)*this->gridScale-(this->width()*RETINA_SUPPORT); i -=this->gridScale*GLscale/2.0) {
for (float j =round(viewY/this->gridScale)*this->gridScale*GLscale; j < round(viewY/this->gridScale)*this->gridScale+(this->height()*RETINA_SUPPORT); j +=this->gridScale*GLscale/2.0) {
painter.drawPoint(QPointF(i,j));
}
}
for (float i = round(-viewX/this->gridScale)*this->gridScale*GLscale; i < round(-viewX/this->gridScale)*this->gridScale+(this->width()*RETINA_SUPPORT); i +=this->gridScale*GLscale/2.0) {
for (float j =round(viewY/this->gridScale)*this->gridScale*GLscale; j > round(viewY/this->gridScale)*this->gridScale-(this->height()*RETINA_SUPPORT); j -=this->gridScale*GLscale/2.0) {
painter.drawPoint(QPointF(i,j));
}
}
}
painter.restore();
}
#endif
// send the painter, and the transforms needed to paint to the right place
emit reDraw(&painter, GLscale, viewX, viewY, (this->width()*RETINA_SUPPORT), (this->height()*RETINA_SUPPORT), standardDrawStyle);
//painter.endNativePainting();
painter.end();
#ifdef Q_OS_MAC2
QImage gldata = QGLWidget::convertToGLFormat(workaround_for_slow_osx_drawing);
glDrawPixels((this->width()*RETINA_SUPPORT), (this->height()*RETINA_SUPPORT), GL_RGBA, GL_UNSIGNED_BYTE, gldata.bits());
glSwapAPPLE();
#endif
}
void GLWidget::move(GLfloat x, GLfloat y)
{
this->viewX =x;
this->viewY =-y;
//emit reDraw();
}
void GLWidget::zoomOut()
{
this->targGLscale *= 2.0;
}
void GLWidget::zoomIn()
{
this->targGLscale /= 2.0;
}
void GLWidget::startConnect()
{
this->connectMode = true;
this->setMouseTracking(true);
}
void GLWidget::finishConnect()
{
this->connectMode = false;
this->setMouseTracking(false);
changed = CHANGED_TIME;
}
void GLWidget::saveImage()
{
//this->makeCurrent();
//QPixmap pic = this->renderPixmap((this->width()*RETINA_SUPPORT)*4,(this->height()*RETINA_SUPPORT)*4, false);
//QImage image = pic.toImage();
//image.save("/home/mp/test_image.png","png");
}