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

3
import tripaint.model.ImageGrid
4
import tripaint.model.image.{GridCell, ImagePool}
5
import tripaint.view.image.TriImageForPreview
6

7
import scalafx.application.Platform
8
import scalafx.beans.property.StringProperty
9
import scalafx.collections.ObservableBuffer
10
import scalafx.geometry.Orientation
11
import scalafx.scene.Node
12
import scalafx.scene.control.*
13
import scalafx.scene.image.{Image, ImageView, PixelFormat, WritableImage}
14
import scalafx.scene.layout.*
15
import scalafx.util.StringConverter
16

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

20
type TextFieldRestriction = String => Boolean
21

22
object TextFieldRestriction {
UNCOV
23
  private def isTrue(pred: => Boolean): Boolean = Try(pred).getOrElse(false)
×
24

25
  def customIntRestriction(pred: Int => Boolean): TextFieldRestriction = { s =>
×
UNCOV
26
    isTrue(pred(s.toInt)) || isTrue(pred((s + "1").toInt))
×
27
  }
28

29
  val intRestriction: TextFieldRestriction = customIntRestriction(_ => true)
×
UNCOV
30
  val uintRestriction: TextFieldRestriction = customIntRestriction(_ >= 0)
×
31
}
32

33
object RestrictedTextField {
34
  def intTF: TextField = restrict(new TextField, TextFieldRestriction.intRestriction)
×
UNCOV
35
  def uintTF: TextField = restrict(new TextField, TextFieldRestriction.uintRestriction)
×
36

37
  def restrict(tf: TextField, contentAllowed: TextFieldRestriction): TextField = {
×
UNCOV
38
    tf.text.onChange((ob, oldVal, newVal) => {
×
39
      val property = ob.asInstanceOf[StringProperty]
UNCOV
40
      property.value = if contentAllowed(newVal) then newVal else oldVal
×
41
    })
42
    tf
43
  }
44
}
45

46
object DialogUtils {
47
  def makeGridPane(content: Seq[Seq[Node]]): GridPane = {
×
48
    val gridPane = new GridPane
×
49
    gridPane.vgap = 10
×
50
    gridPane.hgap = 10
×
51
    for i <- content.indices do {
×
52
      for j <- content(i).indices do {
×
UNCOV
53
        gridPane.add(content(i)(j), j, i)
×
54
      }
55
    }
56
    gridPane
57
  }
58

UNCOV
59
  def getValueFromCustomDialog[R](
×
60
      title: String,
61
      headerText: String = null,
62
      contentText: String = null,
63
      graphic: Node = null,
UNCOV
64
      content: Seq[Region] = Seq.empty,
×
65
      resultConverter: ButtonType => R,
66
      nodeWithFocus: Node = null,
UNCOV
67
      buttons: Seq[ButtonType] = Seq(ButtonType.OK, ButtonType.Cancel)
×
68
  ): Option[R] = {
69
    val dialog = new Dialog[R]
×
70
    dialog.title = title
×
71
    dialog.headerText = headerText
×
72
    dialog.contentText = contentText
×
UNCOV
73
    dialog.graphic = graphic
×
74

NEW
75
    val contentBox = new VBox(content*)
×
UNCOV
76
    contentBox.setSpacing(10)
×
77

78
    dialog.dialogPane().setContent(contentBox)
×
UNCOV
79
    dialog.resultConverter = resultConverter
×
80

81
    for b <- buttons do {
×
UNCOV
82
      dialog.dialogPane().getButtonTypes.add(b)
×
83
    }
84

85
    if nodeWithFocus != null then {
×
UNCOV
86
      dialog.setOnShowing(_ => Platform.runLater(nodeWithFocus.requestFocus()))
×
UNCOV
87
    }
×
88

89
    val result = dialog.delegate.showAndWait()
×
UNCOV
90
    if result.isPresent then Some(result.get) else None
×
91
  }
92

UNCOV
93
  def getValueFromDialog[T](
×
94
      imagePool: ImagePool,
95
      images: Seq[GridCell],
96
      title: String,
97
      headerText: String,
98
      contentText: String,
99
      restriction: String => Boolean,
100
      stringToValue: String => T,
101
      refreshPreviewFn: Option[(T, ImageGrid) => Unit] = None
102
  ): Option[T] = {
UNCOV
103
    val (previewPane, updatePreview) = makeImagePreviewList(images, imagePool)
×
104

105
    val dialog = new TextInputDialog
×
106
    dialog.title = title
×
107
    dialog.headerText = headerText
×
108
    dialog.contentText = contentText
×
109
    dialog.graphic = previewPane
×
UNCOV
110
    RestrictedTextField.restrict(dialog.editor, restriction)
×
111

112
    refreshPreviewFn match {
113
      case Some(refreshFn) =>
×
114
        dialog.editor.text.onChange((_, _, s) =>
×
115
          if restriction(s) then {
×
116
            val value = stringToValue(s)
×
117
            updatePreview(imageGrid => refreshFn(value, imageGrid))
×
118
          } else {
×
UNCOV
119
            updatePreview(_ => ())
×
120
          }
121
        )
UNCOV
122
      case None =>
×
123
    }
124

UNCOV
125
    dialog.showAndWait().map(stringToValue)
×
126
  }
127

UNCOV
128
  def makeImagePreviewList(
×
129
      images: Seq[GridCell],
130
      imagePool: ImagePool
131
  ): (ScrollPane, (ImageGrid => Unit) => Unit) = {
UNCOV
132
    ImagePreviewList.fromImageContent(images, TriImageForPreview.previewSize, imagePool.locationOf)
×
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