Skip to content
Snippets Groups Projects
Commit 3aa9123e authored by Mirko Wächter's avatar Mirko Wächter
Browse files

Added utility class for opening files in an editor

parent c52c9189
No related branches found
No related tags found
No related merge requests found
<editors>
<editor>
<name>qtcreator</name>
<opencommandline>qtcreator -client $file:$line</opencommandline>
</editor>
<editor>
<name>gedit</name>
<opencommandline>gedit $file</opencommandline>
</editor>
<editor>
<name>geany</name>
<opencommandline>geany $file -l $line</opencommandline>
</editor>
</editors>
......@@ -18,12 +18,16 @@ if (ARMARX_BUILD)
set(LIB_FILES "")
set(LIB_SOURCES ArmarXWidgetController.cpp
ArmarXComponentWidgetController.cpp)
ArmarXComponentWidgetController.cpp
utility/editorfileopener.cpp
)
set(LIB_HEADERS ArmarXGuiInterface.h
ArmarXGuiPlugin.h
ArmarXWidgetController.h
ArmarXComponentWidgetController.h)
ArmarXComponentWidgetController.h
utility/editorfileopener.h
)
qt4_wrap_cpp(LIB_FILES ArmarXGuiPlugin.h ArmarXWidgetController.h)
......
#include "editorfileopener.h"
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <Core/core/system/ArmarXDataPath.h>
using namespace armarx;
EditorFileOpener::EditorFileOpener(std::string configFile)
{
setTag("EditorFileOpener");
using namespace boost::property_tree;
if(configFile.empty())
configFile = std::string(EDITORFILEOPENER_CONFIGFILE);
if(!ArmarXDataPath::getAbsolutePath(configFile,configFile))
{
ARMARX_WARNING << "Cannot find file '" << configFile << "'";
return;
}
try
{
xml_parser::read_xml(configFile, pt);
}
catch(xml_parser::xml_parser_error &e)
{
ARMARX_WARNING << "Could not load state-configfile '"
<< configFile << "'. Required parameters may be unset.\nReason at "
<< e.filename() << ":" << e.line() <<":\n" << e.message() << flush;
return;
}
BOOST_FOREACH(ptree::value_type const& v, pt.get_child("editors"))
{
if(v.first == "editor")
{
std::string editorName = v.second.get<std::string>("name");
std::string openCommandLine = v.second.get<std::string>("opencommandline");
editors[editorName] = openCommandLine;
}
}
}
void EditorFileOpener::openFile(const std::string &editorName, const std::string &filepath, int lineNumber)
{
std::string openCommandLine = editors[editorName];
if(openCommandLine.empty())
{
ARMARX_WARNING << "Cannot find editor named '" << editorName << "'";
return;
}
int lenFile = std::string(FILE_VARIABLE).size();
int posFile = openCommandLine.find(FILE_VARIABLE);
if(posFile != std::string::npos )
openCommandLine.replace(posFile, lenFile, filepath);
int lenLine = std::string(LINE_VARIABLE).size();
int posLine = openCommandLine.find(LINE_VARIABLE);
std::stringstream lineStr;
lineStr << lineNumber;
if(posLine != std::string::npos )
openCommandLine.replace(posLine, lenLine, lineStr.str());
boost::algorithm::trim(openCommandLine);
if(*openCommandLine.rbegin() != '&')
openCommandLine += " &";
ARMARX_VERBOSE << "OpenCommand: " << openCommandLine;
if(std::system(openCommandLine.c_str())){}
}
#ifndef _ARMARX_EDITORFILEOPENER_H
#define _ARMARX_EDITORFILEOPENER_H
#include <Core/core/logging/Logging.h>
#include <map>
#include <boost/property_tree/ptree.hpp>
namespace armarx
{
#define EDITORFILEOPENER_CONFIGFILE "Gui/data/EditorFileOpenerConfig.xml"
#define FILE_VARIABLE "$file"
#define LINE_VARIABLE "$line"
class EditorFileOpener : public Logging
{
public:
EditorFileOpener(std::string configFile ="");
void openFile(const std::string& editorName, const std::string& filepath, int lineNumber = 0);
void setOpencommand(const std::string& editorName, const std::string& openCommandLine);
private:
std::map<std::string, std::string> editors;
boost::property_tree::ptree pt;
};
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment