Commit 145a7238 authored by Craig Watson's avatar Craig Watson

Background grid: initialize to *approximately* 1cm upon startup

parent 12a91e8c
......@@ -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()
{
......
......@@ -284,6 +284,7 @@ class UBBoardController : public UBDocumentContainer
void appMainModeChanged(UBApplicationController::MainMode);
private:
void initBackgroundGridSize();
void updatePageSizeState();
void saveViewState();
void adjustDisplayViews();
......
......@@ -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);
}
......
......@@ -48,7 +48,7 @@ QPointer<UBSettings> 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;
......
......@@ -92,7 +92,7 @@ protected:
static const int sFillTransparency;
static const int sDrawTransparency;
static const int sRoundingRadius;
int sPixelsPerCentimeter;
qreal sPixelsPerCentimeter;
};
#endif
......@@ -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
......
......@@ -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");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment