unit U_ComponTypesMarkMask;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, RzButton, RzPanel, ExtCtrls, ActnList, ActnMan,
  cxSpinEdit, cxCheckBox, cxCurrencyEdit, cxColorComboBox,
  cxLookAndFeelPainters, StdCtrls, cxButtons,

  U_BaseCommon, Menus, kbmMemTable, XPMenu, siComp, siLngLnk, cxMemo,
  cxTextEdit, cxDBLookupComboBox, cxLookAndFeels, cxNavigator,
  PlatformDefaultStyleActnCtrls;

type
  TF_ComponTypesMarkMask = class(TForm)
    RzPanel1: TRzPanel;
    RzToolbar1: TRzToolbar;
    RzToolButton2: TRzToolButton;
    GT_CatalogMarkMasks: TcxGridDBTableView;
    GL_CatalogMarkMasks: TcxGridLevel;
    Grid_CatalogMarkMasks: TcxGrid;
    ActionManager1: TActionManager;
    Act_AddMask: TAction;
    Act_EditMask: TAction;
    Act_DelMask: TAction;
    GT_CatalogMarkMasksCompTypeName: TcxGridDBColumn;
    GT_CatalogMarkMasksMarkMask: TcxGridDBColumn;
    Panel_OkCancel: TRzPanel;
    RzSpacer1: TRzSpacer;
    Button_OK: TcxButton;
    Button_Cancel: TcxButton;
    PopupMenu_CatalogMarkMasks: TPopupMenu;
    ActEditMask1: TMenuItem;
    MemTable_MarkTemplates: TkbmMemTable;
    DataSource_MT_MarkTemplates: TDataSource;
    XPMenu1: TXPMenu;
    lng_Forms: TsiLangLinked;
    procedure FormCreate(Sender: TObject);
    procedure Act_EditMaskExecute(Sender: TObject);
    procedure GT_CatalogMarkMasksDblClick(Sender: TObject);
    procedure Panel_OkCancelResize(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    GForm: TForm;

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;

    function ChangeMarkMasks(AMarkMaskList: Tlist): Boolean;
    procedure LoadDataFromMarkMaskList(AMarkMaskList: Tlist; AAsNew: Boolean);
    function SaveDataToMarkMaskList(AMarkMaskList: Tlist): Boolean;
  end;

//var
 // F_ComponTypesMarkMask: TF_ComponTypesMarkMask;

implementation
Uses Unit_DM_SCS, U_Main{, kbmMemTable};
{$R *.dfm}


constructor TF_ComponTypesMarkMask.Create(AOwner: TComponent; AForm: TForm);
begin
    GForm := AForm;
    inherited Create(AOwner);
end;

destructor TF_ComponTypesMarkMask.Destroy;
begin
    inherited;
end;


procedure TF_ComponTypesMarkMask.FormCreate(Sender: TObject);
begin
    MemTable_MarkTemplates.Active := false;
    MemTable_MarkTemplates.FieldDefs.Add('ID', ftInteger);
    MemTable_MarkTemplates.FieldDefs.Add('ID_Catalog', ftInteger);
    MemTable_MarkTemplates.FieldDefs.Add('ID_Component_Type', ftInteger);
    MemTable_MarkTemplates.FieldDefs.Add('Comp_Type_Name', ftString, 255);
    MemTable_MarkTemplates.FieldDefs.Add('MARK_MASK', ftString, 200);
    MemTable_MarkTemplates.FieldDefs.Add('isModified', ftBoolean);
    MemTable_MarkTemplates.FieldDefs.Add('isNew', ftBoolean);

    GT_CatalogMarkMasks.DataController.DataSource := DataSource_MT_MarkTemplates;
end;



function TF_ComponTypesMarkMask.ChangeMarkMasks(AMarkMaskList: Tlist): Boolean;
var ModRes: TModalResult;
begin
  Result := false;
  try
    LoadDataFromMarkMaskList(AMarkMaskList, false);
    ModRes := ShowModal;
    if ModRes = mrOK then
      if SaveDataToMarkMaskList(AMarkMaskList) then
        Result := true;
  except
    on E: Exception do AddExceptionToLog('TF_ComponTypesMarkMask.ChangeMarkMasks: '+E.Message);
  end;
end;


// ##### Загрузка данных #####
procedure TF_ComponTypesMarkMask.LoadDataFromMarkMaskList(AMarkMaskList: Tlist; AAsNew: Boolean);
var i: integer;
    ptrCatalogMarkMask: PCatalogMarkMask;
    ComponentType: TComponentType;
begin
    try
      if AMarkMaskList = nil then
        Exit; /// EXIT ///

      //with TF_Main(GForm).DM.MemTable_CatalogMarkMask do
      with MemTable_MarkTemplates do
        begin
            Active := false;
            Active := true;
            for i := 0 to AMarkMaskList.Count - 1 do
              begin
                  ptrCatalogMarkMask := AMarkMaskList[i];
                  if ptrCatalogMarkMask <> nil then
                    begin
                        //*** Определить наименование Типа
                        ComponentType := F_NormBase.DM.GetComponentType(ptrCatalogMarkMask.IDComponentType);

                        Append;
                        FieldByName('ID').AsInteger := ptrCatalogMarkMask.ID;
                        FieldByName('ID_Catalog').AsInteger := ptrCatalogMarkMask.IDCatalog;
                        FieldByName('ID_Component_Type').AsInteger := ptrCatalogMarkMask.IDComponentType;
                        FieldByName('Comp_Type_Name').AsString := ComponentType.NAME;
                        FieldByName('MARK_MASK').AsString := ptrCatalogMarkMask.MarkMask;
                        FieldByName('isModified').AsBoolean := false;
                        FieldByName('isNew').AsBoolean := AAsNew;
                        Post;

                        ptrCatalogMarkMask.MakeEdit := meNone;
                    end;
              end;
            First;
        end;
    except
      on E: Exception do AddExceptionToLog('TF_ComponTypesMarkMask.LoadDataFromMarkMaskList: '+E.Message);
    end;
end;


// ##### Сохранение данных #####
function TF_ComponTypesMarkMask.SaveDataToMarkMaskList(AMarkMaskList: Tlist): Boolean;
var i: integer;
    ptrCatalogMarkMask: PCatalogMarkMask;
    FindedComponType: Boolean;
begin
    Result := false;
    try
      //with TF_Main(GForm).DM.MemTable_CatalogMarkMask do
      with MemTable_MarkTemplates do
        if Active then
          begin
              First;
              while Not Eof do
                begin
                    if (FieldByName('ismodified').AsBoolean = true) or
                       (FieldByName('isNew').AsBoolean = true) then
                      begin
                          FindedComponType := false;
                          ptrCatalogMarkMask := nil;
                          for i := 0 to AMarkMaskList.Count - 1 do
                            begin
                                ptrCatalogMarkMask := AMarkMaskList[i];
                                if ptrCatalogMarkMask <> nil then
                                  //if ptrCatalogMarkMask.ID = FieldByName('ID').AsInteger then
                                  if ptrCatalogMarkMask.IDComponentType = FieldByName(fnIDComponentType).AsInteger then
                                    begin
                                        ptrCatalogMarkMask.MarkMask := FieldByName(fnMarkMask).AsString;
                                        ptrCatalogMarkMask.MakeEdit := meEdit;
                                        FindedComponType := true;
                                        Break; ///// BREAK /////
                                    end;
                            end;

                            if Not FindedComponType then
                              begin
                                  GetMem(ptrCatalogMarkMask, SizeOf(TCatalogMarkMask));
                                  ptrCatalogMarkMask.ID := -1;
                                  ptrCatalogMarkMask.IDCatalog := -1;
                                  ptrCatalogMarkMask.IDComponentType := FieldByName(fnIDComponentType).AsInteger;
                                  ptrCatalogMarkMask.MarkMask := FieldByName(fnMarkMask).AsString;

                                  ptrCatalogMarkMask.MakeEdit := meMake;
                                  AMarkMaskList.Add(ptrCatalogMarkMask);
                              end;
                      end;
                    Next;
                end;
          end;
      Result := true;
    except
      on E: Exception do AddExceptionToLog('TF_ComponTypesMarkMask.SaveDataToMarkMaskList: '+E.Message);
    end;
end;



// #############################################################################
// #############################################################################

// ##### Редактировать маску #####
procedure TF_ComponTypesMarkMask.Act_EditMaskExecute(Sender: TObject);
var NewMarkMask: String;
begin
    try
      //with TF_Main(GForm).DM.MemTable_CatalogMarkMask do
      with MemTable_MarkTemplates do
        if Active then
          if RecordCount > 0 then
            begin
                NewMarkMask := FieldByName('MARK_MASK').AsString;
                if F_ProjMan.CreateFMarkMask.MakeEditMarkMask(fmEdit, NewMarkMask) then
                  begin
                      Edit;
                      FieldByName('MARK_MASK').AsString := NewMarkMask;
                      FieldByName('isModified').AsBoolean := True;
                      Post;
                  end;
            end;
    except
      on E: Exception do AddExceptionToLog('TF_ComponTypesMarkMask.Act_EditMaskExecute: '+E.Message);
    end;
end;

procedure TF_ComponTypesMarkMask.GT_CatalogMarkMasksDblClick(
  Sender: TObject);
begin
    Act_EditMask.Execute;
end;

procedure TF_ComponTypesMarkMask.Panel_OkCancelResize(Sender: TObject);
begin
    SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_ComponTypesMarkMask.FormDestroy(Sender: TObject);
begin
  DeactiveDataSets(Self);
end;

end.
