class_base_screen.h 15.8 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
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.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
 */
25

26 27 28
/**
 * @file class_base_screen.h
 * @brief BASE_SCREEN class implementation.
29 30 31 32 33 34
 */

#ifndef  __CLASS_BASE_SCREEN_H__
#define  __CLASS_BASE_SCREEN_H__

#include "base_struct.h"
charras's avatar
charras committed
35
#include "class_undoredo_container.h"
36
#include "block_commande.h"
37
#include "common.h"
38 39 40 41 42 43 44 45 46 47


// Forward declarations:
class Ki_PageDescr;


/* Simple class for handling grid arrays. */
class GRID_TYPE
{
public:
48
    int         m_Id;
49
    wxRealPoint m_Size;
50 51 52 53 54

    GRID_TYPE& operator=( const GRID_TYPE& item )
    {
        if( this != &item )
        {
55
            m_Id   = item.m_Id;
56 57 58 59 60 61
            m_Size = item.m_Size;
        }

        return *this;
    }

62

63 64
    const bool operator==( const GRID_TYPE& item ) const
    {
65
        return m_Size == item.m_Size && m_Id == item.m_Id;
66
    }
67 68 69
};


70
typedef std::vector< GRID_TYPE > GRIDS;
71 72


73 74 75 76
/**
 * Class BASE_SCREEN
 * handle how to draw a screen (a board, a schematic ...)
 */
