• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Martomate / TriPaint / 9275511596

28 May 2024 07:51PM UTC coverage: 29.682% (+1.8%) from 27.877%
9275511596

push

github

Martomate
Updated to Scala 3.4 and MUnit 1.0

2 of 11 new or added lines in 9 files covered. (18.18%)

430 existing lines in 38 files now uncovered.

401 of 1351 relevant lines covered (29.68%)

0.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/scala/tripaint/view/gui/AskForFileSaveSettingsDialog.scala
1
package tripaint.view.gui
2

3
import tripaint.model.coords.StorageCoords
4
import tripaint.model.image.ImageStorage
5
import tripaint.model.image.format.StorageFormat
6
import tripaint.view.FileSaveSettings
7
import tripaint.view.gui.DialogUtils.{getValueFromCustomDialog, makeGridPane}
8

9
import scalafx.collections.ObservableBuffer
10
import scalafx.geometry.Orientation
11
import scalafx.scene.control.{ButtonType, ChoiceBox, Label, Separator}
12
import scalafx.scene.image.{Image, ImageView, PixelFormat, WritableImage}
13
import scalafx.scene.layout.{Pane, StackPane}
14
import scalafx.util.StringConverter
15

16
import java.io.{File, FileInputStream, FileNotFoundException, IOException}
17
import scala.util.{Success, Try}
18

19
object AskForFileSaveSettingsDialog {
UNCOV
20
  def askForFileSaveSettings(
×
21
      storage: ImageStorage,
22
      file: File,
23
      formats: Seq[(StorageFormat, String)],
24
      initiallySelectedFormat: Int
25
  ): Option[FileSaveSettings] = {
26
    val imageSize = storage.imageSize
27
    val (previewFile, previewWidth, previewHeight) = (file, imageSize, imageSize)
28

29
    val xCoordTF = RestrictedTextField.uintTF
×
UNCOV
30
    xCoordTF.promptText = "0"
×
31

32
    val yCoordTF = RestrictedTextField.uintTF
×
UNCOV
33
    yCoordTF.promptText = "0"
×
34

UNCOV
35
    val formatMap: Map[StorageFormat, String] = Map.from(formats)
×
36

37
    val formatChooser = {
NEW
38
      val b = new ChoiceBox[StorageFormat](ObservableBuffer(formats.map(_._1)*))
×
39
      b.selectionModel.value.select(initiallySelectedFormat)
×
UNCOV
40
      b.converter = StringConverter.toStringConverter(formatMap(_))
×
41
      b
42
    }
43

44
    val resultFromInputs = () => {
45
      val xt = xCoordTF.text()
×
46
      val yt = yCoordTF.text()
×
UNCOV
47
      val format = formatChooser.selectionModel.value.getSelectedItem
×
48

49
      for {
×
50
        xOffset <- Try(if xt != "" then xt.toInt else 0)
×
UNCOV
51
        yOffset <- Try(if yt != "" then yt.toInt else 0)
×
52
      } yield FileSaveSettings(StorageCoords(xOffset, yOffset), format)
53
    }
54

55
    val previewPane = new StackPane
×
56
    previewPane.setMinSize(previewWidth, previewHeight)
×
UNCOV
57
    previewPane.setMaxSize(previewWidth, previewHeight)
×
58

59
    val previewImage = new WritableImage(imageSize, imageSize)
×
60
    val pixelFormat = PixelFormat.getIntArgbInstance
×
UNCOV
61
    previewPane.children.add(new ImageView(previewImage))
×
62

63
    val previewBorder = new Pane
×
64
    previewBorder.setMinSize(previewWidth, previewHeight)
×
65
    previewBorder.setMaxSize(previewWidth, previewHeight)
×
UNCOV
66
    previewPane.children.add(previewBorder)
×
67

68
    {
69
      import javafx.scene.layout.*
70
      import javafx.scene.paint.Color
UNCOV
71
      val stroke = new BorderStroke(
×
72
        Color.RED,
73
        BorderStrokeStyle.SOLID,
74
        CornerRadii.EMPTY,
75
        BorderWidths.DEFAULT
76
      )
UNCOV
77
      previewBorder.delegate.setBorder(new Border(stroke))
×
78
    }
79

80
    val previewStack = new Pane
×
81
    try {
×
82
      val wholeImage = new ImageView(new Image(new FileInputStream(previewFile)))
×
UNCOV
83
      previewStack.children.add(wholeImage)
×
84
    } catch {
85
      case _: FileNotFoundException =>
×
UNCOV
86
      case _: IOException           =>
×
87
    }
88

UNCOV
89
    previewStack.delegate.getChildren.add(previewPane)
×
90

91
    def updatePreviewAction(): Unit = {
×
92
      resultFromInputs() match {
×
93
        case Success(FileSaveSettings(StorageCoords(x, y), format)) =>
94
          previewPane.setLayoutX(x)
×
UNCOV
95
          previewPane.setLayoutY(y)
×
96

UNCOV
97
          val pixels = storage.toRegularImage(format).toIntArray
×
98

99
          val w = previewImage.pixelWriter
×
100
          w.setPixels(0, 0, imageSize, imageSize, pixelFormat, pixels, 0, imageSize)
×
UNCOV
101
        case _ =>
×
102
      }
103
    }
104

UNCOV
105
    updatePreviewAction()
×
106

107
    xCoordTF.text.onChange(updatePreviewAction())
×
108
    yCoordTF.text.onChange(updatePreviewAction())
×
UNCOV
109
    formatChooser.selectionModel().selectedItemProperty().addListener(_ => updatePreviewAction())
×
110

111
    val inputForm = makeGridPane(
×
112
      Seq(
×
113
        Seq(new Label("X coordinate:"), xCoordTF),
×
114
        Seq(new Label("Y coordinate:"), yCoordTF),
×
UNCOV
115
        Seq(new Label("Format:"), formatChooser)
×
116
      )
117
    )
118

UNCOV
119
    getValueFromCustomDialog[FileSaveSettings](
×
120
      title = "Save file",
121
      headerText = "Where in the file should the image be saved, and how?",
UNCOV
122
      content = Seq(
×
123
        inputForm,
UNCOV
124
        Separator(Orientation.Horizontal),
×
125
        previewStack
126
      ),
127
      resultConverter = {
128
        case ButtonType.OK => resultFromInputs().getOrElse(null)
×
UNCOV
129
        case _             => null
×
130
      },
UNCOV
131
      buttons = Seq(ButtonType.OK, ButtonType.Cancel)
×
132
    )
133
  }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc