summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_ruler.cpp
blob: 2342692279459cfc0fbaf4a2ec0aa592baf08e37 (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
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
/*
 * Kivio - Visual Modelling and Flowcharting
 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
 * Copyright (C) 2002 Patrick Julien <[email protected]>
 *
 * 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 <tqstyle.h>

#include "kdebug.h"

#include "kis_ruler.h"

#define MARKER_WIDTH 1
#define MARKER_HEIGHT RULER_THICKNESS

const char *KisRuler::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"
};

KisRuler::KisRuler(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_THICKNESS);
        initMarker(MARKER_WIDTH, MARKER_HEIGHT);
    } else {
        setFixedWidth(RULER_THICKNESS);
        initMarker(MARKER_HEIGHT, MARKER_WIDTH);
    }
}

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

void KisRuler::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 KisRuler::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_THICKNESS;
    } else {
        w = RULER_THICKNESS;
        h = height();
    }

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

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

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

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

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

void KisRuler::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 KisRuler::updateVisibleArea(TQ_INT32 xpos, TQ_INT32 ypos)
{
    if (m_orientation == Qt::Horizontal)
        m_firstVisible = xpos;
    else
        m_firstVisible = ypos;

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

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

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

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

    if (!m_pixmapBuffer)
        return;

    p.begin(m_pixmapBuffer);
    p.setPen(colorGroup().text());
    p.setBackgroundColor(colorGroup().base());
    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;
            break;
        case KoUnit::U_CM:
        case KoUnit::U_PI:
        case KoUnit::U_INCH:
        default:
            st1 = 1;
            st2 = 2;
            st3 = 5;
            st4 = 10;
    }

    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;

    float cx = KoUnit::fromUserValue(100, m_unit) / m_zoom;
    TQ_INT32 step = tqRound(cx);

    if (step < 5) {
        step = 1;
    } else if (step < 10) {
        step = 5;
    } else if (step < 25) {
        step = 10;
    } else if (step < 50) {
        step = 25;
    } else if (step < 100) {
        step = 50;
    } else if (step < 250) {
        step = 100;
    } else if (step < 500) {
        step = 250;
    } else if (step < 1000) {
        step = 500;
    } else if (step < 2500) {
        step = 1000;
    } else if (step < 5000) {
        step = 2500;
    } else if (step < 10000) {
        step = 5000;
    } else  if (step < 25000) {
        step = 10000;
    } else if (step < 50000) {
        step = 25000;
    } else if (step < 100000) {
        step = 50000;
    } else {
        step = 100000;
    }

    TQ_INT32 start = (TQ_INT32)(KoUnit::fromUserValue(m_firstVisible, m_unit) / m_zoom);
    TQ_INT32 pos = 0;

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

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

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

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

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

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

            start++;
        } while (pos < m_pixmapBuffer->width());
    } else {
        do {
            pos = (TQ_INT32)(KoUnit::fromUserValue(start, m_unit) * m_zoom - m_firstVisible);

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

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

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

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

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

            start++;
        } while (pos < m_pixmapBuffer->height());
    }

    p.end();
}

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

void KisRuler::styleChange(TQStyle& oldStyle)
{
    Q_UNUSED(oldStyle);
    updateGeometry();
    drawRuler();
}

void KisRuler::paletteChange(const TQPalette& oldPalette)
{
    Q_UNUSED(oldPalette);
    drawRuler();
}

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

    super::show();
}

void KisRuler::hide()
{
    /*
    if (m_orientation == Qt::Horizontal)
        setFixedHeight(1);
    else
        setFixedWidth(1);
        */
    super::hide();
}

void KisRuler::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 "kis_ruler.moc"