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.

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