odk/Package_examples.mk | 3 odk/examples/DevelopersGuide/Charts/python/CalcHelper.py | 15 - odk/examples/DevelopersGuide/Charts/python/ChartHelper.py | 114 +++++++ odk/examples/DevelopersGuide/Charts/python/ChartInDraw.py | 177 ++++++++++++ odk/examples/DevelopersGuide/Charts/python/ChartInWriter.py | 77 +++++ odk/examples/DevelopersGuide/Charts/python/Helper.py | 13 6 files changed, 396 insertions(+), 3 deletions(-)
New commits: commit 55b0ddf119714ce1221ceb098a8329de2716c448 Author: zeph <zeph.c...@ymail.com> AuthorDate: Sun Mar 17 16:17:14 2024 -0700 Commit: Hossein <hoss...@libreoffice.org> CommitDate: Fri Apr 19 22:18:25 2024 +0200 tdf#143123 Port ChartInDraw / ChartInWriter examples to Python Ported two more SDK examples ChartInDraw and ChartInWriter from Java to Python. ChartInCalc and helper classes which contain additional helper methods were ported in a previous patch. Change-Id: Idffa0c07282dfc2c0f09ec5a9959f1db34a1d9a5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165088 Tested-by: Jenkins Tested-by: Hossein <hoss...@libreoffice.org> Reviewed-by: Hossein <hoss...@libreoffice.org> diff --git a/odk/Package_examples.mk b/odk/Package_examples.mk index 87bad187b91b..f575e70aae1e 100644 --- a/odk/Package_examples.mk +++ b/odk/Package_examples.mk @@ -49,7 +49,10 @@ $(eval $(call gb_Package_add_files_with_dir,odk_examples,$(SDKDIRNAME)/examples, DevelopersGuide/Charts/java/SelectionChangeListener.java \ DevelopersGuide/Charts/java/bullet.gif \ DevelopersGuide/Charts/python/CalcHelper.py \ + DevelopersGuide/Charts/python/ChartHelper.py \ DevelopersGuide/Charts/python/ChartInCalc.py \ + DevelopersGuide/Charts/python/ChartInDraw.py \ + DevelopersGuide/Charts/python/ChartInWriter.py \ DevelopersGuide/Charts/python/Helper.py \ DevelopersGuide/Charts/python/bullet.gif \ DevelopersGuide/Components/Addons/JobsAddon/Addons.xcu \ diff --git a/odk/examples/DevelopersGuide/Charts/python/CalcHelper.py b/odk/examples/DevelopersGuide/Charts/python/CalcHelper.py index dcbb0b8e8740..8bc427daa3fb 100644 --- a/odk/examples/DevelopersGuide/Charts/python/CalcHelper.py +++ b/odk/examples/DevelopersGuide/Charts/python/CalcHelper.py @@ -1,3 +1,12 @@ +# -*- 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/. +# + import math import random import sys @@ -140,10 +149,10 @@ class CalcHelper: try: # get the sheet to insert the chart sheet = self.get_data_sheet() - cell_range = sheet[0 : row_count - 1, 0 : column_count - 1] + cell_range = sheet[0 : row_count, 0 : column_count] factor = 2.0 * math.pi / (row_count - 1) - factor_col = column_count = 2 + factor_col = column_count + 2 sheet[0, factor_col - 1].Value = 0.2 sheet[1, factor_col - 1].String = "Change the factor above and watch the changes in the chart" @@ -187,3 +196,5 @@ class CalcHelper: except Exception as e: print(f"Couldn't initialize Spreadsheet Document: {e}", file=sys.stderr) traceback.print_exc() + +# vim: set shiftwidth=4 softtabstop=4 expandtab: diff --git a/odk/examples/DevelopersGuide/Charts/python/ChartHelper.py b/odk/examples/DevelopersGuide/Charts/python/ChartHelper.py new file mode 100644 index 000000000000..837be22365cd --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/python/ChartHelper.py @@ -0,0 +1,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: diff --git a/odk/examples/DevelopersGuide/Charts/python/ChartInDraw.py b/odk/examples/DevelopersGuide/Charts/python/ChartInDraw.py new file mode 100644 index 000000000000..df9cf534f9ed --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/python/ChartInDraw.py @@ -0,0 +1,177 @@ +# -*- 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/. +# + +import math + +import Helper +import ChartHelper + +from com.sun.star.awt import Point, Size +from com.sun.star.drawing.FillStyle import SOLID +from com.sun.star.drawing import Direction3D, HomogenMatrix, HomogenMatrixLine +from com.sun.star.chart.ChartLegendPosition import LEFT +from com.sun.star.chart.ChartSolidType import CYLINDER + + +class ChartInDraw(object): + + def __init__(self, chart_doc): + self._chart_document = chart_doc + self._diagram = chart_doc.getDiagram() + + def lock_controllers(self): + self._chart_document.lockControllers() + + def unlock_controllers(self): + self._chart_document.unlockControllers() + + def test_area(self): + area = self._chart_document.getArea() + if area is not None: + # change background color of entire chart + area.setPropertyValue("FillStyle", SOLID) + area.setPropertyValue("FillColor", int(0xEEEEEE)) + + def test_wall(self): + wall = self._diagram.getWall() + # change background color of area + wall.setPropertyValue("FillColor", int(0xCCCCCC)) + wall.setPropertyValue("FillStyle", SOLID) + + def test_title(self): + self._chart_document.setPropertyValue("HasMainTitle", True) + + title = self._chart_document.getTitle() + if title is not None: + title.setPropertyValue("String", "Bar Chart in a Draw Document") + + def test_legend(self): + legend = self._chart_document.getLegend() + legend.setPropertyValue("Alignment", LEFT) + legend.setPropertyValue("FillStyle", SOLID) + legend.setPropertyValue("FillColor", int(0xEEDDEE)) + + def test_threeD(self): + self._diagram.setPropertyValue("Dim3D", True) + self._diagram.setPropertyValue("Deep", True) + # from Chart3DBarProperties: + self._diagram.setPropertyValue("SolidType", int(CYLINDER)) + + # change floor color to Magenta6 + diagram_floor = self._diagram.getFloor() + diagram_floor.setPropertyValue("FillColor", int(0x6B2394)) + + # apply changes to get a 3d scene + self._chart_document.unlockControllers() + self._chart_document.lockControllers() + + # rotate scene to a different angle + matrix = HomogenMatrix() + lines = [ + HomogenMatrixLine(1.0, 0.0, 0.0, 0.0), + HomogenMatrixLine(0.0, 1.0, 0.0, 0.0), + HomogenMatrixLine(0.0, 0.0, 1.0, 0.0), + HomogenMatrixLine(0.0, 0.0, 0.0, 1.0), + ] + + matrix.Line1 = lines[0] + matrix.Line2 = lines[1] + matrix.Line3 = lines[2] + matrix.Line4 = lines[3] + + # rotate 10 degrees along the x axis + angle = 10.0 + cos_x = math.cos(math.pi / 180.0 * angle) + sin_x = math.sin(math.pi / 180.0 * angle) + + # rotate -20 degrees along the y axis + angle = -20.0 + cos_y = math.cos(math.pi / 180.0 * angle) + sin_y = math.sin(math.pi / 180.0 * angle) + + # rotate -5 degrees along the z axis + angle = -5.0 + cos_z = math.cos(math.pi / 180.0 * angle) + sin_z = math.sin(math.pi / 180.0 * angle) + + line1 = HomogenMatrixLine(cos_y * cos_z, cos_y * -sin_z, sin_y, 0.0) + line2 = HomogenMatrixLine( + sin_x * sin_y * cos_z + cos_x * sin_z, + -sin_x * sin_y * sin_z + cos_x * cos_z, + -sin_x * cos_y, + 0.0, + ) + line3 = HomogenMatrixLine( + -cos_x * sin_y * cos_z + sin_x * sin_z, + cos_x * sin_y * sin_z + sin_x * cos_z, + cos_x * cos_y, + 0.0, + ) + matrix.Line1 = line1 + matrix.Line2 = line2 + matrix.Line3 = line3 + self._diagram.setPropertyValue("D3DTransformMatrix", matrix) + + # add a red light source + + # in a chart by default only the second (non-specular) light source is switched on + # light source 1 is a specular light source + self._diagram.setPropertyValue("D3DSceneLightColor1", int(0xFF3333)) + + # set direction + direction = Direction3D() + direction.DirectionX = -0.75 + direction.DirectionY = 0.5 + direction.DirectionZ = 0.5 + + self._diagram.setPropertyValue("D3DSceneLightDirection1", direction) + self._diagram.setPropertyValue("D3DSceneLightOn1", True) + + +# Create a spreadsheet add some data and add a chart +def main(): + helper = Helper.Helper() + chart_helper = ChartHelper.ChartHelper(helper.create_drawing_document()) + + # the unit for measures is 1/100th of a millimeter + # position at (1cm, 1cm) + pos = Point(1000, 1000) + + # size of the chart is 15cm x 13cm + extent = Size(15000, 13000) + + # insert a new chart into the "Chart" sheet of the + # spreadsheet document + chart_doc = chart_helper.insert_ole_chart_in_draw( + pos, extent, "com.sun.star.chart.BarDiagram" + ) + + # instantiate test class with newly created chart + test = ChartInDraw(chart_doc) + + try: + test.lock_controllers() + + test.test_area() + test.test_wall() + test.test_title() + test.test_legend() + test.test_threeD() + + test.unlock_controllers() + + except Exception as err: + print(f"UNO Exception caught: {err}") + + +# Main entry point +if __name__ == "__main__": + main() + +# vim: set shiftwidth=4 softtabstop=4 expandtab: diff --git a/odk/examples/DevelopersGuide/Charts/python/ChartInWriter.py b/odk/examples/DevelopersGuide/Charts/python/ChartInWriter.py new file mode 100644 index 000000000000..7ab64b6073a8 --- /dev/null +++ b/odk/examples/DevelopersGuide/Charts/python/ChartInWriter.py @@ -0,0 +1,77 @@ +# -*- 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/. +# + +import Helper +import ChartHelper + +from com.sun.star.awt import Point, Size +from com.sun.star.drawing.FillStyle import SOLID + + +class ChartInWriter(object): + def __init__(self, chart_doc): + self._chart_document = chart_doc + self._diagram = chart_doc.getDiagram() + + def lock_controllers(self): + self._chart_document.lockControllers() + + def unlock_controllers(self): + self._chart_document.unlockControllers() + + def test_wall(self): + wall = self._diagram.getWall() + # change background color of area + wall.setPropertyValue("FillColor", int(0xEECC99)) + wall.setPropertyValue("FillStyle", SOLID) + + +# Test to create a writer document and insert an OLE Chart. +# Be careful! This does not really work. The Writer currently has no +# interface for dealing with OLE objects. You can add an OLE shape to the +# Writer's drawing layer, but it is not treated correctly as OLE object. +# Thus, you can not activate the chart by double-clicking. The office may +# also crash when the document is closed! +def main(): + helper = Helper.Helper() + chart_helper = ChartHelper.ChartHelper(helper.create_text_document()) + + # the unit for measures is 1/100th of a millimeter + # position at (1cm, 1cm) + pos = Point(1000, 1000) + + # size of the chart is 15cm x 13cm + extent = Size(15000, 13000) + + # insert a new chart into the "Chart" sheet of the + # spreadsheet document + chart_doc = chart_helper.insert_ole_chart_in_writer( + pos, extent, "com.sun.star.chart.AreaDiagram" + ) + + # instantiate test class with newly created chart + test = ChartInWriter(chart_doc) + + try: + test.lock_controllers() + + # do tests here + test.test_wall() + + test.unlock_controllers() + + except Exception as err: + print(f"UNO Exception caught: {err}") + + +# Main entry point +if __name__ == "__main__": + main() + +# vim: set shiftwidth=4 softtabstop=4 expandtab: diff --git a/odk/examples/DevelopersGuide/Charts/python/Helper.py b/odk/examples/DevelopersGuide/Charts/python/Helper.py index f3d2c154ba23..a3512cb8baa2 100644 --- a/odk/examples/DevelopersGuide/Charts/python/Helper.py +++ b/odk/examples/DevelopersGuide/Charts/python/Helper.py @@ -1,3 +1,12 @@ +# -*- 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/. +# + import sys import traceback @@ -40,5 +49,7 @@ class Helper(object): return result - def get_componentContext(self): + def get_component_context(self): return self._context + +# vim: set shiftwidth=4 softtabstop=4 expandtab: