summaryrefslogtreecommitdiff
path: root/src/gui/tikzview.cpp
blob: ddbc4045ae93ebcec6b7f6ef2dc1567ed8b258b0 (plain)
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
/*
    TikZiT - a GUI diagram editor for TikZ
    Copyright (C) 2018 Aleks Kissinger

    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 <https://www.gnu.org/licenses/>.
*/

#include "tikzview.h"
#include "tikzit.h"

#include <QDebug>
#include <QScrollBar>
#include <QSettings>

TikzView::TikzView(QWidget *parent) : QGraphicsView(parent)
{
    setRenderHint(QPainter::Antialiasing);
    setResizeAnchor(QGraphicsView::AnchorViewCenter);
    //setDragMode(QGraphicsView::RubberBandDrag);

    setBackgroundBrush(QBrush(Qt::white));

    _scale = 1.0f;
}

void TikzView::zoomIn()
{
    _scale *= 1.6f;
    scale(1.6,1.6);
}

void TikzView::zoomOut()
{
    _scale *= 0.625f;
    scale(0.625,0.625);
}

void TikzView::setScene(QGraphicsScene *scene)
{
    QGraphicsView::setScene(scene);
    centerOn(QPointF(0.0,0.0));
}

void TikzView::drawBackground(QPainter *painter, const QRectF &rect)
{
    QSettings settings("tikzit", "tikzit");
    QGraphicsView::drawBackground(painter, rect);
    // draw a gray background if disabled
    TikzScene *sc = static_cast<TikzScene*>(scene());
    if (!sc->enabled()) painter->fillRect(rect, QBrush(QColor(240,240,240)));

    // draw the grid

    QPen pen1;
    //pen1.setWidthF(0.5);
    pen1.setCosmetic(true);
    pen1.setColor(settings.value("grid-color-minor", QColor(250,250,255)).value<QColor>());

    QPen pen2 = pen1;
    pen2.setColor(settings.value("grid-color-major", QColor(240,240,250)).value<QColor>());

    QPen pen3 = pen1;
    pen3.setColor(settings.value("grid-color-axes", QColor(220,220,240)).value<QColor>());

    painter->setPen(pen1);

    if (_scale > 0.2f) {
        for (int x = -GRID_SEP; x > rect.left(); x -= GRID_SEP) {
            if (x % (GRID_SEP * GRID_N) != 0) {
                qreal xf = (qreal)x;
                painter->drawLine(xf, rect.top(), xf, rect.bottom());
            }
        }

        for (int x = GRID_SEP; x < rect.right(); x += GRID_SEP) {
            if (x % (GRID_SEP * GRID_N) != 0) {
                qreal xf = (qreal)x;
                painter->drawLine(xf, rect.top(), xf, rect.bottom());
            }
        }

        for (int y = -GRID_SEP; y > rect.top(); y -= GRID_SEP) {
            if (y % (GRID_SEP * GRID_N) != 0) {
                qreal yf = (qreal)y;
                painter->drawLine(rect.left(), yf, rect.right(), yf);
            }
        }

        for (int y = GRID_SEP; y < rect.bottom(); y += GRID_SEP) {
            if (y % (GRID_SEP * GRID_N) != 0) {
                qreal yf = (qreal)y;
                painter->drawLine(rect.left(), yf, rect.right(), yf);
            }
        }
    }

    painter->setPen(pen2);

    for (int x = -GRID_SEP*GRID_N; x > rect.left(); x -= GRID_SEP*GRID_N) {
        qreal xf = (qreal)x;
        painter->drawLine(xf, rect.top(), xf, rect.bottom());
    }

    for (int x = GRID_SEP*GRID_N; x < rect.right(); x += GRID_SEP*GRID_N) {
        qreal xf = (qreal)x;
        painter->drawLine(xf, rect.top(), xf, rect.bottom());
    }

    for (int y = -GRID_SEP*GRID_N; y > rect.top(); y -= GRID_SEP*GRID_N) {
        qreal yf = (qreal)y;
        painter->drawLine(rect.left(), yf, rect.right(), yf);
    }

    for (int y = GRID_SEP*GRID_N; y < rect.bottom(); y += GRID_SEP*GRID_N) {
        qreal yf = (qreal)y;
        painter->drawLine(rect.left(), yf, rect.right(), yf);
    }

    painter->setPen(pen3);
    painter->drawLine(rect.left(), 0, rect.right(), 0);
    painter->drawLine(0, rect.top(), 0, rect.bottom());
}

void TikzView::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier) {
        event->setModifiers(Qt::NoModifier);
        QGraphicsView::wheelEvent(event);
    } else if (event->modifiers() & Qt::ControlModifier) {
        if (event->angleDelta().y() > 0) {
            zoomIn();
        } else if (event->angleDelta().y() < 0) {
            zoomOut();
        }
    }
}