UnoDataAware.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. from .CommonListener import ItemListenerProcAdapter, TextListenerProcAdapter
  20. from .DataAware import DataAware, datetime, Date, Time
  21. from ...common.PropertyNames import PropertyNames
  22. '''
  23. This class supports simple cases where a UI control can
  24. be directly synchronized with a data property.
  25. Such controls are: the different text controls
  26. (synchronizing the "Text" , "Value", "Date", "Time" property),
  27. Checkbox controls, Dropdown listbox controls (synchronizing the
  28. SelectedItems[] property.
  29. For those controls, static convenience methods are offered, to simplify use.
  30. '''
  31. class UnoDataAware(DataAware):
  32. def __init__(self, dataObject, field, unoObject_, unoPropName_, isShort=False):
  33. super(UnoDataAware,self).__init__(dataObject, field)
  34. self.unoControl = unoObject_
  35. self.unoModel = self.unoControl.Model
  36. self.unoPropName = unoPropName_
  37. self.isShort = isShort
  38. def setToUI(self, value):
  39. if (isinstance(value, list)):
  40. value = tuple(value)
  41. elif self.isShort:
  42. value = uno.Any("[]short", (value,))
  43. if value:
  44. if(hasattr(self.unoModel, self.unoPropName)):
  45. if self.unoPropName == "Date":
  46. d = datetime.strptime(value, '%d/%m/%y')
  47. value = Date(d.day, d.month, d.year)
  48. elif self.unoPropName == "Time":
  49. t = datetime.strptime(value, '%H:%M')
  50. value = Time(0, 0, t.minute, t.hour, False)
  51. setattr(self.unoModel, self.unoPropName, value)
  52. else:
  53. uno.invoke(self.unoModel, "set" + self.unoPropName, (value,))
  54. def getFromUI(self):
  55. return getattr(self.unoModel, self.unoPropName)
  56. @classmethod
  57. def __attachTextControl(
  58. self, data, prop, unoText, unoProperty, field, value):
  59. uda = UnoDataAware(data, prop, unoText, unoProperty)
  60. method = getattr(uda,"updateData")
  61. unoText.addTextListener(TextListenerProcAdapter(method))
  62. return uda
  63. @classmethod
  64. def attachEditControl(self, data, prop, unoControl, field):
  65. return self.__attachTextControl(
  66. data, prop, unoControl, "Text", field, "")
  67. @classmethod
  68. def attachDateControl(self, data, prop, unoControl, field):
  69. return self.__attachTextControl(
  70. data, prop, unoControl, "Date", field, 0)
  71. @classmethod
  72. def attachTimeControl(self, data, prop, unoControl, field):
  73. return self.__attachTextControl(
  74. data, prop, unoControl, "Time", field, 0)
  75. @classmethod
  76. def attachNumericControl(self, data, prop, unoControl, field):
  77. return self.__attachTextControl(
  78. data, prop, unoControl, "Value", field, float(0))
  79. @classmethod
  80. def attachCheckBox(
  81. self, data, prop, checkBox, field):
  82. uda = UnoDataAware(data, prop, checkBox, PropertyNames.PROPERTY_STATE)
  83. method = getattr(uda,"updateData")
  84. checkBox.addItemListener(ItemListenerProcAdapter(method))
  85. return uda
  86. @classmethod
  87. def attachListBox(self, data, prop, listBox, field):
  88. uda = UnoDataAware(data, prop, listBox, "SelectedItems", True)
  89. method = getattr(uda,"updateData")
  90. listBox.addItemListener(ItemListenerProcAdapter(method))
  91. return uda