project_widget.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2015 Karkus476 <karkus476@yahoo.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from typing import Any, Callable, Optional, Union
  17. from PyQt5.QtCore import Qt, QPoint
  18. from PyQt5.QtGui import QIcon, QStandardItem, QStandardItemModel
  19. from PyQt5.QtWidgets import (
  20. QFileSystemModel,
  21. QFormLayout,
  22. QLabel,
  23. QMenu,
  24. QToolBar,
  25. QTreeView,
  26. QVBoxLayout,
  27. QWidget,
  28. )
  29. from flexlay.gui.properties_widget import Item
  30. from flexlay.util.signal import Signal
  31. from supertux.addon import Addon
  32. class ProjectWidget(QWidget):
  33. """
  34. A widget for displaying & editing properties of objects etc.
  35. Also see the properties this likes to display:
  36. also see: supertux/property.py
  37. """
  38. def __init__(self, parent: QWidget) -> None:
  39. super().__init__(parent)
  40. self.items: list[Item] = []
  41. self.addon: Optional[Addon] = None
  42. self.vbox = QVBoxLayout()
  43. self.vbox.setSpacing(0)
  44. self.vbox.setContentsMargins(0, 0, 0, 0)
  45. self.heading_label = QLabel("No project")
  46. self.label = QLabel("Create a project by selecting File > New > Project...")
  47. self.vbox.addWidget(self.heading_label)
  48. self.vbox.addWidget(self.label)
  49. self.setLayout(self.vbox)
  50. def init_gui(self) -> None:
  51. # Clear from previous:
  52. self.heading_label.setVisible(False)
  53. self.label.setVisible(False)
  54. self.toolbar = QToolBar()
  55. self.toolbar.setStyleSheet('QToolBar{spacing:0px;}')
  56. package_icon = QIcon("data/images/icons16/addon_package-16.png")
  57. add_icon = QIcon("data/images/supertux/plus.png")
  58. self.toolbar.addAction(package_icon, 'Package add-on...', self.package_addon)
  59. self.toolbar.addAction(add_icon, "Add content...", self.add_content)
  60. self.tree_view = QTreeView()
  61. self.vbox.addWidget(self.toolbar)
  62. self.model = QFileSystemModel()
  63. # self.data = [
  64. # ("SuperTux addon", [
  65. # ("levels", []),
  66. # ("images", []),
  67. # ("sounds", []),
  68. # ("music", []),
  69. # ("scripts", []),
  70. # ("metadata", [])
  71. # ])]
  72. # self.model = QStandardItemModel()
  73. # self.add_items(self.model, self.data)
  74. self.tree_view.setModel(self.model)
  75. self.tree_view.doubleClicked.connect(self.on_tree_view_double_click)
  76. self.tree_view.setContextMenuPolicy(Qt.CustomContextMenu)
  77. self.tree_view.customContextMenuRequested.connect(self.on_context_menu)
  78. self.vbox.addWidget(self.tree_view)
  79. self._layout = QFormLayout()
  80. self.vbox.addLayout(self._layout)
  81. self.setLayout(self.vbox)
  82. self.setMinimumWidth(300)
  83. # Called in many cases. This should have functions connected
  84. # which cause the changes in the widget to be applied
  85. # Called by hitting "Apply", "Ok" or "Finish"
  86. self.call_signal = Signal()
  87. self.call_signal.connect(self.call_callbacks)
  88. def call_callbacks(self) -> None:
  89. for item in self.items:
  90. if item.callback is not None:
  91. item.callback(item.get_value())
  92. def add_callback(self, callback: Callable[[Any], None]) -> None:
  93. """Adds a callback to the callback signal"""
  94. self.call_signal.connect(callback)
  95. def add_items(self, parent: Union[QStandardItem, QStandardItemModel], elements: list[tuple[str, Any]]) -> None:
  96. for text, children in elements:
  97. item = QStandardItem(text)
  98. parent.appendRow(item)
  99. if children:
  100. self.add_items(item, children)
  101. def call(self) -> None:
  102. self.call_signal()
  103. def on_tree_view_double_click(self, item: Any) -> None:
  104. print("double-clicked!")
  105. def on_context_menu(self, position: QPoint) -> None:
  106. menu = QMenu()
  107. menu.addAction(self.tr("Add image..."))
  108. menu.addAction(self.tr("Add sound..."))
  109. menu.addAction(self.tr("Add level..."))
  110. menu.addAction(self.tr("Add script..."))
  111. menu.exec_(self.tree_view.viewport().mapToGlobal(position))
  112. def set_project_directory(self, project_dir: str) -> None:
  113. self.tree_view.setRootIndex(self.model.setRootPath(project_dir))
  114. def set_addon(self, addon: Addon) -> None:
  115. self.addon = addon
  116. # We now have an add-on set, initialize the GUI
  117. self.init_gui()
  118. def package_addon(self) -> None:
  119. print("Package add-on!")
  120. def add_content(self) -> None:
  121. print("Add content to add-on!")
  122. # EOF #