File: GuidesTool.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 261,632 kB
  • sloc: cpp: 650,836; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 17
file content (463 lines) | stat: -rw-r--r-- 15,732 bytes parent folder | download | duplicates (3)
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* This file is part of the KDE project
 * Copyright (C) 2008,2011 Jan Hambrecht <jaham@gmx.net>
 * Copyright (C) 2009 Carlos Licea <carlos.licea@kdemail.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "GuidesTool.h"
#include "GuidesToolFactory.h" // for the Id
#include "GuidesToolOptionWidget.h"
#include "InsertGuidesToolOptionWidget.h"

#include <KoPointerEvent.h>
#include <KoCanvasBase.h>
#include <KoCanvasController.h>
#include <KoCanvasResourceManager.h>
#include <KoViewConverter.h>
#include <KoGuidesData.h>
#include <KoToolManager.h>

#include <QDebug>

#include <QPainter>

GuidesTool::GuidesTool(KoCanvasBase *canvas)
    : KoToolBase(canvas),
    m_orientation(Qt::Horizontal),
    m_index(-1),
    m_position(0),
    m_mode(EditGuide),
    m_options(0),
    m_isMoving(false)
{
}

GuidesTool::~GuidesTool()
{
}

void GuidesTool::paint(QPainter &painter, const KoViewConverter &converter)
{
    if (m_mode == EditGuide && m_index == -1)
        return;

    KoCanvasController *controller = canvas()->canvasController();
    QPoint documentOrigin = canvas()->documentOrigin();
    QPoint canvasOffset(controller->canvasOffsetX(), controller->canvasOffsetY());

    QPointF start, end;
    if (m_orientation == Qt::Horizontal) {
        qreal left = -canvasOffset.x() - documentOrigin.x();
        qreal right = left + canvas()->canvasWidget()->width();
        start = QPointF(left, converter.documentToViewY(m_position));
        end = QPointF(right, converter.documentToViewY(m_position));
    } else {
        qreal top = -canvasOffset.y() - documentOrigin.y();
        qreal bottom = top + canvas()->canvasWidget()->height();
        start = QPointF(converter.documentToViewX(m_position), top);
        end = QPointF(converter.documentToViewX(m_position), bottom);
    }
    painter.setPen(QPen(Qt::red, 0));
    painter.drawLine(start, end);
}

void GuidesTool::repaintDecorations()
{
    canvas()->updateCanvas(updateRectFromGuideLine(m_position, m_orientation));
}

QRectF GuidesTool::updateRectFromGuideLine(qreal position, Qt::Orientation orientation)
{
    QRectF rect;
    KoCanvasController *controller = canvas()->canvasController();
    QPoint documentOrigin = canvas()->documentOrigin();
    QPoint canvasOffset(controller->canvasOffsetX(), controller->canvasOffsetY());
    if (orientation == Qt::Horizontal) {
        qreal pixelBorder = canvas()->viewConverter()->viewToDocumentY(2.0);
        rect.setTop(position - pixelBorder);
        rect.setBottom(position + pixelBorder);
        rect.setLeft(canvas()->viewConverter()->viewToDocumentX(-canvasOffset.x()-documentOrigin.x()));
        rect.setWidth(canvas()->viewConverter()->viewToDocumentX(canvas()->canvasWidget()->width()));
    } else {
        qreal pixelBorder = canvas()->viewConverter()->viewToDocumentX(2.0);
        rect.setLeft(position - pixelBorder);
        rect.setRight(position + pixelBorder);
        rect.setTop(canvas()->viewConverter()->viewToDocumentY(-canvasOffset.y()-documentOrigin.y()));
        rect.setHeight(canvas()->viewConverter()->viewToDocumentY(canvas()->canvasWidget()->height()));
    }

    return rect;
}

