File: GuidesTool.cpp

package info (click to toggle)
koffice 1%3A2.2.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 157,656 kB
  • ctags: 110,762
  • sloc: cpp: 889,358; xml: 22,758; ansic: 6,533; python: 5,224; perl: 2,771; sh: 1,805; yacc: 1,304; ruby: 1,219; sql: 720; lex: 455; makefile: 76
file content (444 lines) | stat: -rw-r--r-- 14,401 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
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
/* This file is part of the KDE project
 * Copyright (C) 2008 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 <KoResourceManager.h>
#include <KoViewConverter.h>
#include <KoGuidesData.h>
#include <KoToolManager.h>

#include <KDebug>

#include <QtGui/QPainter>

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

GuidesTool::~GuidesTool()
{
}

void GuidesTool::paint(QPainter &painter, const KoViewConverter &converter)
{
    if (m_mode == None)
        return;

    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(Qt::red);
    painter.drawLine(start, end);
}

void GuidesTool::repaintDecorations()
{
    if (m_mode == None)
        return;

    QRectF rect;
    KoCanvasController *controller = canvas()->canvasController();
    QPoint documentOrigin = canvas()->documentOrigin();
    QPoint canvasOffset(controller->canvasOffsetX(), controller->canvasOffsetY());
    if (m_orientation == Qt::Horizontal) {
        qreal pixelBorder = canvas()->viewConverter()->viewToDocumentY(2.0);
        rect.setTop(m_position - pixelBorder);
        rect.setBottom(m_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(m_position - pixelBorder);
        rect.setRight(m_position + pixelBorder);
        rect.setTop(canvas()->viewConverter()->viewToDocumentY(-canvasOffset.y()-documentOrigin.y()));
        rect.setHeight(canvas()->viewConverter()->viewToDocumentY(canvas()->canvasWidget()->height()));
    }
    canvas()->updateCanvas(rect);
}

void GuidesTool::activate(ToolActivation toolActivation, const QSet<KoShape*> &)
{
    if (m_mode != None)
        useCursor(m_orientation == Qt::Horizontal ? Qt::SizeVerCursor : Qt::SizeHorCursor);
    else
        useCursor(Qt::ArrowCursor);
    if (toolActivation == KoToolBase::TemporaryActivation)
        canvas()->canvasWidget()->grabMouse();

    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 = None;
}

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 == None) {
        useCursor(Qt::ArrowCursor);
        return;
    }

    if (m_mode == EditGuide && ! m_isMoving) {
        GuideLine line = guideLineAtPosition(event->point);
        if (line.second < 0)
            useCursor(Qt::ArrowCursor);
        else
            useCursor(line.first == Qt::Horizontal ? Qt::SizeVerCursor : Qt::SizeHorCursor);
    } else {
        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::startGuideLineCreation(Qt::Orientation orientation, qreal position)
{
    m_orientation = orientation;
    m_index = -1;
    m_position = position;
    m_mode = AddGuide;

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

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();
                guideLines[m_index] = position;
                guidesData->setHorizontalGuideLines(guideLines);
            } else {
                QList<qreal> guideLines = guidesData->verticalGuideLines();
                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()[index];
    else
        m_position = guidesData->verticalGuideLines()[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 handleRadius = canvas()->resourceManager()->handleRadius();
        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::resourceChanged(int key, const QVariant &res)
{
    if (key == KoCanvasResource::Unit) {
        if (m_options)
            m_options->setUnit(res.value<KoUnit>());
    }
}

QMap< QString, QWidget*> GuidesTool::createOptionWidgets()
{
    QMap< QString, QWidget* > optionWidgets;
    if (m_mode != EditGuide) {
        m_options = new GuidesToolOptionWidget();

        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.insert("Guides Editor", m_options);

        m_insert = new InsertGuidesToolOptionWidget();

        connect(m_insert, SIGNAL(createGuides(GuidesTransaction*)),
                 this, SLOT(insertorCreateGuidesSlot(GuidesTransaction*)));

        optionWidgets.insert("Guides Insertor", m_insert);
    }
    return optionWidgets;
}

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

    QList< qreal > verticalLines;
    QList< qreal > horizontalLines;
    //save previous lines if requested
    if (!result->erasePreviousGuides) {
        verticalLines.append(guidesData->verticalGuideLines());
        horizontalLines.append(guidesData->horizontalGuideLines());
    }

    //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);

    delete result;
}

#include <GuidesTool.moc>