Recent

Author Topic: removing sort indicator in TStringGrid  (Read 944 times)

inferno

  • New Member
  • *
  • Posts: 28
removing sort indicator in TStringGrid
« on: February 19, 2020, 12:10:22 am »
Hi all,
I use TStringGrid with allowed columns sorting and row moving (macOS Mojave 10.14.6 + Cocoa). When some column is first sorted and then the rows are moved, the column has no right order and the sorting indicator (default green arrows or some icons when used TitleImageList, ImageIndexSortAsc and ImageIndexSortDesc) should be removed. Have no idea to to do this. Assigning StringGrid.Columns.Title.ImageIndex:=-1; doesn't work - this value is equal -1 for all columns including the sorted one. Any suggestions?

Best regards,
Inferno

GAN

  • Sr. Member
  • ****
  • Posts: 288
Re: removing sort indicator in TStringGrid
« Reply #1 on: February 19, 2020, 12:55:30 am »
Lazarus 1.8.4 FPC 3.0.4 Linux Mint Mate 17.2 x86_64 GTK-2
Zeos 7.1.3 - Sqlite 3.8.2

Foro Lazarus en español http://forum.lazarus.freepascal.org/index.php/board,73.0.html

inferno

  • New Member
  • *
  • Posts: 28
Re: removing sort indicator in TStringGrid
« Reply #2 on: February 19, 2020, 01:25:06 am »
GAN,
Thank you for the suggestion, but I found this thread before posting and unfortunately this is not a solution in my case. As I mentioned, assigning StringGrid.Columns.Title.ImageIndex:=-1 doesn't work at all. Another sugestion TStringGrid.ColumnClickSort := False is a solution only when you want to completely disable sorting. My case is different: columns sorting is allowed, but sometimes user can disturb the order by row moving (also allowed by goRowMoving:=true). In this case sorting icon should be hidden because column is no longer sorted. But when user after moving rows again click in some column header, sorting should be activated and sort indicator should be displayed again according to sort order.

In other words, I need a solution to temporary hide sort indicator until next sort task.

Best regards,
Inferno

winni

  • Hero Member
  • *****
  • Posts: 1460
Re: removing sort indicator in TStringGrid
« Reply #3 on: February 19, 2020, 01:31:43 am »
Hi!

Do the drawing of the cells on your own:

Set DefaultDrawing to false.

Create OnDrawCell.

No you can draw in row zero every icon you like. But you must keep track of it.

Winni


wp

  • Hero Member
  • *****
  • Posts: 7218
Re: removing sort indicator in TStringGrid
« Reply #4 on: February 19, 2020, 07:24:26 am »
I added a public method "HideSortArrow" in r62644.

If you do not use Lazarus trunk you can patch your current version easily:
  • Open file grids.pp from folder lcl of your Lazarus installation
  • In the interface part of the unit find the public declarations of TCustomGrid and add the line
Code: Pascal  [Select][+][-]
  1. procedure HideSortArrow;
  • In the implementation part add this code (I put it after the TCustomGrid.Sort method):
