7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-07 22:05:32 +00:00

Ruler: fix colours when not the same as the cursor strings

Implement a simple RAII GAL attribute save/restore
class that handles putting the GAL back the way it was found.

This makes draw methods easier to write, as they don't need
to worry if a called method will upset the attribute context
(as long as the caller saves the context, or the callee does).

Obviously not a good idea to use in tight loops, but when text
is involved, this is negligible!
This commit is contained in:
John Beard 2024-11-03 23:50:02 +08:00
parent 11ac6ea976
commit 3549b77530
3 changed files with 102 additions and 2 deletions
common/preview_items
include

View File

@ -121,6 +121,9 @@ void KIGFX::PREVIEW::DrawTextNextToCursor( KIGFX::VIEW* aView, const VECTOR2D& a
bool aDrawingDropShadows )
{
KIGFX::GAL* gal = aView->GetGAL();
GAL_SCOPED_ATTRS settings( *gal );
KIFONT::FONT* font = KIFONT::FONT::GetFont();
// constant text size on screen

View File

@ -286,6 +286,16 @@ public:
m_isFillEnabled = aIsFillEnabled;
}
/**
* Get the fill status.
*
* @return true if fill is enabled, false otherwise.
*/
inline bool GetIsFill() const
{
return m_isFillEnabled;
}
/**
* Enable/disable stroked outlines.
*
@ -296,6 +306,16 @@ public:
m_isStrokeEnabled = aIsStrokeEnabled;
}
/**
* Get the stroke status.
*
* @return true if stroke is enabled, false otherwise.
*/
inline bool GetIsStroke() const
{
return m_isStrokeEnabled;
}
/**
* Set the fill color.
*
@ -1147,6 +1167,81 @@ public:
};
/**
* Attribute save/restore for GAL attributes.
*/
class GAL_SCOPED_ATTRS
{
public:
enum FLAGS
{
STROKE_WIDTH = 1,
STROKE_COLOR = 2,
IS_STROKE = 4,
FILL_COLOR = 8,
IS_FILL = 16,
// It is not clear to me that GAL needs to save text attributes.
// Only BitmapText uses it, and maybe that should be passed in
// explicitly (like for Draw) - every caller of BitmapText sets
// the text attributes anyway.
// TEXT_ATTRS = 32,
STROKE = STROKE_WIDTH | STROKE_COLOR | IS_STROKE,
FILL = FILL_COLOR | IS_FILL,
///< Convenience flag for setting both stroke and fill
ALL = STROKE | FILL,
};
/**
* Instantiates a GAL_SCOPED_ATTRS object, saving the current attributes of the GAL.
*
* By default, all stroke and fill attributes are saved, which is usually what you want.
*/
GAL_SCOPED_ATTRS( KIGFX::GAL& aGal, int aFlags = FLAGS::ALL )
: m_gal( aGal ), m_flags( aFlags )
{
// Save what we need to restore later.
// These are all so cheap to copy, it's likely not worth if'ing
m_strokeWidth = aGal.GetLineWidth();
m_strokeColor = aGal.GetStrokeColor();
m_isStroke = aGal.GetIsStroke();
m_fillColor = aGal.GetFillColor();
m_isFill = aGal.GetIsFill();
}
~GAL_SCOPED_ATTRS()
{
// Restore the attributes that were saved
// based on the flags that were set.
if( m_flags & STROKE_WIDTH )
m_gal.SetLineWidth( m_strokeWidth );
if( m_flags & STROKE_COLOR )
m_gal.SetStrokeColor( m_strokeColor );
if( m_flags & IS_STROKE )
m_gal.SetIsStroke( m_isStroke );
if( m_flags & FILL_COLOR )
m_gal.SetFillColor( m_fillColor );
if( m_flags & IS_FILL )
m_gal.SetIsFill( m_isFill );
}
private:
GAL& m_gal;
int m_flags;
COLOR4D m_strokeColor;
double m_strokeWidth;
bool m_isStroke;
COLOR4D m_fillColor;
bool m_isFill;
};
}; // namespace KIGFX
#endif /* GRAPHICSABSTRACTIONLAYER_H_ */

View File

@ -72,6 +72,8 @@ COLOR4D GetShadowColor( const COLOR4D& aColor );
/**
* Draw strings next to the cursor.
*
* The GAL attribute context will be restored to its original state after this function is called.
*
* @param aGal the GAL to draw on.
* @param aCursorPos the position of the cursor to draw next to.
* @param aTextQuadrant a vector pointing to the quadrant to draw the text in.
@ -81,7 +83,7 @@ void DrawTextNextToCursor( KIGFX::VIEW* aView, const VECTOR2D& aCursorPos,
const VECTOR2D& aTextQuadrant, const wxArrayString& aStrings,
bool aDrawingDropShadows );
} // PREVIEW
} // KIGFX
} // namespace PREVIEW
} // namespace KIGFX
#endif // PREVIEW_PREVIEW_UTILS__H_