KiCad PCB EDA Suite
DPI_SCALING Class Reference

Class to handle configuration and automatic determination of the DPI scale to use for canvases. More...

#include <dpi_scaling.h>

Public Member Functions

 DPI_SCALING (wxConfigBase *aConfig, const wxWindow *aWindow)
 Construct a DPI scale provider. More...
 
double GetScaleFactor () const
 Get the DPI scale from all known sources in order: More...
 
bool GetCanvasIsAutoScaled () const
 Is the current value auto scaled, or is it user-set in the config. More...
 
void SetDpiConfig (bool aAuto, double aValue)
 Set the common DPI config in a given config object. More...
 

Static Public Member Functions

static double GetMaxScaleFactor ()
 
static double GetMinScaleFactor ()
 
static double GetDefaultScaleFactor ()
 Get the "default" scaling factor to use if not other config is available. More...
 

Private Attributes

wxConfigBase * m_config
 The configuration object to use to get/set user setting. More...
 
const wxWindow * m_window
 The WX window to use for WX's automatic DPI checking. More...
 

Detailed Description

Class to handle configuration and automatic determination of the DPI scale to use for canvases.

This has several sources and the availability of some of them are platform dependent.

Definition at line 35 of file dpi_scaling.h.

Constructor & Destructor Documentation

◆ DPI_SCALING()

DPI_SCALING::DPI_SCALING ( wxConfigBase *  aConfig,
const wxWindow *  aWindow 
)

Construct a DPI scale provider.

Parameters
aConfigthe config store to check for a user value (can be nullptr, in which case on automatically determined values are considered)
aWindowa WX window to use for automatic DPI determination
Returns
the scaling factor (1.0 = no scaling)

Definition at line 96 of file dpi_scaling.cpp.

97  : m_config( aConfig ), m_window( aWindow )
98 {
99 }
const wxWindow * m_window
The WX window to use for WX's automatic DPI checking.
Definition: dpi_scaling.h:99
wxConfigBase * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:94

Member Function Documentation

◆ GetCanvasIsAutoScaled()

bool DPI_SCALING::GetCanvasIsAutoScaled ( ) const

Is the current value auto scaled, or is it user-set in the config.

Definition at line 136 of file dpi_scaling.cpp.

137 {
138  if( m_config == nullptr )
139  {
140  // No configuration given, so has to be automatic scaling
141  return true;
142  }
143 
144  const bool automatic = getKiCadConfiguredScale( *m_config ) == boost::none;
145  wxLogTrace( traceHiDpi, "Scale is automatic: %d", automatic );
146  return automatic;
147 }
static OPT< double > getKiCadConfiguredScale(const wxConfigBase &aConfig)
Get a user-configured scale factor from KiCad config file.
Definition: dpi_scaling.cpp:50
const wxChar *const traceHiDpi
Flag to enable trace for HiDPI scaling factors.
Definition: dpi_scaling.cpp:42
wxConfigBase * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:94

References getKiCadConfiguredScale(), m_config, and traceHiDpi.

Referenced by PANEL_COMMON_SETTINGS::TransferDataToWindow().

◆ GetDefaultScaleFactor()

double DPI_SCALING::GetDefaultScaleFactor ( )
static

Get the "default" scaling factor to use if not other config is available.

Definition at line 174 of file dpi_scaling.cpp.

175 {
176  // no scaling => 1.0
177  return 1.0;
178 }

Referenced by GetScaleFactor(), and PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS().

◆ GetMaxScaleFactor()

double DPI_SCALING::GetMaxScaleFactor ( )
static

Definition at line 160 of file dpi_scaling.cpp.

161 {
162  // displays with higher than 4.0 DPI are not really going to be useful
163  // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
164  return 6.0;
165 }

Referenced by PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS().

◆ GetMinScaleFactor()

double DPI_SCALING::GetMinScaleFactor ( )
static

Definition at line 168 of file dpi_scaling.cpp.

169 {
170  // scales under 1.0 don't make sense from a HiDPI perspective
171  return 1.0;
172 }

Referenced by PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS().

◆ GetScaleFactor()

double DPI_SCALING::GetScaleFactor ( ) const

Get the DPI scale from all known sources in order:

  • user config, if given
  • user's environment variables, if set and according to platform
  • WX's internal determination of the DPI scaling (WX > 3.1)

Definition at line 102 of file dpi_scaling.cpp.

103 {
104  OPT<double> val;
105 
106  if( m_config )
107  {
109  }
110 
111  if( !val )
112  {
113  val = getEnviromentScale();
114  }
115 
116  if( !val && m_window )
117  {
118  // Use the native WX reporting.
119  // On Linux, this will not work until WX 3.2 and GTK >= 3.10
120  // Otherwise it returns 1.0
121  val = m_window->GetContentScaleFactor();
122  wxLogTrace( traceHiDpi, "Scale factor (WX): %f", *val );
123  }
124 
125  if( !val )
126  {
127  // Nothing else we can do, give it a default value
128  val = GetDefaultScaleFactor();
129  wxLogTrace( traceHiDpi, "Scale factor (default): %f", *val );
130  }
131 
132  return *val;
133 }
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
static OPT< double > getEnviromentScale()
Get the toolkit scale factor from a user-set environment variable (for example GDK_SCALE on GTK).
Definition: dpi_scaling.cpp:76
static OPT< double > getKiCadConfiguredScale(const wxConfigBase &aConfig)
Get a user-configured scale factor from KiCad config file.
Definition: dpi_scaling.cpp:50
const wxChar *const traceHiDpi
Flag to enable trace for HiDPI scaling factors.
Definition: dpi_scaling.cpp:42
boost::optional< T > OPT
Definition: optional.h:7
const wxWindow * m_window
The WX window to use for WX's automatic DPI checking.
Definition: dpi_scaling.h:99
wxConfigBase * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:94

References GetDefaultScaleFactor(), getEnviromentScale(), getKiCadConfiguredScale(), m_config, m_window, and traceHiDpi.

Referenced by PANEL_COMMON_SETTINGS::OnCanvasScaleAuto(), and PANEL_COMMON_SETTINGS::TransferDataToWindow().

◆ SetDpiConfig()

void DPI_SCALING::SetDpiConfig ( bool  aAuto,
double  aValue 
)

Set the common DPI config in a given config object.

The encoding of the automatic/manual nature of the config is handled internally.

Parameters
aAutostore a value meaning "no user-set scale"
aValuethe value to store (ignored if aAuto set)

Definition at line 150 of file dpi_scaling.cpp.

151 {
152  wxCHECK_RET( m_config != nullptr, "Setting DPI config without a config store." );
153 
154  const double value = aAuto ? 0.0 : aValue;
155 
156  m_config->Write( CANVAS_SCALE_KEY, value );
157 }
#define CANVAS_SCALE_KEY
Definition: pgm_base.h:48
wxConfigBase * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:94

References CANVAS_SCALE_KEY, and m_config.

Referenced by PANEL_COMMON_SETTINGS::TransferDataFromWindow().

Member Data Documentation

◆ m_config

wxConfigBase* DPI_SCALING::m_config
private

The configuration object to use to get/set user setting.

nullptr if only automatic options are wanted

Definition at line 94 of file dpi_scaling.h.

Referenced by GetCanvasIsAutoScaled(), GetScaleFactor(), and SetDpiConfig().

◆ m_window

const wxWindow* DPI_SCALING::m_window
private

The WX window to use for WX's automatic DPI checking.

Definition at line 99 of file dpi_scaling.h.

Referenced by GetScaleFactor().


The documentation for this class was generated from the following files: