tool_event.cpp 4.46 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) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@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
 */

25 26 27 28
#include <cstring>
#include <string>

#include <tool/tool_event.h>
29
#include <tool/tool_action.h>
30 31 32 33
#include <tool/tool_manager.h>

#include <boost/foreach.hpp>

34 35
struct FlagString
{
36 37
    int flag;
    std::string str;
38 39
};

40

Maciej Suminski's avatar
Maciej Suminski committed
41
static const std::string flag2string( int aFlag, const FlagString* aExps )
42
{
43
    std::string rv;
44

Maciej Suminski's avatar
Maciej Suminski committed
45
    for( int i = 0; aExps[i].str.length(); i++ )
46
    {
Maciej Suminski's avatar
Maciej Suminski committed
47 48
        if( aExps[i].flag & aFlag )
            rv += aExps[i].str + " ";
49
    }
50

51
    return rv;
52 53
}

54

55 56 57 58 59 60
bool TOOL_EVENT::IsAction( const TOOL_ACTION* aAction ) const
{
    return Matches( aAction->MakeEvent() );
}


61 62
const std::string TOOL_EVENT::Format() const
{
63 64
    std::string ev;

65 66
    const FlagString categories[] =
    {
Maciej Suminski's avatar
Maciej Suminski committed
67 68 69 70 71
        { TC_MOUSE,    "mouse"    },
        { TC_KEYBOARD, "keyboard" },
        { TC_COMMAND,  "command"  },
        { TC_MESSAGE,  "message"  },
        { TC_VIEW,     "view"     },
72
        { 0,           ""         }
73 74
    };

75 76
    const FlagString actions[] =
    {
Maciej Suminski's avatar
Maciej Suminski committed
77
        { TA_MOUSE_CLICK,           "click"               },
78
        { TA_MOUSE_DBLCLICK,        "double click"        },
Maciej Suminski's avatar
Maciej Suminski committed
79 80 81 82 83
        { TA_MOUSE_UP,              "button-up"           },
        { TA_MOUSE_DOWN,            "button-down"         },
        { TA_MOUSE_DRAG,            "drag"                },
        { TA_MOUSE_MOTION,          "motion"              },
        { TA_MOUSE_WHEEL,           "wheel"               },
84
        { TA_KEY_PRESSED,           "key-pressed"         },
Maciej Suminski's avatar
Maciej Suminski committed
85 86 87 88 89 90 91 92
        { TA_VIEW_REFRESH,          "view-refresh"        },
        { TA_VIEW_ZOOM,             "view-zoom"           },
        { TA_VIEW_PAN,              "view-pan"            },
        { TA_VIEW_DIRTY,            "view-dirty"          },
        { TA_CHANGE_LAYER,          "change-layer"        },
        { TA_CANCEL_TOOL,           "cancel-tool"         },
        { TA_CONTEXT_MENU_UPDATE,   "context-menu-update" },
        { TA_CONTEXT_MENU_CHOICE,   "context-menu-choice" },
93
        { TA_UNDO_REDO,             "undo-redo"           },
Maciej Suminski's avatar
Maciej Suminski committed
94
        { TA_ACTION,                "action"              },
95
        { TA_ACTIVATE,              "activate"            },
Maciej Suminski's avatar
Maciej Suminski committed
96
        { 0,                        ""                    }
97 98
    };

99 100
    const FlagString buttons[] =
    {
101 102 103 104
        { BUT_NONE,   "none"   },
        { BUT_LEFT,   "left"   },
        { BUT_RIGHT,  "right"  },
        { BUT_MIDDLE, "middle" },
105
        { 0,          ""       }
106 107
    };

108 109
    const FlagString modifiers[] =
    {
Maciej Suminski's avatar
Maciej Suminski committed
110 111 112 113
        { MD_SHIFT, "shift" },
        { MD_CTRL,  "ctrl"  },
        { MD_ALT,   "alt"   },
        { 0,        ""      }
114 115 116 117 118 119 120
    };

    ev = "category: ";
    ev += flag2string( m_category, categories );
    ev += " action: ";
    ev += flag2string( m_actions, actions );

Maciej Suminski's avatar
Maciej Suminski committed
121
    if( m_actions & TA_MOUSE )
122 123 124 125 126
    {
        ev += " btns: ";
        ev += flag2string( m_mouseButtons, buttons );
    }

Maciej Suminski's avatar
Maciej Suminski committed
127
    if( m_actions & TA_KEYBOARD )
128
    {
129 130 131
        char tmp[128];
        sprintf( tmp, "key: %d", m_keyCode );
        ev += tmp;
132 133
    }

Maciej Suminski's avatar
Maciej Suminski committed
134
    if( m_actions & ( TA_MOUSE | TA_KEYBOARD ) )
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    {
        ev += " mods: ";
        ev += flag2string( m_modifiers, modifiers );
    }

    if( m_commandId )
    {
        char tmp[128];
        sprintf( tmp, "cmd-id: %d", *m_commandId );
        ev += tmp;
    }

    if( m_commandStr )
        ev += "cmd-str: " + ( *m_commandStr );

    return ev;
151 152
}

153

154 155
const std::string TOOL_EVENT_LIST::Format() const
{
156
    std::string s;
157 158 159

    BOOST_FOREACH( TOOL_EVENT e, m_events )
        s += e.Format() + " ";
160

161
    return s;
162
}