Move setLayerVisibility() and setLayerOpacity() into Drawing and some tests

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1943 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Jeff Schiller 2011-01-28 07:39:30 +00:00
parent 4190990d69
commit 00d05776a2
5 changed files with 111 additions and 68 deletions

View File

@ -385,6 +385,37 @@ svgedit.draw.Drawing.prototype.getLayerVisibility = function(layername) {
return (layer.getAttribute('display') != 'none');
};
// Function: svgedit.draw.Drawing.setLayerVisibility
// Sets the visibility of the layer. If the layer name is not valid, this function return
// false, otherwise it returns true. This is an undo-able action.
//
// Parameters:
// layername - the name of the layer to change the visibility
// bVisible - true/false, whether the layer should be visible
//
// Returns:
// The SVGGElement representing the layer if the layername was valid, otherwise null.
svgedit.draw.Drawing.prototype.setLayerVisibility = function(layername, bVisible) {
if (typeof bVisible != typeof true) {
return null;
}
// find the layer
var layer = null;
for (var i = 0; i < this.getNumLayers(); ++i) {
if (this.getLayerName(i) == layername) {
layer = this.all_layers[i][1];
break;
}
}
if (!layer) return null;
var oldDisplay = layer.getAttribute("display");
if (!oldDisplay) oldDisplay = "inline";
layer.setAttribute("display", bVisible ? "inline" : "none");
return layer;
};
// Function: svgedit.draw.Drawing.getLayerOpacity
// Returns the opacity of the given layer. If the input name is not a layer, null is returned.
//
@ -408,4 +439,24 @@ svgedit.draw.Drawing.prototype.getLayerOpacity = function(layername) {
return null;
};
// Function: svgedit.draw.Drawing.setLayerOpacity
// Sets the opacity of the given layer. If the input name is not a layer, nothing happens.
// If opacity is not a value between 0.0 and 1.0, then nothing happens.
//
// Parameters:
// layername - name of the layer on which to set the opacity
// opacity - a float value in the range 0.0-1.0
svgedit.draw.Drawing.prototype.setLayerOpacity = function(layername, opacity) {
if (typeof opacity != typeof 1.0 || opacity < 0.0 || opacity > 1.0) {
return;
}
for (var i = 0; i < this.getNumLayers(); ++i) {
if (this.getLayerName(i) == layername) {
var g = this.all_layers[i][1];
g.setAttribute("opacity", opacity);
break;
}
}
};
})();

View File

