From 145a7238c078e5cc61ce528fa1843c3648fa69e9 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Sat, 8 Oct 2016 22:30:46 -0400 Subject: [PATCH] Background grid: initialize to *approximately* 1cm upon startup --- src/board/UBBoardController.cpp | 31 +++++++++++++++++++++++++++++++ src/board/UBBoardController.h | 1 + src/board/UBBoardView.cpp | 14 ++++++++------ src/core/UBSettings.cpp | 2 +- src/tools/UBAbstractDrawRuler.h | 2 +- src/tools/UBGraphicsRuler.cpp | 2 +- src/tools/UBGraphicsTriangle.cpp | 2 +- 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 6ba15e3e..e61d613b 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -152,6 +152,8 @@ void UBBoardController::init() setActiveDocumentScene(doc); + initBackgroundGridSize(); + undoRedoStateChange(true); } @@ -162,6 +164,35 @@ UBBoardController::~UBBoardController() delete mDisplayView; } +/** + * @brief Set the default background grid size to appear as roughly 1cm on screen + */ +void UBBoardController::initBackgroundGridSize() +{ + // Besides adjusting for DPI, we also need to scale the grid size by the ratio of the control view size + // to document size. However the control view isn't available as soon as the boardController is created, + // so we approximate this ratio as (document resolution) / (screen resolution). + // Later on, this is calculated by `updateSystemScaleFactor` and stored in `mSystemScaleFactor`. + + QDesktopWidget* desktop = UBApplication::desktop(); + qreal dpi = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2.; + + //qDebug() << "dpi: " << dpi; + + // The display manager isn't initialized yet so we have to just assume the control view is on the main display + qreal screenY = desktop->screenGeometry(mControlView).height(); + qreal documentY = mActiveScene->nominalSize().height(); + qreal resolutionRatio = documentY / screenY; + + //qDebug() << "resolution ratio: " << resolutionRatio; + + int gridSize = (resolutionRatio * 10. * dpi) / UBGeometryUtils::inchSize; + + UBSettings::settings()->crossSize = gridSize; + mActiveScene->setBackgroundGridSize(gridSize); + + //qDebug() << "grid size: " << gridSize; +} int UBBoardController::currentPage() { diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index 5631299a..1b66c1e8 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -284,6 +284,7 @@ class UBBoardController : public UBDocumentContainer void appMainModeChanged(UBApplicationController::MainMode); private: + void initBackgroundGridSize(); void updatePageSizeState(); void saveViewState(); void adjustDisplayViews(); diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 096a4898..0926ec91 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -1607,20 +1607,22 @@ void UBBoardView::drawBackground (QPainter *painter, const QRectF &rect) bgCrossColor.setAlpha (alpha); // fade the crossing on small zooms } + qreal gridSize = scene()->backgroundGridSize(); + painter->setPen (bgCrossColor); if (scene () && scene ()->pageBackground() == UBPageBackground::crossed) { - qreal firstY = ((int) (rect.y () / scene()->backgroundGridSize())) * scene()->backgroundGridSize(); + qreal firstY = ((int) (rect.y () / gridSize)) * gridSize; - for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += scene()->backgroundGridSize()) + for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += gridSize) { painter->drawLine (rect.x (), yPos, rect.x () + rect.width (), yPos); } - qreal firstX = ((int) (rect.x () / scene()->backgroundGridSize())) * scene()->backgroundGridSize(); + qreal firstX = ((int) (rect.x () / gridSize)) * gridSize; - for (qreal xPos = firstX; xPos < rect.x () + rect.width (); xPos += scene()->backgroundGridSize()) + for (qreal xPos = firstX; xPos < rect.x () + rect.width (); xPos += gridSize) { painter->drawLine (xPos, rect.y (), xPos, rect.y () + rect.height ()); } @@ -1628,9 +1630,9 @@ void UBBoardView::drawBackground (QPainter *painter, const QRectF &rect) if (scene() && scene()->pageBackground() == UBPageBackground::ruled) { - qreal firstY = ((int) (rect.y () / scene()->backgroundGridSize())) * scene()->backgroundGridSize(); + qreal firstY = ((int) (rect.y () / gridSize)) * gridSize; - for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += scene()->backgroundGridSize()) + for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += gridSize) { painter->drawLine (rect.x (), yPos, rect.x () + rect.width (), yPos); } diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index 373a9062..fc9e2b55 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -48,7 +48,7 @@ QPointer UBSettings::sSingleton = 0; int UBSettings::pointerDiameter = 40; int UBSettings::crossSize = 24; int UBSettings::minCrossSize = 12; -int UBSettings::maxCrossSize = 64; +int UBSettings::maxCrossSize = 96; //TODO: user-settable? int UBSettings::colorPaletteSize = 5; int UBSettings::objectFrameWidth = 20; int UBSettings::boardMargin = 10; diff --git a/src/tools/UBAbstractDrawRuler.h b/src/tools/UBAbstractDrawRuler.h index 73c10d05..caf84821 100644 --- a/src/tools/UBAbstractDrawRuler.h +++ b/src/tools/UBAbstractDrawRuler.h @@ -92,7 +92,7 @@ protected: static const int sFillTransparency; static const int sDrawTransparency; static const int sRoundingRadius; - int sPixelsPerCentimeter; + qreal sPixelsPerCentimeter; }; #endif diff --git a/src/tools/UBGraphicsRuler.cpp b/src/tools/UBGraphicsRuler.cpp index 65965fcf..66f287b7 100644 --- a/src/tools/UBGraphicsRuler.cpp +++ b/src/tools/UBGraphicsRuler.cpp @@ -180,7 +180,7 @@ void UBGraphicsRuler::paintGraduations(QPainter *painter) // Update the width of one "centimeter" to correspond to the width of the background grid (whether it is displayed or not) sPixelsPerCentimeter = UBApplication::boardController->activeScene()->backgroundGridSize(); - double pixelsPerMillimeter = double(sPixelsPerCentimeter)/10.0; + qreal pixelsPerMillimeter = sPixelsPerCentimeter/10.0; int rulerLengthInMillimeters = (rect().width() - sLeftEdgeMargin - sRoundingRadius)/pixelsPerMillimeter; // When a "centimeter" is too narrow, we only display every 5th number, and every 5th millimeter mark diff --git a/src/tools/UBGraphicsTriangle.cpp b/src/tools/UBGraphicsTriangle.cpp index 7ee4f553..6d58bb3f 100644 --- a/src/tools/UBGraphicsTriangle.cpp +++ b/src/tools/UBGraphicsTriangle.cpp @@ -351,7 +351,7 @@ void UBGraphicsTriangle::paintGraduations(QPainter *painter) // Update the width of one "centimeter" to correspond to the width of the background grid (whether it is displayed or not) sPixelsPerCentimeter = UBApplication::boardController->activeScene()->backgroundGridSize(); - double pixelsPerMillimeter = double(sPixelsPerCentimeter)/10.0; + double pixelsPerMillimeter = sPixelsPerCentimeter/10.0; // When a "centimeter" is too narrow, we only display every 5th number, and every 5th millimeter mark double numbersWidth = fontMetrics.width("00");