Skip to content

Commit

Permalink
display image files in presentation #232
Browse files Browse the repository at this point in the history
mostly contributed by @l-spiecker: restructure songs containing (possibly rotated) images so that MQTT can work with it better
  • Loading branch information
mathisdt committed Feb 26, 2024
1 parent 3943976 commit d130a05
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 123 deletions.
40 changes: 15 additions & 25 deletions src/main/java/org/zephyrsoft/sdb2/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,7 @@
*/
package org.zephyrsoft.sdb2.gui;

import static org.zephyrsoft.sdb2.model.VirtualScreen.SCREEN_A;
import static org.zephyrsoft.sdb2.model.VirtualScreen.SCREEN_B;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
Expand All @@ -53,6 +36,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Consumer;
Expand Down Expand Up @@ -81,7 +65,6 @@
import org.zephyrsoft.sdb2.model.AddressablePart;
import org.zephyrsoft.sdb2.model.ExportFormat;
import org.zephyrsoft.sdb2.model.FilterTypeEnum;
import org.zephyrsoft.sdb2.model.ImageSong;
import org.zephyrsoft.sdb2.model.ScreenContentsEnum;
import org.zephyrsoft.sdb2.model.SelectableDisplay;
import org.zephyrsoft.sdb2.model.Song;
Expand Down Expand Up @@ -120,6 +103,9 @@

import say.swing.JFontChooser;

import static org.zephyrsoft.sdb2.model.VirtualScreen.SCREEN_A;
import static org.zephyrsoft.sdb2.model.VirtualScreen.SCREEN_B;

