Skip to content
Snippets Groups Projects
Commit d0ad479c authored by srosse's avatar srosse
Browse files

OO-3921: add to drawing same resize option as in hotspot

parent 8cc2577b
No related branches found
No related tags found
No related merge requests found
/**
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at the
* <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Initial code contributed and copyrighted by<br>
* frentix GmbH, http://www.frentix.com
* <p>
*/
package org.olat.ims.qti21.ui.editor.interactions;
/**
*
* Initial date: 4 mars 2019<br>
* @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
*
*/
public enum BackgroundSize {
s1024(1024),
s800(800),
s480(480);
private final int size;
private BackgroundSize(int size) {
this.size = size;
}
public int size() {
return size;
}
}
......@@ -34,6 +34,7 @@ import org.olat.core.gui.components.form.flexible.FormItem;
import org.olat.core.gui.components.form.flexible.FormItemContainer;
import org.olat.core.gui.components.form.flexible.elements.FileElement;
import org.olat.core.gui.components.form.flexible.elements.RichTextElement;
import org.olat.core.gui.components.form.flexible.elements.SingleSelection;
import org.olat.core.gui.components.form.flexible.elements.TextElement;
import org.olat.core.gui.components.form.flexible.impl.FormBasicController;
import org.olat.core.gui.components.form.flexible.impl.FormEvent;
......@@ -42,6 +43,7 @@ import org.olat.core.gui.components.form.flexible.impl.elements.FileElementEvent
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.util.CodeHelper;
import org.olat.core.util.FileUtils;
import org.olat.core.util.StringHelper;
import org.olat.core.util.Util;
import org.olat.core.util.ValidationStatus;
......@@ -74,6 +76,7 @@ public class DrawingEditorController extends FormBasicController {
private TextElement titleEl;
private RichTextElement textEl;
private SingleSelection resizeEl;
private FileElement backgroundEl;
private final File itemFile;
......@@ -144,6 +147,15 @@ public class DrawingEditorController extends FormBasicController {
backgroundEl.setDeleteEnabled(true);
backgroundEl.limitToMimeType(mimeTypes, "error.mimetype", new String[]{ mimeTypes.toString() });
String[] resizeKeys = new String[] { "no" };
String[] resizeValues = new String[] { translate("form.imd.background.resize.no") };
resizeEl = uifactory.addRadiosHorizontal("form.imd.background.resize", formLayout, resizeKeys, resizeValues);
resizeEl.setVisible(false);
resizeEl.setEnabled(!readOnly);
if(initialBackgroundImage != null) {
Size size = imageService.getSize(new LocalFileImpl(initialBackgroundImage), null);
optimizeResizeEl(size, false);
}
// Submit Button
FormLayoutContainer buttonsContainer = FormLayoutContainer.createButtonLayout("buttons", getTranslator());
......@@ -222,6 +234,8 @@ public class DrawingEditorController extends FormBasicController {
backgroundEl.setUploadFileName(uniqueFilename);
backgroundImage = backgroundEl.moveUploadFileTo(itemFile.getParentFile());
backgroundEl.setInitialFile(backgroundImage);
Size size = imageService.getSize(new LocalFileImpl(backgroundImage), null);
optimizeResizeEl(size, true);
}
}
}
......@@ -244,7 +258,18 @@ public class DrawingEditorController extends FormBasicController {
if(objectImg != null) {
String filename = objectImg.getName();
String mimeType = WebappHelper.getMimeType(filename);
Size size = imageService.getSize(new LocalFileImpl(objectImg), null);
Size currentSize = imageService.getSize(new LocalFileImpl(objectImg), null);
Size size = currentSize;
if(resizeEl.isVisible() && !resizeEl.isSelected(0)) {
int maxSize = Integer.parseInt(resizeEl.getSelectedKey());
if(maxSize < currentSize.getHeight() || maxSize < currentSize.getWidth()) {
String extension = FileUtils.getFileSuffix(filename);
size = imageService.scaleImage(objectImg, extension, objectImg, maxSize, maxSize, false);
optimizeResizeEl(size, false);
}
}
int height = -1;
int width = -1;
if(size != null) {
......@@ -260,4 +285,42 @@ public class DrawingEditorController extends FormBasicController {
fireEvent(ureq, new AssessmentItemEvent(AssessmentItemEvent.ASSESSMENT_ITEM_CHANGED, itemBuilder.getAssessmentItem(), QTI21QuestionType.drawing));
}
private void optimizeResizeEl(Size size, boolean selectSize) {
List<String> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
String selectedSize = null;
for(BackgroundSize availableSize:BackgroundSize.values()) {
int proposedSize = availableSize.size();
if(proposedSize <= size.getHeight() || proposedSize <= size.getWidth()) {
String s = Integer.toString(availableSize.size());
keys.add(s);
values.add(s + " x " + s);
if((proposedSize == size.getHeight() && proposedSize >= size.getWidth())
|| (proposedSize == size.getWidth() && proposedSize >= size.getHeight())) {
selectedSize = s;
}
}
}
if(selectedSize == null) {
keys.add(0, "no");
values.add(0, translate("form.imd.background.resize.no"));
}
resizeEl.setKeysAndValues(keys.toArray(new String[keys.size()]), values.toArray(new String[values.size()]), null);
if(keys.size() == 1) {
resizeEl.select(keys.get(0), true);
resizeEl.setVisible(false);
} else {
if(selectedSize != null) {
resizeEl.select(selectedSize, true);
} else if(selectSize && keys.size() > 1 && keys.get(1).equals(Integer.toString(BackgroundSize.s1024.size()))) {
resizeEl.select(Integer.toString(BackgroundSize.s1024.size()), true);
} else {
resizeEl.select(keys.get(0), true);
}
resizeEl.setVisible(true);
}
}
}
\ No newline at end of file
......@@ -598,8 +598,8 @@ public class HotspotEditorController extends FormBasicController {
} else {
if(selectedSize != null) {
resizeEl.select(selectedSize, true);
} else if(selectSize && keys.size() > 1 && keys.get(1).equals(Integer.toString(BackgroundSize.s1024.size))) {
resizeEl.select(Integer.toString(BackgroundSize.s1024.size), true);
} else if(selectSize && keys.size() > 1 && keys.get(1).equals(Integer.toString(BackgroundSize.s1024.size()))) {
resizeEl.select(Integer.toString(BackgroundSize.s1024.size()), true);
} else {
resizeEl.select(keys.get(0), true);
}
......@@ -779,20 +779,4 @@ public class HotspotEditorController extends FormBasicController {
coords.set(3, (bottomY - translateY));
}
}
public enum BackgroundSize {
s1024(1024),
s800(800),
s480(480);
private final int size;
private BackgroundSize(int size) {
this.size = size;
}
public int size() {
return size;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment