1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
| # -*- coding: utf-8 -*-
#!/usr/bin/env python
# On importe les bibliothèques que nous allons utiliser.
import sys
from PyQt4 import QtCore, QtGui
# On importe notre boîte de dialogue.
from dial_Projet3 import Ui_dial_Projet3
import time
# # # # # # # # # # # # # # # # # # # # # #
# Comportement de la boîte de dialogue. DEBUT
class MyFrame(QtGui.QFrame):
# Squelette donné par Brian Kelley sur la liste de PyQt.
def __init__(self, parent, titre, pixmapfile):
QtGui.QFrame.__init__(self, parent, QtCore.Qt.Tool)
self.setWindowTitle(titre)
rect = QtCore.QRect(QtGui.QCursor.pos(), pixmapfile.size())
self.setGeometry(rect)
self.setFixedSize(pixmapfile.size())
label = QtGui.QLabel(self)
label.setPixmap(pixmapfile)
label.show()
self.timer = QtCore.QTimer()
self.connect(self.timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("hide()"))
def focusInEvent(self, event):
self.setWindowOpacity(0.5)
def showEvent(self, event):
self.timer.start(5000) # 5 seconds
QtGui.QFrame.showEvent(self, event)
class dial_Projet3(QtGui.QDialog, Ui_dial_Projet3):
def __init__(self):
QtGui.QDialog.__init__(self)
Ui_dial_Projet3.__init__(self)
self.setupUi(self)
# On met un texte brut type qui aidera l'utilisateur à faire ses propres tables tout seul.
self.texte_Type = """Intro
Chap I
>Para 1
>>Partie A
>>Partie B
>Para 2
Chap II
Conclusion"""
self.textUtilisateur.setText(self.texte_Type)
# On crée à la main le tableau pour garder en mémoire la table des matières.
self.tablo_tabMat_Memo = []
self.tablo_tabMat_Memo.append('Intro')
tablo_Para = []
tablo_Para.append('Chap I')
tablo_Partie = []
tablo_Partie.append('Para 1')
tablo_Partie.append('Partie A')
tablo_Partie.append('Partie B')
tablo_Para.append(tablo_Partie)
tablo_Para.append('Para 2')
self.tablo_tabMat_Memo.append(tablo_Para)
self.tablo_tabMat_Memo.append('Chap II')
self.tablo_tabMat_Memo.append('Conclusion')
# On continue à remplir l'arbre à la main.
# PETITE NOUVEAUTE : nous allons faire en sorte que l'arbre soit totalement déployé dès l'affichage(voir le commentaire ci-dessous).
self.treeWidget.clear()
self.treeWidget.setColumnCount(1)
self.treeWidget.setHeaderLabels(["Sommaire"])
self.treeWidget.setItemsExpandable(True)
tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, ["Intro"])
tabNiv_1.setIcon(0, QtGui.QIcon('Images/im_1.png'))
tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, ["Chap I"])
tabNiv_1.setIcon(0, QtGui.QIcon('Images/im_1.png'))
# La ligne suivante permet "d'ouvrir" le noeud Para 1. On fait de même avec tous les noeuds de Chap I, puis avec Chap I lui-même.
self.treeWidget.expandItem(tabNiv_1)
tabNiv_2 = QtGui.QTreeWidgetItem(tabNiv_1, ["Para 1"])
tabNiv_2.setIcon(0, QtGui.QIcon('Images/im_2.png'))
self.treeWidget.expandItem(tabNiv_2)
tabNiv_3 = QtGui.QTreeWidgetItem(tabNiv_2, ["Partie A"])
tabNiv_3.setIcon(0, QtGui.QIcon('Images/im_3.png'))
self.treeWidget.expandItem(tabNiv_3)
tabNiv_3 = QtGui.QTreeWidgetItem(tabNiv_2, ["Partie B"])
tabNiv_3.setIcon(0, QtGui.QIcon('Images/im_3.png'))
self.treeWidget.expandItem(tabNiv_3)
tabNiv_2 = QtGui.QTreeWidgetItem(tabNiv_1, ["Para 2"])
tabNiv_2.setIcon(0, QtGui.QIcon('Images/im_2.png'))
self.treeWidget.expandItem(tabNiv_2)
tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, ["Chap II"])
tabNiv_1.setIcon(0, QtGui.QIcon('Images/im_1.png'))
tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, ["Conclusion"])
tabNiv_1.setIcon(0, QtGui.QIcon('Images/im_1.png'))
# On définit la connection liée au changement du contenu du QTextEdit.
self.connect(self.textUtilisateur, QtCore.SIGNAL("textChanged()"), self.chgtTexte)
self.connect(self.treeWidget, QtCore.SIGNAL("itemClicked(QTreeWidgetItem*,int)"), self.clicNoeud)
# On définit la connection liée au menu contextuel.
self.treeWidget.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.connect(self.treeWidget, QtCore.SIGNAL("customContextMenuRequested(const QPoint &)"), self.afficheImage)
# Menu Contextuel
# Cf. bas de la page :
# http://www.nabble.com/Context-menu-on-items-in-QTreeView-td19981837.html
#
# http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qwidget.html#setContextMenuPolicy
# http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qt.html#ContextMenuPolicy-enum
# http://www.riverbankcomputing.com/static/Docs/PyQt4/html/qwidget.html#customContextMenuRequested
def afficheImage(self, point):
# On récupère les infos sur l'item sélectionné.
index = self.treeWidget.indexAt(point)
if not index.isValid():
return
# print index.text(0)
item = self.treeWidget.itemAt(point)
name = item.text(0) # Nom du noeaud
# Problème des grands images à gérer par exemple en les redimensionnant ou en utilisant un GraphicsView et des scrollbars.
pixmap = QtGui.QPixmap("normale.jpg")
# pixmap = QtGui.QPixmap("tropGrande.jpg")
frame = MyFrame(self, name, pixmap)
frame.show()
# Info récupérée sur le forum suivant : .
def clicNoeud(self,noeudClic):
# La méthode ci-dessous a ses limites car il faut connaître la profondeur de l'arbre.
par_1=''
par_2=''
test_1 = noeudClic.parent()
if test_1 != None:
par_1 = str(noeudClic.parent().text(0))
test_2 = test_1.parent()
if test_2 != None:
par_2 = str(test_2.parent())
if par_2!='':
print "Clic sur une partie-Titre de la partie :"
print " " + str(noeudClic.text(0))
elif par_1!='':
print "Clic sur un paragraphe-Titre du paragraphe :"
print " " + str(noeudClic.text(0))
else:
print "Clic sur un chapitre-Titre du chapitre :"
print " " + str(noeudClic.text(0))
print''
# Comportement de la boîte de dialogue. FIN
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # #
# Analyse brutale du changement de contenu. DEBUT
def chgtTexte(self):
text_TabMatBrut = self.textUtilisateur.toPlainText()
# Traduction du contenu sous forme d'un tableau.
self.tablo_tabMat = []
niv = 0
maj = True
lignes_tabMat_Sale = text_TabMatBrut.split("\n")
lignes_tabMat = []
for i in range(len(lignes_tabMat_Sale)):
# On nettoie les lignes.
uneLignePropre = str(lignes_tabMat_Sale[i]).strip()
if uneLignePropre <> '' and uneLignePropre <> '>' and uneLignePropre <> '>>' :
lignes_tabMat.append(uneLignePropre)
# On parcourt les lignes en les analysant.
for i in range(len(lignes_tabMat)):
uneLignePropre = lignes_tabMat[i]
# Repérage d'une PARTIE
if uneLignePropre[:2] == '>>':
if niv < 2:
maj = False
print u"Un problème de structure logique (sûrement temporaire) a été repéré.\nLa 1ère balise défectueuse est : \"" + uneLignePropre + '".\n'
break
else:
uneLignePropre = uneLignePropre[2:].strip()
if i == len(lignes_tabMat) - 1:
tablo_Partie.append(uneLignePropre)
tablo_Para.append(tablo_Partie)
self.tablo_tabMat.append(tablo_Para)
else:
lignePropreSuivante = lignes_tabMat[i + 1]
if lignePropreSuivante[:2] == '>>':
tablo_Partie.append(uneLignePropre)
elif lignePropreSuivante[0] == '>':
tablo_Partie.append(uneLignePropre)
tablo_Para.append(tablo_Partie)
else :
tablo_Partie.append(uneLignePropre)
tablo_Para.append(tablo_Partie)
self.tablo_tabMat.append(tablo_Para)
niv = 3
# Repérage d'un PARAGRAPHE
elif uneLignePropre[0] == '>':
if niv == 0:
maj = False
print u"Un problème de structure logique (sûrement temporaire) a été repéré.\nLa 1ère balise défectueuse est : \"" + uneLignePropre + '".\n'
break
else:
uneLignePropre = uneLignePropre[1:].strip()
tablo_Partie = []
if i == len(lignes_tabMat) - 1:
tablo_Para.append(uneLignePropre)
self.tablo_tabMat.append(tablo_Para)
else:
lignePropreSuivante = lignes_tabMat[i + 1]
if lignePropreSuivante[:2] == '>>':
tablo_Partie.append(uneLignePropre)
elif lignePropreSuivante[0] == '>':
tablo_Para.append(uneLignePropre)
else:
tablo_Para.append(uneLignePropre)
self.tablo_tabMat.append(tablo_Para)
niv = 2
# Repérage d'un CHAPITRE
else:
niv = 1
tablo_Para = []
tablo_Partie = []
if i == len(lignes_tabMat) - 1:
self.tablo_tabMat.append(uneLignePropre)
else:
lignePropreSuivante = lignes_tabMat[i + 1]
if lignePropreSuivante[0] <> '>':
self.tablo_tabMat.append(uneLignePropre)
else:
tablo_Para.append(uneLignePropre)
# Mise à jour de l'arbre de la table (la technique est similaire à celle utilisée pour l'affichage HTML de la Table des Matières).
if maj:
if self.tablo_tabMat <> self.tablo_tabMat_Memo:
self.lineTitre_2.setText(QtGui.QApplication.translate("dial_TestAnaBalise", "Arbre de la table des matières", None, QtGui.QApplication.UnicodeUTF8))
self.tablo_tabMat_Memo = self.tablo_tabMat
self.treeWidget.clear()
self.treeWidget.setColumnCount(1)
self.treeWidget.setHeaderLabels(["Sommaire"])
self.treeWidget.setItemsExpandable(True)
for i_Chap in range(len(self.tablo_tabMat)):
try:
# ATTENTION ! IL faut donner un tableau avec une seule chaîne pour nourrir l'arbre.
# Donc tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, [self.tablo_tabMat[i_Chap]]) ne marchera
# que si self.tablo_tabMat[i_Chap] est une chaîne de caractères.
# Si ce n'est pas le cas, c'est que le chapitre contient des paragraphes.
tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, [self.tablo_tabMat[i_Chap]])
tabNiv_1.setIcon(0, QtGui.QIcon('Images/im_1.png'))
except:
tabloPara = self.tablo_tabMat[i_Chap]
for i_Para in range(len(tabloPara)):
if i_Para == 0:
tabNiv_1 = QtGui.QTreeWidgetItem(self.treeWidget, [tabloPara[0]])
tabNiv_1.setIcon(0, QtGui.QIcon('Images/im_1.png'))
self.treeWidget.expandItem(tabNiv_1)
else:
try:
tabNiv_2 = QtGui.QTreeWidgetItem(tabNiv_1, [tabloPara[i_Para]])
tabNiv_2.setIcon(0, QtGui.QIcon('Images/im_2.png'))
self.treeWidget.expandItem(tabNiv_2)
except:
tabloPartie = tabloPara[i_Para]
for i_Partie in range(len(tabloPartie)):
if i_Partie == 0:
tabNiv_2 = QtGui.QTreeWidgetItem(tabNiv_1, [tabloPartie[0]])
tabNiv_2.setIcon(0, QtGui.QIcon('Images/im_2.png'))
self.treeWidget.expandItem(tabNiv_2)
else:
tabNiv_3 = QtGui.QTreeWidgetItem(tabNiv_2, [tabloPartie[i_Partie]])
tabNiv_3.setIcon(0, QtGui.QIcon('Images/im_3.png'))
self.treeWidget.expandItem(tabNiv_3)
# Pas de mise à jour possible.
else :
self.tablo_tabMat_Memo = self.tablo_tabMat
self.lineTitre_2.setText(QtGui.QApplication.translate("dial_TestAnaBalise", "Mise à jour impossible", None, QtGui.QApplication.UnicodeUTF8))
# Analyse brutale du changement de contenu. FIN
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # #
# Lancement de l'application.
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Projet3 = dial_Projet3()
Projet3.show()
sys.exit(app.exec_()) |
Partager