Use QTextBrowser to show html text and images for boards and presets.
[bertos.git] / wizard / BBoardPage.py
index 35fc9bc467050a892ecc1c8b77805c23e055dc38..fd6fc9292e7a51afe8fe83fe9ec97fe813eb021a 100644 (file)
@@ -41,12 +41,10 @@ from PyQt4.QtGui import *
 from BWizardPage import BWizardPage
 
 from BCpuPage import BCpuPage
-from BOutputPage import BOutputPage
-from BRoutePage import BRoutePage
 
 import const
 import qvariant_converter
-from bertos_utils import presetList
+from bertos_utils import presetList, _cmp
 
 class BBoardPage(BWizardPage):
     """
@@ -57,7 +55,6 @@ class BBoardPage(BWizardPage):
     def __init__(self):
         BWizardPage.__init__(self, const.UI_LOCATION + "/board_select.ui")
         self.setTitle(self.tr("Select the board from the predefined ones"))
-        self._last_selected = None
 
     ## Overloaded QWizardPage methods ##
 
@@ -66,17 +63,21 @@ class BBoardPage(BWizardPage):
         Overload of the QWizardPage isComplete method.
         """
         if self.selected:
-            _type = qvariant_converter.getDict(self.selected.data(0, Qt.UserRole))["type"]
-            type = qvariant_converter.getString(_type)
-            return type == "project"
+            preset_path = qvariant_converter.getDict(self.selected.data(Qt.UserRole))
+            preset_path = qvariant_converter.getStringDict(preset_path["info"])
+            preset_path = preset_path["path"]
+            self.setProjectInfo("PROJECT_BOARD", preset_path)
+            self.setProjectInfo("PROJECT_FROM_PRESET", True)
+            return True
         else:
             return False
 
     def nextId(self):
-        """
-        Overload of the QWizardPage nextId method.
-        """
-        return self.wizard().pageIndex(BRoutePage)
+        wizard = self.wizard()
+        if not self.projectInfo("PROJECT_FROM_PRESET"):
+            return wizard.pageIndex(BCpuPage)
+        else:
+            return QWizardPage.nextId(self)
 
     ####
 
@@ -92,51 +93,53 @@ class BBoardPage(BWizardPage):
         """
         Overload of the BWizardPage connectSignals method.
         """
-        self.connect(self.pageContent.boardTree, SIGNAL("itemSelectionChanged()"), self, SIGNAL("completeChanged()"))
+        self.connect(self.pageContent.boardList, SIGNAL("itemSelectionChanged()"), self.updateUi)
+        self.connect(self.pageContent.boardList, SIGNAL("itemSelectionChanged()"), self, SIGNAL("completeChanged()"))
+        self.connect(self.pageContent.customButton, SIGNAL("clicked()"), self.customButtonClicked)
 
     def reloadData(self):
         """
         Overload of the BWizardPage reloadData method.
         """
-        self._fillPresetTree()
+        self.project.loadProjectPresets()
+        preset_list = self.projectInfo("PRESET_TREE")
+        preset_list = preset_list["children"]
+        preset_list = sorted(preset_list.values(), _cmp)
+        self.setItems(preset_list)
 
     ####
 
     ## Slots ##
 
+    def updateUi(self):
+        if self.selected:
+            info_dict = qvariant_converter.getDict(self.selected.data(Qt.UserRole))
+            info_dict = qvariant_converter.getStringDict(info_dict["info"])
+            description = info_dict.get("description", "")
+            path = unicode(QUrl.fromLocalFile(info_dict["path"]).toString())
+            description = description.replace("$path", path)
+            self.pageContent.descriptionArea.setHtml(description)
+
+    def customButtonClicked(self):
+        self.setProjectInfo("PROJECT_FROM_PRESET", False)
+        self.wizard().next()
 
     ####
 
+    def setItems(self, preset_list):
+        self.pageContent.boardList.clear()
+        selected_board = self.projectInfo("PROJECT_BOARD")
+        for item_data in preset_list:
+            item_name = item_data["info"].get("name", item_data["info"]["filename"])
+            item_icon = os.path.join(item_data["info"]["path"], const.PREDEFINED_BOARD_ICON_FILE)
+            if not os.path.exists(item_icon):
+                item_icon = const.PREDEFINED_BOARD_DEFAULT_ICON
+            item = QListWidgetItem(QIcon(item_icon), item_name)
+            item.setData(Qt.UserRole, qvariant_converter.convertDict(item_data))
+            self.pageContent.boardList.addItem(item)
+            if selected_board and selected_board == item_data["info"]["path"]:
+                self.pageContent.boardList.setCurrentItem(item)
+
     @property
     def selected(self):
-        # We can only take the selected item list (not the single item)
-        _selected_items = self.pageContent.boardTree.selectedItems()
-        if _selected_items:
-            return _selected_items[0]
-        else:
-            return None
-
-    def _fillPresetTree(self):
-        self.pageContent.boardTree.clear()
-        self.project.loadProjectPresets()
-        preset_tree = self.project.info("PRESET_TREE")
-        for obj in preset_tree["children"]:
-            self._createPresetNode(self.pageContent.boardTree, obj)
-
-    def _createPresetNode(self, parent, obj):
-        item_name = obj["info"].get("name", obj["info"]["filename"])
-        item = QTreeWidgetItem(parent, [item_name]) 
-        item.setIcon(0, QIcon(self._getNodeIcon(obj)))
-        children_dict = obj["children"]
-        item.setData(0, Qt.UserRole, qvariant_converter.convertDict(obj["info"]))
-        for child in children_dict:
-            self._createPresetNode(item, child)
-
-    def _getNodeIcon(self, obj):
-        icon_file = os.path.join(obj["info"]["path"], const.PREDEFINED_BOARD_ICON_FILE)
-        if os.path.exists(icon_file):
-            return icon_file
-        elif obj["info"]["type"] == "dir":
-            return const.PREDEFINED_BOARD_DEFAULT_DIR_ICON
-        else:
-            return const.PREDEFINED_BOARD_DEFAULT_PROJECT_ICON
+        return self.pageContent.boardList.currentItem()
\ No newline at end of file