Skip to content

Commit

Permalink
+ Extend Command framework to allow to update QAction
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Jun 3, 2015
1 parent 0fec404 commit c910189
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Gui/Command.cpp
Expand Up @@ -672,6 +672,10 @@ void Command::languageChange()
}
}

void Command::updateAction(int)
{
}

//===========================================================================
// MacroCommand
//===========================================================================
Expand Down Expand Up @@ -1049,3 +1053,20 @@ void CommandManager::testActive(void)
}
}

void CommandManager::addCommandMode(const char* sContext, const char* sName)
{
_sCommandModes[sContext].push_back(sName);
}

void CommandManager::updateCommands(const char* sContext, int mode)
{
std::map<std::string, std::list<std::string> >::iterator it = _sCommandModes.find(sContext);
if (it != _sCommandModes.end()) {
for (std::list<std::string>::iterator jt = it->second.begin(); jt != it->second.end(); ++jt) {
Command* cmd = getCommandByName(jt->c_str());
if (cmd) {
cmd->updateAction(mode);
}
}
}
}
53 changes: 51 additions & 2 deletions src/Gui/Command.h
Expand Up @@ -84,9 +84,12 @@ class GuiExport CommandBase
protected:
/// Creates the used Action when adding to a widget. The default implementation does nothing.
virtual Action * createAction(void);

public:
/// Reassigns QAction stuff after the language has changed.
virtual void languageChange() = 0;
/// Updates the QAction with respect to the passed mode.
virtual void updateAction(int mode) = 0;
/// The C++ class name is needed as context for the translation framework
virtual const char* className() const = 0;
//@}
Expand Down Expand Up @@ -219,6 +222,8 @@ class GuiExport Command : public CommandBase
static bool isActiveObjectValid(void);
/// Translate command
void languageChange();
/// Updates the QAction with respect to the passed mode.
void updateAction(int mode);
//@}

/** @name Helper methods for issuing commands to the Python interpreter */
Expand Down Expand Up @@ -464,14 +469,19 @@ class GuiExport CommandManager
*/
void runCommandByName (const char* sName) const;

/// method is OBSOLET use GetModuleCommands() or GetAllCommands()
/// method is OBSOLETE use GetModuleCommands() or GetAllCommands()
const std::map<std::string, Command*>& getCommands() const { return _sCommands; }
/// get frequently called by the AppWnd to check the commands are active.
void testActive(void);

void addCommandMode(const char* sContext, const char* sName);
void updateCommands(const char* sContext, int mode);

private:
/// Destroys all commands in the manager and empties the list.
void clearCommands();
std::map<std::string,Command*> _sCommands;
std::map<std::string, Command*> _sCommands;
std::map<std::string, std::list<std::string> > _sCommandModes;
};

} // namespace Gui
Expand Down Expand Up @@ -544,6 +554,24 @@ protected: \
virtual Gui::Action * createAction(void);\
};

/** The Command Macro Standard + isActive() + updateAction()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Werner Mayer
*/
#define DEF_STD_CMD_AU(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual void updateAction(int mode); \
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
};

/** The Command Macro Standard + isActive() + createAction()
* + languageChange()
* This macro makes it easier to define a new command.
Expand All @@ -564,6 +592,27 @@ protected: \
virtual Gui::Action * createAction(void);\
};

/** The Command Macro Standard + isActive() + createAction()
* + languageChange() + updateAction()
* This macro makes it easier to define a new command.
* The parameters are the class name
* @author Werner Mayer
*/
#define DEF_STD_CMD_ACLU(X) class X : public Gui::Command \
{\
public:\
X();\
virtual ~X(){}\
virtual void languageChange(); \
virtual void updateAction(int mode); \
virtual const char* className() const\
{ return #X; }\
protected: \
virtual void activated(int iMsg);\
virtual bool isActive(void);\
virtual Gui::Action * createAction(void);\
};

/** The Command Macro view
* This macro makes it easier to define a new command for the 3D View
* It activate the command only when a 3DView is active.
Expand Down

0 comments on commit c910189

Please sign in to comment.