~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/tools/tool_perspectivegrid/kis_tool_perspectivegrid.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <KoViewConverter.h>
40
40
#include <canvas/kis_perspective_grid_manager.h>
41
41
#include <kis_canvas_resource_provider.h>
42
 
#include <kis_selected_transaction.h>
43
42
#include <kis_painter.h>
44
43
#include <kis_paintop_registry.h>
45
44
#include <kis_view2.h>
52
51
{
53
52
    Q_ASSERT(m_canvas);
54
53
    setObjectName("tool_perspectivegrid");
55
 
 
56
 
    m_drawing = false;
57
54
}
58
55
 
59
56
KisToolPerspectiveGrid::~KisToolPerspectiveGrid()
66
63
 
67
64
    m_canvas->view()->perspectiveGridManager()->startEdition();
68
65
    if (! m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid()->hasSubGrids()) {
69
 
        m_mode = MODE_CREATION;
 
66
        m_internalMode = MODE_CREATION;
70
67
        m_points.clear();
71
68
    } else {
72
 
        m_mode = MODE_EDITING;
 
69
        m_internalMode = MODE_EDITING;
73
70
        useCursor(KisCursor::arrowCursor());
74
71
        m_canvas->view()->perspectiveGridManager()->setVisible(true);
75
72
        m_canvas->updateCanvas(); // TODO only the correct rect
79
76
void KisToolPerspectiveGrid::deactivate()
80
77
{
81
78
    m_canvas->view()->perspectiveGridManager()->stopEdition();
82
 
    if (m_mode == MODE_CREATION) {
 
79
    if (m_internalMode == MODE_CREATION) {
83
80
        m_points.clear();
84
 
        m_drawing = false;
85
81
    }
86
82
    m_canvas->updateCanvas();
87
83
}
113
109
 
114
110
void KisToolPerspectiveGrid::mousePressEvent(KoPointerEvent *event)
115
111
{
116
 
    KisPerspectiveGrid* pGrid = m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid();
117
 
    if (!pGrid->hasSubGrids() && m_mode != MODE_CREATION) { // it's possible that the perspectiv grid was cleared
118
 
        m_mode = MODE_CREATION;
119
 
        m_points.clear();
120
 
    }
121
 
    if (m_mode == MODE_CREATION && event->button() == Qt::LeftButton) {
122
 
        m_drawing = true;
123
 
        m_currentPt = event->point;
124
 
 
125
 
        if (m_points.isEmpty()) {
126
 
            m_points.append(m_currentPt);
127
 
            m_isFirstPoint = true;
 
112
    if(PRESS_CONDITION(event, KisTool::HOVER_MODE,
 
113
                       Qt::LeftButton, Qt::NoModifier)) {
 
114
 
 
115
        setMode(KisTool::PAINT_MODE);
 
116
 
 
117
        KisPerspectiveGrid* pGrid = m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid();
 
118
        if (!pGrid->hasSubGrids() && m_internalMode != MODE_CREATION) { // it's possible that the perspectiv grid was cleared
 
119
            m_internalMode = MODE_CREATION;
 
120
            m_points.clear();
 
121
        }
 
122
        if (m_internalMode == MODE_CREATION) {
 
123
            m_currentPt = event->point;
 
124
 
 
125
            if (m_points.isEmpty()) {
 
126
                m_points.append(m_currentPt);
 
127
                m_isFirstPoint = true;
 
128
            } else {
 
129
                m_isFirstPoint = false;
 
130
            }
 
131
            m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
132
        } else if (m_internalMode == MODE_EDITING) {
 
133
            // Look for the handle which was pressed
 
134
            QPointF mousep = m_canvas->viewConverter()->documentToView(event->point);
 
135
            for (QList<KisSubPerspectiveGrid*>::const_iterator it = pGrid->begin(); it != pGrid->end(); ++it) {
 
136
                KisSubPerspectiveGrid* grid = *it;
 
137
                QPointF gridCenter = grid->center();
 
138
                dbgKrita << "click at " << event->point << " top left at " << *grid->topLeft();
 
139
                if (m_selectedNode1 = nodeNearPoint(grid, mousep)) {
 
140
                    m_internalMode = MODE_DRAGING_NODE;
 
141
                    break;
 
142
                } else if (mouseNear(mousep, ((pixelToView(*grid->topLeft()) + pixelToView(*grid->bottomLeft()))*0.5))) {
 
143
                    dbgPlugins << " PRESS LEFT HANDLE";
 
144
                    m_internalMode = MODE_DRAGING_TRANSLATING_TWONODES;
 
145
                    m_selectedNode1 = new KisPerspectiveGridNode(*grid->topLeft());
 
146
                    m_selectedNode2 = new KisPerspectiveGridNode(*grid->bottomLeft());
 
147
                    KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(m_selectedNode1, grid->topLeft() , grid->bottomLeft(), m_selectedNode2);
 
148
                    m_dragEnd = convertToPixelCoord(event->point);
 
149
                    pGrid->addNewSubGrid(newsubgrid);
 
150
                    m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
151
                    break;
 
152
                } else if (mouseNear(mousep, ((pixelToView(*grid->topRight()) + pixelToView(*grid->bottomRight()))*0.5))) {
 
153
                    dbgPlugins << " PRESS RIGHT HANDLE";
 
154
                    m_internalMode = MODE_DRAGING_TRANSLATING_TWONODES;
 
155
                    m_selectedNode1 = new KisPerspectiveGridNode(*grid->topRight());
 
156
                    m_selectedNode2 = new KisPerspectiveGridNode(*grid->bottomRight());
 
157
                    KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(grid->topRight(), m_selectedNode1, m_selectedNode2, grid->bottomRight());
 
158
                    m_dragEnd = convertToPixelCoord(event->point);
 
159
                    pGrid->addNewSubGrid(newsubgrid);
 
160
                    m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
161
                    break;
 
162
                } else if (mouseNear(mousep, ((pixelToView(*grid->topLeft()) + pixelToView(*grid->topRight()))*0.5))) {
 
163
                    dbgPlugins << " PRESS TOP HANDLE";
 
164
                    m_internalMode = MODE_DRAGING_TRANSLATING_TWONODES;
 
165
                    m_selectedNode1 = new KisPerspectiveGridNode(*grid->topLeft());
 
166
                    m_selectedNode2 = new KisPerspectiveGridNode(*grid->topRight());
 
167
                    KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(m_selectedNode1, m_selectedNode2,  grid->topRight(), grid->topLeft());
 
168
                    m_dragEnd = convertToPixelCoord(event->point);
 
169
                    pGrid->addNewSubGrid(newsubgrid);
 
170
                    m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
171
                    break;
 
172
                } else if (mouseNear(mousep, ((pixelToView(*grid->bottomLeft()) + pixelToView(*grid->bottomRight()))*0.5))) {
 
173
                    dbgPlugins << " PRESS BOTTOM HANDLE";
 
174
                    m_internalMode = MODE_DRAGING_TRANSLATING_TWONODES;
 
175
                    m_selectedNode1 = new KisPerspectiveGridNode(*grid->bottomLeft());
 
176
                    m_selectedNode2 = new KisPerspectiveGridNode(*grid->bottomRight());
 
177
                    KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(grid->bottomLeft(), grid->bottomRight(), m_selectedNode2, m_selectedNode1);
 
178
                    m_dragEnd = convertToPixelCoord(event->point);
 
179
                    pGrid->addNewSubGrid(newsubgrid);
 
180
                    m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
181
                    break;
 
182
                } else if (pixelToView(QRectF((gridCenter.x() - 16), (gridCenter.y() - 16), 32, 32)).contains(mousep)) {
 
183
                    dbgPlugins << " PRESS DELETE ICON";
 
184
                    pGrid->deleteSubGrid(grid);
 
185
                    m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
186
                    if (!pGrid->hasSubGrids()) {
 
187
                        m_internalMode = MODE_CREATION;
 
188
                        useCursor(KisCursor::load("tool_perspectivegrid_cursor.png", 6, 6));
 
189
                        m_points.clear();
 
190
                    }
 
191
                    break;
 
192
                }
 
193
            }
 
194
        }
 
195
 
 
196
 
 
197
    }
 
198
    else {
 
199
        KisTool::mousePressEvent(event);
 
200
    }
 
201
}
 
202
 
 
203
 
 
204
void KisToolPerspectiveGrid::mouseMoveEvent(KoPointerEvent *event)
 
205
{
 
206
    if(MOVE_CONDITION(event, KisTool::PAINT_MODE)) {
 
207
        if (m_internalMode == MODE_CREATION) {
 
208
            if (!m_points.isEmpty()) {
 
209
                // get current mouse position
 
210
                m_currentPt = event->point;
 
211
                // draw new lines on canvas
 
212
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
213
            }
128
214
        } else {
129
 
            m_isFirstPoint = false;
 
215
            if (m_internalMode == MODE_DRAGING_NODE) {
 
216
                QPointF pos = convertToPixelCoord(event);
 
217
                m_selectedNode1->setX(pos.x());
 
218
                m_selectedNode1->setY(pos.y());
 
219
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
220
            }
 
221
            if (m_internalMode == MODE_DRAGING_TRANSLATING_TWONODES) {
 
222
                QPointF translate = convertToPixelCoord(event->point) - m_dragEnd;
 
223
                m_dragEnd = convertToPixelCoord(event->point);
 
224
                *m_selectedNode1 += translate;;
 
225
                *m_selectedNode2 += translate;;
 
226
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
227
            }
130
228
        }
131
 
        m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
132
 
    } else if (m_mode == MODE_EDITING && event->button() == Qt::LeftButton) {
133
 
        // Look for the handle which was pressed
 
229
        bool wasHiglightedNode = m_higlightedNode != 0;
134
230
        QPointF mousep = m_canvas->viewConverter()->documentToView(event->point);
 
231
        KisPerspectiveGrid* pGrid = m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid();
135
232
        for (QList<KisSubPerspectiveGrid*>::const_iterator it = pGrid->begin(); it != pGrid->end(); ++it) {
136
233
            KisSubPerspectiveGrid* grid = *it;
137
 
            QPointF gridCenter = grid->center();
138
 
            dbgKrita << "click at " << event->point << " top left at " << *grid->topLeft();
139
 
            if (m_selectedNode1 = nodeNearPoint(grid, mousep)) {
140
 
                m_mode = MODE_DRAGING_NODE;
141
 
                break;
142
 
            } else if (mouseNear(mousep, ((pixelToView(*grid->topLeft()) + pixelToView(*grid->bottomLeft()))*0.5))) {
143
 
                dbgPlugins << " PRESS LEFT HANDLE";
144
 
                m_mode = MODE_DRAGING_TRANSLATING_TWONODES;
145
 
                m_selectedNode1 = new KisPerspectiveGridNode(*grid->topLeft());
146
 
                m_selectedNode2 = new KisPerspectiveGridNode(*grid->bottomLeft());
147
 
                KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(m_selectedNode1, grid->topLeft() , grid->bottomLeft(), m_selectedNode2);
148
 
                m_dragEnd = convertToPixelCoord(event->point);
149
 
                pGrid->addNewSubGrid(newsubgrid);
150
 
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
151
 
                break;
152
 
            } else if (mouseNear(mousep, ((pixelToView(*grid->topRight()) + pixelToView(*grid->bottomRight()))*0.5))) {
153
 
                dbgPlugins << " PRESS RIGHT HANDLE";
154
 
                m_mode = MODE_DRAGING_TRANSLATING_TWONODES;
155
 
                m_selectedNode1 = new KisPerspectiveGridNode(*grid->topRight());
156
 
                m_selectedNode2 = new KisPerspectiveGridNode(*grid->bottomRight());
157
 
                KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(grid->topRight(), m_selectedNode1, m_selectedNode2, grid->bottomRight());
158
 
                m_dragEnd = convertToPixelCoord(event->point);
159
 
                pGrid->addNewSubGrid(newsubgrid);
160
 
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
161
 
                break;
162
 
            } else if (mouseNear(mousep, ((pixelToView(*grid->topLeft()) + pixelToView(*grid->topRight()))*0.5))) {
163
 
                dbgPlugins << " PRESS TOP HANDLE";
164
 
                m_mode = MODE_DRAGING_TRANSLATING_TWONODES;
165
 
                m_selectedNode1 = new KisPerspectiveGridNode(*grid->topLeft());
166
 
                m_selectedNode2 = new KisPerspectiveGridNode(*grid->topRight());
167
 
                KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(m_selectedNode1, m_selectedNode2,  grid->topRight(), grid->topLeft());
168
 
                m_dragEnd = convertToPixelCoord(event->point);
169
 
                pGrid->addNewSubGrid(newsubgrid);
170
 
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
171
 
                break;
172
 
            } else if (mouseNear(mousep, ((pixelToView(*grid->bottomLeft()) + pixelToView(*grid->bottomRight()))*0.5))) {
173
 
                dbgPlugins << " PRESS BOTTOM HANDLE";
174
 
                m_mode = MODE_DRAGING_TRANSLATING_TWONODES;
175
 
                m_selectedNode1 = new KisPerspectiveGridNode(*grid->bottomLeft());
176
 
                m_selectedNode2 = new KisPerspectiveGridNode(*grid->bottomRight());
177
 
                KisSubPerspectiveGrid* newsubgrid = new KisSubPerspectiveGrid(grid->bottomLeft(), grid->bottomRight(), m_selectedNode2, m_selectedNode1);
178
 
                m_dragEnd = convertToPixelCoord(event->point);
179
 
                pGrid->addNewSubGrid(newsubgrid);
180
 
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
181
 
                break;
182
 
            } else if (pixelToView(QRectF((gridCenter.x() - 16), (gridCenter.y() - 16), 32, 32)).contains(mousep)) {
183
 
                dbgPlugins << " PRESS DELETE ICON";
184
 
                pGrid->deleteSubGrid(grid);
185
 
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
186
 
                if (!pGrid->hasSubGrids()) {
187
 
                    m_mode = MODE_CREATION;
188
 
                    useCursor(KisCursor::load("tool_perspectivegrid_cursor.png", 6, 6));
189
 
                    m_points.clear();
 
234
            if (m_higlightedNode = nodeNearPoint(grid, mousep)) {
 
235
                if (m_higlightedNode == m_selectedNode1 || m_higlightedNode == m_selectedNode2) {
 
236
                    m_higlightedNode = 0;
 
237
                } else {
 
238
                    m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
239
                    break;
190
240
                }
191
 
                break;
192
 
            }
193
 
        }
194
 
    }
195
 
}
196
 
 
197
 
 
198
 
void KisToolPerspectiveGrid::mouseMoveEvent(KoPointerEvent *event)
199
 
{
200
 
    if (m_mode == MODE_CREATION) {
201
 
        if (!m_points.isEmpty()) {
202
 
            // get current mouse position
203
 
            m_currentPt = event->point;
204
 
            // draw new lines on canvas
205
 
            m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
206
 
        }
207
 
    } else {
208
 
        if (m_mode == MODE_DRAGING_NODE) {
209
 
            QPointF pos = convertToPixelCoord(event);
210
 
            m_selectedNode1->setX(pos.x());
211
 
            m_selectedNode1->setY(pos.y());
212
 
            m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
213
 
        }
214
 
        if (m_mode == MODE_DRAGING_TRANSLATING_TWONODES) {
215
 
            QPointF translate = convertToPixelCoord(event->point) - m_dragEnd;
216
 
            m_dragEnd = convertToPixelCoord(event->point);
217
 
            *m_selectedNode1 += translate;;
218
 
            *m_selectedNode2 += translate;;
219
 
            m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
220
 
        }
221
 
    }
222
 
    bool wasHiglightedNode = m_higlightedNode != 0;
223
 
    QPointF mousep = m_canvas->viewConverter()->documentToView(event->point);
224
 
    KisPerspectiveGrid* pGrid = m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid();
225
 
    for (QList<KisSubPerspectiveGrid*>::const_iterator it = pGrid->begin(); it != pGrid->end(); ++it) {
226
 
        KisSubPerspectiveGrid* grid = *it;
227
 
        if (m_higlightedNode = nodeNearPoint(grid, mousep)) {
228
 
            if (m_higlightedNode == m_selectedNode1 || m_higlightedNode == m_selectedNode2) {
229
 
                m_higlightedNode = 0;
230
 
            } else {
231
 
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
232
 
                break;
233
 
            }
234
 
        }
235
 
    }
236
 
    if (wasHiglightedNode && !m_higlightedNode) {
237
 
        m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
241
            }
 
242
        }
 
243
        if (wasHiglightedNode && !m_higlightedNode) {
 
244
            m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
245
        }
 
246
    }
 
247
    else {
 
248
        KisTool::mouseMoveEvent(event);
238
249
    }
239
250
}
240
251
 
241
252
void KisToolPerspectiveGrid::mouseReleaseEvent(KoPointerEvent *event)
242
253
{
 
254
    if(RELEASE_CONDITION(event, KisTool::PAINT_MODE, Qt::LeftButton)) {
 
255
        setMode(KisTool::HOVER_MODE);
243
256
 
244
 
    if (m_mode == MODE_CREATION) {
245
 
        if (m_drawing && event->button() == Qt::LeftButton && !m_isFirstPoint)  {
246
 
            m_points.append(m_currentPt);
247
 
            if (m_points.size() == 4) { // wow we have a grid, isn't that cool ?
248
 
                m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid()->addNewSubGrid(
249
 
                    new KisSubPerspectiveGrid(
250
 
                        new KisPerspectiveGridNode(convertToPixelCoord(m_points[0])),
251
 
                        new KisPerspectiveGridNode(convertToPixelCoord(m_points[1])),
252
 
                        new KisPerspectiveGridNode(convertToPixelCoord(m_points[2])),
253
 
                        new KisPerspectiveGridNode(convertToPixelCoord(m_points[3]))));
254
 
                m_canvas->view()->perspectiveGridManager()->setVisible(true);
255
 
                m_mode = MODE_EDITING;
256
 
                useCursor(KisCursor::arrowCursor());
 
257
        if (m_internalMode == MODE_CREATION) {
 
258
            if (!m_isFirstPoint)  {
 
259
                m_points.append(m_currentPt);
 
260
                if (m_points.size() == 4) { // wow we have a grid, isn't that cool ?
 
261
                    m_canvas->view()->resourceProvider()->currentImage()->perspectiveGrid()->addNewSubGrid(
 
262
                        new KisSubPerspectiveGrid(
 
263
                            new KisPerspectiveGridNode(convertToPixelCoord(m_points[0])),
 
264
                            new KisPerspectiveGridNode(convertToPixelCoord(m_points[1])),
 
265
                            new KisPerspectiveGridNode(convertToPixelCoord(m_points[2])),
 
266
                            new KisPerspectiveGridNode(convertToPixelCoord(m_points[3]))));
 
267
                    m_canvas->view()->perspectiveGridManager()->setVisible(true);
 
268
                    m_internalMode = MODE_EDITING;
 
269
                    useCursor(KisCursor::arrowCursor());
 
270
                }
257
271
            }
258
 
        }
259
 
        m_drawing = false;
260
 
        m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
261
 
    } else {
262
 
        m_mode = MODE_EDITING;
263
 
        // Check if there is a need for merging two nodes
264
 
        if (m_higlightedNode && m_selectedNode2 == 0) {
265
 
            m_higlightedNode->mergeWith(m_selectedNode1);
266
272
            m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
273
        } else {
 
274
            m_internalMode = MODE_EDITING;
 
275
            // Check if there is a need for merging two nodes
 
276
            if (m_higlightedNode && m_selectedNode2 == 0) {
 
277
                m_higlightedNode->mergeWith(m_selectedNode1);
 
278
                m_canvas->updateCanvas(); // TODO update only the relevant part of the canvas
 
279
            }
 
280
            m_selectedNode1 = 0;
 
281
            m_selectedNode2 = 0;
267
282
        }
268
 
        m_selectedNode1 = 0;
269
 
        m_selectedNode2 = 0;
270
 
    }
271
 
 
 
283
    }
 
284
    else {
 
285
        KisTool::mouseReleaseEvent(event);
 
286
    }
272
287
}
273
288
 
274
289
void KisToolPerspectiveGrid::paint(QPainter& gc, const KoViewConverter &converter)
275
290
{
276
291
    Q_UNUSED(converter);
277
 
    if (m_mode == MODE_CREATION) {
 
292
    if (m_internalMode == MODE_CREATION) {
278
293
        drawGridCreation(gc);
279
294
    } else {
280
295
        drawGrid(gc);