77
class BASE_SCREEN : public EDA_ITEM
78
{
79
    EDA_ITEMS m_items;          ///< The drawing items associated with this screen.
80
    GRIDS     m_grids;          ///< List of valid grid sizes.
81
    EDA_ITEM* m_drawList;       ///< Object list for the screen.
82
    wxString  m_fileName;       ///< File used to load the screen.
83
    char      m_FlagRefreshReq; ///< Indicates that the screen should be redrawn.
84 85
    bool      m_FlagModified;   ///< Indicates current drawing has been modified.
    bool      m_FlagSave;       ///< Indicates automatic file save.
86 87
    EDA_ITEM* m_CurrentItem;    ///< Currently selected object
    GRID_TYPE m_Grid;           ///< Current grid selection.
88
    wxPoint   m_scrollCenter;   ///< Current scroll center point in logical units.
89
    wxPoint   m_MousePosition;  ///< Mouse cursor coordinate in logical units.
90 91 92 93 94 95 96

    /**
     * The cross hair position in logical (drawing) units.  The cross hair is not the cursor
     * position.  It is an addition indicator typically drawn on grid to indicate to the
     * user where the current action will be performed.
     */
    wxPoint m_crossHairPosition;
97

98
public:
99
    wxPoint m_DrawOrg;          /* offsets for drawing the circuit on the screen */
100

101
    wxPoint m_O_Curseur;        /* Relative Screen cursor coordinate (on grid)
102 103
                                 * in user units. (coordinates from last reset position)*/

104 105 106
    // Scrollbars management:
    int     m_ScrollPixelsPerUnitX; /* Pixels per scroll unit in the horizontal direction. */
    int     m_ScrollPixelsPerUnitY; /* Pixels per scroll unit in the vertical direction. */
107 108 109 110 111 112 113 114 115
    wxSize  m_ScrollbarNumber;      /* Current virtual draw area size in scroll units.
                                     * m_ScrollbarNumber * m_ScrollPixelsPerUnit =
                                     * virtual draw area size in pixels */
    wxPoint m_ScrollbarPos;     /* Current scroll bar position in scroll units. */

    wxPoint m_StartVisu;        /* Coordinates in drawing units of the current
                                 * view position (upper left corner of device)
                                 */

116
    bool   m_Center;             /* Center on screen.  If true (0.0) is centered
117 118
                                  * on screen coordinates can be < 0 and
                                  * > 0 except for schematics.
119
                                  * false: when coordinates can only be >= 0
120
                                  * Schematic */
121
    bool m_FirstRedraw;
122

charras's avatar
charras committed
123
    // Undo/redo list of commands
124 125 126
    UNDO_REDO_CONTAINER m_UndoList;          /* Objects list for the undo command (old data) */
    UNDO_REDO_CONTAINER m_RedoList;          /* Objects list for the redo command (old data) */
    unsigned            m_UndoRedoCountMax;  // undo/Redo command Max depth
127 128

    /* block control */
129
    BLOCK_SELECTOR      m_BlockLocate;       /* Block description for block commands */
130 131

    /* Page description */
132
    Ki_PageDescr*       m_CurrentSheetDesc;
133 134 135 136 137 138 139 140 141 142 143
    int             m_ScreenNumber;
    int             m_NumberOfScreen;

    wxString        m_Title;
    wxString        m_Date;
    wxString        m_Revision;
    wxString        m_Company;
    wxString        m_Commentaire1;
    wxString        m_Commentaire2;
    wxString        m_Commentaire3;
    wxString        m_Commentaire4;
144

145
    /* Grid and zoom values. */
146
    wxPoint	m_GridOrigin;
147

148 149
    wxArrayDouble m_ZoomList;       /* Array of standard zoom (i.e. scale) coefficients. */
    double     m_Zoom;              /* Current zoom coefficient. */
150
    bool       m_IsPrinting;
151 152

public:
153
    BASE_SCREEN( KICAD_T aType = SCREEN_T );
154 155 156
    ~BASE_SCREEN();

    /**
157
     * Function SetCurItem
158
     * sets the currently selected object, m_CurrentItem.
159
     * @param aItem Any object derived from EDA_ITEM
160
     */
161
    void SetCurItem( EDA_ITEM* aItem ) { m_CurrentItem = aItem; }
162

163 164 165 166 167 168
    EDA_ITEM* GetCurItem() const { return m_CurrentItem; }

    /**
     * Function GetDrawItems().
     *
     * @return - A pointer to the first item in the linked list of draw items.
169
     */
170 171 172
    virtual EDA_ITEM* GetDrawItems() const { return m_drawList; }

    virtual void SetDrawItems( EDA_ITEM* aItem ) { m_drawList = aItem; }
173

174
    void InitDatas();
175

176
    void SetFileName( const wxString& aFileName ) { m_fileName = aFileName; }
177

178
    wxString GetFileName() const { return m_fileName; }
179

180 181 182 183 184 185 186 187 188 189 190 191
    void SetPageSize( wxSize& aPageSize );
    wxSize ReturnPageSize( void );

    /**
     * Function GetInternalUnits
     * @return the screen units scalar.
     *
     * Default implementation returns scalar used for schematic screen.  The
     * internal units used by the schematic screen is 1 mil (0.001").  Override
     * this in derived classes that require internal units other than 1 mil.
     */
    virtual int GetInternalUnits( void );
192

193
    /**
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
     * Function GetCrossHairPosition
     * return the current cross hair position in logical (drawing) coordinates.
     * @param aInvertY Inverts the Y axis position.
     * @return The cross hair position in drawing coordinates.
     */
    wxPoint GetCrossHairPosition( bool aInvertY = false ) const
    {
        if( aInvertY )
            return wxPoint( m_crossHairPosition.x, -m_crossHairPosition.y );

        return wxPoint( m_crossHairPosition.x, m_crossHairPosition.y );
    }

    /**
     * Function SetCrossHairPosition
     * sets the screen cross hair position to \a aPosition in logical (drawing) units.
     * @param aPosition The new cross hair position.
     * @param aSnapToGrid Sets the cross hair position to the nearest grid position to
     *                    \a aPosition.
213 214
     *
     */
215
    void SetCrossHairPosition( const wxPoint& aPosition, bool aSnapToGrid = true );
216

217
    /* general Undo/Redo command control */
218

219 220
    /**
     * Function ClearUndoORRedoList (virtual).
221
     * this function must remove the aItemCount old commands from aList
222
     * and delete commands, pickers and picked items if needed
223 224 225
     * Because picked items must be deleted only if they are not in use, this
     * is a virtual pure function that must be created for SCH_SCREEN and
     * PCB_SCREEN
226
     * @param aList = the UNDO_REDO_CONTAINER of commands
227 228
     * @param aItemCount = number of old commands to delete. -1 to remove all
     *                     old commands this will empty the list of commands.
229 230
     *  Commands are deleted from the older to the last.
     */
231
    virtual void ClearUndoORRedoList( UNDO_REDO_CONTAINER& aList, int aItemCount = -1 ) = 0;
232

233 234
    /**
     * Function ClearUndoRedoList
235
     * clear undo and redo list, using ClearUndoORRedoList()
236 237
     * picked items are deleted by ClearUndoORRedoList() according to their
     * status
238
     */
239
    virtual void ClearUndoRedoList();
240

241 242
    /**
     * Function PushCommandToUndoList
243
     * add a command to undo in undo list
244 245
     * delete the very old commands when the max count of undo commands is
     * reached
246 247
     * ( using ClearUndoORRedoList)
     */
248
    virtual void PushCommandToUndoList( PICKED_ITEMS_LIST* aItem );
249

250 251
    /**
     * Function PushCommandToRedoList
252
     * add a command to redo in redo list
253 254
     * delete the very old commands when the max count of redo commands is
     * reached
255 256
     * ( using ClearUndoORRedoList)
     */
257
    virtual void PushCommandToRedoList( PICKED_ITEMS_LIST* aItem );
258 259 260 261 262

    /** PopCommandFromUndoList
     * return the last command to undo and remove it from list
     * nothing is deleted.
     */
charras's avatar
charras committed
263
    virtual PICKED_ITEMS_LIST* PopCommandFromUndoList();
264 265 266 267 268

    /** PopCommandFromRedoList
     * return the last command to undo and remove it from list
     * nothing is deleted.
     */
charras's avatar
charras committed
269
    virtual PICKED_ITEMS_LIST* PopCommandFromRedoList();
270 271

    int GetUndoCommandCount()
charras's avatar
charras committed
272 273 274
    {
        return m_UndoList.m_CommandsList.size();
    }
275 276 277


    int GetRedoCommandCount()
charras's avatar
charras committed
278 279 280
    {
        return m_RedoList.m_CommandsList.size();
    }
281

282

283 284 285 286 287 288
    void SetModify() { m_FlagModified = true; }
    void ClrModify() { m_FlagModified = false;; }
    void SetSave() { m_FlagSave = true; }
    void ClrSave() { m_FlagSave = false; }
    int IsModify() { return m_FlagModified;  }
    int IsSave() { return m_FlagSave;  }
289 290


291
    //----<zoom stuff>---------------------------------------------------------
292

293 294
    /**
     * Function GetScalingFactor
295
     * @return the the current scale used to draw items on screen
296
     * draw coordinates are user coordinates * GetScalingFactor()
297
     */
298
    double GetScalingFactor() const;
299

300 301
    /**
     * Function SetScalingFactor
302
     * @param aScale = the the current scale used to draw items on screen
303
     * draw coordinates are user coordinates * GetScalingFactor()
304
     */
305
    void SetScalingFactor( double aScale );
306

307 308
    /**
     * Function GetZoom
309 310 311 312
     * @return the current zoom factor
     * Note: the zoom factor is NOT the scaling factor
     *       the scaling factor is m_ZoomScalar * GetZoom()
     */
313
    double GetZoom() const;
314 315 316 317

    /**
     * Function SetZoom
     * adjusts the current zoom factor
318
     * @param coeff - Zoom coefficient.
319
     */
320
    bool SetZoom( double coeff );
321 322 323 324

    /**
     * Function SetZoomList
     * sets the list of zoom factors.
325
     * @param aZoomList An array of zoom factors in ascending order, zero terminated
326
     */
327
    void SetZoomList( const wxArrayDouble& aZoomList );
328

329 330 331 332
    bool SetNextZoom();
    bool SetPreviousZoom();
    bool SetFirstZoom();
    bool SetLastZoom();
333

334
    //----<grid stuff>----------------------------------------------------------
335 336 337 338 339 340

    /**
     * Return the command ID of the currently selected grid.
     *
     * @return int - Currently selected grid command ID.
     */
341
    int GetGridId();
342 343 344 345 346 347 348 349 350 351 352 353 354

    /**
     * Return the grid size of the currently selected grid.
     *
     * @return wxRealPoint - The currently selected grid size.
     */
    wxRealPoint GetGridSize();

    /**
     * Return the grid object of the currently selected grid.
     *
     * @return GRID_TYPE - The currently selected grid.
     */
355
    GRID_TYPE GetGrid();
356

357
    const wxPoint& GetGridOrigin();
358 359 360 361 362 363 364 365 366 367 368
    void SetGrid( const wxRealPoint& size );

    /**
     * Function SetGrid
     * sets the grid size from command ID.
     */
    void SetGrid( int id );
    void SetGridList( GRIDS& sizelist );
    void AddGrid( const GRID_TYPE& grid );
    void AddGrid( const wxRealPoint& size, int id );
    void AddGrid( const wxRealPoint& size, EDA_UNITS_T aUnit, int id );
369

370 371 372 373 374 375
    /**
     * Function GetGridCount().
     * Return the size of the grid list.
     *
     * @returns - The size of the grid list.
     */
376
    size_t GetGridCount() const { return m_grids.size(); }
377 378 379 380 381 382 383 384

    /**
     * Function GetGrid()
     * Returns the grid object at \a aIndex.
     *
     * @param aIndex - The grid list index.
     * @return - The grid object at \a aIndex or the current grid if the grid list is empty.
     */
385
    GRID_TYPE& GetGrid( size_t aIndex );
386 387 388 389 390 391 392

    /**
     * Function GetGrids().
     * Copy the grid list to \a aList.
     *
     * @param aList - List to copy to.
     */
393
    void GetGrids( GRIDS& aList );
394

395 396
    void SetMousePosition( const wxPoint& aPosition ) { m_MousePosition = aPosition; }

397 398
    /**
     * Function RefPos
399 400 401
     * Return the reference position, coming from either the mouse position
     * or the cursor position.
     *
402
     * @param useMouse If true, return mouse position, else cursor's.
403
     *
404
     * @return wxPoint - The reference point, either the mouse position or
405
     *                   the cursor position.
406 407 408
     */
    wxPoint RefPos( bool useMouse )
    {
409
        return useMouse ? m_MousePosition : m_crossHairPosition;
410 411
    }

412 413 414 415 416 417 418 419 420 421
    /**
     * Function GetCursorPosition
     * returns the current cursor position in logical (drawing) units.
     * @param aOnGrid Returns the nearest grid position at the current cursor position.
     * @param aGridSize Custom grid size instead of the current grid size.  Only valid
     *        if \a aOnGrid is true.
     * @return The current cursor position.
     */
    wxPoint GetCursorPosition( bool aOnGrid, wxRealPoint* aGridSize = NULL );

422 423 424 425 426 427 428
    /**
     * Function GetCursorScreenPosition
     * returns the cross hair position in device (display) units.b
     * @return The current cross hair position.
     */
    wxPoint GetCrossHairScreenPosition() const;

429 430 431 432 433 434 435 436 437
    /**
     * Function GetNearestGridPosition
     * returns the nearest \a aGridSize location to \a aPosition.
     * @param aPosition The position to check.
     * @param aGridSize The grid size to locate to if provided.  If NULL then the current
     *                  grid size is used.
     * @return The nearst grid position.
     */
    wxPoint GetNearestGridPosition( const wxPoint& aPosition, wxRealPoint* aGridSize = NULL );
438 439 440 441 442 443 444 445 446 447 448

    /**
     * Function GetClass
     * returns the class name.
     * @return wxString
     */
    virtual wxString GetClass() const
    {
        return wxT( "BASE_SCREEN" );
    }

449 450 451 452 453
    /**
     * Helpers for accessing the draw item list.
     */
    EDA_ITEMS::iterator Begin() { return m_items.begin(); }
    EDA_ITEMS::iterator End() { return m_items.end(); }
454 455
    virtual void AddItem( EDA_ITEM* aItem );
    virtual void InsertItem(  EDA_ITEMS::iterator aIter, EDA_ITEM* aItem );
456

457 458 459 460 461 462 463 464
    /**
     * Function IsBlockActive
     * returns true if a block command is in progress.
     */
    inline bool IsBlockActive() const { return !m_BlockLocate.IsIdle(); }

    void ClearBlockCommand() { m_BlockLocate.Clear(); }

465 466 467 468 469 470
    wxPoint GetScrollCenterPosition() const { return m_scrollCenter; }
    void SetScrollCenterPosition( const wxPoint& aCenterPosition )
    {
        m_scrollCenter = aCenterPosition;
    }

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
#if defined(DEBUG)

    /**
     * Function Show
     * is used to output the object tree, currently for debugging only.
     * @param nestLevel An aid to prettier tree indenting, and is the level
     *          of nesting of this object within the overall tree.
     * @param os The ostream& to output to.
     */
    void Show( int nestLevel, std::ostream& os );

#endif
};


#endif  /* #ifndef __CLASS_BASE_SCREEN_H__ */