#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- import sys import csv from PyQt4 import QtGui from PyQt4 import QtCore import arbre2 as arbre_node class Dialog1(QtGui.QDialog): def __init__(self): super(Dialog1, self).__init__() self.list_arbo = self.import_csv() #self.arbre() self.initUI(self.list_arbo) def import_csv(self): #PARTIE CSV #csv separateur champ:tab separateur texte: double quote: ifile = open('acoubat_export/acoubat-table-classeproduit.csv', 'rb') reader = csv.reader(ifile, delimiter=' ', quotechar='"') self.tableau_importCSV = [] tableau_final = [] for ligne in reader: #print ligne self.tableau_importCSV.append(ligne) ifile.close() #fermeture du fichier CSV self.tableau_importCSV = self.tableau_importCSV[1:(len(self.tableau_importCSV)-1)] arbre=arbre_node.cNode("ACOUBAT", "mon_descriptif") for elem in self.tableau_importCSV: arbre[elem[1]]=arbre_node.cNode(elem[0], elem[2]) #mon_arbre = arbre["ACOUBAT"].affich_list() mon_arbre_string = arbre["ACOUBAT"].affich2() #print mon_arbre mon_arbre_list = mon_arbre_string.split("\n") list_arbo_definitif = [] for elem in mon_arbre_list: list_arbo_definitif.append(elem.split("|||")) return list_arbo_definitif def arbre(self): # Création de l'arbre arbre=arbre_node.cNode("main.c") # Ajout d'une branche (marche grâce à "__setitem__") arbre["main.c"]=arbre_node.cNode("stdio.h") arbre["main.c"]=arbre_node.cNode("stdlib.h") arbre["stdlib.h"]=arbre_node.cNode("sys/types.h") arbre["stdlib.h"]=arbre_node.cNode("sys/socket.h") arbre["stdio.h"]=arbre_node.cNode("sys/base_stdio.h") # Affichage d'une branche (marche grâce à "__getitem__") print arbre["main.c"].affich() print arbre["main.c"] def initUI(self, list_arbo): #http://openclassrooms.com/courses/programmez-avec-le-langage-c/l-architecture-mvc-avec-les-widgets-complexes #relation parent enfant # http://qt-project.org/doc/note_revisions/254/428/view mon_layout = QtGui.QVBoxLayout() #mon_modele = QtGui.QDirModel() """ ma_Qt_List = QtCore.QStringList() ma_list = ["toto", "titi", "tata", "pluplu"] for elem in ma_list: ma_Qt_List.append(elem) monModelList = QtGui.QStringListModel(ma_Qt_List) """ self.monModele = QtGui.QStandardItemModel() for elem in list_arbo: print elem #list_arbo #0 profondeur #1 parent #2 element_fils #3 descriptif_fils for elem in list_arbo: monItem = QtGui.QStandardItem(elem[1]) self.monModele.appendRow([monItem, QtGui.QStandardItem(elem[0])]) for num in range(2, len(elem)): mon_sous_item = QtGui.QStandardItem(elem[num][1]) monItem.appendRow([mon_sous_item,QtGui.QStandardItem(elem[num][0])]) """ self.monModele = QtGui.QStandardItemModel() monItem = QtGui.QStandardItem("Marc DUHIL") self.monModele.appendRow([monItem, QtGui.QStandardItem("MD"), QtGui.QStandardItem("MD2")]) monItem.appendRow([QtGui.QStandardItem("homme"), QtGui.QStandardItem("hom")]) monItem.appendRow([QtGui.QStandardItem("brun"), QtGui.QStandardItem("br")]) monItem.appendRow([QtGui.QStandardItem("acousticien_0"), QtGui.QStandardItem("acouyuyy"), QtGui.QStandardItem("plouplou")]) monItem.appendRow([QtGui.QStandardItem("acousticien"), QtGui.QStandardItem("acou")]) monItem.appendRow(QtGui.QStandardItem("gros")) monItem = QtGui.QStandardItem("Sophie ISARD") self.monModele.appendRow(monItem) monItem.appendRow(QtGui.QStandardItem("femme")) monItem.appendRow(QtGui.QStandardItem("secretaire")) monItem.appendRow(QtGui.QStandardItem("brune")) """ #self.ma_vue = QtGui.QTableView() self.ma_vue = QtGui.QTreeView() #self.ma_vue.header().hide() self.ma_vue.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) self.ma_vue.setModel(self.monModele) #self.ma_vue.hideColumn(1) # à bien placer après avoir fait le "Setmodel" mon_layout.addWidget(self.ma_vue) self.monPushButton = QtGui.QPushButton("Visualisation") QtCore.QObject.connect(self.monPushButton,QtCore.SIGNAL("clicked()"),self.clicSelection) #(utiliser fonction lambda) mon_layout.addWidget(self.monPushButton) self.setLayout(mon_layout) self.show() def clicSelection(self): #self.monModele est un QtGui.QStandardItemModel() """ indice_selection_unique = self.ma_vue.selectionModel().currentIndex() #renvoie un QModelIndex mon_text = self.monModele.data(indice_selection_unique, QtCore.Qt.DisplayRole).toString() QtGui.QMessageBox.information(self, 'Element sélectionné',mon_text) """ print "clic" maSelection = self.ma_vue.selectionModel() #listeElementSelectionne = maSelection.selectedIndexes() #renvoie un list-of-QModelIndex mon_list_of_QModelIndex = maSelection.selectedRows() #renvoie un list-of-QModelIndex TotalElementSelectionne = "" for i in range(len(mon_list_of_QModelIndex)): num_row_du_Qmodel = mon_list_of_QModelIndex[i].row() parent_Qmodel = mon_list_of_QModelIndex[i].parent() print "num_row_du_Qmodel" print num_row_du_Qmodel nb_col = self.monModele.columnCount(mon_list_of_QModelIndex[i]) #nb colonne "dans" leQmodelindex print "nb_col" print nb_col #mon_text = self.monModele.data(self.monModele.index(0,0), QtCore.Qt.DisplayRole) #print mon_text.toString() for num_col in range(nb_col): #mon_text = self.monModele.data(self.monModele.index(num_row_du_Qmodel,num_col), QtCore.Qt.DisplayRole) mon_text = self.monModele.data(self.monModele.index(num_row_du_Qmodel,num_col, parent_Qmodel), QtCore.Qt.DisplayRole) print mon_text.toString() #mon_text = self.monModele.data(self.monModele.index(num_row_du_Qmodel,num_col_du_Qmodel), QtCore.Qt.DisplayRole) #mon_text = self.monModele.data(self.monModele.index(0,0), QtCore.Qt.DisplayRole) #print mon_text.toString() print "" #TotalElementSelectionne = TotalElementSelectionne + elementSelectionne.toString() + "
" #QtGui.QMessageBox.information(self, 'Element sélectionné',TotalElementSelectionne) def main(): app = QtGui.QApplication(sys.argv) ex = Dialog1() sys.exit(app.exec_()) if __name__ == '__main__': main()