summaryrefslogtreecommitdiff
path: root/odk/examples/DevelopersGuide/Charts/python/ChartHelper.py
blob: 837be22365cde1429be02f6843dbfdbf1eba4a42 (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
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# Helper for creating an OLE chart

from com.sun.star.text import VertOrientation
from com.sun.star.text import HoriOrientation


class ChartHelper(object):

    def __init__(self, container_doc):
        self._CHART_CLASS_ID = "12dcae26-281f-416f-a234-c3086127382e"
        self._container_document = container_doc

    def insert_ole_chart_in_writer(self, upper_left, extent, chart_service_name: str):
        """Inserts an OLE chart into a Writer document.

        Args:
            upper_left (Point): The upper-left corner of the chart.
            extent (Size): The size of the chart.
            chart_service_name (str): The name of the chart service to use.

        Returns:
            The ChartDocument inserted
        """
        result = None

        try:
            text_content = self._container_document.createInstance(
                "com.sun.star.text.TextEmbeddedObject"
            )

            if text_content is not None:
                text_content.setPropertyValue("CLSID", self._CHART_CLASS_ID)

                text = self._container_document.getText()
                cursor = text.createTextCursor()

                # insert embedded object in text -> object will be created
                text.insertTextContent(cursor, text_content, True)

                # set size and position
                text_content.setSize(extent)
                text_content.setPropertyValue("VertOrient", int(VertOrientation.NONE))
                text_content.setPropertyValue("HoriOrient", int(HoriOrientation.NONE))
                text_content.setPropertyValue("VertOrientPosition", upper_left.Y)
                text_content.setPropertyValue("HoriOrientPosition", upper_left.X)

                # Get chart document and set diagram
                result = text_content.getPropertyValue("Model")
                result.setDiagram(result.createInstance(chart_service_name))

        except Exception as err:
            print(f"caught exception: {err}")

        return result

    def insert_ole_chart_in_draw(self, upper_left, extent, chart_service_name):
        """Inserts an OLE chart into a Draw document.

        Args:
            upper_left (Point): The upper-left corner of the chart.
            extent (Size): The size of the chart.
            chart_service_name (str): The name of the chart service to use.

        Returns:
            The ChartDocument inserted
        """
        result = None

        try:
            # Get First page
            page = self._container_document.getDrawPages().getByIndex(0)

        except AttributeError:
            # try interface for single draw page (e.g. spreadsheet)
            page = self._container_document.getDrawPage()

        except Exception as err:
            print(f"First page not found in shape collection: {err}")

        try:
            # Create an OLE shape
            shape = self._container_document.createInstance(
                "com.sun.star.drawing.OLE2Shape"
            )

            # Insert the shape into the page
            page.add(shape)
            shape.setPosition(upper_left)
            shape.setSize(extent)

            # set the class id for charts
            shape.setPropertyValue("CLSID", self._CHART_CLASS_ID)

            # retrieve the chart document as model of the OLE shape
            result = shape.getPropertyValue("Model")

            # create a diagram via the factory and set this as new diagram
            result.setDiagram(result.createInstance(chart_service_name))

        except Exception as err:
            print(f"Couldn't change the OLE shape into a chart: {err}")

        return result

# vim: set shiftwidth=4 softtabstop=4 expandtab: