![]() |
TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
|
00001 /**************************************************************************** 00002 00003 Copyright (c) 2008 Deepak Chandran 00004 Contact: Deepak Chandran (dchandran1@gmail.com) 00005 See COPYRIGHT.TXT 00006 00007 Class for drawing text on a GraphicsScene. The text can be associated with 00008 a handle 00009 00010 ****************************************************************************/ 00011 00012 #include <QPainter> 00013 #include "Tool.h" 00014 #include "MainWindow.h" 00015 #include "TextGraphicsItem.h" 00016 #include "NodeGraphicsItem.h" 00017 #include "ConnectionGraphicsItem.h" 00018 00019 namespace Tinkercell 00020 { 00021 00022 ItemHandle * TextGraphicsItem::handle() const 00023 { 00024 return itemHandle; 00025 } 00026 00027 void TextGraphicsItem::setHandle(ItemHandle * handle) 00028 { 00029 if (handle != 0 && !handle->graphicsItems.contains(this)) 00030 { 00031 handle->graphicsItems += this; 00032 } 00033 00034 if (itemHandle) 00035 { 00036 if (itemHandle != handle) 00037 { 00038 itemHandle->graphicsItems.removeAll(this); 00039 itemHandle = handle; 00040 } 00041 } 00042 else 00043 { 00044 itemHandle = handle; 00045 } 00046 } 00047 00049 TextGraphicsItem::TextGraphicsItem(const QString& text, QGraphicsItem* parent) : 00050 QGraphicsTextItem(text,parent), relativePosition(QPair<QGraphicsItem*,QPointF>(0,QPointF())) 00051 { 00052 setTextInteractionFlags(Qt::TextEditorInteraction); 00053 setFlag(QGraphicsItem::ItemIsMovable); 00054 //setFlag(QGraphicsItem::ItemIsSelectable); 00055 itemHandle = 0; 00056 boundingRectItem = new QGraphicsRectItem(this); 00057 boundingRectItem->setPen(QPen(QColor(100,100,100),2)); 00058 boundingRectItem->setBrush(Qt::NoBrush); 00059 boundingRectItem->setVisible(false); 00060 } 00062 TextGraphicsItem::TextGraphicsItem(QGraphicsItem* parent) : 00063 QGraphicsTextItem(parent), relativePosition(QPair<QGraphicsItem*,QPointF>(0,QPointF())) 00064 { 00065 setTextInteractionFlags(Qt::TextEditorInteraction); 00066 setFlag(QGraphicsItem::ItemIsMovable); 00067 //setFlag(QGraphicsItem::ItemIsSelectable); 00068 itemHandle = 0; 00069 boundingRectItem = new QGraphicsRectItem(this); 00070 boundingRectItem->setPen(QPen(QColor(100,100,100),2)); 00071 boundingRectItem->setBrush(Qt::NoBrush); 00072 boundingRectItem->setVisible(false); 00073 } 00075 TextGraphicsItem::TextGraphicsItem(ItemHandle * handle, QGraphicsItem* parent) : 00076 QGraphicsTextItem(parent), relativePosition(QPair<QGraphicsItem*,QPointF>(0,QPointF())) 00077 { 00078 if (handle) setPlainText(handle->name); 00079 itemHandle = 0; 00080 setHandle(handle); 00081 setTextInteractionFlags(Qt::TextEditorInteraction); 00082 setFlag(QGraphicsItem::ItemIsMovable); 00083 //setFlag(QGraphicsItem::ItemIsSelectable); 00084 boundingRectItem = new QGraphicsRectItem(this); 00085 boundingRectItem->setPen(QPen(QColor(100,100,100),2)); 00086 boundingRectItem->setBrush(Qt::NoBrush); 00087 boundingRectItem->setVisible(false); 00088 } 00090 TextGraphicsItem::TextGraphicsItem(const TextGraphicsItem& copy) : QGraphicsTextItem(), relativePosition(copy.relativePosition) 00091 { 00092 setPos(copy.scenePos()); 00093 setTransform(copy.transform()); 00094 setDefaultTextColor(copy.defaultTextColor()); 00095 setVisible(copy.isVisible()); 00096 //setDocument(copy.document()); 00097 setFont(copy.font()); 00098 setHtml (copy.toHtml()); 00099 setTextWidth(copy.textWidth()); 00100 setTextCursor(copy.textCursor()); 00101 setTextInteractionFlags ( copy.textInteractionFlags() ); 00102 setFlag(QGraphicsItem::ItemIsMovable); 00103 //setFlag(QGraphicsItem::ItemIsSelectable); 00104 setPlainText(copy.toPlainText()); 00105 itemHandle = copy.itemHandle; 00106 groupID = copy.groupID; 00107 00108 if (itemHandle) 00109 setHandle(itemHandle); 00110 00111 boundingRectItem = new QGraphicsRectItem(this); 00112 boundingRectItem->setPen(QPen(QColor(100,100,100),2)); 00113 boundingRectItem->setBrush(Qt::NoBrush); 00114 boundingRectItem->setVisible(false); 00115 } 00116 TextGraphicsItem* TextGraphicsItem::clone() 00117 { 00118 return new TextGraphicsItem(*this); 00119 } 00120 TextGraphicsItem::~TextGraphicsItem() 00121 { 00122 if (!itemHandle) return; 00123 setHandle(0); 00124 } 00125 00126 void TextGraphicsItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) 00127 { 00128 QGraphicsTextItem::paint(painter,option,widget); 00129 } 00130 00131 void TextGraphicsItem::showBorder(bool showBorder) 00132 { 00133 boundingRectItem->setRect(this->boundingRect()); 00134 boundingRectItem->setVisible(showBorder); 00135 update(); 00136 } 00137 00138 QString TextGraphicsItem::text() const 00139 { 00140 return toPlainText(); 00141 } 00142 00143 void TextGraphicsItem::setText(const QString& s) 00144 { 00145 setPlainText(s); 00146 } 00147 00148 TextGraphicsItem* TextGraphicsItem::cast(QGraphicsItem * item) 00149 { 00150 //if (MainWindow::invalidPointers.contains( (void*)q )) return 0; 00151 if (!item || ToolGraphicsItem::cast(item->topLevelItem())) return 0; 00152 return qgraphicsitem_cast<TextGraphicsItem*>(item); 00153 } 00154 00155 QList<TextGraphicsItem*> TextGraphicsItem::cast(const QList<QGraphicsItem*>& items) 00156 { 00157 QList<TextGraphicsItem*> items2; 00158 TextGraphicsItem * text = 0; 00159 for (int i=0; i < items.size(); ++i) 00160 { 00161 text = TextGraphicsItem::cast(items[i]); 00162 if (text && !items2.contains(text)) 00163 items2 << text; 00164 } 00165 return items2; 00166 } 00167 00168 QGraphicsItem* TextGraphicsItem::closestItem() const 00169 { 00170 if (!itemHandle) return 0; 00171 00172 QList<QGraphicsItem*> & graphicsItems = itemHandle->graphicsItems; 00173 QPointF p1, p2; 00174 p1 = this->scenePos(); 00175 00176 QGraphicsItem * closest = 0; 00177 double dist1 = -1, dist2; 00178 00179 for (int i=0; i < graphicsItems.size(); ++i) 00180 { 00181 if (NodeGraphicsItem::cast(graphicsItems[i]) || ConnectionGraphicsItem::cast(graphicsItems[i])) 00182 { 00183 p2 = p1 - graphicsItems[i]->scenePos(); 00184 dist2 = p2.x()*p2.x() + p2.y()*p2.y(); 00185 if (dist1 < 0 || dist1 > dist2) 00186 { 00187 closest = graphicsItems[i]; 00188 dist1 = dist2; 00189 } 00190 } 00191 } 00192 00193 return closest; 00194 } 00195 00196 } 00197