📄 baseodfclass4.pas
字号:
unit BaseODFClass4;
interface
uses Classes, SysUtils{$IFDEF VCL6}, StrUtils{$ENDIF}, BaseArchiveClass4, QXMLWriter,
QExport4IniFiles, QExport4Common
{$IFDEF WIN32}
, ExtCtrls
{$IFNDEF NOGUI}, Graphics{$ELSE}, QExport4Graphics{$ENDIF}
{$ENDIF}
{$IFDEF LINUX}
, QExtCtrls
{$IFNDEF NOGUI}, QGraphics{$ELSE}, QExport4Graphics{$ENDIF}
{$ENDIF};
type
TODFTextAlignment = (taODFLeft, taODFRight, taODFCenter, taODFJustify);
TODFTextVerticalAligment = (taODFTop, taODFMiddle, taODFBottom);
TODFStripStyleType = (sstNone, sstColumn, sstRow);
TODFBorderStyle = (bsODFNone, bsODFSolid);
TODSParagraphStyle = class(TCollectionItem)
private
FFont: TFont;
FBackgroundColor: TColor;
FAllowBackground: boolean;
FAlignment: TODFTextAlignment;
procedure SetFont(const Value: TFont);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure SetDefault; virtual;
procedure LoadFromIniFile(IniFile: TQIniFile; const Section: string); virtual;
procedure SaveToIniFile(IniFile: TQIniFile; const Section: string); virtual;
published
property Font: TFont read FFont write SetFont;
property BackgroundColor: TColor read FBackgroundColor
write FBackgroundColor default clWhite;
property AllowBackground: boolean read FAllowBackground
write FAllowBackground default true;
property Alignment: TODFTextAlignment read FAlignment
write FAlignment default taODFLeft;
end;
TODTParagraphStyle = class(TODSParagraphStyle)
private
FHighlightColor: TColor;
FAllowHighlight: boolean;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure SetDefault; override;
procedure LoadFromIniFile(IniFile: TQIniFile; const Section: string); override;
procedure SaveToIniFile(IniFile: TQIniFile; const Section: string); override;
published
property HighlightColor: TColor read FHighlightColor
write FHighlightColor default clWhite;
property AllowHighlight: boolean read FAllowHighlight
write FAllowHighLight default false;
end;
TODFBorder = class(TPersistent)
private
FBorderStyle: TODFBorderStyle;
FBorderWidth: Integer;
FBorderColor: TColor;
procedure SetBorderWidth(const Value: Integer);
public
constructor Create;
procedure Assign(Source: TPersistent); override;
procedure SetDefault;
published
property BorderStyle: TODFBorderStyle read FBorderStyle
write FBorderStyle default bsODFNone;
property BorderWidth: Integer read FBorderWidth
write SetBorderWidth default 1;
property BorderColor: TColor read FBorderColor write FBorderColor
default clBlack;
end;
TODSCellParagraphStyle = class(TODSParagraphStyle)
private
FVerticalAligment: TODFTextVerticalAligment;
FBorder: TODFBorder;
procedure SetBorder(const Value: TODFBorder);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure SetDefault; override;
procedure LoadFromIniFile(IniFile: TQIniFile; const Section: string); override;
procedure SaveToIniFile(IniFile: TQIniFile; const Section: string); override;
published
property VerticalAligment: TODFTextVerticalAligment read FVerticalAligment
write FVerticalAligment default taODFBottom;
property Border: TODFBorder read FBorder write SetBorder;
end;
TODSStripCellParagraphStyle = class(TODSCellParagraphStyle)
private
FUseDataStyleBorder: Boolean;
function GetStripBorder: TODFBorder;
public
constructor Create(Collection: TCollection); override;
procedure Assign(Source: TPersistent); override;
procedure SetDefault; override;
property StripBorder: TODFBorder read GetStripBorder;
procedure LoadFromIniFile(IniFile: TQIniFile; const Section: string); override;
procedure SaveToIniFile(IniFile: TQIniFile; const Section: string); override;
published
property UseDataStyleBorder: Boolean read FUseDataStyleBorder write
FUseDataStyleBorder default True;
end;
TODTCellParagraphStyle = class(TODTParagraphStyle)
private
FVerticalAligment: TODFTextVerticalAligment;
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure SetDefault; override;
procedure LoadFromIniFile(IniFile: TQIniFile; const Section: string); override;
procedure SaveToIniFile(IniFile: TQIniFile; const Section: string); override;
published
property VerticalAligment: TODFTextVerticalAligment read FVerticalAligment
write FVerticalAligment default taODFBottom;
end;
TODTStylesList = class(TCollection)
private
FHolder: TPersistent;
protected
function GetOwner: TPersistent; override;
function GetItem(Index: integer): TODTCellParagraphStyle;
procedure SetItem(Index: integer; Value: TODTCellParagraphStyle);
public
constructor Create(Holder: TPersistent);
property Holder: TPersistent read FHolder;
function Add: TODTCellParagraphStyle;
property Items[Index: Integer]: TODTCellParagraphStyle read GetItem
write SetItem; default;
end;
TODSStylesList = class(TCollection)
private
FHolder: TPersistent;
protected
function GetOwner: TPersistent; override;
function GetItem(Index: integer): TODSStripCellParagraphStyle;
procedure SetItem(Index: integer; Value: TODSStripCellParagraphStyle);
public
constructor Create(Holder: TPersistent);
property Holder: TPersistent read FHolder;
function Add: TODSStripCellParagraphStyle;
property Items[Index: Integer]: TODSStripCellParagraphStyle read GetItem
write SetItem; default;
end;
TBaseODFFile = class(TBaseArchiveClass)
private
FExportStream: TStream;
FExportWriter: TQXMLWriter;
// Convert: -1 - not defined; 0 - text; 1 - spreadsheet
FTypeConvert: integer;
public
constructor Create; override;
destructor Destroy; override;
procedure OpenStream(FName: string);
procedure OpenWriter;
procedure CloseStream;
procedure CloseWriter;
procedure CheckStatus;
procedure MakeAll;
procedure EndFormingContent;
procedure Renew;
procedure DeleteList(FileNames: array of WideString);
procedure MergeContent(Name: WideString; FileNames: array of WideString);
procedure WriteSpecificNode(NodeNum: Integer; Text: WideString;
AttNames, AttValues: array of WideString);
function CreateCommonStructure: Boolean;
property TypeConvert: integer read FTypeConvert write FTypeConvert;
end;
procedure DrawODSSample(PaintBox: TPaintBox; odsStyle: TODSCellParagraphStyle);
procedure DrawODTTextSample(PaintBox: TPaintBox; odtStyle: TODTParagraphStyle);
procedure DrawODTCellSample(PaintBox: TPaintBox; odtStyle: TODTCellParagraphStyle);
function ReplaceSymbols(var Value: WideString): WideString;
implementation
uses
StdCtrls, QExport4ODS, QExport4EmsWideStrUtils;
const
LF = #13#10;
sFileNameNotDefined = 'File name is not defined';
sStructureCreateFailed = 'Failed to create basic structure';
sConvertWrong = 'Convert is not defined';
function ReplaceSymbols(var Value: WideString): WideString;
var
p, i: Integer;
const
SearchSym: array [0..4] of WideString = ('&', '>', '<', '"', ' ');
ReplSym: array [0..4] of WideString = ('&', '>', '<', '"', ' ');
begin
for i := 0 to Length(SearchSym) - 1 do
begin
p := 1;
while p > 0 do
begin
p := WideStringPosEx(SearchSym[i], Value, p);
if p > 0 then
begin
Delete(Value, p, 1);
Insert(ReplSym[i], Value, p);
if SearchSym[i] <> #0 then
Inc(p, 4);
end;
end;
end;
Result := Value;
end;
{ TBaseODFFile }
constructor TBaseODFFile.Create;
begin
inherited;
TypeConvert := -1;
end;
function TBaseODFFile.CreateCommonStructure: Boolean;
var
FileHandle: Integer;
begin
Result := false;
if not (CreateDir(TempPath))
or not(CreateDir(TempPath + '\Configurations2'))
or not(CreateDir(TempPath + '\Configurations2\accelerator'))
or not(CreateDir(TempPath + '\Configurations2\floater'))
or not(CreateDir(TempPath + '\Configurations2\images'))
or not(CreateDir(TempPath + '\Configurations2\images\Bitmaps'))
or not(CreateDir(TempPath + '\Configurations2\menubar'))
or not(CreateDir(TempPath + '\Configurations2\popupmenu'))
or not(CreateDir(TempPath + '\Configurations2\progressbar'))
or not(CreateDir(TempPath + '\Configurations2\statusbar'))
or not(CreateDir(TempPath + '\Configurations2\toolbar'))
or not(CreateDir(TempPath + '\META-INF'))
or not(CreateDir(TempPath + '\Thumbnails')) then
Exit;
FileHandle := FileCreate(TempPath +
'\Configurations2\accelerator\current.xml');
if FileHandle = -1 then
Exit;
FileClose(FileHandle);
FileHandle := FileCreate(TempPath + '\Thumbnails\thumbnail.png');
if FileHandle = -1 then
Exit;
FileClose(FileHandle);
Result := true;
end;
procedure TBaseODFFile.WriteSpecificNode(NodeNum: Integer;
Text: WideString; AttNames, AttValues: array of WideString);
var
CurrDate: TDateTime;
TempString: String;
I: Integer;
begin
//Standard XML Header
case NodeNum of
1:
FExportWriter.CreateProcessingInstruction('1.0', 'UTF-8', '');
//Standard Meta Header - just start and entire meta.xml file will be formed
2: begin
FExportWriter.BeginNode('office:document-meta', '',
['xmlns:office', 'xmlns:xlink', 'xmlns:dc', 'xmlns:meta', 'xmlns:ooo',
'office:version'], ['urn:oasis:names:tc:opendocument:xmlns:office:1.0',
'http://www.w3.org/1999/xlink', 'http://purl.org/dc/elements/1.1/',
'urn:oasis:names:tc:opendocument:xmlns:meta:1.0',
'http://openoffice.org/2004/office', '1.0']);
FExportWriter.BeginNode('office:meta', '', [], []);
FExportWriter.CreateFullNode('meta:generator', '', [], []);
CurrDate := Now;
DateTimeToString(TempString, 'yyyy-mm-dd', CurrDate);
FExportWriter.CreateFullNode('meta:creation-date', TempString +
'T' + TimeToStr(CurrDate), [], []);
FExportWriter.CreateFullNode('dc:date', TempString +
'T' + TimeToStr(CurrDate), [], []);
FExportWriter.CreateFullNode('dc:language', 'en-EN', [], []);
FExportWriter.CreateFullNode('meta:editing-cycles', '0', [], []);
FExportWriter.CreateFullNode('meta:editing-duration', '', [], []);
for I := 1 to 4 do
FExportWriter.CreateFullNode('meta:user-defined', '',
['meta:name'], ['Info ' + IntToStr(I)]);
FExportWriter.CreateFullNode('meta:document-statistic', '', [], []);
FExportWriter.EndNode('office:meta');
FExportWriter.EndNode('office:document-meta');
end;
//For back compatibility with old interface version third number is missed
//Manifest file spreadsheet
104: begin
FExportWriter.BeginNode('manifest:manifest', '',
['xmlns:manifest'],
['urn:oasis:names:tc:opendocument:xmlns:manifest:1.0']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['application/vnd.oasis.opendocument.spreadsheet', '/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/statusbar/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/accelerator/current.xml']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/accelerator/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/floater/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/popupmenu/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/progressbar/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/menubar/']);
FExportWriter.CreateFullNode('manifest:file-entry', '',
['manifest:media-type', 'manifest:full-path'],
['', 'Configurations2/toolbar/']);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -