From 541083a9724687847e7aa303e49d9e87c1a476fd Mon Sep 17 00:00:00 2001 From: letsfindaway Date: Wed, 17 Feb 2021 17:09:29 +0100 Subject: [PATCH] add option to draw intermediate grid lines --- resources/forms/mainWindow.ui | 16 ++++++++++++++++ src/adaptors/UBSvgSubsetAdaptor.cpp | 18 ++++++++++++++++++ src/board/UBBoardView.cpp | 28 ++++++++++++++++++++++++++++ src/core/UBSettings.cpp | 1 + src/core/UBSettings.h | 1 + src/domain/UBGraphicsScene.cpp | 10 ++++++++++ src/domain/UBGraphicsScene.h | 7 +++++++ src/gui/UBBackgroundPalette.cpp | 20 ++++++++++++++++++++ src/gui/UBBackgroundPalette.h | 4 +++- 9 files changed, 104 insertions(+), 1 deletion(-) diff --git a/resources/forms/mainWindow.ui b/resources/forms/mainWindow.ui index dcacf33b..8b08f03c 100644 --- a/resources/forms/mainWindow.ui +++ b/resources/forms/mainWindow.ui @@ -1686,6 +1686,22 @@ Reset grid size + + + true + + + + :/images/minus.svg + :/images/save.svg:/images/minus.svg + + + Draw intermediate grid lines + + + Draw intermediate grid lines + + diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index ad209b1b..62a4e524 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -469,6 +469,14 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene(UBDocumentProx mScene->setBackgroundGridSize(gridSize); } + + QStringRef ubIntermediateLines = mXmlReader.attributes().value(mNamespaceUri, "intermediate-lines"); + + if (!ubIntermediateLines.isNull()) { + bool intermediateLines = ubIntermediateLines.toInt(); + + mScene->setIntermediateLines(intermediateLines); + } } QStringRef ubRuledBackground = mXmlReader.attributes().value(mNamespaceUri, "ruled-background"); @@ -484,6 +492,14 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene(UBDocumentProx mScene->setBackgroundGridSize(gridSize); } + + QStringRef ubIntermediateLines = mXmlReader.attributes().value(mNamespaceUri, "intermediate-lines"); + + if (!ubIntermediateLines.isNull()) { + bool intermediateLines = ubIntermediateLines.toInt(); + + mScene->setIntermediateLines(intermediateLines); + } } UBPageBackground bg; @@ -1140,8 +1156,10 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::writeSvgElement(UBDocumentProxy* pro if (crossedBackground || ruledBackground) { int gridSize = mScene->backgroundGridSize(); + bool intermediateLines = mScene->intermediateLines(); mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "grid-size", QString::number(gridSize)); + mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "intermediate-lines", QString::number(intermediateLines)); } QDesktopWidget* desktop = UBApplication::desktop(); diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 4accb0f6..5ba68364 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -1629,6 +1629,7 @@ void UBBoardView::drawBackground (QPainter *painter, const QRectF &rect) } qreal gridSize = scene()->backgroundGridSize(); + bool intermediateLines = scene()->intermediateLines(); painter->setPen (bgCrossColor); @@ -1647,6 +1648,22 @@ void UBBoardView::drawBackground (QPainter *painter, const QRectF &rect) { painter->drawLine (xPos, rect.y (), xPos, rect.y () + rect.height ()); } + + if (intermediateLines) { + QColor intermediateColor = bgCrossColor; + intermediateColor.setAlphaF(0.5 * bgCrossColor.alphaF()); + painter->setPen(intermediateColor); + + for (qreal yPos = firstY - gridSize/2; yPos < rect.y () + rect.height (); yPos += gridSize) + { + painter->drawLine (rect.x (), yPos, rect.x () + rect.width (), yPos); + } + + for (qreal xPos = firstX - gridSize/2; xPos < rect.x () + rect.width (); xPos += gridSize) + { + painter->drawLine (xPos, rect.y (), xPos, rect.y () + rect.height ()); + } + } } if (scene() && scene()->pageBackground() == UBPageBackground::ruled) @@ -1657,6 +1674,17 @@ void UBBoardView::drawBackground (QPainter *painter, const QRectF &rect) { painter->drawLine (rect.x (), yPos, rect.x () + rect.width (), yPos); } + + if (intermediateLines) { + QColor intermediateColor = bgCrossColor; + intermediateColor.setAlphaF(0.5 * bgCrossColor.alphaF()); + painter->setPen(intermediateColor); + + for (qreal yPos = firstY - gridSize/2; 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 1920d90b..535e57ff 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -50,6 +50,7 @@ int UBSettings::crossSize = 24; int UBSettings::defaultCrossSize = 24; int UBSettings::minCrossSize = 12; int UBSettings::maxCrossSize = 96; //TODO: user-settable? +bool UBSettings::intermediateLines = false; int UBSettings::colorPaletteSize = 5; int UBSettings::objectFrameWidth = 20; int UBSettings::boardMargin = 10; diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index a7353108..665d2749 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -191,6 +191,7 @@ class UBSettings : public QObject static int defaultCrossSize; static int minCrossSize; static int maxCrossSize; + static bool intermediateLines; static int colorPaletteSize; static int objectFrameWidth; diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 4e91018e..e66a0efa 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -359,6 +359,7 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent, bool enableUndoRedoSta } mBackgroundGridSize = UBSettings::settings()->crossSize; + mIntermediateLines = UBSettings::settings()->intermediateLines; // Just for debug. Do not delete please // connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChangedProcessing())); @@ -1155,6 +1156,15 @@ void UBGraphicsScene::setBackgroundGridSize(int pSize) } } +void UBGraphicsScene::setIntermediateLines(bool checked) +{ + mIntermediateLines = checked; + setModified(true); + + foreach(QGraphicsView* view, views()) + view->resetCachedContent(); +} + void UBGraphicsScene::setDrawingMode(bool bModeDesktop) { mIsDesktopMode = bModeDesktop; diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 3b898685..c72b7407 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -230,6 +230,11 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem return mBackgroundGridSize; } + bool intermediateLines() const + { + return mIntermediateLines; + } + bool hasBackground() { return (mBackgroundObject != 0); @@ -359,6 +364,7 @@ public slots: void setBackground(bool pIsDark, UBPageBackground pBackground); void setBackgroundZoomFactor(qreal zoom); void setBackgroundGridSize(int pSize); + void setIntermediateLines(bool checked); void setDrawingMode(bool bModeDesktop); void deselectAllItems(); @@ -442,6 +448,7 @@ public slots: bool mDarkBackground; UBPageBackground mPageBackground; int mBackgroundGridSize; + bool mIntermediateLines; bool mIsDesktopMode; qreal mZoomFactor; diff --git a/src/gui/UBBackgroundPalette.cpp b/src/gui/UBBackgroundPalette.cpp index 92ce0bd8..6569e2f6 100644 --- a/src/gui/UBBackgroundPalette.cpp +++ b/src/gui/UBBackgroundPalette.cpp @@ -48,6 +48,7 @@ void UBBackgroundPalette::init() mSlider->setTracking(true); // valueChanged() is emitted during movement and not just upon releasing the slider mSliderLabel = new QLabel(tr("Grid size")); + mIntermediateLinesLabel = new QLabel(tr("Draw intermediate grid lines")); mResetDefaultGridSizeButton = createPaletteButton(UBApplication::mainWindow->actionDefaultGridSize, this); mResetDefaultGridSizeButton->setFixedSize(24,24); @@ -55,11 +56,22 @@ void UBBackgroundPalette::init() connect(UBApplication::mainWindow->actionDefaultGridSize, SIGNAL(triggered()), this, SLOT(defaultBackgroundGridSize())); + mDrawIntermediateLinesCheckBox = createPaletteButton(UBApplication::mainWindow->actionDrawIntermediateGridLines, this); + mDrawIntermediateLinesCheckBox->setFixedSize(24,24); + mDrawIntermediateLinesCheckBox->setCheckable(true); + mActions << UBApplication::mainWindow->actionDrawIntermediateGridLines; + mButtons.removeLast(); // don't add to button group + + connect(UBApplication::mainWindow->actionDrawIntermediateGridLines, SIGNAL(toggled(bool)), this, SLOT(toggleIntermediateLines(bool))); + mBottomLayout->addSpacing(16); mBottomLayout->addWidget(mSliderLabel); mBottomLayout->addWidget(mSlider); mBottomLayout->addWidget(mResetDefaultGridSizeButton); mBottomLayout->addSpacing(16); + mBottomLayout->addWidget(mIntermediateLinesLabel); + mBottomLayout->addWidget(mDrawIntermediateLinesCheckBox); + mBottomLayout->addSpacing(16); updateLayout(); } @@ -137,6 +149,8 @@ void UBBackgroundPalette::showEvent(QShowEvent* event) connect(mSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderValueChanged(int))); + mDrawIntermediateLinesCheckBox->setChecked(UBApplication::boardController->activeScene()->intermediateLines()); + QWidget::showEvent(event); } @@ -152,6 +166,12 @@ void UBBackgroundPalette::defaultBackgroundGridSize() sliderValueChanged(UBSettings::settings()->defaultCrossSize); } +void UBBackgroundPalette::toggleIntermediateLines(bool checked) +{ + UBApplication::boardController->activeScene()->setIntermediateLines(checked); + UBSettings::settings()->intermediateLines = checked; // since this function is called (indirectly, by refresh) when we switch scenes, the settings will always have the current scene's value. +} + void UBBackgroundPalette::backgroundChanged() { bool dark = UBApplication::boardController->activeScene()->isDarkBackground(); diff --git a/src/gui/UBBackgroundPalette.h b/src/gui/UBBackgroundPalette.h index 83472d47..5a149a0f 100644 --- a/src/gui/UBBackgroundPalette.h +++ b/src/gui/UBBackgroundPalette.h @@ -28,6 +28,7 @@ class UBBackgroundPalette : public UBActionPalette protected slots: void sliderValueChanged(int value); void defaultBackgroundGridSize(); + void toggleIntermediateLines(bool checked); protected: virtual void updateLayout(); @@ -40,8 +41,9 @@ class UBBackgroundPalette : public UBActionPalette QSlider* mSlider; QLabel* mSliderLabel; + QLabel* mIntermediateLinesLabel; UBActionPaletteButton* mResetDefaultGridSizeButton; - + UBActionPaletteButton* mDrawIntermediateLinesCheckBox; };