Skip to content

Commit 8f37882

Browse files
committed
Make watermark text customizable
1 parent 499197c commit 8f37882

File tree

11 files changed

+107
-48
lines changed

11 files changed

+107
-48
lines changed
-232 KB
Binary file not shown.
-18.5 KB
Binary file not shown.
8.18 KB
Loading

app/imageviewer/doc/index.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ Overview
77
As the name suggests, the Image Viewer renders an image file at full resolution
88
in a separate tab in the main window. The tab title shows the image file name.
99

10-
By default a watermark is rendered over the image. The purpose is to avoid confusion in case the image shows a screenshot
11-
of an OPI screen. User may remove the watermark using the Watermark checkbox.
12-
13-
Using the "Scale to fit" button user may rescale the non-SVG images to fit to the current size of the view. The resizing
10+
Using the "Scale to fit" button user may rescale the images to fit to the current size of the view. The resizing
1411
process will honor the aspect ratio of the image, i.e. the resulting view may show empty space
1512
above/below the image, or to left/right of the image. Pressing the button again will switch back to showing the
1613
image at full resolution. It should be noted that if the image is smaller than the current size of the view,
1714
pressing the "Scale to fit" button will zoom in on the image.
1815

19-
.. image:: images/example.png
16+
In some cases it may be necessary to indicate that the image is static in nature and cannot be interacted with, e.g.
17+
when showing a screen shot of an OPI. Ticking the Watermark check box will add an overlay showing the text "WATERMARK"
18+
(may be customized).
19+
20+
.. image:: images/toolbar.png
2021

2122
There is no menu entry for the application. It is launched automatically for instance
2223
when user double clicks on an image file in the File Browser, or when user clicks on
2324
an image in the log entry attachment viewer.
2425

2526
Supported file types are png, jpg (jpeg) and gif. The application also supports Scalable Vector Graphics
26-
files, but user should keep in mind that SVG tools may generate incompatible files.
27+
files, but user should keep in mind that SVG tools may generate incompatible files. When a SVG file is rendered
28+
by the application the "Scale to fit" button is disabled.
2729

2830
If user has configured an external application for a particular image file extension, this is
2931
NOT overridden by the Image Viewer application.

app/imageviewer/src/main/java/org/phoebus/applications/imageviewer/ImageViewerController.java

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
package org.phoebus.applications.imageviewer;
2020

