Skip to content
Snippets Groups Projects
Commit 65f57075 authored by inso's avatar inso
Browse files

Huge models refactoring

parent 17a36e17
No related branches found
No related tags found
No related merge requests found
Showing
with 437 additions and 43 deletions
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_3"> <layout class="QVBoxLayout" name="verticalLayout_3">
<item> <item>
<widget class="QTreeView" name="communitiesList"/> <widget class="QTreeView" name="communityView"/>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_3"> <layout class="QHBoxLayout" name="horizontalLayout_3">
......
...@@ -6,7 +6,9 @@ Created on 2 févr. 2014 ...@@ -6,7 +6,9 @@ Created on 2 févr. 2014
from cutecoin.gen_resources.addAccountDialog_uic import Ui_AddAccountDialog from cutecoin.gen_resources.addAccountDialog_uic import Ui_AddAccountDialog
from PyQt5.QtWidgets import QDialog from PyQt5.QtWidgets import QDialog
from cutecoin.gui.addCommunityDialog import AddCommunityDialog 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 import gnupg
...@@ -23,22 +25,21 @@ class AddAccountDialog(QDialog, Ui_AddAccountDialog): ...@@ -23,22 +25,21 @@ class AddAccountDialog(QDialog, Ui_AddAccountDialog):
# Set up the user interface from Designer. # Set up the user interface from Designer.
super(AddAccountDialog, self).__init__() super(AddAccountDialog, self).__init__()
self.setupUi(self) self.setupUi(self)
self.communities = Communities()
self.dialog = AddCommunityDialog(self)
self.dialog = AddCommunityDialog()
def setData(self): def setData(self):
self.communitiesManager = CommunitiesManager() self.communities = Communities()
gpg = gnupg.GPG() gpg = gnupg.GPG()
availableKeys = gpg.list_keys(True) availableKeys = gpg.list_keys(True)
for key in availableKeys: for key in availableKeys:
self.pgpkeyList.addItem(key['uids'][0]) self.pgpkeyList.addItem(key['uids'][0])
def openAddCommunityDialog(self): def openAddCommunityDialog(self):
self.dialog.setCommunitiesManager(self.communitiesManager) self.dialog.setCommunities(self.communities)
self.dialog.exec_() self.dialog.exec_()
def validAddCommunityDialog(self): def validAddCommunityDialog(self):
# TODO Show items in tree self.communitiesTable.setModel(CommunitiesTreeModel(self.communities))
pass
...@@ -5,10 +5,8 @@ Created on 2 févr. 2014 ...@@ -5,10 +5,8 @@ Created on 2 févr. 2014
''' '''
from cutecoin.gen_resources.addCommunityDialog_uic import Ui_AddCommunityDialog from cutecoin.gen_resources.addCommunityDialog_uic import Ui_AddCommunityDialog
from PyQt5.QtWidgets import QDialog from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QListWidgetItem from cutecoin.models.community.treeModel import CommunityTreeModel
from cutecoin.models.community import CommunitiesManager, Community
from cutecoin.models.node import MainNode from cutecoin.models.node import MainNode
import ucoinpy as ucoin
class AddCommunityDialog(QDialog, Ui_AddCommunityDialog): class AddCommunityDialog(QDialog, Ui_AddCommunityDialog):
''' '''
...@@ -16,15 +14,17 @@ class AddCommunityDialog(QDialog, Ui_AddCommunityDialog): ...@@ -16,15 +14,17 @@ class AddCommunityDialog(QDialog, Ui_AddCommunityDialog):
''' '''
def __init__(self): def __init__(self, accountDialog):
''' '''
Constructor Constructor
''' '''
super(AddCommunityDialog, self).__init__() super(AddCommunityDialog, self).__init__()
self.setupUi(self) self.setupUi(self)
self.accountDialog = accountDialog
self.buttonBox.accepted.connect(self.accountDialog.validAddCommunityDialog)
def setCommunitiesManager(self, communitiesManager): def setCommunities(self, communities):
self.communitiesManager = communitiesManager self.communities = communities
def addCommunity(self): def addCommunity(self):
''' '''
...@@ -32,6 +32,7 @@ class AddCommunityDialog(QDialog, Ui_AddCommunityDialog): ...@@ -32,6 +32,7 @@ class AddCommunityDialog(QDialog, Ui_AddCommunityDialog):
''' '''
server = self.serverEdit.text() server = self.serverEdit.text()
port = self.portBox.value() port = self.portBox.value()
self.communitiesManager.addCommunity(MainNode(server, port)) community = self.communities.addCommunity(MainNode(server, port))
self.communityView.setModel( CommunityTreeModel(community) )
...@@ -11,13 +11,13 @@ class Account(object): ...@@ -11,13 +11,13 @@ class Account(object):
classdocs classdocs
''' '''
def __init__(self, pgpKey, name, communityManager): def __init__(self, pgpKey, name, communities):
''' '''
Constructor Constructor
''' '''
self.pgpKey = pgpKey self.pgpKey = pgpKey
self.name = name self.name = name
self.communityManager = communityManager self.communities = communities
self.transactionNodes = [] self.transactionNodes = []
self.trustableNodes = [] self.trustableNodes = []
self.wallets = [] self.wallets = []
......
'''
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
'''
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
'''
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
'''
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)
...@@ -16,7 +16,6 @@ class Community(object): ...@@ -16,7 +16,6 @@ class Community(object):
''' '''
self.knownNodes = [] self.knownNodes = []
self.knownNodes.append(mainNode) self.knownNodes.append(mainNode)
self.currency = currency self.currency = currency
def members(self): def members(self):
...@@ -36,26 +35,4 @@ class Community(object): ...@@ -36,26 +35,4 @@ class Community(object):
def nodes(self): def nodes(self):
return self.knownNodes 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))
'''
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
'''
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)
...@@ -21,6 +21,9 @@ class Node(object): ...@@ -21,6 +21,9 @@ class Node(object):
def __eq__(self, other): def __eq__(self, other):
return ( self.server == other.server and self.port == other.port ) return ( self.server == other.server and self.port == other.port )
def getText(self):
return self.server + ":" + str(self.port)
class MainNode(Node): class MainNode(Node):
def downstreamPeers(self): def downstreamPeers(self):
...@@ -30,5 +33,6 @@ class MainNode(Node): ...@@ -30,5 +33,6 @@ class MainNode(Node):
peers = [] peers = []
for peer in ucoin.ucg.peering.peers.DownStream().get()['peers']: for peer in ucoin.ucg.peering.peers.DownStream().get()['peers']:
node = Node(peer['ipv4'], peer['port']) node = Node(peer['ipv4'], peer['port'])
print(node.server + ":" + node.port)
peers.append(node) peers.append(node)
return peers
'''
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment