Follow up to rev 870 and 958:

Implement an optimized and universal way to safe and restore the column visibibility of ListTables (and all other VirtualStringTrees) by merging that code with the code for the column-resizing feature.
- Drops global var ListTablesColumnNames and proc SetupListTablesHeader.
- Change + move "RestoreColumnWidths" to private "RestoreListSetup", add code from SetupListTablesHeader
- Change + move "SafeColumnWidths" to private "SafeListSetup", add code from MenuTablelistColumnsClick
- Default visible columns are (must be) set at design time now

Pro:
- RestoreListSetup is called once in FormShow, while SetupListTablesHeader was called each time in LoadDatabaseProperties.
- SafeListSetup is only called once in FormClose
- Just safes the column indexes, not the column names in registry. Makes it safe to rename columns in the future
- Less registry read/write access in general.
- Safe/RestoreListSetup open an easy way to implement further list features (column moving, etc.)
- Makes it a cakewalk to implement the same column un/hiding feature for the remaining 4 Lists.
Contra:
- people/developers who used a build from 870 to this one on will see their activated columns in ListTables reset to the default (7 first columns) because the registry key and value has changed. (but: better do that now than after an official relase)
This commit is contained in:
Ansgar Becker
2007-09-28 21:08:37 +00:00
parent cfafa94e68
commit 6e9a08c12f
3 changed files with 140 additions and 145 deletions

View File

@ -439,71 +439,71 @@ object MDIChild: TMDIChild
WideText = 'Comment' WideText = 'Comment'
end end
item item
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 7 Position = 7
WideText = 'Version' WideText = 'Version'
end end
item item
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 8 Position = 8
Width = 70 Width = 70
WideText = 'Row format' WideText = 'Row format'
end end
item item
Alignment = taRightJustify Alignment = taRightJustify
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 9 Position = 9
Width = 70 Width = 70
WideText = 'Avg row length' WideText = 'Avg row length'
end end
item item
Alignment = taRightJustify Alignment = taRightJustify
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 10 Position = 10
Width = 70 Width = 70
WideText = 'Max data length' WideText = 'Max data length'
end end
item item
Alignment = taRightJustify Alignment = taRightJustify
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 11 Position = 11
Width = 70 Width = 70
WideText = 'Index length' WideText = 'Index length'
end end
item item
Alignment = taRightJustify Alignment = taRightJustify
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 12 Position = 12
Width = 70 Width = 70
WideText = 'Data free' WideText = 'Data free'
end end
item item
Alignment = taRightJustify Alignment = taRightJustify
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 13 Position = 13
Width = 90 Width = 90
WideText = 'Auto increment' WideText = 'Auto increment'
end end
item item
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 14 Position = 14
Width = 120 Width = 120
WideText = 'Check time' WideText = 'Check time'
end end
item item
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 15 Position = 15
Width = 70 Width = 70
WideText = 'Collation' WideText = 'Collation'
end end
item item
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 16 Position = 16
Width = 70 Width = 70
WideText = 'Checksum' WideText = 'Checksum'
end end
item item
Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark, coVisible] Options = [coAllowClick, coEnabled, coParentBidiMode, coParentColor, coResizable, coShowDropMark]
Position = 17 Position = 17
Width = 70 Width = 70
WideText = 'Create options' WideText = 'Create options'
@ -2266,15 +2266,6 @@ object MDIChild: TMDIChild
AutoLineReduction = maManual AutoLineReduction = maManual
Left = 72 Left = 72
Top = 80 Top = 80
object DefaultColumnLayout1: TMenuItem
Tag = 1
Caption = 'Default columns'
Checked = True
OnClick = MenuTablelistColumnsClick
end
object N20: TMenuItem
Caption = '-'
end
end end
object SynCompletionProposal1: TSynCompletionProposal object SynCompletionProposal1: TSynCompletionProposal
Options = [scoLimitToMatchedText, scoUseInsertList, scoUsePrettyText, scoUseBuiltInTimer, scoEndCharCompletion, scoCompleteWithTab, scoCompleteWithEnter] Options = [scoLimitToMatchedText, scoUseInsertList, scoUsePrettyText, scoUseBuiltInTimer, scoEndCharCompletion, scoCompleteWithTab, scoCompleteWithEnter]

View File

