summaryrefslogtreecommitdiffstats
path: root/knights/tab_pgnview.cpp
blob: 9476d606f19614c323d2b6b7f9fe101069452a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/***************************************************************************
                          tab_pgnview.cpp  -  description
                             -------------------
    begin                : Sat Jan 25 2003
    copyright            : (C) 2003 by Troy Corbin Jr.
    email                : tcorbin@users.sf.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "tab_pgnview.moc"
#include "tabmanager.h"
#include "resource.h"
#include "pgn.h"
#include <tqfile.h>
#include <tqregexp.h>
#include <tqtextstream.h>
#include <kstddirs.h>
#include <tdetempfile.h>

tab_pgnView::tab_pgnView(pgn *parent, resource *Rsrc ) : KnightsTextView(0,Rsrc)
{
	myParent = parent;
	whiteImage = NULL;
	blackImage = NULL;

	TQString source = locate( "appdata", "pgn_template.kml" );
	TQFile file( source );
	if( file.open( IO_ReadOnly ) )
	{
		TQTextStream stream( &file );
		document = stream.read();
		file.close();
	}
	else
	{
		kdWarning() << "tab_pgnView::tab_pgnView: Can not find PGN Template" << endl;
	}
}

tab_pgnView::~tab_pgnView()
{
	if( whiteImage )
		delete whiteImage;
	if( blackImage )
		delete blackImage;
}
///////////////////////////////////////
//
//	tab_pgnView::init
//
///////////////////////////////////////
void tab_pgnView::init( void )
{
	/* Replace macros with data */
	document.replace( TQRegExp( "%site%" ), myParent->TAG_Site );
	document.replace( TQRegExp( "%date%" ), myParent->TAG_Date );
	document.replace( TQRegExp( "%round%" ), myParent->TAG_Round );
	document.replace( TQRegExp( "%result%" ), myParent->TAG_Result );
	document.replace( TQRegExp( "%white%" ), myParent->TAG_White );
	document.replace( TQRegExp( "%whitetitle%" ), myParent->TAG_WhiteTitle );
	document.replace( TQRegExp( "%whiteelo%" ), myParent->TAG_WhiteElo );
	document.replace( TQRegExp( "%whiteuscf%" ), myParent->TAG_WhiteUSCF );
	document.replace( TQRegExp( "%whitena%" ), myParent->TAG_WhiteNA );
	document.replace( TQRegExp( "%whitetype%" ), myParent->TAG_WhiteType );
	document.replace( TQRegExp( "%black%" ), myParent->TAG_Black );
	document.replace( TQRegExp( "%blacktitle%" ), myParent->TAG_BlackTitle );
	document.replace( TQRegExp( "%blackelo%" ), myParent->TAG_BlackElo );
	document.replace( TQRegExp( "%blackuscf%" ), myParent->TAG_BlackUSCF );
	document.replace( TQRegExp( "%blackna%" ), myParent->TAG_BlackNA );
	document.replace( TQRegExp( "%blacktype%" ), myParent->TAG_BlackType );
	document.replace( TQRegExp( "%time%" ), myParent->TAG_Time );
	document.replace( TQRegExp( "%utctime%" ), myParent->TAG_UTCTime );
	document.replace( TQRegExp( "%utcdate%" ), myParent->TAG_UTCDate );
	document.replace( TQRegExp( "%event%" ), myParent->TAG_Event );
	document.replace( TQRegExp( "%eventdate%" ), myParent->TAG_EventDate );
	document.replace( TQRegExp( "%eventsponsor"), myParent->TAG_EventSponsor );
	document.replace( TQRegExp( "%section%" ), myParent->TAG_Section );
	document.replace( TQRegExp( "%stage%" ), myParent->TAG_Stage );
	document.replace( TQRegExp( "%board%" ), myParent->TAG_Board );
	document.replace( TQRegExp( "%opening%" ), myParent->TAG_Opening );
	document.replace( TQRegExp( "%variation%" ), myParent->TAG_Variation );
	document.replace( TQRegExp( "%subvariation%" ), myParent->TAG_SubVariation );
	document.replace( TQRegExp( "%eco%" ), myParent->TAG_ECO );
	document.replace( TQRegExp( "%nic%" ), myParent->TAG_NIC );
	document.replace( TQRegExp( "%timecontrol%" ), myParent->TAG_TimeControl );
	document.replace( TQRegExp( "%termination%" ), myParent->TAG_Termination );
	document.replace( TQRegExp( "%setup%" ), myParent->TAG_SetUp );
	document.replace( TQRegExp( "%fen%" ), myParent->TAG_FEN );
	document.replace( TQRegExp( "%annotator%" ), myParent->TAG_Annotator );
	document.replace( TQRegExp( "%mode%" ), myParent->TAG_Mode );
	document.replace( TQRegExp( "%plycount%" ), myParent->TAG_PlyCount );

	/* Get the White Player's Image */
	TQPixmap wi = myResource->loadSCIDImage( myParent->TAG_White );
	if( wi.isNull() )
	{
		if( myParent->TAG_WhiteType == "program" )
			wi.load( locate("data", "knights/default-engine-portrait.jpg" ) );
		else
			wi.load( locate("data", "knights/default-portrait.jpg" ) );
	}
	whiteImage = new KTempFile();
	whiteImage->setAutoDelete( TRUE );
	wi.save( whiteImage->name(), "PNG" );
	document.replace( TQRegExp( "%whiteimage%" ), whiteImage->name() );

	/* Get the Black Player's Image */
	TQPixmap bi = myResource->loadSCIDImage( myParent->TAG_Black );
	if( bi.isNull() )
	{
		if( myParent->TAG_BlackType == "program" )
			bi.load( locate("data", "knights/default-engine-portrait.jpg" ) );
		else
			bi.load( locate("data", "knights/default-portrait.jpg" ) );
	}
	blackImage = new KTempFile();
	blackImage->setAutoDelete( TRUE );
	bi.save( blackImage->name(), "PNG" );
	document.replace( TQRegExp( "%blackimage%" ), blackImage->name() );

	/* Obtain the move data */
	TQString moves;
	TQStringList *list;
	if( myParent->Move_Data.isEmpty() )
	{
		list = myParent->notation(2);
	}
	else
	{
		list = new TQStringList( myParent->Move_Data );
	}

	/* Do some formatting and then merge the strings */
	for( TQStringList::Iterator i = list->begin(); i != list->end(); i++ )
	{
		(*i).replace( TQRegExp( "[^\\0040-\\0176]" ), "" );
		(*i).replace( TQRegExp( "\\(" ), "<i>(" );
		(*i).replace( TQRegExp( "\\)" ), ")</i>" );
		(*i).replace( TQRegExp( "\\{" ), "<b>{" );
		(*i).replace( TQRegExp( "\\}" ), "}</b>" );
	}
	moves = list->join( " " );
	delete list;

	int pos = document.find( "%moves%" );
	if( pos != -1 )
	{
		document.remove( (unsigned int)pos, 7 );
		document.insert( (unsigned int)pos, moves );
	}
	setText( document );
}