{*********************************************************}
{                                                         }
{                     MightyQuery                         }
{            SQL Database Management Software             }
{                                                         }
{   Original developer: Zlatko Matić, 2009-2011           }
{             e-mail: matalab@gmail.com                   }
{  Milkovićeva 6, Mala Ostrna, 10370 Dugo Selo,  Croatia. }
{                                                         }
{*********************************************************}

{-----------------------------------------------------------------------------

The Initial Developer of the Original Code is Zlatko Matić
(matalab@gmail.com or matic.zlatko@gmail.com).

Contributor(s):

Last Modified: 04.06.2011

Known Issues:

History (Change log):
  version 1.0.4, date 04.06.2011 :
		MightyQuery released as free software
-----------------------------------------------------------------------------}

unit Unit2;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, FileUtil, LResources, Forms, Controls, Graphics,
  Dialogs, ExtCtrls, PairSplitter, DbCtrls, DBGrids, StdCtrls,
  Unit1, ZMConnection, ZMQueryDataSet;

type

  { TFormFilters }

  TFormFilters = class(TForm)
    ButtonAppendFilterField: TButton;
    ButtonAppendFilterExpression: TButton;
    ButtonApplyFilter: TButton;
    ButtonRemoveFilter: TButton;
    ComboBoxFilterFields: TComboBox;
    ComboBoxFilterExpressions: TComboBox;
    DatasourceFilters: TDatasource;
    DBGridFilters: TDBGrid;
    DBMemoFilter: TDBMemo;
    DBNavigatorFilters: TDBNavigator;
    PairSplitter1: TPairSplitter;
    PairSplitter2: TPairSplitter;
    PairSplitter3: TPairSplitter;
    PairSplitter4: TPairSplitter;
    PairSplitterSide1: TPairSplitterSide;
    PairSplitterSide2: TPairSplitterSide;
    PairSplitterSide3: TPairSplitterSide;
    PairSplitterSide4: TPairSplitterSide;
    PairSplitterSide5: TPairSplitterSide;
    PairSplitterSide6: TPairSplitterSide;
    PairSplitterSide7: TPairSplitterSide;
    PairSplitterSide8: TPairSplitterSide;
    Panel1: TPanel;
    Panel10: TPanel;
    Panel11: TPanel;
    Panel12: TPanel;
    Panel13: TPanel;
    Panel14: TPanel;
    Panel15: TPanel;
    Panel16: TPanel;
    Panel17: TPanel;
    Panel18: TPanel;
    Panel19: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    Panel8: TPanel;
    Panel9: TPanel;
    StaticText1: TStaticText;
    procedure ButtonAppendFilterExpressionClick(Sender: TObject);
    procedure ButtonAppendFilterFieldClick(Sender: TObject);
    procedure ButtonApplyFilterClick(Sender: TObject);
    procedure ButtonRemoveFilterClick(Sender: TObject);
  private
    { private declarations }
    FDataSet:TZMQueryDataSet;
  public
    { public declarations }
    constructor Create(var pDataset: TZMQueryDataset);
  end; 

var
  FormFilters: TFormFilters;

implementation



{ TFormFilters }

procedure TFormFilters.ButtonAppendFilterFieldClick(Sender: TObject);
var
  vSelectedItem:Integer;
  vCurrentLine, vCurrentPosition:Integer;
  vCurrentLineText:string;
begin
  vSelectedItem:=ComboBoxFilterFields.ItemIndex;
  if vSelectedItem>=0 then
    with ComboBoxFilterFields do begin
      vCurrentLine:=DBMemoFilter.CaretPos.y;
      vCurrentPosition:=DBMemoFilter.CaretPos.x;
      vCurrentLineText:=DBMemoFilter.Lines.Strings[vCurrentLine];
      DBMemoFilter.Lines.Strings[vCurrentLine]:=Copy(vCurrentLineText,0,vCurrentPosition)
        +' '+Items[vSelectedItem]
        +Copy(vCurrentLineText,vCurrentPosition+1,Length(vCurrentLineText)-vCurrentPosition)
        +' ';
    end
end;

procedure TFormFilters.ButtonApplyFilterClick(Sender: TObject);
var vFiltered:Boolean;
begin
  try
    vFiltered:=FDataSet.Filtered; //Remeber Filterd property
    DatasourceFilters.DataSet.Edit;
    DatasourceFilters.DataSet.Post;
    FDataSet.DisableControls;
    try
      {FDataSet.Filtered:=False;}
      FDataSet.Filter:=DatasourceFilters.DataSet.FieldByName('FilterText').AsString;
      FDataSet.Filtered:=True;
    except
      on E:Exception do begin
        {ShowMessage('Error while trying to apply custom filter.  '+E.Message); } //Don't use it, it freezes application!
        FDataSet.Filter:=''; //Remove custom filter
        FDataSet.Filtered:=vFiltered; //Enable OnFilter event, for master/detail filtration.
      end;
    end;
  finally
    FDataSet.Refresh;
    FDataSet.EnableControls;
  end;
end;

procedure TFormFilters.ButtonRemoveFilterClick(Sender: TObject);
begin
  FDataSet.Filtered:=False;
  FDataSet.Filter:='';
  FDataSet.Filtered:=True; //Enable OnFilter event, for master/detail filtration.
end;

procedure TFormFilters.ButtonAppendFilterExpressionClick(Sender: TObject);
var
  vSelectedItem:Integer;
  vCurrentLine, vCurrentPosition:Integer;
  vCurrentLineText:string;
begin
  vSelectedItem:=ComboBoxFilterExpressions.ItemIndex;
  if vSelectedItem>=0 then
    with ComboBoxFilterExpressions do begin
      vCurrentLine:=DBMemoFilter.CaretPos.y;
      vCurrentPosition:=DBMemoFilter.CaretPos.x;
      vCurrentLineText:=DBMemoFilter.Lines.Strings[vCurrentLine];
      DBMemoFilter.Lines.Strings[vCurrentLine]:=Copy(vCurrentLineText,0,vCurrentPosition)
        +' '+Items[vSelectedItem]
        +Copy(vCurrentLineText,vCurrentPosition+1,Length(vCurrentLineText)-vCurrentPosition)
        +' ';
    end
end;


constructor TFormFilters.Create(var pDataset: TZMQueryDataset);
var I:Integer;
begin
  FDataset:=pDataset;
  //Populate ComboBoxFilterFields.
  with ComboBoxFilterFields do begin
    Clear;
    //Iterate through fields and assign field names to items in the combobox.
    if FDataset.Active=True then
      begin
        for I := 0 to FDataset.Fields.Count - 1 do begin
          ComboBoxFilterFields.Items.Add(FDataset.Fields[I].FieldName);
        end;
        ComboBoxFilterFields.ItemIndex:=0;
      end;
  end;
  //Open the form FormFilters.
  try
    FormFilters.Visible:=True;
  except
    on E:Exception do begin
      ShowMessage('Error while trying to open the form "FormFilters". '+ E.Message);
    end;
  end;
end;


initialization
  {$I unit2.ctrs}

end.

