Krita Source Code Documentation
Loading...
Searching...
No Matches
Viewport Class Reference

#include <KoCanvasControllerWidgetViewport_p.h>

+ Inheritance diagram for Viewport:

Public Member Functions

QWidget * canvas () const
 
void handleDragEnterEvent (QDragEnterEvent *event)
 
void handleDragLeaveEvent (QDragLeaveEvent *event)
 
void handleDragMoveEvent (QDragMoveEvent *event)
 
void handleDropEvent (QDropEvent *event)
 
void handlePaintEvent (QPainter &gc, QPaintEvent *event)
 
void resetLayout ()
 
void setCanvas (QWidget *canvas)
 
 Viewport (KoCanvasControllerWidget *parent)
 
 ~Viewport () override
 

Private Member Functions

QPointF correctPosition (const QPoint &point) const
 
void repaint (KoShape *shape)
 

Private Attributes

QWidget * m_canvas
 
KoShapem_draggedShape
 
KoCanvasControllerWidgetm_parent
 

Detailed Description

Definition at line 18 of file KoCanvasControllerWidgetViewport_p.h.

Constructor & Destructor Documentation

◆ Viewport()

Viewport::Viewport ( KoCanvasControllerWidget * parent)
explicit

Definition at line 39 of file KoCanvasControllerWidgetViewport_p.cpp.

40 : QWidget(parent)
42 , m_canvas(0)
43{
44 setAutoFillBackground(true);
45 setAcceptDrops(true);
46 setMouseTracking(true);
48}
KoCanvasControllerWidget * m_parent
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327

References m_parent.

◆ ~Viewport()

Viewport::~Viewport ( )
inlineoverride

Definition at line 25 of file KoCanvasControllerWidgetViewport_p.h.

25{}

Member Function Documentation

◆ canvas()

QWidget * Viewport::canvas ( ) const
inline

Definition at line 28 of file KoCanvasControllerWidgetViewport_p.h.

28 {
29 return m_canvas;
30 }

References m_canvas.

◆ correctPosition()

QPointF Viewport::correctPosition ( const QPoint & point) const
private

Definition at line 214 of file KoCanvasControllerWidgetViewport_p.cpp.

215{
216 KisCanvas2 *kisCanvas = dynamic_cast<KisCanvas2*>(m_parent->canvas());
217 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(kisCanvas, QPointF());
218 return kisCanvas->coordinatesConverter()->widgetToDocument(point);
219}
KisCoordinatesConverter * coordinatesConverter
_Private::Traits< T >::Result widgetToDocument(const T &obj) const
QPointer< KoCanvasBase > canvas
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129

References KoCanvasControllerWidget::canvas, KisCanvas2::coordinatesConverter, KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE, m_parent, and KisCoordinatesConverter::widgetToDocument().

◆ handleDragEnterEvent()

void Viewport::handleDragEnterEvent ( QDragEnterEvent * event)

Definition at line 63 of file KoCanvasControllerWidgetViewport_p.cpp.

