From a05aa3f5b8b39cdea0935198927aef2bbff15efd Mon Sep 17 00:00:00 2001 From: david Date: Wed, 8 Oct 2008 11:20:38 +0000 Subject: [PATCH] =?UTF-8?q?Arreglado=20un=20fallo=20de=20renumerar=20la=20?= =?UTF-8?q?posici=C3=B3n=20al=20copiar=20y=20pegar=20texto=20desde=20el=20?= =?UTF-8?q?portapapeles=20(2=C2=BA=20intento)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://192.168.0.254/svn/Proyectos.Tecsitel_FactuGES2/trunk@668 0c75b7a4-871f-7646-8a2f-f78d34cc349f --- Source/Base/Utiles/uGridClipboardUtils.pas | 190 ++++++++++++++++++--- 1 file changed, 162 insertions(+), 28 deletions(-) diff --git a/Source/Base/Utiles/uGridClipboardUtils.pas b/Source/Base/Utiles/uGridClipboardUtils.pas index b5f44604..35760c1e 100644 --- a/Source/Base/Utiles/uGridClipboardUtils.pas +++ b/Source/Base/Utiles/uGridClipboardUtils.pas @@ -29,6 +29,15 @@ uses cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridDBDataDefinitions, uGridStatusUtils; +const + TIPO_DETALLE_CONCEPTO = 'Concepto'; + CAMPO_ID = 'ID'; + CAMPO_POSICION = 'POSICION'; + CAMPO_TIPO = 'TIPO_DETALLE'; + CAMPO_CONCEPTO = 'CONCEPTO'; + +type + TTipoAnadir = (taAnadir, taInsertar); procedure RegistrarFormatos; begin @@ -45,11 +54,139 @@ begin raise Exception.Create('Error al registrar formato CF_RTF (CopiarSeleccionGridAlPortapapelesRTF)'); end; -procedure RenumerarCampoPosicion(ADataSet : TDataSet); + + +function _DesplazarNPosiciones(ADataSet: TDataSet; NumOrdenIni: Variant; NPosiciones: Variant): Integer; +{ +Función que desplaza NPosiciones el numero de orden a partir del elemento con el +número de orden dado. Devuelve el numero de orden del primer elemento del hueco +generado +} +var + AuxNumOrden: Integer; + AuxNumPos: Integer; + AField: TField; +begin + + AField := ADataSet.FindField(CAMPO_POSICION); + if not Assigned(AField) then + raise Exception.Create('Campo ' + CAMPO_POSICION + ' no encontrado (DesplazarNPosiciones)'); + + if VarIsNull(NPosiciones) then + AuxNumPos := 1 + else + AuxNumPos := NPosiciones; + + if VarIsNull(NumOrdenIni) then + AuxNumOrden := 0 + else + AuxNumOrden := NumOrdenIni + 1; //Añadimos por abajo siempre + + Result := AuxNumOrden; + + with ADataSet do + begin + First; + while not EOF do + begin + if (FieldByName(CAMPO_POSICION).AsInteger >= AuxNumOrden) then + begin + if not (State in dsEditModes) then + Edit; + FieldByName(CAMPO_POSICION).AsInteger := FieldByName(CAMPO_POSICION).AsInteger + AuxNumPos; + Post; + end; + Next; + end; + end; +end; + + +procedure _NuevaTupla(ADataSet: TDataSet; ATipoAnadir: TTipoAnadir = taInsertar); +var + AuxNumOrden : Integer; +begin + ADataSet.DisableControls; + try + with ADataSet do + begin + AuxNumOrden := _DesplazarNPosiciones(ADataSet, FieldByName(CAMPO_POSICION).AsVariant, 1); + + case ATipoAnadir of + taAnadir: Append; + taInsertar: Insert; + end; + + FieldByName(CAMPO_POSICION).AsInteger := AuxNumOrden; + FieldByName(CAMPO_TIPO).AsVariant := TIPO_DETALLE_CONCEPTO; + end; + finally + ADataSet.EnableControls; + end; +end; + + +procedure _Renumerar(ADataSet: TDataSet); +{ +procedimiento que renumera todos los conceptos de la tabla dada por parametro +} +var + i, j : Integer; + AField: TField; +begin + AField := ADataSet.FindField(CAMPO_POSICION); + + if not Assigned(AField) then + raise Exception.Create('Campo ' + CAMPO_POSICION + ' no encontrado (renumerar)'); + + with ADataSet do + begin + for i:=0 to RecordCount-1 do + begin + First; + if not Locate(CAMPO_POSICION, i, []) then + begin + j := i; + First; + while not Locate(CAMPO_POSICION, j, []) do + begin + Inc(j); + First; + end; + + if not (State in dsEditModes) then + Edit; + + FieldByName(CAMPO_POSICION).AsInteger := i; + Post; + end; + end; + end; +end; + + + +{procedure RenumerarCampoPosicion(ADataSet : TDataSet); var i, j : Integer; AField : TField; + AList : TStringList; begin + AList := TStringList.Create; + try + ADataSet.First; + for i:=0 to ADataSet.RecordCount-1 do + begin + AList.Add(ADataSet.FieldByName('POSICION').AsString); + ADataSet.Next; + end; + ShowMessage(AList.Text); + finally + FreeANDNIL(AList); + end; + + + AField := ADataSet.FindField('POSICION'); if Assigned(AField) and not (ADataSet.IsEmpty) then begin @@ -67,15 +204,15 @@ begin Inc(j); First; end; - - Edit; - FieldByName('POSICION').AsInteger := i; - Post; end; + Edit; + FieldByName('POSICION').AsInteger := i; + Post; end; end; end; -end; +end;} + function _BuscarColumna (AView: TcxGridDBTableView; const AName, ATypeValue : String; var AIndex : Integer) : Boolean; @@ -168,16 +305,16 @@ begin begin // Insertar una tupla de la forma adecuada if bEstabaVacia then - ADataSet.Append + _NuevaTupla(ADataSet, taAnadir) else begin if iContador = 0 then ADataSet.Edit else begin ADataSet.Next; if ADataSet.EOF then - ADataSet.Append + _NuevaTupla(ADataSet, taAnadir) else - ADataSet.Insert; + _NuevaTupla(ADataSet, taInsertar) end; end; @@ -185,14 +322,16 @@ begin for iCols := 0 to NumCols - 1 do begin AValue := AReader.ReadVariant; - if (Pos(AValueNameList[iCols], AView.DataController.KeyFieldNames) = 0) and - (AValueNameList[iCols] <> 'POSICION') then + if (Pos(AValueNameList[iCols], AView.DataController.KeyFieldNames) = 0) then begin - if _BuscarColumna(AView, AValueNameList[iCols], AValueTypeList[iCols], AIndex) then - begin - if AIndex <> -1 then - ADataSet.FieldByName(AView.DataController.GetItemField(AIndex).FieldName).Value := AValue; - end; + if (AValueNameList[iCols] = 'POSICION') then + ADataSet.FieldByName('POSICION').Value := ADataSet.RecNo + else + if _BuscarColumna(AView, AValueNameList[iCols], AValueTypeList[iCols], AIndex) then + begin + if AIndex <> -1 then + ADataSet.FieldByName(AView.DataController.GetItemField(AIndex).FieldName).Value := AValue; + end; end; end; finally @@ -200,7 +339,7 @@ begin end; end; - RenumerarCampoPosicion(ADataSet); + _Renumerar(ADataSet); finally AView.DataController.EndUpdate; @@ -490,7 +629,7 @@ begin try CopiarSeleccionGridAlPortapapeles(AGrid); AGrid.ActiveView.DataController.DeleteSelection; - RenumerarCampoPosicion(TcxGridDBTableView(AGrid.ActiveView).DataController.DataSet); + _Renumerar(TcxGridDBTableView(AGrid.ActiveView).DataController.DataSet); finally HideHourglassCursor; end; @@ -531,12 +670,10 @@ end; procedure PegarTextoDesdePortapapeles (AGrid : TcxGrid); var - ACaption : String; AGridStatus : TcxGridStatus; ATextList : TStringList; ADataSet : TDataSet; - AColumnID: Integer; ARecordID : Integer; ARecordIndex : Integer; @@ -555,17 +692,14 @@ begin AView := TcxGridDBTableView(AGrid.ActiveView); - AGridStatus := TcxGridStatus.Create(TcxGridDBTableView(AGrid.ActiveView)); Clipboard.Open; try ATextList := TStringList.Create; try CopyStringsFromClipboard(CF_TEXT, ATextList); - ADataSet := AView.DataController.DataSource.DataSet; bEstabaVacia := ADataSet.IsEmpty; - AView.DataController.BeginUpdate; try // Localizar el punto donde se empieza a insertar @@ -583,16 +717,16 @@ begin begin // Insertar una tupla de la forma adecuada if bEstabaVacia then - ADataSet.Append + _NuevaTupla(ADataSet, taAnadir) else begin if iContador = 0 then ADataSet.Edit else begin ADataSet.Next; if ADataSet.EOF then - ADataSet.Append + _NuevaTupla(ADataSet, taAnadir) else - ADataSet.Insert; + _NuevaTupla(ADataSet, taInsertar) end; end; @@ -603,7 +737,8 @@ begin ADataSet.Post; end; end; - RenumerarCampoPosicion(ADataSet); + + _Renumerar(ADataSet); finally AView.DataController.EndUpdate; end; @@ -649,7 +784,6 @@ begin PegarTextoDesdePortapapeles(AGrid); Exit; end; - end; end.