def __init__(self, parent=None): super().__init__(parent=parent) fp = open("css/client.css", "r") self.setStyleSheet(fp.read()) self.setCacheMode(QGraphicsView.CacheBackground) self.setAttribute(Qt.WA_TranslucentBackground) self.setAttribute(Qt.WA_DeleteOnClose) item = Body(self.parent(), self.width(), self.height()) scene = QGraphicsScene() scene.addItem(item) self.setScene(scene) self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint) self.tmp_pos = QPoint(0, 0)
class PieChart (QGraphicsView): def __init__(self,parent,total_value): super(PieChart,self).__init__(parent) self.my_scene = QGraphicsScene() #self.my_scene.setSceneRect(self.sceneRect()) self.setScene(self.my_scene) #self.setBackgroundBrush(QBrush(QtCore.Qt.black, QtCore.Qt.SolidPattern)); self.total_value = total_value self.initialize = False self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.margin = {'top':10,'bottom':10,'left':10,'right':10} self.space = 30 self.pie_items = [] def setTotal (self, total): self.total_value = total # def setData (self,list_item): # # rect = QGraphicsRectItem(self.sceneRect()) # rect.setBrush(QColor("red")) # self.scene().addItem(rect) def setData (self,list_items): size = min(self.frameSize().width()/2.0,self.frameSize().height()) self.size_pie = QSize(size,size) #print ('size pie ',self.size_pie) if self.initialize == False: self.c_box = QComboBox() self.c_box.addItem("all") self.c_box.addItem("alive") self.c_box.addItem("dead") self.c_box.currentIndexChanged.connect(self.update) proxy = self.my_scene.addWidget(self.c_box) x = self.size_pie.width()+ self.space proxy.setPos(QPointF(x,self.margin['top'])) self.initialize = True self.data = list_items self.update() def update(self): restant = 360 i = 0 #self.my_scene.clear() for item in self.pie_items : self.my_scene.removeItem(item) self.pie_items = [] self.scene().setSceneRect(QRectF(0,0,self.frameSize().width(),self.frameSize().height())) #print ('size',self.scene().sceneRect()) #proxy = QGraphicsProxyWidget () for item in self.data : if (i==len(self.data)-1 ): angle = restant else: try : angle = int(360*item[self.c_box.currentText()]/self.total_value[self.c_box.currentText()]) except ZeroDivisionError: angle = 0 ellipse = Section(0,0,self.size_pie.width(),self.size_pie.height()) y = (self.parent().size().height()-self.size_pie.height())/2.0 x_pie = ((self.parent().size().width()/2.0)-self.size_pie.height())/2.0 ellipse.setPos(x_pie,y) ellipse.setStartAngle(16*(360-restant)) ellipse.setSpanAngle(angle*16) ellipse.setBrush(item['color']) self.my_scene.addItem(ellipse) self.pie_items.append(ellipse) # text pourcentage a afficher dans les portions de disque try : v = (item[self.c_box.currentText()]/self.total_value[self.c_box.currentText()])*100 except ZeroDivisionError : v = 0 text = QGraphicsSimpleTextItem("{0:5.2f}".format(v)+"%") trans = QTransform().translate(x_pie+self.size_pie.width()/2.0,y+self.size_pie.height()/2.0).rotate(((360-restant)+angle/2.0)*-1) pts = trans.map(QPointF(self.size_pie.width()/3.0,0)) text.setPos(pts.x(),pts.y()) self.my_scene.addItem(text) self.pie_items.append(text) #libelle rect = QGraphicsRectItem(0,0,10,10) x = x_pie + self.size_pie.width()+ self.space interval_height = (self.parent().size().height()-self.margin['top']-self.margin['bottom'])/(len(self.data)+1) rect.setPos(QPointF(x,self.margin['top']+((i+1)*interval_height))) rect.setBrush(item['color']) self.my_scene.addItem(rect) self.pie_items.append(rect) text = QGraphicsSimpleTextItem(item['label']+ " ("+str(int(item[self.c_box.currentText()]))+")") pts = rect.pos() transform = QTransform().translate(30, 0) pts = transform.map(pts) text.setPos(pts) self.my_scene.addItem(text) self.pie_items.append(text) restant = restant - angle i +=1 # self.fitInView(self.scene.sceneRect())
class CardView(QGraphicsView): def __init__(self, parent=None): super(CardView, self).__init__(parent) self.scene = QGraphicsScene() self.setScene(self.scene) self.setMinimumWidth(400) self.cards = [] def show_cards(self): for card in self.cards: card.show_face() def hide_cards(self): for card in self.cards: card.show_back() def deselect_all(self): for card in self.cards: if card.card.selected: card.deselect() def auto_select(self): #for AI card selection for card in self.cards: if card.card.selected: card.show_face() card.select() def add_card(self, card): new_card = CardGraphicsItem(card) self.cards.append(new_card) mv = 90*(len(self.cards)-1) new_card.moveBy(mv,0) self.scene.addItem(new_card) self.scene.update() if len(self.cards) > 10: for card in self.cards: card.setScale(0.1) self.reset_bounds() def del_cards(self, cards_to_keep): for c in self.cards: if c.card not in cards_to_keep: self.remove_card_from_scene(c) c.card.deselect() self.cards[:] = [c for c in self.cards if c.card in cards_to_keep] self.reset_bounds() def remove_card_from_scene(self, card): self.scene.removeItem(card) for other in self.cards[self.cards.index(card):]: other.moveBy(-90,0) self.reset_bounds() def update_scene(self, current_cards): self.del_cards(current_cards) for c in current_cards: if c not in [ca.card for ca in self.cards]: self.add_card(c) def reset_bounds(self): #ensures cards are neatly displayed in view bounds = self.scene.itemsBoundingRect() self.scene.setSceneRect(bounds)
class CameraInspectorCore(QGraphicsView): _w = setting.camera_resolution[0] _h = setting.camera_resolution[1] def __init__(self): super().__init__() self._zoom = 0 self._scene = None self._image = None self._last_rect = None self._info = None self._require_fit_map = False self._disconnect_map = None self._loading_map = None self._setup_ui() def _generate_loading_map(self): text = 'Loading...' pixmap = QPixmap(self._w, self._h) pixmap.fill(self.palette().dark().color()) painter = QPainter(pixmap) painter.setRenderHint(QPainter.TextAntialiasing) font = QFont() font.setPixelSize(60) painter.setFont(font) painter.setPen(self.palette().text().color()) text_size = painter.fontMetrics().size(0, text) painter.drawText((self._w - text_size.width()) / 2, (self._h - text_size.height()) / 2, text_size.width(), text_size.height(), Qt.AlignCenter, text) painter.end() return pixmap def _setup_ui(self): self.setRenderHint(QPainter.Antialiasing) self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse) self.setResizeAnchor(QGraphicsView.AnchorUnderMouse) self.setFrameShape(QFrame.NoFrame) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self._loading_map = self._generate_loading_map() self._scene = QGraphicsScene(self) self._scene.setBackgroundBrush(QBrush(self.palette().dark().color())) self._image = QGraphicsPixmapItem() self._scene.addItem(self._image) # hud hud_color = self.palette().highlight().color().lighter(150) self._overlay = self._scene.addEllipse(QRectF(), QPen(hud_color, 4), QBrush(Qt.NoBrush)) self._crosshair = self._scene.addEllipse(QRectF(), QPen(hud_color, 2), QBrush(Qt.NoBrush)) rect = QRectF(0, 0, self._w, self._h) cx = rect.center().x() cy = rect.center().y() r = min(rect.width(), rect.height()) * 0.7 self._overlay.setRect(QRectF(cx - r / 2, cy - r / 2, r, r)) rc = min(rect.width(), rect.height()) * 0.05 self._crosshair.setRect(QRectF(cx - rc / 2, cy - rc / 2, rc, rc)) self._overlay.setVisible(False) self._crosshair.setVisible(False) # scene self.setScene(self._scene) self._info = CameraInspectorInfo() self.setLayout(self._info) def toggle_overlay(self, toggle): self._overlay.setVisible(toggle) self._crosshair.setVisible(toggle) def change_camera(self, serial): self._info.update_info(serial=serial) self.set_map(self._loading_map) self._require_fit_map = True def set_map(self, pixmap): if pixmap and pixmap.width() == self._w: self._image.setPixmap(pixmap) if self._require_fit_map: self._fit_map() self._require_fit_map = False else: self._image.setPixmap(self._loading_map) if self._zoom != 0: self._fit_map() def _fit_map(self): self.setDragMode(QGraphicsView.NoDrag) rect = QRectF(self._image.pixmap().rect()) self.setSceneRect(rect) # 取得目前 scale,歸回 scale 1 m = self.transform().mapRect(QRectF(0, 0, 1, 1)) self.scale(1 / m.width(), 1 / m.height()) # 縮放成適合圖像大小 scenerect = self.transform().mapRect(rect) factor = min(self.width() / scenerect.width(), self.height() / scenerect.height()) self.scale(factor, factor) self._zoom = 0 self._info.update_info(zoom=self.transform().m11()) def wheelEvent(self, event): if event.angleDelta().y() > 0: factor = 1.25 self._zoom += 1 else: factor = 0.8 self._zoom -= 1 if self._zoom > 0: self.scale(factor, factor) self.setDragMode(QGraphicsView.ScrollHandDrag) elif self._zoom == 0: self._fit_map() else: self._zoom = 0 self._info.update_info(zoom=self.transform().m11()) def resizeEvent(self, event): if not self.isVisible(): return if self._zoom == 0: if not self._image.pixmap().isNull(): self._fit_map() elif self._last_rect is not None: la = self._last_rect.center() c = self.rect().center() self.translate(c.x() - la.x(), c.y() - la.y()) self._last_rect = self.rect()
class GUI(QtWidgets.QMainWindow): def __init__(self,game): super().__init__() self.setCentralWidget(QtWidgets.QWidget()) # QMainWindown must have a centralWidget to be able to add layouts self.horizontal = QtWidgets.QHBoxLayout() # Horizontal main layout self.centralWidget().setLayout(self.horizontal) self.btn1 = QPushButton("New wave",self) self.btn1.move(660,600) self.btn1.clicked.connect(self.activateWave) items = ("level1", "level2", "level3", "level4","level5") item, ok = QInputDialog.getItem(self, "Select level","levels", items, 0, False) self.game = game self.game.loadMap(item) #self.start.addButton(QAbstractButton, QMessageBox_ButtonRole) self.map = game.map.map self.initWindow() self.loadTextures() self.drawMap() self.drawHp() self.squares() self.placeButtons() self.drawPlayerStats() self.drawTowerPrices() self.updateGame() self.placeTower = 0 # 0 for not active, 1 for arrow, 2 for gun and 3 for laser self.timer = QtCore.QTimer() self.timer.start(50) # Milliseconds self.timer.timeout.connect(self.updateGame) def initWindow(self): self.setGeometry(40, 40, 800, 640) self.setWindowTitle('Tower Defense') #self.setFixedSize(self.size()) self.show() self.showFullScreen() # Add a scene for drawing 2d objects self.scene = QGraphicsScene(self) self.scene.setSceneRect(0, 0, 640, 640) # Add a view for showing the scene self.view = QGraphicsView(self.scene, self) self.view.show() self.view.setAlignment(QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft) self.view.adjustSize() self.horizontal.addWidget(self.view) def closeGui(self): self.close() def drawMap(self): dark = QColor(20,20,20) light = QColor(211,211,211) brush = QBrush() pen = QPen() pen.setColor(QColor(0, 0, 0, 75)) # Create graphic objects for map squares for y in range(0,len(self.map)): for x in range(0,len(self.map[y])): shape = QGraphicsRectItem(self.map[y][x].size*x,self.map[y][x].size*y,self.map[y][x].size,self.map[y][x].size) if self.map[y][x].isWall: brush.setTexture(self.grass) else: brush.setTexture(self.gravel) self.map[y][x].shape = shape shape.setPen(pen) shape.setBrush(brush) self.scene.addItem(shape) def loadTextures(self): self.grass = QPixmap("grass.png") self.gravel = QPixmap("gravel2.png") self.arrow = QPixmap("arrow.png") self.kappa = QPixmap("kappa.png") self.miro = QPixmap("pikkumiro.png") self.heart = QPixmap("heart.png") def visualizeBfs(self): shape = QGraphicsRectItem(self.square.size*self.square.coord.x,self.square.size*self.square.coord.y, self.square.size,self.square.size) shape.setBrush(QColor(0,255,0)) self.scene.addItem(shape) self.update() self.square = self.square.cameFrom def drawEnemies(self): # Go through enemies in the game and draw them for enemy in self.game.enemies: if enemy.shape == None: if enemy.etype == 0: shape = QGraphicsEllipseItem(0,0,20,20) enemy.r = 10 elif enemy.etype == 1: shape = QGraphicsEllipseItem(0,0,30,30) enemy.r = 15 elif enemy.etype == 2: shape = QGraphicsEllipseItem(0,0,40,40) enemy.r = 20 shape.setBrush(QColor(0,255,0)) shape.setX(enemy.x) shape.setY(enemy.y) self.scene.addItem(shape) enemy.shape = shape ratio = enemy.hp/enemy.maxHp enemy.shape.setBrush(QColor((1-ratio)*255,ratio*255,0)) enemy.shape.setX(enemy.x-enemy.r) enemy.shape.setY(enemy.y-enemy.r) if enemy.shape.collidesWithItem(self.game.endSquare.shape): if self.game.player.lose == False: self.game.player.hp -= 1 self.scene.removeItem(self.health.pop()) enemy.goal=True if enemy.dead: if enemy.etype == 0: self.game.player.getGold(10) self.game.player.getPoints(100) elif enemy.etype == 1: self.game.player.getGold(50) self.game.player.getPoints(500) elif enemy.etype == 2: self.game.player.getGold(120) self.game.player.getPoints(1000) if enemy.dead or enemy.goal: self.scene.removeItem(enemy.shape) self.game.enemies.remove(enemy) self.update() def drawTowers(self): for tower in self.game.towers: if tower.shape == None: shape = QGraphicsRectItem(0,0,40,40) shape.setX(tower.x) shape.setY(tower.y) if tower.ttype == 1: shape.setBrush(QColor(120,100,20)) elif tower.ttype == 2: shape.setBrush(QColor(70,70,50)) elif tower.ttype ==3: shape.setBrush(QColor(20,20,20)) self.scene.addItem(shape) tower.shape = shape #r = QGraphicsEllipseItem(tower.x+20-tower.range,tower.y+20-tower.range,tower.range*2,tower.range*2) #self.scene.addItem(r) if tower.ttype == 3: for enemy in self.game.enemies: if tower.canFire(enemy): if enemy in tower.firesAt: i = tower.firesAt.index(enemy) tower.lines[i].setLine(tower.x+20,tower.y+20,enemy.x,enemy.y) else: line = QGraphicsLineItem(tower.x+20,tower.y+20,enemy.x,enemy.y) line.setPen(QColor(255,0,0)) self.scene.addItem(line) tower.firesAt.append(enemy) tower.lines.append(line) enemy.hp -= tower.damage if enemy.hp <= 0: i = tower.firesAt.index(enemy) self.scene.removeItem(tower.lines[i]) else: if enemy in tower.firesAt: i = tower.firesAt.index(enemy) self.scene.removeItem(tower.lines[i]) def drawProjectiles(self): for projectile in self.game.projectiles: if projectile.shape == None: if projectile.ptype == 1: shape = QGraphicsPixmapItem(self.arrow) elif projectile.ptype == 2: shape = QGraphicsEllipseItem(0,0,5,5) shape.setBrush(QColor(0,0,0)) shape.setX(projectile.x) shape.setY(projectile.y) self.scene.addItem(shape) projectile.shape = shape projectile.shape.setX(projectile.x) projectile.shape.setY(projectile.y) if projectile.ptype == 1: projectile.shape.setRotation(projectile.rotation) if projectile.shape.collidesWithItem(projectile.enemy.shape): projectile.enemy.getHit(projectile.damage) self.scene.removeItem(projectile.shape) self.game.projectiles.remove(projectile) self.update() def updateGame(self): self.game.updateAll() self.checkButtons() self.drawEnemies() self.drawTowers() self.drawProjectiles() self.checkGameOver() self.updatePlayerStats() if self.game.over: box = QMessageBox() box.setWindowTitle("Game Over") box.setText("You got "+str(self.game.player.points)+" points.") box.setStandardButtons(QMessageBox.Ok) box.buttonClicked.connect(self.closeGui) retval = box.exec_() def mousePressEvent(self, event): if self.placeTower != 0: pos = event.pos() x = floor(pos.x()/self.game.squareSize) y = floor(pos.y()/self.game.squareSize) self.game.addTower(x,y,self.placeTower) self.placeTower = 0 def placeButtons(self): for b in self.game.buttons: self.scene.addItem(b) def checkButtons(self): for b in self.game.buttons: if b.activated and b.type == 1: self.placeTower = 1 elif b.activated and b.type ==2: self.placeTower = 2 elif b.activated and b.type == 3: self.placeTower = 3 if self.game.buttons[0].activated == False and self.game.buttons[1].activated == False and self.game.buttons[2].activated == False: self.placeTower = 0 def squares(self): shape2 = QGraphicsRectItem(0,0,40,40) shape2.setX(self.game.endSquare.x) shape2.setY(self.game.endSquare.y) shape2.setBrush(QColor(0,0,200,0)) shape2.setPen(QColor(0,0,0,0)) self.scene.addItem(shape2) self.game.endSquare.shape = shape2 self.update() def drawHp(self): self.health = [] hpcount = self.game.player.hp for i in range(0,hpcount): heart = QGraphicsPixmapItem(self.heart) heart.setY(660) heart.setX(20+i*50) self.scene.addItem(heart) self.health.append(heart) def activateWave(self): if not self.game.wave.active: self.game.wave.active = True self.game.wave.generateEnemies() def checkGameOver(self): if self.game.player.lose: self.game.over = True def drawPlayerStats(self): font = QFont("Arial",20) self.gold = QLabel("Gold: " + str(self.game.player.gold)) self.gold.setFont(font) self.gold.move(QPoint(545,660)) self.points = QLabel("Points: " + str(self.game.player.points)) self.points.setFont(font) self.points.move(QPoint(545,705)) self.wavenum = QLabel("Wave number: " + str(self.game.wave.level)) self.wavenum.setFont(font) self.wavenum.move(QPoint(20,705)) self.scene.addWidget(self.wavenum) self.scene.addWidget(self.gold) self.scene.addWidget(self.points) def updatePlayerStats(self): self.gold.setText("Gold: "+str(self.game.player.gold)) self.points.setText("Points: "+str(self.game.player.points)) self.wavenum.setText("Wave number: " + str(self.game.wave.level)) wg = self.gold.fontMetrics().width(self.gold.text()) wp = self.points.fontMetrics().width(self.points.text()) wn = self.wavenum.fontMetrics().width(self.wavenum.text()) self.wavenum.setFixedWidth(wn) self.gold.setFixedWidth(wg) self.points.setFixedWidth(wp) def drawTowerPrices(self): font = QFont("Arial",15) arrowtower = QLabel("50") arrowtower.setFont(font) arrowtower.move(QPoint(650,70)) self.scene.addWidget(arrowtower) guntower = QLabel("100") guntower.setFont(font) guntower.move(QPoint(650,150)) self.scene.addWidget(guntower) lasertower = QLabel("500") lasertower.setFont(font) lasertower.move(QPoint(650,230)) self.scene.addWidget(lasertower)
class HistoChart (QGraphicsView): def __init__(self,parent,size=QSize(400,400)): super(HistoChart,self).__init__(parent) self.my_scene = QGraphicsScene() #self.my_scene.setSceneRect(self.sceneRect()) self.setScene(self.my_scene) #self.setBackgroundBrush(QBrush(QtCore.Qt.black, QtCore.Qt.SolidPattern)); self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.size_text_width = -1 self.margin = {'top':10,'bottom':10,'left':10,'right':10} self.initialize = False self.max_length_text = 10 self.histo_items = [] def setData (self,list_items): size = min(self.frameSize().width()/2.0,self.frameSize().height()) self.size_pie = QSize(size,size) self.data = list_items if self.initialize == False: self.c_box = QComboBox() self.c_box.addItem("warrior") self.c_box.addItem("rank") self.c_box.addItem("power") self.c_box.currentIndexChanged.connect(self.update) proxy = self.my_scene.addWidget(self.c_box) proxy.setPos(QPointF(0.0,self.margin['top'])) self.margin['top']+=self.c_box.height() self.initialize = True self.update() def update(self): i = 0 #self.my_scene.clear() for item in self.histo_items : self.my_scene.removeItem(item) self.histo_items = [] self.scene().setSceneRect(QRectF(0,0,self.frameSize().width(),self.frameSize().height())) max = -1 for value in self.data.values(): if value[self.c_box.currentText()] > max: max = value[self.c_box.currentText()] size_text_number = QGraphicsTextItem(str(max)).boundingRect().width()+10 interval= self.parent().size().height()-self.margin['top']-self.margin['bottom'] interval = interval /len(self.data) temp = "aaaaaaaaaaaaaaaaa" if len(temp)> self.max_length_text: data = temp[:self.max_length_text]+"." else : data = temp self.size_text_width = QGraphicsTextItem(data).boundingRect().width()+10 #print ('width:',self.size_text_width) horizontal_size = self.parent().size().width()- self.margin['left']- self.margin['right']-self.size_text_width- size_text_number try: ratio = horizontal_size/ max except ZeroDivisionError : ratio = 0 i = 0 for groupe,value in zip(self.data.keys(),self.data.values()) : if self.c_box.currentText() == "warrior": title_str = 'Nombre de Heros' bar_all = QGraphicsRectItem(0,self.margin['top'],value['warrior']*ratio,interval*0.8) bar_all.setPos(self.size_text_width,interval*0.2+(i*interval)) gradient = QLinearGradient(QPointF(bar_all.rect().width()/2,0),QPointF(bar_all.rect().width()/2,bar_all.rect().height()+self.margin['top'])) gradient.setColorAt(0,QColor('white')) gradient.setColorAt(1,QColor('red')) brush = QBrush(gradient) #brush.setTexture(QPixmap(":/textures/"+groupe.attribs['color'])) bar_all.setBrush(brush) self.my_scene.addItem(bar_all) self.histo_items.append(bar_all) bar_alive = QGraphicsRectItem(0,self.margin['top'],value['alive']*ratio,interval*0.8) bar_alive.setPos(self.size_text_width,interval*0.2+(i*interval)) gradient = QLinearGradient(QPointF(bar_alive.rect().width()/2,0),QPointF(bar_alive.rect().width()/2,bar_alive.rect().height()+self.margin['top'])) # gradient.setStart(QPointF(0.5,0)) # gradient.setStop(QPointF(0.5,1)) gradient.setColorAt(0,QColor('white')) gradient.setColorAt(1,QColor('green')) brush = QBrush(gradient) bar_alive.setBrush(brush) self.my_scene.addItem(bar_alive) self.histo_items.append(bar_alive) text_nb_warriors= QGraphicsTextItem(str(value['warrior'])) text_nb_warriors.setDefaultTextColor(QColor('green')) trans = QTransform().translate(bar_all.pos().x()+bar_all.rect().width()+10,interval*0.2+(i*interval)+self.margin['top']) pts = trans.map(QPointF(0,0.0)) text_nb_warriors.setPos(pts.x(),pts.y()) self.my_scene.addItem(text_nb_warriors) self.histo_items.append(text_nb_warriors) #bar ranl elif self.c_box.currentText()== "rank": title_str = "Rank Moyen" bar_rank = QGraphicsRectItem(0,self.margin['top'],value['rank']*ratio,interval*0.8) bar_rank.setPos(self.size_text_width, interval*0.2+(i*interval)) gradient = QLinearGradient(QPointF(bar_rank.rect().width()/2,0),QPointF(bar_rank.rect().width()/2,bar_rank.rect().height()+self.margin['top'])) # gradient.setStart(QPointF(0.5,0)) # gradient.setStop(QPointF(0.5,1)) gradient.setColorAt(0,QColor('white')) gradient.setColorAt(1,QColor('red')) brush = QBrush(gradient) bar_rank.setBrush(brush) self.my_scene.addItem(bar_rank) self.histo_items.append(bar_rank) # value text_rank = QGraphicsTextItem("{0:1.1f}".format(value['rank'])) text_rank.setDefaultTextColor(QColor('red')) trans = QTransform().translate(bar_rank.pos().x()+bar_rank.rect().width()+10,interval*0.2+(i*interval)+self.margin['top']) pts = trans.map(QPointF(0,0.0)) text_rank.setPos(pts.x(),pts.y()) self.my_scene.addItem(text_rank) self.histo_items.append(text_rank) else: title_str = "Puissance" bar_rank = QGraphicsRectItem(0,self.margin['top'],value['power']*ratio,interval*0.8) bar_rank.setPos(self.size_text_width, interval*0.2+(i*interval)) gradient = QLinearGradient(QPointF(bar_rank.rect().width()/2,0),QPointF(bar_rank.rect().width()/2,bar_rank.rect().height()+self.margin['top'])) # gradient.setStart(QPointF(0.5,0)) # gradient.setStop(QPointF(0.5,1)) gradient.setColorAt(0,QColor('white')) gradient.setColorAt(1,QColor('blue')) brush = QBrush(gradient) bar_rank.setBrush(brush) self.my_scene.addItem(bar_rank) self.histo_items.append(bar_rank) # value try : valeur = (value['power']/max)*100 except ZeroDivisionError : valeur = 0 text_rank = QGraphicsTextItem("{0:1.1f}".format(valeur)) text_rank.setDefaultTextColor(QColor('blue')) trans = QTransform().translate(bar_rank.pos().x()+bar_rank.rect().width()+10,interval*0.2+(i*interval)+self.margin['top']) pts = trans.map(QPointF(0,0.0)) text_rank.setPos(pts.x(),pts.y()) self.my_scene.addItem(text_rank) self.histo_items.append(text_rank) #dessin du titre title = QGraphicsTextItem(title_str) title.setPos(self.margin['left']+self.size_text_width+horizontal_size/2.0,self.c_box.pos().y()) self.my_scene.addItem(title) self.histo_items.append(title) #affichage des label colonne de gauche if len(groupe.name)> self.max_length_text: data = groupe.name[:self.max_length_text]+".." else : data = groupe.name text = QGraphicsTextItem(data) #text.setTextWidth(20) trans = QTransform().translate(self.margin['left'],interval*0.2+(i*interval)+self.margin['top']) pts = trans.map(QPointF(0,0.0)) text.setPos(pts.x(),pts.y()) self.my_scene.addItem(text) self.histo_items.append(text) i +=1 # self.fitInView(self.scene.sceneRect())