diff --git a/res/ui/addCommunityDialog.ui b/res/ui/addCommunityDialog.ui index a7f25323b5f8e9139cd06d6ec19acb5b038f24b9..9da5921fca17c04e52ffc24039fe45c03a262fe3 100644 --- a/res/ui/addCommunityDialog.ui +++ b/res/ui/addCommunityDialog.ui @@ -24,7 +24,7 @@ </property> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> - <widget class="QTreeView" name="communitiesList"/> + <widget class="QTreeView" name="communityView"/> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_3"> diff --git a/src/cutecoin/gui/addAccountDialog.py b/src/cutecoin/gui/addAccountDialog.py index 9e8a86c93b545c7dcbd6c3235d230ea792b5361e..046914cafd687b44c3ab21d4be55a26cc23d8f22 100644 --- a/src/cutecoin/gui/addAccountDialog.py +++ b/src/cutecoin/gui/addAccountDialog.py @@ -6,7 +6,9 @@ Created on 2 févr. 2014 from cutecoin.gen_resources.addAccountDialog_uic import Ui_AddAccountDialog from PyQt5.QtWidgets import QDialog from cutecoin.gui.addCommunityDialog import AddCommunityDialog -from cutecoin.models.community import CommunitiesManager +from cutecoin.models.account.communities import Communities +from cutecoin.models.account.communities.treeModel import CommunitiesTreeModel + import gnupg @@ -23,22 +25,21 @@ class AddAccountDialog(QDialog, Ui_AddAccountDialog): # Set up the user interface from Designer. super(AddAccountDialog, self).__init__() self.setupUi(self) - - - self.dialog = AddCommunityDialog() + self.communities = Communities() + self.dialog = AddCommunityDialog(self) def setData(self): - self.communitiesManager = CommunitiesManager() + self.communities = Communities() gpg = gnupg.GPG() availableKeys = gpg.list_keys(True) for key in availableKeys: self.pgpkeyList.addItem(key['uids'][0]) def openAddCommunityDialog(self): - self.dialog.setCommunitiesManager(self.communitiesManager) + self.dialog.setCommunities(self.communities) self.dialog.exec_() def validAddCommunityDialog(self): - # TODO Show items in tree - pass + self.communitiesTable.setModel(CommunitiesTreeModel(self.communities)) + diff --git a/src/cutecoin/gui/addCommunityDialog.py b/src/cutecoin/gui/addCommunityDialog.py index b0d622c632a15b6bc748695a0afb5c77104153e2..4f5966d61ce80e8febe14588002f635a02c56d67 100644 --- a/src/cutecoin/gui/addCommunityDialog.py +++ b/src/cutecoin/gui/addCommunityDialog.py @@ -5,10 +5,8 @@ Created on 2 févr. 2014 ''' from cutecoin.gen_resources.addCommunityDialog_uic import Ui_AddCommunityDialog from PyQt5.QtWidgets import QDialog -from PyQt5.QtWidgets import QListWidgetItem -from cutecoin.models.community import CommunitiesManager, Community +from cutecoin.models.community.treeModel import CommunityTreeModel from cutecoin.models.node import MainNode -import ucoinpy as ucoin class AddCommunityDialog(QDialog, Ui_AddCommunityDialog): ''' @@ -16,15 +14,17 @@ class AddCommunityDialog(QDialog, Ui_AddCommunityDialog): ''' - def __init__(self): + def __init__(self, accountDialog): ''' Constructor ''' super(AddCommunityDialog, self).__init__() self.setupUi(self) + self.accountDialog = accountDialog + self.buttonBox.accepted.connect(self.accountDialog.validAddCommunityDialog) - def setCommunitiesManager(self, communitiesManager): - self.communitiesManager = communitiesManager + def setCommunities(self, communities): + self.communities = communities def addCommunity(self): ''' @@ -32,6 +32,7 @@ class AddCommunityDialog(QDialog, Ui_AddCommunityDialog): ''' server = self.serverEdit.text() port = self.portBox.value() - self.communitiesManager.addCommunity(MainNode(server, port)) + community = self.communities.addCommunity(MainNode(server, port)) + self.communityView.setModel( CommunityTreeModel(community) ) diff --git a/src/cutecoin/models/account.py b/src/cutecoin/models/account/__init__.py similarity index 84% rename from src/cutecoin/models/account.py rename to src/cutecoin/models/account/__init__.py index f334ea4cffecfbdfdf70ef5c723c61750d305e47..6d9a4a46a3cd2142d16edbdf76307b32238410e7 100644 --- a/src/cutecoin/models/account.py +++ b/src/cutecoin/models/account/__init__.py @@ -11,13 +11,13 @@ class Account(object): classdocs ''' - def __init__(self, pgpKey, name, communityManager): + def __init__(self, pgpKey, name, communities): ''' Constructor ''' self.pgpKey = pgpKey self.name = name - self.communityManager = communityManager + self.communities = communities self.transactionNodes = [] self.trustableNodes = [] self.wallets = [] diff --git a/src/cutecoin/models/account/communities/__init__.py b/src/cutecoin/models/account/communities/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..7e50ad206c1618f9043f0da878d8ace8bf711d2c --- /dev/null +++ b/src/cutecoin/models/account/communities/__init__.py @@ -0,0 +1,34 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' +import ucoinpy as ucoin +from cutecoin.models.community import Community + +class Communities(object): + ''' + classdocs + ''' + def __init__(self): + ''' + Constructor + ''' + self.communitiesList = [] + + def getCommunity(self, currency): + for com in self.communitiesList: + if com.currency == currency: + return com + + def addCommunity(self, mainNode): + ucoin.settings['server'] = mainNode.server + ucoin.settings['port'] = mainNode.port + currentAmendment = ucoin.hdc.amendments.Promoted().get() + currency = currentAmendment['currency'] + community = self.getCommunity(currency) + if community == None: + community = Community(mainNode, currency) + self.communitiesList.append(community) + + return community \ No newline at end of file diff --git a/src/cutecoin/models/account/communities/itemModel.py b/src/cutecoin/models/account/communities/itemModel.py new file mode 100644 index 0000000000000000000000000000000000000000..65f39f0e396438c25298fb024ebfa4598313db11 --- /dev/null +++ b/src/cutecoin/models/account/communities/itemModel.py @@ -0,0 +1,34 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' + +class CommunitiesItemModel(object): + def __init__(self, account): + self.communitiesText = account.pgpKey + self.communityItems = [] + + def appendChild(self, communityItem): + self.communityItems.append(communityItem) + + def child(self, row): + return self.communityItems[row] + + def childCount(self): + return len(self.communityItems) + + def columnCount(self): + return 1 + + def data(self, column): + try: + return self.communitiesText + except IndexError: + return None + + def parent(self): + return None + + def row(self): + return 0 \ No newline at end of file diff --git a/src/cutecoin/models/account/communities/listModel.py b/src/cutecoin/models/account/communities/listModel.py new file mode 100644 index 0000000000000000000000000000000000000000..62c68739914a2475799269e240d6eb7c4f7cdc1e --- /dev/null +++ b/src/cutecoin/models/account/communities/listModel.py @@ -0,0 +1,32 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' + +from PyQt5.QtCore import QAbstractListModel, Qt + +class CommunitiesListModel(QAbstractListModel): + ''' + A Qt abstract item model to display communities in a tree + ''' + def __init__(self, communities, parent=None): + ''' + Constructor + ''' + super(CommunitiesListModel, self).__init__(parent) + self.communities = communities + + + def rowCount(self ,parent): + return len(self.communities.communitiesList) + + def data(self,index,role): + + if role == Qt.DisplayRole: + row=index.row() + value = self.communities.communitiesList[row].currency + return value + + def flags(self,index): + return Qt.ItemIsSelectable | Qt.ItemIsEnabled diff --git a/src/cutecoin/models/account/communities/treeModel.py b/src/cutecoin/models/account/communities/treeModel.py new file mode 100644 index 0000000000000000000000000000000000000000..2e6fa0e6d1df11674761976cf2465eca25e5e0ec --- /dev/null +++ b/src/cutecoin/models/account/communities/treeModel.py @@ -0,0 +1,104 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' + +from PyQt5.QtCore import QAbstractItemModel, Qt, QModelIndex +from cutecoin.models.node.itemModel import NodeTreeItem +from cutecoin.models.account.communities.itemModel import CommunitiesItemModel +from cutecoin.models.community.itemModel import CommunityItemModel +from cutecoin.models.node.itemModel import MainNodeTreeItem + +class CommunitiesTreeModel(QAbstractItemModel): + ''' + A Qt abstract item model to display communities in a tree + ''' + def __init__(self, account): + ''' + Constructor + ''' + super(CommunitiesTreeModel, self).__init__(None) + self.communities = account.communities + self.rootItem = CommunitiesItemModel(account) + self.refreshTreeNodes() + + def columnCount(self, parent): + return 1 + + def data(self, index, role): + if not index.isValid(): + return None + + if role != Qt.DisplayRole: + return None + + item = index.internalPointer() + + return item.data + + def flags(self, index): + if not index.isValid(): + return Qt.NoItemFlags + + return Qt.ItemIsEnabled | Qt.ItemIsSelectable + + def headerData(self, section, orientation, role): + if orientation == Qt.Horizontal and role == Qt.DisplayRole: + return "Communities nodes" + + return None + + def index(self, row, column, parent): + if not self.hasIndex(row, column, parent): + return QModelIndex() + + if not parent.isValid(): + parentItem = self.rootItem + else: + parentItem = parent.internalPointer() + + childItem = parentItem.child(row) + if childItem: + return self.createIndex(row, column, childItem) + else: + return QModelIndex() + + def parent(self, index): + if not index.isValid(): + return QModelIndex() + + childItem = index.internalPointer() + parentItem = childItem.parent() + + if parentItem == self.rootItem: + return QModelIndex() + + return self.createIndex(parentItem.row(), 0, parentItem) + + def rowCount(self, parent): + if parent.column() > 0: + return 0 + + if not parent.isValid(): + parentItem = self.rootItem + else: + parentItem = parent.internalPointer() + + return parentItem.childCount() + + + def refreshTreeNodes(self): + for community in self.communities.communitiesList: + communityItem = CommunityItemModel(community, self) + self.rootItem.appendChild(communityItem) + for mainNode in community.knownNodes: + mainNodeItem = MainNodeTreeItem(mainNode, communityItem) + communityItem.appendChild(mainNodeItem) + for node in mainNode.downstreamPeers(): + nodeItem = NodeTreeItem(node, mainNodeItem) + mainNodeItem.appendChild(nodeItem) + + + + diff --git a/src/cutecoin/models/community.py b/src/cutecoin/models/community/__init__.py similarity index 52% rename from src/cutecoin/models/community.py rename to src/cutecoin/models/community/__init__.py index 4ad3bcf1370a711fe915fa7538a2eb53bed0675a..e6aa91d1fef1f65287328d70e99af2b7ec89bb30 100644 --- a/src/cutecoin/models/community.py +++ b/src/cutecoin/models/community/__init__.py @@ -16,7 +16,6 @@ class Community(object): ''' self.knownNodes = [] self.knownNodes.append(mainNode) - self.currency = currency def members(self): @@ -36,26 +35,4 @@ class Community(object): def nodes(self): return self.knownNodes -class CommunitiesManager(object): - ''' - classdocs - ''' - def __init__(self): - ''' - Constructor - ''' - self.communities = [] - - def getCommunity(self, currency): - for com in self.communities: - if com.currency == currency: - return com - - def addCommunity(self, mainNode): - ucoin.settings['server'] = mainNode.server - ucoin.settings['port'] = mainNode.port - mainNode.downstreamPeers() - currentAmendment = ucoin.hdc.amendments.Promoted().get() - currency = currentAmendment['currency'] - if self.getCommunity(currency) == None: - self.communities.append(Community(mainNode, currency)) + diff --git a/src/cutecoin/models/community/itemModel.py b/src/cutecoin/models/community/itemModel.py new file mode 100644 index 0000000000000000000000000000000000000000..61ccfbcd2ac8fedb79441afb565c148a18387a66 --- /dev/null +++ b/src/cutecoin/models/community/itemModel.py @@ -0,0 +1,38 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' + +class CommunityItemModel(object): + def __init__(self, community, communitiesItem=None): + self.communitiesItem = communitiesItem + self.communityText = community.currency + self.mainNodeItems = [] + + def appendChild(self, item): + self.mainNodeItems.append(item) + + def child(self, row): + return self.mainNodeItems[row] + + def childCount(self): + return len(self.mainNodeItems) + + def columnCount(self): + return len(self.itemData) + + def data(self, column): + try: + return self.communityText + except IndexError: + return None + + def parent(self): + return self.communitiesItem + + def row(self): + if self.communitiesItem: + return self.communitiesItem.communityItems.index(self) + + return 0 diff --git a/src/cutecoin/models/community/treeModel.py b/src/cutecoin/models/community/treeModel.py new file mode 100644 index 0000000000000000000000000000000000000000..53d4b71b769340aaf2f73b7f974c3e09991ea607 --- /dev/null +++ b/src/cutecoin/models/community/treeModel.py @@ -0,0 +1,98 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' + +from PyQt5.QtCore import QAbstractItemModel, QModelIndex, Qt +from cutecoin.models.node.itemModel import NodeTreeItem +from cutecoin.models.community.itemModel import CommunityItemModel + +class CommunityTreeModel(QAbstractItemModel): + ''' + A Qt abstract item model to display nodes of a community + ''' + def __init__(self, community): + ''' + Constructor + ''' + super(CommunityTreeModel, self).__init__(None) + self.community = community + self.rootItem = CommunityItemModel(self.community) + self.refreshTreeNodes() + + def columnCount(self, parent): + return 1 + + def data(self, index, role): + if not index.isValid(): + return None + + if role != Qt.DisplayRole: + return None + + item = index.internalPointer() + + return item.data + + def flags(self, index): + if not index.isValid(): + return Qt.NoItemFlags + + return Qt.ItemIsEnabled | Qt.ItemIsSelectable + + def headerData(self, section, orientation, role): + if orientation == Qt.Horizontal and role == Qt.DisplayRole: + return self.rootItem.data + + return None + + def index(self, row, column, parent): + if not self.hasIndex(row, column, parent): + return QModelIndex() + + if not parent.isValid(): + parentItem = self.rootItem + else: + parentItem = parent.internalPointer() + + childItem = parentItem.child(row) + if childItem: + return self.createIndex(row, column, childItem) + else: + return QModelIndex() + + def parent(self, index): + if not index.isValid(): + return QModelIndex() + + childItem = index.internalPointer() + parentItem = childItem.parent() + + if parentItem == self.rootItem: + return QModelIndex() + + return self.createIndex(parentItem.row(), 0, parentItem) + + def rowCount(self, parent): + if parent.column() > 0: + return 0 + + if not parent.isValid(): + parentItem = self.rootItem + else: + parentItem = parent.internalPointer() + + return parentItem.childCount() + + + def refreshTreeNodes(self): + for mainNode in self.community.knownNodes: + mainNodeItem = NodeTreeItem(mainNode, self.rootItem) + print("mainNode : " + mainNode.getText()) + self.rootItem.appendChild(mainNodeItem) + for node in mainNode.downstreamPeers(): + print("\t node : " + node.getText()) + nodeItem = NodeTreeItem(node, mainNodeItem) + mainNodeItem.appendChild(nodeItem) + diff --git a/src/cutecoin/models/node.py b/src/cutecoin/models/node/__init__.py similarity index 87% rename from src/cutecoin/models/node.py rename to src/cutecoin/models/node/__init__.py index 69435cb1bfb47a189350f72cc3268da268a4f891..d73ad3f4a8673eba79bad2ae59d0e81a7f2368e7 100644 --- a/src/cutecoin/models/node.py +++ b/src/cutecoin/models/node/__init__.py @@ -21,6 +21,9 @@ class Node(object): def __eq__(self, other): return ( self.server == other.server and self.port == other.port ) + def getText(self): + return self.server + ":" + str(self.port) + class MainNode(Node): def downstreamPeers(self): @@ -30,5 +33,6 @@ class MainNode(Node): peers = [] for peer in ucoin.ucg.peering.peers.DownStream().get()['peers']: node = Node(peer['ipv4'], peer['port']) - print(node.server + ":" + node.port) peers.append(node) + return peers + diff --git a/src/cutecoin/models/node/itemModel.py b/src/cutecoin/models/node/itemModel.py new file mode 100644 index 0000000000000000000000000000000000000000..61f16e28cb48b4cff35c8cbcca5cb15d829892b5 --- /dev/null +++ b/src/cutecoin/models/node/itemModel.py @@ -0,0 +1,71 @@ +''' +Created on 5 févr. 2014 + +@author: inso +''' + + +class NodeTreeItem(object): + def __init__(self, node, mainNodeItem=None): + self.mainNodeItem = mainNodeItem + self.nodeText = node.getText() + + def appendChild(self, item): + pass + + def child(self, row): + return None + + def childCount(self): + return 0 + + def columnCount(self): + return 1 + + def data(self, column): + try: + return self.nodeText + except IndexError: + return None + + def parent(self): + return self.mainNodeItem + + def row(self): + if self.mainNodeItem: + return self.mainNodeItem.nodeItems.index(self) + + return 0 + +class MainNodeTreeItem(object): + def __init__(self, mainNode, communityItem=None): + self.communityItem = communityItem + self.mainNodeText = mainNode.getText() + self.nodeItems = [] + + def appendChild(self, nodeItem): + self.nodeItems.append(nodeItem) + + def child(self, row): + return self.nodeItems[row] + + def childCount(self): + return len(self.nodeItems) + + def columnCount(self): + return 1 + + def data(self, column): + try: + return self.mainNodeText + except IndexError: + return None + + def parent(self): + return self.communityItem + + def row(self): + if self.communityItem: + return self.communityItem.mainNodeItems.index(self) + + return 0 \ No newline at end of file