From 081dbee1ed153c958981ed46659665b5945cdb49 Mon Sep 17 00:00:00 2001 From: Craig Watson Date: Fri, 2 Sep 2016 17:43:35 -0400 Subject: [PATCH] User-resizable background grid: - The background selection palette now includes a slider to change the size of the background grid. Default min/max values are 16 and 64px, defined in UBSettings. Grid resizes dynamically as the slider is moved. - Measuring tools' (ruler, triangle) markers follow grid size: 1 square of the background grid corresponds to 1cm - Grid size can be different for each page of a document - Grid size is saved in the .svg - Documents with a background grid but no specified grid size follow the default size defined in UBSettings. Previously, grid size was calculated based on DPI, which can vary from one OS, computer or display to the next. This new setting allows documents to be migrated from one machine to another with no unexpected changes in grid size happening. It also makes it easy to correct any problems importing old documents (whose grid size might be smaller or larger than expected when imported on a new version of OpenBoard). --- src/adaptors/UBSvgSubsetAdaptor.cpp | 19 +++- src/board/UBBoardController.cpp | 4 - src/board/UBBoardPaletteManager.cpp | 11 +- src/board/UBBoardPaletteManager.h | 3 +- src/board/UBBoardView.cpp | 8 +- src/core/UBSettings.cpp | 4 +- src/core/UBSettings.h | 4 + src/domain/UBGraphicsScene.cpp | 23 ++++- src/domain/UBGraphicsScene.h | 8 ++ src/gui/UBActionPalette.h | 12 +-- src/gui/UBBackgroundPalette.cpp | 153 ++++++++++++++++++++++++++++ src/gui/UBBackgroundPalette.h | 46 +++++++++ src/gui/gui.pri | 6 +- src/tools/UBAbstractDrawRuler.cpp | 5 +- src/tools/UBAbstractDrawRuler.h | 2 +- src/tools/UBGraphicsCompass.cpp | 8 +- src/tools/UBGraphicsCompass.h | 1 - src/tools/UBGraphicsRuler.cpp | 57 ++++++++--- src/tools/UBGraphicsTriangle.cpp | 35 +++++-- 19 files changed, 346 insertions(+), 63 deletions(-) create mode 100644 src/gui/UBBackgroundPalette.cpp create mode 100644 src/gui/UBBackgroundPalette.h diff --git a/src/adaptors/UBSvgSubsetAdaptor.cpp b/src/adaptors/UBSvgSubsetAdaptor.cpp index e44eba15..82da8160 100644 --- a/src/adaptors/UBSvgSubsetAdaptor.cpp +++ b/src/adaptors/UBSvgSubsetAdaptor.cpp @@ -453,11 +453,22 @@ UBGraphicsScene* UBSvgSubsetAdaptor::UBSvgSubsetReader::loadScene(UBDocumentProx QStringRef ubCrossedBackground = mXmlReader.attributes().value(mNamespaceUri, "crossed-background"); - if (!ubDarkBackground.isNull()) + if (!ubCrossedBackground.isNull()) crossedBackground = (ubCrossedBackground.toString() == xmlTrue); mScene->setBackground(darkBackground, crossedBackground); + if (crossedBackground) { + QStringRef ubGridSize = mXmlReader.attributes().value(mNamespaceUri, "grid-size"); + + if (!ubGridSize.isNull()) { + int gridSize = ubGridSize.toInt(); + + UBSettings::settings()->crossSize = gridSize; + mScene->setBackgroundGridSize(gridSize); + } + } + QStringRef pageNominalSize = mXmlReader.attributes().value(mNamespaceUri, "nominal-size"); if (!pageNominalSize.isNull()) { @@ -1068,6 +1079,12 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::writeSvgElement(UBDocumentProxy* pro mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "dark-background", mScene->isDarkBackground() ? xmlTrue : xmlFalse); mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "crossed-background", mScene->isCrossedBackground() ? xmlTrue : xmlFalse); + if (mScene->isCrossedBackground()) { + int gridSize = mScene->backgroundGridSize(); + + mXmlWriter.writeAttribute(UBSettings::uniboardDocumentNamespaceUri, "grid-size", QString::number(gridSize)); + } + QDesktopWidget* desktop = UBApplication::desktop(); if (proxy->pageDpi() == 0) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index c7e1c657..a87524d2 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -122,10 +122,6 @@ UBBoardController::UBBoardController(UBMainWindow* mainWindow) mMarkerColorOnDarkBackground = UBSettings::settings()->markerColors(true).at(markerColorIndex); mMarkerColorOnLightBackground = UBSettings::settings()->markerColors(false).at(markerColorIndex); - QDesktopWidget* desktop = UBApplication::desktop(); - int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2; - int sPixelsPerMillimeter = qRound(dpiCommon / UBGeometryUtils::inchSize); - UBSettings::settings()->crossSize = 10*sPixelsPerMillimeter; } diff --git a/src/board/UBBoardPaletteManager.cpp b/src/board/UBBoardPaletteManager.cpp index bb7324d1..cbff6491 100644 --- a/src/board/UBBoardPaletteManager.cpp +++ b/src/board/UBBoardPaletteManager.cpp @@ -45,6 +45,7 @@ #include "gui/UBZoomPalette.h" #include "gui/UBWebToolsPalette.h" #include "gui/UBActionPalette.h" +#include "gui/UBBackgroundPalette.h" #include "gui/UBFavoriteToolPalette.h" #include "gui/UBStartupHintsPalette.h" @@ -253,11 +254,11 @@ void UBBoardPaletteManager::setupPalettes() backgroundsActions << UBApplication::mainWindow->actionPlainDarkBackground; backgroundsActions << UBApplication::mainWindow->actionCrossedDarkBackground; - mBackgroundsPalette = new UBActionPalette(backgroundsActions, Qt::Horizontal , mContainer); + mBackgroundsPalette = new UBBackgroundPalette(backgroundsActions, mContainer); mBackgroundsPalette->setButtonIconSize(QSize(128, 128)); mBackgroundsPalette->groupActions(); mBackgroundsPalette->setClosable(true); - mBackgroundsPalette->setAutoClose(true); + mBackgroundsPalette->setAutoClose(false); mBackgroundsPalette->adjustSizeAndPosition(); mBackgroundsPalette->hide(); @@ -532,7 +533,7 @@ void UBBoardPaletteManager::changeBackground() else UBApplication::boardController->changeBackground(false, false); - UBApplication::mainWindow->actionBackgrounds->setChecked(false); + mBackgroundsPalette->backgroundChanged(); } @@ -552,8 +553,10 @@ void UBBoardPaletteManager::activeSceneChanged() if (mZoomPalette) connect(mZoomPalette, SIGNAL(mouseEntered()), activeScene, SLOT(hideTool())); - if (mBackgroundsPalette) + if (mBackgroundsPalette) { connect(mBackgroundsPalette, SIGNAL(mouseEntered()), activeScene, SLOT(hideTool())); + mBackgroundsPalette->refresh(); + } } diff --git a/src/board/UBBoardPaletteManager.h b/src/board/UBBoardPaletteManager.h index e6b42aa1..408e895f 100644 --- a/src/board/UBBoardPaletteManager.h +++ b/src/board/UBBoardPaletteManager.h @@ -48,6 +48,7 @@ class UBClockPalette; class UBPageNumberPalette; class UBZoomPalette; class UBActionPalette; +class UBBackgroundPalette; class UBBoardController; class UBServerXMLHttpRequest; class UBKeyboardPalette; @@ -113,7 +114,7 @@ class UBBoardPaletteManager : public QObject /** The right dock palette */ UBRightPalette* mRightPalette; - UBActionPalette *mBackgroundsPalette; + UBBackgroundPalette *mBackgroundsPalette; UBActionPalette *mToolsPalette; UBActionPalette* mAddItemPalette; UBActionPalette* mErasePalette; diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp index 34ed6f1f..b50d0115 100644 --- a/src/board/UBBoardView.cpp +++ b/src/board/UBBoardView.cpp @@ -1609,16 +1609,16 @@ void UBBoardView::drawBackground (QPainter *painter, const QRectF &rect) if (scene () && scene ()->isCrossedBackground ()) { - qreal firstY = ((int) (rect.y () / UBSettings::crossSize)) * UBSettings::crossSize; + qreal firstY = ((int) (rect.y () / scene()->backgroundGridSize())) * scene()->backgroundGridSize(); - for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += UBSettings::crossSize) + for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += scene()->backgroundGridSize()) { painter->drawLine (rect.x (), yPos, rect.x () + rect.width (), yPos); } - qreal firstX = ((int) (rect.x () / UBSettings::crossSize)) * UBSettings::crossSize; + qreal firstX = ((int) (rect.x () / scene()->backgroundGridSize())) * scene()->backgroundGridSize(); - for (qreal xPos = firstX; xPos < rect.x () + rect.width (); xPos += UBSettings::crossSize) + for (qreal xPos = firstX; xPos < rect.x () + rect.width (); xPos += scene()->backgroundGridSize()) { painter->drawLine (xPos, rect.y (), xPos, rect.y () + rect.height ()); } diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index c344908a..47177e89 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -46,7 +46,9 @@ QPointer UBSettings::sSingleton = 0; int UBSettings::pointerDiameter = 40; -int UBSettings::crossSize = 32; +int UBSettings::crossSize = 24; +int UBSettings::minCrossSize = 12; +int UBSettings::maxCrossSize = 64; 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 9d04f0dc..0d874471 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -182,7 +182,11 @@ class UBSettings : public QObject static QColor documentSizeMarkColorDarkBackground; static QColor documentSizeMarkColorLightBackground; + // Background grid static int crossSize; + static int minCrossSize; + static int maxCrossSize; + static int colorPaletteSize; static int objectFrameWidth; diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index 5181af4a..01918dc3 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -353,6 +353,8 @@ UBGraphicsScene::UBGraphicsScene(UBDocumentProxy* parent, bool enableUndoRedoSta UBApplication::applicationController->initialVScroll())); } + mBackgroundGridSize = UBSettings::settings()->crossSize; + // Just for debug. Do not delete please // connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionChangedProcessing())); connect(UBApplication::undoStack.data(), SIGNAL(indexChanged(int)), this, SLOT(updateSelectionFrameWrapper(int))); @@ -981,6 +983,18 @@ void UBGraphicsScene::setBackgroundZoomFactor(qreal zoom) mZoomFactor = zoom; } + +void UBGraphicsScene::setBackgroundGridSize(int pSize) +{ + if (pSize > 0) { + mBackgroundGridSize = pSize; + setModified(true); + + foreach(QGraphicsView* view, views()) + view->resetCachedContent(); + } +} + void UBGraphicsScene::setDrawingMode(bool bModeDesktop) { mIsDesktopMode = bModeDesktop; @@ -1158,6 +1172,7 @@ UBGraphicsScene* UBGraphicsScene::sceneDeepCopy() const UBGraphicsScene* copy = new UBGraphicsScene(this->document(), this->mUndoRedoStackEnabled); copy->setBackground(this->isDarkBackground(), this->isCrossedBackground()); + copy->setBackgroundGridSize(mBackgroundGridSize); copy->setSceneRect(this->sceneRect()); if (this->mNominalSize.isValid()) @@ -2404,16 +2419,16 @@ void UBGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) if (isCrossedBackground()) { - qreal firstY = ((int) (rect.y () / UBSettings::crossSize)) * UBSettings::crossSize; + qreal firstY = ((int) (rect.y () / backgroundGridSize())) * backgroundGridSize(); - for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += UBSettings::crossSize) + for (qreal yPos = firstY; yPos < rect.y () + rect.height (); yPos += backgroundGridSize()) { painter->drawLine (rect.x (), yPos, rect.x () + rect.width (), yPos); } - qreal firstX = ((int) (rect.x () / UBSettings::crossSize)) * UBSettings::crossSize; + qreal firstX = ((int) (rect.x () / backgroundGridSize())) * backgroundGridSize(); - for (qreal xPos = firstX; xPos < rect.x () + rect.width (); xPos += UBSettings::crossSize) + for (qreal xPos = firstX; xPos < rect.x () + rect.width (); xPos += backgroundGridSize()) { painter->drawLine (xPos, rect.y (), xPos, rect.y () + rect.height ()); } diff --git a/src/domain/UBGraphicsScene.h b/src/domain/UBGraphicsScene.h index 29966f6d..9d6d9500 100644 --- a/src/domain/UBGraphicsScene.h +++ b/src/domain/UBGraphicsScene.h @@ -220,6 +220,11 @@ class UBGraphicsScene: public UBCoreGraphicsScene, public UBItem return mCrossedBackground; } + int backgroundGridSize() const + { + return mBackgroundGridSize; + } + bool hasBackground() { return (mBackgroundObject != 0); @@ -331,6 +336,7 @@ public slots: void setBackground(bool pIsDark, bool pIsCrossed); void setBackgroundZoomFactor(qreal zoom); + void setBackgroundGridSize(int pSize); void setDrawingMode(bool bModeDesktop); void deselectAllItems(); @@ -404,6 +410,8 @@ public slots: bool mDarkBackground; bool mCrossedBackground; + int mBackgroundGridSize; + bool mIsDesktopMode; qreal mZoomFactor; diff --git a/src/gui/UBActionPalette.h b/src/gui/UBActionPalette.h index 97752769..3e1e3238 100644 --- a/src/gui/UBActionPalette.h +++ b/src/gui/UBActionPalette.h @@ -55,9 +55,9 @@ class UBActionPalette : public UBFloatingPalette void setToolButtonStyle(Qt::ToolButtonStyle); QList actions(); - void setActions(QList actions); + virtual void setActions(QList actions); void groupActions(); - void addAction(QAction* action); + virtual void addAction(QAction* action); void setClosable(bool closable); void setAutoClose(bool autoClose) @@ -72,10 +72,10 @@ class UBActionPalette : public UBFloatingPalette bool m_customCloseProcessing; virtual int border(); - void clearLayout(); + virtual void clearLayout(); QSize buttonSize(); - UBActionPaletteButton* getButtonFromAction(QAction* action); + virtual UBActionPaletteButton* getButtonFromAction(QAction* action); public slots: void close(); @@ -91,7 +91,7 @@ class UBActionPalette : public UBFloatingPalette virtual void mouseReleaseEvent(QMouseEvent * event); virtual void init(Qt::Orientation orientation); - void updateLayout(); + virtual void updateLayout(); QList mButtons; QButtonGroup* mButtonGroup; @@ -105,7 +105,7 @@ class UBActionPalette : public UBFloatingPalette QPoint mMousePos; UBActionPaletteButton *createPaletteButton(QAction* action, QWidget *parent); - private slots: + protected slots: void buttonClicked(); void actionChanged(); }; diff --git a/src/gui/UBBackgroundPalette.cpp b/src/gui/UBBackgroundPalette.cpp new file mode 100644 index 00000000..d10fcc08 --- /dev/null +++ b/src/gui/UBBackgroundPalette.cpp @@ -0,0 +1,153 @@ +#include "UBBackgroundPalette.h" + +UBBackgroundPalette::UBBackgroundPalette(QList actions, QWidget * parent) + : UBActionPalette(parent) +{ + init(); + setActions(actions); +} + + +UBBackgroundPalette::UBBackgroundPalette(QWidget * parent) + : UBActionPalette(parent) +{ + init(); +} + + + +void UBBackgroundPalette::init() +{ + UBActionPalette::clearLayout(); + delete layout(); + + + m_customCloseProcessing = false; + + mButtonSize = QSize(32, 32); + mIsClosable = false; + mAutoClose = false; + mButtonGroup = 0; + mToolButtonStyle = Qt::ToolButtonIconOnly; + mButtons.clear(); + + mVLayout = new QVBoxLayout(this); + mTopLayout = new QHBoxLayout(); + mBottomLayout = new QHBoxLayout(); + + mVLayout->addLayout(mTopLayout); + mVLayout->addLayout(mBottomLayout); + + mSlider = new QSlider(Qt::Horizontal); + + mSlider->setMinimum(UBSettings::settings()->minCrossSize); + mSlider->setMaximum(UBSettings::settings()->maxCrossSize); + mSlider->setSingleStep(2); + mSlider->setTracking(true); // valueChanged() is emitted during movement and not just upon releasing the slider + + mSliderLabel = new QLabel(tr("Grid size")); + + mBottomLayout->addSpacing(16); + mBottomLayout->addWidget(mSliderLabel); + mBottomLayout->addWidget(mSlider); + mBottomLayout->addSpacing(16); + + updateLayout(); +} + +void UBBackgroundPalette::addAction(QAction* action) +{ + UBActionPaletteButton* button = createPaletteButton(action, this); + + mTopLayout->addWidget(button); + mActions << action; +} + +void UBBackgroundPalette::setActions(QList actions) +{ + mMapActionToButton.clear(); + + foreach(QAction* action, actions) + { + addAction(action); + } + + actionChanged(); +} + +void UBBackgroundPalette::updateLayout() +{ + if (mToolButtonStyle == Qt::ToolButtonIconOnly) { + mVLayout->setContentsMargins (sLayoutContentMargin / 2 + border(), sLayoutContentMargin / 2 + border() + , sLayoutContentMargin / 2 + border(), sLayoutContentMargin / 2 + border()); + } + else + { + mVLayout->setContentsMargins (sLayoutContentMargin + border(), sLayoutContentMargin + border() + , sLayoutContentMargin + border(), sLayoutContentMargin + border()); + + } + update(); +} + +void UBBackgroundPalette::clearLayout() +{ + while(!mTopLayout->isEmpty()) { + QLayoutItem* pItem = mTopLayout->itemAt(0); + QWidget* pW = pItem->widget(); + mTopLayout->removeItem(pItem); + delete pItem; + mTopLayout->removeWidget(pW); + delete pW; + } + + delete mTopLayout; + + while(!mBottomLayout->isEmpty()) { + QLayoutItem* pItem = mBottomLayout->itemAt(0); + QWidget* pW = pItem->widget(); + mBottomLayout->removeItem(pItem); + delete pItem; + mBottomLayout->removeWidget(pW); + delete pW; + } + + delete mBottomLayout; + + delete mVLayout; + + mActions.clear(); + mButtons.clear(); +} + +void UBBackgroundPalette::showEvent(QShowEvent* event) +{ + backgroundChanged(); + + mSlider->setValue(UBApplication::boardController->activeScene()->backgroundGridSize()); + connect(mSlider, SIGNAL(valueChanged(int)), + this, SLOT(sliderValueChanged(int))); + + QWidget::showEvent(event); +} + +void UBBackgroundPalette::sliderValueChanged(int value) +{ + UBApplication::boardController->activeScene()->setBackgroundGridSize(value); +} + +void UBBackgroundPalette::backgroundChanged() +{ + bool dark = UBApplication::boardController->activeScene()->isDarkBackground(); + + if (dark) + mSliderLabel->setStyleSheet("QLabel { color : white; }"); + else + mSliderLabel->setStyleSheet("QLabel { color : black; }"); +} + +void UBBackgroundPalette::refresh() +{ + backgroundChanged(); + mSlider->setValue(UBApplication::boardController->activeScene()->backgroundGridSize()); +} diff --git a/src/gui/UBBackgroundPalette.h b/src/gui/UBBackgroundPalette.h new file mode 100644 index 00000000..c9f6ffe1 --- /dev/null +++ b/src/gui/UBBackgroundPalette.h @@ -0,0 +1,46 @@ +#ifndef UBBACKGROUNDPALETTE_H +#define UBBACKGROUNDPALETTE_H + +#include "gui/UBActionPalette.h" +#include "core/UBApplication.h" +#include "board/UBBoardController.h" +#include "domain/UBGraphicsScene.h" + +class UBBackgroundPalette : public UBActionPalette +{ + Q_OBJECT + + public: + + UBBackgroundPalette(QList actions, QWidget* parent = 0); + UBBackgroundPalette(QWidget* parent = 0); + + void addAction(QAction *action); + void setActions(QList actions); + void clearLayout(); + + + public slots: + void showEvent(QShowEvent* event); + void backgroundChanged(); + void refresh(); + + protected slots: + void sliderValueChanged(int value); + + protected: + virtual void updateLayout(); + void init(); + + + QVBoxLayout* mVLayout; + QHBoxLayout* mTopLayout; + QHBoxLayout* mBottomLayout; + + QSlider* mSlider; + QLabel* mSliderLabel; + + +}; + +#endif // UBBACKGROUNDPALETTE_H diff --git a/src/gui/gui.pri b/src/gui/gui.pri index 38f5f654..a4921eff 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -40,7 +40,8 @@ HEADERS += src/gui/UBThumbnailView.h \ src/gui/UBFeaturesActionBar.h \ src/gui/UBMessagesDialog.h \ src/gui/UBOpenSankoreImporterWidget.h \ - src/gui/UBStartupHintsPalette.h + src/gui/UBStartupHintsPalette.h \ + src/gui/UBBackgroundPalette.h SOURCES += src/gui/UBThumbnailView.cpp \ src/gui/UBFloatingPalette.cpp \ src/gui/UBToolbarButtonGroup.cpp \ @@ -83,7 +84,8 @@ SOURCES += src/gui/UBThumbnailView.cpp \ src/gui/UBFeaturesActionBar.cpp \ src/gui/UBMessagesDialog.cpp \ src/gui/UBOpenSankoreImporterWidget.cpp \ - src/gui/UBStartupHintsPalette.cpp + src/gui/UBStartupHintsPalette.cpp \ + src/gui/UBBackgroundPalette.cpp win32:SOURCES += src/gui/UBKeyboardPalette_win.cpp macx:OBJECTIVE_SOURCES += src/gui/UBKeyboardPalette_mac.mm linux-g++:SOURCES += src/gui/UBKeyboardPalette_linux.cpp diff --git a/src/tools/UBAbstractDrawRuler.cpp b/src/tools/UBAbstractDrawRuler.cpp index 170c4b2b..70fde592 100644 --- a/src/tools/UBAbstractDrawRuler.cpp +++ b/src/tools/UBAbstractDrawRuler.cpp @@ -58,11 +58,8 @@ UBAbstractDrawRuler::UBAbstractDrawRuler() : mShowButtons(false) , mAntiScaleRatio(1.0) { - //we actually need to evaluate pixels per millimeter - QDesktopWidget* desktop = UBApplication::desktop(); - int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2; - sPixelsPerMillimeter = qRound(dpiCommon / UBGeometryUtils::inchSize); + sPixelsPerCentimeter = UBApplication::boardController->activeScene()->backgroundGridSize(); } void UBAbstractDrawRuler::create(QGraphicsItem& item) diff --git a/src/tools/UBAbstractDrawRuler.h b/src/tools/UBAbstractDrawRuler.h index 60bc2e5f..73c10d05 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 sPixelsPerMillimeter; + int sPixelsPerCentimeter; }; #endif diff --git a/src/tools/UBGraphicsCompass.cpp b/src/tools/UBGraphicsCompass.cpp index 20c1c28b..d1282b1f 100644 --- a/src/tools/UBGraphicsCompass.cpp +++ b/src/tools/UBGraphicsCompass.cpp @@ -65,10 +65,6 @@ UBGraphicsCompass::UBGraphicsCompass() , mDrewCenterCross(false) { setRect(sDefaultRect); - //TODO claudio: remove code duplication - QDesktopWidget* desktop = UBApplication::desktop(); - int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2; - mPixelsPerMillimeter = qRound(dpiCommon / 25.4f); setFlag(QGraphicsItem::ItemIsMovable, true); setFlag(QGraphicsItem::ItemIsSelectable, true); @@ -427,7 +423,9 @@ void UBGraphicsCompass::paintAngleDisplay(QPainter *painter) void UBGraphicsCompass::paintRadiusDisplay(QPainter *painter) { - qreal radiusInCentimeters = rect().width() / (mPixelsPerMillimeter * 10); + double pixelsPerCentimeter = UBApplication::boardController->activeScene()->backgroundGridSize(); + + qreal radiusInCentimeters = rect().width() / pixelsPerCentimeter; QString format = rect().width() >= sDisplayRadiusUnitMinLength ? "%1 cm" : "%1"; QString radiusText = QString(format).arg(radiusInCentimeters, 0, 'f', 1); diff --git a/src/tools/UBGraphicsCompass.h b/src/tools/UBGraphicsCompass.h index 485237db..5cd80530 100644 --- a/src/tools/UBGraphicsCompass.h +++ b/src/tools/UBGraphicsCompass.h @@ -123,7 +123,6 @@ class UBGraphicsCompass: public QObject, public QGraphicsRectItem, public UBItem QGraphicsSvgItem* mResizeSvgItem; qreal mAntiScaleRatio; bool mDrewCenterCross; - int mPixelsPerMillimeter; // Constants static const int sNeedleLength = 18; diff --git a/src/tools/UBGraphicsRuler.cpp b/src/tools/UBGraphicsRuler.cpp index cb95cc88..65965fcf 100644 --- a/src/tools/UBGraphicsRuler.cpp +++ b/src/tools/UBGraphicsRuler.cpp @@ -176,20 +176,44 @@ void UBGraphicsRuler::paintGraduations(QPainter *painter) painter->save(); painter->setFont(font()); QFontMetricsF fontMetrics(painter->font()); - for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++) - { - int graduationX = rotationCenter().x() + sPixelsPerMillimeter * millimeters; - int graduationHeight = (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter) ? - UBGeometryUtils::centimeterGraduationHeight : - ((0 == millimeters % UBGeometryUtils::millimetersPerHalfCentimeter) ? - UBGeometryUtils::halfCentimeterGraduationHeight : UBGeometryUtils::millimeterGraduationHeight); - painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() + graduationHeight)); - painter->drawLine(QLine(graduationX, rotationCenter().y() + rect().height(), graduationX, rotationCenter().y() + rect().height() - graduationHeight)); - if (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter) + + // 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; + int rulerLengthInMillimeters = (rect().width() - sLeftEdgeMargin - sRoundingRadius)/pixelsPerMillimeter; + + // When a "centimeter" is too narrow, we only display every 5th number, and every 5th millimeter mark + double numbersWidth = fontMetrics.width("00"); + bool shouldDisplayAllNumbers = (numbersWidth <= (sPixelsPerCentimeter - 5)); + + for (int millimeters(0); millimeters < rulerLengthInMillimeters; millimeters++) { + + double graduationX = rotationCenter().x() + pixelsPerMillimeter * millimeters; + double graduationHeight = 0; + + if (millimeters % UBGeometryUtils::millimetersPerCentimeter == 0) + graduationHeight = UBGeometryUtils::centimeterGraduationHeight; + + else if (millimeters % UBGeometryUtils::millimetersPerHalfCentimeter == 0) + graduationHeight = UBGeometryUtils::halfCentimeterGraduationHeight; + + else + graduationHeight = UBGeometryUtils::millimeterGraduationHeight; + + + if (shouldDisplayAllNumbers || millimeters % UBGeometryUtils::millimetersPerHalfCentimeter == 0) { + painter->drawLine(QLineF(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() + graduationHeight)); + painter->drawLine(QLineF(graduationX, rotationCenter().y() + rect().height(), graduationX, rotationCenter().y() + rect().height() - graduationHeight)); + } + + + if ((shouldDisplayAllNumbers && millimeters % UBGeometryUtils::millimetersPerCentimeter == 0) + || millimeters % (UBGeometryUtils::millimetersPerCentimeter*5) == 0) { QString text = QString("%1").arg((int)(millimeters / UBGeometryUtils::millimetersPerCentimeter)); - if (graduationX + fontMetrics.width(text) / 2 < rect().right()) - { + + if (graduationX + fontMetrics.width(text) / 2 < rect().right()) { qreal textWidth = fontMetrics.width(text); qreal textHeight = fontMetrics.tightBoundingRect(text).height() + 5; painter->drawText( @@ -201,7 +225,10 @@ void UBGraphicsRuler::paintGraduations(QPainter *painter) } } } + + painter->restore(); + } void UBGraphicsRuler::paintRotationCenter(QPainter *painter) @@ -252,7 +279,7 @@ QRectF UBGraphicsRuler::closeButtonRect() const closePixmap.height() * mAntiScaleRatio); QPointF closeRectCenter( - rect().left() + sLeftEdgeMargin + sPixelsPerMillimeter * 5, + rect().left() + sLeftEdgeMargin + sPixelsPerCentimeter/2, rect().center().y()); QPointF closeRectTopLeft( @@ -270,9 +297,9 @@ QRectF UBGraphicsRuler::rotateButtonRect() const rotatePixmap.width() * mAntiScaleRatio, rotatePixmap.height() * mAntiScaleRatio); - int centimeters = (int)(rect().width() - sLeftEdgeMargin - resizeButtonRect().width()) / (int)(10 * sPixelsPerMillimeter); + int centimeters = (int)(rect().width() - sLeftEdgeMargin - resizeButtonRect().width()) / (sPixelsPerCentimeter); QPointF rotateRectCenter( - rect().left() + sLeftEdgeMargin + (centimeters - 0.5) * 10 * sPixelsPerMillimeter, + rect().left() + sLeftEdgeMargin + (centimeters - 0.5) * sPixelsPerCentimeter, rect().center().y()); QPointF rotateRectTopLeft( diff --git a/src/tools/UBGraphicsTriangle.cpp b/src/tools/UBGraphicsTriangle.cpp index 5ba3ab91..7ee4f553 100644 --- a/src/tools/UBGraphicsTriangle.cpp +++ b/src/tools/UBGraphicsTriangle.cpp @@ -349,14 +349,27 @@ void UBGraphicsTriangle::paintGraduations(QPainter *painter) painter->setFont(font()); QFontMetricsF fontMetrics(painter->font()); - for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / sPixelsPerMillimeter; millimeters++) + // 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; + + // When a "centimeter" is too narrow, we only display every 5th number, and every 5th millimeter mark + double numbersWidth = fontMetrics.width("00"); + bool shouldDisplayAllNumbers = (numbersWidth <= (sPixelsPerCentimeter - 5)); + + for (int millimeters = 0; millimeters < (rect().width() - sLeftEdgeMargin - sRoundingRadius) / pixelsPerMillimeter; millimeters++) { - int graduationX = rotationCenter().x() + kx * sPixelsPerMillimeter * millimeters; + double graduationX = rotationCenter().x() + kx * pixelsPerMillimeter * millimeters; + double graduationHeight = 0; - int graduationHeight = (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter) ? - UBGeometryUtils::centimeterGraduationHeight : - ((0 == millimeters % UBGeometryUtils::millimetersPerHalfCentimeter) ? - UBGeometryUtils::halfCentimeterGraduationHeight : UBGeometryUtils::millimeterGraduationHeight); + if (millimeters % UBGeometryUtils::millimetersPerCentimeter == 0) + graduationHeight = UBGeometryUtils::centimeterGraduationHeight; + + else if (millimeters % UBGeometryUtils::millimetersPerHalfCentimeter == 0) + graduationHeight = UBGeometryUtils::halfCentimeterGraduationHeight; + + else + graduationHeight = UBGeometryUtils::millimeterGraduationHeight; qreal requiredSpace = graduationHeight + SEPARATOR ; /* B____C @@ -387,12 +400,14 @@ void UBGraphicsTriangle::paintGraduations(QPainter *painter) qreal BC = rect().height(); qreal DE = AD * BC / AB, availableSpace = DE; - if (requiredSpace <= availableSpace) - painter->drawLine(QLine(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight)); - else + if (availableSpace < requiredSpace) break; - if (0 == millimeters % UBGeometryUtils::millimetersPerCentimeter) + if (shouldDisplayAllNumbers || millimeters % UBGeometryUtils::millimetersPerHalfCentimeter == 0) + painter->drawLine(QLineF(graduationX, rotationCenter().y(), graduationX, rotationCenter().y() - ky * graduationHeight)); + + if ((shouldDisplayAllNumbers && millimeters % UBGeometryUtils::millimetersPerCentimeter == 0) + || millimeters % (UBGeometryUtils::millimetersPerCentimeter*5) == 0) { QString text = QString("%1").arg((int)(millimeters / UBGeometryUtils::millimetersPerCentimeter)); qreal textWidth = fontMetrics.width(text);