TextSectionHandler.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. #
  18. import uno
  19. import traceback
  20. class TextSectionHandler(object):
  21. '''Creates a new instance of TextSectionHandler'''
  22. def __init__(self, xMSF, xTextDocument):
  23. self.xMSFDoc = xMSF
  24. self.xTextDocument = xTextDocument
  25. self.xText = xTextDocument.Text
  26. def removeTextSectionbyName(self, SectionName):
  27. try:
  28. xAllTextSections = self.xTextDocument.TextSections
  29. if xAllTextSections.hasByName(SectionName):
  30. oTextSection = self.xTextDocument.TextSections.getByName(
  31. SectionName)
  32. self.removeTextSection(oTextSection)
  33. except Exception:
  34. traceback.print_exc()
  35. def hasTextSectionByName(self, SectionName):
  36. xAllTextSections = self.xTextDocument.TextSections
  37. return xAllTextSections.hasByName(SectionName)
  38. def removeTextSection(self, _oTextSection):
  39. try:
  40. self.xText.removeTextContent(_oTextSection)
  41. except Exception:
  42. traceback.print_exc()
  43. def removeAllTextSections(self):
  44. try:
  45. TextSectionCount = self.xTextDocument.TextSections.Count
  46. xAllTextSections = self.xTextDocument.TextSections
  47. for i in range(TextSectionCount - 1, -1, -1):
  48. xTextContentTextSection = xAllTextSections.getByIndex(i)
  49. self.xText.removeTextContent(xTextContentTextSection)
  50. except Exception:
  51. traceback.print_exc()
  52. def breakLinkOfTextSection(self, oTextSection):
  53. try:
  54. oSectionLink = \
  55. uno.createUnoStruct('com.sun.star.text.SectionFileLink')
  56. oSectionLink.FileURL = ""
  57. uno.invoke(oTextSection, "setPropertyValues",
  58. (("FileLink", "LinkRegion"), (oSectionLink, "")))
  59. except Exception:
  60. traceback.print_exc()
  61. def linkSectiontoTemplate(
  62. self, TemplateName, SectionName, oTextSection=None):
  63. try:
  64. if not oTextSection:
  65. oTextSection = self.xTextDocument.TextSections.getByName(
  66. SectionName)
  67. oSectionLink = \
  68. uno.createUnoStruct('com.sun.star.text.SectionFileLink')
  69. oSectionLink.FileURL = TemplateName
  70. uno.invoke(oTextSection, "setPropertyValues",
  71. (("FileLink", "LinkRegion"), (oSectionLink, SectionName)))
  72. NewSectionName = oTextSection.Name
  73. if NewSectionName is not SectionName:
  74. oTextSection.Name = SectionName
  75. except Exception:
  76. traceback.print_exc()
  77. def insertTextSection(self, GroupName, TemplateName, _bAddParagraph):
  78. try:
  79. if _bAddParagraph:
  80. xTextCursor = self.xText.createTextCursor()
  81. self.xText.insertControlCharacter(
  82. xTextCursor, ControlCharacter.PARAGRAPH_BREAK, False)
  83. xTextCursor.collapseToEnd()
  84. xSecondTextCursor = self.xText.createTextCursor()
  85. xSecondTextCursor.gotoEnd(False)
  86. insertTextSection(GroupName, TemplateName, xSecondTextCursor)
  87. except Exception:
  88. traceback.print_exc()
  89. def insertTextSection(self, sectionName, templateName, position):
  90. try:
  91. if self.xTextDocument.TextSections.hasByName(sectionName):
  92. xTextSection = \
  93. self.xTextDocument.TextSections.getByName(sectionName)
  94. else:
  95. xTextSection = self.xMSFDoc.createInstance(
  96. "com.sun.star.text.TextSection")
  97. position.getText().insertTextContent(
  98. position, xTextSection, False)
  99. linkSectiontoTemplate(xTextSection, templateName, sectionName)
  100. except Exception:
  101. traceback.print_exc()