~ubuntu-branches/ubuntu/wily/psi/wily-proposed

« back to all changes in this revision

Viewing changes to src/whiteboarding/wbscene.cpp

  • Committer: Package Import Robot
  • Author(s): Jan Niehusmann
  • Date: 2014-07-01 21:49:34 UTC
  • mfrom: (6.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20140701214934-gt4dkgm94byi4vnn
Tags: 0.15-1
* New upstream version
* set debhelper compat level to 9
* set Standards-Version to 3.9.5 (no further changes)
* add lintian override regarding license-problem-non-free-RFC
* use qconf to regenerate configure script
* implement hardening using buildflags instead of hardening-wrapper

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
 *
15
15
 * You should have received a copy of the GNU General Public License
16
16
 * along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 *
19
19
 */
20
20
 
21
21
#include "wbscene.h"
22
22
 
23
23
WbScene::WbScene(SxeSession* session, QObject * parent) : QGraphicsScene(parent) {
24
 
    session_ = session;
 
24
        session_ = session;
25
25
};
26
26
 
27
27
void WbScene::queueTransformationRegeneration(WbItem* item) {
32
32
void WbScene::regenerateTransformations() {
33
33
        foreach(QPointer<WbItem> item, pendingTranformations_) {
34
34
                if(item) {
35
 
            // qDebug() << QString("Regenerating %1 transform.").arg((unsigned int) &(*item)).toAscii();
 
35
                        // qDebug() << QString("Regenerating %1 transform.").arg((unsigned int) &(*item)).toAscii();
36
36
                        item->regenerateTransform();
37
37
                }
38
38
        }
39
39
        pendingTranformations_.clear();
40
 
    session_->flush();
 
40
        session_->flush();
41
41
}
42
42
 
43
43
QPointF WbScene::selectionCenter() const {
44
 
    QList<QGraphicsItem*> items = selectedItems();
45
 
 
46
 
    if(items.size() < 1)
47
 
        return QPointF();
48
 
 
49
 
    QRectF box = items.at(0)->sceneBoundingRect();
50
 
    foreach(QGraphicsItem* item, items) {
51
 
        box = box.united(item->sceneBoundingRect());
52
 
    }
53
 
 
54
 
    // qDebug() << QString("Selection center: %1 %2").arg(box.center().x()).arg(box.center().y()).toAscii();
55
 
 
56
 
    return box.center();
 
44
        QList<QGraphicsItem*> items = selectedItems();
 
45
 
 
46
        if(items.size() < 1) {
 
47
                return QPointF();
 
48
        }
 
49
 
 
50
        QRectF box = items.at(0)->sceneBoundingRect();
 
51
        foreach(QGraphicsItem* item, items) {
 
52
                box = box.united(item->sceneBoundingRect());
 
53
        }
 
54
 
 
55
        // qDebug() << QString("Selection center: %1 %2").arg(box.center().x()).arg(box.center().y()).toAscii();
 
56
 
 
57
        return box.center();
57
58
}
58
59
 
59
60
void WbScene::bringForward(int n) {
60
 
    bring(n, false);
 
61
        bring(n, false);
61
62
}
62
63
 
63
64
void WbScene::bringToFront() {
64
 
    bring(1, true);
 
65
        bring(1, true);
65
66
}
66
67
 
67
68
void WbScene::sendBackwards(int n) {
68
 
    bring(-n, false);
 
69
        bring(-n, false);
69
70
}
70
71
 
71
72
void WbScene::sendToBack() {
72
 
    bring(-1, true);
 
73
        bring(-1, true);
73
74
}
74
75
 
75
76
static bool zValueLessThan(QGraphicsItem* item1, QGraphicsItem* item2) {
76
 
    return (item1->zValue() < item2->zValue());
 
77
        return (item1->zValue() < item2->zValue());
77
78
}
78
79
 
79
80
void WbScene::group() {
80
 
    if(selectedItems().size() > 1) {
81
 
        // Create the group
82
 
        QDomElement temp = QDomDocument().createElement("g");
83
 
        temp.setAttribute("id", "e" + SxeSession::generateUUID());
84
 
        const QDomNode group = session_->insertNodeAfter(temp, session_->document().documentElement());
85
 
 
86
 
        // Arrange the items by Z coordinate
87
 
        QList<QGraphicsItem*> selected = selectedItems();
88
 
        qSort(selected.begin(), selected.end(), zValueLessThan);
89
 
 
90
 
        // Reparent each selected item
91
 
        foreach(QGraphicsItem* item, selected) {
92
 
            WbItem* wbitem = dynamic_cast<WbItem*>(item);
93
 
            if(wbitem)
94
 
                session_->insertNodeAfter(wbitem->node(), group);
95
 
        }
96
 
        clearSelection();
97
 
        
98
 
        session_->flush();
99
 
    }
 
81
        if(selectedItems().size() > 1) {
 
82
                // Create the group
 
83
                QDomElement temp = QDomDocument().createElement("g");
 
84
                temp.setAttribute("id", "e" + SxeSession::generateUUID());
 
85
                const QDomNode group = session_->insertNodeAfter(temp, session_->document().documentElement());
 
86
 
 
87
                // Arrange the items by Z coordinate
 
88
                QList<QGraphicsItem*> selected = selectedItems();
 
89
                qSort(selected.begin(), selected.end(), zValueLessThan);
 
90
 
 
91
                // Reparent each selected item
 
92
                foreach(QGraphicsItem* item, selected) {
 
93
                        WbItem* wbitem = dynamic_cast<WbItem*>(item);
 
94
                        if(wbitem)
 
95
                                session_->insertNodeAfter(wbitem->node(), group);
 
96
                }
 
97
                clearSelection();
 
98
 
 
99
                session_->flush();
 
100
        }
100
101
}
101
102
 
102
103
void WbScene::ungroup() {
103
 
    foreach(QGraphicsItem* item, selectedItems()) {        
104
 
        // find the QDomElement matching the selected item
105
 
        WbItem* wbitem = dynamic_cast<WbItem*>(item);
106
 
        if(wbitem) {
107
 
            QDomElement group = wbitem->node().toElement();
108
 
 
109
 
            if(group.nodeName() == "g") {
110
 
                QDomNodeList children = group.childNodes();
111
 
 
112
 
                QMatrix groupTransform = WbItem::parseSvgTransform(group.attribute("transform"));
113
 
 
114
 
                for(int i = children.size() - 1; i >= 0; i--) {
115
 
                    QDomElement child = children.at(i).toElement();
116
 
                    if(!child.isNull()) {
117
 
 
118
 
                        if(!groupTransform.isIdentity()) {
119
 
                            // combine the transformations of the group and the child
120
 
                            QMatrix childTransform = WbItem::parseSvgTransform(child.attribute("transform"));
121
 
                            session_->setAttribute(child, "transform", WbItem::toSvgTransform(childTransform * groupTransform));
122
 
                        }
123
 
 
124
 
                        // move the child from the group to the root <svg/>
125
 
                        session_->insertNodeAfter(children.at(i), session_->document().documentElement(), group);
126
 
 
127
 
                    }
128
 
                }
129
 
            
130
 
                // delete the group itself
131
 
                session_->removeNode(group);
132
 
            }
133
 
 
134
 
        }
135
 
    }
136
 
    clearSelection();
137
 
 
138
 
    session_->flush();
 
104
        foreach(QGraphicsItem* item, selectedItems()) {
 
105
                // find the QDomElement matching the selected item
 
106
                WbItem* wbitem = dynamic_cast<WbItem*>(item);
 
107
                if(wbitem) {
 
108
                        QDomElement group = wbitem->node().toElement();
 
109
 
 
110
                        if(group.nodeName() == "g") {
 
111
                                QDomNodeList children = group.childNodes();
 
112
 
 
113
                                QMatrix groupTransform = WbItem::parseSvgTransform(group.attribute("transform"));
 
114
 
 
115
                                for(int i = children.size() - 1; i >= 0; i--) {
 
116
                                        QDomElement child = children.at(i).toElement();
 
117
                                        if(!child.isNull()) {
 
118
 
 
119
                                                if(!groupTransform.isIdentity()) {
 
120
                                                        // combine the transformations of the group and the child
 
121
                                                        QMatrix childTransform = WbItem::parseSvgTransform(child.attribute("transform"));
 
122
                                                        session_->setAttribute(child, "transform", WbItem::toSvgTransform(childTransform * groupTransform));
 
123
                                                }
 
124
 
 
125
                                                // move the child from the group to the root <svg/>
 
126
                                                session_->insertNodeAfter(children.at(i), session_->document().documentElement(), group);
 
127
 
 
128
                                        }
 
129
                                }
 
130
 
 
131
                                // delete the group itself
 
132
                                session_->removeNode(group);
 
133
                        }
 
134
 
 
135
                }
 
136
        }
 
137
        clearSelection();
 
138
 
 
139
        session_->flush();
139
140
}
140
141
 
141
142
void WbScene::bring(int n, bool toExtremum) {
142
 
    if(n == 0)
143
 
         return;
144
 
 
145
 
    // bring each selected item
146
 
    foreach(QGraphicsItem* selecteditem, selectedItems()) {
147
 
 
148
 
        if (!(selecteditem->parentItem() && selecteditem->parentItem()->isSelected())) {
149
 
 
150
 
            WbItem* selectedwbitem = dynamic_cast<WbItem*>(selecteditem);
151
 
 
152
 
            if(selectedwbitem) {
153
 
 
154
 
                QList<QGraphicsItem*> colliding = selecteditem->collidingItems();
155
 
                
156
 
                // find the relative position of selecteditem itself and
157
 
                // remove other selected items from the list colliding
158
 
                int i = 0;
159
 
                while(i < colliding.size()) {
160
 
                    if(selectedItems().contains(colliding[i])) {
161
 
                        colliding.removeAt(i);
162
 
                    } else if(colliding[i]->zValue() < selecteditem->zValue()) {
163
 
                        break;
164
 
                    } else
165
 
                        i++;
166
 
                }
167
 
                if(n < 0)
168
 
                    i--;
169
 
 
170
 
                // bring the selected node n levels above/below the item itself
171
 
                if(colliding.size() > 0) {
172
 
                    WbItem* referencewbitem;
173
 
                    if(n > 0) {
174
 
                        if(i - n > 0 && !toExtremum)
175
 
                            referencewbitem = dynamic_cast<WbItem*>(colliding[i - n]);
176
 
                        else
177
 
                            referencewbitem = dynamic_cast<WbItem*>(colliding[0]);
178
 
                    } else {
179
 
                        if(i - n < colliding.size() && !toExtremum)
180
 
                            referencewbitem = dynamic_cast<WbItem*>(colliding[i - n]);
181
 
                        else
182
 
                            referencewbitem = dynamic_cast<WbItem*>(colliding[colliding.size() - 1]);
183
 
                    }
184
 
 
185
 
                    if(referencewbitem && !referencewbitem->node().isNull()) {
186
 
                        if(n > 0)
187
 
                            session_->insertNodeAfter(selectedwbitem->node(), selectedwbitem->node().parentNode(), referencewbitem->node());
188
 
                        else
189
 
                            session_->insertNodeBefore(selectedwbitem->node(), selectedwbitem->node().parentNode(), referencewbitem->node());
190
 
                    }
191
 
                }
192
 
            }
193
 
        }
194
 
    }
195
 
 
196
 
    session_->flush();
 
143
        if(n == 0)
 
144
                 return;
 
145
 
 
146
        // bring each selected item
 
147
        foreach(QGraphicsItem* selecteditem, selectedItems()) {
 
148
 
 
149
                if (!(selecteditem->parentItem() && selecteditem->parentItem()->isSelected())) {
 
150
 
 
151
                        WbItem* selectedwbitem = dynamic_cast<WbItem*>(selecteditem);
 
152
 
 
153
                        if(selectedwbitem) {
 
154
 
 
155
                                QList<QGraphicsItem*> colliding = selecteditem->collidingItems();
 
156
 
 
157
                                // find the relative position of selecteditem itself and
 
158
                                // remove other selected items from the list colliding
 
159
                                int i = 0;
 
160
                                while(i < colliding.size()) {
 
161
                                        if(selectedItems().contains(colliding[i])) {
 
162
                                                colliding.removeAt(i);
 
163
                                        } else if(colliding[i]->zValue() < selecteditem->zValue()) {
 
164
                                                break;
 
165
                                        } else
 
166
                                                i++;
 
167
                                }
 
168
                                if(n < 0)
 
169
                                        i--;
 
170
 
 
171
                                // bring the selected node n levels above/below the item itself
 
172
                                if(colliding.size() > 0) {
 
173
                                        WbItem* referencewbitem;
 
174
                                        if(n > 0) {
 
175
                                                if(i - n > 0 && !toExtremum)
 
176
                                                        referencewbitem = dynamic_cast<WbItem*>(colliding[i - n]);
 
177
                                                else
 
178
                                                        referencewbitem = dynamic_cast<WbItem*>(colliding[0]);
 
179
                                        } else {
 
180
                                                if(i - n < colliding.size() && !toExtremum)
 
181
                                                        referencewbitem = dynamic_cast<WbItem*>(colliding[i - n]);
 
182
                                                else
 
183
                                                        referencewbitem = dynamic_cast<WbItem*>(colliding[colliding.size() - 1]);
 
184
                                        }
 
185
 
 
186
                                        if(referencewbitem && !referencewbitem->node().isNull()) {
 
187
                                                if(n > 0)
 
188
                                                        session_->insertNodeAfter(selectedwbitem->node(), selectedwbitem->node().parentNode(), referencewbitem->node());
 
189
                                                else
 
190
                                                        session_->insertNodeBefore(selectedwbitem->node(), selectedwbitem->node().parentNode(), referencewbitem->node());
 
191
                                        }
 
192
                                }
 
193
                        }
 
194
                }
 
195
        }
 
196
 
 
197
        session_->flush();
197
198
}