Recent

Author Topic: TValueListEditor - A Cell is cleared after sorting  (Read 13074 times)

egsuh

  • Hero Member
  • *****
  • Posts: 855
TValueListEditor - A Cell is cleared after sorting
« on: August 17, 2018, 10:36:21 am »
I'd like to sort rows of TValueListEditor based on the "key" value. But as the keys are strings,  sorting numbers would result like   1,11,2,3  instead of 1,2,3,11.   So I added a little trick.

Anyway my point is that, once sorted, Keys or Values column of the first row is blanked if the cursor is somewhere within the TValueListEditor.   So I added another trick, which moves focus to other control in the form first. The following codes work fine. But deleting a cell is rather strange.

Code: Pascal  [Select][+][-]
  1.    if vlEditor.RowCount >= 3 then
  2.    begin
  3.       edCodeLabel.SetFocus;  // If this is omitted, first key or value column is erased depending on
  4.                                              // the last position of cursor.
  5.       for ti := 1 to vlEditor.RowCount-1 do vlEditor.Keys[ti] := Format('%5s', [trim(vlEditor.keys[ti])]);
  6.       vlEditor.Strings.Sort;
  7.       for ti := 1 to vlEditor.RowCount-1 do vlEditor.Keys[ti] := trim(vlEditor.keys[ti]);
  8.    end;
  9.  

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #1 on: August 17, 2018, 12:10:48 pm »
Does this also happen if you enclose this in BeginUpdate /EndUpdate?
You can also disable editing (so the editor is nof visible anymore): Options := Options-[goEdit]

What Lazarus version?
What OS?
What widgetset?

Bart

wp

  • Hero Member
  • *****
  • Posts: 8898
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #2 on: August 17, 2018, 12:11:43 pm »
The attached demo shows that sorting does not seem to be implemented for TValueListEditor at all. The left part of the form shows a TValueListEditor, the right part a standard TStringGrid, both with the same dummy entries. Clicking on the "Sort" button does have an effect on the TStringGrid, but nothing changes for the TValueListEditor.

Looking at this issue in more detail reveals that TValueListEditor and TStringGrid store the strings differently, but TValueListEditor does not implement its own Sort method (its Strings do implement a CustomSort but this is never called). So, I think the following patch fixes the issue.
  • Go to folder "lcl" of your Lazarus installation and make a backup copy of the file "valedit.pas"
  • Load this file into Lazarus and make these modifications
  • In the declaration part of TValueListEditor add a protected method "Sort":
 
Code: Pascal  [Select][+][-]
  1. type
  2.     TValueListEditor = class(TCustomStringGrid)
  3.     ...
  4.     protected
  5.      ...
  6.      procedure Sort(ColSorting: Boolean; index,IndxFrom,IndxTo:Integer); override;
  7.      ...
  8.     end;
  • And in the implementation part of the unit add this code:
Code: Pascal  [Select][+][-]
  1. procedure TValueListEditor.Sort(ColSorting: Boolean;
  2.   index,IndxFrom,IndxTo:Integer);
  3. begin
  4.   if not ColSorting then
  5.     raise Exception.Create('TValueListEditor: Only sorting by columns allowed.');
  6.   Strings.Sort;
  7. end;

With this modification the TValueListEditor short my demo correctly.

Please test. If it works correctly I can apply the changes to the Lazarus sources.
« Last Edit: August 17, 2018, 12:15:05 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #3 on: August 17, 2018, 12:20:11 pm »
Delphi does not implement Sort vor TValueListEditor AFAICS.

The problem lies in the fact that manipulating entries by code is easier (and less accident prone) if you manipulate the internals strings (TStrings).
The grid is just a fancy display for the internal strings...

Overriding Sort is OK with me.
But current example code should not mess up like it does.

Bart

wp

  • Hero Member
  • *****
  • Posts: 8898
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #4 on: August 17, 2018, 12:32:54 pm »
Sorry I don't understand...

To me the problem was that I called SortColRow which is a public method inherited from TCustomDrawGrid, and there is nothing forbidden in doing so. But this method does not seem to work for TValueListEditor, there were even cases in the input data crashing the program. Therefore, this must be fixed, and overriding TValueListEditor.Sort fixes it although my solution ignores the index parameters passed. maybe there are better solutions...

AFICS, Delphi does not implement direct sorting in none of its grids. Therefore, the issue does not show up here.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #5 on: August 17, 2018, 02:33:29 pm »
TS sorts the ValueListEditor using vlEditor.Strings.Sort;
This is not the same as TStringGrid.Sort.
The underlying data in a TValueListEditor are kept in a TStrings descendant.
All data manipulation is internally handled as manipulating these TStrings.
The grid is just a visual front-end.