2121
import javafx.beans.property.SimpleBooleanProperty;
22+
import javafx.beans.property.SimpleDoubleProperty;
2223
import javafx.beans.property.SimpleObjectProperty;
2324
import javafx.embed.swing.SwingFXUtils;
2425
import javafx.fxml.FXML;
2526
import javafx.scene.Node;
2627
import javafx.scene.control.Button;
28+
import javafx.scene.control.CheckBox;
2729
import javafx.scene.control.Label;
2830
import javafx.scene.control.ScrollPane;
2931
import javafx.scene.image.Image;
@@ -73,14 +75,19 @@ public class ImageViewerController {
7375
@FXML
7476
private BorderPane imageParent;
7577

78+
@FXML
79+
private CheckBox showWatermarkCheckBox;
80+
7681
private Image image;
7782

78-
private SimpleBooleanProperty isSVG = new SimpleBooleanProperty(false);
79-
private SimpleBooleanProperty fitToWindow = new SimpleBooleanProperty(false);
83+
private final SimpleBooleanProperty isSVG = new SimpleBooleanProperty(false);
84+
private final SimpleBooleanProperty fitToWindow = new SimpleBooleanProperty(false);
85+
86+
private final SimpleBooleanProperty showWatermarkProperty = new SimpleBooleanProperty(true);
8087

81-
private SimpleBooleanProperty showWatermarkProperty = new SimpleBooleanProperty(true);
88+
private final SimpleObjectProperty fontProperty = new SimpleObjectProperty();
8289

83-
private SimpleObjectProperty fontProperty = new SimpleObjectProperty();
90+
private final SimpleDoubleProperty rotationProperty = new SimpleDoubleProperty();
8491

8592
@FXML
8693
public void initialize() {
@@ -108,10 +115,12 @@ public void initialize() {
108115
fontProperty.set(new Font(20));
109116

110117
watermarkPane.visibleProperty().bind(showWatermarkProperty);
111-
watermarkText.fontProperty().bind(fontProperty);
112118

119+
watermarkText.textProperty().set(ImageViewerPreferences.watermark_text);
120+
watermarkText.fontProperty().bind(fontProperty);
113121
watermarkText.getStylesheets().addAll(getClass().getResource("/style.css").toExternalForm());
114122
watermarkText.getStyleClass().add("outline");
123+
watermarkText.rotateProperty().bind(rotationProperty);
115124
}
116125

117126
public Node getRoot() {
@@ -122,9 +131,11 @@ public Node getRoot() {
122131
* Sets the image in the view. Shows error dialog if the image file
123132
* cannot be loaded/rendered, e.g. non-image file or incompatible SVG.
124133
*
125-
* @param url
134+
* @param url URL to image resource
126135
*/
127-
public void setImage(URL url) {
136+
public void setImage(URL url, boolean showWatermark) {
137+
showWatermarkProperty.set(showWatermark);
138+
showWatermarkCheckBox.selectedProperty().set(showWatermark);
128139
try {
129140
if (url.toExternalForm().endsWith("svg")) {
130141
image = SVGTranscoder.loadSVG(url.openStream(), 0, 0);
@@ -164,15 +175,32 @@ private void scale() {
164175
imageView.setFitHeight(image.getHeight());
165176
}
166177
fontProperty.set(getFont());
178+
setRotation();
167179
}
168180

169181
public void toggleWatermark() {
170182
showWatermarkProperty.set(!showWatermarkProperty.getValue());
171183
}
172184

185+
/**
186+
* Computes a font size based on current size of image view and length of the watermark text.
187+
* In short: larger image view -> larger font, longer text -> smaller font.
188+
*
189+
* @return A bold {@link Font} using calculated size.
190+
*/
173191
private Font getFont() {
174192
double height = imageView.getFitHeight();
175193
double width = imageView.getFitWidth();
176-
return Font.font("Liberation Sans", FontWeight.BOLD, FontPosture.REGULAR, Math.min(width, height) / 10);
194+
int textLength = ImageViewerPreferences.watermark_text.length();
195+
double scalingFactor = textLength / 2.0;
196+
return Font.font("Liberation Sans", FontWeight.BOLD, FontPosture.REGULAR, Math.min(width, height) / scalingFactor);
197+
}
198+
199+
/**
200+
* Determines rotation of the watermark text depending on portrait or landscape orientation of
201+
* the image. If portrait the text is rotated more to better fit it on top ov the image.
202+
*/
203+
private void setRotation() {
204+
rotationProperty.set(imageView.getFitHeight() < imageView.getFitWidth() ? 25.0 : 60.0);
177205
}
178206
}

app/imageviewer/src/main/java/org/phoebus/applications/imageviewer/ImageViewerInstance.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,34 @@
1818

1919
package org.phoebus.applications.imageviewer;
2020

21-
import javafx.application.Platform;
2221
import javafx.fxml.FXMLLoader;
2322
import org.phoebus.framework.nls.NLS;
2423
import org.phoebus.framework.spi.AppDescriptor;
2524
import org.phoebus.framework.spi.AppInstance;
2625
import org.phoebus.ui.docking.DockItem;
2726
import org.phoebus.ui.docking.DockItemWithInput;
2827
import org.phoebus.ui.docking.DockPane;
29-
import org.phoebus.ui.docking.DockStage;
3028

31-
import java.io.IOException;
32-
import java.io.InputStream;
33-
import java.net.MalformedURLException;
3429
import java.net.URI;
35-
import java.net.URL;
3630
import java.util.ResourceBundle;
3731
import java.util.logging.Level;
3832
import java.util.logging.Logger;
3933

4034
public class ImageViewerInstance implements AppInstance {
4135

42-
public static final String NAME = "Image Viewer";
43-
44-
public enum ViewMode{
36+
public enum ViewMode {
4537
TAB,
4638
DIALOG
47-
};
39+
}
4840

4941
private DockItem dockItem;
5042

51-
private AppDescriptor appDescriptor;
43+
private final AppDescriptor appDescriptor;
5244

53-
public ImageViewerInstance(AppDescriptor appDescriptor, URI uri, ViewMode viewMode){
45+
public ImageViewerInstance(AppDescriptor appDescriptor, URI uri, ViewMode viewMode) {
5446
this.appDescriptor = appDescriptor;
5547

56-
if(viewMode == ViewMode.TAB){
57-
final DockItemWithInput existing = DockStage.getDockItemWithInput(NAME, uri);
48+
if (viewMode == ViewMode.TAB) {
5849
FXMLLoader fxmlLoader = new FXMLLoader();
5950
ResourceBundle resourceBundle = NLS.getMessages(Messages.class);
6051
fxmlLoader.setResources(resourceBundle);
@@ -64,28 +55,25 @@ public ImageViewerInstance(AppDescriptor appDescriptor, URI uri, ViewMode viewMo
6455
ImageViewerController imageViewerController = fxmlLoader.getController();
6556
dockItem = new DockItemWithInput(this, imageViewerController.getRoot(), uri, null, null);
6657
DockPane.getActiveDockPane().addTab(dockItem);
67-
Platform.runLater(() -> {
68-
try {
69-
imageViewerController.setImage(uri.toURL());
70-
} catch (MalformedURLException e) {
71-
Logger.getLogger(ImageViewerInstance.class.getName())
72-
.log(Level.WARNING, "Unable to set image", e);
73-
}
74-
});
58+
boolean showWatermark = false;
59+
String queryParams = uri.getQuery();
60+
if (queryParams != null && queryParams.contains("watermark=true")) {
61+
showWatermark = true;
62+
}
63+
imageViewerController.setImage(uri.toURL(), showWatermark);
7564
} catch (Exception e) {
7665
Logger.getLogger(ImageViewerInstance.class.getName())
77-
.log(Level.WARNING, "Unable to load fxml", e);
66+
.log(Level.WARNING, "Unable to load fxml", e);
7867
}
7968
}
8069
}
8170

8271
@Override
83-
public AppDescriptor getAppDescriptor()
84-
{
72+
public AppDescriptor getAppDescriptor() {
8573
return appDescriptor;
8674
}
8775

88-
public void raise(){
76+
public void raise() {
8977
dockItem.select();
9078
}
9179
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Oak Ridge National Laboratory.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*******************************************************************************/
8+
package org.phoebus.applications.imageviewer;
9+
10+
import org.phoebus.framework.preferences.AnnotatedPreferences;
11+
import org.phoebus.framework.preferences.Preference;
12+
13+
/**
14+
* Preference settings for Image Viewer app
15+
*
16+
* @author Georg Weiss
17+
*/
18+
@SuppressWarnings("nls")
19+
public class ImageViewerPreferences {
20+
@Preference
21+
public static String watermark_text;
22+
23+
static {
24+
AnnotatedPreferences.initialize(ImageViewerPreferences.class, "/image_viewer_preferences.properties");
25+
}
26+
}

app/imageviewer/src/main/resources/ImageViewer.fxml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<ToolBar>
3434
<Button fx:id="scaleToFitButton" prefWidth="130.0" text="%ScaleToFit">
3535
</Button>
36-
<CheckBox fx:id="showWatermark" onAction="#toggleWatermark" prefWidth="130.0" selected="true"
36+
<CheckBox fx:id="showWatermarkCheckBox" onAction="#toggleWatermark" prefWidth="130.0" selected="true"
3737
text="%watermarkButton">
3838
<padding>
3939
<Insets left="10.0"/>
@@ -43,16 +43,14 @@
4343

4444
</top>
4545
<center>
46-
4746
<ScrollPane fx:id="scrollPane">
4847
<BorderPane fx:id="imageParent">
4948
<center>
5049
<StackPane fx:id="stackPane">
5150
<ImageView fx:id="imageView"/>
5251
<BorderPane fx:id="watermarkPane">
5352
<center>
54-
<Label fx:id="watermarkText" rotate="25.0" style="-fx-text-fill: #AAAAAA90;"
55-
text="%watermarkText"/>
53+
<Label fx:id="watermarkText" rotate="25.0" style="-fx-text-fill: #AAAAAA90;"/>
5654
</center>
5755
</BorderPane>
5856
</StackPane>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# --------------------------------------------
2+
# Package org.phoebus.applications.imageviewer
3+
# --------------------------------------------
4+
5+
# Watermark text
6+
watermark_text=W A T E R M A R K

app/imageviewer/src/main/resources/org/phoebus/applications/imageviewer/messages.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ ErrorDialogText=Failed to load image resource {0}
2121
OneHundredPercent=Show 100%
2222
ScaleToFit=Scale to fit
2323
watermarkButton=Watermark
24-
watermarkText=W A T E R M A R K

0 commit comments

Comments
 (0)