Recent

Author Topic: Tspinedit Tfloatspinedit min/max values  (Read 10982 times)

Andyk

  • Jr. Member
  • **
  • Posts: 72
Tspinedit Tfloatspinedit min/max values
« on: June 18, 2012, 04:19:04 pm »
Just noticed a bug in these controls.

The default behavior when minvalue=maxvalue should be that the value is unconstrained.

Currently, the value is always limited by the Min and Max values.

The code I changed made it compatible with Delphi's Tspinedit.

Code: [Select]
function TCustomFloatSpinEdit.GetLimitedValue(const AValue: Double): Double;
begin
  Result := AValue;
  if (FMaxValue <> FMinValue) then
 // if FMaxValue >= FMinValue then
  begin
    if Result < FMinValue then Result := FMinValue;
    if Result > FMaxValue then Result := FMaxValue;
  end;
end;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Tspinedit Tfloatspinedit min/max values
« Reply #1 on: June 18, 2012, 04:43:50 pm »
Great that you could improve the code... If you want to see that improvement as part of the next release, it's probably best if you submit it to the bugtracker (see link left) as a patch (see e.g. http://wiki.lazarus.freepascal.org/Creating_A_Patch).

Note: you'd have to have the latest SVN version of Lazarus so the patch can be easily applied... (see e.g. the Getting the Source tab on the left).

I understand if that seems a lot of trouble for a couple of measly lines of code... but it looks like you'll be churning out a lot more improvements ;)

Thanks,
BigChimp
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Tspinedit Tfloatspinedit min/max values
« Reply #2 on: June 19, 2012, 11:53:09 am »
...
The default behavior when minvalue=maxvalue should be that the value is unconstrained.
...
Code: [Select]
function TCustomFloatSpinEdit.GetLimitedValue(const AValue: Double): Double;
begin
  Result := AValue;
  if (FMaxValue <> FMinValue) then
 // if FMaxValue >= FMinValue then
  begin
    if Result < FMinValue then Result := FMinValue;
    if Result > FMaxValue then Result := FMaxValue;
  end;
end;

Q: How does Delphi behave when Max < Min?
IOW: would this alos be OK?

Code: [Select]
function TCustomFloatSpinEdit.GetLimitedValue(const AValue: Double): Double;
begin
  Result := AValue;
  if FMaxValue > FMinValue then  // > instead of >=
  begin
    if Result < FMinValue then Result := FMinValue;
    if Result > FMaxValue then Result := FMaxValue;
  end;
end;

