Skip to content

Commit

Permalink
Adjust logo handling for app
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Scholz committed Dec 2, 2023
1 parent aec83b0 commit fd500cc
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ object Mapper {
backgroundColor = viewModel.backgroundColor.value.toColorInfo(),
foregroundColor = viewModel.foregroundColor.value.toColorInfo(),
dotShape = viewModel.dotShape.value,
logo = viewModel.logo.value,
logoBase64 = viewModel.logoBase64.value,
useBase64Logo = viewModel.useBase64Logo.value,
logoRelativeSize = viewModel.logoRelativeSize.value,
logoBackgroundColor = viewModel.logoBackgroundColor.value.toColorInfo(),
logoShape = viewModel.logoShape.value,
Expand All @@ -36,7 +34,7 @@ object Mapper {
backgroundColor.value = qrCodeConfig.backgroundColor.toColor()
foregroundColor.value = qrCodeConfig.foregroundColor.toColor()
dotShape.value = qrCodeConfig.dotShape
logo.value = qrCodeConfig.logo
logoBase64.value = qrCodeConfig.logoBase64
logoRelativeSize.value = qrCodeConfig.logoRelativeSize
logoBackgroundColor.value = qrCodeConfig.logoBackgroundColor.toColor()
logoShape.value = qrCodeConfig.logoShape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ data class QrCodeConfig(
val foregroundColor: ColorInfo,
val dotShape: QrCodeDotShape,

val logo: String,
val logoBase64: String?,
val useBase64Logo: Boolean,
val logoBase64: String,
val logoRelativeSize: Double,
val logoBackgroundColor: ColorInfo,
val logoShape: LogoShape,
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import io.github.simonscholz.qrcode.QrCodeFactory
import io.github.simonscholz.qrcode.QrPositionalSquaresConfig
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import javax.swing.JOptionPane

class ImageService(private val qrCodeConfigViewModel: QrCodeConfigViewModel) {

Expand All @@ -35,22 +32,7 @@ class ImageService(private val qrCodeConfigViewModel: QrCodeConfigViewModel) {
outerBorderColor = qrCodeConfigViewModel.positionalSquareOuterBorderColor.value,
),
)
if (!qrCodeConfigViewModel.useBase64Logo.value && qrCodeConfigViewModel.logo.value.isNotBlank() && File(qrCodeConfigViewModel.logo.value).exists()) {
runCatching {
ImageIO.read(File(qrCodeConfigViewModel.logo.value)).let {
val scaledLogo = getScaledLogo(it, qrCodeConfigViewModel)

builder.qrLogoConfig(
logo = scaledLogo,
relativeSize = qrCodeConfigViewModel.logoRelativeSize.value,
bgColor = qrCodeConfigViewModel.logoBackgroundColor.value,
shape = qrCodeConfigViewModel.logoShape.value,
)
}
}.onFailure { _ ->
JOptionPane.showMessageDialog(null, "You did not select a proper image", "Image Loading Error", JOptionPane.ERROR_MESSAGE)
}
} else if (qrCodeConfigViewModel.useBase64Logo.value && qrCodeConfigViewModel.logoBase64.value.isNotBlank()) {
if (qrCodeConfigViewModel.logoBase64.value.isNotBlank()) {
builder.qrLogoConfig(
base64Logo = qrCodeConfigViewModel.logoBase64.value,
relativeSize = qrCodeConfigViewModel.logoRelativeSize.value,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package io.github.simonscholz.ui.properties

import io.github.simonscholz.extension.toButtonSelectedObservable
import io.github.simonscholz.extension.toDoubleObservable
import io.github.simonscholz.extension.toEnabledInvertedObservable
import io.github.simonscholz.extension.toEnabledObservable
import io.github.simonscholz.extension.toObservable
import io.github.simonscholz.extension.toSelectedItemObservable
import io.github.simonscholz.model.QrCodeConfigViewModel
import io.github.simonscholz.qrcode.LogoShape
import io.github.simonscholz.qrcode.toBase64
import io.github.simonscholz.ui.CustomItems
import net.miginfocom.swing.MigLayout
import org.eclipse.core.databinding.DataBindingContext
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.StringSelection
import java.io.File
import javax.swing.ButtonGroup
import javax.imageio.ImageIO
import javax.swing.ImageIcon
import javax.swing.JButton
import javax.swing.JComboBox
import javax.swing.JFileChooser
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JRadioButton
import javax.swing.JSpinner
import javax.swing.JTextField
import javax.swing.SpinnerNumberModel
import javax.swing.filechooser.FileNameExtensionFilter

Expand All @@ -30,52 +29,55 @@ object LogoPropertiesUI {
fun createLogoPropertiesUI(dataBindingContext: DataBindingContext, qrCodeConfigViewModel: QrCodeConfigViewModel): JPanel {
val logoPropertiesPanel = JPanel(MigLayout())

logoPropertiesPanel.add(JLabel("Logo source:"))

val radioPanel = JPanel(MigLayout("nogrid"))
val useFileButton = JRadioButton("Use file")
val useBase64Button = JRadioButton("Use base 64")
ButtonGroup().run {
add(useFileButton)
add(useBase64Button)
}
radioPanel.add(useFileButton)
radioPanel.add(useBase64Button)
dataBindingContext.bindValue(useBase64Button.toButtonSelectedObservable(), qrCodeConfigViewModel.useBase64Logo)
logoPropertiesPanel.add(radioPanel, "wrap")

logoPropertiesPanel.add(JLabel("Logo:"))
val logoTextField = JTextField()
dataBindingContext.bindValue(logoTextField.toObservable(), qrCodeConfigViewModel.logo)
logoPropertiesPanel.add(logoTextField, "growx, width 200:220:300")
val chooseFile = JButton("...")
chooseFile.addActionListener {

val buttonPanel = JPanel(MigLayout())
val chooseFileButton = JButton()
chooseFileButton.toolTipText = "Choose image file"
chooseFileButton.icon = ImageIcon(LogoPropertiesUI::class.java.classLoader.getResource("image16.png"))
chooseFileButton.addActionListener {
val fileChooser = JFileChooser()
fileChooser.fileFilter = FileNameExtensionFilter("Image files", "png", "jpg", "jpeg")
fileChooser.showOpenDialog(logoPropertiesPanel.parent).let {
if (it == JFileChooser.APPROVE_OPTION) {
val file: File = fileChooser.selectedFile
logoTextField.text = file.absolutePath
ImageIO.read(file).let { image ->
qrCodeConfigViewModel.logoBase64.value = image.toBase64()
}
}
}
}
logoPropertiesPanel.add(chooseFile, "wrap, growx, width 30:30:30")
dataBindingContext.bindValue(logoTextField.toEnabledInvertedObservable(), qrCodeConfigViewModel.useBase64Logo)
dataBindingContext.bindValue(chooseFile.toEnabledInvertedObservable(), qrCodeConfigViewModel.useBase64Logo)
buttonPanel.add(chooseFileButton, "width 30:30:30")

val base64ImageButton = JButton()
base64ImageButton.toolTipText = "Paste base64 image text"
base64ImageButton.icon = ImageIcon(LogoPropertiesUI::class.java.classLoader.getResource("base64_16.png"))
base64ImageButton.addActionListener {
val base64ImageInput = JOptionPane.showInputDialog("Paste base64 image text", "")
if (base64ImageInput != null) {
qrCodeConfigViewModel.logoBase64.value = base64ImageInput
}
}
buttonPanel.add(base64ImageButton, "width 30:30:30")

logoPropertiesPanel.add(JLabel("Base64 encoded Logo:"))
val base64LogoTextField = JTextField().apply {
dataBindingContext.bindValue(this.toObservable(), qrCodeConfigViewModel.logoBase64)
logoPropertiesPanel.add(this, "growx, width 200:220:300")
dataBindingContext.bindValue(this.toEnabledObservable(), qrCodeConfigViewModel.useBase64Logo)
val copyButton = JButton()
copyButton.toolTipText = "Copy base64 image text"
copyButton.icon = ImageIcon(LogoPropertiesUI::class.java.classLoader.getResource("copy16.png"))
copyButton.addActionListener {
val clipboard: Clipboard = Toolkit.getDefaultToolkit().systemClipboard
val copyString = StringSelection(qrCodeConfigViewModel.logoBase64.value)
clipboard.setContents(copyString, null)
}
buttonPanel.add(copyButton, "width 30:30:30")

val deleteBase64LogoTextField = JButton()
deleteBase64LogoTextField.icon = ImageIcon(LogoPropertiesUI::class.java.classLoader.getResource("dustbin_remove16.png"))
deleteBase64LogoTextField.toolTipText = "Remove icon"
deleteBase64LogoTextField.icon = ImageIcon(LogoPropertiesUI::class.java.classLoader.getResource("delete16.png"))
deleteBase64LogoTextField.addActionListener {
base64LogoTextField.text = ""
qrCodeConfigViewModel.logoBase64.value = ""
}
logoPropertiesPanel.add(deleteBase64LogoTextField, "wrap, growx, width 30:30:30")
buttonPanel.add(deleteBase64LogoTextField, "wrap, width 30:30:30")
logoPropertiesPanel.add(buttonPanel, "wrap, growx, span 3")

logoPropertiesPanel.add(JLabel("Relative Logo Size:"))
val sizeSpinnerModel = SpinnerNumberModel(.2, .0, 1.0, 0.01)
Expand Down
Binary file added qr-code-app/src/main/resources/base64_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added qr-code-app/src/main/resources/copy16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added qr-code-app/src/main/resources/delete16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed qr-code-app/src/main/resources/dustbin_remove16.png
Binary file not shown.
Binary file added qr-code-app/src/main/resources/image16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fd500cc

Please sign in to comment.