File: figure2d.cpp

package info (click to toggle)
vibes 0.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,684 kB
  • sloc: cpp: 6,120; python: 412; makefile: 214; sh: 13
file content (355 lines) | stat: -rw-r--r-- 11,604 bytes parent folder | download
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
#include "figure2d.h"
#include <QWheelEvent>
#include <QtCore>
#include <QFileDialog>
#include <QSvgGenerator>
#include <QScrollBar>

#include "vibesscene2d.h"
#include <QtGui>
#include <QComboBox>
#include <QLabel>
#include <QString>

Figure2D::Figure2D(QWidget *parent) :
    QGraphicsView(parent),
    lbProjX(new QLabel("xlabelhere",this)),
    lbProjY(new QLabel("ylabelhere",this)),
    showAxis(true),
    fontSize(11),
    xTicksSpacing(50),
    yTicksSpacing(35)
{
    setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

    // Create a new scene
    setScene(new VibesScene2D(this));
    this->scale(1.0, -1.0);
    this->show();
    setDragMode(ScrollHandDrag);
    // Force full viewport update (avoid problems with axes)
    setViewportUpdateMode(FullViewportUpdate);
    // Never show the scrollbars
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    // Keep the view centered when resizing window
    setResizeAnchor(AnchorViewCenter);

    lbProjX->setAlignment(Qt::AlignRight);
    lbProjX->move(width()-50, 0);
    lbProjX->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
    lbProjX->resize(100, 20);
    lbProjX->installEventFilter(this);
    lbProjX->show();

    lbProjY->move(5, 15);
    lbProjY->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
    lbProjY->resize(100, 20);
    lbProjY->installEventFilter(this);
    lbProjY->show();

    cbProjX = new QComboBox(lbProjX);
    cbProjX->setMaximumSize(lbProjX->size());
    connect(cbProjX, SIGNAL(currentTextChanged(QString)), lbProjX, SLOT(setText(QString)));
    connect(cbProjX, SIGNAL(currentIndexChanged(QString)), lbProjX, SLOT(setText(QString)));
    for (int i=0; i<3; ++i)
        cbProjX->addItem(QString("x: dim %1").arg(i), i);
    cbProjX->setCurrentIndex(scene()->dimX());
    //cbProjX->installEventFilter(this);
    connect(cbProjX, SIGNAL(currentIndexChanged(int)), scene(), SLOT(setDimX(int)));
    connect(scene(), SIGNAL(changedDimX(int)), cbProjX, SLOT(setCurrentIndex(int)));

    cbProjY = new QComboBox(lbProjY);
    cbProjY->setMaximumSize(lbProjY->size());
    connect(cbProjY, SIGNAL(currentTextChanged(QString)), lbProjY, SLOT(setText(QString)));
    connect(cbProjY, SIGNAL(currentIndexChanged(QString)), lbProjY, SLOT(setText(QString)));
    for (int i=0; i<3; ++i)
        cbProjY->addItem(QString("y: dim %1").arg(i), i);
    cbProjY->setCurrentIndex(scene()->dimY());
    //cbProjY->installEventFilter(this);
    connect(cbProjY, SIGNAL(currentIndexChanged(int)), scene(), SLOT(setDimY(int)));
    connect(scene(), SIGNAL(changedDimY(int)), cbProjY, SLOT(setCurrentIndex(int)));

    connect(scene(), SIGNAL(dimensionsChanged()), this, SLOT(refreshProjectionSelectors()));
}

bool Figure2D::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == lbProjX) {
        if (event->type() == QEvent::Enter) {
            cbProjX->show();
            return true;
        } else {
            return false;
        }
    } else if (obj == lbProjY) {
        if (event->type() == QEvent::Enter) {
            cbProjY->show();
            return true;
        } else {
            return false;
        }
    } else {
        // pass the event on to the parent class
        return QGraphicsView::eventFilter(obj, event);
    }
}

void Figure2D::refreshProjectionSelectors()
{
    for (int i=0; i<scene()->nbDim(); ++i)
    {
        if (i >= cbProjX->count())
            cbProjX->addItem(scene()->dimName(i), i);
        else
            cbProjX->setItemText(i,scene()->dimName(i));

        if (i >= cbProjY->count())
            cbProjY->addItem(scene()->dimName(i), i);
        else
            cbProjY->setItemText(i,scene()->dimName(i));
    }
    cbProjX->setCurrentIndex( scene()->dimX() );
    cbProjY->setCurrentIndex( scene()->dimY() );

    lbProjX->setText( scene()->dimName( scene()->dimX() ) );
    lbProjY->setText( scene()->dimName( scene()->dimY() ) );
    /// \todo Remove unused dimensions
}

