DataAware.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 traceback
  19. import uno
  20. from abc import ABCMeta, abstractmethod
  21. from com.sun.star.util import Date
  22. from com.sun.star.util import Time
  23. from datetime import datetime
  24. '''
  25. DataAware objects are used to live-synchronize UI and DataModel/DataObject.
  26. It is used as listener on UI events, to keep the DataObject up to date.
  27. This class, as a base abstract class, sets a frame of functionality,
  28. delegating the data Object get/set methods to a Value object,
  29. and leaving the UI get/set methods abstract.
  30. Note that event listening is *not* a part of this model.
  31. the updateData() or updateUI() methods should be programmatically called.
  32. in child classes, the updateData() will be bound to UI event calls.
  33. <br><br>
  34. This class holds references to a Data Object and a Value object.
  35. The Value object "knows" how to get and set a value from the
  36. Data Object.
  37. '''
  38. class DataAware(object):
  39. __metaclass__ = ABCMeta
  40. '''
  41. creates a DataAware object for the given data object and Value object.
  42. @param dataObject_
  43. @param value_
  44. '''
  45. def __init__(self, dataObject_, field_):
  46. self._dataObject = dataObject_
  47. self._field = field_
  48. '''
  49. sets the given value to the UI control
  50. @param newValue the value to set to the ui control.
  51. '''
  52. @abstractmethod
  53. def setToUI (self,newValue):
  54. pass
  55. '''
  56. gets the current value from the UI control.
  57. @return the current value from the UI control.
  58. '''
  59. @abstractmethod
  60. def getFromUI (self):
  61. pass
  62. '''
  63. updates the UI control according to the
  64. current state of the data object.
  65. '''
  66. def updateUI(self):
  67. try:
  68. data = getattr(self._dataObject, self._field)
  69. except Exception:
  70. data = uno.invoke(self._dataObject, "get" + self._field, ())
  71. ui = self.getFromUI()
  72. if data is not ui:
  73. try:
  74. self.setToUI(data)
  75. except Exception:
  76. traceback.print_exc()
  77. '''
  78. updates the DataObject according to
  79. the current state of the UI control.
  80. '''
  81. def updateData(self):
  82. useUno = False
  83. try:
  84. try:
  85. data = getattr(self._dataObject, self._field)
  86. except Exception:
  87. useUno = True
  88. data = uno.invoke(self._dataObject, "get" + self._field, ())
  89. ui = self.getFromUI()
  90. if data is not ui:
  91. if isinstance(ui,Date):
  92. d = datetime(ui.Year, ui.Month, ui.Day)
  93. ui = d.strftime('%d/%m/%y')
  94. elif isinstance(ui,Time):
  95. t = datetime(1, 1, 1, ui.Hours, ui.Minutes)
  96. ui = t.strftime('%H:%M')
  97. if useUno:
  98. uno.invoke(self._dataObject, "set" + self._field, (ui,))
  99. else:
  100. if isinstance(ui,tuple):
  101. #Listbox Element
  102. ui = ui[0]
  103. setattr(self._dataObject, self._field, ui)
  104. except Exception:
  105. traceback.print_exc()