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.

158 lines
4.3 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Kicad Developers, see change_log.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. #include <preview_items/preview_utils.h>
  24. #include <gal/graphics_abstraction_layer.h>
  25. #include <base_units.h>
  26. #include <pcb_painter.h>
  27. #include <view/view.h>
  28. double KIGFX::PREVIEW::PreviewOverlayDeemphAlpha( bool aDeemph )
  29. {
  30. return aDeemph ? 0.5 : 1.0;
  31. }
  32. static wxString getDimensionUnit( EDA_UNITS_T aUnits )
  33. {
  34. switch( aUnits )
  35. {
  36. case INCHES:
  37. return _( "\"" );
  38. case MILLIMETRES:
  39. return _( "mm" );
  40. case DEGREES:
  41. return _( "deg" );
  42. case UNSCALED_UNITS:
  43. break;
  44. // no default: handle all cases
  45. }
  46. return wxEmptyString;
  47. }
  48. static wxString formatPreviewDimension( double aVal, EDA_UNITS_T aUnits )
  49. {
  50. int precision = 4;
  51. // show a sane precision for the preview, which doesn't need to
  52. // be accurate down to the nanometre
  53. switch( aUnits )
  54. {
  55. case MILLIMETRES:
  56. precision = 2; // 10um
  57. break;
  58. case INCHES:
  59. precision = 4; // 1mil
  60. break;
  61. case DEGREES:
  62. precision = 1; // 0.1deg (limit of formats anyway)
  63. break;
  64. case UNSCALED_UNITS:
  65. break;
  66. }
  67. const wxString fmtStr = wxString::Format( "%%.%df", precision );
  68. wxString str = wxString::Format( fmtStr, To_User_Unit( aUnits, aVal ) );
  69. const wxString symbol = getDimensionUnit( aUnits );
  70. if( symbol.size() )
  71. str << " " << symbol;
  72. return str;
  73. }
  74. wxString KIGFX::PREVIEW::DimensionLabel( const wxString& prefix,
  75. double aVal, EDA_UNITS_T aUnits )
  76. {
  77. wxString str;
  78. if( prefix.size() )
  79. str << prefix << ": ";
  80. str << formatPreviewDimension( aVal, aUnits );
  81. return str;
  82. }
  83. void KIGFX::PREVIEW::SetConstantGlyphHeight( KIGFX::GAL& aGal, double aHeight )
  84. {
  85. aHeight /= aGal.GetWorldScale();
  86. auto glyphSize = aGal.GetGlyphSize();
  87. glyphSize = glyphSize * ( aHeight / glyphSize.y );
  88. aGal.SetGlyphSize( glyphSize );
  89. }
  90. void KIGFX::PREVIEW::DrawTextNextToCursor( KIGFX::VIEW* aView,
  91. const VECTOR2D& aCursorPos, const VECTOR2D& aTextQuadrant,
  92. const std::vector<wxString>& aStrings )
  93. {
  94. auto gal = aView->GetGAL();
  95. auto glyphSize = gal->GetGlyphSize();
  96. auto rs = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( aView->GetPainter()->GetSettings() );
  97. const auto lineSpace = glyphSize.y * 0.2;
  98. auto linePitch = glyphSize.y + lineSpace;
  99. // radius string goes on the right of the cursor centre line
  100. // with a small horizontal offset (enough to keep clear of a
  101. // system cursor if present)
  102. auto textPos = aCursorPos;
  103. // if the text goes above the cursor, shift it up
  104. if( aTextQuadrant.y > 0 )
  105. {
  106. textPos.y -= linePitch * ( aStrings.size() + 1 );
  107. }
  108. if( aTextQuadrant.x < 0 )
  109. {
  110. gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_LEFT );
  111. textPos.x += 15.0 / gal->GetWorldScale();
  112. }
  113. else
  114. {
  115. gal->SetHorizontalJustify( GR_TEXT_HJUSTIFY_RIGHT );
  116. textPos.x -= 15.0 / gal->GetWorldScale();
  117. }
  118. gal->SetStrokeColor( rs->GetLayerColor( LAYER_AUX_ITEMS ).WithAlpha(
  119. PreviewOverlayDeemphAlpha( true ) ) );
  120. gal->SetIsFill( false );
  121. // write strings top-to-bottom
  122. for( const auto& str : aStrings )
  123. {
  124. textPos.y += linePitch;
  125. gal->BitmapText( str, textPos, 0.0 );
  126. }
  127. }