void Figure2D::mouseMoveEvent(QMouseEvent * event)
{
    if (cbProjX->isVisible())
    {
        cbProjX->hide();
        refreshProjectionSelectors();
        //this->scene()->update();
    }
    if (cbProjY->isVisible())
    {
        cbProjY->hide();
        refreshProjectionSelectors();
        //this->scene()->update();
    }
    QGraphicsView::mouseMoveEvent(event);
}


void Figure2D::drawForeground(QPainter *painter, const QRectF &rect)
{
    // if axis is disable
    if (!showAxis) return;
    // Black pen and empty brush for drawing axis
    painter->setPen(QPen(Qt::black, 0));//, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin));
    painter->setBrush(Qt::NoBrush);

    // Draw figure border frame (if bounds are specifier)
    //painter->drawRect(this->sceneRect());

    // Min spacing between ticks (divisor is min spacing in px)
    double nb_ticks_x = this->viewport()->width() / (double)xTicksSpacing;
    double nb_ticks_y = this->viewport()->height() / (double)yTicksSpacing;

    int log_scale_x = ceil(log10(rect.width()/nb_ticks_x)*3.0);
    double scale_x = pow(10.0, floor((double)log_scale_x/3));
    switch (log_scale_x%3) {
    case 0: break;
    case 1: case-2: scale_x *= 2.0; break;
    case 2: case-1: scale_x *= 5.0; break;
    }

    int log_scale_y = ceil(log10(rect.height()/nb_ticks_y)*3.0);
    double scale_y = pow(10.0, floor((double)log_scale_y/3));
    switch (log_scale_y%3) {
    case 0: break;
    case 1: case-2: scale_y *= 2.0; break;
    case 2: case-1: scale_y *= 5.0; break;
    }

    double x0 = ceil(rect.left() / scale_x) * scale_x;
    double y0 = ceil(qMin(rect.bottom(),rect.top()) / scale_y) * scale_y;

    painter->setTransform(QTransform());
    painter->setWindow(this->viewport()->rect());

    QFont axisTicksFont("Helvetica", fontSize);
    axisTicksFont.setStyleHint(QFont::Helvetica);
    painter->setFont(axisTicksFont);
    painter->setPen(QColor(0,0,0));

        for (double xtick=x0; xtick<qMax(rect.right(), rect.left()); xtick+=scale_x)
    {
        double x_wnd = mapFromScene(xtick,0).x();

        painter->drawLine(x_wnd,0,x_wnd,5);
        QString xtick_txt;
        if(fabs(xtick) < 1.0e-12)
            xtick_txt.setNum(xtick, 'f', 0);
        else
            xtick_txt.setNum(xtick, 'g');
        painter->drawText(x_wnd+3,fontSize+3, xtick_txt);
    }

    for (double ytick=y0; ytick<qMax(rect.top(),rect.bottom()); ytick+=scale_y)
    {
        double y_wnd = mapFromScene(0,ytick).y();

        painter->drawLine(0,y_wnd,5,y_wnd);
        QString ytick_txt;
        if(fabs(ytick) < 1.0e-12)
            ytick_txt.setNum(ytick, 'f', 0);
        else
            ytick_txt.setNum(ytick, 'g');
        painter->drawText(3, y_wnd+fontSize+3, ytick_txt);
    }
}

void Figure2D::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers().testFlag(Qt::ControlModifier))
    {
        QGraphicsView::wheelEvent(event);
    }
    else
    {
        setTransformationAnchor(QGraphicsView::AnchorUnderMouse);

        // Scales the view to zoom according to mouse wheel
        double s = qPow(2.0, 0.04*event->angleDelta().y()/8.0);

        double sx = s;
        double sy = s;

        if (event->modifiers().testFlag(Qt::AltModifier))
            sx = 1.0;
        if (event->modifiers().testFlag(Qt::ShiftModifier))
            sy = 1.0;
        this->scale(sx,sy);
//        double dx = sceneRect().width() * (s - 1.0);
//        double dy = sceneRect().height() * (s - 1.0);
//        this->setSceneRect(sceneRect().adjusted(-dx,-dy,dx,dy));
//        qDebug() << sceneRect();
//        fitInView(sceneRect());
    }
}

