• 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/AskForFileOpenSettingsDialog.scala
1
package tripaint.view.gui
2

3
import tripaint.infrastructure.FileSystem
4
import tripaint.model.{Color, ImageGrid}
5
import tripaint.model.coords.{GridCoords, StorageCoords}
6
import tripaint.model.image.{GridCell, ImageStorage}
7
import tripaint.model.image.format.StorageFormat
8
import tripaint.view.FileOpenSettings
9
import tripaint.view.gui.DialogUtils.{getValueFromCustomDialog, makeGridPane}
10
import tripaint.view.image.TriImageForPreview
11

12
import scalafx.collections.ObservableBuffer
13
import scalafx.embed.swing.SwingFXUtils
14
import scalafx.geometry.Orientation
15
import scalafx.scene.control.{ButtonType, ChoiceBox, Label, Separator}
16
import scalafx.scene.image.ImageView
17
import scalafx.scene.layout.Pane
18
import scalafx.util.StringConverter
19

20
import java.io.File
21
import scala.util.{Success, Try}
22

23
object AskForFileOpenSettingsDialog {
UNCOV
24
  def askForFileOpenSettings(
×
25
      imagePreview: (File, Int, Int, Int),
26
      formats: Seq[(StorageFormat, String)],
27
      initiallySelectedFormat: Int,
28
      fileSystem: FileSystem
29
  ): Option[FileOpenSettings] = {
UNCOV
30
    val (previewFile, imageSize, xCount, yCount) = imagePreview
×
31
    val (previewWidth, previewHeight) = (xCount * imageSize, yCount * imageSize)
32

33
    val xCoordTF = RestrictedTextField.uintTF
×
UNCOV
34
    xCoordTF.promptText = "0"
×
35

36
    val yCoordTF = RestrictedTextField.uintTF
×
UNCOV
37
    yCoordTF.promptText = "0"
×
38

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

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

48
    val resultFromInputs = () => {
49
      val xt = xCoordTF.text()
×
50
      val yt = yCoordTF.text()
×
UNCOV
51
      val format = formatChooser.selectionModel.value.getSelectedItem
×
52

53
      for {
×
54
        xOffset <- Try(if xt != "" then xt.toInt else 0)
×
UNCOV
55
        yOffset <- Try(if yt != "" then yt.toInt else 0)
×
56
      } yield FileOpenSettings(StorageCoords(xOffset, yOffset), format)
57
    }
58

59
    val previewPaneBorder = {
60
      import javafx.scene.layout.*
61
      import javafx.scene.paint.Color
UNCOV
62
      val stroke = new BorderStroke(
×
63
        Color.RED,
64
        BorderStrokeStyle.SOLID,
65
        CornerRadii.EMPTY,
66
        BorderWidths.DEFAULT
67
      )
UNCOV
68
      new Border(stroke)
×
69
    }
70

71
    val previewPane = {
72
      val p = new Pane
×
73
      p.setMinSize(previewWidth, previewHeight)
×
74
      p.setMaxSize(previewWidth, previewHeight)
×
UNCOV
75
      p.delegate.setBorder(previewPaneBorder)
×
76
      p
77
    }
78

UNCOV
79
    val underlyingImage = fileSystem.readImage(previewFile).get
×
80

UNCOV
81
    val wholeImage = new ImageView(SwingFXUtils.toFXImage(underlyingImage.toBufferedImage, null))
×
82

83
    val previewStack = new Pane
×
UNCOV
84
    previewStack.delegate.getChildren.addAll(wholeImage, previewPane)
×
85

86
    val blankImage = ImageStorage.fill(imageSize, Color.White)
×
87
    val imageGridCells = Seq.tabulate(xCount)(x => new GridCell(GridCoords(x, 0), blankImage))
×
UNCOV
88
    val imageGrid = ImageGrid.fromCells(imageSize, imageGridCells)
×
89

90
    val (triPreviewPane, updateTriPreview) = ImagePreviewList.fromImageContent(
×
UNCOV
91
      imageGrid.images,
×
92
      TriImageForPreview.previewSize,
93
      _ => None
94
    )
95

96
    def updatePreviewAction(): Unit = {
×
97
      resultFromInputs() match {
×
98
        case Success(FileOpenSettings(StorageCoords(sx, sy), format)) =>
99
          previewPane.setLayoutX(sx)
×
UNCOV
100
          previewPane.setLayoutY(sy)
×
101

UNCOV
102
          for x <- 0 until xCount do {
×
103
            val offset = StorageCoords(sx + x * imageSize, sy)
104
            val newPreviewImage = ImageStorage
105
              .fromRegularImage(underlyingImage, offset, format, imageSize)
×
UNCOV
106
              .getOrElse(blankImage)
×
107

UNCOV
108
            imageGrid.replaceImage(GridCoords(x, 0), newPreviewImage)
×
109

UNCOV
110
            updateTriPreview(_ => ())
×
111
          }
UNCOV
112
        case _ =>
×
113
      }
114
    }
115

UNCOV
116
    updatePreviewAction()
×
117

118
    xCoordTF.text.onChange(updatePreviewAction())
×
119
    yCoordTF.text.onChange(updatePreviewAction())
×
UNCOV
120
    formatChooser.onAction = _ => updatePreviewAction()
×
121

122
    val inputForm = makeGridPane(
×
123
      Seq(
×
124
        Seq(new Label("X coordinate:"), xCoordTF),
×
125
        Seq(new Label("Y coordinate:"), yCoordTF),
×
UNCOV
126
        Seq(new Label("Format:"), formatChooser)
×
127
      )
128
    )
129

UNCOV
130
    getValueFromCustomDialog[FileOpenSettings](
×
131
      title = "Open image",
132
      headerText = "Which part of the image should be opened? Please enter the top left corner:",
UNCOV
133
      content = Seq(
×
134
        inputForm,
UNCOV
135
        Separator(Orientation.Horizontal),
×
136
        previewStack
137
      ),
138
      graphic = triPreviewPane,
139
      resultConverter = {
140
        case ButtonType.OK => resultFromInputs().getOrElse(null)
×
UNCOV
141
        case _             => null
×
142
      },
UNCOV
143
      buttons = Seq(ButtonType.OK, ButtonType.Cancel)
×
144
    )
145
  }
146
}
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