Skip to content

Commit

Permalink
Add filter execution events. Fixes #402.
Browse files Browse the repository at this point in the history
Trying out a new listener handler class.
  • Loading branch information
ivmartel committed Sep 27, 2017
1 parent 17f84b5 commit a76aa8b
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ dwv.App = function ()
}
}
toolList.Filter = new dwv.tool.Filter(filterList, this);
toolList.Filter.addEventListener("filter-run", fireEvent);
toolList.Filter.addEventListener("filter-undo", fireEvent);
}
}
else {
Expand Down
159 changes: 158 additions & 1 deletion src/tools/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ dwv.tool.Filter = function ( filterList, app )
* @type Boolean
*/
this.displayed = false;
/**
* Listener handler.
* @type Object
*/
var listenerHandler = new dwv.utils.ListenerHandler();

/**
* Setup the filter GUI. Called at app startup.
Expand All @@ -48,6 +53,8 @@ dwv.tool.Filter = function ( filterList, app )
gui.setup(this.filterList);
for( var key in this.filterList ){
this.filterList[key].setup();
this.filterList[key].addEventListener("filter-run", fireEvent);
this.filterList[key].addEventListener("filter-undo", fireEvent);
}
}
};
Expand Down Expand Up @@ -97,6 +104,32 @@ dwv.tool.Filter = function ( filterList, app )
app.onKeydown(event);
};

/**
* Add an event listener to this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type,
* will be called with the fired event.
*/
this.addEventListener = function (type, callback) {
listenerHandler.add(type, callback);
};
/**
* Remove an event listener from this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type.
*/
this.removeEventListener = function (type, callback) {
listenerHandler.remove(type, callback);
};
/**
* Fire an event: call all associated listeners with the input event object.
* @param {Object} event The event to fire.
* @private
*/
function fireEvent (event) {
listenerHandler.fireEvent(event);
}

}; // class dwv.tool.Filter

/**
Expand Down Expand Up @@ -186,6 +219,11 @@ dwv.tool.filter.Threshold = function ( app )
* @type Boolean
*/
var resetImage = true;
/**
* Listener handler.
* @type Object
*/
var listenerHandler = new dwv.utils.ListenerHandler();

/**
* Setup the filter GUI. Called at app startup.
Expand Down Expand Up @@ -230,11 +268,39 @@ dwv.tool.filter.Threshold = function ( app )
resetImage = false;
}
var command = new dwv.tool.RunFilterCommand(filter, app);
command.onExecute = fireEvent;
command.onUndo = fireEvent;
command.execute();
// save command in undo stack
app.addToUndoStack(command);
};

/**
* Add an event listener to this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type,
* will be called with the fired event.
*/
this.addEventListener = function (type, callback) {
listenerHandler.add(type, callback);
};
/**
* Remove an event listener from this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type.
*/
this.removeEventListener = function (type, callback) {
listenerHandler.remove(type, callback);
};
/**
* Fire an event: call all associated listeners with the input event object.
* @param {Object} event The event to fire.
* @private
*/
function fireEvent (event) {
listenerHandler.fireEvent(event);
}

}; // class dwv.tool.filter.Threshold


Expand All @@ -250,6 +316,11 @@ dwv.tool.filter.Sharpen = function ( app )
* @type Object
*/
var gui = new dwv.gui.Sharpen(app);
/**
* Listener handler.
* @type Object
*/
var listenerHandler = new dwv.utils.ListenerHandler();

/**
* Setup the filter GUI. Called at app startup.
Expand Down Expand Up @@ -285,11 +356,39 @@ dwv.tool.filter.Sharpen = function ( app )
var filter = new dwv.image.filter.Sharpen();
filter.setOriginalImage(app.getImage());
var command = new dwv.tool.RunFilterCommand(filter, app);
command.onExecute = fireEvent;
command.onUndo = fireEvent;
command.execute();
// save command in undo stack
app.addToUndoStack(command);
};

/**
* Add an event listener to this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type,
* will be called with the fired event.
*/
this.addEventListener = function (type, callback) {
listenerHandler.add(type, callback);
};
/**
* Remove an event listener from this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type.
*/
this.removeEventListener = function (type, callback) {
listenerHandler.remove(type, callback);
};
/**
* Fire an event: call all associated listeners with the input event object.
* @param {Object} event The event to fire.
* @private
*/
function fireEvent (event) {
listenerHandler.fireEvent(event);
}

}; // dwv.tool.filter.Sharpen

/**
Expand All @@ -304,6 +403,11 @@ dwv.tool.filter.Sobel = function ( app )
* @type Object
*/
var gui = new dwv.gui.Sobel(app);
/**
* Listener handler.
* @type Object
*/
var listenerHandler = new dwv.utils.ListenerHandler();

/**
* Setup the filter GUI. Called at app startup.
Expand Down Expand Up @@ -339,11 +443,39 @@ dwv.tool.filter.Sobel = function ( app )
var filter = new dwv.image.filter.Sobel();
filter.setOriginalImage(app.getImage());
var command = new dwv.tool.RunFilterCommand(filter, app);
command.onExecute = fireEvent;
command.onUndo = fireEvent;
command.execute();
// save command in undo stack
app.addToUndoStack(command);
};

/**
* Add an event listener to this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type,
* will be called with the fired event.
*/
this.addEventListener = function (type, callback) {
listenerHandler.add(type, callback);
};
/**
* Remove an event listener from this class.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type.
*/
this.removeEventListener = function (type, callback) {
listenerHandler.remove(type, callback);
};
/**
* Fire an event: call all associated listeners with the input event object.
* @param {Object} event The event to fire.
* @private
*/
function fireEvent (event) {
listenerHandler.fireEvent(event);
}

}; // class dwv.tool.filter.Sobel