/**
* Main window of the application.
*/
Expand Down Expand Up @@ -1343,7 +1329,10 @@ protected void handleAddImage() {
try {
File[] selectedFiles = chooser.getSelectedFiles();
for (File selectedFile : selectedFiles) {
presentModel.addSong(new ImageSong(selectedFile));
Song imageSong = new Song(UUID.randomUUID().toString());
imageSong.setTitle(selectedFile.getName());
imageSong.setImage(selectedFile.getAbsoluteFile().toURI().toString());
presentModel.addSong(imageSong);
}
presentList.setSelectedIndex(presentModel.getSize() - 1);
} catch (Throwable ex) {
Expand Down Expand Up @@ -2139,27 +2128,28 @@ public void mouseClicked(MouseEvent e) {
} else if (e.getButton() == MouseEvent.BUTTON3) {
// select item at mouse cursor position
int index = presentList.locationToIndex(e.getPoint());
if (presentModel.get(index) instanceof ImageSong imageSong) {
if (!StringTools.isEmpty(presentModel.get(index).getImage())) {
Song imageSong = presentModel.get(index);
presentList.setSelectedIndex(index);
JPopupMenu rotateMenu = new JPopupMenu();
JMenuItem rotate90 = new JMenuItem("rotate right by 90°");
rotate90.addActionListener(ae -> {
imageSong.setRotateRight(90);
imageSong.setImageRotationAsInt(90);
presentList.updateUI();
});
JMenuItem rotate270 = new JMenuItem("rotate left by 90°");
rotate270.addActionListener(ae -> {
imageSong.setRotateRight(270);
imageSong.setImageRotationAsInt(270);
presentList.updateUI();
});
JMenuItem rotate180 = new JMenuItem("rotate by 180°");
rotate180.addActionListener(ae -> {
imageSong.setRotateRight(180);
imageSong.setImageRotationAsInt(180);
presentList.updateUI();
});
JMenuItem rotate0 = new JMenuItem("reset rotation");
rotate0.addActionListener(ae -> {
imageSong.setRotateRight(0);
imageSong.setImageRotationAsInt(0);
presentList.updateUI();
});
rotateMenu.add(rotate90);
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/org/zephyrsoft/sdb2/gui/SongCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.net.URI;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zephyrsoft.sdb2.model.Song;
import org.zephyrsoft.sdb2.util.StringTools;
import org.zephyrsoft.sdb2.util.gui.ImageTools;
Expand All @@ -35,6 +39,7 @@
* List entry for a {@link Song}.
*/
public class SongCell extends JPanel {
private static final Logger LOG = LoggerFactory.getLogger(SongCell.class);

private static final long serialVersionUID = 6861947343987825552L;
public static final int TITLE_BOTTOM_SPACE = 5;
Expand Down Expand Up @@ -118,20 +123,25 @@ public void setFirstLine(String text) {
}
}

public void setImage(final String imageFile, int degreesToRotateRight) {
if (imageFile == null) {
public void setImage(final String imageUrl, int degreesToRotateRight) {
if (imageUrl == null) {
this.image.setVisible(false);
} else {
ImageIcon imageIcon = new ImageIcon(imageFile);
Image image = imageIcon.getImage();
image = ImageTools.rotate(image, degreesToRotateRight);
double factor = (songTitle.getPreferredSize().getHeight() + firstLine.getPreferredSize().getHeight()
+ TITLE_BOTTOM_SPACE + FIRSTLINE_BOTTOM_SPACE) * 2 / image.getHeight(null);
image = ImageTools.scale(image, factor);
this.image.setIcon(new ImageIcon(image));
this.image.setText("");
this.image.setVisible(true);
}
try {
ImageIcon imageIcon = new ImageIcon(URI.create(imageUrl).toURL());
Image image = imageIcon.getImage();
image = ImageTools.rotate(image, degreesToRotateRight);
double factor = (songTitle.getPreferredSize().getHeight() + firstLine.getPreferredSize().getHeight()
+ TITLE_BOTTOM_SPACE + FIRSTLINE_BOTTOM_SPACE) * 2 / image.getHeight(null);
image = ImageTools.scale(image, factor);
this.image.setIcon(new ImageIcon(image));
this.image.setText("");
this.image.setVisible(true);
} catch (MalformedURLException e) {
this.image.setVisible(false);
LOG.warn("could not locate image {}", imageUrl, e);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
*/
package org.zephyrsoft.sdb2.gui.renderer;

import java.awt.Color;
import java.awt.Component;
import java.awt.*;

import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
Expand All @@ -29,10 +27,10 @@
import org.slf4j.LoggerFactory;
import org.zephyrsoft.sdb2.Feature;
import org.zephyrsoft.sdb2.gui.SongCell;
import org.zephyrsoft.sdb2.model.ImageSong;
import org.zephyrsoft.sdb2.model.Song;
import org.zephyrsoft.sdb2.model.SongParser;
import org.zephyrsoft.sdb2.service.IndexerService;
import org.zephyrsoft.sdb2.util.StringTools;
import org.zephyrsoft.sdb2.util.gui.SongCellHighlighter;

/**
Expand Down Expand Up @@ -131,8 +129,8 @@ public Component getListCellRendererComponent(JList<? extends Song> list, Song v
if (value != null) {
ret.setSongTitle(value.getTitle() != null ? value.getTitle() : "");
ret.setFirstLine(SongParser.getFirstLyricsLine(value));
if (value instanceof ImageSong imageSong) {
ret.setImage(imageSong.getFile().getAbsolutePath(), imageSong.getRotateRight());
if (!StringTools.isEmpty(value.getImage())) {
ret.setImage(value.getImage(), value.getImageRotationAsInt());
}
}

Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/zephyrsoft/sdb2/model/ImageSong.java

This file was deleted.

51 changes: 48 additions & 3 deletions src/main/java/org/zephyrsoft/sdb2/model/Song.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,14 @@ public class Song implements Serializable, Comparable<Song>, Persistable {
private String tempo;
@XmlElement(name = "lyrics")
private String lyrics;

@XmlElement(name = "image")
private String image;
/**
* in degrees to rotate right
*/
@XmlElement(name = "imageRotation")
private String imageRotation;

/**
* Create a song instance. CAUTION: every song has to have a UUID! This constructor is only necessary for
* unmarshalling from XML.
Expand Down Expand Up @@ -97,6 +104,8 @@ public Song(Song song) {
drumNotes = song.getDrumNotes();
tempo = song.getTempo();
lyrics = song.getLyrics();
image = song.getImage();
imageRotation = song.getImageRotation();
}

/**
Expand Down Expand Up @@ -163,6 +172,18 @@ public String getDrumNotes() {
return drumNotes;
}

public String getImage() {
return image;
}

public String getImageRotation() {
return imageRotation;
}

public int getImageRotationAsInt() {
return imageRotation == null ? 0 : Integer.parseInt(imageRotation);
}

public String getCleanChordSequence() {
return chordSequence == null
? null
Expand Down Expand Up @@ -213,6 +234,18 @@ public void setChordSequence(String chordSequence) {
this.chordSequence = chordSequence;
}

public void setImage(String image) {
this.image = image;
}

public void setImageRotation(String imageRotation) {
this.imageRotation = imageRotation;
}

public void setImageRotationAsInt(int imageRotation) {
this.imageRotation = String.valueOf(imageRotation);
}

public String getUUID() {
return uuid;
}
Expand Down Expand Up @@ -283,7 +316,9 @@ public boolean equals(Object obj) {
equalsAllowNull(chordSequence, other.chordSequence) &&
equalsAllowNull(drumNotes, other.drumNotes) &&
equalsAllowNull(tempo, other.tempo) &&
equalsAllowNull(lyrics, other.lyrics);
equalsAllowNull(lyrics, other.lyrics) &&
equalsAllowNull(image, other.image) &&
equalsAllowNull(imageRotation, other.imageRotation);
}

@Override
Expand Down Expand Up @@ -319,7 +354,9 @@ && isEmpty(getLyrics())
&& isEmpty(getTonality())
&& isEmpty(getTempo())
&& isEmpty(getDrumNotes())
&& isEmpty(getChordSequence());
&& isEmpty(getChordSequence())
&& isEmpty(getImage())
&& isEmpty(getImageRotation());
}

public Map<String, String> toMap() {
Expand All @@ -340,6 +377,8 @@ public Map<String, String> toMap() {
put("tempo", getTempo());
put("drumNotes", getDrumNotes());
put("chordSequence", getChordSequence());
put("image", getImage());
put("imageRotation", getImageRotation());
}
};
}
Expand Down Expand Up @@ -390,6 +429,12 @@ public void fromMap(Map<String, String> map) {
case "chordSequence":
setChordSequence(value);
break;
case "image":
setImage(value);
break;
case "imageRotation":
setImageRotation(value);
break;
}
}
}
Expand Down
Loading

0 comments on commit d130a05

Please sign in to comment.