@ -221,8 +221,6 @@ type
ButtonDataSearch: TButton; ButtonDataSearch: TButton;
Find1: TMenuItem; Find1: TMenuItem;
popupDbGridHeader: TPopupMenu; popupDbGridHeader: TPopupMenu;
DefaultColumnLayout1: TMenuItem;
N20: TMenuItem;
SynCompletionProposal1: TSynCompletionProposal; SynCompletionProposal1: TSynCompletionProposal;
popupQueryLoad: TPopupMenu; popupQueryLoad: TPopupMenu;
OpenDialogSQLFile: TOpenDialog; OpenDialogSQLFile: TOpenDialog;
@ -522,7 +520,6 @@ type
UserQueryFiring : Boolean; UserQueryFiring : Boolean;
CachedTableLists : TStringList; CachedTableLists : TStringList;
QueryHelpersSelectedItems : Array[0..3] of Integer; QueryHelpersSelectedItems : Array[0..3] of Integer;
ListTablesColumnNames : TStringList;
CreateDatabaseForm : TCreateDatabaseForm; CreateDatabaseForm : TCreateDatabaseForm;
TablePropertiesForm : Ttbl_properties_form; TablePropertiesForm : Ttbl_properties_form;
@ -535,12 +532,14 @@ type
function RunThreadedQuery(AQuery : String) : TMysqlQuery; function RunThreadedQuery(AQuery : String) : TMysqlQuery;
procedure DisplayRowCountStats(ds: TDataSet); procedure DisplayRowCountStats(ds: TDataSet);
procedure insertFunction(Sender: TObject); procedure insertFunction(Sender: TObject);
procedure SetupListTablesHeader;
function GetActiveDatabase: string; function GetActiveDatabase: string;
function GetSelectedTable: string; function GetSelectedTable: string;
procedure SetSelectedDatabase(db: string); procedure SetSelectedDatabase(db: string);
procedure SetSelectedTable(table: string); procedure SetSelectedTable(table: string);
procedure ProcessClientSQL(command: String; parameter: String); procedure ProcessClientSQL(command: String; parameter: String);
procedure SaveListSetup( List: TVirtualStringTree );
procedure RestoreListSetup( List: TVirtualStringTree );
procedure SetVisibleListColumns( List: TVirtualStringTree; Columns: TStringList );
public public
TemporaryDatabase : String; TemporaryDatabase : String;
@ -891,28 +890,6 @@ var
menuitem : Tmenuitem; menuitem : Tmenuitem;
reg : TRegistry; reg : TRegistry;
{**
Sub-procedure: Restore width of list-columns from registry
}
procedure RestoreColumnWidths( List: TVirtualStringTree );
var
i : Byte;
colwidth : Integer;
colwidths : TStringList;
begin
if not reg.ValueExists( REGPREFIX_COLWIDTHS + List.Name ) then
exit;
colwidths := Explode( ',', reg.ReadString( REGPREFIX_COLWIDTHS + List.Name ) );
for i := 0 to colwidths.Count - 1 do
begin
colwidth := MakeInt(colwidths[i]);
// Check if column number exists and width is at least 1 pixel
if (List.Header.Columns.Count > i) and (colwidth > 0) then
List.Header.Columns[i].Width := colwidth;
end;
FreeAndNil(colwidths);
end;
begin begin
reg := TRegistry.Create; reg := TRegistry.Create;
if reg.OpenKey( REGPATH, true ) then if reg.OpenKey( REGPATH, true ) then
@ -1035,11 +1012,11 @@ begin
prefRememberFilters := True; prefRememberFilters := True;
// Restore width of columns of all VirtualTrees // Restore width of columns of all VirtualTrees
RestoreColumnWidths(ListVariables); RestoreListSetup(ListVariables);
RestoreColumnWidths(ListProcesses); RestoreListSetup(ListProcesses);
RestoreColumnWidths(ListCommandStats); RestoreListSetup(ListCommandStats);
RestoreColumnWidths(ListTables); RestoreListSetup(ListTables);
RestoreColumnWidths(ListColumns); RestoreListSetup(ListColumns);
// Open server-specific registry-folder. // Open server-specific registry-folder.
// relative from already opened folder! // relative from already opened folder!
@ -1050,6 +1027,20 @@ begin
end; end;
reg.CloseKey; reg.CloseKey;
reg.Free; reg.Free;
// Generate menuitems for popupDbGridHeader (column selection for ListTables)
popupDBGridHeader.Items.Clear;
for i:=0 to ListTables.Header.Columns.Count-1 do
begin
menuitem := TMenuItem.Create( popupDBGridHeader );
menuitem.Caption := ListTables.Header.Columns[i].Text;
menuitem.OnClick := MenuTablelistColumnsClick;
// Disable hiding first column
menuitem.Enabled := i>0;
menuitem.Checked := coVisible in ListTables.Header.Columns[i].Options;
popupDbGridHeader.Items.Add( menuitem );
end;
end; end;
@ -1057,25 +1048,6 @@ procedure TMDIChild.FormClose(Sender: TObject; var Action: TCloseAction);
var var
ws : String; ws : String;
reg : TRegistry; reg : TRegistry;
{**
Sub-procedure: Save width of list-columns to registry
}
procedure SaveColumnWidths( List: TVirtualStringTree );
var
i : Byte;
colwidths : String;
begin
colwidths := '';
for i := 0 to List.Header.Columns.Count - 1 do
begin
if colwidths <> '' then
colwidths := colwidths + ',';
colwidths := colwidths + IntToStr(List.Header.Columns[i].Width);
end;
reg.WriteString( REGPREFIX_COLWIDTHS + List.Name, colwidths );
end;
begin begin
SetWindowConnected( false ); SetWindowConnected( false );
SetWindowName( main.discname ); SetWindowName( main.discname );
@ -1115,11 +1087,11 @@ begin
WriteInteger( 'sqloutheight', PageControlBottom.Height ); WriteInteger( 'sqloutheight', PageControlBottom.Height );
// Save width of probably resized columns of all VirtualTrees // Save width of probably resized columns of all VirtualTrees
SaveColumnWidths(ListVariables); SaveListSetup(ListVariables);
SaveColumnWidths(ListProcesses); SaveListSetup(ListProcesses);
SaveColumnWidths(ListCommandStats); SaveListSetup(ListCommandStats);
SaveColumnWidths(ListTables); SaveListSetup(ListTables);
SaveColumnWidths(ListColumns); SaveListSetup(ListColumns);
// Open server-specific registry-folder. // Open server-specific registry-folder.
// relative from already opened folder! // relative from already opened folder!
@ -2025,9 +1997,6 @@ begin
ListTables.BeginUpdate; ListTables.BeginUpdate;
ListTables.Clear; ListTables.Clear;
// (Un)hides (un)selected column names in list
SetupListTablesHeader;
SetLength(VTRowDataListTables, ds.RecordCount); SetLength(VTRowDataListTables, ds.RecordCount);
for i := 1 to ds.RecordCount do for i := 1 to ds.RecordCount do
begin begin
@ -6155,77 +6124,19 @@ end;
procedure TMDIChild.MenuTablelistColumnsClick(Sender: TObject); procedure TMDIChild.MenuTablelistColumnsClick(Sender: TObject);
var var
menuitem : TMenuItem; menuitem : TMenuItem;
reg : TRegistry; VisibleColumns : TStringList;
begin
menuitem := (Sender as TMenuItem);
reg := TRegistry.Create;
reg.OpenKey( REGPATH, true );
if ListTablesColumnNames.IndexOf( menuitem.Caption ) > -1 then
ListTablesColumnNames.Delete( ListTablesColumnNames.IndexOf( menuitem.Caption ) )
else
ListTablesColumnNames.Add( menuitem.Caption );
// Store list of columns in registry
reg.WriteString( REGNAME_LISTTABLESCOLUMNNAMES, ListTablesColumnNames.DelimitedText );
SetupListTablesHeader;
end;
{**
(Un)hides (un)selected columns in ListTables
}
procedure TMDIChild.SetupListTablesHeader;
var
reg : TRegistry;
i : Integer; i : Integer;
menuitem : TMenuItem;
begin begin
if ListTablesColumnNames = nil then VisibleColumns := TStringList.Create;
begin menuitem := TMenuItem( Sender );
// First time we read the columns list from registry into this global variable menuitem.Checked := not menuitem.Checked;
ListTablesColumnNames := TStringList.Create;
reg := TRegistry.Create;
reg.openkey( REGPATH, true );
if reg.ValueExists( REGNAME_LISTTABLESCOLUMNNAMES ) then
begin
ListTablesColumnNames.DelimitedText := reg.ReadString( REGNAME_LISTTABLESCOLUMNNAMES );
// Ensure first column (Table) is always visible
if ListTablesColumnNames.IndexOf(ListTables.Header.Columns[0].Text) = -1 then
ListTablesColumnNames.Add( ListTables.Header.Columns[0].Text );
end
else begin
// If not set, by default make the first 7 columns visible
for i := 0 to ListTables.Header.Columns.Count - 1 do for i := 0 to ListTables.Header.Columns.Count - 1 do
begin begin
ListTablesColumnNames.Add(ListTables.Header.Columns[i].Text); menuitem := popupDbGridHeader.Items[i];
if i >= 6 then if menuitem.Checked then
break; VisibleColumns.Add(IntToStr(i));
end;
end;
reg.Free;
end;
// All columns in ListTables are created at designtime. Only (un)hide them here
// Plus generate menuitems for popupDBgridColumns
popupDBGridHeader.Items.Clear;
for i:=0 to ListTables.Header.Columns.Count-1 do
begin
menuitem := TMenuItem.Create( popupDBGridHeader );
menuitem.Caption := ListTables.Header.Columns[i].Text;
menuitem.OnClick := MenuTablelistColumnsClick;
// Disable hiding first column
menuitem.Enabled := i>0;
popupDbGridHeader.Items.Add( menuitem );
if (i=0) or (ListTablesColumnNames.IndexOf( ListTables.Header.Columns[i].Text ) > -1) then
begin
ListTables.Header.Columns[i].Options := ListTables.Header.Columns[i].Options + [coVisible];
menuitem.Checked := True;
end
else begin
ListTables.Header.Columns[i].Options := ListTables.Header.Columns[i].Options - [coVisible];
menuitem.Checked := False;
end;
end; end;
SetVisibleListColumns( ListTables, VisibleColumns );
end; end;
@ -6355,4 +6266,96 @@ begin
end; end;
{**
Save setup of a VirtualStringTree to registry
}
procedure TMDIChild.SaveListSetup( List: TVirtualStringTree );
var
i : Byte;
ColWidths, ColsVisible : String;
reg : TRegistry;
begin
reg := TRegistry.Create;
reg.OpenKey( REGPATH, true );
ColWidths := '';
ColsVisible := '';
for i := 0 to List.Header.Columns.Count - 1 do
begin
// Column widths
if ColWidths <> '' then
ColWidths := ColWidths + ',';
ColWidths := ColWidths + IntToStr(List.Header.Columns[i].Width);
// Column visibility
if coVisible in List.Header.Columns[i].Options then
begin
if ColsVisible <> '' then
ColsVisible := ColsVisible + ',';
ColsVisible := ColsVisible + IntToStr(i);
end;
end;
reg.WriteString( REGPREFIX_COLWIDTHS + List.Name, ColWidths );
reg.WriteString( REGPREFIX_COLSVISIBLE + List.Name, ColsVisible );
end;
{**
Restore setup of VirtualStringTree from registry
}
procedure TMDIChild.RestoreListSetup( List: TVirtualStringTree );
var
i : Byte;
colwidth : Integer;
ValueList : TStringList;
reg : TRegistry;
begin
reg := TRegistry.Create;
reg.OpenKey( REGPATH, true );
ValueList := TStringList.Create;
// Column widths
if reg.ValueExists( REGPREFIX_COLWIDTHS + List.Name ) then
begin
ValueList := Explode( ',', reg.ReadString( REGPREFIX_COLWIDTHS + List.Name ) );
for i := 0 to ValueList.Count - 1 do
begin
colwidth := MakeInt(ValueList[i]);
// Check if column number exists and width is at least 1 pixel
if (List.Header.Columns.Count > i) and (colwidth > 0) then
List.Header.Columns[i].Width := colwidth;
end;
end;
// Column visibility
if reg.ValueExists( REGPREFIX_COLSVISIBLE + List.Name ) then
begin
ValueList := Explode( ',', reg.ReadString( REGPREFIX_COLSVISIBLE + List.Name ) );
SetVisibleListColumns( List, ValueList );
end;
reg.Free;
ValueList.Free;
end;
{**
(Un)hide columns in a VirtualStringTree.
}
procedure TMDIChild.SetVisibleListColumns( List: TVirtualStringTree; Columns: TStringList );
var
i : Integer;
begin
for i := 0 to List.Header.Columns.Count - 1 do
begin
if Columns.IndexOf( IntToStr(i) ) > -1 then
List.Header.Columns[i].Options := List.Header.Columns[i].Options + [coVisible]
else
List.Header.Columns[i].Options := List.Header.Columns[i].Options - [coVisible];
end;
end;
end. end.

View File

@ -41,6 +41,7 @@ const
REGNAME_LISTTABLESCOLUMNNAMES = 'ListTablesColumnNames'; REGNAME_LISTTABLESCOLUMNNAMES = 'ListTablesColumnNames';
REGPREFIX_COLWIDTHS = 'ColWidths_'; REGPREFIX_COLWIDTHS = 'ColWidths_';
REGPREFIX_COLSVISIBLE = 'ColsVisible_';
// how much memory we're aiming to use for the // how much memory we're aiming to use for the
// data grid and it's automatic limit function // data grid and it's automatic limit function