LetterDocument.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. from ..text.TextDocument import TextDocument, traceback, \
  19. TextFieldHandler, Configuration
  20. from ..text.TextSectionHandler import TextSectionHandler
  21. from com.sun.star.table import BorderLine
  22. from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
  23. from com.sun.star.style.ParagraphAdjust import CENTER
  24. from com.sun.star.text.PageNumberType import CURRENT
  25. from com.sun.star.style.NumberingType import ARABIC
  26. from com.sun.star.text.HoriOrientation import NONE as NONEHORI
  27. from com.sun.star.text.VertOrientation import NONE as NONEVERT
  28. from com.sun.star.text.RelOrientation import PAGE_FRAME
  29. from com.sun.star.text.TextContentAnchorType import AT_PAGE
  30. from com.sun.star.text.SizeType import FIX
  31. from com.sun.star.text.WrapTextMode import THROUGH
  32. from com.sun.star.awt.FontWeight import BOLD
  33. from com.sun.star.beans import UnknownPropertyException
  34. class LetterDocument(TextDocument):
  35. def __init__(self, xMSF, listener):
  36. super(LetterDocument,self).__init__(xMSF, listener, None,
  37. "WIZARD_LIVE_PREVIEW")
  38. self.keepLogoFrame = True
  39. self.keepBendMarksFrame = True
  40. self.keepLetterSignsFrame = True
  41. self.keepSenderAddressRepeatedFrame = True
  42. self.keepAddressFrame = True
  43. def switchElement(self, sElement, bState):
  44. try:
  45. mySectionHandler = TextSectionHandler(
  46. self.xMSF, self.xTextDocument)
  47. oSection = \
  48. mySectionHandler.xTextDocument.TextSections.getByName(sElement)
  49. oSection.IsVisible = bState
  50. except Exception:
  51. traceback.print_exc()
  52. def updateDateFields(self):
  53. FH = TextFieldHandler(
  54. self.xTextDocument, self.xTextDocument)
  55. FH.updateDateFields()
  56. def switchFooter(self, sPageStyle, bState, bPageNumber, sText):
  57. if self.xTextDocument is not None:
  58. try:
  59. self.xTextDocument.lockControllers()
  60. xNameAccess = self.xTextDocument.StyleFamilies
  61. xPageStyleCollection = xNameAccess.getByName("PageStyles")
  62. xPageStyle = xPageStyleCollection.getByName(sPageStyle)
  63. if bState:
  64. xPageStyle.FooterIsOn = True
  65. xFooterText = xPageStyle.FooterText
  66. xFooterText.String = sText
  67. if bPageNumber:
  68. #Adding the Page Number
  69. myCursor = xFooterText.Text.createTextCursor()
  70. myCursor.gotoEnd(False)
  71. xFooterText.insertControlCharacter(myCursor,
  72. PARAGRAPH_BREAK, False)
  73. myCursor.setPropertyValue("ParaAdjust", CENTER )
  74. xPageNumberField = \
  75. self.xTextDocument.createInstance(
  76. "com.sun.star.text.TextField.PageNumber")
  77. xPageNumberField.setPropertyValue("SubType", CURRENT)
  78. xPageNumberField.NumberingType = ARABIC
  79. xFooterText.insertTextContent(xFooterText.End,
  80. xPageNumberField, False)
  81. else:
  82. xPageStyle.FooterIsOn = False
  83. self.xTextDocument.unlockControllers()
  84. except Exception:
  85. traceback.print_exc()
  86. def hasElement(self, sElement):
  87. if self.xTextDocument is not None:
  88. SH = TextSectionHandler(self.xMSF, self.xTextDocument)
  89. return SH.hasTextSectionByName(sElement)
  90. else:
  91. return False
  92. def switchUserField(self, sFieldName, sNewContent, bState):
  93. myFieldHandler = TextFieldHandler(
  94. self.xMSF, self.xTextDocument)
  95. if bState:
  96. myFieldHandler.changeUserFieldContent(sFieldName, sNewContent)
  97. else:
  98. myFieldHandler.changeUserFieldContent(sFieldName, "")
  99. def fillSenderWithUserData(self):
  100. try:
  101. myFieldHandler = TextFieldHandler(
  102. self.xTextDocument, self.xTextDocument)
  103. oUserDataAccess = Configuration.getConfigurationRoot(
  104. self.xMSF, "org.openoffice.UserProfile/Data", False)
  105. myFieldHandler.changeUserFieldContent(
  106. "Company", oUserDataAccess.getByName("o"))
  107. myFieldHandler.changeUserFieldContent(
  108. "Street", oUserDataAccess.getByName("street"))
  109. myFieldHandler.changeUserFieldContent(
  110. "PostCode", oUserDataAccess.getByName("postalcode"))
  111. myFieldHandler.changeUserFieldContent(
  112. "City", oUserDataAccess.getByName("l"))
  113. myFieldHandler.changeUserFieldContent(
  114. "State", oUserDataAccess.getByName("st"))
  115. except Exception:
  116. traceback.print_exc()
  117. def killEmptyUserFields(self):
  118. myFieldHandler = TextFieldHandler(
  119. self.xMSF, self.xTextDocument)
  120. myFieldHandler.removeUserFieldByContent()
  121. def killEmptyFrames(self):
  122. try:
  123. if not self.keepLogoFrame:
  124. xTF = self.getFrameByName(
  125. "Company Logo", self.xTextDocument)
  126. if xTF is not None:
  127. xTF.dispose()
  128. if not self.keepBendMarksFrame:
  129. xTF = self.getFrameByName(
  130. "Bend Marks", self.xTextDocument)
  131. if xTF is not None:
  132. xTF.dispose()
  133. if not self.keepLetterSignsFrame:
  134. xTF = self.getFrameByName(
  135. "Letter Signs", self.xTextDocument)
  136. if xTF is not None:
  137. xTF.dispose()
  138. if not self.keepSenderAddressRepeatedFrame:
  139. xTF = self.getFrameByName(
  140. "Sender Address Repeated", self.xTextDocument)
  141. if xTF is not None:
  142. xTF.dispose()
  143. if not self.keepAddressFrame:
  144. xTF = self.getFrameByName(
  145. "Sender Address", self.xTextDocument)
  146. if xTF is not None:
  147. xTF.dispose()
  148. except Exception:
  149. traceback.print_exc()
  150. class BusinessPaperObject(object):
  151. def __init__(self, xTextDocument, FrameText, Width, Height, XPos, YPos):
  152. self.xTextDocument = xTextDocument
  153. self.iWidth = Width
  154. self.iHeight = Height
  155. self.iXPos = XPos
  156. self.iYPos = YPos
  157. self.xFrame = None
  158. try:
  159. self.xFrame = \
  160. self.xTextDocument.createInstance(
  161. "com.sun.star.text.TextFrame")
  162. self.setFramePosition()
  163. self.xFrame.AnchorType = AT_PAGE
  164. self.xFrame.SizeType = FIX
  165. self.xFrame.TextWrap = THROUGH
  166. self.xFrame.Opaque = True
  167. self.xFrame.BackColor = 15790320
  168. myBorder = BorderLine()
  169. myBorder.OuterLineWidth = 0
  170. self.xFrame.LeftBorder = myBorder
  171. self.xFrame.RightBorder = myBorder
  172. self.xFrame.TopBorder = myBorder
  173. self.xFrame.BottomBorder = myBorder
  174. self.xFrame.Print = False
  175. xTextCursor = \
  176. self.xTextDocument.Text.createTextCursor()
  177. xTextCursor.gotoEnd(True)
  178. xText = self.xTextDocument.Text
  179. xText.insertTextContent(
  180. xTextCursor, self.xFrame,
  181. False)
  182. xFrameText = self.xFrame.Text
  183. xFrameCursor = xFrameText.createTextCursor()
  184. xFrameCursor.setPropertyValue("CharWeight", BOLD)
  185. xFrameCursor.setPropertyValue("CharColor", 16777215)
  186. xFrameCursor.setPropertyValue("CharFontName", "Albany")
  187. xFrameCursor.setPropertyValue("CharHeight", 18)
  188. xFrameText.insertString(xFrameCursor, FrameText, False)
  189. except Exception:
  190. traceback.print_exc()
  191. def setFramePosition(self):
  192. try:
  193. self.xFrame.HoriOrient = NONEHORI
  194. self.xFrame.VertOrient = NONEVERT
  195. self.xFrame.Height = self.iHeight
  196. self.xFrame.Width = self.iWidth
  197. self.xFrame.HoriOrientPosition = self.iXPos
  198. self.xFrame.VertOrientPosition = self.iYPos
  199. self.xFrame.HoriOrientRelation = PAGE_FRAME
  200. self.xFrame.VertOrientRelation = PAGE_FRAME
  201. except Exception:
  202. traceback.print_exc()
  203. def removeFrame(self):
  204. if self.xFrame is not None:
  205. try:
  206. self.xTextDocument.Text.removeTextContent(
  207. self.xFrame)
  208. except UnknownPropertyException:
  209. pass
  210. except Exception:
  211. traceback.print_exc()