00001 """ 00002 Copyright (c) 2013, Cogniteam 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright 00009 notice, this list of conditions and the following disclaimer. 00010 00011 * Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in the 00013 documentation and/or other materials provided with the distribution. 00014 00015 * Neither the name of the Cogniteam nor the 00016 names of its contributors may be used to endorse or promote products 00017 derived from this software without specific prior written permission. 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 00023 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 """ 00030 00031 from __future__ import division 00032 00033 from python_qt_binding.QtCore import Qt, QPointF 00034 from python_qt_binding.QtGui import QBrush, QGraphicsEllipseItem, QGraphicsRectItem, QGraphicsSimpleTextItem, \ 00035 QPainterPath, QPen, QGraphicsPolygonItem, QPolygonF 00036 00037 00038 class StaticClassError(Exception): 00039 pass 00040 00041 00042 class QGraphicsRoundRectItem(QGraphicsRectItem): 00043 def __init__(self, bounding_box): 00044 super(QGraphicsRoundRectItem, self).__init__(bounding_box) 00045 00046 def paint(self, painter, option, widget): 00047 if self.pen() is not None: 00048 painter.setPen(self.pen()) 00049 painter.drawRoundedRect(self.boundingRect(), 4.0, 4.0) 00050 00051 00052 class ShapeFactory: 00053 message = None 00054 00055 def __init__(self): 00056 raise StaticClassError("%s is a static class and cannot be initiated." % ShapeFactory) 00057 00058 @staticmethod 00059 def create(shape, bounding_box): 00060 ShapeFactory.message = None 00061 graphics_item = None 00062 00063 if shape in ('box', 'rect', 'rectangle'): 00064 graphics_item = QGraphicsRoundRectItem(bounding_box) 00065 elif shape in ('ellipse', 'point'): 00066 graphics_item = QGraphicsEllipseItem(bounding_box) 00067 elif shape == 'diamond': 00068 points = QPolygonF([QPointF(bounding_box.x(), 00069 bounding_box.y() + bounding_box.height() / 2), 00070 00071 QPointF(bounding_box.x() + bounding_box.width() / 2, 00072 bounding_box.y() + bounding_box.height()), 00073 00074 QPointF(bounding_box.x() + bounding_box.width(), 00075 bounding_box.y() + bounding_box.height() / 2), 00076 00077 QPointF(bounding_box.x() + bounding_box.width() / 2, 00078 bounding_box.y())]) 00079 graphics_item = QGraphicsPolygonItem(points) 00080 elif shape == 'parallelogram': 00081 points = QPolygonF([QPointF(bounding_box.x() + bounding_box.width() * 1/6, 00082 bounding_box.y()), 00083 00084 QPointF(bounding_box.x() + bounding_box.width(), 00085 bounding_box.y()), 00086 00087 QPointF(bounding_box.x() + bounding_box.width() * 5/6, 00088 bounding_box.y() + bounding_box.height()), 00089 00090 QPointF(bounding_box.x(), 00091 bounding_box.y() + bounding_box.height())]) 00092 graphics_item = QGraphicsPolygonItem(points) 00093 elif shape == 'cds': 00094 points = QPolygonF([QPointF(bounding_box.x(), 00095 bounding_box.y()), 00096 00097 QPointF(bounding_box.x() + bounding_box.width() * 5/6, 00098 bounding_box.y()), 00099 00100 QPointF(bounding_box.x() + bounding_box.width(), 00101 bounding_box.y() + bounding_box.height() / 2), 00102 00103 QPointF(bounding_box.x() + bounding_box.width() * 5/6, 00104 bounding_box.y() + bounding_box.height()), 00105 00106 QPointF(bounding_box.x(), 00107 bounding_box.y() + bounding_box.height())]) 00108 graphics_item = QGraphicsPolygonItem(points) 00109 elif shape == 'rarrow': 00110 points = QPolygonF([QPointF(bounding_box.x(), 00111 bounding_box.y()), 00112 00113 QPointF(bounding_box.x() + bounding_box.width() * 4/6, 00114 bounding_box.y()), 00115 00116 QPointF(bounding_box.x() + bounding_box.width() * 4/6, 00117 bounding_box.y() - bounding_box.height() * 2/6), 00118 00119 QPointF(bounding_box.x() + bounding_box.width(), 00120 bounding_box.y() + bounding_box.height() / 2), 00121 00122 QPointF(bounding_box.x() + bounding_box.width() * 4/6, 00123 bounding_box.y() + bounding_box.height() * 8/6), 00124 00125 QPointF(bounding_box.x() + bounding_box.width() * 4/6, 00126 bounding_box.y() + bounding_box.height()), 00127 00128 QPointF(bounding_box.x(), 00129 bounding_box.y() + bounding_box.height())]) 00130 graphics_item = QGraphicsPolygonItem(points) 00131 elif shape == 'larrow': 00132 points = QPolygonF([QPointF(bounding_box.x() + bounding_box.width() * 2/6, 00133 bounding_box.y()), 00134 00135 QPointF(bounding_box.x() + bounding_box.width() * 2/6, 00136 bounding_box.y() - bounding_box.height() * 2/6), 00137 00138 QPointF(bounding_box.x(), 00139 bounding_box.y() + bounding_box.height() / 2), 00140 00141 QPointF(bounding_box.x() + bounding_box.width() * 2/6, 00142 bounding_box.y() + bounding_box.height() * 8/6), 00143 00144 QPointF(bounding_box.x() + bounding_box.width() * 2/6, 00145 bounding_box.y() + bounding_box.height()), 00146 00147 QPointF(bounding_box.x() + bounding_box.width(), 00148 bounding_box.y() + bounding_box.height()), 00149 00150 QPointF(bounding_box.x() + bounding_box.width(), 00151 bounding_box.y())]) 00152 graphics_item = QGraphicsPolygonItem(points) 00153 elif shape == 'record': 00154 graphics_item = QGraphicsRectItem(bounding_box) 00155 elif shape == 'hexagon': 00156 points = QPolygonF([QPointF(bounding_box.x() + bounding_box.width() * 1/5, 00157 bounding_box.y()), 00158 00159 QPointF(bounding_box.x() + bounding_box.width() * 4/5, 00160 bounding_box.y()), 00161 00162 QPointF(bounding_box.x() + bounding_box.width(), 00163 bounding_box.y() + bounding_box.height() / 2), 00164 00165 QPointF(bounding_box.x() + bounding_box.width() * 4/5, 00166 bounding_box.y() + bounding_box.height()), 00167 00168 QPointF(bounding_box.x() + bounding_box.width() * 1/5, 00169 bounding_box.y() + bounding_box.height()), 00170 00171 QPointF(bounding_box.x(), 00172 bounding_box.y() + bounding_box.height() / 2)]) 00173 graphics_item = QGraphicsPolygonItem(points) 00174 elif shape == 'triangle': 00175 points = QPolygonF([QPointF(bounding_box.x() + bounding_box.width() / 2, 00176 bounding_box.y()), 00177 00178 QPointF(bounding_box.x() + bounding_box.width(), 00179 bounding_box.y() + bounding_box.height() * 3/4), 00180 00181 QPointF(bounding_box.x(), 00182 bounding_box.y() + bounding_box.height() * 3/4)]) 00183 graphics_item = QGraphicsPolygonItem(points) 00184 elif shape == 'circle': 00185 diameter = min(bounding_box.width(), bounding_box.height()) 00186 graphics_item = QGraphicsEllipseItem(bounding_box.x(), bounding_box.y(), diameter, diameter) 00187 else: 00188 graphics_item = QGraphicsRectItem(bounding_box) 00189 ShapeFactory.message = "WARNING: %s is unknown shape, box used instead" % shape 00190 00191 return graphics_item