64{
65 // if not a canvas set then ignore this, makes it possible to assume
66 // we have a canvas in all the support methods.
67 if (!(m_parent->canvas() && m_parent->canvas()->canvasWidget())) {
68 event->ignore();
69 return;
70 }
71
72 delete m_draggedShape;
74
75 // only allow dropping when active layer is editable
76 KoSelection *selection = m_parent->canvas()->shapeManager()->selection();
77 KoShapeLayer *activeLayer = selection->activeLayer();
78 if (activeLayer && (!activeLayer->isShapeEditable() || activeLayer->isGeometryProtected())) {
79 event->ignore();
80 return;
81 }
82
83 const QMimeData *data = event->mimeData();
84
85 if (data->hasFormat(SHAPETEMPLATE_MIMETYPE) ||
86 data->hasFormat(SHAPEID_MIMETYPE) ||
87 data->hasFormat("image/svg+xml"))
88 {
89 if (data->hasFormat("image/svg+xml")) {
91 QSizeF fragmentSize;
92
93 QList<KoShape*> shapes = KoSvgPaste::fetchShapesFromData(data->data("image/svg+xml"),
94 canvas->shapeController()->documentRectInPixels(),
95 canvas->shapeController()->pixelsPerInch(),
96 &fragmentSize);
97
98 if (!shapes.isEmpty()) {
99 m_draggedShape = shapes[0];
100 }
101 }
102 else {
103 QByteArray itemData;
104 bool isTemplate = true;
105
106 if (data->hasFormat(SHAPETEMPLATE_MIMETYPE)) {
107 itemData = data->data(SHAPETEMPLATE_MIMETYPE);
108 }
109 else if (data->hasFormat(SHAPEID_MIMETYPE)) {
110 isTemplate = false;
111 itemData = data->data(SHAPEID_MIMETYPE);
112 }
113
114
115 QDataStream dataStream(&itemData, QIODevice::ReadOnly);
116 QString id;
117 dataStream >> id;
118 QString properties;
119 if (isTemplate)
120 dataStream >> properties;
121
122 // and finally, there is a point.
123 QPointF offset;
124 dataStream >> offset;
125
126 // The rest of this method is mostly a copy paste from the KoCreateShapeStrategy
127 // So, lets remove this again when Zagge adds his new class that does this kind of thing. (KoLoadSave)
129 if (! factory) {
130 warnFlake << "Application requested a shape that is not registered '" <<
131 id << "', Ignoring";
132 event->ignore();
133 return;
134 }
135 if (isTemplate) {
136 KoProperties props;
137 props.load(properties);
138 m_draggedShape = factory->createShape(&props, m_parent->canvas()->shapeController()->resourceManager());
139 }
140 else {
141 m_draggedShape = factory->createDefaultShape(m_parent->canvas()->shapeController()->resourceManager());
142 }
143
144 if (m_draggedShape->shapeId().isEmpty()) {
145 m_draggedShape->setShapeId(factory->id());
146 }
147 }
148
149 event->setDropAction(Qt::CopyAction);
150 event->accept();
151
152 Q_ASSERT(m_draggedShape);
153 if (!m_draggedShape) return;
154
155 // calculate maximum existing shape zIndex
156
157 int pasteZIndex = 0;
158
159 {
160 QList<KoShape*> allShapes = m_parent->canvas()->shapeManager()->topLevelShapes();
161
162 if (!allShapes.isEmpty()) {
163 std::sort(allShapes.begin(), allShapes.end(), KoShape::compareShapeZIndex);
164 pasteZIndex = qMin(int(KoShape::maxZIndex), allShapes.last()->zIndex() + 1);
165 }
166 }
167
168 m_draggedShape->setZIndex(pasteZIndex);
170
171 m_parent->canvas()->shapeManager()->addShape(m_draggedShape);
172 } else {
173 event->ignore();
174 }
175}
#define warnFlake
Definition FlakeDebug.h:16
#define SHAPETEMPLATE_MIMETYPE
#define SHAPEID_MIMETYPE
const T value(const QString &id) const
void load(const QDomElement &root)
KoShapeLayer * activeLayer() const
virtual KoShape * createDefaultShape(KoDocumentResourceManager *documentResources=0) const
virtual KoShape * createShape(const KoProperties *params, KoDocumentResourceManager *documentResources=0) const
static KoShapeRegistry * instance()
void setZIndex(qint16 zIndex)
Definition KoShape.cpp:954
QString shapeId() const
Definition KoShape.cpp:1057
virtual bool isShapeEditable(bool recursive=true) const
checks recursively if the shape or one of its parents is not visible or locked
Definition KoShape.cpp:1165
static bool compareShapeZIndex(KoShape *s1, KoShape *s2)
Definition KoShape.cpp:434
bool isGeometryProtected() const
Definition KoShape.cpp:1024
void setShapeId(const QString &id)
Definition KoShape.cpp:1062
void setAbsolutePosition(const QPointF &newPosition, KoFlake::AnchorPosition anchor=KoFlake::Center)
Definition KoShape.cpp:668
static const qint16 maxZIndex
Definition KoShape.h:502
static QList< KoShape * > fetchShapesFromData(const QByteArray &data, QRectF viewportInPx, qreal resolutionPPI, QSizeF *fragmentSize=nullptr)
QPointF correctPosition(const QPoint &point) const

