From b897c5f290cc148049658dfb28b00dadab0cea20 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 13 May 2016 17:31:54 +0200 Subject: [PATCH] BOARD_ITEM_CONTAINER class. --- include/wxPcbStruct.h | 4 ++ pcbnew/board_item_container.h | 73 +++++++++++++++++++++++++++++++++++ pcbnew/class_board.cpp | 21 ++++------ pcbnew/class_board.h | 40 ++++--------------- pcbnew/class_module.cpp | 21 +++++----- pcbnew/class_module.h | 35 +++-------------- pcbnew/module_editor_frame.h | 3 ++ pcbnew/moduleframe.cpp | 6 +++ pcbnew/pcb_base_edit_frame.h | 8 ++++ pcbnew/pcbframe.cpp | 6 +++ 10 files changed, 132 insertions(+), 85 deletions(-) create mode 100644 pcbnew/board_item_container.h diff --git a/include/wxPcbStruct.h b/include/wxPcbStruct.h index 887e7f3075..270643d3e1 100644 --- a/include/wxPcbStruct.h +++ b/include/wxPcbStruct.h @@ -39,6 +39,7 @@ /* Forward declarations of classes. */ class PCB_SCREEN; class BOARD; +class BOARD_ITEM_CONTAINER; class TEXTE_PCB; class MODULE; class TRACK; @@ -916,6 +917,9 @@ public: ///> @copydoc PCB_BASE_FRAME::SetBoard() void SetBoard( BOARD* aBoard ); + ///> @copydoc PCB_BASE_EDIT_FRAME::GetModel() + BOARD_ITEM_CONTAINER* GetModel() const override; + ///> @copydoc PCB_BASE_FRAME::SetPageSettings() void SetPageSettings( const PAGE_INFO& aPageSettings ); // overload diff --git a/pcbnew/board_item_container.h b/pcbnew/board_item_container.h new file mode 100644 index 0000000000..9e100bd1fa --- /dev/null +++ b/pcbnew/board_item_container.h @@ -0,0 +1,73 @@ +/* + * This program source code file is part of KICAD, a free EDA CAD application. + * + * Copyright (C) 2016 CERN + * @author Maciej Suminski + * + * 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 + */ + +#ifndef BOARD_ITEM_CONTAINER_H +#define BOARD_ITEM_CONTAINER_H + +#include + +enum ADD_MODE { ADD_INSERT, ADD_APPEND }; + +/** + * @brief Abstract interface for BOARD_ITEMs capable of storing other items inside. + * @see MODULE + * @see BOARD + */ +class BOARD_ITEM_CONTAINER : public BOARD_ITEM +{ +public: + BOARD_ITEM_CONTAINER( BOARD_ITEM* aParent, KICAD_T aType ) + : BOARD_ITEM( aParent, aType ) + { + } + + virtual ~BOARD_ITEM_CONTAINER() + { + } + + /** + * @brief Adds an item to the container. + * @param aItem is an item to be added. + * @param aMode decides whether the item is added in the beginning or at the end of the list. + */ + virtual void Add( BOARD_ITEM* aItem, ADD_MODE aMode = ADD_INSERT ) = 0; + + /** + * @brief Removes an item from the container. + * @param aItem is an item to be removed. + */ + virtual void Remove( BOARD_ITEM* aItem ) = 0; + + /** + * @brief Removes an item from the containter and deletes it. + * @param aItem is an item to be deleted. + */ + virtual void Delete( BOARD_ITEM* aItem ) + { + Remove( aItem ); + delete aItem; + } +}; + +#endif /* BOARD_ITEM_CONTAINER_H */ diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp index 562ae0e2dc..aac91c9ca9 100644 --- a/pcbnew/class_board.cpp +++ b/pcbnew/class_board.cpp @@ -67,7 +67,7 @@ wxPoint BOARD_ITEM::ZeroOffset( 0, 0 ); BOARD::BOARD() : - BOARD_ITEM( (BOARD_ITEM*) NULL, PCB_T ), + BOARD_ITEM_CONTAINER( (BOARD_ITEM*) NULL, PCB_T ), m_NetInfo( this ), m_paper( PAGE_INFO::A4 ) { @@ -858,7 +858,7 @@ bool BOARD::IsModuleLayerVisible( LAYER_ID layer ) } -void BOARD::Add( BOARD_ITEM* aBoardItem, int aControl ) +void BOARD::Add( BOARD_ITEM* aBoardItem, ADD_MODE aMode ) { if( aBoardItem == NULL ) { @@ -883,7 +883,7 @@ void BOARD::Add( BOARD_ITEM* aBoardItem, int aControl ) case PCB_TRACE_T: case PCB_VIA_T: - if( aControl & ADD_APPEND ) + if( aMode == ADD_APPEND ) { m_Track.PushBack( (TRACK*) aBoardItem ); } @@ -897,7 +897,7 @@ void BOARD::Add( BOARD_ITEM* aBoardItem, int aControl ) break; case PCB_ZONE_T: - if( aControl & ADD_APPEND ) + if( aMode == ADD_APPEND ) m_Zone.PushBack( (SEGZONE*) aBoardItem ); else m_Zone.PushFront( (SEGZONE*) aBoardItem ); @@ -905,7 +905,7 @@ void BOARD::Add( BOARD_ITEM* aBoardItem, int aControl ) break; case PCB_MODULE_T: - if( aControl & ADD_APPEND ) + if( aMode == ADD_APPEND ) m_Modules.PushBack( (MODULE*) aBoardItem ); else m_Modules.PushFront( (MODULE*) aBoardItem ); @@ -915,14 +915,11 @@ void BOARD::Add( BOARD_ITEM* aBoardItem, int aControl ) m_Status_Pcb = 0; break; - case PCB_MODULE_EDGE_T: - assert( false ); // TODO Orson: I am just checking if it is supposed to be here - case PCB_DIMENSION_T: case PCB_LINE_T: case PCB_TEXT_T: case PCB_TARGET_T: - if( aControl & ADD_APPEND ) + if( aMode == ADD_APPEND ) m_Drawings.PushBack( aBoardItem ); else m_Drawings.PushFront( aBoardItem ); @@ -946,7 +943,7 @@ void BOARD::Add( BOARD_ITEM* aBoardItem, int aControl ) } -BOARD_ITEM* BOARD::Remove( BOARD_ITEM* aBoardItem ) +void BOARD::Remove( BOARD_ITEM* aBoardItem ) { // find these calls and fix them! Don't send me no stinking' NULL. wxASSERT( aBoardItem ); @@ -959,6 +956,7 @@ BOARD_ITEM* BOARD::Remove( BOARD_ITEM* aBoardItem ) m_NetInfo.RemoveNet( item ); break; } + case PCB_MARKER_T: // find the item in the vector, then remove it @@ -1001,7 +999,6 @@ BOARD_ITEM* BOARD::Remove( BOARD_ITEM* aBoardItem ) case PCB_DIMENSION_T: case PCB_LINE_T: case PCB_TEXT_T: - case PCB_MODULE_EDGE_T: case PCB_TARGET_T: m_Drawings.Remove( aBoardItem ); break; @@ -1012,8 +1009,6 @@ BOARD_ITEM* BOARD::Remove( BOARD_ITEM* aBoardItem ) } m_ratsnest->Remove( aBoardItem ); - - return aBoardItem; } diff --git a/pcbnew/class_board.h b/pcbnew/class_board.h index e2a98bd31b..dd4476c267 100644 --- a/pcbnew/class_board.h +++ b/pcbnew/class_board.h @@ -42,6 +42,7 @@ #include #include #include +#include class PCB_BASE_FRAME; @@ -162,7 +163,7 @@ DECL_VEC_FOR_SWIG(TRACKS, TRACK*) * Class BOARD * holds information pertinent to a Pcbnew printed circuit board. */ -class BOARD : public BOARD_ITEM +class BOARD : public BOARD_ITEM_CONTAINER { friend class PCB_EDIT_FRAME; @@ -215,7 +216,7 @@ private: // The default copy constructor & operator= are inadequate, // either write one or do not use it at all BOARD( const BOARD& aOther ) : - BOARD_ITEM( aOther ), m_NetInfo( this ) + BOARD_ITEM_CONTAINER( aOther ), m_NetInfo( this ) { assert( false ); } @@ -270,38 +271,11 @@ public: void SetFileFormatVersionAtLoad( int aVersion ) { m_fileFormatVersionAtLoad = aVersion; } int GetFileFormatVersionAtLoad() const { return m_fileFormatVersionAtLoad; } - /** - * Function Add - * adds the given item to this BOARD and takes ownership of its memory. - * @param aBoardItem The item to add to this board. - * @param aControl An int which can vary how the item is added. - */ - void Add( BOARD_ITEM* aBoardItem, int aControl = 0 ); + ///> @copydoc BOARD_ITEM_CONTAINER::Add() + void Add( BOARD_ITEM* aItem, ADD_MODE aMode = ADD_INSERT ) override; -#define ADD_APPEND 1 ///< aControl flag for Add( aControl ), appends not inserts - - /** - * Function Delete - * removes the given single item from this BOARD and deletes its memory. - * @param aBoardItem The item to remove from this board and delete - */ - void Delete( BOARD_ITEM* aBoardItem ) - { - // developers should run DEBUG versions and fix such calls with NULL - wxASSERT( aBoardItem ); - - if( aBoardItem ) - delete Remove( aBoardItem ); - } - - - /** - * Function Remove - * removes \a aBoardItem from this BOARD and returns it to caller without deleting it. - * @param aBoardItem The item to remove from this board. - * @return BOARD_ITEM* \a aBoardItem which was passed in. - */ - BOARD_ITEM* Remove( BOARD_ITEM* aBoardItem ); + ///> @copydoc BOARD_ITEM_CONTAINER::Remove() + void Remove( BOARD_ITEM* aBoardItem ) override; /** * Function DuplicateAndAddItem diff --git a/pcbnew/class_module.cpp b/pcbnew/class_module.cpp index 05367d656d..a539add30f 100644 --- a/pcbnew/class_module.cpp +++ b/pcbnew/class_module.cpp @@ -50,7 +50,7 @@ MODULE::MODULE( BOARD* parent ) : - BOARD_ITEM( (BOARD_ITEM*) parent, PCB_MODULE_T ), + BOARD_ITEM_CONTAINER( (BOARD_ITEM*) parent, PCB_MODULE_T ), m_initial_comments( 0 ) { m_Attributs = MOD_DEFAULT; @@ -79,7 +79,7 @@ MODULE::MODULE( BOARD* parent ) : MODULE::MODULE( const MODULE& aModule ) : - BOARD_ITEM( aModule ) + BOARD_ITEM_CONTAINER( aModule ) { m_Pos = aModule.m_Pos; m_fpid = aModule.m_fpid; @@ -256,7 +256,7 @@ void MODULE::DrawAncre( EDA_DRAW_PANEL* panel, wxDC* DC, const wxPoint& offset, } -void MODULE::Add( BOARD_ITEM* aBoardItem, bool doAppend ) +void MODULE::Add( BOARD_ITEM* aBoardItem, ADD_MODE aMode ) { switch( aBoardItem->Type() ) { @@ -267,14 +267,14 @@ void MODULE::Add( BOARD_ITEM* aBoardItem, bool doAppend ) // no break case PCB_MODULE_EDGE_T: - if( doAppend ) + if( aMode == ADD_APPEND ) m_Drawings.PushBack( aBoardItem ); else m_Drawings.PushFront( aBoardItem ); break; case PCB_PAD_T: - if( doAppend ) + if( aMode == ADD_APPEND ) m_Pads.PushBack( static_cast( aBoardItem ) ); else m_Pads.PushFront( static_cast( aBoardItem ) ); @@ -292,10 +292,11 @@ void MODULE::Add( BOARD_ITEM* aBoardItem, bool doAppend ) } aBoardItem->SetParent( this ); + SetLastEditTime(); } -BOARD_ITEM* MODULE::Remove( BOARD_ITEM* aBoardItem ) +void MODULE::Remove( BOARD_ITEM* aBoardItem ) { switch( aBoardItem->Type() ) { @@ -306,10 +307,12 @@ BOARD_ITEM* MODULE::Remove( BOARD_ITEM* aBoardItem ) // no break case PCB_MODULE_EDGE_T: - return m_Drawings.Remove( aBoardItem ); + m_Drawings.Remove( aBoardItem ); + break; case PCB_PAD_T: - return m_Pads.Remove( static_cast( aBoardItem ) ); + m_Pads.Remove( static_cast( aBoardItem ) ); + break; default: { @@ -319,8 +322,6 @@ BOARD_ITEM* MODULE::Remove( BOARD_ITEM* aBoardItem ) wxFAIL_MSG( msg ); } } - - return NULL; } diff --git a/pcbnew/class_module.h b/pcbnew/class_module.h index 2a0888de97..5e21fe645d 100644 --- a/pcbnew/class_module.h +++ b/pcbnew/class_module.h @@ -36,6 +36,7 @@ #include #include // ALL_LAYERS definition. #include +#include #include #include @@ -75,7 +76,7 @@ enum MODULE_ATTR_T }; -class MODULE : public BOARD_ITEM +class MODULE : public BOARD_ITEM_CONTAINER { public: MODULE( BOARD* parent ); @@ -94,35 +95,11 @@ public: MODULE* Next() const { return static_cast( Pnext ); } MODULE* Back() const { return static_cast( Pback ); } - /** - * Function Add - * adds the given item to this MODULE and takes ownership of its memory. - * @param aBoardItem The item to add to this board. - * @param doAppend If true, then append, else insert. - */ - void Add( BOARD_ITEM* aBoardItem, bool doAppend = true ); + ///> @copydoc BOARD_ITEM_CONTAINER::Add() + void Add( BOARD_ITEM* aBoardItem, ADD_MODE aMode = ADD_INSERT ) override; - /** - * Function Delete - * removes the given single item from this MODULE and deletes its memory. - * @param aBoardItem The item to remove from this module and delete - */ - void Delete( BOARD_ITEM* aBoardItem ) - { - // developers should run DEBUG versions and fix such calls with NULL - wxASSERT( aBoardItem ); - - if( aBoardItem ) - delete Remove( aBoardItem ); - } - - /** - * Function Remove - * removes \a aBoardItem from this MODULE and returns it to caller without deleting it. - * @param aBoardItem The item to remove from this module. - * @return BOARD_ITEM* \a aBoardItem which was passed in. - */ - BOARD_ITEM* Remove( BOARD_ITEM* aBoardItem ); + ///> @copydoc BOARD_ITEM_CONTAINER::Remove() + void Remove( BOARD_ITEM* aBoardItem ) override; /** * Function ClearAllNets diff --git a/pcbnew/module_editor_frame.h b/pcbnew/module_editor_frame.h index 9d81f4876c..83ffbade0c 100644 --- a/pcbnew/module_editor_frame.h +++ b/pcbnew/module_editor_frame.h @@ -54,6 +54,9 @@ public: */ static const wxChar* GetFootprintEditorFrameName(); + ///> @copydoc PCB_BASE_FRAME::GetModel() + BOARD_ITEM_CONTAINER* GetModel() const override; + BOARD_DESIGN_SETTINGS& GetDesignSettings() const; // overload PCB_BASE_FRAME, get parent's void SetDesignSettings( const BOARD_DESIGN_SETTINGS& aSettings ); // overload diff --git a/pcbnew/moduleframe.cpp b/pcbnew/moduleframe.cpp index 932e1a2ad5..20de683891 100644 --- a/pcbnew/moduleframe.cpp +++ b/pcbnew/moduleframe.cpp @@ -350,6 +350,12 @@ FOOTPRINT_EDIT_FRAME::~FOOTPRINT_EDIT_FRAME() } +BOARD_ITEM_CONTAINER* FOOTPRINT_EDIT_FRAME::GetModel() const +{ + return GetBoard()->m_Modules; +} + + const wxString FOOTPRINT_EDIT_FRAME::getLibPath() { try diff --git a/pcbnew/pcb_base_edit_frame.h b/pcbnew/pcb_base_edit_frame.h index d9cc1cbd6a..3890bb9fc9 100644 --- a/pcbnew/pcb_base_edit_frame.h +++ b/pcbnew/pcb_base_edit_frame.h @@ -27,6 +27,8 @@ #include +class BOARD_ITEM_CONTAINER; + /** * Common, abstract interface for edit frames. */ @@ -42,6 +44,12 @@ public: virtual ~PCB_BASE_EDIT_FRAME() {}; + /** + * Function GetModel() + * returns the primary data model. + */ + virtual BOARD_ITEM_CONTAINER* GetModel() const = 0; + /** * Function CreateNewLibrary * prompts user for a library path, then creates a new footprint library at that diff --git a/pcbnew/pcbframe.cpp b/pcbnew/pcbframe.cpp index 89405495c6..9724c209b5 100644 --- a/pcbnew/pcbframe.cpp +++ b/pcbnew/pcbframe.cpp @@ -489,6 +489,12 @@ void PCB_EDIT_FRAME::SetBoard( BOARD* aBoard ) } +BOARD_ITEM_CONTAINER* PCB_EDIT_FRAME::GetModel() const +{ + return m_Pcb; +} + + void PCB_EDIT_FRAME::SetPageSettings( const PAGE_INFO& aPageSettings ) { PCB_BASE_FRAME::SetPageSettings( aPageSettings );