void Figure2D::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_A:
      this->showAxis = !this->showAxis;
      this->scene()->update();
      break;
    case Qt::Key_Plus:
    case Qt::Key_Q:
        this->scale(1.25,1.25);
        break;
    case Qt::Key_Minus:
    case Qt::Key_W:
        this->scale(0.8,0.8);
        break;
    case Qt::Key_X:
        this->xTicksSpacing = this->xTicksSpacing < 512? this->xTicksSpacing+1: 512;
        this->scene()->update();
        break;
    case Qt::Key_S:
        this->xTicksSpacing = this->xTicksSpacing > 1? this->xTicksSpacing-1: 1;
        this->scene()->update();
        break;
    case Qt::Key_Y:
        this->yTicksSpacing = this->yTicksSpacing < 512? this->yTicksSpacing+1: 512;
        this->scene()->update();
        break;
    case Qt::Key_H:
        this->yTicksSpacing = this->yTicksSpacing > 1? this->yTicksSpacing-1: 1;
        this->scene()->update();
        break;
    case Qt::Key_Asterisk:
    case Qt::Key_F:
        this->fontSize = this->fontSize < 512? this->fontSize+1: 512;
        this->scene()->update();
        break;
    case Qt::Key_Slash:
    case Qt::Key_V:
        this->fontSize = this->fontSize > 1? this->fontSize-1: 1;
        this->scene()->update();
        break;
    case Qt::Key_Space:
        // Back to default settings
        this->showAxis = true;
        this->xTicksSpacing = 50;
        this->yTicksSpacing = 35;
        this->fontSize = 11;
        this->scene()->update();
        break;
    default:
        QGraphicsView::keyPressEvent(event);
    }
}

void Figure2D::closeEvent(QCloseEvent *event)
{
    // Delete on close
    this->deleteLater();
}

void Figure2D::resizeEvent(QResizeEvent *event)
{
    lbProjX->move(width()-lbProjX->width()-5, 10);
    lbProjY->move(10, height()-lbProjX->height()-5);

    if (event->oldSize().width() > 0 && event->oldSize().height())
        this->scale((double)event->size().width() / event->oldSize().width(),
                    (double)event->size().height() / event->oldSize().height());

    QGraphicsView::resizeEvent(event);
}

void Figure2D::exportGraphics(QString fileName)
{
    // Open file save dialog if no filename given
    if (fileName.isEmpty())
        fileName = QFileDialog::getSaveFileName(this, tr("Export VIBes graphics"),
                QString(), tr("Portable Network Graphics (*.png);;"
                              "Joint Photographic Experts Group (*.jpg *.jpeg);;"
                              "Windows Bitmap (*.bmp);;"
                              "Scalable Vector Graphics (*.svg)"));

    // Abort if no file selected
    if (fileName.isEmpty())
        return;

    // Append .png if no extension was specified
    if (fileName.indexOf('.',1) < 0) // Search '.' from the second character (*nix hidden files start with a dot)
        fileName.append(".png");

    // Save as raster
    if (fileName.endsWith(".jpg", Qt::CaseInsensitive)
            || fileName.endsWith(".jpeg", Qt::CaseInsensitive)
            || fileName.endsWith(".png", Qt::CaseInsensitive)
            || fileName.endsWith(".bmp", Qt::CaseInsensitive))
    {
        QImage image(this->size(), QImage::Format_ARGB32);
        image.fill(QColor(255,255,255,0));
        QPainter painter;
        painter.begin(&image);
        this->render(&painter);
        painter.end();
        image.save(fileName);
    }
    // Save as vector (SVG)
    else if (fileName.endsWith(".svg", Qt::CaseInsensitive))
    {
        QSvgGenerator generator;
        generator.setFileName(fileName);
        generator.setSize(this->size());
        generator.setViewBox(QRect(QPoint(0,0),this->size()));
        generator.setTitle(tr("VIBes figure"));
        generator.setDescription(tr("Graphics generated with VIBes on %1.").arg(QDateTime::currentDateTime().toString()));
        QPainter painter;
        painter.begin(&generator);
        this->render(&painter);
        painter.end();
    }
}