/**
Expand All @@ -365,16 +497,41 @@ dwv.tool.RunFilterCommand = function (filter, app) {
*/
this.execute = function ()
{
// run filter and set app image
app.setImage(filter.update());
// update display
app.render();
// callback
this.onExecute({'type': 'filter-run', 'id': this.getName()});
};

/**
* Undo the command.
*/
this.undo = function () {
// reset the image
app.setImage(filter.getOriginalImage());
// update display
app.render();
// callback
this.onUndo({'type': 'filter-undo', 'id': this.getName()});
};

}; // RunFilterCommand class

/**
* Handle an execute event.
* @param {Object} event The execute event with type and id.
*/
dwv.tool.RunFilterCommand.prototype.onExecute = function (/*event*/)
{
// default does nothing.
};
/**
* Handle an undo event.
* @param {Object} event The undo event with type and id.
*/
dwv.tool.RunFilterCommand.prototype.onUndo = function (/*event*/)
{
// default does nothing.
};
67 changes: 67 additions & 0 deletions src/utils/listen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// namespaces
var dwv = dwv || {};
dwv.utils = dwv.utils || {};

/**
* ListenerHandler class: handles add/removing and firing listeners.
* @constructor
*/
dwv.utils.ListenerHandler = function ()
{
/**
* listeners.
* @private
*/
var listeners = {};

/**
* Add an event listener.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type,
* will be called with the fired event.
*/
this.add = function (type, callback)
{
// create array if not present
if ( typeof listeners[type] === "undefined" ) {
listeners[type] = [];
}
// add callback to listeners array
listeners[type].push(callback);
};

/**
* Remove an event listener.
* @param {String} type The event type.
* @param {Object} callback The method associated with the provided event type.
*/
this.remove = function (type, callback)
{
// check if the type is present
if( typeof listeners[type] === "undefined" ) {
return;
}
// remove from listeners array
for ( var i = 0; i < listeners[type].length; ++i ) {
if ( listeners[type][i] === callback ) {
listeners[type].splice(i,1);
}
}
};

/**
* Fire an event: call all associated listeners with the input event object.
* @param {Object} event The event to fire.
*/
this.fireEvent = function (event)
{
// check if they are listeners for the event type
if ( typeof listeners[event.type] === "undefined" ) {
return;
}
// fire events
for ( var i=0; i < listeners[event.type].length; ++i ) {
listeners[event.type][i](event);
}
};
};
2 changes: 2 additions & 0 deletions viewers/mobile/applauncher.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function startApp() {
//myapp.addEventListener("frame-change", listener);
//myapp.addEventListener("zoom-change", listener);
//myapp.addEventListener("offset-change", listener);
//myapp.addEventListener("filter-run", listener);
//myapp.addEventListener("filter-undo", listener);

// initialise the application
myapp.init({
Expand Down
1 change: 1 addition & 0 deletions viewers/mobile/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<script type="text/javascript" src="../../src/tools/zoomPan.js"></script>
<script type="text/javascript" src="../../src/utils/browser.js" data-cover></script>
<script type="text/javascript" src="../../src/utils/i18n.js"></script>
<script type="text/javascript" src="../../src/utils/listen.js"></script>
<script type="text/javascript" src="../../src/utils/progress.js"></script>
<script type="text/javascript" src="../../src/utils/string.js"></script>
<script type="text/javascript" src="../../src/utils/uri.js"></script>
Expand Down
1 change: 1 addition & 0 deletions viewers/simple/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<script type="text/javascript" src="../../src/tools/zoomPan.js"></script>
<script type="text/javascript" src="../../src/utils/browser.js"></script>
<script type="text/javascript" src="../../src/utils/i18n.js"></script>
<script type="text/javascript" src="../../src/utils/listen.js"></script>
<script type="text/javascript" src="../../src/utils/progress.js"></script>
<script type="text/javascript" src="../../src/utils/string.js"></script>
<script type="text/javascript" src="../../src/utils/uri.js"></script>
Expand Down
1 change: 1 addition & 0 deletions viewers/simplistic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<script type="text/javascript" src="../../src/utils/browser.js"></script>
<script type="text/javascript" src="../../src/utils/i18n.js"></script>
<script type="text/javascript" src="../../src/utils/progress.js"></script>
<script type="text/javascript" src="../../src/utils/listen.js"></script>
<script type="text/javascript" src="../../src/utils/string.js"></script>
<script type="text/javascript" src="../../src/utils/uri.js"></script>
<script type="text/javascript" src="../../src/utils/thread.js"></script>
Expand Down
1 change: 1 addition & 0 deletions viewers/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<script type="text/javascript" src="../../src/tools/zoomPan.js"></script>
<script type="text/javascript" src="../../src/utils/browser.js"></script>
<script type="text/javascript" src="../../src/utils/i18n.js"></script>
<script type="text/javascript" src="../../src/utils/listen.js"></script>
<script type="text/javascript" src="../../src/utils/progress.js"></script>
<script type="text/javascript" src="../../src/utils/string.js"></script>
<script type="text/javascript" src="../../src/utils/uri.js"></script>
Expand Down

0 comments on commit a76aa8b

Please sign in to comment.