I cannot test myself with Delphi (my version (D3) doesn't have a TSpinEdit)

Bart

Andyk

  • Jr. Member
  • **
  • Posts: 72
Re: Tspinedit Tfloatspinedit min/max values
« Reply #3 on: June 19, 2012, 02:08:30 pm »
...
The default behavior when minvalue=maxvalue should be that the value is unconstrained.
...
Code: [Select]
function TCustomFloatSpinEdit.GetLimitedValue(const AValue: Double): Double;
begin
  Result := AValue;
  if (FMaxValue <> FMinValue) then
 // if FMaxValue >= FMinValue then
  begin
    if Result < FMinValue then Result := FMinValue;
    if Result > FMaxValue then Result := FMaxValue;
  end;
end;

Q: How does Delphi behave when Max < Min?
IOW: would this alos be OK?

Code: [Select]
function TCustomFloatSpinEdit.GetLimitedValue(const AValue: Double): Double;
begin
  Result := AValue;
  if FMaxValue > FMinValue then  // > instead of >=
  begin
    if Result < FMinValue then Result := FMinValue;
    if Result > FMaxValue then Result := FMaxValue;
  end;
end;

I cannot test myself with Delphi (my version (D3) doesn't have a TSpinEdit)

Bart

Ideally, the values should be automatically trimmed in the SetMinvalue and SetMaxvalue methods so that Fmaxvalue is always >=Fminvalue.

This is not how its done in Delphi though, the Fminvalue and Fmaxvalue have no setter methods, so its not optimal, they just float at whatever the user sets.




Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Tspinedit Tfloatspinedit min/max values
« Reply #4 on: June 19, 2012, 04:58:47 pm »
That didn't really answer my question.

How does Delphi behave if MaxValue > MinValue: is the value constarined, and if so, how?

Bart

Andyk

  • Jr. Member
  • **
  • Posts: 72
Re: Tspinedit Tfloatspinedit min/max values
« Reply #5 on: June 19, 2012, 05:10:05 pm »
That didn't really answer my question.

How does Delphi behave if MaxValue > MinValue: is the value constarined, and if so, how?

Bart

It did answer your question, they have no setter methods, so they are therefore unconstrained.

So if
Minvalue=5
Maxvalue=4

Then setting Value=0 will first be adjusted by Minvalue up to 5 and then by Maxvalue down to 4.


Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Tspinedit Tfloatspinedit min/max values
« Reply #6 on: June 19, 2012, 10:58:03 pm »
It did answer your question, they have no setter methods, so they are therefore unconstrained.

How does the information that there is no setter for Min/Maxvalue in Delphi imply that Value is unconstrained?
I think we misunderstood eachother.

Minvalue=5
Maxvalue=4
Then setting Value=0 will first be adjusted by Minvalue up to 5 and then by Maxvalue down to 4.

Is this actual Delhi behaviour?
If so, I find it rather strange.

Would there be any real life scenario where a program depends on this behaviour (MinValue > MaxValue)?

Constraining Value only when MaxValue > MinValue sounds more logical to me.

Bart

Andyk

  • Jr. Member
  • **
  • Posts: 72
Re: Tspinedit Tfloatspinedit min/max values
« Reply #7 on: June 20, 2012, 09:01:07 am »
It did answer your question, they have no setter methods, so they are therefore unconstrained.

How does the information that there is no setter for Min/Maxvalue in Delphi imply that Value is unconstrained?
I think we misunderstood eachother.

Minvalue=5
Maxvalue=4
Then setting Value=0 will first be adjusted by Minvalue up to 5 and then by Maxvalue down to 4.

Is this actual Delhi behaviour?
If so, I find it rather strange.

Would there be any real life scenario where a program depends on this behaviour (MinValue > MaxValue)?

Constraining Value only when MaxValue > MinValue sounds more logical to me.

Bart

If a property has setter methods e.g

property MaxValue:double  read Fmaxvalue write setmaxvalue;

Then the value can be constrained in the setter method, saying that a property has no setter method eg.

property MaxValue:double  read Fmaxvalue write Fmaxvalue;

implies automatically that the property is unconstrained.

So, yes you are correct, it makes no sense to do it that way, there should be a setter method.

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Tspinedit Tfloatspinedit min/max values
« Reply #8 on: June 20, 2012, 11:04:18 am »
I'm not in favour of constraints on the MinValue / MaxValue, as this forces you to input them in the correct order,  and as MaxValue comes before MinValue on the properties page it is even more relevant.  eg.  Say if the Current MinValue = 200, MaxValue = 300, and then you decide to have MaxValue = 100, MinValue = 1, then your forced to set MinValue first.

Keeping things simple, if MaxValue <= MinValue there is no Value constraint would seem the simplest and most logical change.

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Tspinedit Tfloatspinedit min/max values
« Reply #9 on: June 20, 2012, 09:32:37 pm »
Not necessarily.
You can make it so that if you set Max < Min, then Min is set to the same value and vice versa.

Bart

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Tspinedit Tfloatspinedit min/max values
« Reply #10 on: June 20, 2012, 09:45:34 pm »
Quote
Not necessarily.

Then it's not a constraint, but an auto-fix.  Like I said, I'm not in favour of a constraint on the MinValue / MaxValue, but I see no issue with the auto-fix.

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: Tspinedit Tfloatspinedit min/max values
« Reply #11 on: June 20, 2012, 10:18:58 pm »
I would also think it was logical to adjust Value if you change Min/MaxValue and Value isn't inside the constraints anymore.

IMO these are designflaws form the Borland era.
But if we fix them we may change behaviour of current programs (IOW it's not backwards compatible) and we are not Delphi compatible anymore (but that is not a holy grail).

I will as on the LazarusDev mailinglist.

Bart

 

TinyPortal © 2005-2018