Commit 81ea3ce4 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Missing files.

parent f261bf38
Loading
Loading
Loading
Loading
+101 −0
Original line number Original line Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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
 */

#include "conditional_menu.h"


void CONDITIONAL_MENU::AddItem( const TOOL_ACTION& aAction, const SELECTION_CONDITION& aCondition,
                                int aOrder )
{
    assert( aAction.GetId() > 0 ); // Check if action was previously registered in ACTION_MANAGER
    addEntry( ENTRY( &aAction, aCondition, aOrder ) );
}


void CONDITIONAL_MENU::AddMenu( CONTEXT_MENU* aMenu, const wxString& aLabel, bool aExpand,
                                const SELECTION_CONDITION& aCondition, int aOrder )
{
    addEntry( ENTRY( aMenu, aLabel, aExpand, aCondition, aOrder ) );
}


void CONDITIONAL_MENU::AddSeparator( const SELECTION_CONDITION& aCondition, int aOrder )
{
    addEntry( ENTRY( aCondition, aOrder ) );
}


CONTEXT_MENU& CONDITIONAL_MENU::Generate( SELECTION& aSelection )
{
    // Do not delete entries - they are going to be reused
    m_menu.GetMenuItems().clear();

    for( std::list<ENTRY>::iterator it = m_entries.begin(); it != m_entries.end(); ++it )
    {
        const SELECTION_CONDITION& cond = it->Condition();

        if( !cond( aSelection ) )
            continue;

        switch( it->Type() )
        {
            case ENTRY::ACTION:
                m_menu.Add( *it->Action() );
                break;

            case ENTRY::MENU:
                m_menu.Add( it->Menu(), it->Label(), it->Expand() );
                break;

            case ENTRY::WXITEM:
                m_menu.Append( it->wxItem() );
                break;

            case ENTRY::SEPARATOR:
                m_menu.AppendSeparator();
                break;

            default:
                assert( false );
                break;
        }
    }

    return m_menu;
}


void CONDITIONAL_MENU::addEntry( ENTRY aEntry )
{
    if( aEntry.Order() < 0 )        // Any order, so give it any order number
        aEntry.SetOrder( m_entries.size() );

    std::list<ENTRY>::iterator it = m_entries.begin();

    // Find the right spot for the entry
    while( it != m_entries.end() && it->Order() <= aEntry.Order() )
        ++it;

    m_entries.insert( it, aEntry );
}
+214 −0
Original line number Original line Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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 CONDITIONAL_MENU_H
#define CONDITIONAL_MENU_H

#include <tool/context_menu.h>
#include "selection_conditions.h"

#include <boost/unordered_map.hpp>

class SELECTION_TOOL;

class CONDITIONAL_MENU
{
public:
    ///> Constant to indicate that we do not care about an ENTRY location in the menu.
    static const int ANY_ORDER = -1;

    /**
     * Function AddItem()
     *
     * Adds a menu entry to run a TOOL_ACTION on selected items.
     * @param aAction is a menu entry to be added.
     * @param aCondition is a condition that has to be fulfilled to enable the menu entry.
     * @param aOrder determines location of the added item, higher numbers are put on the bottom.
     * You may use ANY_ORDER here if you think it does not matter.
     */
    void AddItem( const TOOL_ACTION& aAction,
                  const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                  int aOrder = ANY_ORDER );

    /**
     * Function AddMenu()
     *
     * Adds a submenu to the menu.
     * @param aMenu is the submenu to be added.
     * @param aLabel is the label of added submenu.
     * @param aExpand determines if the added submenu items should be added as individual items
     * or as a submenu.
     * @param aCondition is a condition that has to be fulfilled to enable the submenu entry.
     * @param aOrder determines location of the added menu, higher numbers are put on the bottom.
     * You may use ANY_ORDER here if you think it does not matter.
     */
    void AddMenu( CONTEXT_MENU* aMenu, const wxString& aLabel, bool aExpand = false,
                  const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                  int aOrder = ANY_ORDER );

