File: ChartResizeStrategy.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 (142 lines) | stat: -rw-r--r-- 5,435 bytes parent folder | download | duplicates (2)
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
/* This file is part of the KDE project
 * 
 *   Copyright (C) 2017 Dag Andersen <danders@get2net.dk>
 * 
 *   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 "ChartResizeStrategy.h"

#include <KoShape.h>
#include <KoShapeContainer.h>
#include <KoCanvasBase.h>
#include <KoShapeManager.h>
#include <KoShapeContainerModel.h>
#include <commands/KoShapeMoveCommand.h>
#include <commands/KoShapeSizeCommand.h>
#include <klocalizedstring.h>


ChartResizeStrategy::ChartResizeStrategy(KoShape *shape)
    : m_chart(dynamic_cast<KoShapeContainer*>(shape))
    , m_plotArea(0)
{
    if (m_chart) {
        QRectF rect = m_chart->boundingRect();
        QPointF middle((rect.right() - rect.left()) * 0.5, (rect.bottom() - rect.top()) * 0.5);
        for (KoShape *shape : m_chart->shapes()) {
            if (!shape->isVisible()) {
                continue;
            }
            if (shape->shapeId() == "ChartShapePlotArea") {
                // Plot area shall be resized
                m_plotArea = shape;
                m_plotAreaStartSize = shape->size();
                rect = shape->boundingRect();
                middle = QPointF(rect.left() + ((rect.right() - rect.left()) * 0.5), rect.top() + ((rect.bottom() - rect.top()) * 0.5));
            } else {
                m_shapes << shape;
                m_startPositions << shape->position();
            }
        }
        // NOTE: We use plot area middle point for positioning all other shapes
        // This is correct for axis titles/legend,
        // but the others should have used the *chart* middle.
        // As we do not know which is which and it works ok in "normal" situations
        // we go for this atm.
        for (KoShape *shape : m_shapes) {
            QRectF r = shape->boundingRect();
            if (r.left() > middle.x()) {
                // to the right
                m_rightX << shape;
            } else if (r.right() >= middle.x()) {
                // overlaps
                m_overlapX << shape;
            }
            if (r.top() > middle.y()) {
                // below
                m_belowY << shape;
            } else if (r.bottom() >= middle.y()) {
                // overlaps
                m_overlapY << shape;
            }
        }
    }
}

void ChartResizeStrategy::setSize(const QSizeF &startSize, qreal scaleX, qreal scaleY)
{
    if (!m_chart) {
        return;
    }
    qreal diffX = (startSize.width() * scaleX) - startSize.width();
    qreal diffY = (startSize.height() * scaleY) - startSize.height();
    if (m_plotArea) {
        // Resize plot area the same as the chart,
        // it will leave the same amount of free space as before to be used by other shapes.
        // All other components are just moved and do not take more/less space.
        QSizeF plotAreaSize = QSizeF(m_plotAreaStartSize.width() + diffX, m_plotAreaStartSize.height() + diffY);
        m_plotArea->setSize(plotAreaSize);
    }
    for (int i = 0; i < m_shapes.count(); ++i) {
        KoShape *shape = m_shapes.at(i);
        QPointF startPos = m_startPositions.at(i);
        QPointF diff; // default: keep distance to top/left
        if (m_rightX.contains(shape)) {
            // keep distance to right edge
            diff.setX(diffX);
        } else if (m_overlapX.contains(shape)) {
            // proportional move, could be improved but works pretty well ;)
            diff.setX(diffX * 0.5);
        }
        if (m_belowY.contains(shape)) {
            // keep distance to bottom edge
            diff.setY(diffY);
        } else if (m_overlapY.contains(shape)) {
            // proportional move, could be improved but works pretty well ;)
            diff.setY(diffY * 0.5);
        }
        shape->update();
        shape->setPosition(startPos + diff);
        shape->update();
    }
}

void ChartResizeStrategy::createCommand(KUndo2Command *cmd)
{
    if (!m_chart) {
        return;
    }
    // get the shapes that has actually been moved
    QVector<QPointF> oldPositions;
    QVector<QPointF> newPositions;
    QList<KoShape*> movedShapes;
    for (int i = 0; i < m_shapes.count(); ++i) {
        KoShape *shape = m_shapes.at(i);
        QPointF pos = shape->position();
        if (pos != m_startPositions.at(i)) {
            movedShapes << shape;
            oldPositions << m_startPositions.at(i);
            newPositions << pos;
        }
    }
    if (!movedShapes.isEmpty()) {
        new KoShapeMoveCommand(movedShapes, oldPositions, newPositions, cmd);
    }
    if (m_plotArea) {
        new KoShapeSizeCommand(QList<KoShape*>()<<m_plotArea, QVector<QSizeF>()<<m_plotAreaStartSize, QVector<QSizeF>()<<m_plotArea->size(), cmd );
    }
}