Skip to content

Commit

Permalink
Drawing window pan & zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
WandererFan committed Sep 29, 2016
1 parent 958686d commit 6641689
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
29 changes: 24 additions & 5 deletions src/Mod/TechDraw/Gui/MDIViewPage.cpp
Expand Up @@ -145,7 +145,6 @@ MDIViewPage::MDIViewPage(ViewProviderPage *pageVp, Gui::Document* doc, QWidget*
this , SLOT (selectionChanged())
);


// A fresh page is added and we iterate through its collected children and add these to Canvas View -MLP
// if docobj is a featureviewcollection (ex orthogroup), add its child views. if there are ever children that have children,
// we'll have to make this recursive. -WF
Expand All @@ -168,9 +167,13 @@ MDIViewPage::MDIViewPage(ViewProviderPage *pageVp, Gui::Document* doc, QWidget*
App::DocumentObject *obj = m_vpPage->getDrawPage()->Template.getValue();
auto pageTemplate( dynamic_cast<TechDraw::DrawTemplate *>(obj) );
if( pageTemplate ) {
//make sceneRect 1 pagesize bigger in every direction
double width = pageTemplate->Width.getValue();
double height = pageTemplate->Height.getValue();
m_view->scene()->setSceneRect(QRectF(-width,-2.0 * height,3.0*width,3.0*height));
attachTemplate(pageTemplate);
viewAll();
}

}


Expand Down Expand Up @@ -250,11 +253,9 @@ void MDIViewPage::contextMenuEvent(QContextMenuEvent *event)

void MDIViewPage::attachTemplate(TechDraw::DrawTemplate *obj)
{
//why doesn't setting the template set the papersize???
m_view->setPageTemplate(obj);
double width = obj->Width.getValue();
double height = obj->Height.getValue();
m_view->scene()->setSceneRect(QRectF(-1.,-height,width+1.,height)); //the +/- 1 is because of the way the template is define???
m_paperSize = getPaperSize(int(round(width)),int(round(height)));
if (width > height) {
m_orientation = QPrinter::Landscape;
Expand All @@ -263,6 +264,23 @@ void MDIViewPage::attachTemplate(TechDraw::DrawTemplate *obj)
}
}

QPointF MDIViewPage::getTemplateCenter(TechDraw::DrawTemplate *obj)
{
double cx = obj->Width.getValue()/2.0;
double cy = -obj->Height.getValue()/2.0;
QPointF result(cx,cy);
return result;
}

void MDIViewPage::centerOnPage(void)
{
App::DocumentObject *obj = m_vpPage->getDrawPage()->Template.getValue();
auto pageTemplate( dynamic_cast<TechDraw::DrawTemplate *>(obj) );
if( pageTemplate ) {
QPointF viewCenter = getTemplateCenter(pageTemplate);
m_view->centerOn(viewCenter);
}
}

