| 1 | unit UFormList;
|
|---|
| 2 |
|
|---|
| 3 | {$mode delphi}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
|
|---|
| 9 | ActnList, Menus, StdCtrls, ExtCtrls, SpecializedList, USqlDatabase,
|
|---|
| 10 | UListViewSort, UDataView, URegistry, UPrintPreview;
|
|---|
| 11 |
|
|---|
| 12 | type
|
|---|
| 13 | TColumnControlType = (ctText, ctReference, ctBoolean);
|
|---|
| 14 |
|
|---|
| 15 | TDbColumn = class
|
|---|
| 16 | Caption: string;
|
|---|
| 17 | Name: string;
|
|---|
| 18 | Visible: Boolean;
|
|---|
| 19 | Width: Integer;
|
|---|
| 20 | ControlType: TColumnControlType;
|
|---|
| 21 | ReferencedTable: string;
|
|---|
| 22 | end;
|
|---|
| 23 |
|
|---|
| 24 | { TListColumns }
|
|---|
| 25 |
|
|---|
| 26 | TListDbColumn = class(TListObject)
|
|---|
| 27 | function AddItem(Caption, Name: string; Visible: Boolean; Width: Integer): TDbColumn;
|
|---|
| 28 | end;
|
|---|
| 29 |
|
|---|
| 30 | { TDataViewList }
|
|---|
| 31 |
|
|---|
| 32 | TDataViewList = class(TDataView)
|
|---|
| 33 | Caption: string;
|
|---|
| 34 | Name: string;
|
|---|
| 35 | ImageIndex: Integer;
|
|---|
| 36 | Columns: TListDbColumn;
|
|---|
| 37 | constructor Create;
|
|---|
| 38 | destructor Destroy; override;
|
|---|
| 39 | procedure LoadFromRegistry(Context: TRegistryContext);
|
|---|
| 40 | procedure SaveToRegistry(Context: TRegistryContext);
|
|---|
| 41 | end;
|
|---|
| 42 |
|
|---|
| 43 | { TFormList }
|
|---|
| 44 |
|
|---|
| 45 | TFormList = class(TForm)
|
|---|
| 46 | AAdd: TAction;
|
|---|
| 47 | APrint: TAction;
|
|---|
| 48 | AExport: TAction;
|
|---|
| 49 | AReload: TAction;
|
|---|
| 50 | AModify: TAction;
|
|---|
| 51 | ADelete: TAction;
|
|---|
| 52 | ADuplicate: TAction;
|
|---|
| 53 | ActionList1: TActionList;
|
|---|
| 54 | LabelItemCount: TLabel;
|
|---|
| 55 | ListView1: TListView;
|
|---|
| 56 | MenuItem1: TMenuItem;
|
|---|
| 57 | MenuItem2: TMenuItem;
|
|---|
| 58 | MenuItem3: TMenuItem;
|
|---|
| 59 | MenuItem4: TMenuItem;
|
|---|
| 60 | MenuItem5: TMenuItem;
|
|---|
| 61 | Panel1: TPanel;
|
|---|
| 62 | PopupMenu1: TPopupMenu;
|
|---|
| 63 | PrintPreview1: TPrintPreview;
|
|---|
| 64 | ToolBar1: TToolBar;
|
|---|
| 65 | ToolButton1: TToolButton;
|
|---|
| 66 | ToolButton2: TToolButton;
|
|---|
| 67 | ToolButton3: TToolButton;
|
|---|
| 68 | ToolButton4: TToolButton;
|
|---|
| 69 | ToolButton5: TToolButton;
|
|---|
| 70 | ToolButton6: TToolButton;
|
|---|
| 71 | ToolButton7: TToolButton;
|
|---|
| 72 | procedure AAddExecute(Sender: TObject);
|
|---|
| 73 | procedure ADeleteExecute(Sender: TObject);
|
|---|
| 74 | procedure ADuplicateExecute(Sender: TObject);
|
|---|
| 75 | procedure AModifyExecute(Sender: TObject);
|
|---|
| 76 | procedure APrintExecute(Sender: TObject);
|
|---|
| 77 | procedure AReloadExecute(Sender: TObject);
|
|---|
| 78 | procedure FormCreate(Sender: TObject);
|
|---|
| 79 | procedure FormDestroy(Sender: TObject);
|
|---|
| 80 | procedure FormShow(Sender: TObject);
|
|---|
| 81 | procedure ListView1Data(Sender: TObject; Item: TListItem);
|
|---|
| 82 | procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 83 | Selected: Boolean);
|
|---|
| 84 | procedure PrintPreview1Print(Sender: TObject);
|
|---|
| 85 | private
|
|---|
| 86 | FView: TDataViewList;
|
|---|
| 87 | procedure SetView(AValue: TDataViewList);
|
|---|
| 88 | public
|
|---|
| 89 | DbRows: TDbRows;
|
|---|
| 90 | property View: TDataViewList read FView write SetView;
|
|---|
| 91 | procedure UpdateInterface;
|
|---|
| 92 | procedure UpdateData;
|
|---|
| 93 | end;
|
|---|
| 94 |
|
|---|
| 95 | var
|
|---|
| 96 | FormList: TFormList;
|
|---|
| 97 |
|
|---|
| 98 | implementation
|
|---|
| 99 |
|
|---|
| 100 | {$R *.lfm}
|
|---|
| 101 |
|
|---|
| 102 | uses
|
|---|
| 103 | UCore, UFormEdit, UFormMain;
|
|---|
| 104 |
|
|---|
| 105 | resourcestring
|
|---|
| 106 | SItemDeletion = 'Item deletion';
|
|---|
| 107 | SDoYouWantToDeleteItem = 'Do you want to really delete item?';
|
|---|
| 108 | SItemCount = 'Item count: %s';
|
|---|
| 109 | SModifyItem = 'Modify item';
|
|---|
| 110 | SAddItem = 'Add item';
|
|---|
| 111 |
|
|---|
| 112 | { TDataViewList }
|
|---|
| 113 |
|
|---|
| 114 | constructor TDataViewList.Create;
|
|---|
| 115 | begin
|
|---|
| 116 | Columns := TListDbColumn.Create;
|
|---|
| 117 | end;
|
|---|
| 118 |
|
|---|
| 119 | destructor TDataViewList.Destroy;
|
|---|
| 120 | begin
|
|---|
| 121 | Columns.Free;
|
|---|
| 122 | inherited Destroy;
|
|---|
| 123 | end;
|
|---|
| 124 |
|
|---|
| 125 | procedure TDataViewList.LoadFromRegistry(Context: TRegistryContext);
|
|---|
| 126 | begin
|
|---|
| 127 |
|
|---|
| 128 | end;
|
|---|
| 129 |
|
|---|
| 130 | procedure TDataViewList.SaveToRegistry(Context: TRegistryContext);
|
|---|
| 131 | begin
|
|---|
| 132 |
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | { TListColumns }
|
|---|
| 136 |
|
|---|
| 137 | function TListDbColumn.AddItem(Caption, Name: string; Visible: Boolean;
|
|---|
| 138 | Width: Integer): TDbColumn;
|
|---|
| 139 | begin
|
|---|
| 140 | Result := TDbColumn.Create;
|
|---|
| 141 | Result.Caption := Caption;
|
|---|
| 142 | Result.Name := Name;
|
|---|
| 143 | Result.Visible := Visible;
|
|---|
| 144 | Result.Width := Width;
|
|---|
| 145 | Add(Result);
|
|---|
| 146 | end;
|
|---|
| 147 |
|
|---|
| 148 | { TFormList }
|
|---|
| 149 |
|
|---|
| 150 | procedure TFormList.FormCreate(Sender: TObject);
|
|---|
| 151 | begin
|
|---|
| 152 | DbRows := TDbRows.Create;
|
|---|
| 153 | end;
|
|---|
| 154 |
|
|---|
| 155 | procedure TFormList.AModifyExecute(Sender: TObject);
|
|---|
| 156 | var
|
|---|
| 157 | Index: Integer;
|
|---|
| 158 | begin
|
|---|
| 159 | try
|
|---|
| 160 | FormEdit := TFormEdit.Create(FormMain);
|
|---|
| 161 | Index := FormMain.DataViewLists.IndexOf(View);
|
|---|
| 162 | FormEdit.ItemId := Integer(ListView1.Selected.Data);
|
|---|
| 163 | FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
|
|---|
| 164 | FormEdit.LoadFromDatabase;
|
|---|
| 165 | FormEdit.Caption := SModifyItem;
|
|---|
| 166 | Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
|
|---|
| 167 | if FormEdit.ShowModal = mrOk then begin
|
|---|
| 168 | FormEdit.SaveToDatabase;
|
|---|
| 169 | UpdateData;
|
|---|
| 170 | end;
|
|---|
| 171 | finally
|
|---|
| 172 | FormEdit.Free;
|
|---|
| 173 | end;
|
|---|
| 174 | end;
|
|---|
| 175 |
|
|---|
| 176 | procedure TFormList.APrintExecute(Sender: TObject);
|
|---|
| 177 | begin
|
|---|
| 178 | PrintPreview1.Preview;
|
|---|
| 179 | end;
|
|---|
| 180 |
|
|---|
| 181 | procedure TFormList.AReloadExecute(Sender: TObject);
|
|---|
| 182 | begin
|
|---|
| 183 | UpdateData;
|
|---|
| 184 | end;
|
|---|
| 185 |
|
|---|
| 186 | procedure TFormList.AAddExecute(Sender: TObject);
|
|---|
| 187 | var
|
|---|
| 188 | Index: Integer;
|
|---|
| 189 | begin
|
|---|
| 190 | try
|
|---|
| 191 | FormEdit := TFormEdit.Create(FormMain);
|
|---|
| 192 | Index := FormMain.DataViewLists.IndexOf(View);
|
|---|
| 193 | FormEdit.ItemId := 0;
|
|---|
| 194 | FormEdit.View := TDataViewForm(FormMain.DataViewForms[Index]);
|
|---|
| 195 | FormEdit.ClearData;
|
|---|
| 196 | FormEdit.Caption := SAddItem;
|
|---|
| 197 | Core.CoolTranslator1.TranslateComponentRecursive(FormEdit);
|
|---|
| 198 | if FormEdit.ShowModal = mrOk then begin
|
|---|
| 199 | FormEdit.SaveToDatabase;
|
|---|
| 200 | UpdateData;
|
|---|
| 201 | end;
|
|---|
| 202 | finally
|
|---|
| 203 | FormEdit.Free;
|
|---|
| 204 | end;
|
|---|
| 205 | end;
|
|---|
| 206 |
|
|---|
| 207 | procedure TFormList.ADeleteExecute(Sender: TObject);
|
|---|
| 208 | begin
|
|---|
| 209 | if MessageDlg(SItemDeletion, SDoYouWantToDeleteItem, mtConfirmation,
|
|---|
| 210 | [mbYes, mbNo], 0) = mrYes then begin
|
|---|
| 211 | Core.Database.Delete(View.Name, '`Id` = ' + IntToStr(Integer(ListView1.Selected.Data)));
|
|---|
| 212 | UpdateData;
|
|---|
| 213 | end;
|
|---|
| 214 | end;
|
|---|
| 215 |
|
|---|
| 216 | procedure TFormList.ADuplicateExecute(Sender: TObject);
|
|---|
| 217 | begin
|
|---|
| 218 | if FormEdit.ShowModal = mrOk then begin
|
|---|
| 219 | end;
|
|---|
| 220 | end;
|
|---|
| 221 |
|
|---|
| 222 | procedure TFormList.FormDestroy(Sender: TObject);
|
|---|
| 223 | begin
|
|---|
| 224 | ListView1.Items.Count := 0;
|
|---|
| 225 | FreeAndNil(DbRows);
|
|---|
| 226 | end;
|
|---|
| 227 |
|
|---|
| 228 | procedure TFormList.FormShow(Sender: TObject);
|
|---|
| 229 | begin
|
|---|
| 230 | UpdateInterface;
|
|---|
| 231 | end;
|
|---|
| 232 |
|
|---|
| 233 | procedure TFormList.ListView1Data(Sender: TObject; Item: TListItem);
|
|---|
| 234 | var
|
|---|
| 235 | I: Integer;
|
|---|
| 236 | begin
|
|---|
| 237 | if (Item.Index >= 0) and (Item.Index < DbRows.Count) then begin
|
|---|
| 238 | for I := 0 to View.Columns.Count - 1 do begin
|
|---|
| 239 | Item.Data := Pointer(StrToInt(DbRows[Item.Index].Values['Id']));
|
|---|
| 240 | if I = 0 then Item.Caption := DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]
|
|---|
| 241 | else Item.SubItems.Add(DbRows[Item.Index].Values[TDbColumn(View.Columns[I]).Name]);
|
|---|
| 242 | end;
|
|---|
| 243 | end;
|
|---|
| 244 | end;
|
|---|
| 245 |
|
|---|
| 246 | procedure TFormList.ListView1SelectItem(Sender: TObject; Item: TListItem;
|
|---|
| 247 | Selected: Boolean);
|
|---|
| 248 | begin
|
|---|
| 249 | UpdateInterface;
|
|---|
| 250 | end;
|
|---|
| 251 |
|
|---|
| 252 | procedure TFormList.PrintPreview1Print(Sender: TObject);
|
|---|
| 253 | begin
|
|---|
| 254 |
|
|---|
| 255 | end;
|
|---|
| 256 |
|
|---|
| 257 | procedure TFormList.SetView(AValue: TDataViewList);
|
|---|
| 258 | begin
|
|---|
| 259 | if FView = AValue then Exit;
|
|---|
| 260 | if Assigned(AValue) then begin
|
|---|
| 261 | end else begin
|
|---|
| 262 | DbRows.Clear;
|
|---|
| 263 | ListView1.Items.Clear;
|
|---|
| 264 | end;
|
|---|
| 265 | FView := AValue;
|
|---|
| 266 | end;
|
|---|
| 267 |
|
|---|
| 268 | procedure TFormList.UpdateInterface;
|
|---|
| 269 | begin
|
|---|
| 270 | AAdd.Enabled := True;
|
|---|
| 271 | AModify.Enabled := Assigned(ListView1.Selected);
|
|---|
| 272 | ADelete.Enabled := Assigned(ListView1.Selected);
|
|---|
| 273 | ADuplicate.Enabled := Assigned(ListView1.Selected);
|
|---|
| 274 | end;
|
|---|
| 275 |
|
|---|
| 276 | procedure TFormList.UpdateData;
|
|---|
| 277 | var
|
|---|
| 278 | NewColumn: TListColumn;
|
|---|
| 279 | I: Integer;
|
|---|
| 280 | Filter: string;
|
|---|
| 281 | begin
|
|---|
| 282 | ListView1.Columns.Clear;
|
|---|
| 283 | Filter := 'Id';
|
|---|
| 284 | for I := 0 to View.Columns.Count - 1 do
|
|---|
| 285 | with TDbColumn(View.Columns[I]) do begin
|
|---|
| 286 | NewColumn := ListView1.Columns.Add;
|
|---|
| 287 | NewColumn.Caption := Caption;
|
|---|
| 288 | NewColumn.Width := Width;
|
|---|
| 289 | NewColumn.Visible := Visible;
|
|---|
| 290 | Filter := Filter + ', `' + Name + '`';
|
|---|
| 291 | end;
|
|---|
| 292 | if Core.Database.Connected then
|
|---|
| 293 | Core.Database.Query(DbRows, 'SELECT ' + Filter + ' FROM ' + View.Name);
|
|---|
| 294 |
|
|---|
| 295 | ListView1.Items.Count := DbRows.Count;
|
|---|
| 296 | ListView1.Refresh;
|
|---|
| 297 |
|
|---|
| 298 | LabelItemCount.Caption := Format(SItemCount, [IntToStr(DbRows.Count)]);
|
|---|
| 299 | end;
|
|---|
| 300 |
|
|---|
| 301 | end.
|
|---|
| 302 |
|
|---|