It's an ugly design IMO.

However, directly manipulating the Strings property should not lead to visual anomalies, nor should it delete a cells content in this case.
Q: is the content really deleted, or does it just no show anymore?
I'm unsure how this can be fixed.

Your suggestion to implement Sort/CustomSort is good though, but it won't solve th eproblem TS described.
(The errormessage should of course be a resourcestring)
Feel free to implement that (otherwise open a bugreport an assign it to me).

There may be more methods of TCustomDrawGrid that may screw up a TValueListEditor  :(
I tried to override the ones I found in the past.

Bart

wp

  • Hero Member
  • *****
  • Posts: 8898
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #6 on: August 17, 2018, 04:28:40 pm »
@Bart: Thanks for the explanation. As usual I did not read the first post very carefully  ;) and did not notice the ValEdit.Strings.Sort - otherwise I propably would not have tried SortColRow...

I one found one more inconsistency: TValueListEditor.InsertRowWithValues adds a new row after, TStringGrid.InsertRowWithValues before the specified row index. See attached modified demo, Button "Insert row with values". Initially I thought, maybe ValueListEditor has Row=0 below the fixed row because the title row is handled differently, but in all other cases that i checked the indexing seems to be consistent between ValueListEditor and StringGrid.

OK, I can post the SortColRow patch to BugTracker for you to take care of it. Thank you.

@egsuh: I tried to follow your method of sorting the Strings of the TValueListEditor and don't see any blank rows (Button "Strings.Sort" below the ValueListEditor at the left). Could you please post a compilable project which shows the issue?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #7 on: August 17, 2018, 10:57:59 pm »
Delphi 7 has InsertRow(Key: string; Value: String; Append: Boolean);
The 3rd parameter controls if insert is before or after the current row.

@wp: thanks for the patch in the bugtracker. I'll take a look when I have time.

Bart
« Last Edit: August 17, 2018, 11:05:19 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #8 on: August 17, 2018, 11:30:01 pm »
Hmm, I cannot seem to reproduce the original problem of TS.

Strings.Sort sorts using Key column.
A sort using Value column might also come in handy?

Bart

wp

  • Hero Member
  • *****
  • Posts: 8898
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #9 on: August 17, 2018, 11:47:13 pm »
Hmm, I cannot seem to reproduce the original problem of TS.
Like me

Strings.Sort sorts using Key column.
A sort using Value column might also come in handy?
That's what a TValueListEditor.SortColRow could be good for. But now it strikes my mind that the flexibility to sort alongs rows and columns will always be confusing - sorting of the valuelisteditor can only be by colums. Maybe my entire patch is nonsense: Why not just override SortColRow in TValueListEditor and raise an exception with a proper error message to prevent the coder from using it?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #10 on: August 18, 2018, 12:00:19 am »
Let's continue discussing in the bugtracker.
Sorting TStrings on the Value pair might be a challenge though?

Bart

egsuh

  • Hero Member
  • *****
  • Posts: 855
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #11 on: August 20, 2018, 05:42:47 am »
@Bart,

Now it does not make blank cell, but the content of a cell is not changed after sorting.
I added a new form (Form2)  to your project, and added a button at the right-lower corner.

  1) Goto Form2,
  2) set any one cell of the valuelisteditor FOCUSED,
  3) then press "sort" tool button.

In your form (Form1), the focus is automatically moved to the button, so there are no problem in cell values.


wp

  • Hero Member
  • *****
  • Posts: 8898
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #12 on: August 20, 2018, 09:15:23 am »
Your project is not very helpful because it depends on non-common components (TCFrameEditor). Please modify it such that it only uses components of the standard Lazarus installation.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

egsuh

  • Hero Member
  • *****
  • Posts: 855
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #13 on: August 20, 2018, 11:50:02 am »
@wp

It's nothing but a frame containing TValueListEditor.
The difference is only that I use TToolButton instead of TButton.
Why should it make any difference in action?

Bart

  • Hero Member
  • *****
  • Posts: 4476
    • Bart en Mariska's Webstek
Re: TValueListEditor - A Cell is cleared after sorting
« Reply #14 on: August 20, 2018, 12:03:17 pm »
Please test latest patch in bugtracker issue (valedit.sort.3.diff) and if it does not work for you then also please report in the bugtracker.

I will not discuss this further in the forum.
The bugtracker is now the place where this is handled.

Bart

 

TinyPortal © 2005-2018