    /**
     * Function AddSeparator()
     *
     * Adds a separator to the menu.
     * @param aCondition is a condition that has to be fulfilled to enable the submenu entry.
     * @param aOrder determines location of the added menu, higher numbers are put on the bottom.
     * You may use ANY_ORDER here if you think it does not matter.
     */
    void AddSeparator( const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                       int aOrder = ANY_ORDER );

    /**
     * Function Generate()
     *
     * Generates a context menu that contains only entries that are satisfying assigned conditions.
     * @param aSelection is selection for which the conditions are checked against.
     * @return Menu filtered by the entry conditions.
     */
    CONTEXT_MENU& Generate( SELECTION& aSelection );

private:
    ///> Returned menu instance, prepared by Generate() function.
    CONTEXT_MENU m_menu;

    ///> Helper class to organize menu entries.
    class ENTRY
    {
    public:
        ENTRY( const TOOL_ACTION* aAction,
                            const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( ACTION ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
        {
            m_data.action = aAction;
        }

        ENTRY( CONTEXT_MENU* aMenu, const wxString aLabel, bool aExpand = false,
                            const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( MENU ), m_condition( aCondition ), m_order( aOrder ), m_label( aLabel ), m_expand( aExpand )
        {
            m_data.menu = aMenu;
        }

        ENTRY( wxMenuItem* aItem, const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( WXITEM ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
        {
            m_data.wxItem = aItem;
        }

        // Separator
        ENTRY( const SELECTION_CONDITION& aCondition = SELECTION_CONDITIONS::ShowAlways,
                            int aOrder = ANY_ORDER ) :
            m_type( SEPARATOR ), m_condition( aCondition ), m_order( aOrder ), m_expand( false )
        {
            m_data.wxItem = NULL;
        }

        ///> Possible entry types.
        enum ENTRY_TYPE {
            ACTION,
            MENU,
            WXITEM,
            SEPARATOR
        };

        inline ENTRY_TYPE Type() const
        {
            return m_type;
        }

        inline const TOOL_ACTION* Action() const
        {
            assert( m_type == ACTION );
            return m_data.action;
        }

        inline CONTEXT_MENU* Menu() const
        {
            assert( m_type == MENU );
            return m_data.menu;
        }

        inline wxMenuItem* wxItem() const
        {
            assert( m_type == WXITEM );
            return m_data.wxItem;
        }

        inline const wxString& Label() const
        {
            assert( m_type == MENU );
            return m_label;
        }

        inline bool Expand() const
        {
            assert( m_type == MENU );
            return m_expand;
        }

        inline const SELECTION_CONDITION& Condition() const
        {
            return m_condition;
        }

        inline int Order() const
        {
            return m_order;
        }

        inline void SetOrder( int aOrder )
        {
            m_order = aOrder;
        }

    private:
        ENTRY_TYPE m_type;

        union {
            const TOOL_ACTION* action;
            CONTEXT_MENU* menu;
            wxMenuItem* wxItem;
        } m_data;

        ///> Condition to be fulfilled to show the entry in menu.
        SELECTION_CONDITION m_condition;

        ///> Order number, the higher the number the lower position it takes it is in the menu.
        int m_order;

        /// CONTEXT_MENU specific fields.
        const wxString m_label;
        bool m_expand;
    };

    ///> Inserts the entry, preserving the requested order.
    void addEntry( ENTRY aEntry );

    ///> List of all menu entries.
    std::list<ENTRY> m_entries;
};

#endif /* CONDITIONAL_MENU_H */
+68 −0
Original line number Original line Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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
 */

#include "grid_menu.h"
#include <id.h>
#include <draw_frame.h>
#include <class_base_screen.h>
#include <tools/common_actions.h>

#include <boost/bind.hpp>

GRID_MENU::GRID_MENU( EDA_DRAW_FRAME* aParent ) : m_parent( aParent )
{
    BASE_SCREEN* screen = aParent->GetScreen();

    SetIcon( grid_select_xpm );
    SetMenuHandler( boost::bind( &GRID_MENU::EventHandler, this, _1 ) );
    SetUpdateHandler( boost::bind( &GRID_MENU::Update, this ) );

    wxArrayString gridsList;
    screen->BuildGridsChoiceList( gridsList, g_UserUnit != INCHES );

    for( unsigned int i = 0; i < gridsList.GetCount(); ++i )
    {
        GRID_TYPE& grid = screen->GetGrid( i );
        Append( grid.m_Id, gridsList[i], wxEmptyString, true );
    }
}


OPT_TOOL_EVENT GRID_MENU::EventHandler( const wxMenuEvent& aEvent )
{
    OPT_TOOL_EVENT event( COMMON_ACTIONS::gridPreset.MakeEvent() );
    long idx = aEvent.GetId() - ID_POPUP_GRID_SELECT - 1;
    event->SetParameter( idx );

    return event;
}


void GRID_MENU::Update()
{
    for( unsigned int i = 0; i < GetMenuItemCount(); ++i )
        Check( ID_POPUP_GRID_SELECT + 1 + i, false );

    Check( m_parent->GetScreen()->GetGridId(), true );
}
+44 −0
Original line number Original line Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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 GRID_MENU_H
#define GRID_MENU_H

#include <tool/context_menu.h>

class EDA_DRAW_FRAME;

class GRID_MENU : public CONTEXT_MENU
{
public:
    GRID_MENU( EDA_DRAW_FRAME* aParent );

    OPT_TOOL_EVENT EventHandler( const wxMenuEvent& aEvent );
    void Update();

private:
    EDA_DRAW_FRAME* m_parent;
};

#endif /* GRID_MENU_H */
+71 −0
Original line number Original line Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 *
 * 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
 */

#include "zoom_menu.h"
#include <id.h>
#include <draw_frame.h>
#include <class_base_screen.h>
#include <tools/common_actions.h>

#include <boost/bind.hpp>

ZOOM_MENU::ZOOM_MENU( EDA_DRAW_FRAME* aParent ) : m_parent( aParent )
{
    BASE_SCREEN* screen = aParent->GetScreen();

    SetIcon( zoom_selection_xpm );
    SetMenuHandler( boost::bind( &ZOOM_MENU::EventHandler, this, _1 ) );
    SetUpdateHandler( boost::bind( &ZOOM_MENU::Update, this ) );

    //int zoom = screen->GetZoom();
    int maxZoomIds = std::min( ID_POPUP_ZOOM_LEVEL_END - ID_POPUP_ZOOM_LEVEL_START,
                               (int) screen->m_ZoomList.size() );

    for( int i = 0; i < maxZoomIds; ++i )
    {
        Append( ID_POPUP_ZOOM_LEVEL_START + i,
            wxString::Format( _( "Zoom: %.2f" ), aParent->GetZoomLevelCoeff() / screen->m_ZoomList[i] ),
            wxEmptyString, wxITEM_CHECK );
    }
}


OPT_TOOL_EVENT ZOOM_MENU::EventHandler( const wxMenuEvent& aEvent )
{
    OPT_TOOL_EVENT event( COMMON_ACTIONS::zoomPreset.MakeEvent() );
    long idx = aEvent.GetId() - ID_POPUP_ZOOM_LEVEL_START;
    event->SetParameter( idx );

    return event;
}


void ZOOM_MENU::Update()
{
    double zoom = m_parent->GetScreen()->GetZoom();
    const std::vector<int>& zoomList = m_parent->GetScreen()->m_ZoomList;

    for( unsigned int i = 0; i < GetMenuItemCount(); ++i )
        Check( ID_POPUP_ZOOM_LEVEL_START + i, zoomList[i] == zoom );
}
Loading