void GuidesTool::activate(ToolActivation /*toolActivation*/, const QSet<KoShape*> &)
{
    if (m_index >= 0)
        useCursor(m_orientation == Qt::Horizontal ? Qt::SizeVerCursor : Qt::SizeHorCursor);
    else
        useCursor(Qt::ArrowCursor);

    if (m_options) {
        KoGuidesData *guidesData = canvas()->guidesData();
        if (! guidesData)
            return;
        m_options->setHorizontalGuideLines(guidesData->horizontalGuideLines());
        m_options->setVerticalGuideLines(guidesData->verticalGuideLines());
        m_options->selectGuideLine(m_orientation, m_index);
        m_options->setUnit(canvas()->unit());
    }
}

void GuidesTool::deactivate()
{
    canvas()->canvasWidget()->releaseMouse();
    m_mode = EditGuide;
    m_index = -1;
}

void GuidesTool::mousePressEvent(KoPointerEvent *event)
{
    GuideLine line = guideLineAtPosition(event->point);
    if (line.second >= 0) {
        guideLineSelected(line.first, static_cast<int>(line.second));
        m_isMoving = true;
    }
}

void GuidesTool::mouseMoveEvent(KoPointerEvent *event)
{
    if (m_mode == EditGuide && ! m_isMoving) {
        GuideLine line = guideLineAtPosition(event->point);
        if (line.second < 0) {
            useCursor(Qt::ArrowCursor);
            setStatusText(i18n("Double click to add guide line."));
        } else {
            useCursor(line.first == Qt::Horizontal ? Qt::SizeVerCursor : Qt::SizeHorCursor);
            setStatusText(i18n("Click and drag to move guide line. Double click to remove guide line."));
        }
    } else {
        setStatusText("");
        repaintDecorations();
        m_position = m_orientation == Qt::Horizontal ? event->point.y() : event->point.x();
        updateGuidePosition(m_position);
        repaintDecorations();
    }
}

void GuidesTool::mouseReleaseEvent(KoPointerEvent *event)
{
    Q_UNUSED(event);

    KoGuidesData *guidesData = canvas()->guidesData();
    if (! guidesData) {
        event->ignore();
        return;
    }

    if (m_mode == AddGuide) {
        // add the new guide line
        guidesData->addGuideLine(m_orientation, m_position);
    } else if (m_mode == EditGuide) {
        if (m_isMoving) {
            m_isMoving = false;
            if (m_orientation == Qt::Horizontal)
                m_options->setHorizontalGuideLines(guidesData->horizontalGuideLines());
            else
                m_options->setVerticalGuideLines(guidesData->verticalGuideLines());
            m_options->selectGuideLine(m_orientation, m_index);
        }
    }

    if (m_mode != EditGuide)
        emit done();
}

void GuidesTool::mouseDoubleClickEvent(KoPointerEvent *event)
{
    KoGuidesData *guidesData = canvas()->guidesData();
    if (!guidesData) {
        event->ignore();
        return;
    }

    repaintDecorations();

    // get guide line at position
    GuideLine line = guideLineAtPosition(event->point);
    if (line.second < 0) {
        // no guide line hit -> insert a new one
        m_orientation = m_options->orientation();
        m_position = m_orientation == Qt::Horizontal ? event->point.y() : event->point.x();
        // no guide line hit -> insert a new one
        guidesData->addGuideLine(m_orientation, m_position);
        if (m_orientation == Qt::Horizontal) {
            m_options->setHorizontalGuideLines(guidesData->horizontalGuideLines());
            m_index = guidesData->horizontalGuideLines().count()-1;
        } else {
            m_options->setVerticalGuideLines(guidesData->verticalGuideLines());
            m_index = guidesData->verticalGuideLines().count()-1;
        }
        m_options->selectGuideLine(m_orientation, m_index);
    }
    else {
        // guide line hit -> remove it
        QList<qreal> lines;
        if (line.first == Qt::Horizontal) {
            lines = guidesData->horizontalGuideLines();
            lines.removeAt(line.second);
            guidesData->setHorizontalGuideLines(lines);
            m_options->setHorizontalGuideLines(lines);
            m_index = -1;
        } else {
            lines = guidesData->verticalGuideLines();
            lines.removeAt(line.second);
            guidesData->setVerticalGuideLines(lines);
            m_options->setVerticalGuideLines(lines);
            m_index = -1;
        }
    }

    repaintDecorations();
}