References KoSelection::activeLayer(), KoCanvasControllerWidget::canvas, canvas(), KoShape::compareShapeZIndex(), correctPosition(), KoShapeFactoryBase::createDefaultShape(), KoShapeFactoryBase::createShape(), KoSvgPaste::fetchShapesFromData(), KoShapeFactoryBase::id, KoShapeRegistry::instance(), KoShape::isGeometryProtected(), KoShape::isShapeEditable(), KoProperties::load(), m_draggedShape, m_parent, KoShape::maxZIndex, KoShape::setAbsolutePosition(), KoShape::setShapeId(), KoShape::setZIndex(), KoShape::shapeId(), SHAPEID_MIMETYPE, SHAPETEMPLATE_MIMETYPE, KoGenericRegistry< T >::value(), and warnFlake.

◆ handleDragLeaveEvent()

void Viewport::handleDragLeaveEvent ( QDragLeaveEvent * event)

Definition at line 245 of file KoCanvasControllerWidgetViewport_p.cpp.

246{
247 if (m_draggedShape) {
249 m_parent->canvas()->shapeManager()->remove(m_draggedShape);
250 delete m_draggedShape;
251 m_draggedShape = 0;
252 } else {
253 m_parent->canvas()->toolProxy()->dragLeaveEvent(event);
254 }
255}

References KoCanvasControllerWidget::canvas, m_draggedShape, m_parent, and repaint().

◆ handleDragMoveEvent()

void Viewport::handleDragMoveEvent ( QDragMoveEvent * event)

Definition at line 221 of file KoCanvasControllerWidgetViewport_p.cpp.

222{
223 if (!m_draggedShape) {
224 m_parent->canvas()->toolProxy()->dragMoveEvent(event, correctPosition(event->pos()));
225 return;
226 }
227
233}
virtual void update() const
Definition KoShape.cpp:605

References KoCanvasControllerWidget::canvas, correctPosition(), m_draggedShape, m_parent, repaint(), KoShape::setAbsolutePosition(), and KoShape::update().

◆ handleDropEvent()

void Viewport::handleDropEvent ( QDropEvent * event)

Definition at line 177 of file KoCanvasControllerWidgetViewport_p.cpp.

178{
179 if (!m_draggedShape) {
180 m_parent->canvas()->toolProxy()->dropEvent(event, correctPosition(event->pos()));
181 return;
182 }
183
185 m_parent->canvas()->shapeManager()->remove(m_draggedShape); // remove it to not interfere with z-index calc.
186
187 m_draggedShape->setPosition(QPointF(0, 0)); // always save position.
188 QPointF newPos = correctPosition(event->pos());
189 m_parent->canvas()->clipToDocument(m_draggedShape, newPos); // ensure the shape is dropped inside the document.
191
192
193 KUndo2Command * cmd = m_parent->canvas()->shapeController()->addShape(m_draggedShape, 0);
194
195 if (cmd) {
196 m_parent->canvas()->addCommand(cmd);
197 KoSelection *selection = m_parent->canvas()->shapeManager()->selection();
198
199 // repaint selection before selecting newly create shape
200 Q_FOREACH (KoShape * shape, selection->selectedShapes()) {
201 shape->update();
202 }
203
204 selection->deselectAll();
205 selection->select(m_draggedShape);
206 } else {
207
208 delete m_draggedShape;
209 }
210
211 m_draggedShape = 0;
212}
void deselectAll()
clear the selections list
void select(KoShape *shape)
const QList< KoShape * > selectedShapes() const
virtual void setPosition(const QPointF &position)
Set the position of the shape in pt.
Definition KoShape.cpp:295

