📄 templatemanager.pas
字号:
end;
procedure TTemplateManagerForm.NewNoteClick(Sender: TObject);
var TemplateRecordPtr: PTemplateRecord;
begin
new(TemplateRecordPtr);
with TemplateRecordPtr^ do
begin
Description:='Blank Note';
TemplateStringStream:=TStringStream.Create('');
Author:=MainForm.SQLConnection.Params.Values['User_Name'];
NewNote:=True;
Modded:=True;
OpenForEditing:=True;
with TemplatesListView.Items.Insert(0) do
begin
Caption:=Description;
SubItems.Add(Author);
Data:=TObject(TemplateRecordPtr);
end;
end;
TemplatesListView.Selected:=TemplatesListView.TopItem;
TemplatesListView.ItemFocused:=TemplatesListView.TopItem;
TemplatesListBoxClick(Sender);
end;
procedure TTemplateManagerForm.EditNoteClick(Sender: TObject);
begin
if TemplatesListView.Items.Count<=0 then Exit;
if TemplatesListView.ItemFocused=nil then Exit;
if not Assigned(TemplatesListView.ItemFocused.Data) then Exit;
if PTemplateRecord(TemplatesListView.ItemFocused.Data)^.Author<>
MainForm.SQLConnection.Params.Values['User_Name'] then
begin
MessageDlg('Unable to edit note - You are not the author of this template', mtError, [mbOk], 0);
Exit;
end;
PTemplateRecord(TemplatesListView.ItemFocused.Data)^.OpenforEditing:=True;
EnableTemplateEditingControls;
end;
procedure TTemplateManagerForm.DeleteNoteClick(Sender: TObject);
begin
if TemplatesListView.Items.Count<=0 then Exit;
if TemplatesListView.ItemFocused=nil then Exit;
if not Assigned(TemplatesListView.ItemFocused.Data) then Exit;
if PTemplateRecord(TemplatesListView.ItemFocused.Data)^.Author<>
MainForm.SQLConnection.Params.Values['User_Name'] then
begin
MessageDlg('Unable to delete - You are not the author of this template', mtError, [mbOk], 0);
Exit;
end;
if MessageDlg('Are you sure you want to delete the selected template?',
mtConfirmation, [mbYes, mbNo], 0) <> mrYes then Exit;
if not PTemplateRecord(TemplatesListView.ItemFocused.Data)^.NewNote then
begin
with SQLQuery do
begin
SQL.Clear;
SQL.Add('delete from templates where templateid='+
inttostr(PTemplateRecord(TemplatesListView.ItemFocused.Data)^.TemplateID) );
ExecSQL;
end;
end;
PTemplateRecord(TemplatesListView.ItemFocused.Data)^.TemplateStringStream.Free;
Dispose(PTemplateRecord(TemplatesListView.ItemFocused.Data));
TemplatesListView.ItemFocused.Delete;
PreviousItem:=nil;
TemplatesListBoxClick(Sender);
end;
procedure TTemplateManagerForm.SaveNoteClick(Sender: TObject);
begin
if TemplatesListView.Items.Count<=0 then Exit;
if TemplatesListView.ItemFocused=nil then Exit;
if not Assigned(TemplatesListView.ItemFocused.Data) then Exit;
if not (PTemplateRecord(TemplatesListView.ItemFocused.Data)^.NewNote or PTemplateRecord(TemplatesListView.ItemFocused.Data)^.Modded or DescriptionEdit.Modified or TemplateRichEdit.Modified) then
begin
MessageDlg('No need to save - There have been no modifications to the template! ', mtError, [mbOk], 0);
Exit;
end;
if PTemplateRecord(TemplatesListView.ItemFocused.Data)^.Author<>
MainForm.SQLConnection.Params.Values['User_Name'] then
begin
MessageDlg('Unable to delete - You are not the author of this template', mtError, [mbOk], 0);
Exit;
end;
if DescriptionEdit.Text='' then
begin
MessageDlg('You must enter a description to save this template!', mtError, [mbOk], 0);
Exit;
end;
DisableTemplateEditingControls;
DateSeparator := '/';
ShortDateFormat := 'yyyy/mm/dd';
LongTimeFormat:='hh:nn:ss';
with SQLQuery do
begin
SQL.Clear;
Params.Clear;
with PTemplateRecord(TemplatesListView.ItemFocused.Data)^ do
begin
Description:=DescriptionEdit.Text;
TemplateStringStream.Position:=0;
TemplateRichEdit.Lines.SaveToStream(TemplateStringStream);
if NewNote then
begin
SQL.Text:='insert into templates (description, template, author) values (:description, :template, :author)';
Params[0].AsString:=Description;
Params[1].LoadFromStream(TemplateStringStream, ftFmtMemo);
Params[2].AsString:=Author;
ExecSql;
end
else
begin
SQL.Text:='update templates set description=:description, template=:template where templateid=:templateid';
Params[0].AsString:=Description;
Params[1].LoadFromStream(TemplateStringStream, ftFmtMemo);
Params[2].AsString:=InttoStr(TemplateID);
ExecSql;
end;
NewNote:=False;
Modded:=False;
OpenforEditing:=False;
DescriptionEdit.Modified:=False;
TemplateRichEdit.Modified:=False;
end; //with
end;
PreviousItem:=nil; //Force screen update
TemplatesListBoxClick(Sender);
end;
procedure TTemplateManagerForm.RevertNoteClick(Sender: TObject);
begin
if TemplatesListView.Items.Count<=0 then Exit;
if TemplatesListView.ItemFocused=nil then Exit;
if not Assigned(TemplatesListView.ItemFocused.Data) then Exit;
if PTemplateRecord(TemplatesListView.ItemFocused.Data)^.NewNote then
begin
MessageDlg('Nothing to revert - This is a new note! ', mtError, [mbOk], 0);
Exit;
end;
if not (PTemplateRecord(TemplatesListView.ItemFocused.Data)^.Modded or DescriptionEdit.Modified or TemplateRichEdit.Modified) then
begin
MessageDlg('No need to revert - There have been no modifications to the template! ', mtError, [mbOk], 0);
Exit;
end;
if MessageDlg('Reload template and lose changes?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then Exit;
with SQLQuery do
begin
SQL.Clear;
SQL.Add('select * from templates where templateid='''+InttoStr(PTemplateRecord(TemplatesListView.ItemFocused.Data)^.TemplateID)+'''');
Open;
First;
with PTemplateRecord(TemplatesListView.ItemFocused.Data)^ do
begin
//TemplateID:=FieldByName('templateid').AsInteger;
Description:=FieldByName('description').AsString;
TemplateStringStream:=TStringStream.Create(FieldByName('template').AsString);
Author:=FieldByName('author').AsString;
NewNote:=False;
Modded:=False;
OpenforEditing:=False;
end;
//no need for next since only one record
Close;
end;
PreviousItem:=nil; //this will force a screen redraw
TemplatesListBoxClick(Sender);
end;
procedure TTemplateManagerForm.RefreshTemplatesDBClick(Sender: TObject);
var TemplateRecordPtr: PTemplateRecord;
SelectedTemplateIDHolder, i: integer;
Save_Cursor: TCursor;
begin
Save_Cursor:=Screen.Cursor;
Screen.Cursor:=crSQLWait;
SelectedTemplateIDHolder:=-1;
if TemplatesListView.ItemFocused<>nil then
begin
if Assigned(TemplatesListView.ItemFocused.Data) then
SelectedTemplateIDHolder:=PTemplateRecord(TemplatesListView.ItemFocused.Data)^.TemplateID;
end;
TemplatesListView.Items.BeginUpdate;
CleanUpTemplatesListView;
with SQLQuery do
begin
SQL.Clear;
if ShowOnlyMyTemplatesCheckBox.Checked then
SQL.Add('select * from templates where author='''+SQLConnection.Params.Values['User_Name']+''' order by description')
else
SQL.Add('select * from templates order by description');
Open;
First;
while (not EOF) do
begin
new(TemplateRecordPtr);
with TemplateRecordPtr^ do
begin
TemplateID:=FieldByName('templateid').AsInteger;
Description:=FieldByName('description').AsString;
TemplateStringStream:=TStringStream.Create(FieldByName('template').AsString);
Author:=FieldByName('author').AsString;
NewNote:=False;
Modded:=False;
OpenforEditing:=False;
with TemplatesListView.Items.Add do
begin
Caption:=Description;
SubItems.Add(Author);
Data:=TObject(TemplateRecordPtr);
end;
end;
Next;
end;
Close;
end;
TemplatesListView.Items.EndUpdate;
Screen.Cursor:=Save_Cursor;
if (SelectedTemplateIDHolder=-1) or (TemplatesListView.Items.Count<=0) then
begin
TemplatesListBoxClick(Sender);
Exit;
end;
for i:=0 to TemplatesListView.Items.Count-1 do
begin
if PTemplateRecord(TemplatesListView.Items[i].Data)^.TemplateID = SelectedTemplateIDHolder then
begin
TemplatesListView.Selected := TemplatesListView.Items[i];
TemplatesListView.ItemFocused := TemplatesListView.Items[i];
Break;
end;
end;
TemplatesListBoxClick(Sender);
end;
procedure TTemplateManagerForm.ReplaceText(FindText, ReplaceText: String; StartPos: Integer; SearchOptions: TSearchTypes; MatchCase: boolean);
var FoundPos: Integer;
begin
//This function finds and replaces text in a RichEdit Control
//The MatchCase boolean controls a special function
//What case matching does is first convert the replacement text to all lower case
//Then it checks the first letter of the found text
//If it is uppercase, it makes the first letter of the replacement text uppercase
//If it is lowercase, it does nothing
//Case example: "He", "he", "She" or "she".
//Sample call:
//Editor.Lines.BeginUpdate;
//ReplaceText('white','BLACK', 0, [stWholeWord], True);
//Editor.Lines.EndUpdate;
// Did not include Being/Endupdate in this function because when used outside,
// 10 replace text calls could be encapsualted with single begin/endupdate
repeat
FoundPos:= TemplateRichEdit.FindText(FindText, StartPos, Length(TemplateRichEdit.Text), SearchOptions);
if FoundPos <> -1 then
begin
TemplateRichEdit.SelStart:= FoundPos;
TemplateRichEdit.SelLength:= Length(FindText);
if MatchCase then
begin
ReplaceText:=LowerCase(ReplaceText);
//Need to use SelText[2] instead of [1] because FindText starts with "<" ex <HESHE>
if TemplateRichEdit.SelText[2] = UpCase(TemplateRichEdit.SelText[2]) then
ReplaceText[1]:=UpCase(ReplaceText[1]); //Uppercase
end;
TemplateRichEdit.SelText:= ReplaceText;
StartPos:= FoundPos + Length(ReplaceText);
end;
until FoundPos = -1;
end;
procedure TTemplateManagerForm.OKButtonClick(Sender: TObject);
var i: integer;
NoteRecordPtr: PNoteRecord;
begin
//If the template manager was accessed from the main menu instead of as part
//of a note import then there is no need to go past this point. Just
//exit the procedure and close the form.
if not FImport then
begin
Close;
Exit;
end;
for i:=0 to TemplatesListView.Items.Count-1 do
begin
if PTemplateRecord(TemplatesListView.Items[i].Data)^.Modded or DescriptionEdit.Modified or TemplateRichEdit.Modified then
begin
if MessageDlg('At least one template has been modified. Continue without saving changes?', mtConfirmation, [mbYes, mbNo], 0) <> mrYes then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -