Advertisement
Guest User

mysquare.cpp

a guest
May 17th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.77 KB | None | 0 0
  1. #include "mysquare.h"
  2. #include "mysvgitem.h"
  3. #include<QTranslator>
  4.  
  5. MySquare::MySquare()
  6.         :QGraphicsItem()
  7. {
  8.     myItemRect = QRectF(0,0,150,70); //default value
  9.     setFlag(ItemIsMovable);
  10.     setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
  11.     setFlag(QGraphicsItem::ItemIsSelectable);
  12.     setMyPolygon();//member function
  13.     setFlag(ItemSendsScenePositionChanges, true);
  14.     setAcceptHoverEvents(true);
  15.     posXY = new QString(QString::number(this->x())+", "+
  16.             QString::number(this->y()));
  17.     itemXY = new QGraphicsTextItem(this);
  18.     //itemXY->setPlainText((*posXY));
  19.     //itemXY->setTextInteractionFlags(Qt::TextEditorInteraction);
  20.     itemXY->setZValue(2000);
  21.     //text->setHtml("<heloooooooooo>");
  22.     itemXY->setPos(this->x(), this->y());
  23.     this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
  24.     this->setPos(100,100);
  25. }
  26.  
  27. QRectF MySquare::boundingRect() const
  28. {
  29.     return myItemRect;
  30. }
  31.  
  32. void MySquare::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  33.  
  34. {
  35.     Q_UNUSED(widget);
  36.     Q_UNUSED(option);
  37.     QRectF rect = boundingRect();
  38.     QBrush brush(Qt::green);
  39.     QPainterPath path;
  40.     path.addRoundedRect( rect.translated(0.3,0.3), 5.0, 5.0 );
  41.     QFont font;
  42.     //font.setFamily("Armenian (phonetic)");
  43.     font.setPointSize(20);
  44.     //QString mystr = "Partners and\nCustomers";
  45.     //path.addText(QPointF(40,30),font, tr("Partners \nand Customers"));
  46.     painter->fillPath(path, Qt::white);
  47.     painter->setPen(Qt::blue);
  48.     //painter->drawText(QRect(105, 55, 115, 35), Qt::AlignLeft, mystr );
  49.     painter->drawText(MyTextRect, Qt::AlignLeft, MyText );
  50.     QPen pen(Qt::black, 1);
  51.     painter->setPen(pen);
  52.     //painter->setBrush(brush);
  53.  
  54.     painter->setRenderHint(QPainter::Antialiasing); // this for rectangel alignment
  55.     painter->drawPath(path);
  56.  
  57.  
  58. }
  59.  
  60. void MySquare::mousePressEvent(QGraphicsSceneMouseEvent *event)
  61. {
  62.     //update();
  63.     //qDebug() << this->type();
  64.     bool diagonal_flag = false ;
  65.     clickFlag = true;
  66.     //QString *xy = new QString(QString::number(event->pos().x())+", "+
  67.     //        QString::number(event->pos().y()));
  68.     //itemXY->setPlainText((*xy));
  69.     QPointF p = event->pos();
  70.     QRectF r = sceneBoundingRect(); // relative to scene
  71.     QPointF tl = mapFromScene(r.topLeft());
  72.     QPointF br = mapFromScene(r.bottomRight());
  73.     if (p.x() >= br.x()-10 && p.y() >= br.y()-10)
  74.     {
  75.         diagonal_flag = true ;
  76.         //qDebug() << "rd_diagonal" ;
  77.     }
  78.     else { diagonal_flag = false ;}
  79.     if (p.x() <= tl.x()+10 && !diagonal_flag)
  80.     {
  81.         resize_direction_ = rd_left;
  82.         //itemXY->setPlainText("rd_left");
  83.     }
  84.     else if (p.x() >= br.x()-10 && !diagonal_flag)
  85.     {
  86.         resize_direction_ = rd_right;
  87.     }
  88.     else if (p.y() <= tl.y()+10 && !diagonal_flag)
  89.     {
  90.         resize_direction_ = rd_top;
  91.     }
  92.     else if (p.y() >= br.y()-10 && !diagonal_flag)
  93.     {
  94.         resize_direction_ = rd_bottom;
  95.         //qDebug() << "rd_right" ;
  96.     }
  97.     ////// testing diagonal resize /////
  98.     else if (diagonal_flag)
  99.     {
  100.         resize_direction_ = rd_diagonal;
  101.         //qDebug() << "rd_diagonal" ;
  102.     }
  103.     else
  104.     {
  105.         resize_direction_ = rd_none;
  106.     }
  107.     //this->setCursor(Qt::SizeAllCursor);
  108.  
  109.     QGraphicsItem::mousePressEvent(event);
  110. }
  111.  
  112. void MySquare::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  113. {
  114.     clickFlag = false;
  115.     if (isSelected())
  116.     {
  117.         // resize border to mouse position
  118.         QPointF p = event->pos();
  119.         QPointF pp = mapToItem(this, p);
  120.         //QRectF r = rect();
  121.         QRectF r = boundingRect();
  122.         //////// minimum size limit //////
  123.         if (r.width() <= myItemRect.width() && r.width() < 30.0)
  124.         {
  125.             resize_direction_ = rd_none;
  126.             return;
  127.         }
  128.         if (r.height() <= myItemRect.height() && r.height() < 30.0)
  129.         {
  130.             resize_direction_ = rd_none;
  131.             return;
  132.         }
  133.         ///////////// resize item ////
  134.         switch (resize_direction_)
  135.         {
  136.         case rd_left:
  137.             r.setLeft(pp.x());
  138.             prepareGeometryChange();
  139.             myItemRect = r ;
  140.             break;
  141.         case rd_top:
  142.             r.setTop(pp.y());
  143.             prepareGeometryChange();
  144.             myItemRect = r ;
  145.             break;
  146.         case rd_right:
  147.             r.setRight(pp.x());
  148.             prepareGeometryChange();
  149.             myItemRect = r ;
  150.             break;
  151.         case rd_bottom:
  152.             r.setBottom(pp.y());
  153.             prepareGeometryChange();
  154.             myItemRect = r ;
  155.             break;
  156.         case rd_diagonal:
  157.             r.setBottomRight(pp);
  158.             //qDebug() << " diagonal moving ";
  159.             prepareGeometryChange();
  160.             myItemRect = r ;
  161.             break;
  162.  
  163.         default:
  164.             QGraphicsItem::mouseMoveEvent(event);
  165.             //this->setCursor(Qt::ArrowCursor);
  166.             break;
  167.         }
  168.         return; //
  169.     }
  170.     QGraphicsItem::mouseMoveEvent(event);
  171. }
  172.  
  173. void MySquare::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  174. {
  175.     //Pressed = false;
  176.     QRectF r = sceneBoundingRect(); // relative to scene
  177.     if (r.width() <= myItemRect.width() && r.width() < 30.0)
  178.         myItemRect.setWidth(35);
  179.     if (r.height() <= myItemRect.height() && r.height() < 30.0)
  180.         myItemRect.setHeight(35);
  181.     //update();
  182.     if(clickFlag)
  183.     {
  184.         emit click();
  185.     }
  186.     //QTimer::singleShot(500, this, SIGNAL(click()));
  187.     QGraphicsItem::mouseReleaseEvent(event);
  188. }
  189.  
  190. QVariant MySquare::itemChange(GraphicsItemChange change, const QVariant &value)
  191. {
  192.     if (change == QGraphicsItem::ItemPositionChange && scene()) {
  193.         foreach (Arrow *arrow, arrows) {
  194.             arrow->updatePosition();
  195.         }
  196.        *posXY = QString::number(this->x())+", "+
  197.                             QString::number(this->y());
  198.         //itemXY->setPlainText((*posXY));
  199.  
  200.     }
  201.     if (change == QGraphicsItem::ItemOpacityChange ) {
  202.         foreach (Arrow *arrow, arrows) {
  203.             arrow->setOpacity(this->opacity()-0.2);
  204.         }
  205.     }
  206.     return QGraphicsItem::itemChange(change, value);
  207. }
  208.  
  209. void MySquare::addArrow(Arrow *arrow)
  210. {
  211.     arrows.append(arrow);
  212. }
  213.  
  214. QPointF MySquare::returnCenter()
  215. {
  216.     QRectF rect = boundingRect();
  217.     qreal width = rect.width();
  218.     qreal height = rect.height();
  219.     QPointF p = QPointF(width/2 + rect.x(), rect.y() + height/2 );
  220.     //return p;// changet for test
  221.     return this->pos()+p;// changed for test
  222. }
  223.  
  224. void MySquare::setMyPolygon()
  225. {
  226.     QRectF rec = boundingRect();
  227.     QPolygonF poly ;
  228.     //QPointF p = rec.topLeft();
  229.     poly << rec.topLeft() + this->pos()  << rec.topRight() + this->pos()
  230.         << rec.bottomRight()  + this->pos() << rec.bottomLeft() + this->pos()
  231.         << rec.topLeft() + this->pos();
  232.     myPolygon =  poly;
  233. }
  234.  
  235.  
  236. void MySquare::setItemSize(qreal width,qreal height)
  237. {
  238.     prepareGeometryChange();
  239.     myItemRect.setWidth(width);
  240.     myItemRect.setHeight(height);
  241. }
  242.  
  243. void MySquare::setSize(QSizeF size)
  244. {
  245.     prepareGeometryChange();
  246.     myItemRect.setWidth(size.width());
  247.     myItemRect.setHeight(size.height());
  248. }
  249.  
  250. QSizeF MySquare::size()
  251. {
  252.     return QSizeF( myItemRect.width(), myItemRect.height());
  253. }
  254.  
  255. void MySquare::setImage(const QString& str)
  256. {
  257.     QImage image(QCoreApplication::applicationDirPath() + "/" + str);
  258.  
  259.     QImage img = image;
  260.     QGraphicsItem *item = new QGraphicsPixmapItem(\
  261.                     QPixmap::fromImage(img));
  262.  
  263.     //svg->setMaximumCacheSize(QSize(350,350));
  264.     Q_ASSERT(!item.isNull());
  265.     item->setFlag(QGraphicsItem::ItemIsSelectable,false);
  266.     item->setParentItem(this);
  267. }
  268.  
  269. void MySquare::setImage(const QString& str, const QSize size)
  270. {
  271.     QImage image(QCoreApplication::applicationDirPath() + "/" + str);
  272.  
  273.     QImage img = image.scaled(size);
  274.     /*QGraphicsPixmapItem* item = new QGraphicsPixmapItem(\
  275.                     QPixmap::fromImage(image));*/
  276.     QGraphicsItem *item = new QGraphicsPixmapItem(\
  277.                     QPixmap::fromImage(img));
  278.  
  279.     //item->setSize(size);
  280.     //svg->setMaximumCacheSize(QSize(350,350));
  281.     Q_ASSERT(!item.isNull());
  282.     item->setFlag(QGraphicsItem::ItemIsSelectable,false);
  283.     item->setPos(21,11);
  284.     item->setParentItem(this);
  285. }
  286.  
  287. void MySquare::setImage(const QString& str ,QRectF rec)
  288. {
  289.     QImage image(QCoreApplication::applicationDirPath() + "/" + str);
  290.  
  291.     QImage img = image.scaled(QSize(rec.width(),rec.height()));
  292.     /*QGraphicsPixmapItem* item = new QGraphicsPixmapItem(\
  293.                     QPixmap::fromImage(image));*/
  294.     QGraphicsItem *item = new QGraphicsPixmapItem(\
  295.                     QPixmap::fromImage(img));
  296.     Q_ASSERT(!item.isNull());
  297.     item->setFlag(QGraphicsItem::ItemIsSelectable,false);
  298.     //item->setSize(QSize(rec.width(),rec.height()));
  299.     item->setParentItem(this);
  300.     item->setPos(rec.x(),rec.y());
  301. }
  302.  
  303. void MySquare::setSvgImage(const QString& str)
  304. {
  305.     QGraphicsItem  *m_svgItem = new QGraphicsSvgItem(\
  306.             QCoreApplication::applicationDirPath() + "/" + str);
  307.     Q_ASSERT(!m_svgItem.isNull());
  308.     m_svgItem->setFlag(QGraphicsItem::ItemIsSelectable,false);
  309.     m_svgItem->setPos(5,5);
  310.     m_svgItem->setParentItem(this);
  311. }
  312.  
  313. void MySquare::setSvgImage(const QString& str ,QSizeF size)
  314. {
  315.     /*QGraphicsSvgItem  *svg = new QGraphicsSvgItem(\
  316.             QCoreApplication::applicationDirPath() + "/" + str);*/
  317.     MySvgItem  *svg = new MySvgItem(\
  318.             QCoreApplication::applicationDirPath() + "/" + str );
  319.     svg->setSize(size);
  320.     //svg->setMaximumCacheSize(QSize(350,350));
  321.     Q_ASSERT(!svg.isNull());
  322.     QGraphicsItem  *m_svgItem = svg;
  323.     //m_svgItem->setPos(5,5);
  324.     m_svgItem->setFlag(QGraphicsItem::ItemIsSelectable,false);
  325.     m_svgItem->setParentItem(this);
  326.     m_svgItem->setZValue(-1000);
  327. }
  328.  
  329. void MySquare::setSvgImage(const QString& str ,QRectF rec)
  330. {
  331.     MySvgItem  *svg = new MySvgItem(\
  332.             QCoreApplication::applicationDirPath() + "/" + str );
  333.     Q_ASSERT(!svg.isNull());
  334.     svg->setSize(QSize(rec.width(),rec.height()));
  335.     QGraphicsItem  *m_svgItem = svg;
  336.     m_svgItem->setFlag(QGraphicsItem::ItemIsSelectable,false);
  337.     m_svgItem->setParentItem(this);
  338.     m_svgItem->setPos(rec.x(),rec.y());
  339.     m_svgItem->setZValue(-1000);
  340. }
  341.  
  342. void MySquare::setText(const QString& str ,QRectF rec)
  343. {
  344.     QGraphicsTextItem *text = new QGraphicsTextItem(this);
  345.     //text->setTextInteractionFlags(Qt::TextEditorInteraction);
  346.     text->setFlag(QGraphicsItem::ItemIsSelectable,false);
  347.     text->setHtml(str);
  348.     text->setPos(rec.x(), rec.y());
  349. }
  350.  
  351. MySquare::~MySquare()
  352. {
  353.     for (int i=0; i< arrows.count(); i++)
  354.     {
  355.         if (arrows.at(i)== NULL){
  356.           delete arrows.at(i);  
  357.         }
  358.     }
  359. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement