summaryrefslogtreecommitdiffstats
path: root/karbon/widgets/vruler.cpp
blob: c0ff0a20134583917d151eb139e3e651c271fa9c (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
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
/*
 * Kivio - Visual Modelling and Flowcharting
 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
 * Copyright (C) 2002 Patrick Julien <freak@codepimps.org>
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <tqpainter.h>

#include "kdebug.h"

#include "vruler.h"

#define MARKER_WIDTH 1
#define MARKER_HEIGHT 20
#define RULER_SIZE 20

const char *VRuler::m_nums[] = {
    "70 7 2 1",
    "  c Black",
    "X c None",
    "XX   XXXXXX XXXX   XXXX   XXXXXX XXX     XXXX  XXX     XXX   XXXX   XX",
    "X XXX XXXX  XXX XXX XX XXX XXXX  XXX XXXXXXX XXXXXXXXX XX XXX XX XXX X",
    "X XXX XXXXX XXXXXXX XXXXXX XXX X XXX XXXXXX XXXXXXXXX XXX XXX XX XXX X",
    "X XXX XXXXX XXXXX  XXXXX  XXX XX XXX    XXX    XXXXXX XXXX   XXXX    X",
    "X XXX XXXXX XXXX XXXXXXXXX XX     XXXXXX XX XXX XXXX XXXX XXX XXXXXX X",
    "X XXX XXXXX XXX XXXXXX XXX XXXXX XXXXXXX XX XXX XXXX XXXX XXX XXXXX XX",
    "XX   XXXXXX XXX     XXX   XXXXXX XXX    XXXX   XXXXX XXXXX   XXXX  XXX"
};

VRuler::VRuler(Qt::Orientation o, TQWidget *parent, const char *name) : super(parent, name, WRepaintNoErase | WResizeNoErase), m_pixmapNums(m_nums)
{
    setBackgroundMode(NoBackground);
    setFrameStyle(Box | Sunken);
    setLineWidth(1);
    setMidLineWidth(0);
    m_orientation = o;
    m_unit = KoUnit::U_PT;
    m_zoom = 1.0;
    m_firstVisible = 0;
    m_pixmapBuffer = 0;
    m_currentPosition = -1;

    if (m_orientation == Qt::Horizontal) {
        setFixedHeight(RULER_SIZE);
        initMarker(MARKER_WIDTH, MARKER_HEIGHT);
    } else {
        setFixedWidth(RULER_SIZE);
        initMarker(MARKER_HEIGHT, MARKER_WIDTH);
    }
}

VRuler::~VRuler()
{
    delete m_pixmapBuffer;
}

void VRuler::initMarker(TQ_INT32 w, TQ_INT32 h)
{
    TQPainter p;

    m_pixmapMarker.resize(w, h);
    p.begin(&m_pixmapMarker);
    p.setPen(blue);
    p.eraseRect(0, 0, w, h);
    p.drawLine(0, 0, w - 1, h - 1);
    p.end();
}

void VRuler::recalculateSize()
{
    TQ_INT32 w;
    TQ_INT32 h;

    if (m_pixmapBuffer) {
        delete m_pixmapBuffer;
        m_pixmapBuffer = 0;
    }

    if (m_orientation == Qt::Horizontal) {
        w = width();
        h = RULER_SIZE;
    } else {
        w = RULER_SIZE;
        h = height();
    }

    m_pixmapBuffer = new TQPixmap(w, h);
    TQ_CHECK_PTR(m_pixmapBuffer);

    drawRuler();
    updatePointer(m_currentPosition, m_currentPosition);
}

KoUnit::Unit VRuler::unit() const
{
    return  m_unit;
}

void VRuler::setUnit(KoUnit::Unit u)
{
    m_unit = u;
    drawRuler();
    updatePointer(m_currentPosition, m_currentPosition);
    repaint();
}

void VRuler::setZoom(double zoom)
{
    m_zoom = zoom;
    recalculateSize();
    drawRuler();
    updatePointer(m_currentPosition, m_currentPosition);
    repaint();
}

void VRuler::updatePointer(TQ_INT32 x, TQ_INT32 y)
{
    if (m_pixmapBuffer) {
        if (m_orientation == Qt::Horizontal) {
            if (m_currentPosition != -1)
                repaint(m_currentPosition, 1, MARKER_WIDTH, MARKER_HEIGHT);

            if (x != -1) {
                bitBlt(this, x, 1, &m_pixmapMarker, 0, 0, MARKER_WIDTH, MARKER_HEIGHT);
                m_currentPosition = x;
            }
        } else {
            if (m_currentPosition != -1)
                repaint(1, m_currentPosition, MARKER_HEIGHT, MARKER_WIDTH);

            if (y != -1) {
                bitBlt(this, 1, y, &m_pixmapMarker, 0, 0, MARKER_HEIGHT, MARKER_WIDTH);
                m_currentPosition = y;
            }
        }
    }
}

void VRuler::updateVisibleArea(TQ_INT32 xpos, TQ_INT32 ypos)
{
    if (m_orientation == Qt::Horizontal)
        m_firstVisible = xpos;
    else
        m_firstVisible = ypos;

	//kdDebug() << "--###-- VRuler::updateVisibleArea(" << xpos << ", " << ypos << ")" << endl;
    drawRuler();
    repaint();
    updatePointer(m_currentPosition, m_currentPosition);
	//kdDebug() << "--###-- VRuler::updatePointer(" << m_currentPosition << ", " << m_currentPosition << ")" << endl;
}

void VRuler::paintEvent(TQPaintEvent *e)
{
    if (m_pixmapBuffer) {
        const TQRect& rect = e -> rect();

        bitBlt(this, rect.topLeft(), m_pixmapBuffer, rect);
        super::paintEvent(e);
    }
}

void VRuler::drawRuler()
{
    TQPainter p;
    TQString buf;
    TQ_INT32 st1 = 0;
    TQ_INT32 st2 = 0;
    TQ_INT32 st3 = 0;
    TQ_INT32 st4 = 0;
    TQ_INT32 stt = 0;

    if (!m_pixmapBuffer)
        return;

    p.begin(m_pixmapBuffer);
    p.setPen(TQColor(0x70, 0x70, 0x70));
    p.setBackgroundColor(colorGroup().background());
    p.eraseRect(0, 0, m_pixmapBuffer -> width(), m_pixmapBuffer -> height());

    switch (m_unit) {
        case KoUnit::U_PT:
        case KoUnit::U_MM:
        case KoUnit::U_DD:
        case KoUnit::U_CC:
            st1 = 1;
            st2 = 5;
            st3 = 10;
            st4 = 25;
            stt = 100;
            break;
        case KoUnit::U_CM:
        case KoUnit::U_PI:
        case KoUnit::U_INCH:
            st1 = 1;
            st2 = 2;
            st3 = 5;
            st4 = 10;
            stt = 1;
            break;
        default:
            break;
    }

    TQ_INT32 pos = 0;
    bool s1 = KoUnit::fromUserValue(st1, m_unit) * m_zoom > 3.0;
    bool s2 = KoUnit::fromUserValue(st2, m_unit) * m_zoom > 3.0;
    bool s3 = KoUnit::fromUserValue(st3, m_unit) * m_zoom > 3.0;
    bool s4 = KoUnit::fromUserValue(st4, m_unit) * m_zoom > 3.0;

    if (m_orientation == Qt::Horizontal) {
        // XXX: This was 7 * 4 -- why? what was the idea about having 30 point intervals?
        float cx = KoUnit::fromUserValue(100, m_unit) / m_zoom;
        TQ_INT32 step = tqRound(cx);//((TQ_INT32)(cx / (float)stt) + 1) * stt;
        TQ_INT32 start = (TQ_INT32)(KoUnit::toUserValue(m_firstVisible, m_unit) / m_zoom);

        do {
            pos = (TQ_INT32)(KoUnit::fromUserValue(start, m_unit) * m_zoom - m_firstVisible);

            if (!s3 && s4 && start % st4 == 0)
                p.drawLine(pos, RULER_SIZE - 9, pos, RULER_SIZE);

            if (s3 && start % st3 == 0)
                p.drawLine(pos, RULER_SIZE - 9, pos, RULER_SIZE);

            if (s2 && start % st2 == 0)
                p.drawLine(pos, RULER_SIZE - 7, pos, RULER_SIZE);

            if (s1 && start % st1 == 0)
                p.drawLine(pos, RULER_SIZE - 5, pos, RULER_SIZE);

            if (step && start % step == 0) {
                buf.setNum(TQABS(start));
                drawNums(&p, pos, 4, buf, true);
            }

            start++;
        } while (pos < m_pixmapBuffer -> width());
    } else {
        m_firstVisible = 0;
        float cx = KoUnit::fromUserValue(100, m_unit) / m_zoom;
        TQ_INT32 height = m_pixmapBuffer -> height() - 1;
        TQ_INT32 step = tqRound(cx);
        TQ_INT32 start = (TQ_INT32)(KoUnit::toUserValue(m_firstVisible, m_unit) / m_zoom);

        do {
            pos = height - (TQ_INT32)(KoUnit::fromUserValue(start, m_unit) * m_zoom - m_firstVisible);

            if (!s3 && s4 && start % st4 == 0)
                p.drawLine(RULER_SIZE - 9, pos, RULER_SIZE, pos);

            if (s3 && start % st3 == 0)
                p.drawLine(RULER_SIZE - 9, pos, RULER_SIZE, pos);

            if (s2 && start % st2 == 0)
                p.drawLine(RULER_SIZE - 7, pos, RULER_SIZE, pos);

            if (s1 && start % st1 == 0)
                p.drawLine(RULER_SIZE - 5, pos, RULER_SIZE, pos);

            if (step && start % step == 0) {
                buf.setNum(TQABS(start));
                drawNums(&p, 4, pos, buf, false);
            }

            start++;
        } while (pos > 0);
    }

    p.end();
}

void VRuler::resizeEvent(TQResizeEvent *)
{
    recalculateSize();
}

void VRuler::show()
{
    if (m_orientation == Qt::Horizontal) {
        setFixedHeight(RULER_SIZE);
        initMarker(MARKER_WIDTH, MARKER_HEIGHT);
    } else {
        setFixedWidth(RULER_SIZE);
        initMarker(MARKER_HEIGHT, MARKER_WIDTH);
    }

    super::show();
}

void VRuler::hide()
{
    if (m_orientation == Qt::Horizontal)
        setFixedHeight(1);
    else
        setFixedWidth(1);
}

void VRuler::drawNums(TQPainter *p, TQ_INT32 x, TQ_INT32 y, TQString& num, bool orientationHoriz)
{
    if (orientationHoriz)
        x -= 7;
    else
        y -= 8;

    for (TQ_UINT32 k = 0; k < num.length(); k++) {
        TQ_INT32 st = num.at(k).digitValue() * 7;

        p -> drawPixmap(x, y, m_pixmapNums, st, 0, 7, 7);

        if (orientationHoriz)
            x += 7;
        else
            y += 8;
    }
}

#include "vruler.moc"