1 // Copyright (c) 2016 The SigViewer Development Team
2 // Licensed under the GNU General Public License (GPL)
3 // https://www.gnu.org/licenses/gpl
4 
5 
6 #include "gui_action_factory.h"
7 
8 namespace sigviewer
9 {
10 
11 GuiActionFactory* GuiActionFactory::instance_ = 0;
12 
13 //-----------------------------------------------------------------------------
getInstance()14 GuiActionFactory* GuiActionFactory::getInstance ()
15 {
16     if (!instance_)
17     {
18         instance_ = new GuiActionFactory;
19     }
20     return instance_;
21 }
22 
23 //-----------------------------------------------------------------------------
registerCommand(QString const & name,QSharedPointer<GuiActionCommand> command)24 void GuiActionFactory::registerCommand (QString const& name, QSharedPointer<GuiActionCommand> command)
25 {
26     command_map_[name] = command;
27     QStringList action_ids = command->getActionIDs();
28     for (QStringList::const_iterator id_iter = action_ids.begin ();
29          id_iter != action_ids.end ();
30          ++id_iter)
31         action_to_command_map_[*id_iter] = command;
32 }
33 
34 
35 //-----------------------------------------------------------------------------
getQActions(QString const & command_name) const36 QList<QAction*> GuiActionFactory::getQActions (QString const& command_name) const
37 {
38     if (command_name.size() == 0)
39     {
40         QList<QAction*> actions;
41         foreach (QSharedPointer<GuiActionCommand> command, command_map_.values())
42             actions.append (command->getQActions());
43         return actions;
44     }
45     else if (!command_map_.contains (command_name))
46         return QList<QAction*>();
47     else
48         return command_map_[command_name]->getQActions ();
49 }
50 
51 //-----------------------------------------------------------------------------
getQAction(QString const & action_id) const52 QAction* GuiActionFactory::getQAction (QString const& action_id) const
53 {
54     if (!action_to_command_map_.contains (action_id))
55         return 0;
56     else
57         return action_to_command_map_[action_id]->getQAction (action_id);
58 }
59 
60 
61 //-----------------------------------------------------------------------------
initAllCommands()62 void GuiActionFactory::initAllCommands ()
63 {
64 	for (CommandMap::iterator cmd_iter = command_map_.begin();
65 	cmd_iter != command_map_.end();
66 		++cmd_iter)
67 	{
68 		cmd_iter.value()->initConnections();
69 		cmd_iter.value()->init();
70 	}
71 }
72 
73 }
74