Code: Pascal  [Select][+][-]
  1. procedure TCustomGrid.HideSortArrow;
  2. begin
  3.   FSortColumn := -1;
  4.   InvalidateGrid;
  5. end;
    In principle this method should be called whenever rows are rearranged in a sorted grid. The place where this should happen is procedure ColRowModed, but I am not 100% sure if this has any side-effects. Therefore, you must call HideSortArrow yourself in the OnColRowMoved method.
    « Last Edit: February 19, 2020, 09:09:29 am by wp »
    Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

    inferno

    • New Member
    • *
    • Posts: 28
    Re: removing sort indicator in TStringGrid
    « Reply #5 on: February 20, 2020, 01:13:02 am »
    Thank you for suggestions! It looks the solution with adding HideSortArrow to TCustomGrid works well, no side-effects until now. But I hope it will be implemented directly in TCustomGrid as there are many cases where sorting order can be changed. It is definitely something that was missed during sorting implementation.

    Best regards,
    Inferno

    wp

    • Hero Member
    • *****
    • Posts: 7218
    Re: removing sort indicator in TStringGrid
    « Reply #6 on: February 23, 2020, 01:02:09 pm »
    I now changed the grid's internal method DoOPMoveColRow to remove the sort arrow when a row is moved in a column-sorted grid without user code. I think it works correctly, but please test yourself and report back.

    To implement the change in a non-trunk Lazarus version do this:
    • Load grids.pas (from folder "lcl" of your Lazarus installation)
    • Find procedure TCustomGrid.DoOPMoveColRow
    • Near the end, there is an "if" block with the comment "// adjust sorted column"
    • Add an "else" block with "FSortColumn := -1". The entire "if" block should be like this:
    Code: Pascal  [Select][+][-]
    1.   // adjust sorted column
    2.   if IsColumn and (FSortColumn>=0) then begin       // "begin" added
    3.     if Between(FSortColumn, FromIndex, ToIndex) then begin  
    4.       if FSortColumn=FromIndex then
    5.         FSortColumn := ToIndex
    6.       else
    7.       if FromIndex<FSortColumn then
    8.         Dec(FSortColumn)
    9.       else
    10.         Inc(FSortColumn);
    11.     end;
    12.   end else                // added
    13.     FSortColumn := -1;    // added
                     
    Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

    inferno

    • New Member
    • *
    • Posts: 28
    Re: removing sort indicator in TStringGrid
    « Reply #7 on: February 26, 2020, 07:36:01 pm »
    Hi wp,
    Thank you for the update! I checked your solution and it looks it works properly. I thought about such approach (removing sort indicator automatically without invoking dedicated method after any row movement) but probably this could be not expected behaviour in each scenario. Not each movement operation disrupt sort order. For example if you have sorted field with values:

    bananas
    bananas
    oranges
    strawberries
    tomatoes

    and make row movement between first two rows, the sort order will be kept and removing sort indicator is not necessary.

    Also I think to be consistent the same modification should be done in TCustomGrid.DoOPExchangeColRow. There is method ExchangeColRow and using it somewhere in the code can cause sort order disruption, so the sort icon should be removed also in this case. But on the other hand ExchangeColRow method is used by the grid during sorting, so it could be not as easy to implement as with MoveColRow method.

    So I think that using dedicated method for removing the sort indicator is enough and is more flexible.

    BTW Do you have idea how to use the sort indicators in higher resolutions than 16x16 (icons with higher resolution are not scaled)? I develop on macOS with Retina display and such icons looks very poor comparing to the rest of Cocoa GUI which is very sharp. The same issue have with TTrayIcon.

    Best regards,
    Inferno

    wp

    • Hero Member
    • *****
    • Posts: 7218
    Re: removing sort indicator in TStringGrid
    « Reply #8 on: February 26, 2020, 11:57:00 pm »
    Sounds reasonable. Reverted the change.

    Regarding the sort indicator: Which Lazarus version are you using? I checked v2.0.6 and it does contain the high-resolution imagelist for the grids unit.

    If you do have v2.0.6 or trunk what happens with the sort indicator when you change the TitleStyle of the grid?
    Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

    inferno

    • New Member
    • *
    • Posts: 28
    Re: removing sort indicator in TStringGrid
    « Reply #9 on: April 13, 2020, 04:37:15 pm »
    Hi,
    I use Typhon IDE. Changing TitleStyle doesn't help - tried all three setttings. Image List contains high resolution icons but they are converted to default resolution 16x16 in the grid.

    Best regards,
    Inferno

    wp

    • Hero Member
    • *****
    • Posts: 7218
    Re: removing sort indicator in TStringGrid
    « Reply #10 on: April 13, 2020, 04:57:24 pm »
    Then ask in the CodeTyphon forum. I have neither an idea nor influence on what they are doing.
    Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

    inferno

    • New Member
    • *
    • Posts: 28
    Re: removing sort indicator in TStringGrid
    « Reply #11 on: April 13, 2020, 05:14:33 pm »
    Hi wp,
    I expect mostly they are just port the code without any changes...

    Best regards,
    Inferno

    wp

    • Hero Member
    • *****
    • Posts: 7218
    Re: removing sort indicator in TStringGrid
    « Reply #12 on: April 13, 2020, 05:46:38 pm »
    At least they remove the name "Lazarus" everywhere and replace it by their own...

    But anyway: I just checked your report on a Windows running at 144ppi, and the custom sort images are correctly scaled. Did you set the Scaled property of the Imagelist to true?
    Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

    inferno

    • New Member
    • *
    • Posts: 28
    Re: removing sort indicator in TStringGrid
    « Reply #13 on: April 13, 2020, 11:13:09 pm »
    That's it, thank you wp! I knew it must be something easy to fix. It was confusing that regarless of icon size the ImageList always displayed the resized version 16x16.

    How about TTrayIcon - do you have any idea how to add scaled icon? In this case there is no ImageList and icon is not resized or scaled. If you add 16x16 it is fully displayed, if the icon resolution is higher only the part of the picture is displayed.

    Regarding Typhon - you are right, they should keep the origin of source codes. I suppose they have ambitions to provide full-featured multiplatform IDE with as many components as possible and with homogenus source codes tracked myself. On the other hand they provide it to everybody for free and each Typhon user is fully aware that it comes from Lazarus and is based on hard work of the Lazarus community. In my opinion it is better choice for macOS.

    Best regards,
    Inferno

    wp

    • Hero Member
    • *****
    • Posts: 7218
    Re: removing sort indicator in TStringGrid
    « Reply #14 on: April 14, 2020, 12:20:51 am »
    I am not very familiar with TTrayIcon. Maybe it's best if you post a report in bug tracker. Add a simple demo showing the issue.
    Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

     

    TinyPortal © 2005-2018