def __init__(self, parent): QGraphicsScene.__init__(self, parent) self.zScale = 100.0 # width (in pixels) of the bar self.barWidth = 20 # offset from the left border self.xOffset = 10 self.clear()
def __init__(self, yTitle, parent): QGraphicsScene.__init__(self, parent) # width of the scale bar fm = QFontMetrics(QFont()) # width = size of "100.0" with the default font + 10% self.yTitle = yTitle self.barWidth = max(fm.width(yTitle), fm.width("000.00")) self.xOffset = self.barWidth self.yOffset = fm.height() * 2 # define the transformation between distance, altitude and scene coordinates self.xRatio = 1.0 self.yRatio = 1.0 self.marker = PointMarker(self) self.clear()
def __init__(self, x, y, w, h): QGraphicsScene.__init__(self, x, y, w, h)
def __init__(self, model, parent): QGraphicsScene.__init__(self, parent) tables_coords = [] spiral = spiral_iterator() min_grid_x = None max_grid_x = None min_grid_y = None max_grid_y = None self.table_items = {} for table_name, table in model.tables().items(): # widget, grid_x, grid_y, x, y grid_x, grid_y = next(spiral) if grid_x > max_grid_x or max_grid_x is None: max_grid_x = grid_x if grid_x < min_grid_x or min_grid_x is None: min_grid_x = grid_x if grid_y > max_grid_y or max_grid_y is None: max_grid_y = grid_y if grid_y < min_grid_y or min_grid_y is None: min_grid_y = grid_y w = TableWidget(table) w.linkActivated.connect(self.tableSelected) tw = self.addWidget(w) tables_coords.append((tw, grid_x, grid_y, 0, 0, table_name)) self.table_items[table_name] = tw # resolve columns / rows size column_width = {} row_height = {} for table, grid_x, grid_y, x, y, _ in tables_coords: wmax = column_width.get(grid_x) hmax = row_height.get(grid_y) wsize = table.widget().size() if wsize.width() > wmax or wmax is None: column_width[grid_x] = wsize.width() if wsize.height() > hmax or hmax is None: row_height[grid_y] = wsize.height() # total_width = sum(column_width.values()) # total_height = sum(row_height.values()) # resolve coordinates column_x = {} row_y = {} w = 0 for x in range(min_grid_x, max_grid_x + 1): column_x[x] = w w += column_width[x] h = 0 for y in range(min_grid_y, max_grid_y + 1): row_y[y] = h h += row_height[y] table_pos = {} for i in range(len(tables_coords)): table, grid_x, grid_y, _, _, table_name = tables_coords[i] wsize = table.widget().size() x = column_x[grid_x] + (column_width[grid_x] - wsize.width()) / 2.0 y = row_y[grid_y] + (row_height[grid_y] - wsize.height()) / 2.0 tables_coords[i] = (table, x, y) table_pos[table_name] = (x, y, wsize.width(), wsize.height(), table) table.setPos(x, y) def distance(p1, p2): l = QLineF(p1, p2) return l.length() # display links arrows def add_link(table_name, table_name2, y_idx): tx, ty, tw, th, table = table_pos[table_name] tx2, ty2, tw2, th2, _ = table_pos[table_name2] (ax, ay, aw, ah) = table.widget().attributeCoords(y_idx) l1 = QLineF(ax - 3, ay + ah / 2.0, tx2 + tw2 / 2.0, ty2 + th2 / 2.0) l2 = QLineF(ax + aw + 3, ay + ah / 2.0, tx2 + tw2 / 2.0, ty2 + th2 / 2.0) if l1.length() < l2.length(): p1 = l1.p1() l = l1 else: p1 = l2.p1() l = l2 # add a diamond ds = 6 dbrush = QBrush(QColor("black")) diamond = QPolygonF( [ p1 + QPointF(0, ds), p1 + QPointF(ds, 0), p1 + QPointF(0, -ds), p1 + QPointF(-ds, 0), p1 + QPointF(0, ds), ] ) i1 = self.addPolygon(diamond, QPen(), dbrush) # cross with the second table widget points = [ horizontal_intersection(l, ty2, tx2, tx2 + tw2), horizontal_intersection(l, ty2 + th2, tx2, tx2 + tw2), vertical_intersection(l, tx2, ty2, ty2 + th2), vertical_intersection(l, tx2 + tw2, ty2, ty2 + th2), ] pp = [p for p in points if p is not None] p2 = min(pp, key=lambda p: distance(p1, p)) l = QLineF(p1, p2) i2 = self.addLine(l) i2.setZValue(1) alpha = math.atan2(p2.y() - p1.y(), p2.x() - p1.x()) alpha1 = alpha + 30.0 / 180.0 * math.pi alpha2 = alpha - 30.0 / 180.0 * math.pi r = -10 p3 = QPointF(r * math.cos(alpha1), r * math.sin(alpha1)) + p2 p4 = QPointF(r * math.cos(alpha2), r * math.sin(alpha2)) + p2 i3 = self.addLine(QLineF(p2, p3)) i4 = self.addLine(QLineF(p2, p4)) return [i1, i2, i3, i4] for table_name, table in model.tables().items(): tw = self.table_items[table_name] y = len(table.columns()) items = [tw] for l in table.links(): if l.max_occurs() != 1: continue items += add_link(table_name, l.ref_table().name(), y) y += 1 for l in table.back_links(): items += add_link(table_name, l.ref_table().name(), y) y += 1 self.table_items[table_name] = items # make items invisible for now for item in items[1:]: disable_link_item(item)