bool MDIViewPage::attachView(App::DocumentObject *obj)
{
Expand Down Expand Up @@ -795,7 +813,8 @@ void MDIViewPage::setRenderer(QAction *action)

void MDIViewPage::viewAll()
{
m_view->fitInView(m_view->scene()->sceneRect(), Qt::KeepAspectRatio);
//m_view->fitInView(m_view->scene()->sceneRect(), Qt::KeepAspectRatio);
m_view->fitInView(m_view->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
}


Expand Down
5 changes: 5 additions & 0 deletions src/Mod/TechDraw/Gui/MDIViewPage.h
Expand Up @@ -29,6 +29,7 @@

#include <QPrinter>
#include <QGraphicsScene>
#include <QPointF>

QT_BEGIN_NAMESPACE
class QAction;
Expand Down Expand Up @@ -82,6 +83,10 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection

QGraphicsScene* m_scene;

QPointF getTemplateCenter(TechDraw::DrawTemplate *obj);
void centerOnPage(void);


public Q_SLOTS:
void setRenderer(QAction *action);
void viewAll();
Expand Down
42 changes: 38 additions & 4 deletions src/Mod/TechDraw/Gui/QGVPage.cpp
Expand Up @@ -37,6 +37,8 @@
# include <cmath>
#endif

#include <App/Application.h>
#include <App/Material.h>
#include <Base/Console.h>
#include <Base/Stream.h>
#include <Gui/FileDialog.h>
Expand Down Expand Up @@ -92,18 +94,26 @@ QGVPage::QGVPage(ViewProviderPage *vp, QGraphicsScene* s, QWidget *parent)
setObjectName(QString::fromLocal8Bit(name));

setScene(s);

setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
//setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
//setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
setCacheMode(QGraphicsView::CacheBackground);
setTransformationAnchor(AnchorUnderMouse);
//setTransformationAnchor(AnchorUnderMouse);
//setTransformationAnchor(NoAnchor);
setTransformationAnchor(AnchorViewCenter);
setResizeAnchor(AnchorViewCenter);
setAlignment(Qt::AlignCenter);

setDragMode(ScrollHandDrag);
setCursor(QCursor(Qt::ArrowCursor));
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

bkgBrush = new QBrush(QColor::fromRgb(70,70,70));
bkgBrush = new QBrush(getBackgroundColor());

resetCachedContent();
}

QGVPage::~QGVPage()
{
delete bkgBrush;
Expand All @@ -126,7 +136,7 @@ void QGVPage::drawBackground(QPainter *p, const QRectF &)


p->setBrush(*bkgBrush);
p->drawRect(viewport()->rect());
p->drawRect(viewport()->rect().adjusted(-2,-2,2,2)); //just bigger than viewport to prevent artifacts

if(!m_vpPage) {
return;
Expand Down Expand Up @@ -544,10 +554,26 @@ void QGVPage::paintEvent(QPaintEvent *event)

void QGVPage::wheelEvent(QWheelEvent *event)
{
qreal factor = std::pow(1.2, -event->delta() / 240.0);
//Delta is the distance that the wheel is rotated, in eighths of a degree.
//positive indicates rotation forwards away from the user; negative backwards toward the user.
//Most mouse types work in steps of 15 degrees, in which case the delta value is a multiple of 120; i.e., 120 units * 1/8 = 15 degrees.
//1 click = 15 degrees. 15 degrees = 120 deltas. delta/240 -> 1 click = 0.5 ==> factor = 1.2^0.5 = 1.095
// 1 click = -0.5 ==> factor = 1.2^-0.5 = 0.91
//so to change wheel direction, multiply (event->delta() / 240.0) by +/-1
double mouseBase = 1.2; //magic numbers. change for different mice?
double mouseAdjust = 240.0;

QPointF center = mapToScene(viewport()->rect().center());
qreal factor = std::pow(mouseBase, event->delta() / mouseAdjust);
scale(factor, factor);

QPointF newCenter = mapToScene(viewport()->rect().center());
QPointF change = newCenter - center;
translate(change.x(), change.y());

event->accept();
}

void QGVPage::enterEvent(QEvent *event)
{
QGraphicsView::enterEvent(event);
Expand All @@ -571,5 +597,13 @@ TechDraw::DrawPage* QGVPage::getDrawPage()
return m_vpPage->getDrawPage();
}

QColor QGVPage::getBackgroundColor()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Colors");
App::Color fcColor;
fcColor.setPackedValue(hGrp->GetUnsigned("Background", 0x70707000));
return fcColor.asValue<QColor>();
}

#include <Mod/TechDraw/Gui/moc_QGVPage.cpp>
1 change: 1 addition & 0 deletions src/Mod/TechDraw/Gui/QGVPage.h
Expand Up @@ -102,6 +102,7 @@ public Q_SLOTS:

static QColor SelectColor;
static QColor PreselectColor;
QColor getBackgroundColor();

QGITemplate *pageTemplate;
std::vector<QGIView *> views;
Expand Down

0 comments on commit 6641689

Please sign in to comment.