Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull out a scaled slider class for finer grained control of slice positions #51

Closed

Conversation

Andreas-Forster
Copy link
Member

This PR fixes #50 by introducing a new class SubDividedSliderAdapter. The sliders have by default a 100x finer resolution than now. This should fix for most use cases the issue #50 where the slices could not be made visible by using the sliders.

@Andreas-Forster
Copy link
Member Author

Andreas-Forster commented Jun 9, 2020

Yet, this PR should serve as a discussion basis and testing is still needed before merging it.

Copy link
Member

@clangguth clangguth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me in general, I've added a few tiny nitpicking suggestions.

The only thing that still bothers me a bit is that most of the time references are made to the adapter, but sometimes to the contained slider. (listenTo(x.slider.slider)).

This could be overcome by making the adapter itself a ScalismoPublisher that listens to the slider events and re-publishes them on behalf of itself. Then the only occurence where direct access to the contained slider would still be needed is in the layout() calls, because they expect an actual Swing component.

@@ -121,29 +121,23 @@ class ViewportPanel3D(frame: ScalismoFrame, override val name: String = "3D") ex
class ViewportPanel2D(frame: ScalismoFrame, val axis: Axis) extends ViewportPanel(frame) {
override def name: String = axis.toString

private lazy val positionSlider = new Slider {
private val SubDivisions: Double = 100.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obsolete and can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - addressed in new commit

positionSlider.value = positionSlider.value + 1
}
}
override def apply(): Unit = positionSlider.up
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override def apply(): Unit = positionSlider.up
override def apply(): Unit = positionSlider.up()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - addressed in new commit

positionSlider.value = positionSlider.value - 1
}
}
override def apply(): Unit = positionSlider.down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override def apply(): Unit = positionSlider.down
override def apply(): Unit = positionSlider.down()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - addressed in new commit

slider.value = slider.value - 1
}
}
override def apply(): Unit = slider.down
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override def apply(): Unit = slider.down
override def apply(): Unit = slider.down()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - addressed in new commit

slider.value = slider.value + 1
}
}
override def apply(): Unit = slider.up
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override def apply(): Unit = slider.up
override def apply(): Unit = slider.up()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - addressed in new commit


class SubDividedSliderAdapter(private val _slider: Slider, private val factor: Double = 100.0) {

def slider: Slider = _slider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to introduce a private variable here, as it's an object anyway which is made accessible using this getter.

Therefore, my suggestion is to simply make slider a val. In the same vein, there's not really a need to keep factor private.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree - addressed in new commit

@Andreas-Forster
Copy link
Member Author

I made the SubDivdedSliderAdapter a ScalismoPublisher. Further, I had to introduce a new FancySliderDecorator as the FancySlider does not handle the position of the labels correctly.

It looks now that we have quite a few sliders classes. Not sure if there is a better solution.

@clangguth
Copy link
Member

Hmmm... ok, please give me a couple of days to take a closer look at how this can be properly solved.

@clangguth
Copy link
Member

Sorry, I don't currently find the time for a proper solution, but here's an idea/outline of a "one-size fits all" slider class which covers all existing use cases, maybe someone finds a way to implement it:

  • ensure that it works with multiple types, not just integers. This would mean creating a new generic class which mimicks the interface of the existing Slider class, but in a typed way. Implementation-wise it could either wrap a JSlider or a Slider.
  • include the formatting part of the FancySlider, so that labels can be shown if needed.
  • the tricky part: ensure that it works for any domain (min/max) and that it allows to set a "step size" (or factor, or granularity, or whatever you want to call it).

Essentially, one needs to find functions to map from that "application domain" (AD) to a "slider domain" (SD) and back, where SD only consists of integers and the step size is always 1. However, min/max of SD could be adjusted if needed, but the largest possible range is (Int.MinValue, Int.MaxValue). The mapping between AD and SD would also need to be bijective, so that changes (movement of the slider) properly get propagated back to the application.

@Andreas-Forster
Copy link
Member Author

Andreas-Forster commented Jun 23, 2020

I added a quick draft for a TypedSlider[A] class which should work for Int, Float and Double. I replaced all sliders except the anonymous class using directly scala.swing.Slider here.

A quick test showed that it seems to work. However, I have not yet tested all classes I changed and maybe there is an unhandled corner case. For example, one could require that the step-size is always strictly positive. A lower bound for the step-size based on the min and max value could also make sense, in order to not prevent that the slider's internal value does not overflow. These should however not occur if you use the sliders in a reasonable range. Any opinion if such checks should be added? Do you see other problems?

Further the implicit could probably be removed if one creates a slider class per type (int, float, double). Any opinion here if this should be changed?

@marcelluethi
Copy link
Contributor

I lost track about the status of this PR. Is it okay to close it or are we close to something that can be merged?

@Andreas-Forster
Copy link
Member Author

I think we can close it for now. We could still pick it up later on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slicing through small images does not have the right resolution
3 participants