designer_source.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (c) 2026 Riverbank Computing Limited <info@riverbankcomputing.com>
  2. #
  3. # This file is part of PyQt6.
  4. #
  5. # This file may be used under the terms of the GNU General Public License
  6. # version 3.0 as published by the Free Software Foundation and appearing in
  7. # the file LICENSE included in the packaging of this file. Please review the
  8. # following information to ensure the GNU General Public License version 3.0
  9. # requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  10. #
  11. # If you do not wish to use this file under the terms of the GPL version 3.0
  12. # then you may purchase a commercial license. For more information contact
  13. # info@riverbankcomputing.com.
  14. #
  15. # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  16. # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. from ..uic import UIFile
  18. from .source_file import SourceFile
  19. from .translations import Context, Message
  20. from .user import User, UserException
  21. class DesignerSource(SourceFile, User):
  22. """ Encapsulate a Designer source file. """
  23. def __init__(self, **kwargs):
  24. """ Initialise the object. """
  25. super().__init__(**kwargs)
  26. # Read the source file.
  27. self.progress("Reading {0}...".format(self.filename))
  28. try:
  29. ui_file = UIFile(self.filename)
  30. except Exception as e:
  31. raise UserException(str(e))
  32. if ui_file.widget is not None:
  33. context = Context(ui_file.class_name)
  34. # Get each <string> element. Note that we don't support the
  35. # <stringlist> element which seems to provide defaults for the
  36. # attributes of any child <string> elements.
  37. for string_el in ui_file.widget.iter('string'):
  38. if string_el.get('notr', 'false') == 'true':
  39. continue
  40. # This can be None or an empty string depending on the exact
  41. # XML.
  42. if not string_el.text:
  43. continue
  44. message = Message(self.filename, 0, string_el.text,
  45. string_el.get('comment', ''), False)
  46. extra_comment = string_el.get('extracomment')
  47. if extra_comment:
  48. message.embedded_comments.extra_comments.append(
  49. extra_comment)
  50. context.messages.append(message)
  51. if context.messages:
  52. self.contexts.append(context)