lib_field.h 8.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file lib_field.h
 */
28 29 30 31

#ifndef CLASS_LIBENTRY_FIELDS_H
#define CLASS_LIBENTRY_FIELDS_H

32
#include <eda_text.h>
33
#include <lib_draw_item.h>
34 35


36 37 38 39 40
/**
 * Class LIB_FIELD
 * is used in symbol libraries.  At least MANDATORY_FIELDS are always present
 * in a ram resident library symbol.  All constructors must ensure this because
 * the component property editor assumes it.
41 42 43 44 45 46 47 48 49
 * <p>
 * A field is a string linked to a component.  Unlike purely graphical text, fields can
 * be used in netlist generation and other tools (BOM).
 *
 *  The first 4 fields have a special meaning:
 *
 *  0 = REFERENCE
 *  1 = VALUE
 *  2 = FOOTPRINT (default Footprint)
50
 *  3 = DATASHEET (user doc link)
51 52 53 54
 *
 *  others = free fields
 * </p>
 *
55
 * @see enum NumFieldType
56
 */
57
class LIB_FIELD : public LIB_ITEM, public EDA_TEXT
58
{
59 60 61 62 63 64
    int      m_id;           ///< @see enum NumFieldType
    wxString m_name;         ///< Name (not the field text value itself, that is .m_Text)

    wxString m_savedText;    ///< Temporary storage for the string when edition.
    bool     m_rotate;       ///< Flag to indicate a rotation occurred while editing.
    bool     m_updateText;   ///< Flag to indicate text change occurred while editing.
65 66 67

    /**
     * Draw the field.
68 69 70 71
     * <p>
     * If \a aData not NULL, \a aData must point a wxString which is used instead of
     * the m_Text
     * </p>
72
     */
73
    void drawGraphic( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aOffset,
74
                      EDA_COLOR_T aColor, GR_DRAWMODE aDrawMode, void* aData,
75
                      const TRANSFORM& aTransform );
76 77 78 79 80 81 82 83

    /**
     * Calculate the new circle at \a aPosition when editing.
     *
     * @param aPosition - The position to edit the circle in drawing coordinates.
     */
    void calcEdit( const wxPoint& aPosition );

84 85
public:

86
    LIB_FIELD( int idfield = 2 );
87

88
    LIB_FIELD( LIB_COMPONENT * aParent, int idfield = 2 );
89 90 91

    // Do not create a copy constructor.  The one generated by the compiler is adequate.

92
    ~LIB_FIELD();
93

94
    wxString GetClass() const
95
    {
96
        return wxT( "LIB_FIELD" );
97 98
    }

99 100 101 102
    /**
     * Object constructor initialization helper.
     */
    void Init( int idfield );
103

104 105 106 107 108
    /**
     * Returns the field name.
     *
     * The first four field IDs are reserved and therefore always return their respective
     * names.  The user definable fields will return FieldN where N is the ID of the field
109
     * when the m_name member is empty.
110
     *
111 112 113
     * @param aTranslate True to return translated field name (default).  False to return
     *                   the english name (useful when the name is used as keyword in
     *                   netlists ...)
114 115
     * @return Name of the field.
     */
116
    wxString GetName( bool aTranslate = true ) const;
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    /**
     * Function SetName
     *
     * Sets a user definable field name to \a aName.
     *
     * Reserved fields such as value and reference are not renamed.  If the field name is
     * changed, the field modified flag is set.  If the field is the child of a component,
     * the parent component's modified flag is also set.
     *
     * @param aName - User defined field name.
     */
    void SetName( const wxString& aName );

    int GetId() { return m_id; }

    void SetId( int aId ) { m_id = aId; }
134

135
    int GetPenSize( ) const;
136

137
    bool Save( OUTPUTFORMATTER& aFormatter );
138

139
    bool Load( LINE_READER& aLineReader, wxString& errorMsg );
140

141 142 143
    /**
     * Copy parameters of this field to another field. Pointers are not copied.
     *
144
     * @param aTarget Target field to copy values to.
145
     */
146
    void Copy( LIB_FIELD* aTarget ) const;
147

148
    void SetFields( const std::vector <LIB_FIELD> aFields );
149

150 151 152 153 154 155 156 157 158
    /**
     * Function IsVoid
     * @return true if the field value is void (no text in this field)
     */
    bool IsVoid()
    {
        return m_Text.IsEmpty();
    }

159 160
    /**
     * Function IsVisible
161 162 163 164 165 166 167
     * @return true is this field is visible, false if flagged invisible
     */
    bool IsVisible()
    {
        return (m_Attributs & TEXT_NO_VISIBLE) == 0 ? true : false;
    }

168
    EDA_RECT GetBoundingBox() const;
169

170
    void GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList );
