You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.TXT for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef RENDER_SETTINGS_H
  24. #define RENDER_SETTINGS_H
  25. #include <map>
  26. #include <set>
  27. #include <gal/color4d.h>
  28. #include <layer_ids.h>
  29. #include <memory>
  30. #include <wx/dc.h>
  31. class COLOR_SETTINGS;
  32. namespace KIGFX
  33. {
  34. class VIEW_ITEM;
  35. /**
  36. * Container for all the knowledge about how graphical objects are drawn on any output
  37. * surface/device.
  38. *
  39. * This includes:
  40. * - color/transparency settings
  41. * - highlighting and high contrast mode control
  42. * - drawing quality control (sketch/outline mode)
  43. * - text processing flags
  44. *
  45. * The class acts as an interface between the PAINTER object and the GUI (i.e. Layers/Items
  46. * widget or display options dialog).
  47. */
  48. class RENDER_SETTINGS
  49. {
  50. public:
  51. RENDER_SETTINGS();
  52. virtual ~RENDER_SETTINGS();
  53. virtual void LoadColors( const COLOR_SETTINGS* aSettings ) { }
  54. /**
  55. * Set the specified layer as high-contrast.
  56. *
  57. * @param aLayerId is a layer number that should be displayed in a specific mode.
  58. * @param aEnabled is the new layer state ( true = active or false = not active).
  59. */
  60. inline void SetLayerIsHighContrast( int aLayerId, bool aEnabled = true )
  61. {
  62. if( aEnabled )
  63. m_highContrastLayers.insert( aLayerId );
  64. else
  65. m_highContrastLayers.erase( aLayerId );
  66. }
  67. /**
  68. * Return information whether the queried layer is marked as high-contrast.
  69. *
  70. * @return True if the queried layer is marked as active.
  71. */
  72. inline bool GetLayerIsHighContrast( int aLayerId ) const
  73. {
  74. return ( m_highContrastLayers.count( aLayerId ) > 0 );
  75. }
  76. /**
  77. * Returns the set of currently high-contrast layers.
  78. */
  79. const std::set<int> GetHighContrastLayers() const
  80. {
  81. return m_highContrastLayers;
  82. }
  83. /**
  84. * Return the board layer which is in high-contrast mode.
  85. *
  86. * There should only be one board layer which is high-contrast at any given time, although
  87. * there might be many high-contrast synthetic (GAL) layers.
  88. */
  89. PCB_LAYER_ID GetPrimaryHighContrastLayer() const
  90. {
  91. for( int layer : m_highContrastLayers )
  92. {
  93. if( layer >= PCBNEW_LAYER_ID_START && layer < PCB_LAYER_ID_COUNT )
  94. return (PCB_LAYER_ID) layer;
  95. }
  96. return UNDEFINED_LAYER;
  97. }
  98. PCB_LAYER_ID GetActiveLayer() const { return m_activeLayer; }
  99. void SetActiveLayer( PCB_LAYER_ID aLayer ) { m_activeLayer = aLayer; }
  100. const wxString& GetLayerName() const { return m_layerName; }
  101. void SetLayerName( const wxString& aLayerName ) { m_layerName = aLayerName; }
  102. LSET GetPrintLayers() const { return m_printLayers; }
  103. void SetPrintLayers( LSET aLayerSet ) { m_printLayers = aLayerSet; }
  104. /**
  105. * Clear the list of active layers.
  106. */
  107. inline void ClearHighContrastLayers()
  108. {
  109. m_highContrastLayers.clear();
  110. }
  111. /**
  112. * Return current highlight setting.
  113. *
  114. * @return True if highlight is enabled, false otherwise.
  115. */
  116. inline bool IsHighlightEnabled() const
  117. {
  118. return m_highlightEnabled;
  119. }
  120. /**
  121. * Return the netcode of currently highlighted net.
  122. *
  123. * @return Netcode of currently highlighted net.
  124. */
  125. inline const std::set<int>& GetHighlightNetCodes() const
  126. {
  127. return m_highlightNetcodes;
  128. }
  129. /**
  130. * Turns on/off highlighting.
  131. *
  132. * It may be done for the active layer or the specified net(s)..
  133. *
  134. * @param aEnabled tells if highlighting should be enabled.
  135. * @param aNetcode is optional and if specified, turns on highlighting only for the net with
  136. * number given as the parameter.
  137. */
  138. inline void SetHighlight( bool aEnabled, int aNetcode = -1, bool aMulti = false )
  139. {
  140. m_highlightEnabled = aEnabled;
  141. if( aEnabled )
  142. {
  143. if( !aMulti )
  144. m_highlightNetcodes.clear();
  145. m_highlightNetcodes.insert( aNetcode );
  146. }
  147. else
  148. m_highlightNetcodes.clear();
  149. }
  150. /**
  151. * Turns on highlighting and highlights multiple nets
  152. * @param aHighlight is a set of netcodes to highlight
  153. * @param aEnabled tells if highlighting should be enabled.
  154. */
  155. inline void SetHighlight( std::set<int>& aHighlight, bool aEnabled = true )
  156. {
  157. m_highlightEnabled = aEnabled;
  158. if( aEnabled )
  159. m_highlightNetcodes = aHighlight;
  160. else
  161. m_highlightNetcodes.clear();
  162. }
  163. /**
  164. * Turns on/off high contrast display mode.
  165. */
  166. void SetHighContrast( bool aEnabled ) { m_hiContrastEnabled = aEnabled; }
  167. bool GetHighContrast() const { return m_hiContrastEnabled; }
  168. void SetDrawBoundingBoxes( bool aEnabled ) { m_drawBoundingBoxes = aEnabled; }
  169. bool GetDrawBoundingBoxes() const { return m_drawBoundingBoxes; }
  170. /**
  171. * Returns the color that should be used to draw the specific VIEW_ITEM on the specific layer
  172. * using currently used render settings.
  173. *
  174. * @param aItem is the VIEW_ITEM.
  175. * @param aLayer is the layer.
  176. * @return The color.
  177. */
  178. virtual COLOR4D GetColor( const VIEW_ITEM* aItem, int aLayer ) const = 0;
  179. float GetDrawingSheetLineWidth() const { return m_drawingSheetLineWidth; }
  180. int GetDefaultPenWidth() const { return m_defaultPenWidth; }
  181. void SetDefaultPenWidth( int aWidth ) { m_defaultPenWidth = aWidth; }
  182. int GetMinPenWidth() const { return m_minPenWidth; }
  183. void SetMinPenWidth( int aWidth ) { m_minPenWidth = aWidth; }
  184. double GetDashLengthRatio() const { return m_dashLengthRatio; }
  185. void SetDashLengthRatio( double aRatio ) { m_dashLengthRatio = aRatio; }
  186. double GetDashLength( int aLineWidth ) const;
  187. double GetDotLength( int aLineWidth ) const;
  188. double GetGapLengthRatio() const { return m_gapLengthRatio; }
  189. void SetGapLengthRatio( double aRatio ) { m_gapLengthRatio = aRatio; }
  190. double GetGapLength( int aLineWidth ) const;
  191. virtual bool GetShowPageLimits() const { return true; }
  192. bool IsPrinting() const { return m_isPrinting; }
  193. void SetIsPrinting( bool isPrinting ) { m_isPrinting = isPrinting; }
  194. bool IsPrintBlackAndWhite() const { return m_printBlackAndWite; }
  195. void SetPrintBlackAndWhite( bool aPrintBlackAndWhite )
  196. {
  197. m_printBlackAndWite = aPrintBlackAndWhite;
  198. }
  199. bool PrintBlackAndWhiteReq() const
  200. {
  201. return m_printBlackAndWite && m_isPrinting;
  202. }
  203. /**
  204. * Return current background color settings.
  205. */
  206. virtual const COLOR4D& GetBackgroundColor() const = 0;
  207. /**
  208. * Set the background color.
  209. */
  210. virtual void SetBackgroundColor( const COLOR4D& aColor ) = 0;
  211. /**
  212. * Return current grid color settings.
  213. */
  214. virtual const COLOR4D& GetGridColor() = 0;
  215. /**
  216. * Return current cursor color settings.
  217. */
  218. virtual const COLOR4D& GetCursorColor() = 0;
  219. /**
  220. * Return the color used to draw a layer.
  221. *
  222. * @param aLayer is the layer number.
  223. */
  224. inline const COLOR4D& GetLayerColor( int aLayer ) const
  225. {
  226. // We don't (yet?) have a separate color for intersheet refs
  227. if( aLayer == LAYER_INTERSHEET_REFS )
  228. aLayer = LAYER_GLOBLABEL;
  229. return m_layerColors[aLayer];
  230. }
  231. /**
  232. * Change the color used to draw a layer.
  233. *
  234. * @param aLayer is the layer number.
  235. * @param aColor is the new color.
  236. */
  237. inline void SetLayerColor( int aLayer, const COLOR4D& aColor )
  238. {
  239. m_layerColors[aLayer] = aColor;
  240. update(); // recompute other shades of the color
  241. }
  242. virtual bool IsBackgroundDark() const
  243. {
  244. return false;
  245. }
  246. /**
  247. * Set line width used for drawing outlines.
  248. *
  249. * @param aWidth is the new width.
  250. */
  251. void SetOutlineWidth( float aWidth )
  252. {
  253. m_outlineWidth = aWidth;
  254. }
  255. void SetHighlightFactor( float aFactor ) { m_highlightFactor = aFactor; }
  256. void SetSelectFactor( float aFactor ) { m_selectFactor = aFactor; }
  257. void SetDefaultFont( const wxString& aFont ) { m_defaultFont = aFont; }
  258. const wxString& GetDefaultFont() const { return m_defaultFont; }
  259. // TODO: these can go away once the drawing sheet is moved to Cairo-based printing
  260. wxDC* GetPrintDC() const { return m_printDC; }
  261. void SetPrintDC( wxDC* aDC ) { m_printDC = aDC; }
  262. protected:
  263. /**
  264. * Precalculates extra colors for layers (e.g. highlighted, darkened and any needed version
  265. * of base colors).
  266. */
  267. virtual void update();
  268. PCB_LAYER_ID m_activeLayer; // The active layer (as shown by appearance mgr)
  269. wxString m_layerName;
  270. std::set<int> m_highContrastLayers; // High-contrast layers (both board layers and
  271. // synthetic GAL layers)
  272. COLOR4D m_layerColors[LAYER_ID_COUNT]; // Layer colors
  273. COLOR4D m_layerColorsHi[LAYER_ID_COUNT]; // Layer colors for highlighted objects
  274. COLOR4D m_layerColorsSel[LAYER_ID_COUNT]; // Layer colors for selected objects
  275. COLOR4D m_hiContrastColor[LAYER_ID_COUNT]; // High-contrast mode layer colors
  276. COLOR4D m_layerColorsDark[LAYER_ID_COUNT]; // Darkened layer colors (for high-contrast mode)
  277. COLOR4D m_backgroundColor; // The background color
  278. /// Parameters for display modes
  279. bool m_hiContrastEnabled; // High contrast display mode on/off
  280. float m_hiContrastFactor; // Factor used for computing high contrast color
  281. bool m_highlightEnabled; // Highlight display mode on/off
  282. std::set<int> m_highlightNetcodes; // Set of net cods to be highlighted
  283. float m_highlightFactor; // Factor used for computing highlight color
  284. bool m_drawBoundingBoxes; // Visual aid for debugging
  285. float m_selectFactor; // Specifies how color of selected items is changed
  286. float m_outlineWidth; // Line width used when drawing outlines
  287. float m_drawingSheetLineWidth;// Line width used for borders and titleblock
  288. int m_defaultPenWidth;
  289. int m_minPenWidth; // Some clients (such as PDF) don't like ultra-thin
  290. // lines. This sets an absolute minimum.
  291. double m_dashLengthRatio;
  292. double m_gapLengthRatio;
  293. wxString m_defaultFont;
  294. bool m_isPrinting; // true when draw to a printer
  295. bool m_printBlackAndWite; // true if black and white printing is requested: some
  296. // backgrounds are not printed to avoid not visible items
  297. LSET m_printLayers;
  298. wxDC* m_printDC; // This can go away once the drawing sheet is moved to
  299. // Cairo-based printing.
  300. };
  301. }
  302. #endif /* RENDER_SETTINGS_H */