void GuidesTool::createGuideLine(Qt::Orientation orientation, qreal position)
{
    m_orientation = orientation;
    m_index = -1;
    m_position = position;
    m_mode = AddGuide;

    KoToolManager::instance()->switchToolRequested(GuidesToolId);

    // grab the mouse so we get mouse events as the dragging started on a ruler
    canvas()->canvasWidget()->grabMouse();
}

void GuidesTool::moveGuideLine(Qt::Orientation orientation, int index)
{
    m_orientation = orientation;
    m_index = index;
    m_mode = MoveGuide;
}

void GuidesTool::editGuideLine(Qt::Orientation orientation, int index)
{
    m_orientation = orientation;
    m_index = index;
    m_mode = EditGuide;
}

void GuidesTool::updateGuidePosition(qreal position)
{
    if (m_mode == MoveGuide || m_mode == EditGuide) {
        KoGuidesData *guidesData = canvas()->guidesData();
        if (guidesData) {
            if (m_orientation == Qt::Horizontal) {
                QList<qreal> guideLines = guidesData->horizontalGuideLines();
                if (m_index >= 0 && m_index < guideLines.count()) {
                    guideLines[m_index] = position;
                    guidesData->setHorizontalGuideLines(guideLines);
                }
            } else {
                QList<qreal> guideLines = guidesData->verticalGuideLines();
                if (m_index >= 0 && m_index < guideLines.count()) {
                    guideLines[m_index] = position;
                    guidesData->setVerticalGuideLines(guideLines);
                }
            }
        }
    }
}

void GuidesTool::guideLineSelected(Qt::Orientation orientation, int index)
{
    KoGuidesData *guidesData = canvas()->guidesData();
    if (! guidesData)
        return;

    repaintDecorations();

    m_orientation = orientation;
    m_index = index;

    if (m_orientation == Qt::Horizontal)
        m_position = guidesData->horizontalGuideLines().value(index);
    else
        m_position = guidesData->verticalGuideLines().value(index);

    repaintDecorations();
}

void GuidesTool::guideLinesChanged(Qt::Orientation orientation)
{
    KoGuidesData *guidesData = canvas()->guidesData();
    if (! guidesData)
        return;

    repaintDecorations();

    if (orientation == Qt::Horizontal)
        guidesData->setHorizontalGuideLines(m_options->horizontalGuideLines());
    else
        guidesData->setVerticalGuideLines(m_options->verticalGuideLines());

    if (orientation == m_orientation) {
        QList<qreal> lines;
        if (m_orientation == Qt::Horizontal)
            lines = guidesData->horizontalGuideLines();
        else
            lines = guidesData->verticalGuideLines();

        int oldIndex = m_index;

        if (lines.count() == 0)
            m_index = -1;
        else if (m_index >= lines.count())
            m_index = 0;

        if (m_index >= 0)
            m_position = lines[m_index];

        if (oldIndex != m_index)
            m_options->selectGuideLine(m_orientation, m_index);
    }

    repaintDecorations();
}

GuidesTool::GuideLine GuidesTool::guideLineAtPosition(const QPointF &position)
{
    int index = -1;
    Qt::Orientation orientation = Qt::Horizontal;

    // check if we are on a guide line
    KoGuidesData *guidesData = canvas()->guidesData();
    if (guidesData && guidesData->showGuideLines()) {
        qreal minDistance = canvas()->viewConverter()->viewToDocumentX(handleRadius());
        int i = 0;
        foreach (qreal guidePos, guidesData->horizontalGuideLines()) {
            qreal distance = qAbs(guidePos - position.y());
            if (distance < minDistance) {
                orientation = Qt::Horizontal;
                index = i;
                minDistance = distance;
            }
            i++;
        }
        i = 0;
        foreach (qreal guidePos, guidesData->verticalGuideLines()) {
            qreal distance = qAbs(guidePos - position.x());
            if (distance < minDistance) {
                orientation = Qt::Vertical;
                index = i;
                minDistance = distance;
            }
            i++;
        }
    }

    return QPair<Qt::Orientation,int>(orientation, index);
}

