Hi,
Sorry, I was not sure about attaching files. Here are the files.

Aysun

On Tue, May 13, 2008 at 2:34 PM, Hans Meine <[EMAIL PROTECTED]>
wrote:

> On Dienstag 13 Mai 2008, Aysun Bascetincelik wrote:
> > Here is the code:
> > __________________________
> >
> >
> > #!/usr/bin/env python
> [snip]
>
> I don't know what the policy for this list is, but if you had attached the
> file (and the needed .ui) file, we could have tried your code out.  As it
> is
> now, the indentation is totally broken (and the graphicsForm is missing).
>
> Ciao, /  /                                                    .o.
>     /--/                                                     ..o
>    /  / ANS                                                  ooo
>
> _______________________________________________
> PyQt mailing list    PyQt@riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'graphicsForm.ui'
#
# Created: Tue May 13 12:57:46 2008
#      by: PyQt4 UI code generator 4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(QtCore.QSize(QtCore.QRect(0,0,632,444).size()).expandedTo(MainWindow.minimumSizeHint()))

        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.graphicsView = QtGui.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(0,0,500,400))
        self.graphicsView.setMaximumSize(QtCore.QSize(500,400))
        self.graphicsView.setBaseSize(QtCore.QSize(500,400))
        self.graphicsView.setObjectName("graphicsView")

        self.pointButton = QtGui.QToolButton(self.centralwidget)
        self.pointButton.setGeometry(QtCore.QRect(540,10,50,26))
        self.pointButton.setCheckable(True)
        self.pointButton.setChecked(False)
        self.pointButton.setObjectName("pointButton")

        self.delButton = QtGui.QToolButton(self.centralwidget)
        self.delButton.setGeometry(QtCore.QRect(540,110,51,26))
        self.delButton.setObjectName("delButton")

        self.pathButton = QtGui.QToolButton(self.centralwidget)
        self.pathButton.setGeometry(QtCore.QRect(540,40,50,26))
        self.pathButton.setCheckable(True)
        self.pathButton.setChecked(False)
        self.pathButton.setObjectName("pathButton")

        self.selectButton = QtGui.QToolButton(self.centralwidget)
        self.selectButton.setGeometry(QtCore.QRect(540,80,50,26))
        self.selectButton.setCheckable(True)
        self.selectButton.setChecked(False)
        self.selectButton.setObjectName("selectButton")
        MainWindow.setCentralWidget(self.centralwidget)

        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0,0,632,25))
        self.menubar.setObjectName("menubar")

        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        MainWindow.setMenuBar(self.menubar)

        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuFile.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pointButton.setText(QtGui.QApplication.translate("MainWindow", "Point", None, QtGui.QApplication.UnicodeUTF8))
        self.delButton.setText(QtGui.QApplication.translate("MainWindow", "Delete", None, QtGui.QApplication.UnicodeUTF8))
        self.pathButton.setText(QtGui.QApplication.translate("MainWindow", "Path", None, QtGui.QApplication.UnicodeUTF8))
        self.selectButton.setText(QtGui.QApplication.translate("MainWindow", "Select", None, QtGui.QApplication.UnicodeUTF8))
        self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))

Attachment: graphicsForm.ui
Description: application/designer

#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui
from graphicsForm import Ui_MainWindow

class DiagramScene(QtGui.QGraphicsScene):

    def __init__(self, graphicsView, parent=None):
        QtGui.QGraphicsScene.__init__(self, QtCore.QRectF(QtCore.QPointF(0,0), QtCore.QPointF(450,350)), parent)
	self.itemToAdd = 0
	self.mode = 0 # 0: add mode, 1:select mode
	self.view = graphicsView	

    def mousePressEvent(self, mouseEvent):
        if (mouseEvent.button() != QtCore.Qt.LeftButton):
            return
	x = mouseEvent.scenePos().x()
	y = mouseEvent.scenePos().y()
	
	if(self.mode == 0):
	    self.createItem(x, y) 
	elif(self.mode == 1):
	    self.selectItem(x, y)
        QtGui.QGraphicsScene.mousePressEvent(self, mouseEvent)

    def createItem(self, x, y):
	if (self.itemToAdd == 0):
	    self.addItem(MyPoint(x, y))
	elif (self.itemToAdd == 1):
	    self.addLine(QtCore.QLineF(x,y,x+50,y+50))
	self.update()
#	self.view.update()
#	self.view.show()

    def selectItem(self, x, y):
	selected = self.itemAt(x,y)
	print selected
	if selected:
	    selected.setSelected(True)

class StartProgram(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
	self.scene = DiagramScene(self.ui.graphicsView)
	self.ui.graphicsView.setScene(self.scene)
	self.ui.graphicsView.setInteractive(True)
	self.ui.pointButton.setChecked(True)
	self.ui.pathButton.setChecked(False)

	QtCore.QObject.connect(self.ui.pointButton,QtCore.SIGNAL("clicked()"), self.pointButtonClicked)
	QtCore.QObject.connect(self.ui.pathButton,QtCore.SIGNAL("clicked()"), self.pathButtonClicked)
	QtCore.QObject.connect(self.ui.selectButton,QtCore.SIGNAL("clicked()"), self.selectButtonClicked)

    def pointButtonClicked(self):
	self.ui.pointButton.setChecked(True)
	self.ui.pathButton.setChecked(False)
	self.scene.itemToAdd = 0 #0 is point

    def pathButtonClicked(self):
	self.ui.pathButton.setChecked(True)
	self.ui.pointButton.setChecked(False)
	self.scene.itemToAdd = 1 #1 is path

    def selectButtonClicked(self):
	if self.ui.selectButton.isChecked():
	    self.scene.mode = 1
	else :
	    self.scene.mode = 0

class MyPoint(QtGui.QGraphicsPathItem):
    pointToDraw = QtCore.QRectF()    

    def __init__(self, x, y, parent=None, scene=None):
  	super(MyPoint, self).__init__()
	self.x = x
	self.y = y
	MyPoint.pointToDraw.setCoords(x,y, x+15,y+15)
	self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
	self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)

    def paint(self, painter, option, widget=None):	
	if self.isSelected():
	    painter.setBrush(QtGui.QBrush(QtGui.QColor(0,255,255)))
	else:
 	    painter.setBrush(QtGui.QBrush(QtGui.QColor(0,0,255)))
	painter.drawRect(MyPoint.pointToDraw)

    def path(self):	
	path = QtGui.QPainterPath()
	path.addEllipse(MyPoint.pointToDraw) #to add ellipse?!?!?!?!?
	return path

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartProgram()
    myapp.show()
    sys.exit(app.exec_())


_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to