2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ModelerGraphicItem.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.modeler.ModelerParameterDefinitionDialog import ModelerParameterDefinitionDialog
|
|
|
|
from processing.modeler.ModelerParametersDialog import ModelerParametersDialog
|
2012-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4 import QtCore, QtGui
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.parameters.Parameter import Parameter
|
2012-09-15 18:25:25 +03:00
|
|
|
import os
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
class ModelerGraphicItem(QtGui.QGraphicsItem):
|
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
BOX_HEIGHT = 30
|
2012-09-15 18:25:25 +03:00
|
|
|
BOX_WIDTH = 200
|
|
|
|
|
|
|
|
def __init__(self, element, elementIndex, model):
|
|
|
|
super(ModelerGraphicItem, self).__init__(None, None)
|
|
|
|
self.model = model
|
|
|
|
self.element = element
|
|
|
|
self.elementIndex = elementIndex
|
2013-04-17 13:16:34 +02:00
|
|
|
self.inputFolded = True
|
|
|
|
self.outputFolded = True
|
2012-09-15 18:25:25 +03:00
|
|
|
if isinstance(element, Parameter):
|
|
|
|
icon = QtGui.QIcon(os.path.dirname(__file__) + "/../images/input.png")
|
|
|
|
self.pixmap = icon.pixmap(20, 20, state=QtGui.QIcon.On)
|
|
|
|
self.text = element.description
|
2013-05-05 11:55:59 +02:00
|
|
|
elif isinstance(element, basestring): #output name
|
|
|
|
icon = QtGui.QIcon(os.path.dirname(__file__) + "/../images/output.png")
|
|
|
|
self.pixmap = icon.pixmap(20, 20, state=QtGui.QIcon.On)
|
2013-05-15 20:42:04 +02:00
|
|
|
self.text = element
|
2012-09-15 18:25:25 +03:00
|
|
|
else:
|
2013-04-17 13:16:34 +02:00
|
|
|
state=QtGui.QIcon.On
|
|
|
|
if self.elementIndex in self.model.deactivated:
|
|
|
|
state=QtGui.QIcon.Off
|
2012-09-15 18:25:25 +03:00
|
|
|
self.text = element.name
|
2013-04-17 13:16:34 +02:00
|
|
|
self.pixmap = element.getIcon().pixmap(15, 15, state=state)
|
2012-09-15 18:25:25 +03:00
|
|
|
self.arrows = []
|
|
|
|
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
|
|
|
|
self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
|
|
|
|
self.setZValue(1000)
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-05-05 11:55:59 +02:00
|
|
|
if not isinstance(element, basestring):
|
|
|
|
icon = QtGui.QIcon(os.path.dirname(__file__) + "/../images/edit.png")
|
2013-05-15 20:42:04 +02:00
|
|
|
pt = QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH / 2 - FlatButtonGraphicItem.WIDTH / 2, ModelerGraphicItem.BOX_HEIGHT / 2 - FlatButtonGraphicItem.HEIGHT / 2 + 1)
|
2013-05-05 11:55:59 +02:00
|
|
|
self.editButton = FlatButtonGraphicItem(icon, pt, self.editElement)
|
|
|
|
self.editButton.setParentItem(self)
|
|
|
|
icon = QtGui.QIcon(os.path.dirname(__file__) + "/../images/delete.png")
|
2013-05-15 20:42:04 +02:00
|
|
|
pt = QtCore.QPointF(ModelerGraphicItem.BOX_WIDTH / 2 - FlatButtonGraphicItem.WIDTH / 2, -ModelerGraphicItem.BOX_HEIGHT / 2 + FlatButtonGraphicItem.HEIGHT / 2 + 1)
|
2013-05-05 11:55:59 +02:00
|
|
|
self.deleteButton = FlatButtonGraphicItem(icon, pt, self.removeElement)
|
|
|
|
self.deleteButton.setParentItem(self)
|
2013-05-15 20:42:04 +02:00
|
|
|
|
|
|
|
if isinstance(element, GeoAlgorithm):
|
|
|
|
if element.parameters:
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = self.getLinkPointForParameter(-1)
|
2013-05-15 20:42:04 +02:00
|
|
|
x = self.getXPositionForFoldButton()
|
2013-06-12 00:05:52 +02:00
|
|
|
pt = QtCore.QPointF(x, pt.y() + 2)
|
2013-04-17 13:16:34 +02:00
|
|
|
self.inButton = FoldButtonGraphicItem(pt, self.foldInput)
|
|
|
|
self.inButton.setParentItem(self)
|
|
|
|
if element.outputs:
|
|
|
|
pt = self.getLinkPointForOutput(-1)
|
2013-05-15 20:42:04 +02:00
|
|
|
x = self.getXPositionForFoldButton()
|
2013-06-12 00:05:52 +02:00
|
|
|
pt = QtCore.QPointF(x, pt.y() + 2)
|
2013-04-17 13:16:34 +02:00
|
|
|
self.outButton = FoldButtonGraphicItem(pt, self.foldOutput)
|
|
|
|
self.outButton.setParentItem(self)
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def foldInput(self, folded):
|
|
|
|
self.inputFolded = folded
|
2013-05-15 20:42:04 +02:00
|
|
|
self.prepareGeometryChange()
|
|
|
|
if self.element.outputs:
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = self.getLinkPointForOutput(-1)
|
2013-05-15 20:42:04 +02:00
|
|
|
x = self.getXPositionForFoldButton()
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = QtCore.QPointF(x, pt.y())
|
|
|
|
self.outButton.position = pt
|
|
|
|
self.update()
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def foldOutput(self, folded):
|
2013-05-15 20:42:04 +02:00
|
|
|
self.outputFolded = folded
|
|
|
|
self.prepareGeometryChange()
|
|
|
|
self.update()
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def addArrow(self, arrow):
|
|
|
|
self.arrows.append(arrow)
|
|
|
|
|
|
|
|
def boundingRect(self):
|
2013-04-17 13:16:34 +02:00
|
|
|
font = QtGui.QFont("Verdana", 8)
|
2013-05-15 20:42:04 +02:00
|
|
|
fm = QtGui.QFontMetricsF(font)
|
2013-04-17 13:16:34 +02:00
|
|
|
numParams = 0 if self.inputFolded else len(self.element.parameters)
|
2013-05-15 20:42:04 +02:00
|
|
|
numOutputs = 0 if self.outputFolded else len(self.element.outputs)
|
2013-06-12 00:05:52 +02:00
|
|
|
hUp = (fm.height() * 1.2) * (numParams + 2)
|
|
|
|
hDown = (fm.height() * 1.2) * (numOutputs + 2)
|
|
|
|
rect = QtCore.QRectF(-(ModelerGraphicItem.BOX_WIDTH + 2)/2, -(ModelerGraphicItem.BOX_HEIGHT + 2)/2 - hUp,
|
|
|
|
ModelerGraphicItem.BOX_WIDTH + 2, ModelerGraphicItem.BOX_HEIGHT + hDown + hUp)
|
2012-09-15 18:25:25 +03:00
|
|
|
return rect
|
|
|
|
|
|
|
|
def mouseDoubleClickEvent(self, event):
|
|
|
|
self.editElement()
|
|
|
|
|
|
|
|
def contextMenuEvent(self, event):
|
|
|
|
popupmenu = QtGui.QMenu()
|
|
|
|
removeAction = popupmenu.addAction("Remove")
|
|
|
|
removeAction.triggered.connect(self.removeElement)
|
|
|
|
editAction = popupmenu.addAction("Edit")
|
|
|
|
editAction.triggered.connect(self.editElement)
|
|
|
|
if isinstance(self.element, GeoAlgorithm):
|
|
|
|
if self.elementIndex in self.model.deactivated:
|
|
|
|
removeAction = popupmenu.addAction("Activate")
|
|
|
|
removeAction.triggered.connect(self.activateAlgorithm)
|
|
|
|
else:
|
|
|
|
deactivateAction = popupmenu.addAction("Deactivate")
|
|
|
|
deactivateAction.triggered.connect(self.deactivateAlgorithm)
|
|
|
|
popupmenu.exec_(event.screenPos())
|
|
|
|
|
|
|
|
def deactivateAlgorithm(self):
|
2013-05-05 11:55:59 +02:00
|
|
|
self.model.setPositions(self.scene().getParameterPositions(), self.scene().getAlgorithmPositions(), self.scene().getOutputPositions())
|
2012-09-15 18:25:25 +03:00
|
|
|
self.model.deactivateAlgorithm(self.elementIndex, True)
|
|
|
|
|
|
|
|
def activateAlgorithm(self):
|
2013-05-05 11:55:59 +02:00
|
|
|
self.model.setPositions(self.scene().getParameterPositions(), self.scene().getAlgorithmPositions(), self.scene().getOutputPositions())
|
2012-09-15 18:25:25 +03:00
|
|
|
if not self.model.activateAlgorithm(self.elementIndex, True):
|
|
|
|
QtGui.QMessageBox.warning(None, "Could not activate Algorithm",
|
|
|
|
"The selected algorithm depends on other currently non-active algorithms.\nActivate them them before trying to activate it.")
|
|
|
|
|
|
|
|
def editElement(self):
|
2013-05-05 11:55:59 +02:00
|
|
|
self.model.setPositions(self.scene().getParameterPositions(), self.scene().getAlgorithmPositions(), self.scene().getOutputPositions())
|
2012-09-15 18:25:25 +03:00
|
|
|
if isinstance(self.element, Parameter):
|
|
|
|
dlg = ModelerParameterDefinitionDialog(self.model, param = self.element)
|
|
|
|
dlg.exec_()
|
|
|
|
if dlg.param != None:
|
|
|
|
self.model.updateParameter(self.elementIndex, dlg.param)
|
|
|
|
self.element = dlg.param
|
|
|
|
self.text = self.element.description
|
|
|
|
self.update()
|
2013-05-05 11:55:59 +02:00
|
|
|
elif isinstance(self.element, GeoAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
dlg = self.element.getCustomModelerParametersDialog(self.model, self.elementIndex)
|
|
|
|
if not dlg:
|
|
|
|
dlg = ModelerParametersDialog(self.element, self.model, self.elementIndex)
|
|
|
|
dlg.exec_()
|
|
|
|
if dlg.params != None:
|
2013-02-03 10:26:43 +01:00
|
|
|
self.model.updateAlgorithm(self.elementIndex, dlg.params, dlg.values, dlg.outputs, dlg.dependencies)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def removeElement(self):
|
|
|
|
if isinstance(self.element, Parameter):
|
|
|
|
if not self.model.removeParameter(self.elementIndex):
|
|
|
|
QtGui.QMessageBox.warning(None, "Could not remove element",
|
|
|
|
"Other elements depend on the selected one.\nRemove them before trying to remove it.")
|
2013-05-05 11:55:59 +02:00
|
|
|
elif isinstance(self.element, GeoAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
if not self.model.removeAlgorithm(self.elementIndex):
|
|
|
|
QtGui.QMessageBox.warning(None, "Could not remove element",
|
|
|
|
"Other elements depend on the selected one.\nRemove them before trying to remove it.")
|
|
|
|
|
|
|
|
def getAdjustedText(self, text):
|
|
|
|
font = QtGui.QFont("Verdana", 8)
|
|
|
|
fm = QtGui.QFontMetricsF(font)
|
|
|
|
w = fm.width(text)
|
2013-04-17 13:16:34 +02:00
|
|
|
if w < self.BOX_WIDTH - 25 - FlatButtonGraphicItem.WIDTH:
|
2012-09-15 18:25:25 +03:00
|
|
|
return text
|
|
|
|
|
|
|
|
text = text[0:-3] + "..."
|
|
|
|
w = fm.width(text)
|
2013-04-17 13:16:34 +02:00
|
|
|
while(w > self.BOX_WIDTH - 25 - FlatButtonGraphicItem.WIDTH):
|
2012-09-15 18:25:25 +03:00
|
|
|
text = text[0:-4] + "..."
|
|
|
|
w = fm.width(text)
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
def paint(self, painter, option, widget=None):
|
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
rect = QtCore.QRectF(-(ModelerGraphicItem.BOX_WIDTH + 2)/2.0, -(ModelerGraphicItem.BOX_HEIGHT + 2)/2.0,
|
2012-09-15 18:25:25 +03:00
|
|
|
ModelerGraphicItem.BOX_WIDTH + 2, ModelerGraphicItem.BOX_HEIGHT + 2)
|
2013-06-20 23:40:51 +02:00
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.gray, 1))
|
2013-06-12 00:05:52 +02:00
|
|
|
color = QtGui.QColor(125,232, 232)
|
|
|
|
if isinstance(self.element, Parameter):
|
|
|
|
color = QtGui.QColor(179,179, 255)
|
|
|
|
elif isinstance(self.element, GeoAlgorithm):
|
|
|
|
color = QtCore.Qt.white
|
|
|
|
painter.setBrush(QtGui.QBrush(color, QtCore.Qt.SolidPattern))
|
2012-09-15 18:25:25 +03:00
|
|
|
painter.drawRect(rect)
|
|
|
|
font = QtGui.QFont("Verdana", 8)
|
|
|
|
painter.setFont(font)
|
2013-04-17 13:16:34 +02:00
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.black))
|
2012-09-15 18:25:25 +03:00
|
|
|
if self.isSelected():
|
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.blue))
|
2013-04-17 13:16:34 +02:00
|
|
|
if isinstance(self.element, GeoAlgorithm):
|
|
|
|
if self.elementIndex in self.model.deactivated:
|
2013-05-15 20:42:04 +02:00
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.lightGray))
|
2012-09-15 18:25:25 +03:00
|
|
|
fm = QtGui.QFontMetricsF(font)
|
|
|
|
text = self.getAdjustedText(self.text)
|
|
|
|
h = fm.height()
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH)/2 + 25, h/2.0)
|
2012-09-15 18:25:25 +03:00
|
|
|
painter.drawText(pt, text)
|
2013-04-17 13:16:34 +02:00
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.black))
|
2013-05-15 20:42:04 +02:00
|
|
|
if isinstance(self.element, GeoAlgorithm):
|
2013-06-12 00:05:52 +02:00
|
|
|
h = -(fm.height() * 1.2)
|
|
|
|
h = h - ModelerGraphicItem.BOX_HEIGHT / 2.0 + 5
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH)/2 + 25, h)
|
|
|
|
painter.drawText(pt, "In")
|
|
|
|
i = 1
|
|
|
|
if not self.inputFolded:
|
|
|
|
for param in self.element.parameters:
|
2013-05-15 20:42:04 +02:00
|
|
|
text = self.getAdjustedText(param.description)
|
2013-06-12 00:05:52 +02:00
|
|
|
h = -(fm.height() * 1.2) * (i + 1)
|
|
|
|
h = h - ModelerGraphicItem.BOX_HEIGHT / 2.0 + 5
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH)/2 + 33, h)
|
|
|
|
painter.drawText(pt, text)
|
2013-05-15 20:42:04 +02:00
|
|
|
i += 1
|
2013-06-12 00:05:52 +02:00
|
|
|
i = 1
|
2013-06-20 23:40:51 +02:00
|
|
|
h = (fm.height() * 1.2)
|
2013-05-15 20:42:04 +02:00
|
|
|
h = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH)/2 + 25, h)
|
|
|
|
painter.drawText(pt, "Out")
|
|
|
|
if not self.outputFolded:
|
|
|
|
for out in self.element.outputs:
|
2013-05-15 20:42:04 +02:00
|
|
|
text = self.getAdjustedText(out.description)
|
2013-04-17 13:16:34 +02:00
|
|
|
h = (fm.height() * 1.2) * (i + 1)
|
2013-05-15 20:42:04 +02:00
|
|
|
h = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
|
2013-04-17 13:16:34 +02:00
|
|
|
pt = QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH)/2 + 33, h)
|
|
|
|
painter.drawText(pt, text)
|
|
|
|
i += 1
|
2013-05-05 11:55:59 +02:00
|
|
|
if self.pixmap:
|
|
|
|
painter.drawPixmap(-(ModelerGraphicItem.BOX_WIDTH / 2.0) + 3, -8, self.pixmap)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def getLinkPointForParameter(self, paramIndex):
|
2013-05-13 22:33:05 +02:00
|
|
|
offsetX = 25
|
2013-04-17 13:16:34 +02:00
|
|
|
if self.inputFolded:
|
|
|
|
paramIndex = -1
|
2013-05-13 22:33:05 +02:00
|
|
|
offsetX = 17
|
2013-04-17 13:16:34 +02:00
|
|
|
font = QtGui.QFont("Verdana", 8)
|
2013-05-15 20:42:04 +02:00
|
|
|
fm = QtGui.QFontMetricsF(font)
|
|
|
|
if isinstance(self.element, GeoAlgorithm):
|
2013-06-12 00:05:52 +02:00
|
|
|
h = -(fm.height() * 1.2) * (paramIndex + 2) - fm.height() / 2.0 + 8
|
|
|
|
h = h - ModelerGraphicItem.BOX_HEIGHT / 2.0
|
2013-05-05 11:55:59 +02:00
|
|
|
else:
|
|
|
|
h = 0
|
2013-04-17 13:16:34 +02:00
|
|
|
return QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH)/2 + offsetX, h)
|
2013-05-15 20:42:04 +02:00
|
|
|
|
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def getXPositionForFoldButton(self):
|
|
|
|
return 0
|
2013-05-15 20:42:04 +02:00
|
|
|
|
|
|
|
def getLinkPointForOutput(self, outputIndex):
|
|
|
|
if isinstance(self.element, GeoAlgorithm):
|
2013-06-12 00:05:52 +02:00
|
|
|
numParams = 0# if self.inputFolded else len(self.element.parameters)
|
2013-05-15 20:42:04 +02:00
|
|
|
outputIndex = outputIndex if not self.outputFolded else -1
|
|
|
|
text = self.getAdjustedText(self.element.outputs[outputIndex].description)
|
2013-04-17 13:16:34 +02:00
|
|
|
font = QtGui.QFont("Verdana", 8)
|
2013-05-15 20:42:04 +02:00
|
|
|
fm = QtGui.QFontMetricsF(font)
|
2013-06-09 16:04:44 +02:00
|
|
|
w = fm.width(text)
|
2013-06-12 00:05:52 +02:00
|
|
|
h = (fm.height() * 1.2) * (outputIndex + 1) + fm.height() / 2.0
|
|
|
|
y = h + ModelerGraphicItem.BOX_HEIGHT / 2.0 + 5
|
2013-04-17 13:16:34 +02:00
|
|
|
x = -(ModelerGraphicItem.BOX_WIDTH)/2 + 33 + w + 5 if not self.outputFolded else 10
|
|
|
|
return QtCore.QPointF(x, y)
|
|
|
|
else:
|
|
|
|
return QtCore.QPointF(0, 0)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def itemChange(self, change, value):
|
|
|
|
if change == QtGui.QGraphicsItem.ItemPositionChange:
|
|
|
|
for arrow in self.arrows:
|
|
|
|
arrow.updatePosition()
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
def polygon(self):
|
2013-04-17 13:16:34 +02:00
|
|
|
font = QtGui.QFont("Verdana", 8)
|
2013-06-20 23:40:51 +02:00
|
|
|
fm = QtGui.QFontMetricsF(font)
|
2013-06-12 00:05:52 +02:00
|
|
|
hUp = (fm.height() * 1.2) * (len(self.element.parameters) + 2)
|
|
|
|
hDown = (fm.height() * 1.2) * (len(self.element.outputs) + 2)
|
2012-09-15 18:25:25 +03:00
|
|
|
pol = QtGui.QPolygonF([
|
2013-06-12 00:05:52 +02:00
|
|
|
QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH + 2)/2, -(ModelerGraphicItem.BOX_HEIGHT + 2)/2 - hUp),
|
|
|
|
QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH + 2)/2, (ModelerGraphicItem.BOX_HEIGHT + 2)/2 + hDown),
|
|
|
|
QtCore.QPointF((ModelerGraphicItem.BOX_WIDTH + 2)/2, (ModelerGraphicItem.BOX_HEIGHT + 2)/2 + hDown),
|
|
|
|
QtCore.QPointF((ModelerGraphicItem.BOX_WIDTH + 2)/2, -(ModelerGraphicItem.BOX_HEIGHT + 2)/2 - hUp),
|
|
|
|
QtCore.QPointF(-(ModelerGraphicItem.BOX_WIDTH + 2)/2, -(ModelerGraphicItem.BOX_HEIGHT + 2)/2 - hUp)])
|
2012-09-15 18:25:25 +03:00
|
|
|
return pol
|
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
class FlatButtonGraphicItem(QtGui.QGraphicsItem):
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
WIDTH = 16
|
2013-05-15 20:42:04 +02:00
|
|
|
HEIGHT = 16
|
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def __init__(self, icon, position, action):
|
|
|
|
super(FlatButtonGraphicItem, self).__init__(None, None)
|
2013-05-15 20:42:04 +02:00
|
|
|
self.setAcceptHoverEvents(True)
|
2013-04-17 13:16:34 +02:00
|
|
|
self.setFlag(QtGui.QGraphicsItem.ItemIsMovable, False)
|
|
|
|
self.pixmap = icon.pixmap(self.WIDTH, self.HEIGHT, state=QtGui.QIcon.On)
|
|
|
|
self.position = position
|
2013-05-15 20:42:04 +02:00
|
|
|
self.isIn = False
|
|
|
|
self.action = action
|
2013-04-17 13:16:34 +02:00
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
self.action()
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def paint(self, painter, option, widget=None):
|
|
|
|
pt = QtCore.QPointF(-self.WIDTH / 2, -self.HEIGHT / 2) + self.position
|
2013-05-15 20:42:04 +02:00
|
|
|
rect = QtCore.QRectF(pt.x(), pt.y(), self.WIDTH, self.HEIGHT)
|
2013-04-17 13:16:34 +02:00
|
|
|
if self.isIn:
|
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.transparent, 1))
|
2013-05-15 20:42:04 +02:00
|
|
|
painter.setBrush(QtGui.QBrush(QtCore.Qt.lightGray, QtCore.Qt.SolidPattern))
|
2013-04-17 13:16:34 +02:00
|
|
|
else:
|
2013-05-15 20:42:04 +02:00
|
|
|
painter.setPen(QtGui.QPen(QtCore.Qt.transparent, 1))
|
2013-06-12 00:05:52 +02:00
|
|
|
painter.setBrush(QtGui.QBrush(QtCore.Qt.transparent, QtCore.Qt.SolidPattern))
|
2013-05-15 20:42:04 +02:00
|
|
|
painter.drawRect(rect)
|
2013-04-17 13:16:34 +02:00
|
|
|
painter.drawPixmap(pt.x(), pt.y(), self.pixmap)
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def boundingRect(self):
|
|
|
|
rect = QtCore.QRectF(self.position.x() - self.WIDTH / 2, self.position.y() - self.HEIGHT / 2, self.WIDTH, self.HEIGHT)
|
2013-05-15 20:42:04 +02:00
|
|
|
return rect
|
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def hoverEnterEvent(self, event):
|
2013-05-15 20:42:04 +02:00
|
|
|
self.isIn = True
|
2013-04-17 13:16:34 +02:00
|
|
|
self.update()
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def hoverLeaveEvent(self, event):
|
2013-05-15 20:42:04 +02:00
|
|
|
self.isIn = False
|
2013-04-17 13:16:34 +02:00
|
|
|
self.update()
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
class FoldButtonGraphicItem(FlatButtonGraphicItem):
|
|
|
|
|
2013-04-24 15:01:00 +02:00
|
|
|
WIDTH = 11
|
|
|
|
HEIGHT = 11
|
2013-05-15 20:42:04 +02:00
|
|
|
|
|
|
|
icons = { True : QtGui.QIcon(os.path.dirname(__file__) + "/../images/plus.png"),
|
2013-04-24 15:01:00 +02:00
|
|
|
False : QtGui.QIcon(os.path.dirname(__file__) + "/../images/minus.png")}
|
2013-05-15 20:42:04 +02:00
|
|
|
|
2013-04-17 13:16:34 +02:00
|
|
|
def __init__(self, position, action):
|
|
|
|
self.folded = True
|
|
|
|
icon = self.icons[True]
|
2013-05-15 20:42:04 +02:00
|
|
|
super(FoldButtonGraphicItem, self).__init__(icon, position, action)
|
2013-04-17 13:16:34 +02:00
|
|
|
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
self.folded = not self.folded
|
|
|
|
icon = self.icons[self.folded]
|
|
|
|
self.pixmap = icon.pixmap(self.WIDTH, self.HEIGHT, state=QtGui.QIcon.On)
|
|
|
|
self.action(self.folded)
|