mirror of
https://github.com/wassname/sloth.git
synced 2026-07-04 21:05:03 +08:00
Merge branch 'GraphicalObjects'
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.Qt import *
|
||||
|
||||
class AnnotationGraphicsItem(QAbstractGraphicsShapeItem):
|
||||
def __init__(self,controls_enabled=True, parent=None, **kwargs):
|
||||
super(AnnotationGraphicsItem, self).__init__(parent)
|
||||
|
||||
self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsMovable)
|
||||
|
||||
self.text_font_ = QFont()
|
||||
self.text_font_.setPointSize(4)
|
||||
self.text_item_ = QGraphicsSimpleTextItem()
|
||||
self.text_item_.setFont(self.text_font_)
|
||||
self.updateText()
|
||||
|
||||
def boundingRect(self):
|
||||
return QRectF(0, 0, 0, 0)
|
||||
|
||||
def updateText(self):
|
||||
self.text_item_.setText("CLASS")
|
||||
|
||||
def itemChange(self, change, value):
|
||||
if change == QGraphicsItem.ItemSelectedChange:
|
||||
self.setControlsVisible(value.toBool())
|
||||
return QGraphicsItem.itemChange(self, change, value)
|
||||
|
||||
def setControlsVisible(self, visible=True):
|
||||
self.controls_visible_ = visible
|
||||
#for corner in self.corner_items_:
|
||||
# corner.setVisible(self.controls_enabled_ and self.controls_visible_)
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.QtCore import *
|
||||
from pointitem import *
|
||||
import math
|
||||
|
||||
modes = range(5)
|
||||
SELECT, POINT, LINE, RECTANGLE, POLYGON = modes
|
||||
|
||||
class AnnotationScene(QGraphicsScene):
|
||||
def __init__(self, parent=None):
|
||||
super(AnnotationScene, self).__init__(parent)
|
||||
self.setBackgroundBrush(Qt.darkGray)
|
||||
|
||||
self.reset()
|
||||
self.setSceneRect(0,0, 640, 480)
|
||||
|
||||
self.mode_ = None
|
||||
self.mousePressed_ = None
|
||||
self.activeItem_ = None
|
||||
|
||||
def reset(self):
|
||||
self.clear()
|
||||
|
||||
def setMode(self, mode):
|
||||
assert (mode in modes)
|
||||
self.mode_ = mode
|
||||
print mode
|
||||
|
||||
|
||||
def mousePressEvent(self, event):
|
||||
if not self.sceneRect().contains(event.scenePos()):
|
||||
return
|
||||
elif self.mode_ == SELECT:
|
||||
QGraphicsScene.mousePressEvent(self, event)
|
||||
return
|
||||
elif self.mode_ == POINT:
|
||||
self.insertItem(AnnotationGraphicsPointItem(event.scenePos()))
|
||||
event.accept()
|
||||
elif self.mode_ == LINE:
|
||||
self.mousePressed_ = event.scenePos()
|
||||
line = QGraphicsLineItem(QLineF(event.scenePos(), event.scenePos()))
|
||||
self.activeItem_ = line
|
||||
self.addItem(line)
|
||||
|
||||
event.accept()
|
||||
self.update()
|
||||
|
||||
def mouseReleaseEvent(self, event):
|
||||
self.mousePressed_ = None
|
||||
QGraphicsScene.mouseReleaseEvent(self, event)
|
||||
|
||||
def mouseMoveEvent(self, event):
|
||||
print LINE
|
||||
if not self.sceneRect().contains(event.scenePos()):
|
||||
return
|
||||
elif self.mode_ == LINE:
|
||||
## TODO
|
||||
activeItem.
|
||||
else:
|
||||
QGraphicsScene.mouseMoveEvent(self, event)
|
||||
|
||||
def insertItem(self, item):
|
||||
self.addItem(item)
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.Qt import *
|
||||
from annotationitem import *
|
||||
|
||||
class AnnotationGraphicsLineItem(AnnotationGraphicsItem):
|
||||
|
||||
def __init__(self, pos, endPoint, parent=None):
|
||||
AnnotationGraphicsItem.__init__(self, False, parent)
|
||||
self.setPos(pos)
|
||||
self.endPoint_ = endPoint
|
||||
self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsMovable)
|
||||
self.setPen(QColor('green'))
|
||||
|
||||
def resizeContents(self, rect):
|
||||
pass
|
||||
|
||||
def boundingRect(self):
|
||||
width = abs(self.endPoint_.x() - self.pos().x())
|
||||
height = abs(self.endPoint_.y() - self.pos().y())
|
||||
return QRectF(-10, -10, width, height)
|
||||
|
||||
def paint(self, painter, option, widget = None):
|
||||
pen = self.pen()
|
||||
if self.isSelected():
|
||||
pen.setColor(QColor('red'))
|
||||
painter.setPen(pen)
|
||||
painter.drawLine(self.pos, self.endPoint_)
|
||||
|
||||
def itemChange(self, change, value):
|
||||
return AnnotationGraphicsItem.itemChange(self, change, value)
|
||||
|
||||
|
||||
|
||||
Executable
+68
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/python
|
||||
import sys, os
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.QtCore import *
|
||||
from annotationscene import *
|
||||
|
||||
class MainWindow(QMainWindow):
|
||||
def __init__(self, argv, parent=None):
|
||||
QMainWindow.__init__(self, parent)
|
||||
|
||||
vlayout = QVBoxLayout()
|
||||
buttonSelect = QPushButton("Select")
|
||||
buttonSelect.clicked.connect(self.clickedSelect)
|
||||
vlayout.addWidget(buttonSelect)
|
||||
buttonPoint = QPushButton("Point")
|
||||
buttonPoint.clicked.connect(self.clickedPoint)
|
||||
vlayout.addWidget(buttonPoint)
|
||||
buttonLine = QPushButton("Line")
|
||||
buttonLine.clicked.connect(self.clickedLine)
|
||||
vlayout.addWidget(buttonLine)
|
||||
buttonRectangle = QPushButton("Rectangle")
|
||||
buttonRectangle.clicked.connect(self.clickedRectangle)
|
||||
vlayout.addWidget(buttonRectangle)
|
||||
buttonPolygon = QPushButton("Polygon")
|
||||
buttonPolygon.clicked.connect(self.clickedPolygon)
|
||||
vlayout.addWidget(buttonPolygon)
|
||||
|
||||
|
||||
hlayout = QHBoxLayout()
|
||||
self.view_ = QGraphicsView()
|
||||
hlayout.addLayout(vlayout)
|
||||
hlayout.addWidget(self.view_, 1)
|
||||
|
||||
self.scene_ = AnnotationScene(self)
|
||||
self.view_.setScene(self.scene_)
|
||||
|
||||
central = QWidget()
|
||||
central.setLayout(hlayout)
|
||||
self.setCentralWidget(central)
|
||||
|
||||
def clickedSelect(self):
|
||||
self.scene_.setMode(SELECT)
|
||||
|
||||
def clickedPoint(self):
|
||||
self.scene_.setMode(POINT)
|
||||
|
||||
def clickedRectangle(self):
|
||||
self.scene_.setMode(RECTANGLE)
|
||||
|
||||
def clickedLine(self):
|
||||
self.scene_.setMode(LINE)
|
||||
|
||||
def clickedPolygon(self):
|
||||
self.scene_.setMode(POLYGON)
|
||||
|
||||
|
||||
def main():
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
wnd = MainWindow(sys.argv[1:])
|
||||
wnd.resize(800,600)
|
||||
wnd.show()
|
||||
|
||||
return app.exec_()
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
from PyQt4.QtGui import *
|
||||
from PyQt4.Qt import *
|
||||
from annotationitem import *
|
||||
|
||||
class AnnotationGraphicsPointItem(AnnotationGraphicsItem):
|
||||
size_ = 4
|
||||
|
||||
def __init__(self, pos, parent=None):
|
||||
AnnotationGraphicsItem.__init__(self, False, parent)
|
||||
self.setPos(pos)
|
||||
self.setFlags(QGraphicsItem.ItemIsSelectable|QGraphicsItem.ItemIsMovable)
|
||||
self.setPen(QColor('green'))
|
||||
|
||||
def resizeContents(self, rect):
|
||||
pass
|
||||
|
||||
def boundingRect(self):
|
||||
return QRectF(-2*self.size_,
|
||||
-2*self.size_,
|
||||
4*self.size_,
|
||||
4*self.size_)
|
||||
|
||||
def paint(self, painter, option, widget = None):
|
||||
pen = self.pen()
|
||||
if self.isSelected():
|
||||
pen.setColor(QColor('red'))
|
||||
painter.setPen(pen)
|
||||
painter.drawEllipse(0,0,self.size_,self.size_)
|
||||
|
||||
def itemChange(self, change, value):
|
||||
return AnnotationGraphicsItem.itemChange(self, change, value)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user