/
additionalformats-chapter.docbook
543 lines (455 loc) · 17.7 KB
/
additionalformats-chapter.docbook
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<appendix id="supportingadditionalfileformats">
<title>Supporting Additional File Formats</title>
<para>
This section describes
how to create additional
readers for unsupported file formats. If you are not already familiar with data source
concepts, please read
<link linkend="DataSourceConcepts">Data Source Concepts</link>
</para>
<sect1 id="supportingadditionalfileformatscreatingdatasource">
<title>Creating Datasource Readers</title>
<para>
If you wish to use a file format other than those currently supported, you might choose to write
custom datasource readers.
</para>
<para>
All &kst; datasource readers are regular &kde; plugins.
Like all &kde; plugins, each datasource reader must have a shared object file
and a <filename>*.desktop</filename> file. Before writing the reader, the
library name of the plugin must be decided. This name must be a valid
variable name in C, as it will be used in the function names of the shared object.
For example, the library name of the reader for ASCII files is
<quote>ascii</quote>.
</para>
<note>
<para>
&kde; plugins are <emphasis>not</emphasis>
the same as the &kst; plugins used to manipulate data. All references to
plugins in this section are to &kde; plugins.
</para>
</note>
<sect2 id="supportingadditionalfileformatscreatingdatasourceobjectfile">
<title>The Shared Object</title>
<para>
A datasource reader should be a subclass of the abstract
<classname>KstDataSource</classname> class. Ensure that you include the header file
for <classname>KstDataSource</classname> in the source code for your datasource reader:
<screen>
#include <kstdatasource.h>
</screen>
There are certain requirements that a datasource reader must meet. One requirement
concerns the presence of exported C functions. Other requirements
are consequences of the fact that datasource readers inherit from <classname>KstDataSource</classname>.
Both sets of requirements, along with suggestions and general explanations, are provided in the
following sections.
</para>
<sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfileexportedfxns">
<title>Exported C Functions</title>
<para>
The exported C functions are listed below. In the following
examples, all instances of <varname><libname></varname> should be replaced with
the actual library name of the plugin.
</para>
<variablelist>
<varlistentry>
<term><function><returnvalue>KstDataSource *</returnvalue>create_<libname>(const QString& <parameter>filename</parameter>, const QString& <parameter>type</parameter>)</function></term>
<listitem>
<para>
This function should create a new datasource reader of type <varname><libname></varname>,
where <varname><libname></varname> is the library name of the plugin. A pointer of type
<classname>KstDataSource</classname> to the new reader should be returned.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function><returnvalue>bool</returnvalue> understands_<libname>(const QString& <parameter>filename</parameter>)</function></term>
<listitem>
<para>
This function should return true if the file specified by <varname>filename</varname> is
of a valid type supported by this reader, and false otherwise.
The function should check the contents of the
file for validity, and not rely on any filename extensions.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function><returnvalue>QStringList</returnvalue> provides_<libname>()</function></term>
<listitem>
<para>
This function should return a <classname>QStringList</classname> of the file types
supported by this reader. The strings returned are arbitrary, but should be
descriptive of and appropriate for the actual file types.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfilemembervars">
<title>Protected Member Variables</title>
<para>
<classname>KstDataSource</classname> contains various protected member variables that
the custom datasource reader can use. These variables are described below.
</para>
<variablelist>
<varlistentry>
<term><varname>bool _valid</varname></term>
<listitem>
<para>
This variable should be <literal>true</literal> if the custom datasource reader is
valid. Most likely the reader will be valid, unless there is an error condition (such
as the data file being unreadable by the reader). This variable is used by the
<function>isValid()</function> function of <classname>KstDataSource</classname>, which
is usually not reimplemented in subclasses.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>QStringList _fieldList</varname></term>
<listitem>
<para>
This variable should hold a list of the field names in the data source.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>QString _filename</varname></term>
<listitem>
<para>
This variable should hold the name of the data file this datasource reader is
associated with.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>QString _source</varname></term>
<listitem>
<para>
This variable should hold the type name of the source.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfilevirtualfxns">
<title>Virtual Functions</title>
<para>
The <classname>KstDataSource</classname> class contains many virtual functions that
should be redefined in the custom datasource reader. These functions are
in the template files <filename>template.h</filename> and <filename>template.cpp</filename>,
listed in the
<link linkend="supportingadditionalfileformatscreatingdatasourceobjectfileexample">Example Templates</link>
section. Descriptions of the functions follow.
</para>
<variablelist>
<varlistentry>
<term><function>TemplateSource(const QString&amp; <parameter>filename</parameter>, const QString&amp; <parameter>type</parameter>) </function></term>
<listitem>
<para>
The constructor for the datasource reader. <varname>filename</varname> is the name of the
file to read data from, and <varname>type</varname> is the type of data the file contains.
This constructor should most likely invoke the <classname>KstDataSource</classname> constructor
in the constructor initializer list, and probably call the <function>update</function>
function listed below to initialize member variables. In particular, the
<varname>bool _valid</varname> variable should be set appropriately.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual ~TemplateSource()</function></term>
<listitem>
<para>
The destructor for the datasource reader. Any dynamically allocated memory should be
freed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>KstObject::UpdateType</returnvalue> update(<parameter>int = -1</parameter>) </function></term>
<listitem>
<para>
This function should read any new data entered in the data file since the last time <function>update</function>
was called, and update the member variables appropriately. The function should return
<varname>KstObject::UPDATE</varname> if the file contained changes, or <varname>KstObject::NO_CHANGE</varname>
otherwise.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>int</returnvalue> readField(double *<parameter>v</parameter>, const QString &<parameter>field</parameter>, int <parameter>s</parameter>, int <parameter>n</parameter>)</function></term>
<listitem>
<para>
This function should read <varname>n</varname>
frames of data, starting at frame <varname>s</varname> from the field specified by the field name
<varname>field</varname>, and return the contents in the array <varname>v</varname>.
If <varname>n</varname> is less than 0, the function should instead read 1 sample from the start of
frame <varname>s</varname>. The number of samples that were read should be returned.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>bool</returnvalue> isValidField(const QString &<parameter>field</parameter>) const</function></term>
<listitem>
<para>
This function should return true of the field specified by the field name <varname>field</varname>
is a valid field in the current data source, or false if the field is not a valid field.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>int</returnvalue> samplesPerFrame(const QString& <parameter>field</parameter>)</function></term>
<listitem>
<para>
This function should return the ratio of samples per frame for the field specified by the field
name <varname>field</varname>. For data sources that do not make use of this concept, the number of samples
per frame can be set to <literal>1</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>int</returnvalue> frameCount() const</function></term>
<listitem>
<para>
This function should return the total number of frames in the data source, as of the last time
<function>update</function> was called.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>QString</returnvalue> fileType() const <parameter></parameter></function></term>
<listitem>
<para>
This function should return the file type of the data file currently being used, usually the
same as the <varname>type</varname> parameter that was passed to the constructor. Alternatively, it
can contain an error message (to indicate, for example, that the file is not
valid).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>virtual <returnvalue>void</returnvalue> save(QTextStream &<parameter>ts</parameter>)</function></term>
<listitem>
<para>
This function should save the file description information to <varname>ts</varname>. In most cases
the implementation provided by <classname>KstDataSource</classname> should be sufficient.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfileexample">
<title>Example Templates</title>
<para>
In general, the following two template files can be used to create new shared object files.
Simply modify the function bodies as appropriate for your particular data source.
</para>
<informalexample>
<screen>
/***************************************************************************
template.h - data source plugin template
-------------------
begin : Fri Oct 17 2003
copyright : (C) 2003 The University of Toronto
email :
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef TEMPLATE_H
#define TEMPLATE_H
#include &lt;kstdatasource.h&gt;
class TemplateSource : public KstDataSource {
public:
TemplateSource(const QString& filename, const QString& type);
virtual ~TemplateSource();
virtual KstObject::UpdateType update(int = -1);
virtual int readField(double *v, const QString &field, int s, int n);
virtual bool isValidField(const QString &field) const;
virtual int samplesPerFrame(const QString &field);
virtual int frameCount() const;
virtual QString fileType() const;
virtual void save(QTextStream &ts);
};
#endif
</screen>
</informalexample>
<para>
</para>
<informalexample>
<screen>
/***************************************************************************
template.cpp - data source template
-------------------
begin : Fri Oct 17 2003
copyright : (C) 2003 The University of Toronto
email :
***************************************************************************/
/***************************************************************************
* *
* 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 "template.h"
TemplateSource::TemplateSource(const QString& filename, const QString& type)
: KstDataSource(filename, type) {
}
TemplateSource::~TemplateSource() {
}
KstObject::UpdateType TemplateSource::update(int u) {
Q_UNUSED(u)
return KstObject::NO_CHANGE;
}
int TemplateSource::readField(double *v, const QString& field, int s, int n) {
Q_UNUSED(v)
Q_UNUSED(field)
Q_UNUSED(s)
Q_UNUSED(n)
return -1;
}
bool TemplateSource::isValidField(const QString& field) const {
Q_UNUSED(field)
return false;
}
int TemplateSource::samplesPerFrame(const QString &field) {
Q_UNUSED(field)
return 0;
}
int TemplateSource::frameCount() const {
return 0;
}
QString TemplateSource::fileType() const {
return QString::null;
}
void TemplateSource::save(QTextStream &ts) {
KstDataSource::save(ts);
}
extern "C" {
KstDataSource *create_template(const QString& filename, const QString& type) {
return new TemplateSource(filename, type);
}
QStringList provides_template() {
QStringList rc;
// create the stringlist
return rc;
}
bool understands_template(const QString& filename) {
// determine if it is an X file
Q_UNUSED(filename)
return false;
}
}
</screen>
</informalexample>
</sect3>
</sect2>
<sect2 id="supportingadditionalfileformatscreatingdatasourcedesktopfile">
<title>The <filename>.desktop</filename> File</title>
<para>
The following is an example of a <filename>.desktop</filename> file for the template plugin:
</para>
<informalexample>
<screen>
[Desktop Entry]
Encoding=UTF-8
Type=Service
ServiceTypes=Kst Data Source
X-KDE-ModuleType=Plugin
X-Kst-Plugin-Library=template
X-Kst-Plugin-Author=The University of Toronto
Name=File Reader Template
Comment=Long description of the file reader template.
</screen>
</informalexample>
<para>
You should add translations in additional languages for the Name and Comment fields
by adding additional Name and Comment fields with <literal>[xx]</literal> appended
to the end of the field names, where <literal>xx</literal> is the two-letter
language code. For example, to add Spanish translations, the following lines would
need to be added:
</para>
<informalexample>
<screen>
Name[es]=Plantilla de lectura de ficheros
Comment[es]=Plantilla de código para hacer un complemento de lectura de ficheros.
</screen>
</informalexample>
<para>
The field <literal>X-Kst-Plugin-Library</literal> should be exactly the same
as the decided library name for the plugin.
</para>
</sect2>
<sect2 id="supportingadditionalfileformatscreatingdatasourcecompiling">
<title>Compiling and Copying</title>
<para>
To compile and install the new custom datasource reader, create a new directory under
<filename>kst/datasources</filename> of the source package. Place the source
files for the object, along with
the <filename>.desktop</filename> file in the new directory. Then, edit
<filename>kst/datasources/Makefile.am</filename> so that <varname>SUBDIRS</varname>
contains the name of the new subdirectory. For example, if the new subdirectory is called
<filename>template</filename>, <varname>SUBDIRS</varname> might be changed to the
following:
<informalexample>
<screen>
SUBDIRS=ascii dirfile frame indirect template $(PIOLIB_SUBDIR) $(FITSIO_SUBDIR)
</screen>
</informalexample>
</para>
<para>
After the required files are in the newly created subdirectory, a <filename>Makefile.am</filename>
needs to be created in there as well. Use the following sample as a template, replacing
all instances of <quote>template</quote> with your custom library name in
<varname>kde_module_LTLIBRARIES</varname>, <varname>kstdata_template_la_SOURCES</varname>
and <varname>services_DATA</varname>.
</para>
<informalexample>
<screen>
INCLUDES=-I$(srcdir)/../.. $(all_includes)
kde_module_LTLIBRARIES=kstdata_template.la
kstdata_template_la_LDFLAGS=$(all_libraries) -module -avoid-version
kstdata_template_la_SOURCES=template.cpp
METASOURCES=AUTO
services_DATA=kstdata_template.desktop
servicesdir=$(kde_servicesdir)/kst
</screen>
</informalexample>
<para>
Once this is done, you can compile and re-install &kst; from the modified source package, or alternatively,
only install the new libraries, like follows (using the <quote>template</quote> subdirectory as an example).
First change to the root directory of the &kst; source package. Then,
<screen><userinput><command>./configure --prefix=`kde-config --prefix`</command>
<command>cd ./kst/datasources/template</command>
<command>make</command>
<command>make install</command></userinput></screen>
</para>
<para>
Restart &kst; and the new datasource reader should be loaded.
</para>
</sect2>
</sect1>
</appendix>
<!-- Keep this comment at the end of the file
Local variables:
mode: xml
sgml-omittag:nil
sgml-shorttag:nil
sgml-namecase-general:nil
sgml-general-insert-case:lower
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:0
sgml-indent-data:true
sgml-parent-document:("index.docbook" "book" "appendix")
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->