Source code for gwa.code.draw

#!/usr/bin/python
# -*- coding: utf-8 -*-

import DBA
import cairo

[docs]class Draw: """ Graphical representation of measurement campaign : arguments: - Id_campagne (integer) Choice of the campaign on which the drawing will be done - File (type string) Selecting the file name that will be created : arguments optional: - Resolution (integer) Choosing the resolution of the draw file ** Example ** >>> id_campaign = 52 >>> File = 'test.png' draw.Draw (id_campaign 'test.png') """ def __init__(self, id_camp, file='temp.png',resolution=50, main=None): """ Initialization function """ self.file = file self.main_app=main #Information in database to create a drawing file self.db = DBA.DBConnect(main=self.main_app) id_place = self.db.select('campaign',\ ['id_place'],\ {'id_campaign':id_camp})[0][0] self.width, self.height = self.db.select('place',\ ['length', 'width'],\ {'id_place':id_place})[0] width_pix = self.width*resolution height_pix = self.height*resolution # Matrix initialization # Create a matrix that will allow to store information. # (0) no statement # (1) information recorded # (2) information being ignored self.matrix=[ [0]*(self.height+1) for i in range(self.width+1)] #File creation self.area = cairo.ImageSurface(cairo.FORMAT_ARGB32, width_pix, height_pix) self.context = cairo.Context(self.area) #White Background self.context.set_source_rgb(1, 1, 1) self.context.paint() self.lastCoor = (0,0) #Length definition for cross drawing # Circle's radius # Lines's width if self.width > self.height: self.lines_length = (float(resolution) / height_pix)*0.25 self.radius = self.lines_length*1.9 else: self.lines_length = (float(resolution) / width_pix)*0.25 self.radius = self.lines_length*1.9 self.largeur_ligne = self.lines_length*0.2 #Must be executed after the initialization of drawings variables self.crossDrawing((0,0)) self.area.write_to_png(self.file)
[docs] def updateDrawing(self, coorX, coorC='', color=True): """ Update drawing file representing movement Cross = position Cicle = old position Values = green circle Ignored values = red circle : arguments: - CoorX (type tuple) Tuple representing cross coordinates - CoorC (type tuple) Tuple representing circle coordinates - color (boolean) True => green circle, False => red circle ** Example ** >>> CoorX = (2.0) draw.Draw().UpdateDrawing (coorX) """ #File creation self.area = cairo.ImageSurface.create_from_png(self.file) self.context = cairo.Context(self.area) #If a circle is specified in arguments, we draw it, else we draw a cross if coorC is not None: # Matrix update and circle drawing x, y = coorC if color: self.matrix[x][y] = 1 self.circleDrawing((x,y),(0,1,0)) else: self.matrix[x][y] = 2 self.circleDrawing((x,y),(1,0,0)) #Drawing old point (cross position) x, y = self.lastCoor if self.matrix[x][y] == 0: self.circleDrawing((x,y),(1,1,1)) if self.matrix[x][y] == 1: self.circleDrawing((x,y),(0,1,0)) if self.matrix[x][y] == 2: self.circleDrawing((x,y),(1,0,0)) #Cross drawing (next measure) self.crossDrawing(coorX) #Save the cross position self.lastCoor = coorX #Drawing in file self.area.write_to_png(self.file)
[docs] def crossDrawing(self, coordinates): """ Drawing cross for next measure :Argument: coordinates (type tuple) Coordinates choice **example** >>> coordinates (3,1) draw.Drawing().crossDrawing(coordinates) """ #Save context self.context.save() #Reformat the matrix with file size self.context.scale(self.area.get_width(),self.area.get_height()) #Cross color definition self.context.set_source_rgb(0, 0, 0) #Length and width(0 and 1) if self.width > self.height: self.context.scale(self.height/float(self.width),1) y_scaled = (self.height-(self.height-coordinates[1])) / float(self.height) x_scaled = coordinates[0]/float(self.height) else: self.context.scale(1,float(self.width)/self.height) y_scaled = (self.width-(self.width-coordinates[1])) / float(self.width) x_scaled = coordinates[0]/float(self.width) #Line width self.context.set_line_width(self.largeur_ligne) #Horizontal line creation self.context.move_to(x_scaled-(self.lines_length),y_scaled) self.context.line_to(x_scaled+(self.lines_length),y_scaled) self.context.stroke() #Vertical line creation self.context.move_to(x_scaled,y_scaled-(self.lines_length)) self.context.line_to(x_scaled,y_scaled+(self.lines_length)) self.context.stroke() #Context loading self.context.restore()
[docs] def circleDrawing(self, coordinates, color_rgb): """ drawing circle :arguments: coordinates (type tuple) coordinates (x,y) color_rgb (type tuple) form: (red,green,blue). Colors values between 0.0 and 1.0 **example** >>> coordinates = (2,1) >>> color_rgb = (1,0,1) draw.Draw().circleDrawing(coordinates,color_rgb) """ #Save context and format matrix self.context.save() self.context.scale(self.area.get_width(),self.area.get_height()) #Length and width(0 and 1) if self.width > self.height: self.context.scale(self.height/float(self.width),1) y = (self.height-(self.height-coordinates[1])) / float(self.height) x = coordinates[0]/float(self.height) else: self.context.scale(1,float(self.width)/self.height) y = (self.width-(self.width-coordinates[1])) / float(self.width) x = coordinates[0]/float(self.width) #Two circle with differents radius and colors radial = cairo.RadialGradient(x, y, self.radius*0.90,x, y,self.radius ) radial.add_color_stop_rgba(0, 0, 0, 0, 1) radial.add_color_stop_rgba(1, 0, 0, 0, 0) red, green, blue = color_rgb self.context.set_source_rgb(red, green, blue) self.context.mask(radial) self.context.restore()
[docs] def getmatrix(self): """ get matrix :return: Return matrix (2 dimension) **example** >>> draw.Draw().getmatrix() """ return self.matrix
if __name__ == "__main__": '''a = Draw(52,'test1.png') b = Draw(53,'test2.png') a.UpdateDrawing((1,0)) a.UpdateDrawing((2,0))'''