@ -66,13 +66,13 @@ function embedded_svg_edit(frame){
//Run in firebug on http://svg-edit.googlecode.com/svn/trunk/docs/files/svgcanvas-js.html
//for(var i=0,q=[],f = document.querySelectorAll("div.CFunction h3.CTitle a");i<f.length;i++){q.push(f[i].name)};q
//var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getCurrentLayerName", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear"];
//var functions = ["clearSelection", "addToSelection", "removeFromSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getCurrentLayerName", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility", "moveSelectedToLayer", "clear"];
//Newer, well, it extracts things that aren't documented as well. All functions accessible through the normal thingy can now be accessed though the API
//var l=[];for(var i in svgCanvas){if(typeof svgCanvas[i] == "function"){l.push(i)}};
//run in svgedit itself
var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection", "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getCurrentLayerName", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "getLayerVisibility", "setLayerVisibility", "moveSelectedToLayer", "getLayerOpacity", "setLayerOpacity", "clear", "clearPath", "getNodePoint", "clonePathNode", "deletePathNode", "getResolution", "getImageTitle", "setImageTitle", "setResolution", "setBBoxZoom", "setZoom", "getMode", "setMode", "getStrokeColor", "setStrokeColor", "getFillColor", "setFillColor", "setStrokePaint", "setFillPaint", "getStrokeWidth", "setStrokeWidth", "getStrokeStyle", "setStrokeStyle", "getOpacity", "setOpacity", "getFillOpacity", "setFillOpacity", "getStrokeOpacity", "setStrokeOpacity", "getTransformList", "getBBox", "getRotationAngle", "setRotationAngle", "each", "bind", "setIdPrefix", "getBold", "setBold", "getItalic", "setItalic", "getFontFamily", "setFontFamily", "getFontSize", "setFontSize", "getText", "setTextContent", "setImageURL", "setRectRadius", "setSegType", "quickClone", "changeSelectedAttributeNoUndo", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements", "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements", "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText", "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion", "setIconSize", "setLang", "setCustomHandlers"]
var functions = ["updateElementFromJson", "embedImage", "fixOperaXML", "clearSelection", "addToSelection", "removeFromSelection", "addNodeToSelection", "open", "save", "getSvgString", "setSvgString", "createLayer", "deleteCurrentLayer", "getCurrentLayerName", "setCurrentLayer", "renameCurrentLayer", "setCurrentLayerPosition", "setLayerVisibility", "moveSelectedToLayer", "clear", "clearPath", "getNodePoint", "clonePathNode", "deletePathNode", "getResolution", "getImageTitle", "setImageTitle", "setResolution", "setBBoxZoom", "setZoom", "getMode", "setMode", "getStrokeColor", "setStrokeColor", "getFillColor", "setFillColor", "setStrokePaint", "setFillPaint", "getStrokeWidth", "setStrokeWidth", "getStrokeStyle", "setStrokeStyle", "getOpacity", "setOpacity", "getFillOpacity", "setFillOpacity", "getStrokeOpacity", "setStrokeOpacity", "getTransformList", "getBBox", "getRotationAngle", "setRotationAngle", "each", "bind", "setIdPrefix", "getBold", "setBold", "getItalic", "setItalic", "getFontFamily", "setFontFamily", "getFontSize", "setFontSize", "getText", "setTextContent", "setImageURL", "setRectRadius", "setSegType", "quickClone", "changeSelectedAttributeNoUndo", "changeSelectedAttribute", "deleteSelectedElements", "groupSelectedElements", "ungroupSelectedElement", "moveToTopSelectedElement", "moveToBottomSelectedElement", "moveSelectedElements", "getStrokedBBox", "getVisibleElements", "cycleElement", "getUndoStackSize", "getRedoStackSize", "getNextUndoCommandText", "getNextRedoCommandText", "undo", "redo", "cloneSelectedElements", "alignSelectedElements", "getZoom", "getVersion", "setIconSize", "setLang", "setCustomHandlers"]
//TODO: rewrite the following, it's pretty scary.
for(var i = 0; i < functions.length; i++){

View File

@ -3756,13 +3756,13 @@
if (layerNameToHighlight) {
for (var i = 0; i < curNames.length; ++i) {
if (curNames[i] != layerNameToHighlight) {
svgCanvas.setLayerOpacity(curNames[i], 0.5);
svgCanvas.getCurrentDrawing().setLayerOpacity(curNames[i], 0.5);
}
}
}
else {
for (var i = 0; i < curNames.length; ++i) {
svgCanvas.setLayerOpacity(curNames[i], 1.0);
svgCanvas.getCurrentDrawing().setLayerOpacity(curNames[i], 1.0);
}
}
};
@ -3785,7 +3785,7 @@
}
appendstr += "\">";
if (svgCanvas.getLayerVisibility(name)) {
if (svgCanvas.getCurrentDrawing().getLayerVisibility(name)) {
appendstr += "<td class=\"layervis\"/><td class=\"layername\" >" + name + "</td></tr>";
}
else {

View File

@ -7151,19 +7151,6 @@ this.setCurrentLayerPosition = function(newpos) {
return false;
};
// Function: getLayerVisibility
// Returns whether the layer is visible. If the layer name is not valid, then this function
// returns false.
//
// Parameters:
// layername - the name of the layer which you want to query.
//
// Returns:
// The visibility state of the layer, or false if the layer name was invalid.
this.getLayerVisibility = function(layername) {
return getCurrentDrawing().getLayerVisibility(layername);
};
// Function: setLayerVisibility
// Sets the visibility of the layer. If the layer name is not valid, this function return
// false, otherwise it returns true. This is an undo-able action.
@ -7175,27 +7162,21 @@ this.getLayerVisibility = function(layername) {
// Returns:
// true if the layer's visibility was set, false otherwise
this.setLayerVisibility = function(layername, bVisible) {
// find the layer
var layer = null;
for (var i = 0; i < current_drawing.getNumLayers(); ++i) {
if (current_drawing.getLayerName(i) == layername) {
layer = current_drawing.all_layers[i][1];
break;
var drawing = getCurrentDrawing();
var prevVisibility = drawing.getLayerVisibility(layername);
var layer = drawing.setLayerVisibility(layername, bVisible);
if (layer) {
var oldDisplay = prevVisibility ? 'inline' : 'none';
addCommandToHistory(new ChangeElementCommand(layer, {'display':oldDisplay}, 'Layer Visibility'));
} else {
return false;
}
}
if (!layer) return false;
var oldDisplay = layer.getAttribute("display");
if (!oldDisplay) oldDisplay = "inline";
layer.setAttribute("display", bVisible ? "inline" : "none");
addCommandToHistory(new ChangeElementCommand(layer, {"display":oldDisplay}, "Layer Visibility"));
if (layer == current_drawing.current_layer) {
if (layer == drawing.getCurrentLayer()) {
clearSelection();
pathActions.clear();
}
// call("changed", [selected]);
return true;
};
@ -7290,40 +7271,6 @@ this.mergeAllLayers = function() {
addCommandToHistory(batchCmd);
}
// Function: getLayerOpacity
// Returns the opacity of the given layer. If the input name is not a layer, null is returned.
//
// Parameters:
// layername - name of the layer on which to get the opacity
//
// Returns:
// The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null
// if layername is not a valid layer
this.getLayerOpacity = function(layername) {
return getCurrentDrawing().getLayerOpacity(layername);
};
// Function: setLayerOpacity
// Sets the opacity of the given layer. If the input name is not a layer, nothing happens.
// This is not an undo-able action. NOTE: this function exists solely to apply
// a highlighting/de-emphasis effect to a layer, when it is possible for a user to affect
// the opacity of a layer, we will need to allow this function to produce an undo-able action.
// If opacity is not a value between 0.0 and 1.0, then nothing happens.
//
// Parameters:
// layername - name of the layer on which to set the opacity
// opacity - a float value in the range 0.0-1.0
this.setLayerOpacity = function(layername, opacity) {
if (opacity < 0.0 || opacity > 1.0) return;
for (var i = 0; i < current_drawing.getNumLayers(); ++i) {
if (current_drawing.getLayerName(i) == layername) {
var g = current_drawing.all_layers[i][1];
g.setAttribute("opacity", opacity);
break;
}
}
};
// Function: leaveContext
// Return from a group context to the regular kind, make any previously
// disabled elements enabled again

View File

@ -393,6 +393,28 @@
ok(drawing.getLayerVisibility(LAYER3));
});
test('Test setLayerVisibility()', function() {
expect(6);
var drawing = new svgedit.draw.Drawing(svg);
setupSvgWith3Layers(svg);
drawing.identifyLayers();
ok(drawing.setLayerVisibility);
equals(typeof drawing.setLayerVisibility, typeof function(){});
drawing.setLayerVisibility(LAYER3, false);
drawing.setLayerVisibility(LAYER2, true);
drawing.setLayerVisibility(LAYER1, false);
ok(!drawing.getLayerVisibility(LAYER1));
ok(drawing.getLayerVisibility(LAYER2));
ok(!drawing.getLayerVisibility(LAYER3));
drawing.setLayerVisibility(LAYER3, 'test-string');
ok(!drawing.getLayerVisibility(LAYER3));
});
test('Test getLayerOpacity()', function() {
expect(5);
@ -407,6 +429,29 @@
ok(drawing.getLayerOpacity(LAYER3) == 1.0);
});
test('Test setLayerOpacity()', function() {
expect(6);
var drawing = new svgedit.draw.Drawing(svg);
setupSvgWith3Layers(svg);
drawing.identifyLayers();
ok(drawing.setLayerOpacity);
equals(typeof drawing.setLayerOpacity, typeof function(){});
drawing.setLayerOpacity(LAYER1, 0.4);
drawing.setLayerOpacity(LAYER2, 'invalid-string');
drawing.setLayerOpacity(LAYER3, -1.4);
ok(drawing.getLayerOpacity(LAYER1) == 0.4);
QUnit.log('layer2 opacity', drawing.getLayerOpacity(LAYER2));
ok(drawing.getLayerOpacity(LAYER2) == 1.0);
ok(drawing.getLayerOpacity(LAYER3) == 1.0);
drawing.setLayerOpacity(LAYER3, 100);
ok(drawing.getLayerOpacity(LAYER3) == 1.0);
});
});
</script>
</head>