References KoCanvasControllerWidget::canvas, correctPosition(), KoSelection::deselectAll(), m_draggedShape, m_parent, repaint(), KoSelection::select(), KoSelection::selectedShapes(), KoShape::setAbsolutePosition(), KoShape::setPosition(), and KoShape::update().

◆ handlePaintEvent()

void Viewport::handlePaintEvent ( QPainter & gc,
QPaintEvent * event )

Definition at line 257 of file KoCanvasControllerWidgetViewport_p.cpp.

258{
259 Q_UNUSED(event);
260 if (m_draggedShape) {
261 const KoViewConverter *vc = m_parent->canvas()->viewConverter();
262
263 painter.save();
264 painter.setOpacity(0.6);
265 painter.setRenderHint(QPainter::Antialiasing);
266 painter.setTransform(vc->documentToView());
267 m_draggedShape->paint(painter);
268 painter.restore();
269 }
270}
virtual void paint(QPainter &painter) const =0
Paint the shape fill The class extending this one is responsible for painting itself....
virtual QPointF documentToView(const QPointF &documentPoint) const

References KoCanvasControllerWidget::canvas, KoViewConverter::documentToView(), m_draggedShape, m_parent, and KoShape::paint().

◆ repaint()

void Viewport::repaint ( KoShape * shape)
private

Definition at line 235 of file KoCanvasControllerWidgetViewport_p.cpp.

236{
237 KisCanvas2 *kisCanvas = dynamic_cast<KisCanvas2*>(m_parent->canvas());
239
240 QRect updateRect = kisCanvas->coordinatesConverter()->documentToWidget(shape->boundingRect()).toAlignedRect();
241 updateRect.adjust(-2, -2, 2, 2); // adjust for antialias
242 update(updateRect);
243}
_Private::Traits< T >::Result documentToWidget(const T &obj) const
virtual QRectF boundingRect() const
Get the bounding box of the shape.
Definition KoShape.cpp:335
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
bool update(QSpinBox *spinBox)

References KoShape::boundingRect(), KoCanvasControllerWidget::canvas, KisCanvas2::coordinatesConverter, KisCoordinatesConverter::documentToWidget(), KIS_SAFE_ASSERT_RECOVER_RETURN, and m_parent.

◆ resetLayout()

void Viewport::resetLayout ( )

Resizes the subordinate canvas widget as needed

Definition at line 272 of file KoCanvasControllerWidgetViewport_p.cpp.

273{
274 const int viewH = size().height();
275 const int viewW = size().width();
276
277 if (m_canvas) {
278 QRect geom = QRect(0, 0, viewW, viewH);
279 if (m_canvas->geometry() != geom) {
280 m_canvas->setGeometry(geom);
281 m_canvas->update();
282 }
283 }
284}
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References m_canvas.

◆ setCanvas()

void Viewport::setCanvas ( QWidget * canvas)

Definition at line 50 of file KoCanvasControllerWidgetViewport_p.cpp.

51{
52 if (m_canvas) {
53 m_canvas->hide();
54 delete m_canvas;
55 }
57 if (!canvas) return;
58 m_canvas->setParent(this);
60 m_canvas->show();
61}

References canvas(), m_canvas, and resetLayout().

Member Data Documentation

◆ m_canvas

QWidget* Viewport::m_canvas
private

Definition at line 56 of file KoCanvasControllerWidgetViewport_p.h.

◆ m_draggedShape

KoShape* Viewport::m_draggedShape
private

Definition at line 55 of file KoCanvasControllerWidgetViewport_p.h.

◆ m_parent

KoCanvasControllerWidget* Viewport::m_parent
private

Definition at line 54 of file KoCanvasControllerWidgetViewport_p.h.


The documentation for this class was generated from the following files: