PageRenderTime 106ms CodeModel.GetById 51ms app.highlight 51ms RepoModel.GetById 1ms app.codeStats 0ms

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