#! /usr/bin/env python # This software is distributed according to the terms of the Gnu General # Public License. See http://www.gnu.org for more information. This software # comes with no warranty whatsoever, implied or otherwise. import sys from qt import * from qtcanvas import * #------------------------------------------------------------------------------# class CanvasWidget(QWidget): def __init__(self, *args): apply(QWidget.__init__, (self, ) + args) self.canvas = Canvas(self) self.canvas.resize(555, 553) self.view = CanvasView(self.canvas, self) self.view.resize(559, 557) #------------------------------------------------------------------------------# class Canvas(QCanvas): def __init__(self, *args): apply(QCanvas.__init__, (self, ) + args) self.setBackgroundPixmap(QPixmap("satellite.jpg")) #------------------------------------------------------------------------------# class CanvasView(QCanvasView): def __init__(self, *args): apply(QCanvasView.__init__, (self, ) + args) self.rectangles = [] self.pen = QPen(Qt.red, 1) self.selRect = None self.maxRect = None def contentsMousePressEvent(self, ev): colliders = self.canvas().collisions(ev.pos()) if (len(colliders) > 0): self.selectRect(colliders[0]) else: # to prevent overlapping slices, we define a rectangular area whose # edges are the farthest the new rectangle can extend in any # direction. the area contains the clicked point and is defined by # the edges of all existing slices. maxRect = QCanvasRectangle(0, 0, self.canvas().width(), self.canvas().height(), self.canvas()) # for rect in self.rectangles: self.selRect = None self.newRect = QCanvasRectangle(self.canvas()) self.newRect.move(ev.x(), ev.y()) self.newRect.setPen(self.pen) def contentsMouseMoveEvent(self, ev): # don't draw if click is in an existing rectangle if (self.selRect is not None): return # everything's kosher; draw the rectangle w = ev.x() - self.newRect.x() h = ev.y() - self.newRect.y() self.newRect.setSize(w, h) self.rectangles.append(self.newRect) self.newRect.show() self.canvas().update() def selectRect(self, rect): self.selRect = rect #------------------------------------------------------------------------------# def main(args): app = QApplication(args) cw = CanvasWidget() cw.resize(700, 650) app.setMainWidget(cw) cw.show() app.exec_loop() #------------------------------------------------------------------------------# main(sys.argv)