171

172
    bool HitTest( const wxPoint& aPosition );
173

174
    bool HitTest( wxPoint aPosition, int aThreshold, const TRANSFORM& aTransform );
charras's avatar
charras committed
175

176
    void operator=( const LIB_FIELD& field )
177
    {
178
        m_id = field.m_id;
179
        m_Text = field.m_Text;
180
        m_name = field.m_name;
181 182
        m_Pos = field.m_Pos;
        m_Size = field.m_Size;
183
        m_Thickness = field.m_Thickness;
184 185 186 187
        m_Orient = field.m_Orient;
        m_Mirror = field.m_Mirror;
        m_Attributs = field.m_Attributs;
        m_Italic = field.m_Italic;
charras's avatar
charras committed
188
        m_Bold = field.m_Bold;
189 190
        m_HJustify = field.m_HJustify;
        m_VJustify = field.m_VJustify;
191
        m_Parent = field.m_Parent;
192
    }
193

194 195 196 197 198 199 200
    /**
     * Return the text of a field.
     *
     * If the field is the reference field, the unit number is used to
     * create a pseudo reference text.  If the base reference field is U,
     * the string U?A will be returned for unit = 1.
     *
201 202
     * @todo This should be handled by the field object.
     *
203
     * @param unit - The package unit number.  Only effects reference field.
204
     * @return Field text.
205 206
     */
    wxString GetFullText( int unit = 1 );
207

208
    EDA_COLOR_T GetDefaultColor();
209

210
    void BeginEdit( STATUS_FLAGS aEditMode, const wxPoint aStartPoint = wxPoint( 0, 0 ) );
211 212 213 214 215 216 217

    bool ContinueEdit( const wxPoint aNextPoint );

    void EndEdit( const wxPoint& aPosition, bool aAbort = false );

    void Rotate();

218 219 220
    /**
     * Sets the field text to \a aText.
     *
221
     * This method does more than just set the set the field text.  There are special
222 223 224 225 226 227 228 229 230
     * cases when changing the text string alone is not enough.  If the field is the
     * value field, the parent component's name is changed as well.  If the field is
     * being moved, the name change must be delayed until the next redraw to prevent
     * drawing artifacts.
     *
     * @param aText - New text value.
     */
    void SetText( const wxString& aText );

231
    void SetOffset( const wxPoint& aOffset );
232

233
    bool Inside( EDA_RECT& aRect ) const;
234

235
    void Move( const wxPoint& aPosition );
236

237
    wxPoint GetPosition() const { return m_Pos; }
238

239
    void MirrorHorizontal( const wxPoint& aCenter );
240

241
    void MirrorVertical( const wxPoint& aCenter );
242

243
    void Rotate( const wxPoint& aCenter, bool aRotateCCW = true );
244

245 246
    void Plot( PLOTTER* aPlotter, const wxPoint& aOffset, bool aFill,
               const TRANSFORM& aTransform );
247

248
    int GetWidth() const { return m_Thickness; }
249

250
    void SetWidth( int aWidth ) { m_Thickness = aWidth; }
251

252
    wxString GetSelectMenuText() const;
253

254
    BITMAP_DEF GetMenuImage() const { return  move_field_xpm; }
255

256
    EDA_ITEM* Clone() const;
257

258
private:
259 260

    /**
261
     * @copydoc LIB_ITEM::compare()
262
     *
263
     * The field specific sort order is as follows:
264 265 266 267 268 269 270 271
     *
     *      - Field ID, REFERENCE, VALUE, etc.
     *      - Field string, case insensitive compare.
     *      - Field horizontal (X) position.
     *      - Field vertical (Y) position.
     *      - Field width.
     *      - Field height.
     */
272
    int compare( const LIB_ITEM& aOther ) const;
273 274
};

275
typedef std::vector< LIB_FIELD > LIB_FIELDS;
276

277
#endif  //  CLASS_LIBENTRY_FIELDS_H