Permalink
Browse files

Add coordinate and number support in command line, and Lua code reorg…

…anisation
  • Loading branch information...
feragon committed Jun 7, 2016
1 parent 61be581 commit b10e27997db630665e8c17ab3fc8eb3ffcb0c356
View
@@ -422,4 +422,9 @@ void CadMdiChild::setId(unsigned int id) {
void CadMdiChild::setDestroyCallback(LuaIntf::LuaRef destroyCallback) {
_destroyCallback = destroyCallback;
}
void CadMdiChild::keyPressEvent(QKeyEvent *event) {
QWidget::keyPressEvent(event);
emit keyPressed(event);
}
View
@@ -3,6 +3,7 @@
#include <QScrollBar>
#include <QVBoxLayout>
#include <QWidget>
#include <QKeyEvent>
#include <lcadviewer.h>
#include "cad/meta/color.h"
#include <cad/document/storagemanager.h>
@@ -39,6 +40,8 @@ class CadMdiChild : public QWidget {
void setDestroyCallback(LuaIntf::LuaRef callback);
void keyPressEvent(QKeyEvent* event);
public slots:
void on_actionAdd_Random_Lines_triggered();
@@ -50,6 +53,9 @@ class CadMdiChild : public QWidget {
void ctxMenu(const QPoint& pos);
signals:
void keyPressed(QKeyEvent* event);
public:
QWidget* view() const;
std::shared_ptr<lc::Document> document() const;
View
@@ -1,6 +1,5 @@
#include "luaqobject.h"
#include <iostream>
#include "widgets/clicommand.h"
LuaQObject::LuaQObject(QObject* object):
_object(object),
_valid(false)
@@ -94,52 +93,56 @@ QObject* LuaQObject::findChild(std::string name) {
}
void LuaQObject::pushArg(LuaIntf::LuaState s, int const type, void const* arg) {
switch (type)
{
if(type == qRegisterMetaType<lc::geo::Coordinate>()) {
s.push(*(lc::geo::Coordinate*) arg);
return;
}
switch (type) {
case QMetaType::Void:
s.push(nullptr);
break;
case QMetaType::Bool:
s.push(*(bool*)arg);
s.push(*(bool *) arg);
break;
case QMetaType::Int:
s.push(*(int*)arg);
s.push(*(int *) arg);
break;
case QMetaType::UInt:
s.push(*(unsigned int*)arg);
s.push(*(unsigned int *) arg);
break;
case QMetaType::Long:
s.push(*(long*)arg);
s.push(*(long *) arg);
break;
case QMetaType::LongLong:
s.push(*(long long*)arg);
s.push(*(long long *) arg);
break;
case QMetaType::Short:
s.push(*(short*)arg);
s.push(*(short *) arg);
break;
case QMetaType::Char:
s.push(*(char*)arg);
s.push(*(char *) arg);
break;
case QMetaType::ULong:
s.push(*(unsigned long*)arg);
s.push(*(unsigned long *) arg);
break;
case QMetaType::ULongLong:
s.push(*(unsigned long long*)arg);
s.push(*(unsigned long long *) arg);
break;
case QMetaType::UShort:
s.push(*(unsigned short*)arg);
s.push(*(unsigned short *) arg);
break;
case QMetaType::UChar:
s.push(*(unsigned char*)arg);
s.push(*(unsigned char *) arg);
break;
case QMetaType::Double:
s.push(*(double*)arg);
s.push(*(double *) arg);
break;
case QMetaType::Float:
s.push(*(float*)arg);
s.push(*(float *) arg);
break;
case QMetaType::QString:
s.push(reinterpret_cast<const QString*>(arg));
s.push(reinterpret_cast<const QString *>(arg));
break;
default:
s.push(nullptr);
View
@@ -11,6 +11,7 @@ extern "C"
#include <QMetaMethod>
#include "lua-intf/LuaIntf/LuaIntf.h"
#include "lua/qtbridge.h"
class LuaQObject : public QObject {
public:
View
@@ -1,4 +1,6 @@
#include "lua/qtbridge.h"
#include "luaqobject.h"
#include "luainterface.h"
namespace LuaIntf {
LUA_USING_SHARED_PTR_TYPE(std::shared_ptr)
@@ -10,6 +12,7 @@ void luaOpenQtBridge(lua_State *L) {
addQtLayoutBindings(L);
addQtWidgetsBindings(L);
addLCBindings(L);
addQtMetaTypes();
}
void addQtBaseBindings(lua_State *L) {
@@ -145,7 +148,7 @@ void addLCBindings(lua_State *L) {
.beginModule("lc")
.beginExtendClass<CadMdiChild, QWidget>("CadMdiChild")
.addConstructor(LUA_SP(std::shared_ptr<CadMdiChild>), LUA_ARGS())
.addConstructor(LUA_SP(std::shared_ptr<CadMdiChild>), LUA_ARGS(LuaIntf::_opt<QWidget*>))
.addFunction("cursor", &CadMdiChild::cursor)
.addFunction("document", &CadMdiChild::document)
.addProperty("id", &CadMdiChild::id, &CadMdiChild::setId)
@@ -183,9 +186,12 @@ void addLCBindings(lua_State *L) {
.beginExtendClass<CliCommand, QDockWidget>("CliCommand")
.addConstructor(LUA_SP(std::shared_ptr<CliCommand>), LUA_ARGS(QWidget*))
.addFunction("addCommand", &CliCommand::addCommand)
.addFunction("focus", &CliCommand::focus)
.addFunction("write", &CliCommand::write, LUA_ARGS(const char*))
.endClass()
.endModule();
}
}
void addQtMetaTypes() {
qRegisterMetaType<lc::geo::Coordinate>();
}
View
@@ -28,13 +28,14 @@ extern "C"
#include "widgets/luascript.h"
#include "widgets/clicommand.h"
#include "luainterface.h"
#include "luaqobject.h"
Q_DECLARE_METATYPE(lc::geo::Coordinate);
void luaOpenQtBridge(lua_State *L);
void addQtBaseBindings(lua_State *L);
void addQtWindowBindings(lua_State *L);
void addQtLayoutBindings(lua_State *L);
void addQtWidgetsBindings(lua_State *L);
void addLCBindings(lua_State *L);
void addLCBindings(lua_State *L);
void addQtMetaTypes();
@@ -1,4 +1,3 @@
#include <QtWidgets/QtWidgets>
#include "clicommand.h"
#include "ui_clicommand.h"
@@ -46,27 +45,89 @@ void CliCommand::write(QString message) {
}
void CliCommand::onReturnPressed() {
_history.push_front(ui->command->text());
auto text = ui->command->text();
bool isNumber;
_history.push_front(text);
if(_history.size() > _historySize) {
_history.pop_back();
}
enterCommand(ui->command->text());
}
auto number = text.toDouble(&isNumber);
if(isNumber) {
enterNumber(number);
}
else if(text.indexOf(";") != -1 || text.indexOf(",") != -1) {
enterCoordinate(text);
}
else {
enterCommand(text);
}
void CliCommand::focus() {
ui->command->setFocus();
ui->command->clear();
}
void CliCommand::keyPressEvent(QKeyEvent *event) {
onKeyPressed(event);
}
void CliCommand::enterCommand(QString command) {
if(command == "") {
return;
}
auto completion = _completer->currentCompletion();
if(command.compare(completion, Qt::CaseInsensitive) == 0) {
write("Command: " + completion);
emit commandEntered(completion);
}
else {
write("Command " + command + " not found");
ui->history->item(ui->history->count() - 1)->setForeground(Qt::red);
}
}
void CliCommand::enterCoordinate(QString coordinate) {
lc::geo::Coordinate point;
QStringList numbers;
if(coordinate.indexOf(";") != -1) {
numbers = coordinate.split(";");
}
else {
numbers = coordinate.split(",");
}
if(numbers.size() > 2) {
point = lc::geo::Coordinate(numbers[0].toFloat(), numbers[1].toFloat(), numbers[2].toFloat());
}
else {
point = lc::geo::Coordinate(numbers[0].toFloat(), numbers[1].toFloat());
}
auto message = QString("Coordinate: x=%1; y=%2; z=%3").arg(point.x()).arg(point.y()).arg(point.z());
write(message);
emit coordinateEntered(point);
}
void CliCommand::enterNumber(double number) {
write(QString("Number: %1").arg(number));
emit numberEntered(number);
}
void CliCommand::onKeyPressed(QKeyEvent *event) {
switch(event->key()) {
case Qt::Key_Up:
if(_historyIndex + 1 < _history.size()) {
_historyIndex++;
ui->command->setText(_history[_historyIndex]);
}
break;
case Qt::Key_Down:
if(_historyIndex > 0) {
_historyIndex--;
@@ -77,18 +138,10 @@ void CliCommand::keyPressEvent(QKeyEvent *event) {
ui->command->clear();
}
break;
}
}
void CliCommand::enterCommand(QString command) {
if(_commands->stringList().indexOf(command) == -1) {
write("Command " + command + " not found");
ui->history->item(ui->history->count() - 1)->setForeground(Qt::red);
}
else {
write("Command: " + command);
emit commandEntered(command);
default:
ui->command->setFocus();
ui->command->event(event);
break;
}
ui->command->clear();
}
View
@@ -8,6 +8,8 @@
#include <memory>
#include <cad/geometry/geocoordinate.h>
namespace Ui {
class CliCommand;
}
@@ -23,17 +25,23 @@ class CliCommand : public QDockWidget {
bool addCommand(std::string name);
void enterCommand(QString command);
void write(QString message);
void focus();
public slots:
void onReturnPressed();
void onKeyPressed(QKeyEvent *event);
signals:
void commandEntered(QString command);
void coordinateEntered(lc::geo::Coordinate coordinate);
void numberEntered(double number);
private:
void enterCommand(QString command);
void enterCoordinate(QString coordinate);
void enterNumber(double number);
Ui::CliCommand* ui;
std::shared_ptr<QCompleter> _completer;
std::shared_ptr<QStringListModel> _commands;
@@ -7,15 +7,16 @@ function Operations:new(id)
Operations.__index = self
setmetatable(o, self)
self.id = id
event.register("command", self)
o.id = id
event.register("command", o)
return o
end
function Operations:onEvent(eventName, ...)
--If current window is focused
if(mdiArea:activeSubWindow():widget().id == self.id) then
if(active_widget().id == self.id) then
if(eventName == "point") then
self:get_line_point(...)
elseif(eventName == "command") then
@@ -38,10 +39,10 @@ end
function Operations:get_line_point(point)
if(self.line_vars['lastPoint'] ~= nil) then
local lp = self.line_vars['lastPoint']
local d = mdiArea:activeSubWindow():widget():document()
local d = active_widget():document()
local b = Builder(d)
local layer = d:layerByName("0")
local l=Line(Coord(lp[1], lp[2]), Coord(point[1], point[2]), layer);
local l=Line(Coord(lp:x(), lp:y()), Coord(point:x(), point:y()), layer);
b:append(l)
b:execute()
Oops, something went wrong.

0 comments on commit b10e279

Please sign in to comment.