ListModelBinder.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 abc import abstractmethod
  19. from .ListDataListener import ListDataListener
  20. class ListModelBinder(ListDataListener):
  21. def __init__(self, unoListBox, listModel_):
  22. self.unoList = unoListBox
  23. self.unoListModel = unoListBox.Model
  24. self.listModel = None
  25. self.setListModel(listModel_)
  26. self.renderer = self.Renderer()
  27. def setListModel(self, newListModel):
  28. if self.listModel is not None:
  29. self.listModel.removeListDataListener(self)
  30. self.listModel = newListModel
  31. self.listModel.addListDataListener(self)
  32. def update(self, i):
  33. self.remove(i, i)
  34. self.insert(i)
  35. def remove(self, i1, i2):
  36. self.unoList.removeItems(i1, i2 - i1 + 1)
  37. def insert(self, i):
  38. self.unoList.addItem(self.getItemString(i), i)
  39. def getItemString(self, i):
  40. return self.getItemString1(self.listModel.getElementAt(i))
  41. def getItemString1(self, item):
  42. return self.renderer.render(item)
  43. def getSelectedItems(self):
  44. return self.unoListModel.SelectedItems
  45. class Renderer:
  46. @abstractmethod
  47. def render(self, item):
  48. if (item is None):
  49. return ""
  50. elif (isinstance(item, int)):
  51. return str(item)
  52. else:
  53. return item.toString()