Move to pixel size instead of point size for fonts

This commit is contained in:
Gaute Lindkvist 2020-01-07 14:00:51 +01:00
parent f279e85c2c
commit 4829c5eba2
14 changed files with 73 additions and 30 deletions

View File

@ -18,9 +18,14 @@
#include "RiaFontCache.h" #include "RiaFontCache.h"
#include "RiaGuiApplication.h"
#include "cafAppEnum.h" #include "cafAppEnum.h"
#include "cafFixedAtlasFont.h" #include "cafFixedAtlasFont.h"
#include <QDesktopWidget>
#include <cmath>
namespace caf namespace caf
{ {
template <> template <>
@ -131,6 +136,36 @@ RiaFontCache::FontSize RiaFontCache::fontSizeEnumFromPointSize( int pointSize )
return closestEnumValue; return closestEnumValue;
} }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaFontCache::pointSizeToPixelSize( int pointSize )
{
auto app = RiaGuiApplication::instance();
if ( app )
{
int dpi = app->desktop()->logicalDpiX();
double inches = pointSize / 72.0;
return static_cast<int>( std::ceil( inches * dpi ) );
}
return pointSize;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaFontCache::pixelSizeToPointSize( int pixelSize )
{
auto app = RiaGuiApplication::instance();
if ( app )
{
int dpi = app->desktop()->logicalDpiX();
double inches = pixelSize / dpi;
return static_cast<int>( std::ceil( inches * 72.0 ) );
}
return pixelSize;
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
/// ///
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -56,6 +56,8 @@ public:
static cvf::ref<caf::FixedAtlasFont> getFont( FontSize fontSize ); static cvf::ref<caf::FixedAtlasFont> getFont( FontSize fontSize );
static FontSize legacyEnumToPointSize( int enumValue ); static FontSize legacyEnumToPointSize( int enumValue );
static FontSize fontSizeEnumFromPointSize( int pointSize ); static FontSize fontSizeEnumFromPointSize( int pointSize );
static int pointSizeToPixelSize( int pointSize );
static int pixelSizeToPointSize( int pixelSize );
static void clear(); static void clear();

View File

@ -308,7 +308,7 @@ QString RimGridCrossPlotDataSet::groupTitle() const
{ {
if ( m_grouping != NO_GROUPING ) if ( m_grouping != NO_GROUPING )
{ {
return QString( "[%1]" ).arg( groupParameter() ); return groupParameter();
} }
return ""; return "";
} }

View File

@ -48,7 +48,7 @@ QSize RiuAbstractLegendFrame::sizeHint() const
QFontMetrics fontMetrics( this->font() ); QFontMetrics fontMetrics( this->font() );
QRect titleRect = fontMetrics.boundingRect( QRect( 0, 0, 200, 200 ), Qt::AlignLeft, m_title ); QRect titleRect = fontMetrics.boundingRect( QRect( 0, 0, 200, 200 ), Qt::AlignLeft, m_title );
int preferredContentHeight = titleRect.height() + labelCount() * layout.lineSpacing; int preferredContentHeight = titleRect.height() + labelCount() * layout.lineSpacing + 0.5 * layout.lineSpacing;
int preferredHeight = preferredContentHeight + layout.margins.top() + layout.margins.bottom(); int preferredHeight = preferredContentHeight + layout.margins.top() + layout.margins.bottom();
int titleWidth = titleRect.width() + layout.margins.left() + layout.margins.right(); int titleWidth = titleRect.width() + layout.margins.left() + layout.margins.right();
@ -81,7 +81,7 @@ QSize RiuAbstractLegendFrame::minimumSizeHint() const
QFontMetrics fontMetrics( this->font() ); QFontMetrics fontMetrics( this->font() );
QRect titleRect = fontMetrics.boundingRect( QRect( 0, 0, 200, 200 ), Qt::AlignLeft, m_title ); QRect titleRect = fontMetrics.boundingRect( QRect( 0, 0, 200, 200 ), Qt::AlignLeft, m_title );
int preferredContentHeight = titleRect.height() + 2.25 * layout.lineSpacing; int preferredContentHeight = titleRect.height() + 2 * layout.lineSpacing + 0.5 * layout.lineSpacing;
int preferredHeight = preferredContentHeight + layout.margins.top() + layout.margins.bottom(); int preferredHeight = preferredContentHeight + layout.margins.top() + layout.margins.bottom();
int titleWidth = titleRect.width() + layout.margins.left() + layout.margins.right(); int titleWidth = titleRect.width() + layout.margins.left() + layout.margins.right();
@ -104,22 +104,24 @@ QSize RiuAbstractLegendFrame::minimumSizeHint() const
void RiuAbstractLegendFrame::renderTo( QPainter* painter, const QRect& targetRect ) void RiuAbstractLegendFrame::renderTo( QPainter* painter, const QRect& targetRect )
{ {
QFont font = this->font(); QFont font = this->font();
font.setPointSize( RiaApplication::instance()->preferences()->defaultPlotFontSize() ); font.setPixelSize(
RiaFontCache::pointSizeToPixelSize( RiaApplication::instance()->preferences()->defaultPlotFontSize() ) );
this->setFont( font ); this->setFont( font );
LayoutInfo layout( QSize( targetRect.width(), targetRect.height() ) ); LayoutInfo layout( QSize( targetRect.width(), targetRect.height() ) );
layoutInfo( &layout ); layoutInfo( &layout );
painter->save(); painter->save();
painter->translate( targetRect.topLeft() );
painter->setFont( this->font() ); painter->setFont( this->font() );
painter->translate( targetRect.topLeft() );
QPoint titlePos( layout.margins.left(), layout.margins.top() ); QPoint titlePos( layout.margins.left(), layout.margins.top() );
{ {
painter->save(); painter->save();
painter->translate( QPoint( layout.margins.left(), layout.margins.top() ) );
QTextDocument td; QTextDocument td;
td.setDocumentMargin( 0.0 );
td.setDefaultFont( this->font() ); td.setDefaultFont( this->font() );
td.setHtml( m_title ); td.setPlainText( m_title );
td.drawContents( painter ); td.drawContents( painter );
painter->restore(); painter->restore();
} }
@ -130,6 +132,7 @@ void RiuAbstractLegendFrame::renderTo( QPainter* painter, const QRect& targetRec
painter->save(); painter->save();
painter->translate( tickLabel.first.topLeft() ); painter->translate( tickLabel.first.topLeft() );
QTextDocument td; QTextDocument td;
td.setDocumentMargin( 0.0 );
td.setDefaultFont( this->font() ); td.setDefaultFont( this->font() );
td.setPlainText( tickLabel.second ); td.setPlainText( tickLabel.second );
td.drawContents( painter ); td.drawContents( painter );

View File

@ -51,7 +51,7 @@ RiuTextOverlayContentFrame::RiuTextOverlayContentFrame( QWidget* parent /*= null
layout->addWidget( m_textLabel ); layout->addWidget( m_textLabel );
QFont font = m_textLabel->font(); QFont font = m_textLabel->font();
font.setPointSize( RiaApplication::instance()->preferences()->defaultPlotFontSize() ); RiaFontCache::pointSizeToPixelSize( RiaApplication::instance()->preferences()->defaultPlotFontSize() );
m_textLabel->setFont( font ); m_textLabel->setFont( font );
} }

View File

@ -37,9 +37,9 @@ void RiuCategoryLegendFrame::layoutInfo( LayoutInfo* layout ) const
int colorBarWidth = 25; int colorBarWidth = 25;
int colorBarHeight = layout->overallLegendSize.height() - layout->margins.top() - layout->margins.bottom() - int colorBarHeight = layout->overallLegendSize.height() - layout->margins.top() - layout->margins.bottom() -
titleLines.size() * layout->lineSpacing; titleLines.size() * layout->lineSpacing - 0.5 * layout->lineSpacing;
int colorBarStartY = layout->margins.top() + titleLines.size() * layout->lineSpacing; int colorBarStartY = layout->margins.top() + titleLines.size() * layout->lineSpacing + 0.5 * layout->lineSpacing;
layout->colorBarRect = QRect( layout->margins.left(), colorBarStartY, colorBarWidth, colorBarHeight ); layout->colorBarRect = QRect( layout->margins.left(), colorBarStartY, colorBarWidth, colorBarHeight );
@ -100,9 +100,8 @@ QRect RiuCategoryLegendFrame::labelRect( const LayoutInfo& layout, int index ) c
{ {
float categoryHeight = static_cast<float>( layout.colorBarRect.height() ) / labelCount(); float categoryHeight = static_cast<float>( layout.colorBarRect.height() ) / labelCount();
const int posX = layout.tickEndX + layout.tickTextLeadSpace; const int posX = layout.tickEndX + layout.tickTextLeadSpace;
int posY = static_cast<int>( layout.colorBarRect.bottom() - index * categoryHeight ); int posY = static_cast<int>( layout.colorBarRect.bottom() - ( index + 1 ) * categoryHeight );
int height = std::max( (int)categoryHeight, layout.charHeight );
QString labelI = this->label( index ); QString labelI = this->label( index );
int width = this->fontMetrics().boundingRect( labelI ).width(); int width = this->fontMetrics().boundingRect( labelI ).width();
return QRect( posX, posY - height, width, height ); return QRect( posX, posY, width, categoryHeight );
} }

View File

@ -47,12 +47,13 @@ void RiuDockedQwtPlot::applyFontSizes( bool replot /*= false*/ )
{ {
QwtText text = this->axisTitle( axis ); QwtText text = this->axisTitle( axis );
QFont font = text.font(); QFont font = text.font();
font.setPointSize( fontPointSize ); font.setPixelSize(
RiaFontCache::pointSizeToPixelSize( RiaApplication::instance()->preferences()->defaultPlotFontSize() ) );
text.setFont( font ); text.setFont( font );
this->setAxisTitle( axis, text ); this->setAxisTitle( axis, text );
QFont valuesFont = this->axisFont( axis ); QFont valuesFont = this->axisFont( axis );
valuesFont.setPointSize( fontPointSize ); valuesFont.setPixelSize( font.pixelSize() );
this->setAxisFont( axis, valuesFont ); this->setAxisFont( axis, valuesFont );
} }

View File

@ -148,7 +148,7 @@ void RiuGridCrossQwtPlot::setLegendFontSize( int fontSize )
if ( legend() ) if ( legend() )
{ {
QFont font = legend()->font(); QFont font = legend()->font();
font.setPointSize( fontSize ); font.setPixelSize( RiaFontCache::pointSizeToPixelSize( fontSize ) );
legend()->setFont( font ); legend()->setFont( font );
// Set font size for all existing labels // Set font size for all existing labels
QList<QwtLegendLabel*> labels = legend()->findChildren<QwtLegendLabel*>(); QList<QwtLegendLabel*> labels = legend()->findChildren<QwtLegendLabel*>();

View File

@ -262,10 +262,11 @@ void RiuMultiPlotPage::setSelectionsVisible( bool visible )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RiuMultiPlotPage::setFontSize( int fontSize ) void RiuMultiPlotPage::setFontSize( int fontSize )
{ {
int pixelSize = RiaFontCache::pointSizeToPixelSize( fontSize );
{ {
QFont font = m_plotTitle->font(); QFont font = m_plotTitle->font();
font.setPointSize( fontSize + 2 ); font.setPixelSize( pixelSize + 2 );
font.setBold( true ); font.setBold( true );
m_plotTitle->setFont( font ); m_plotTitle->setFont( font );
} }
@ -273,7 +274,7 @@ void RiuMultiPlotPage::setFontSize( int fontSize )
for ( QLabel* subTitle : m_subTitles ) for ( QLabel* subTitle : m_subTitles )
{ {
QFont font = subTitle->font(); QFont font = subTitle->font();
font.setPointSize( fontSize ); font.setPixelSize( pixelSize );
font.setBold( true ); font.setBold( true );
subTitle->setFont( font ); subTitle->setFont( font );
} }
@ -733,7 +734,7 @@ void RiuMultiPlotPage::reinsertPlotWidgets()
} }
legends[visibleIndex]->setMaxColumns( legendColumns ); legends[visibleIndex]->setMaxColumns( legendColumns );
QFont legendFont = legends[visibleIndex]->font(); QFont legendFont = legends[visibleIndex]->font();
legendFont.setPointSize( m_plotDefinition->legendFontSize() ); legendFont.setPixelSize( RiaFontCache::pointSizeToPixelSize( m_plotDefinition->legendFontSize() ) );
legends[visibleIndex]->setFont( legendFont ); legends[visibleIndex]->setFont( legendFont );
legends[visibleIndex]->show(); legends[visibleIndex]->show();
} }

View File

@ -245,8 +245,10 @@ void RiuMultiPlotWindow::setSelectionsVisible( bool visible )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
void RiuMultiPlotWindow::setFontSize( int fontSize ) void RiuMultiPlotWindow::setFontSize( int fontSize )
{ {
QFont font = this->font(); QFont font = this->font();
font.setPointSize( fontSize ); int pixelSize = RiaFontCache::pointSizeToPixelSize( fontSize );
font.setPixelSize( pixelSize );
this->setFont( font ); this->setFont( font );
for ( auto page : m_pages ) for ( auto page : m_pages )
{ {
@ -259,7 +261,7 @@ void RiuMultiPlotWindow::setFontSize( int fontSize )
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
int RiuMultiPlotWindow::fontSize() const int RiuMultiPlotWindow::fontSize() const
{ {
return this->font().pointSize(); return RiaFontCache::pixelSizeToPointSize( this->font().pixelSize() );
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View File

@ -66,7 +66,7 @@ void RiuQwtPlotTools::setCommonPlotBehaviour( QwtPlot* plot )
int fontSize = RiaApplication::instance()->preferences()->defaultPlotFontSize(); int fontSize = RiaApplication::instance()->preferences()->defaultPlotFontSize();
// Axis number font // Axis number font
QFont axisFont = plot->axisFont( QwtPlot::xBottom ); QFont axisFont = plot->axisFont( QwtPlot::xBottom );
axisFont.setPointSize( fontSize ); axisFont.setPixelSize( RiaFontCache::pointSizeToPixelSize( fontSize ) );
plot->setAxisFont( QwtPlot::xBottom, axisFont ); plot->setAxisFont( QwtPlot::xBottom, axisFont );
plot->setAxisFont( QwtPlot::xTop, axisFont ); plot->setAxisFont( QwtPlot::xTop, axisFont );
@ -80,7 +80,7 @@ void RiuQwtPlotTools::setCommonPlotBehaviour( QwtPlot* plot )
{ {
QwtText axisTitle = plot->axisTitle( axis ); QwtText axisTitle = plot->axisTitle( axis );
QFont axisTitleFont = axisTitle.font(); QFont axisTitleFont = axisTitle.font();
axisTitleFont.setPointSize( fontSize ); axisTitleFont.setPixelSize( RiaFontCache::pointSizeToPixelSize( fontSize ) );
axisTitleFont.setBold( false ); axisTitleFont.setBold( false );
axisTitle.setFont( axisTitleFont ); axisTitle.setFont( axisTitleFont );
axisTitle.setRenderFlags( Qt::AlignRight ); axisTitle.setRenderFlags( Qt::AlignRight );

View File

@ -145,14 +145,14 @@ void RiuQwtPlotWidget::setAxisFontsAndAlignment( QwtPlot::Axis axis,
{ {
// Axis number font // Axis number font
QFont axisFont = this->axisFont( axis ); QFont axisFont = this->axisFont( axis );
axisFont.setPointSize( valueFontSize ); axisFont.setPixelSize( RiaFontCache::pointSizeToPixelSize( valueFontSize ) );
axisFont.setBold( false ); axisFont.setBold( false );
this->setAxisFont( axis, axisFont ); this->setAxisFont( axis, axisFont );
// Axis title font // Axis title font
QwtText axisTitle = this->axisTitle( axis ); QwtText axisTitle = this->axisTitle( axis );
QFont axisTitleFont = axisTitle.font(); QFont axisTitleFont = axisTitle.font();
axisTitleFont.setPointSize( titleFontSize ); axisTitleFont.setPixelSize( RiaFontCache::pointSizeToPixelSize( titleFontSize ) );
axisTitleFont.setBold( titleBold ); axisTitleFont.setBold( titleBold );
axisTitle.setFont( axisTitleFont ); axisTitle.setFont( axisTitleFont );
axisTitle.setRenderFlags( alignment ); axisTitle.setRenderFlags( alignment );

View File

@ -78,9 +78,9 @@ void RiuScalarMapperLegendFrame::layoutInfo( LayoutInfo* layout ) const
int colorBarWidth = 25; int colorBarWidth = 25;
int colorBarHeight = layout->overallLegendSize.height() - layout->margins.top() - layout->margins.bottom() - int colorBarHeight = layout->overallLegendSize.height() - layout->margins.top() - layout->margins.bottom() -
titleLines.size() * layout->lineSpacing; titleLines.size() * layout->lineSpacing - 0.5 * layout->lineSpacing;
int colorBarStartY = layout->margins.top() + titleLines.size() * layout->lineSpacing; int colorBarStartY = layout->margins.top() + titleLines.size() * layout->lineSpacing + 0.5 * layout->lineSpacing;
layout->colorBarRect = QRect( layout->margins.left(), colorBarStartY, colorBarWidth, colorBarHeight ); layout->colorBarRect = QRect( layout->margins.left(), colorBarStartY, colorBarWidth, colorBarHeight );
@ -203,5 +203,5 @@ QRect RiuScalarMapperLegendFrame::labelRect( const LayoutInfo& layout, int index
QString labelI = this->label( index ); QString labelI = this->label( index );
int width = this->fontMetrics().boundingRect( labelI ).width() + 4; int width = this->fontMetrics().boundingRect( labelI ).width() + 4;
int height = std::min( layout.lineSpacing, std::abs( posY - posYp1 ) ); int height = std::min( layout.lineSpacing, std::abs( posY - posYp1 ) );
return QRect( posX, posY - height / 2 - layout.charAscent / 2, width, height ); return QRect( posX, posY - height / 2, width, height );
} }

View File

@ -172,7 +172,7 @@ void RiuSummaryQwtPlot::setLegendFontSize( int fontSize )
if ( legend() ) if ( legend() )
{ {
QFont font = legend()->font(); QFont font = legend()->font();
font.setPointSize( fontSize ); font.setPixelSize( RiaFontCache::pointSizeToPixelSize( fontSize ) );
legend()->setFont( font ); legend()->setFont( font );
// Set font size for all existing labels // Set font size for all existing labels
QList<QwtLegendLabel*> labels = legend()->findChildren<QwtLegendLabel*>(); QList<QwtLegendLabel*> labels = legend()->findChildren<QwtLegendLabel*>();