Skip to content

Commit

Permalink
command: added cli calculator by cal/calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
dxli committed Nov 22, 2014
1 parent 677a663 commit e6d4f7b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
24 changes: 24 additions & 0 deletions librecad/src/cmd/rs_commands.cpp
Expand Up @@ -753,5 +753,29 @@ QString RS_Commands::msgAvailableCommands() {
return tr("Available commands:");
}

/**
* @brief extractCliCal, filter cli calculator math expression
* @param cmd, cli string
* @return math expression for RS_Math:eval();
*/
QString RS_Commands::filterCliCal(const QString& cmd)
{

QString str=cmd.trimmed();
const QRegExp calCmd(R"(^(cal|calculate))");
if(!(str.contains(calCmd)
|| str.startsWith(tr("cal","command to trigger cli calculator"), Qt::CaseInsensitive)
|| str.startsWith(tr("calculate","command to trigger cli calculator"), Qt::CaseInsensitive)
)) {
return QString();
}
int index=str.indexOf(QRegExp(R"(\s)"));
bool spaceFound=(index>=0);
str=str.mid(index);
index=str.indexOf(QRegExp(R"(\S)"));
if(!(spaceFound && index>=0)) return QString();
str=str.mid(index);
return str;
}

// EOF
14 changes: 10 additions & 4 deletions librecad/src/cmd/rs_commands.h
Expand Up @@ -70,10 +70,16 @@ class RS_Commands : public QObject {
static QString msgAvailableCommands();
void updateAlias();

// Prefixes for function-, Meta- and Alt- keys.
static const char *FnPrefix;
static const char *AltPrefix;
static const char *MetaPrefix;
// Prefixes for function-, Meta- and Alt- keys.
static const char *FnPrefix;
static const char *AltPrefix;
static const char *MetaPrefix;
/**
* @brief extractCliCal, filter cli calculator math expression
* @param cmd, cli string
* @return math expression for RS_Math:eval();
*/
static QString filterCliCal(const QString& cmd);

protected:
static RS_Commands* uniqueInstance;
Expand Down
1 change: 1 addition & 0 deletions librecad/src/lib/debug/rs_debug.h
Expand Up @@ -33,6 +33,7 @@
#endif

#include <QString>
#include <QDebug>

/** print out a debug header*/
#define DEBUG_HEADER() std::cout<<__FILE__<<" : "<<__FUNCTION__<<" : line "<<__LINE__<<std::endl
Expand Down
26 changes: 25 additions & 1 deletion librecad/src/lib/gui/rs_eventhandler.cpp
Expand Up @@ -30,6 +30,7 @@
#include "rs_actioninterface.h"
#include "rs_dialogfactory.h"
#include "rs_commandevent.h"
#include "rs_commands.h"

/**
* Constructor.
Expand Down Expand Up @@ -233,15 +234,38 @@ void RS_EventHandler::keyReleaseEvent(QKeyEvent* e) {
}


bool RS_EventHandler::cliCalculator(const QString& cmd) const
{
QString str=RS_Commands::filterCliCal(cmd);

if(str.isEmpty()){
// RS_DIALOGFACTORY->commandMessage("No math expression");
return false;
}
bool ok=true;
double result=RS_Math::eval(str,&ok);
if(ok)
RS_DIALOGFACTORY->commandMessage(str + " = "+QString::number(result));
else
RS_DIALOGFACTORY->commandMessage("Calculator error for input: "+ str);
return true;
}

/**
* Handles command line events.
*/
void RS_EventHandler::commandEvent(RS_CommandEvent* e) {
RS_DEBUG->print("RS_EventHandler::commandEvent");

QString cmd = e->getCommand();

// allow using command line as a calculator
if (!e->isAccepted()) {
if(cliCalculator(cmd)) {
e->accept();
return;
}
}

if (coordinateInputEnabled) {
if (!e->isAccepted()) {

Expand Down
21 changes: 14 additions & 7 deletions librecad/src/lib/gui/rs_eventhandler.h
Expand Up @@ -54,9 +54,9 @@ class RS_EventHandler {
void keyPressEvent(QKeyEvent* e);
void keyReleaseEvent(QKeyEvent* e);

void commandEvent(RS_CommandEvent* e);
void enableCoordinateInput();
void disableCoordinateInput();
void commandEvent(RS_CommandEvent* e);
void enableCoordinateInput();
void disableCoordinateInput();

void setDefaultAction(RS_ActionInterface* action);
RS_ActionInterface* getDefaultAction();
Expand All @@ -75,12 +75,19 @@ class RS_EventHandler {
void setSnapRestriction(RS2::SnapRestriction sr);

protected:
RS_GraphicView* graphicView;
RS_ActionInterface* defaultAction;
// RS_ActionInterface* currentActions[RS_MAXACTIONS];
RS_GraphicView* graphicView;
RS_ActionInterface* defaultAction;
// RS_ActionInterface* currentActions[RS_MAXACTIONS];
QList<RS_ActionInterface*> currentActions;
int actionIndex;
bool coordinateInputEnabled;
bool coordinateInputEnabled;
private:
/**
* @brief cliCalEvent, process cli "cal" calculator command
* @param cmd, cli line to check for "cal" command
* @return true, if cli starts with "cal"
*/
bool cliCalculator(const QString& cmd) const;
};

#endif

0 comments on commit e6d4f7b

Please sign in to comment.