FreeCAD

Форк
0
/
QGIProjGroup.cpp 
200 строк · 7.8 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2012-2013 Luke Parry <l.parry@warwick.ac.uk>            *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
# include <QGraphicsScene>
26
# include <QGraphicsSceneMouseEvent>
27
# include <QList>
28
#endif
29

30
#include <App/Document.h>
31
#include <Base/Console.h>
32
#include <Gui/Selection.h>
33
#include <Mod/TechDraw/App/DrawProjGroup.h>
34
#include <Mod/TechDraw/App/DrawProjGroupItem.h>
35

36
#include "QGIProjGroup.h"
37
#include "Rez.h"
38

39

40
using namespace TechDrawGui;
41

42
QGIProjGroup::QGIProjGroup()
43
{
44
    m_origin = new QGraphicsItemGroup();                  //QGIG added to this QGIG??
45
    m_origin->setParentItem(this);
46

47
    setFlag(ItemIsSelectable, false);
48
    setFlag(ItemIsMovable, true);
49
    setFiltersChildEvents(true);
50
//    setFrameState(false);
51
}
52

53
TechDraw::DrawProjGroup * QGIProjGroup::getDrawView() const
54
{
55
    App::DocumentObject *obj = getViewObject();
56
    return dynamic_cast<TechDraw::DrawProjGroup *>(obj);
57
}
58

59
bool QGIProjGroup::sceneEventFilter(QGraphicsItem* watched, QEvent *event)
60
{
61
// i want to handle events before the child item that would ordinarily receive them
62
    if(event->type() == QEvent::GraphicsSceneMousePress ||
63
       event->type() == QEvent::GraphicsSceneMouseMove  ||
64
       event->type() == QEvent::GraphicsSceneMouseRelease) {
65

66
        QGIView *qAnchor = getAnchorQItem();
67
        if(qAnchor && watched == qAnchor) {
68
            QGraphicsSceneMouseEvent *mEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
69

70
            switch(event->type()) {
71
              case QEvent::GraphicsSceneMousePress:
72
                  // TODO - Perhaps just pass the mouse event on to the anchor somehow?
73
                  if (scene() && !qAnchor->isSelected()) {
74
                      scene()->clearSelection();
75
                      qAnchor->setSelected(true);
76
                  }
77
                  mousePressEvent(mEvent);
78
                  break;
79
              case QEvent::GraphicsSceneMouseMove:
80
                  mouseMoveEvent(mEvent);
81
                  break;
82
              case QEvent::GraphicsSceneMouseRelease:
83
                  mouseReleaseEvent(mEvent);
84
                  break;
85
              default:
86
                  break;
87
            }
88
            return true;
89
        }
90
    }
91

92
    return false;
93
}
94
QVariant QGIProjGroup::itemChange(GraphicsItemChange change, const QVariant &value)
95
{
96
    if(change == ItemChildAddedChange && scene()) {
97
         QGraphicsItem*childItem = value.value<QGraphicsItem*>();
98
         QGIView* gView = dynamic_cast<QGIView *>(childItem);
99
         if(gView) {
100
            TechDraw::DrawView *fView = gView->getViewObject();
101
            if(fView->isDerivedFrom<TechDraw::DrawProjGroupItem>()) {
102
                TechDraw::DrawProjGroupItem *projItemPtr = static_cast<TechDraw::DrawProjGroupItem *>(fView);
103
                QString type = QString::fromLatin1(projItemPtr->Type.getValueAsString());
104

105
                if (type == QString::fromLatin1("Front")) {
106
                    gView->alignTo(m_origin, QString::fromLatin1("None"));
107
                    installSceneEventFilter(gView);
108
                } else if ( type == QString::fromLatin1("Top") ||
109
                    type == QString::fromLatin1("Bottom")) {
110
                    gView->alignTo(m_origin, QString::fromLatin1("Vertical"));
111
                } else if ( type == QString::fromLatin1("Left")  ||
112
                            type == QString::fromLatin1("Right") ||
113
                            type == QString::fromLatin1("Rear") ) {
114
                    gView->alignTo(m_origin, QString::fromLatin1("Horizontal"));
115
                } else if ( type == QString::fromLatin1("FrontTopRight") ||
116
                            type == QString::fromLatin1("FrontBottomLeft") ) {
117
                    gView->alignTo(m_origin, QString::fromLatin1("45slash"));
118
                } else if ( type == QString::fromLatin1("FrontTopLeft") ||
119
                            type == QString::fromLatin1("FrontBottomRight") ) {
120
                    gView->alignTo(m_origin, QString::fromLatin1("45backslash"));
121
                }
122
            }
123
         }
124
    }
125
    return QGIViewCollection::itemChange(change, value);
126
}
127

128
void QGIProjGroup::mousePressEvent(QGraphicsSceneMouseEvent * event)
129
{
130
    QGIView *qAnchor = getAnchorQItem();
131
    if(qAnchor) {
132
        QPointF transPos = qAnchor->mapFromScene(event->scenePos());
133
        if(qAnchor->shape().contains(transPos)) {
134
            mousePos = event->screenPos();
135
        }
136
    }
137
    event->accept();
138
}
139

140
void QGIProjGroup::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
141
{
142
    QGIView *qAnchor = getAnchorQItem();
143
    if(scene() && qAnchor && (qAnchor == scene()->mouseGrabberItem())) {
144
        if((mousePos - event->screenPos()).manhattanLength() > 5) {    //if the mouse has moved more than 5, process the mouse event
145
            QGIViewCollection::mouseMoveEvent(event);
146
        }
147

148
    }
149
    event->accept();
150
}
151

152
void QGIProjGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
153
{
154
     if(scene()) {
155
       QGIView *qAnchor = getAnchorQItem();
156
        if((mousePos - event->screenPos()).manhattanLength() < 5) {
157
            if(qAnchor && qAnchor->shape().contains(event->pos())) {
158
                event->ignore();
159
                qAnchor->mouseReleaseEvent(event);
160
            }
161
        } else if(scene() && qAnchor) {
162
            // End of Drag
163
            getViewObject()->setPosition(Rez::appX(x()), Rez::appX(getY()));
164
        }
165
    }
166
    QGIViewCollection::mouseReleaseEvent(event);
167
}
168

169
QGIView * QGIProjGroup::getAnchorQItem() const
170
{
171
    // Get the currently assigned anchor view
172
    App::DocumentObject *anchorObj = getDrawView()->Anchor.getValue();
173
    auto anchorView( dynamic_cast<TechDraw::DrawView *>(anchorObj) );
174
    if (!anchorView) {
175
        return nullptr;
176
    }
177

178
    // Locate the anchor view's qgraphicsitemview
179
    QList<QGraphicsItem*> list = childItems();
180

181
    for (QList<QGraphicsItem*>::iterator it = list.begin(); it != list.end(); ++it) {
182
        QGIView *view = dynamic_cast<QGIView *>(*it);
183
        if(view && strcmp(view->getViewName(), anchorView->getNameInDocument()) == 0) {
184
              return view;
185
        }
186
    }
187
    return nullptr;
188
}
189

190
//QGIPG does not rotate. Only individual views rotate
191
void QGIProjGroup::rotateView()
192
{
193
    Base::Console().Warning("QGIPG: Projection Groups do not rotate. Change ignored\n");
194
}
195

196
void QGIProjGroup::drawBorder()
197
{
198
//QGIProjGroup does not have a border!
199
//    Base::Console().Message("TRACE - QGIProjGroup::drawBorder - doing nothing!!\n");
200
}
201

202

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.