/vp_plugins/global/myscene.cpp

http://cupsfilter.googlecode.com/ · C++ · 322 lines · 248 code · 65 blank · 9 comment · 33 complexity · 09bd9ac7a5f48614f62cee438669f830 MD5 · raw file

  1. #include "myscene.h"
  2. #include "mytextitem.h"
  3. #include "picitem.h"
  4. #include "commands.h"
  5. #include "mytypes.h"
  6. #include <QtGui/QGraphicsSceneMouseEvent>
  7. #include <QtGui/QGraphicsSceneContextMenuEvent>
  8. #include <QtGui/QMenu>
  9. #include <QtGui/QAction>
  10. #include <QtGui/QUndoStack>
  11. #include <QtGui/QFontDialog>
  12. #include <QtGui/QFont>
  13. #include <QtGui/QColorDialog>
  14. #include <QtGui/QInputDialog>
  15. #include <QtGui/QGraphicsPixmapItem>
  16. #include <QtCore/QLineF>
  17. #include <QtCore/QVarLengthArray>
  18. #include <QPainter>
  19. using namespace VPrn;
  20. myScene::myScene(int Number, QUndoStack* undoStack, QObject *parent )
  21. : QGraphicsScene(parent)
  22. , m_mode (true)
  23. , m_angle(0.0)
  24. , m_undoStack(0)
  25. , m_Number(-1)
  26. {
  27. // initialise variables
  28. m_undoStack = undoStack;
  29. connect( this, SIGNAL(selectionChanged()),
  30. this, SLOT (selectItems()) );
  31. m_Number = Number;
  32. }
  33. QUndoStack *myScene::undoStack() const
  34. {
  35. return m_undoStack;
  36. }
  37. void myScene::createPageForPrint(qreal width,qreal height)
  38. {
  39. this->clear();
  40. this->setSceneRect(0, 0, width,height);
  41. this->setBackgroundBrush(Qt::white);
  42. QPixmap pixmap = QPixmap(int (width),int(height));
  43. pixmap.fill(Qt::white);
  44. QGraphicsPixmapItem *paper_rect = new QGraphicsPixmapItem();
  45. paper_rect->setPixmap(pixmap);
  46. paper_rect->setData(ObjectName, "Paper");
  47. paper_rect->setZValue(-1000.0);
  48. paper_rect->setShapeMode(QGraphicsPixmapItem::HeuristicMaskShape);
  49. this->addItem(paper_rect);
  50. }
  51. void myScene::createPage(qreal width,qreal height,qreal m_top,qreal m_bottom,
  52. qreal m_right,qreal m_left)
  53. {
  54. QPixmap pixmap;
  55. QPixmap pixmap2;
  56. this->clear();
  57. this->setSceneRect(0, 0, width,height);
  58. //this->setBackgroundBrush(Qt::white);
  59. this->setBackgroundBrush(Qt::transparent);
  60. QGraphicsRectItem *paper_rect =
  61. new QGraphicsRectItem (QRectF(0,0, width,height));
  62. paper_rect->setPen(QPen(Qt::black));
  63. paper_rect->setBrush(QBrush(Qt::white));
  64. paper_rect->setZValue(-1000.0);
  65. paper_rect->setData(ObjectName, "Paper");
  66. this->addItem(paper_rect);
  67. QGraphicsRectItem *border_rect =
  68. new QGraphicsRectItem (
  69. QRectF(m_left, m_top,width-m_left-m_right,height-m_top-m_bottom)
  70. );
  71. border_rect->setPen(QPen(Qt::black,2,Qt::DotLine));
  72. border_rect->setBrush(QBrush(Qt::white));
  73. #if QT_VERSION >= 0x040500
  74. border_rect->setOpacity(1);
  75. #endif
  76. border_rect->setZValue(-900);
  77. border_rect->setData(ObjectName, "Border");
  78. border_rect->setParentItem(paper_rect);
  79. }
  80. QGraphicsItem * myScene::getPaperItem()
  81. {
  82. QGraphicsItem *item(0);
  83. // ????? ????????? ?? ??????
  84. for (int i = 0; i < this->items().size(); ++i){
  85. item = this->items().at(i);
  86. if ( item->data(ObjectName).toString()=="Paper"){
  87. break;
  88. }
  89. }
  90. return item;
  91. }
  92. void myScene::AddImgElement(const QString & file_name)
  93. {
  94. QPixmap pixmap(file_name);
  95. PicItem *Item = new PicItem(pixmap,this->getPaperItem());
  96. Item->setData(ObjectName, "tImg");
  97. Item->setZValue(100);
  98. Item->setFlags(QGraphicsItem::ItemIsMovable |
  99. QGraphicsItem::ItemIsSelectable |
  100. QGraphicsItem::ItemIsFocusable);
  101. Item->setOffset(-0.5*QPointF(pixmap.width(), pixmap.height()));
  102. Item->setTransformationMode(Qt::SmoothTransformation);
  103. }
  104. void myScene::addImgElem(const QPointF &ps,const qreal m_angle,const qreal m_scaled,const QPixmap &img )
  105. {
  106. PicItem *Item;
  107. Item = new PicItem(img);
  108. Item->setData(ObjectName, "tImg");
  109. Item->setZValue(100);
  110. Item->setPos(ps);
  111. Item->setFlags(QGraphicsItem::ItemIsMovable |
  112. QGraphicsItem::ItemIsSelectable |
  113. QGraphicsItem::ItemIsFocusable);
  114. Item->setOffset(-0.5*QPointF(img.width(), img.height()));
  115. Item->setTransformationMode(Qt::SmoothTransformation);
  116. Item->setMyTransform(m_angle,m_scaled);
  117. Item->setParentItem(this->getPaperItem());
  118. }
  119. void myScene::addBaseElem(const QString &tag,const QString &text,const QPointF &ps,
  120. const QFont &fnt,const QColor &col,const qreal m_angle)
  121. {
  122. myTextItem * pItem = new myTextItem(this->getPaperItem());
  123. pItem->setPos(ps);
  124. pItem->setFont(fnt);
  125. pItem->setDefaultTextColor(col);
  126. pItem->setETag(tag);
  127. if (text.isEmpty()){
  128. pItem->setEText(tag);
  129. }else{
  130. pItem->setEText(text);
  131. }
  132. if (m_mode){
  133. pItem->setPlainText(tag);
  134. }else{
  135. pItem->setPlainText(text);
  136. }
  137. pItem->setZValue(100);
  138. pItem->setFlag(QGraphicsItem::ItemIsMovable, true);
  139. pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);
  140. pItem->setFlag(QGraphicsItem::ItemIsFocusable, true);
  141. pItem->setData(ObjectName, "tElem");
  142. if (m_undoStack){
  143. m_undoStack->push( new CommandTextItemAdd( this, pItem ) );
  144. m_undoStack->push( new CommandTextItemRotate( this, pItem,m_angle ) );
  145. }else{
  146. pItem->setAngle(m_angle);
  147. }
  148. }
  149. void myScene::selectItems()
  150. {
  151. // refresh record of selected stations and their starting positions
  152. m_TextItems.clear();
  153. foreach( QGraphicsItem* item, selectedItems() ){
  154. if ( dynamic_cast<myTextItem*>( item ) ){
  155. m_TextItems.append(
  156. qMakePair( dynamic_cast<myTextItem*>(item), item->pos() )
  157. );
  158. }
  159. }
  160. }
  161. void myScene::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
  162. {
  163. // if any TextItem moved, then create undo commands
  164. foreach( ItemPos TextItemPos ,m_TextItems )
  165. if ( TextItemPos.first->pos() != TextItemPos.second )
  166. m_undoStack->push( new CommandTextItemMove( TextItemPos.first,
  167. TextItemPos.second.x(), TextItemPos.second.y(),
  168. TextItemPos.first->x(), TextItemPos.first->y() ) );
  169. // refresh record of selected stations and call base mouseReleaseEvent
  170. selectItems();
  171. QGraphicsScene::mouseReleaseEvent( event );
  172. }
  173. void myScene::contextMenuEvent( QGraphicsSceneContextMenuEvent* event )
  174. {
  175. // we only want to display a menu if user clicked a textItem
  176. qreal x = event->scenePos().x();
  177. qreal y = event->scenePos().y();
  178. QMenu menu;
  179. myTextItem* textItem = dynamic_cast<myTextItem*>( itemAt( x, y ) );
  180. PicItem* picItem = dynamic_cast<PicItem*>( itemAt( x, y ) );
  181. qreal angle;
  182. if (textItem == 0 && picItem ==0) return;
  183. if ( textItem != 0 ){
  184. setTagAction = menu.addAction(QIcon(":/edit.png"),
  185. QObject::trUtf8("???????? ???????? ????"));
  186. changeFontAction = menu.addAction(QIcon(":/fontDialog.png"),
  187. QObject::trUtf8("???????? ?????"));
  188. changeColorAction = menu.addAction(QIcon(":/colorDialog.png"),
  189. QObject::trUtf8("???????? ????"));
  190. menu.addSeparator();
  191. rotateRightAction = menu.addAction(QIcon(":/rotateRight.png"),
  192. QObject::trUtf8("??????? ?????? ?? 90????."));
  193. rotateLeftAction = menu.addAction(QIcon(":/rotateLeft.png"),
  194. QObject::trUtf8("??????? ????? ?? 90????."));
  195. menu.addSeparator();
  196. }
  197. delElemAction = menu.addAction(QObject::trUtf8("??????? ???????"));
  198. QAction * act = menu.exec(event->screenPos());
  199. if (act ==setTagAction){
  200. QString tag = QInputDialog::getText(event->widget(),
  201. QObject::trUtf8("??????? ?????"),
  202. QObject::trUtf8("????? ??? ????????:"),
  203. QLineEdit::Normal, textItem->getETag());
  204. if (!tag.isEmpty()){
  205. m_undoStack->push( new CommandTextItemChangeTag( this, textItem,tag ) );
  206. textItem->setEText(QObject::trUtf8("?????..."));
  207. textItem->setPlainText(tag);
  208. }
  209. }
  210. if (act == delElemAction){
  211. if ( textItem != 0 ){
  212. m_undoStack->push( new CommandTextItemDelete( this, textItem ) );
  213. }
  214. if ( picItem != 0 ){
  215. this->removeItem(picItem);
  216. }
  217. }
  218. if (act == rotateRightAction){
  219. angle = -90.0;
  220. m_undoStack->push( new CommandTextItemRotate( this, textItem,angle ) );
  221. }
  222. if (act == rotateLeftAction){
  223. angle = 90.0;
  224. m_undoStack->push( new CommandTextItemRotate( this, textItem,angle) );
  225. }
  226. if (act == changeFontAction ){
  227. bool ok;
  228. QFont font = QFontDialog::getFont( &ok, QFont("Times", 14,QFont::Bold),0,
  229. QObject::trUtf8("???????? ????? ??? ???????? "));
  230. if (ok) {// ???????????? ???????? OK, ? ????? ??????????????? ? ?????????
  231. //currentFont = font;
  232. m_undoStack->push( new CommandTextItemChangeFont( this, textItem,font ) );
  233. }
  234. }
  235. if (act == changeColorAction ){
  236. QColor col ;
  237. #if QT_VERSION >= 0x040500
  238. col = QColorDialog::getColor ( Qt::white,0,QObject::trUtf8("???????? ???? ???????? ???????? ") ) ;
  239. #else
  240. #endif
  241. if (col.isValid()) {// ???????????? ???????? OK, ? ???? ??????????????? ? ?????????
  242. m_undoStack->push( new CommandTextItemChangeColor( this, textItem,col ) );
  243. }
  244. }
  245. }
  246. void myScene::setViewMode()
  247. {
  248. m_mode = (m_mode == true ) ? m_mode = false : m_mode = true;
  249. ///??????? ?? ??????? ???????? ????? ? ??????? ???????????? ???????
  250. QList<QGraphicsItem *> mTxtItem;
  251. mTxtItem = this->items();
  252. foreach (QGraphicsItem *item, mTxtItem){
  253. if ( dynamic_cast<myTextItem*>( item ) ){
  254. dynamic_cast<myTextItem*>( item )->setMode(m_mode);
  255. }
  256. }
  257. }