Warning, /graphics/kst-plot/docbook/kst/additionalformats-chapter.docbook is written in an unsupported language. File is not indexed.
0001 <appendix id="supportingadditionalfileformats">
0002 <title>Supporting Additional File Formats</title>
0003 <warning>
0004 <para>WARNING: This section was written for Kst version 1 and may be somewhat, or even totally obsolete. WARNING</para>
0005 </warning>
0006 <para>
0007 This section describes
0008 how to create additional
0009 readers for unsupported file formats. If you are not already familiar with data source
0010 concepts, please read
0011 <link linkend="DataSourceConcepts">Data Source Concepts</link>
0012 </para>
0013
0014
0015
0016 <sect1 id="supportingadditionalfileformatscreatingdatasource">
0017 <title>Creating Datasource Readers</title>
0018
0019 <para>
0020 If you wish to use a file format other than those currently supported, you might choose to write
0021 custom datasource readers.
0022 </para>
0023 <para>
0024 All &kst; datasource readers are regular &kde; plugins.
0025 Like all &kde; plugins, each datasource reader must have a shared object file
0026 and a <filename>*.desktop</filename> file. Before writing the reader, the
0027 library name of the plugin must be decided. This name must be a valid
0028 variable name in C, as it will be used in the function names of the shared object.
0029 For example, the library name of the reader for ASCII files is
0030 <quote>ascii</quote>.
0031 </para>
0032 <note>
0033 <para>
0034 &kde; plugins are <emphasis>not</emphasis>
0035 the same as the &kst; plugins used to manipulate data. All references to
0036 plugins in this section are to &kde; plugins.
0037 </para>
0038 </note>
0039
0040 <sect2 id="supportingadditionalfileformatscreatingdatasourceobjectfile">
0041 <title>The Shared Object</title>
0042 <para>
0043 A datasource reader should be a subclass of the abstract
0044 <classname>KstDataSource</classname> class. Ensure that you include the header file
0045 for <classname>KstDataSource</classname> in the source code for your datasource reader:
0046 <screen>
0047 #include <kstdatasource.h>
0048 </screen>
0049 There are certain requirements that a datasource reader must meet. One requirement
0050 concerns the presence of exported C functions. Other requirements
0051 are consequences of the fact that datasource readers inherit from <classname>KstDataSource</classname>.
0052 Both sets of requirements, along with suggestions and general explanations, are provided in the
0053 following sections.
0054 </para>
0055
0056 <sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfileexportedfxns">
0057 <title>Exported C Functions</title>
0058 <para>
0059 The exported C functions are listed below. In the following
0060 examples, all instances of <varname><libname></varname> should be replaced with
0061 the actual library name of the plugin.
0062 </para>
0063 <variablelist>
0064 <varlistentry>
0065 <term><function><returnvalue>KstDataSource *</returnvalue>create_<libname>(const QString& <parameter>filename</parameter>, const QString& <parameter>type</parameter>)</function></term>
0066 <listitem>
0067 <para>
0068 This function should create a new datasource reader of type <varname><libname></varname>,
0069 where <varname><libname></varname> is the library name of the plugin. A pointer of type
0070 <classname>KstDataSource</classname> to the new reader should be returned.
0071 </para>
0072 </listitem>
0073 </varlistentry>
0074
0075 <varlistentry>
0076 <term><function><returnvalue>bool</returnvalue> understands_<libname>(const QString& <parameter>filename</parameter>)</function></term>
0077 <listitem>
0078 <para>
0079 This function should return true if the file specified by <varname>filename</varname> is
0080 of a valid type supported by this reader, and false otherwise.
0081 The function should check the contents of the
0082 file for validity, and not rely on any filename extensions.
0083 </para>
0084 </listitem>
0085 </varlistentry>
0086
0087 <varlistentry>
0088 <term><function><returnvalue>QStringList</returnvalue> provides_<libname>()</function></term>
0089 <listitem>
0090 <para>
0091 This function should return a <classname>QStringList</classname> of the file types
0092 supported by this reader. The strings returned are arbitrary, but should be
0093 descriptive of and appropriate for the actual file types.
0094 </para>
0095 </listitem>
0096 </varlistentry>
0097
0098 </variablelist>
0099
0100 </sect3>
0101
0102 <sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfilemembervars">
0103 <title>Protected Member Variables</title>
0104 <para>
0105 <classname>KstDataSource</classname> contains various protected member variables that
0106 the custom datasource reader can use. These variables are described below.
0107 </para>
0108
0109 <variablelist>
0110 <varlistentry>
0111 <term><varname>bool _valid</varname></term>
0112 <listitem>
0113 <para>
0114 This variable should be <literal>true</literal> if the custom datasource reader is
0115 valid. Most likely the reader will be valid, unless there is an error condition (such
0116 as the data file being unreadable by the reader). This variable is used by the
0117 <function>isValid()</function> function of <classname>KstDataSource</classname>, which
0118 is usually not reimplemented in subclasses.
0119 </para>
0120 </listitem>
0121 </varlistentry>
0122
0123 <varlistentry>
0124 <term><varname>QStringList _fieldList</varname></term>
0125 <listitem>
0126 <para>
0127 This variable should hold a list of the field names in the data source.
0128 </para>
0129 </listitem>
0130 </varlistentry>
0131
0132 <varlistentry>
0133 <term><varname>QString _filename</varname></term>
0134 <listitem>
0135 <para>
0136 This variable should hold the name of the data file this datasource reader is
0137 associated with.
0138 </para>
0139 </listitem>
0140 </varlistentry>
0141
0142 <varlistentry>
0143 <term><varname>QString _source</varname></term>
0144 <listitem>
0145 <para>
0146 This variable should hold the type name of the source.
0147 </para>
0148 </listitem>
0149 </varlistentry>
0150 </variablelist>
0151
0152 </sect3>
0153
0154 <sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfilevirtualfxns">
0155 <title>Virtual Functions</title>
0156 <para>
0157 The <classname>KstDataSource</classname> class contains many virtual functions that
0158 should be redefined in the custom datasource reader. These functions are
0159 in the template files <filename>template.h</filename> and <filename>template.cpp</filename>,
0160 listed in the
0161 <link linkend="supportingadditionalfileformatscreatingdatasourceobjectfileexample">Example Templates</link>
0162 section. Descriptions of the functions follow.
0163 </para>
0164
0165 <variablelist>
0166 <varlistentry>
0167 <term><function>TemplateSource(const QString&amp; <parameter>filename</parameter>, const QString&amp; <parameter>type</parameter>) </function></term>
0168 <listitem>
0169 <para>
0170 The constructor for the datasource reader. <varname>filename</varname> is the name of the
0171 file to read data from, and <varname>type</varname> is the type of data the file contains.
0172 This constructor should most likely invoke the <classname>KstDataSource</classname> constructor
0173 in the constructor initializer list, and probably call the <function>update</function>
0174 function listed below to initialize member variables. In particular, the
0175 <varname>bool _valid</varname> variable should be set appropriately.
0176 </para>
0177 </listitem>
0178 </varlistentry>
0179
0180 <varlistentry>
0181 <term><function>virtual ~TemplateSource()</function></term>
0182 <listitem>
0183 <para>
0184 The destructor for the datasource reader. Any dynamically allocated memory should be
0185 freed.
0186 </para>
0187 </listitem>
0188 </varlistentry>
0189
0190 <varlistentry>
0191 <term><function>virtual <returnvalue>KstObject::UpdateType</returnvalue> update(<parameter>int = -1</parameter>) </function></term>
0192 <listitem>
0193 <para>
0194 This function should read any new data entered in the data file since the last time <function>update</function>
0195 was called, and update the member variables appropriately. The function should return
0196 <varname>KstObject::UPDATE</varname> if the file contained changes, or <varname>KstObject::NO_CHANGE</varname>
0197 otherwise.
0198 </para>
0199 </listitem>
0200 </varlistentry>
0201
0202 <varlistentry>
0203 <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>
0204 <listitem>
0205 <para>
0206 This function should read <varname>n</varname>
0207 frames of data, starting at frame <varname>s</varname> from the field specified by the field name
0208 <varname>field</varname>, and return the contents in the array <varname>v</varname>.
0209 If <varname>n</varname> is less than 0, the function should instead read 1 sample from the start of
0210 frame <varname>s</varname>. The number of samples that were read should be returned.
0211 </para>
0212 </listitem>
0213 </varlistentry>
0214
0215 <varlistentry>
0216 <term><function>virtual <returnvalue>bool</returnvalue> isValidField(const QString &<parameter>field</parameter>) const</function></term>
0217 <listitem>
0218 <para>
0219 This function should return true of the field specified by the field name <varname>field</varname>
0220 is a valid field in the current data source, or false if the field is not a valid field.
0221 </para>
0222 </listitem>
0223 </varlistentry>
0224
0225 <varlistentry>
0226 <term><function>virtual <returnvalue>int</returnvalue> samplesPerFrame(const QString& <parameter>field</parameter>)</function></term>
0227 <listitem>
0228 <para>
0229 This function should return the ratio of samples per frame for the field specified by the field
0230 name <varname>field</varname>. For data sources that do not make use of this concept, the number of samples
0231 per frame can be set to <literal>1</literal>.
0232 </para>
0233 </listitem>
0234 </varlistentry>
0235
0236 <varlistentry>
0237 <term><function>virtual <returnvalue>int</returnvalue> frameCount() const</function></term>
0238 <listitem>
0239 <para>
0240 This function should return the total number of frames in the data source, as of the last time
0241 <function>update</function> was called.
0242 </para>
0243 </listitem>
0244 </varlistentry>
0245
0246 <varlistentry>
0247 <term><function>virtual <returnvalue>QString</returnvalue> fileType() const <parameter></parameter></function></term>
0248 <listitem>
0249 <para>
0250 This function should return the file type of the data file currently being used, usually the
0251 same as the <varname>type</varname> parameter that was passed to the constructor. Alternatively, it
0252 can contain an error message (to indicate, for example, that the file is not
0253 valid).
0254 </para>
0255 </listitem>
0256 </varlistentry>
0257
0258 <varlistentry>
0259 <term><function>virtual <returnvalue>void</returnvalue> save(QTextStream &<parameter>ts</parameter>)</function></term>
0260 <listitem>
0261 <para>
0262 This function should save the file description information to <varname>ts</varname>. In most cases
0263 the implementation provided by <classname>KstDataSource</classname> should be sufficient.
0264 </para>
0265 </listitem>
0266 </varlistentry>
0267
0268
0269 </variablelist>
0270
0271 </sect3>
0272
0273 <sect3 id="supportingadditionalfileformatscreatingdatasourceobjectfileexample">
0274 <title>Example Templates</title>
0275 <para>
0276 In general, the following two template files can be used to create new shared object files.
0277 Simply modify the function bodies as appropriate for your particular data source.
0278 </para>
0279 <informalexample>
0280 <screen>
0281 /***************************************************************************
0282 template.h - data source plugin template
0283 -------------------
0284 begin : Fri Oct 17 2003
0285 copyright : (C) 2003 The University of Toronto
0286 email :
0287 ***************************************************************************/
0288
0289 /***************************************************************************
0290 * *
0291 * This program is free software; you can redistribute it and/or modify *
0292 * it under the terms of the GNU General Public License as published by *
0293 * the Free Software Foundation; either version 2 of the License, or *
0294 * (at your option) any later version. *
0295 * *
0296 ***************************************************************************/
0297
0298 #ifndef TEMPLATE_H
0299 #define TEMPLATE_H
0300
0301 #include &lt;kstdatasource.h&gt;
0302
0303
0304 class TemplateSource : public KstDataSource {
0305 public:
0306 TemplateSource(const QString& filename, const QString& type);
0307
0308 virtual ~TemplateSource();
0309
0310 virtual KstObject::UpdateType update(int = -1);
0311
0312 virtual int readField(double *v, const QString &field, int s, int n);
0313
0314 virtual bool isValidField(const QString &field) const;
0315
0316 virtual int samplesPerFrame(const QString &field);
0317
0318 virtual int frameCount() const;
0319
0320 virtual QString fileType() const;
0321
0322 virtual void save(QTextStream &ts);
0323 };
0324
0325
0326 #endif
0327 </screen>
0328 </informalexample>
0329 <para>
0330
0331 </para>
0332 <informalexample>
0333 <screen>
0334 /***************************************************************************
0335 template.cpp - data source template
0336 -------------------
0337 begin : Fri Oct 17 2003
0338 copyright : (C) 2003 The University of Toronto
0339 email :
0340 ***************************************************************************/
0341
0342 /***************************************************************************
0343 * *
0344 * This program is free software; you can redistribute it and/or modify *
0345 * it under the terms of the GNU General Public License as published by *
0346 * the Free Software Foundation; either version 2 of the License, or *
0347 * (at your option) any later version. *
0348 * *
0349 ***************************************************************************/
0350
0351 #include "template.h"
0352
0353
0354 TemplateSource::TemplateSource(const QString& filename, const QString& type)
0355 : KstDataSource(filename, type) {
0356 }
0357
0358
0359 TemplateSource::~TemplateSource() {
0360 }
0361
0362
0363 KstObject::UpdateType TemplateSource::update(int u) {
0364 Q_UNUSED(u)
0365 return KstObject::NO_CHANGE;
0366 }
0367
0368
0369 int TemplateSource::readField(double *v, const QString& field, int s, int n) {
0370 Q_UNUSED(v)
0371 Q_UNUSED(field)
0372 Q_UNUSED(s)
0373 Q_UNUSED(n)
0374 return -1;
0375 }
0376
0377
0378 bool TemplateSource::isValidField(const QString& field) const {
0379 Q_UNUSED(field)
0380 return false;
0381 }
0382
0383
0384 int TemplateSource::samplesPerFrame(const QString &field) {
0385 Q_UNUSED(field)
0386 return 0;
0387 }
0388
0389
0390 int TemplateSource::frameCount() const {
0391 return 0;
0392 }
0393
0394
0395 QString TemplateSource::fileType() const {
0396 return QString::null;
0397 }
0398
0399
0400 void TemplateSource::save(QTextStream &ts) {
0401 KstDataSource::save(ts);
0402 }
0403
0404
0405 extern "C" {
0406 KstDataSource *create_template(const QString& filename, const QString& type) {
0407 return new TemplateSource(filename, type);
0408 }
0409
0410 QStringList provides_template() {
0411 QStringList rc;
0412 // create the stringlist
0413 return rc;
0414 }
0415
0416 bool understands_template(const QString& filename) {
0417 // determine if it is an X file
0418 Q_UNUSED(filename)
0419 return false;
0420 }
0421
0422 }
0423 </screen>
0424 </informalexample>
0425 </sect3>
0426
0427 </sect2>
0428
0429 <sect2 id="supportingadditionalfileformatscreatingdatasourcedesktopfile">
0430 <title>The <filename>.desktop</filename> File</title>
0431 <para>
0432 The following is an example of a <filename>.desktop</filename> file for the template plugin:
0433 </para>
0434 <informalexample>
0435 <screen>
0436 [Desktop Entry]
0437 Encoding=UTF-8
0438 Type=Service
0439 ServiceTypes=Kst Data Source
0440 X-KDE-ModuleType=Plugin
0441 X-Kst-Plugin-Library=template
0442 X-Kst-Plugin-Author=The University of Toronto
0443 Name=File Reader Template
0444 Comment=Long description of the file reader template.
0445 </screen>
0446 </informalexample>
0447
0448 <para>
0449 You should add translations in additional languages for the Name and Comment fields
0450 by adding additional Name and Comment fields with <literal>[xx]</literal> appended
0451 to the end of the field names, where <literal>xx</literal> is the two-letter
0452 language code. For example, to add Spanish translations, the following lines would
0453 need to be added:
0454 </para>
0455 <informalexample>
0456 <screen>
0457 Name[es]=Plantilla de lectura de ficheros
0458 Comment[es]=Plantilla de código para hacer un complemento de lectura de ficheros.
0459 </screen>
0460 </informalexample>
0461 <para>
0462 The field <literal>X-Kst-Plugin-Library</literal> should be exactly the same
0463 as the decided library name for the plugin.
0464 </para>
0465 </sect2>
0466
0467 <sect2 id="supportingadditionalfileformatscreatingdatasourcecompiling">
0468 <title>Compiling and Copying</title>
0469 <para>
0470 To compile and install the new custom datasource reader, create a new directory under
0471 <filename>kst/datasources</filename> of the source package. Place the source
0472 files for the object, along with
0473 the <filename>.desktop</filename> file in the new directory. Then, edit
0474 <filename>kst/datasources/Makefile.am</filename> so that <varname>SUBDIRS</varname>
0475 contains the name of the new subdirectory. For example, if the new subdirectory is called
0476 <filename>template</filename>, <varname>SUBDIRS</varname> might be changed to the
0477 following:
0478 <informalexample>
0479 <screen>
0480 SUBDIRS=ascii dirfile frame indirect template $(PIOLIB_SUBDIR) $(FITSIO_SUBDIR)
0481 </screen>
0482 </informalexample>
0483 </para>
0484
0485
0486 <para>
0487 After the required files are in the newly created subdirectory, a <filename>Makefile.am</filename>
0488 needs to be created in there as well. Use the following sample as a template, replacing
0489 all instances of <quote>template</quote> with your custom library name in
0490 <varname>kde_module_LTLIBRARIES</varname>, <varname>kstdata_template_la_SOURCES</varname>
0491 and <varname>services_DATA</varname>.
0492 </para>
0493 <informalexample>
0494 <screen>
0495 INCLUDES=-I$(srcdir)/../.. $(all_includes)
0496
0497 kde_module_LTLIBRARIES=kstdata_template.la
0498
0499 kstdata_template_la_LDFLAGS=$(all_libraries) -module -avoid-version
0500 kstdata_template_la_SOURCES=template.cpp
0501
0502 METASOURCES=AUTO
0503
0504 services_DATA=kstdata_template.desktop
0505 servicesdir=$(kde_servicesdir)/kst
0506 </screen>
0507 </informalexample>
0508
0509 <para>
0510 Once this is done, you can compile and re-install &kst; from the modified source package, or alternatively,
0511 only install the new libraries, like follows (using the <quote>template</quote> subdirectory as an example).
0512 First change to the root directory of the &kst; source package. Then,
0513 <screen><userinput><command>./configure --prefix=`kde-config --prefix`</command>
0514 <command>cd ./kst/datasources/template</command>
0515 <command>make</command>
0516 <command>make install</command></userinput></screen>
0517 </para>
0518
0519 <para>
0520 Restart &kst; and the new datasource reader should be loaded.
0521 </para>
0522 </sect2>
0523
0524 </sect1>
0525
0526
0527 </appendix>
0528
0529
0530 <!-- Keep this comment at the end of the file
0531 Local variables:
0532 mode: xml
0533 sgml-omittag:nil
0534 sgml-shorttag:nil
0535 sgml-namecase-general:nil
0536 sgml-general-insert-case:lower
0537 sgml-minimize-attributes:nil
0538 sgml-always-quote-attributes:t
0539 sgml-indent-step:0
0540 sgml-indent-data:true
0541 sgml-parent-document:("index.docbook" "book" "appendix")
0542 sgml-exposed-tags:nil
0543 sgml-local-catalogs:nil
0544 sgml-local-ecat-files:nil
0545 End:
0546 -->