20 #include <QGraphicsRectItem>
21 #include <QGraphicsEllipseItem>
22 #include <QGraphicsPolygonItem>
47 double dx = position.x() -
start.x();
48 double dy = position.y() -
start.y();
50 if ( constrainSquare )
52 if ( std::fabs( dx ) > std::fabs( dy ) )
54 width = std::fabs( dx );
59 height = std::fabs( dy );
63 x =
start.x() - ( ( dx < 0 ) ? width : 0 );
64 y =
start.y() - ( ( dy < 0 ) ? height : 0 );
94 x =
start.x() - width;
95 y =
start.y() - height;
100 return QRectF( x, y, width, height );
136 if ( mRubberBandItem )
138 layout()->removeItem( mRubberBandItem );
139 delete mRubberBandItem;
146 mRubberBandItem =
new QGraphicsRectItem( 0, 0, 0, 0 );
147 mRubberBandItem->setBrush(
brush() );
148 mRubberBandItem->setPen(
pen() );
149 mRubberBandStartPos = position;
150 t.translate( position.x(), position.y() );
151 mRubberBandItem->setTransform( t );
153 layout()->addItem( mRubberBandItem );
159 if ( !mRubberBandItem )
164 bool constrainSquare = modifiers & Qt::ShiftModifier;
165 bool fromCenter = modifiers & Qt::AltModifier;
167 QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
168 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
170 t.translate( newRect.x(), newRect.y() );
171 mRubberBandItem->setTransform( t );
178 bool constrainSquare = modifiers & Qt::ShiftModifier;
179 bool fromCenter = modifiers & Qt::AltModifier;
181 if ( mRubberBandItem )
183 layout()->removeItem( mRubberBandItem );
184 delete mRubberBandItem;
185 mRubberBandItem =
nullptr;
187 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
203 if ( mRubberBandItem )
205 layout()->removeItem( mRubberBandItem );
206 delete mRubberBandItem;
213 mRubberBandItem =
new QGraphicsEllipseItem( 0, 0, 0, 0 );
214 mRubberBandItem->setBrush(
brush() );
215 mRubberBandItem->setPen(
pen() );
216 mRubberBandStartPos = position;
217 t.translate( position.x(), position.y() );
218 mRubberBandItem->setTransform( t );
220 layout()->addItem( mRubberBandItem );
226 if ( !mRubberBandItem )
231 bool constrainSquare = modifiers & Qt::ShiftModifier;
232 bool fromCenter = modifiers & Qt::AltModifier;
234 QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
235 mRubberBandItem->setRect( 0, 0, newRect.width(), newRect.height() );
237 t.translate( newRect.x(), newRect.y() );
238 mRubberBandItem->setTransform( t );
245 bool constrainSquare = modifiers & Qt::ShiftModifier;
246 bool fromCenter = modifiers & Qt::AltModifier;
248 if ( mRubberBandItem )
250 layout()->removeItem( mRubberBandItem );
251 delete mRubberBandItem;
252 mRubberBandItem =
nullptr;
254 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
274 if ( mRubberBandItem )
276 layout()->removeItem( mRubberBandItem );
277 delete mRubberBandItem;
284 mRubberBandItem =
new QGraphicsPolygonItem();
285 mRubberBandItem->setBrush(
brush() );
286 mRubberBandItem->setPen(
pen() );
287 mRubberBandStartPos = position;
288 t.translate( position.x(), position.y() );
289 mRubberBandItem->setTransform( t );
291 layout()->addItem( mRubberBandItem );
297 if ( !mRubberBandItem )
302 bool constrainSquare = modifiers & Qt::ShiftModifier;
303 bool fromCenter = modifiers & Qt::AltModifier;
305 QRectF newRect =
updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );
307 QPolygonF shapePolygon = QPolygonF() << QPointF( 0, newRect.height() )
308 << QPointF( newRect.width(), newRect.height() )
309 << QPointF( newRect.width() / 2.0, 0 )
310 << QPointF( 0, newRect.height() );
312 mRubberBandItem->setPolygon( shapePolygon );
314 t.translate( newRect.x(), newRect.y() );
315 mRubberBandItem->setTransform( t );
322 bool constrainSquare = modifiers & Qt::ShiftModifier;
323 bool fromCenter = modifiers & Qt::AltModifier;
325 if ( mRubberBandItem )
327 layout()->removeItem( mRubberBandItem );
328 delete mRubberBandItem;
329 mRubberBandItem =
nullptr;
331 return updateRect( mRubberBandStartPos, position, constrainSquare, fromCenter );