SHARE
TWEET

HierarchyTreeView

a guest May 19th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. #Created on May 3, 2015
  3.  
  4. #@author: Petar Bjeljac
  5.  
  6. from PySide2.QtCore import QModelIndex, Qt
  7. from PySide2.QtWidgets import QTreeView, QMenu, QAction, QAbstractItemView
  8.  
  9. from src.model.Node import Node
  10. from src.gui.MainWindow import *
  11.  
  12.  
  13. class HierarchyTreeView(QTreeView):
  14.  
  15.     def __init__(self):
  16.         super(HierarchyTreeView, self).__init__()
  17.        
  18.         # ukljucuje kontekstni meni
  19.         self.setContextMenuPolicy(Qt.CustomContextMenu)
  20.         self.customContextMenuRequested.connect(self.openMenu)
  21.  
  22.     def openMenu(self, position):
  23.         self.contextMenu = QMenu()
  24.         newMenu = QMenu("New")
  25.         self.contextMenu.addMenu(newMenu)
  26.        
  27.         actionNewProj = QAction("NewProject", None)
  28.         actionNewProj.triggered.connect(self.addNode)
  29.  
  30.         actionNewDoc = QAction("NewDocument", None)
  31.         actionNewDoc.triggered.connect(self.addNode)    #umesto add node napraviti addDocument
  32.  
  33.         actionNewElem = QAction("NewElement", None)
  34.         actionNewElem.triggered.connect(self.addNode)   #umesto add node napraviti addElement
  35.  
  36.         actionRename = QAction("Rename", None)
  37.         actionRename.triggered.connect(self.renameNode)
  38.        
  39.         actionRemProj = QAction("Delete", None)
  40.         actionRemProj.triggered.connect(self.removeNode)
  41.        
  42.         newMenu.addAction(actionNewProj)
  43.         self.contextMenu.addAction(actionRename)
  44.         self.contextMenu.addAction(actionRemProj)
  45.  
  46.         newMenu.addAction(actionNewDoc)
  47.         newMenu.addAction(actionNewElem)
  48.        
  49.         # prikaz kontekstnog menija
  50.         self.contextMenu.exec_(self.viewport().mapToGlobal(position))
  51.        
  52.     def addNode(self):
  53.         """
  54.            TODO: implementirati dijalog za unos naziva, mogućnost dodavanje tipiziranih čvorova i rukovanje situacijom postojanja elementa sa istim nazivom.
  55.        """
  56.         model = self.model()
  57.  
  58.         text = self.textInput()
  59.  
  60.         for cvor in Node.lista:
  61.             while text == cvor:
  62.                 self.errorMessage()
  63.                 text = self.textInput()
  64.  
  65.         node = Node(text)
  66.  
  67.        
  68.         if not self.currentIndex().isValid():
  69.             model.insertRow(model.rowCount(self.currentIndex()), node)
  70.         else:
  71.             model.insertRow(model.rowCount(self.currentIndex()), node, self.currentIndex())
  72.         self.expand(self.currentIndex())
  73.    
  74.     def removeNode(self):           #ovo je reseno skroz   ON POSTOJI ALI MI NE PRIKAZUJE IME
  75.         """
  76.            TODO: implementirati dijalog za potvrdu akcije brisanja.
  77.        """
  78.         flags = QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
  79.         button = QMessageBox.question(self, u"Pitanje", u"Da li ste sigurni?", flags)
  80.  
  81.         if button == QMessageBox.Yes:                                                               #SAMO OVA 2 SAM MENJAO POKRENI PA CES SKONTATI          PROMENI IME ALI GA NE ZAPISE NEGO OSTAVI PRAZNO
  82.             QMessageBox.information(self, "Odabrano dugme", "Odabrano je dugme Yes")
  83.             model = self.model()
  84.             model.removeRow(self.currentIndex().internalPointer().getIndex(), self.currentIndex().parent())
  85.         else:
  86.             QMessageBox.information(self, "Odabrano dugme", "Odabrano je dugme No")
  87.  
  88.     def renameNode(self):
  89.         """
  90.            TODO: implementirati dijalog za unos naziva i rukovanje situacijom postojanja elementa sa istim nazivom.
  91.        """
  92.         text = self.textInput()
  93.         for cvor in Node.lista:
  94.             while text == cvor:
  95.                 self.errorMessage()
  96.                 text = self.textInput()
  97.         self.currentIndex().internalPointer().setName(text)
  98.  
  99.     def mousePressEvent(self, event):
  100.         if self.selectionMode() == QAbstractItemView.SingleSelection:
  101.             self.clearSelection()
  102.             self.setCurrentIndex(QModelIndex())
  103.         super(HierarchyTreeView, self).mousePressEvent(event)
  104.  
  105.     def textInput(self):
  106.         retText, okButton = QInputDialog.getText(self, "Naziv", "Unesite naziv")
  107.         if okButton:
  108.             return retText
  109.  
  110.     def errorMessage(self):
  111.         QMessageBox.critical(self, u"Greška", u"Dogodila se greška, ista imena! ")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top