This section describes how to create additional readers for currently unsupported file formats. If you are not already familiar with data source concepts, please read Data Source Concepts
If you wish to use a file format other than those currently supported, you might choose to write a custom datasource reader.
All Kst datasource readers are regular KDE plugins.
Like all KDE plugins, each datasource reader must have a shared object file
and a .desktop
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
“ascii”.
KDE plugins are not the same as the Kst plugins used to manipulate data. All references to plugins in this section are to KDE plugins.
A datasource reader should be a subclass of the abstract
KstDataSource
class. Ensure that you include the header file
for KstDataSource
in the source code for your datasource reader:
#include <kstdatasource.h>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
KstDataSource
.
Both sets of requirements, along with suggestions and general explanations, are provided in the
following sections.
The exported “C” functions are listed below. In the following
examples, all instances of libname
should be replaced with
the actual library name of the plugin.
KstDataSource *create_libname
(
KConfig* cfg
,
const QString& filename
,
const QString& type
)
This function should create a new datasource reader of type libname
,
where libname
is the library name of the plugin. A pointer of type
KstDataSource
to the new reader should be returned.
KstDataSource *load_libname
(
KConfig* cfg
,
const QString& filename
,
const QString& type
,
const QDomElement& e
)
This function should create a new datasource reader of type libname
,
where libname
is the library name of the plugin. A pointer of type
KstDataSource
to the new reader should be returned.
int understands_libname
(const QString& filename
)
This function should return an integer from 0 to 100. A value of 0 indicates a complete inability of the datasource
to read the file specified by filename
, while a value of 100 indicates that the datasource
is completely confident that it is best able to understand the file specified by filename
.
The function should check the contents of the file for validity, and not simply rely on any filename extensions.
QStringList fieldList_libname
(
KConfig* cfg
,
const QString& filename
,
const QString& type
,
QString* typeSuggestion
,
bool* complete
)
This function should, if it is able to understand the file specified by filename
, return a
list of the fields that it found within the file, else it should return an empty QStringList
.
The value of typeSuggestion
should be set to the file type and the value of complete
should be set to false if no fields were found, else it should be set to true.
QStringList matrixList_libname
(
KConfig* cfg
,
const QString& filename
,
const QString& type
,
QString* typeSuggestion
,
bool* complete
)
This function should, if it is able to understand the file specified by filename
, return a
list of the matrices that it found within the file, else it should return an empty QStringList
.
The value of typeSuggestion
should be set to the file type and the value of complete
should be set to false if no matrices were found, else it should be set to true.
QWidget* widget_libname
( )
If the datasource wants to provide a custom configuration dialog it should implement this function and return the newly created
QWidget
derived class here.
QStringList provides_libname
( )
This function should return a QStringList
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.
bool supportsHierarchy_libname
( )
This function should return true if the datasource supports hierarchical field names.
The hierarchy is denoted by the “/
” character.
KstDataSource
contains various protected member variables that
the custom datasource reader can use. These variables are described below.
bool _valid
This variable should be true
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
isValid()
function of KstDataSource
, which
is usually not reimplemented in subclasses.
QStringList _fieldList
This variable should hold a list of the field names in the data source.
QString _filename
This variable should hold the name of the data file this datasource reader is associated with.
QString _source
This variable should hold the type name of the source.
The KstDataSource
class contains many virtual functions that
can be overridden in the custom datasource reader. It is only necessary to override
those functions where the default implementation is insufficient.
These functions are in the template files
template.h
and template.cpp
, listed in the
Example Templates section.
Descriptions of the functions follow.
TemplateSource(const QString& filename
, const QString& type
)
The constructor for the datasource reader. filename
is the name of the
file to read data from, and type
is the type of data the file contains.
This constructor should most likely invoke the KstDataSource
constructor
in the constructor initializer list, and probably call the update
function listed below to initialize member variables. In particular, the
_valid
variable should be set appropriately.
virtual ~TemplateSource()
The destructor for the datasource reader. Any dynamically allocated memory should be freed.
virtual KstObject::UpdateType update(int = -1
)
This function should see if new data exists in the data file since the last time update
was called, and update the frame count and other member variables appropriately.
The function should return
KstObject::UPDATE
if the file contained changes, or KstObject::NO_CHANGE
otherwise.
virtual int readField(double *v
, const QString &field
, int s
, int n
)
This function should read n
frames of data, starting at frame s
from the field specified by the field name
field
, and return the contents in the array v
.
If n
is less than 0, the function should instead read 1 sample from the start of
frame s
. The number of samples that were read should be returned.
virtual bool isValidField(const QString &field
) const
This function should return true if the field specified by the field name field
is a valid field in the current data source, or false if the field is not a valid field.
virtual int readMatrix(
KstMatrixData *data
,
const QString &matrix
,
int xStart
,
int yStart
,
int xNumSteps
,
int yNumSteps
)
This function should read the specified sub-range of the matrix, reading xNumSteps
starting at xStart
, and yNumSteps
starting at yStart
from the
matrix specified by the matrix name
matrix
, and return the contents in the KstMatrixData
data
.
If xNumSteps
is less than 0, the function should instead read 1 sample starting from xStart
.
If yNumSteps
is less than 0, the function should instead read 1 sample starting from yStart
.
virtual bool isValidMatrix(const QString &matrix
) const
This function should return true if the matrix specified by the matrix name matrix
is a valid matrix in the current data source, or false if the matrix is not a valid matrix.
virtual bool matrixDimensions(
const QString &matrix
,
int* xDim
,
int* yDim
)
This function should return true if the matrix specified by the matrix name matrix
is a valid matrix in the current data source, or false if the matrix is not a valid matrix.
If the matrix is a valid matrix then the values of xDim
and yDim
should be set to the x-dimension and y-dimension of the matrix, respectively.
virtual int samplesPerFrame(const QString& field
)
This function should return the ratio of samples per frame for the field specified by the field
name field
. For data sources that do not make use of this concept, the number of samples
per frame can be set to 1
.
virtual int frameCount() const
This function should return the total number of frames in the data source, as of the last time
update
was called.
virtual QString fileType() const
This function should return the file type of the data file currently being used, usually the
same as the type
parameter that was passed to the constructor. Alternatively, it
can contain an error message (to indicate, for example, that the file is not
valid).
virtual void save(QTextStream &ts
)
This function should save the file description information to ts
. In most cases
the implementation provided by KstDataSource
should be sufficient.
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.
/*************************************************************************** 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 <kstdatasource.h> 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
/*************************************************************************** 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; } }
The following is an example of a .desktop
file for the template plugin:
[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 British Columbia Name=File Reader Template Comment=Long description of the file reader template.
You should add translations in additional languages for the Name and Comment fields
by adding additional Name and Comment fields with [xx]
appended
to the end of the field names, where xx
is the two-letter
language code. For example, to add Spanish translations, the following lines would
need to be added:
Name[es]=Plantilla de lectura de ficheros Comment[es]=Plantilla de código para hacer un complemento de lectura de ficheros.
The field X-Kst-Plugin-Library
should be exactly the same
as the decided library name for the plugin.
To compile and install the new custom datasource reader, create a new directory under
kst/datasources
of the source package. Place the source
files for the object, along with
the .desktop
file in the new directory. Then, edit
kst/datasources/Makefile.am
so that SUBDIRS
contains the name of the new subdirectory. For example, if the new subdirectory is called
template
, SUBDIRS
might be changed to the
following:
SUBDIRS=ascii dirfile frame indirect template $(PIOLIB_SUBDIR) $(FITSIO_SUBDIR)
After the required files are in the newly created subdirectory, a Makefile.am
needs to be created in there as well. Use the following sample as a template, replacing
all instances of “template” with your custom library name in
kde_module_LTLIBRARIES
, kstdata_template_la_SOURCES
and services_DATA
.
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
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 “template” subdirectory as an example). First change to the root directory of the Kst source package. Then,
./configure --prefix=`kde-config --prefix`
cd ./kst/datasources/template
make
make install
Restart Kst and the new datasource reader should be automatically loaded.
Would you like to make a comment or contribute an update to this page?
Send feedback to the KDE Docs Team