Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI improvements #51

Merged
merged 3 commits into from
May 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lcUI/cadmdichild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,15 @@ void CadMdiChild::cancelCurrentOperations() {

lc::UndoManager_SPtr CadMdiChild::undoManager() const {
return _undoManager;
}

std::shared_ptr<LCViewer::Cursor> CadMdiChild::cursor() const {
return _cursor;
}

unsigned int CadMdiChild::id() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change the _id type to "unsigned long long"?

even though it's mostly 32 bit for "unsigned int", the C++ standard only requires "unsigned int" type to be at least 16 bit.

I suppose we need 64 bit to avoid collision on a busy system.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _id is only 16 bits that makes 65536 new documents, that's possible if someone never close the program.
I think the better solution is to reuse the id of closed documents, then all the windows would have to be opened at the same time to obtain a collision. It should be possible by calling a Lua function with Qt destroyed event

return _id;
}
void CadMdiChild::setId(unsigned int id) {
_id = id;
}
5 changes: 5 additions & 0 deletions lcUI/cadmdichild.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ class CadMdiChild : public QWidget {
std::shared_ptr<OperationManager> operationManager() const;
lc::StorageManager_SPtr storageManager() const;
LCViewer::LCADViewer* viewer() const {return _viewer;} ;
std::shared_ptr<LCViewer::Cursor> cursor() const;
void cancelCurrentOperations();
void import(std::string);

unsigned int id();
void setId(unsigned int id);

private:
int randInt(int low, int high);
unsigned int _id;

std::shared_ptr<lc::Document> _document;
lc::UndoManagerImpl_SPtr _undoManager;
Expand Down
7 changes: 7 additions & 0 deletions lcUI/lua/qtbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void addQtWindowBindings(lua_State *L) {
.addFunction("addSubWindow", [](QMdiArea* mdiArea, QWidget* subWindow) {
return mdiArea->addSubWindow(subWindow);
})
.addFunction("subWindowList", &QMdiArea::subWindowList)
.endClass()

.beginExtendClass<QMdiSubWindow, QWidget>("QMdiSubWindow")
Expand Down Expand Up @@ -148,14 +149,20 @@ void addLCBindings(lua_State *L) {
CadMdiChild* child = new CadMdiChild;
return child;
})
.addFunction("cursor", &CadMdiChild::cursor)
.addFunction("document", &CadMdiChild::document)
.addProperty("id", &CadMdiChild::id, &CadMdiChild::setId)
.addFunction("import", &CadMdiChild::import)
.addFunction("newDocument", &CadMdiChild::newDocument)
.addFunction("undoManager", &CadMdiChild::undoManager)
.addFunction("view", &CadMdiChild::view)
.addFunction("viewer", &CadMdiChild::viewer)
.endClass()

.beginClass<LCViewer::Cursor>("Cursor")
.addFunction("position", &LCViewer::Cursor::position)
.endClass()

.beginExtendClass<LCViewer::LCADViewer, QWidget>("LCADViewer")
.addFunction("x", &LCViewer::LCADViewer::x)
.addFunction("y", &LCViewer::LCADViewer::y)
Expand Down
37 changes: 22 additions & 15 deletions lcUILua/actions/event.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
local event = {}
event = {}

function event.register(eventName, callback, ...)
event[eventName] = {}
event[eventName].callback = callback
if(... ~= nil) then
event[eventName].args = {...}
function event.register(eventName, callback)
if(event[eventName] == nil) then
event[eventName] = {}
event[eventName].callbacks = {callback}
end

table.insert(event[eventName].callbacks, callback)
end

function event.trigger(eventName, ...)
if(event[eventName]) then
if(event[eventName].args) then
event[eventName].callback(event[eventName].args, {...})
else
event[eventName].callback({...})
for i, cb in pairs(event[eventName].callbacks) do
if type(cb) == "function" then
cb(...)
elseif cb.onEvent then
cb:onEvent(eventName, ...)
end
end
end
end

function event.delete(eventName)
event[eventName] = nil
end

return event
function event.delete(eventName, callback)
if(event[eventName]) then
for i, cb in pairs(event[eventName].callbacks) do
if(callback == cb) then
table.remove(event[eventName].callbacks, i)
end
end
end
end
60 changes: 37 additions & 23 deletions lcUILua/actions/operations.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
local event = require 'actions.event'
require 'actions.event'

local operations = {}
Operations = {}

function operations.create_line()
print("Draw a line")
print("Click on first point")
event.register('point', operations.get_line_point1, nil)
function Operations:new()
local o = {}
Operations.__index = self
setmetatable(o, self)
return o
end

function operations.get_line_point1(point)
event.delete('point')
event.register('point', operations.get_line_point2, point[1], point[2])
print("Click on second point")
function Operations:onEvent(eventName, ...)
--If current window is focused
if(mdiArea:activeSubWindow():widget().id == self.id) then
if(eventName == "point") then
self:get_line_point(...)
end
end
end

function operations.get_line_point2(point1, point2)
d = mdiArea:activeSubWindow():widget():document()
b = Builder(d)
layer = d:layerByName("0")
function Operations:create_line()
self.line_vars = {}
self.line_vars['lastPoint'] = nil

l=Line(Coord(point1[1],point1[2]), Coord(point2[1],point2[2]), layer);
b:append(l)
b:execute()
event.delete('point')
end

function operations.click(x,y)
event.trigger('point', x, y)
print("Draw a line")
print("Click on first point")
event.register('point', self)
end

return operations
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 b = Builder(d)
local layer = d:layerByName("0")
local l=Line(Coord(lp[1], lp[2]), Coord(point[1], point[2]), layer);

b:append(l)
b:execute()

event.delete('point', self)
else
self.line_vars['lastPoint'] = point
print("Click on second point")
end
end
38 changes: 29 additions & 9 deletions lcUILua/ui/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
operations = require 'actions.operations'
require 'actions.operations'

op = {}

function new_file()
cadMdiChild = lc.CadMdiChild()
cadMdiChild:newDocument()
mdiArea:addSubWindow(cadMdiChild)
window = mdiArea:addSubWindow(cadMdiChild)
cadMdiChild:showMaximized()
cadMdiChild:viewer():autoScale()

luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", "click")
luaInterface:luaConnect(cadMdiChild:view(), "destroyed()", "onMdiChildDestroyed")

id = #op + 1
op[id] = Operations:new()
op[id].id = id
window:widget().id = id
end

function open_file()
Expand Down Expand Up @@ -41,18 +50,29 @@ end
function load_file(fileName)
cadMdiChild = lc.CadMdiChild()
cadMdiChild:import(fileName:toStdString())
mdiArea:addSubWindow(cadMdiChild)
window = mdiArea:addSubWindow(cadMdiChild)
cadMdiChild:showMaximized()

luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", "click")
luaInterface:luaConnect(cadMdiChild:view(), "destroyed()", "onMdiChildDestroyed")

id = #op + 1
op[id] = Operations:new()
op[id].id = id
window:widget().id = id
end

function click()
local view = mdiArea:activeSubWindow():widget():view()
local x = view:x()
local y = view:y()
local widget = mdiArea:activeSubWindow():widget()
local position = widget:cursor():position()
local x = position:x()
local y = position:y()

event.trigger('point', {x, y})
end

operations.click(x, y)
function create_line()
local widget = mdiArea:activeSubWindow():widget()
op[widget.id]:create_line()
end

--UI
Expand All @@ -77,7 +97,7 @@ luaInterface:luaConnect(mainWindow:findChild("actionNew"), "triggered(bool)", "n
luaInterface:luaConnect(mainWindow:findChild("actionOpen"), "triggered(bool)", "open_file")
luaInterface:luaConnect(mainWindow:findChild("actionUndo"), "triggered(bool)", "undo")
luaInterface:luaConnect(mainWindow:findChild("actionRedo"), "triggered(bool)", "redo")
luaInterface:luaConnect(lineAction, "triggered(bool)", "operations.create_line")
luaInterface:luaConnect(lineAction, "triggered(bool)", "create_line")

luaScript = lc.LuaScript(mdiArea)
mainWindow:addDockWidget(2, luaScript)
Expand Down
4 changes: 4 additions & 0 deletions lcviewernoqt/drawables/lccursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ void Cursor::onSnapPointEvent(SnapPointEvent const & event) {
Nano::Signal<void(const LocationEvent&)> Cursor::locationEvents() const {
return _locationEvent;
}

lc::geo::Coordinate Cursor::position() const {
return lc::geo::Coordinate(_lastSnapEvent.snapPoint().x(), _lastSnapEvent.snapPoint().y());
}
2 changes: 2 additions & 0 deletions lcviewernoqt/drawables/lccursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace LCViewer {

void onSnapPointEvent(SnapPointEvent const & event);

lc::geo::Coordinate position() const;

Nano::Signal<void(const LocationEvent &)> locationEvents() const;

private:
Expand Down
3 changes: 3 additions & 0 deletions lcviewernoqt/managers/snapmanagerimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ void SnapManagerImpl::setDeviceLocation(int x, int y) {
}
}

//If no snap points found show cursor at mouse pos
_snapPointEvent(SnapPointEvent(location));

// FIXME: Currently sending a snapEvent so the cursor get's updated, what we really want is some sort of a release snap event
// but only when we had a snap, but just lost it
//SnapPointEvent snapEvent(lc::geo::Coordinate(event.mousePosition().x(), event.mousePosition().y()));
Expand Down
2 changes: 1 addition & 1 deletion lcviewerqt/lcadviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LCADViewer::LCADViewer(QWidget *parent) :

setMouseTracking(true);
this->_altKeyActive = false;

setCursor(Qt::BlankCursor);
}

LCADViewer::~LCADViewer() {
Expand Down