setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+ setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
myFitAllGap = 40;
myRectBand = nullptr;
}
-
+#include<iostream>
//=======================================================================
// Name : GraphicsView_ViewPort
// Purpose : Destructor
void GraphicsView_ViewPort::fitAll()
{
QRectF aRect = scene()->sceneRect();
+/*
+ QGraphicsRectItem *sceneRectItem = new QGraphicsRectItem(aRect);
+ sceneRectItem->setPen(QPen(QColor("blue"),4));
+ addItem(sceneRectItem);
+*/
fitInView( aRect.adjusted(-myFitAllGap, -myFitAllGap, myFitAllGap, myFitAllGap), Qt::KeepAspectRatio);
}
+void GraphicsView_ViewPort::updateSceneRect(const QRectF &rect)
+{
+ // std::cout << "Update Scene Rect" << std::endl;
+ // scene()->setSceneRect(rect);
+// QGraphicsView::updateSceneRect(rect);
+}
+
void GraphicsView_ViewPort::fitSelect()
{
if (scene()->selectedItems().isEmpty())
QRectF selectionRect;
foreach (QGraphicsItem *item, scene()->selectedItems())
- {
- selectionRect = selectionRect.united(item->sceneBoundingRect());
- }
+ selectionRect = selectionRect.united(item->sceneBoundingRect());
- if( !selectionRect.isNull() )
- {
-// double aGap = qMax( selectionRect.width(), selectionRect.height() ) / 5;
-// selectionRect.adjust( -aGap, -aGap, aGap, aGap );
- fitInView( selectionRect, Qt::KeepAspectRatio );
- }
+ if(!selectionRect.isNull())
+ fitInView(selectionRect, Qt::KeepAspectRatio);
}
void GraphicsView_ViewPort::fitRect(const QRectF& theRect)
myRectBand->setGeometry(QRect(myRectBandStart, QSize()));
myRectBand->show();
}
-/*
- if( !myAreSelectionPointsInitialized )
- {
- myFirstSelectionPoint = QPoint( x, y );
- myLastSelectionPoint = QPoint( x, y );
- myAreSelectionPointsInitialized = true;
- }
-
- if( !myRectBand )
- {
- myRectBand = new QRubberBand( QRubberBand::Rectangle, this );
- QPalette palette;
- palette.setColor( myRectBand->foregroundRole(), Qt::white );
- myRectBand->setPalette( palette );
- }
- myRectBand->hide();
-*/
}
void GraphicsView_ViewPort::drawSelectByRect( int x, int y )
{
myRectBand->hide();
QRectF selectionRect = mapToScene(myRectBand->geometry()).boundingRect();
+ fitInView(selectionRect, Qt::KeepAspectRatio);
+/*
QList<QGraphicsItem *> selectedItems = scene()->items(selectionRect, Qt::IntersectsItemShape);
for (QGraphicsItem *item : selectedItems)
item->setSelected(true);
-
+*/
delete myRectBand;
myRectBand = nullptr;
QGraphicsView::mouseReleaseEvent(event);
}
+void GraphicsView_ViewPort::resizeEvent(QResizeEvent *event)
+{
+ QGraphicsView::resizeEvent(event);
+
+/*
+ // Get the current visible scene rectangle
+ QRectF visibleSceneRect = mapToScene(viewport()->rect()).boundingRect();
+
+ // Calculate the scaling factors for both width and height
+ qreal widthScaleFactor = event->size().width() / event->oldSize().width();
+ qreal heightScaleFactor = event->size().height() / event->oldSize().height();
+
+ // Apply the scaling factors to the visible scene rectangle
+ visibleSceneRect.setWidth(visibleSceneRect.width() * widthScaleFactor);
+ visibleSceneRect.setHeight(visibleSceneRect.height() * heightScaleFactor);
+
+ // Map the adjusted visible scene rectangle back to viewport coordinates
+ QRectF viewportRect = mapFromScene(visibleSceneRect).boundingRect();
+
+ // Ensure that the viewport rectangle stays within the scene boundaries
+
+ if (viewportRect.width() > sceneRect().width()) {
+ viewportRect.setWidth(sceneRect().width());
+ }
+ if (viewportRect.height() > sceneRect().height()) {
+ viewportRect.setHeight(sceneRect().height());
+ }
+
+ // Update the viewport
+ fitInView(, Qt::KeepAspectRatio);
+*/
+}
+void GraphicsView_ViewPort::fitWidth()
+{
+ double aGap = myFitAllGap;
+
+ QRectF aRect;
+ foreach (QGraphicsItem *item, scene()->selectedItems())
+ aRect = aRect.united(item->sceneBoundingRect());
+
+ QRectF aRect1 = aRect;
+
+ double aTop = aRect1.top();
+ double aLeft = aRect1.left();
+ double w = aRect1.width();
+ double h = aRect1.height();
+
+ double aMargin = 10;
+ aRect.setY( aRect.center().y() );
+ aRect.setX( aRect.center().x() );
+ aRect.setHeight( aMargin );
+ aRect.setWidth( aMargin );
+
+
+ addItem(new QGraphicsRectItem(aRect));
+
+ fitInView( aRect.adjusted( -aGap, -aGap, aGap, aGap ), Qt::KeepAspectRatio );
+ ensureVisible( aLeft, aTop, aMargin, aMargin, 0, aGap );
+// ensureVisible(aRect1);
+
+}