void GuidesTool::canvasResourceChanged(int key, const QVariant &res)
{
    if (key == KoCanvasResourceManager::Unit) {
        if (m_options)
            m_options->setUnit(res.value<KoUnit>());
    }
}

QList<QPointer<QWidget> > GuidesTool::createOptionWidgets()
{
    QList<QPointer<QWidget> > optionwidgets;
    m_options = new GuidesToolOptionWidget();
    m_options->setWindowTitle(i18n("Guides Editor"));
    connect(m_options, SIGNAL(guideLineSelected(Qt::Orientation,int)),
            this, SLOT(guideLineSelected(Qt::Orientation,int)));
    connect(m_options, SIGNAL(guideLinesChanged(Qt::Orientation)),
            this, SLOT(guideLinesChanged(Qt::Orientation)));
    optionwidgets.append(m_options);

    m_insert = new InsertGuidesToolOptionWidget();
    m_insert->setWindowTitle(i18n("Guides Insertor"));
    connect(m_insert, SIGNAL(createGuides(GuidesTransaction*)),
             this, SLOT(insertorCreateGuidesSlot(GuidesTransaction*)));
    optionwidgets.append(m_insert);

    return optionwidgets;
}

void GuidesTool::insertorCreateGuidesSlot(GuidesTransaction *result)
{
    KoGuidesData *guidesData = canvas()->guidesData();
    const QSizeF pageSize = canvas()->resourceManager()->sizeResource(KoCanvasResourceManager::PageSize);

    QList< qreal > verticalLines;
    QList< qreal > horizontalLines;
    //save previous lines if requested
    if (!result->erasePreviousGuides) {
        verticalLines.append(guidesData->verticalGuideLines());
        horizontalLines.append(guidesData->horizontalGuideLines());
    } else {
        // trigger repaint at old guide positions
        foreach(qreal pos, guidesData->verticalGuideLines()) {
            canvas()->updateCanvas(updateRectFromGuideLine(pos, Qt::Vertical));
        }
        foreach(qreal pos, guidesData->horizontalGuideLines()) {
            canvas()->updateCanvas(updateRectFromGuideLine(pos, Qt::Horizontal));
        }
    }

    //vertical guides
    if (result->insertVerticalEdgesGuides) {
        verticalLines << 0 << pageSize.width();
    }

    int lastI = result->verticalGuides;
    qreal verticalJumps = pageSize.width() / (qreal)(result->verticalGuides + 1);
    for (int i = 1 ; i <= lastI; ++i) {
        verticalLines << verticalJumps * (qreal)i;
    }
    guidesData->setVerticalGuideLines(verticalLines);

    //horizontal guides
    lastI = result->horizontalGuides;
    if (result->insertHorizontalEdgesGuides) {
        horizontalLines << 0 << pageSize.height();
    }

    qreal horizontalJumps = pageSize.height() / (qreal)(result->horizontalGuides + 1);
    for (int i = 1 ; i <= lastI; ++i) {
        horizontalLines << horizontalJumps * (qreal)i;
    }
    guidesData->setHorizontalGuideLines(horizontalLines);

    // trigger repaint at new guide positions
    foreach(qreal pos, guidesData->verticalGuideLines()) {
        canvas()->updateCanvas(updateRectFromGuideLine(pos, Qt::Vertical));
    }
    foreach(qreal pos, guidesData->horizontalGuideLines()) {
        canvas()->updateCanvas(updateRectFromGuideLine(pos, Qt::Horizontal));
    }

    m_orientation = m_options->orientation();
    m_index = m_orientation == Qt::Horizontal ? horizontalLines.count()-1 : verticalLines.count()-1;
    m_options->setHorizontalGuideLines(horizontalLines);
    m_options->setVerticalGuideLines(verticalLines);
    m_options->selectGuideLine(m_orientation, m_index);

    delete result;
}