MainPaintScene.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "MainPaintScenee.h"
  2. #include<QGraphicsSceneMouseEvent>
  3. #include<QGraphicsRectItem>
  4. #include <QDebug>
  5. const double initWidth =700.0;
  6. const double initHeight =663.0;
  7. MainPaintScene::MainPaintScene(QObject *parent) :
  8. QGraphicsScene(parent)
  9. {
  10. // QRect mainRect(0,0,2000,2000);
  11. // setSceneRect(mainRect);//设置场景范围
  12. // addRect(mainRect,QPen(Qt::black));//把场景范围直观显示出来,只为了方便理解
  13. // _colorBak=Qt::red;
  14. }
  15. MainPaintScene::~MainPaintScene()
  16. {
  17. SafeDelete (textItem);
  18. }
  19. void MainPaintScene::InsertPicture(QString path)
  20. {
  21. DrawTool * tool =DrawTool::findTool(image);
  22. if (tool)
  23. {
  24. tool->setPicture(path);
  25. }
  26. }
  27. void MainPaintScene::mouseEvent(QGraphicsSceneMouseEvent *mouseEvent)
  28. {
  29. switch( mouseEvent->type() ){
  30. case QEvent::GraphicsSceneMousePress:
  31. QGraphicsScene::mousePressEvent(mouseEvent);
  32. break;
  33. case QEvent::GraphicsSceneMouseMove:
  34. QGraphicsScene::mouseMoveEvent(mouseEvent);
  35. break;
  36. case QEvent::GraphicsSceneMouseRelease:
  37. QGraphicsScene::mouseReleaseEvent(mouseEvent);
  38. break;
  39. }
  40. }
  41. //打开文件
  42. void MainPaintScene::SetBackGroundImage(QString path)
  43. {
  44. this->clear();
  45. //this->addRect(0,0,initWidth,initHeight,QPen(Qt::green),QBrush(Qt::red));
  46. QPixmap pixmap =QPixmap::fromImage(QImage(path));
  47. double dpiX = QApplication::desktop()->physicalDpiX();
  48. double dpiY = QApplication::desktop()->physicalDpiY();
  49. int physicalX=(int)(dpiX*5.5/2.54+0.5f);
  50. int physicalY=(int)(dpiY*7/2.54+0.5f);
  51. QPixmap fitpixmap = pixmap.scaled(physicalX, physicalY, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  52. // QPixmap fitpixmap = pixmap.scaled(1083, 1378, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 饱满填充
  53. QGraphicsPixmapItem* _backGroundItem= this->addPixmap(fitpixmap);
  54. // if(pixmap.width()>pixmap.height())
  55. // {
  56. // //匹配宽度
  57. // _backGroundItem->setScale( initWidth/pixmap.width() );
  58. // }
  59. // else//匹配高度
  60. // {
  61. // _backGroundItem->setScale( initHeight /pixmap.height() );
  62. // }
  63. _backGroundItem->setPos(0,0);
  64. }
  65. void MainPaintScene::keyPressEvent(QKeyEvent *event)
  66. {
  67. // DrawTool * tool =DrawTool::findTool( DrawTool::c_drawShape );
  68. // if (tool)
  69. // tool->keyPressEvent(event,this);
  70. QList<QGraphicsItem *> selectItems = selectedItems();
  71. switch (event->key())
  72. {
  73. case Qt::Key_Delete:
  74. if(selectItems.count()>0)
  75. {
  76. while(selectItems.count()>0)
  77. {
  78. QGraphicsItem * item =selectItems.last();
  79. removeItem(item);
  80. selectItems.pop_back();
  81. }
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. QGraphicsScene::keyPressEvent(event);
  88. }
  89. void MainPaintScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
  90. {
  91. DrawTool * tool =DrawTool::findTool( DrawTool::c_drawShape );
  92. if( DrawTool::c_drawShape ==text) //文字独立出来
  93. {
  94. textItem = new GraphicsTextItem();
  95. QFont myFont =textItem->font();
  96. myFont.setWeight(QFont::Bold);
  97. myFont.setFamily("SimSun");
  98. myFont.setPixelSize(24);
  99. textItem->setDefaultTextColor(_color);
  100. textItem->setFont(myFont);
  101. textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
  102. textItem->setZValue(1000.0);
  103. connect(textItem, SIGNAL(lostFocus(GraphicsTextItem*)),
  104. this, SLOT(editorLostFocus(GraphicsTextItem*)));
  105. connect(textItem, SIGNAL(selectedChange(GraphicsTextItem*)),
  106. this, SIGNAL(itemSelected(GraphicsTextItem*)));
  107. addItem(textItem);
  108. textItem->setDefaultTextColor(Qt::red);
  109. textItem->setPos(mouseEvent->scenePos());
  110. textItem->setFlag(QGraphicsItem::ItemIsMovable);
  111. textItem->setAcceptDrops(true);
  112. SetCurrentShape(selection);
  113. }
  114. else if (tool)
  115. {
  116. tool->mousePressEvent(mouseEvent,this);
  117. }
  118. }
  119. void MainPaintScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
  120. {
  121. DrawTool * tool = DrawTool::findTool( DrawTool::c_drawShape );
  122. if( DrawTool::c_drawShape ==text) //文字独立出来
  123. {
  124. QGraphicsScene::mouseMoveEvent(mouseEvent);
  125. }
  126. else if(tool)
  127. tool->mouseMoveEvent(mouseEvent,this);
  128. }
  129. void MainPaintScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
  130. {
  131. DrawTool * tool =DrawTool::findTool( DrawTool::c_drawShape );
  132. if( DrawTool::c_drawShape ==text) //文字独立出来
  133. {
  134. }
  135. else if ( tool )
  136. tool->mouseReleaseEvent(mouseEvent,this);
  137. }
  138. void MainPaintScene::setCurrentColor(QColor color)
  139. {
  140. DrawTool * tool =DrawTool::findTool( DrawTool::c_drawShape );
  141. if ( tool )
  142. tool->setCurrentColor(color);
  143. _color =color;
  144. }
  145. void MainPaintScene::SetCurrentShape(DrawBaseShape shape)
  146. {
  147. if ( shape == selection)
  148. DrawTool::c_drawShape = selection;
  149. if ( shape == rotation)
  150. DrawTool::c_drawShape = rotation;
  151. if ( shape == line)
  152. DrawTool::c_drawShape = line;
  153. if ( shape == rectangle)
  154. DrawTool::c_drawShape = rectangle;
  155. if ( shape == ellipse)
  156. DrawTool::c_drawShape = ellipse;
  157. if ( shape == penPath)
  158. DrawTool::c_drawShape = penPath;
  159. if ( shape == text)
  160. DrawTool::c_drawShape = text;
  161. if ( shape == image)
  162. DrawTool::c_drawShape = image;
  163. if ( shape == arrowLine)
  164. DrawTool::c_drawShape = arrowLine;
  165. if ( shape == triangle)
  166. DrawTool::c_drawShape = triangle;
  167. DrawTool * tool =DrawTool::findTool( DrawTool::c_drawShape );
  168. if ( tool )
  169. tool->setCurrentColor(_color);
  170. }