#include "pixmapitem.h" #include "itabs.h" PixmapItem::PixmapItem( NodeItem *from, NodeItem *to ) : EdgeItem(from, to), QGraphicsPixmapItem () { item_type = type(); filename = QString(); orig_image = new QPixmap(":images/checkerboard.png"); scale_image = new QPixmap(":images/checkerboard.png"); setPixmap(scale_image->scaled(10,10)); setPos(10,50); from->setOutEdge( (EdgeItem *)this ); to->setInEdge( (EdgeItem *)this ); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } PixmapItem::PixmapItem( QDomElement el ) : EdgeItem(new NodeItem(), new NodeItem()), QGraphicsPixmapItem() { item_type = type(); int x1 = el.attribute("x").toInt(), y1 = el.attribute("y").toInt(), width = el.attribute("width").toInt(), height = el.attribute("height").toInt(); QDomCDATASection cd = el.firstChild().toCDATASection (); orig_image = new QPixmap(); QByteArray bytes = QByteArray::fromBase64(cd.data().toAscii ()); orig_image->loadFromData(bytes); scale_image = new QPixmap(); scale_image->loadFromData(bytes); setPos(x1,y1); setPixmap(scale_image->scaled(width,height)); setFromPoint(x1,y1); setToPoint(x1+width, y1+height); setCursor(QCursor(Qt::SizeAllCursor)); setFlag(ItemIsSelectable); setFlag(ItemIsMovable); } void PixmapItem::paintItem( int x0, int y0, QPainter *pp, qreal scale ) { QRect r = pixmap().rect(); QRect r1 = orig_image->rect(); // QPixmap *p = orig_image; // QImage image(filename); // QImage image1 = image.scaled(int(r.width()*scale),int(r.height()*scale)); QPixmap image1 = orig_image->scaled(int(r.width()*scale),int(r.height()*scale)); // pp->drawImage(QPoint(int((x()+scenePos().x())*scale+x0), int((y()+scenePos().y())*scale)+y), image1); pp->drawPixmap(int(x()*scale+x0), int((y())*scale)+y0, image1); } void PixmapItem::setFromPoint( int x0, int y0 ) { setBlock(true); QPointF p = to_node->pos(); qreal dx, dy; dx = p.x()-x0; dy = p.y()-y0; if(dx<=0) dx=3; if(dy<=0) dy=3; setPos(x0,y0); setPixmap(scale_image->scaled(int(dx), int(dy))); // pixmap().rect().setTopLeft(QPoint(x0,y0)); xx = x0; yy = y0; from_node->setPos(x0,y0); emit position_changed(this); setBlock(false); } void PixmapItem::setToPoint( int x0, int y0 ) { setBlock(true); setPixmap(scale_image->scaled(int(x0-from_node->pos().x()),int(y0-from_node->pos().y()))); to_node->setPos(x0,y0); emit position_changed(this); setBlock(false); } void PixmapItem::setTabs( QTabWidget *properties) { // conf = settings; PositionTab *ptab = new PositionTab(this); properties->addTab(ptab, tr("Position")); ImageTab *itab = new ImageTab(this); properties->addTab(itab, tr("Image")); } QVariant PixmapItem::itemChange(GraphicsItemChange change, const QVariant &value) { if(block()) {return QGraphicsPixmapItem::itemChange(change, value);} // if(block()) {return -1;} setBlock(true); if(change == ItemSelectedChange) { if(value.toBool()) { emit item_selected(this); from_node->show(); to_node->show(); } else { emit item_selected(0); from_node->hide(); to_node->hide(); } } else if (change == ItemPositionChange) { QRectF r = sceneBoundingRect(); from_node->setPos(r.x(),r.y()); to_node->setPos(r.x()+r.width(), r.y()+r.height()); emit position_changed(this); } setBlock(false); return QGraphicsPixmapItem::itemChange(change, value); } QDomElement PixmapItem::toXml(QDomDocument doc) { QDomElement l =doc.createElement("pixmap"); QByteArray bytes; QBuffer buffer(&bytes); buffer.open(QIODevice::WriteOnly); orig_image->save(&buffer, "PNG"); QDomCDATASection cd = doc.createCDATASection(bytes.toBase64 ()); l.appendChild(cd); l.setAttribute("x",x()); l.setAttribute("y",y()); l.setAttribute("width",pixmap().rect().width()); l.setAttribute("height",pixmap().rect().height()); return l; }