How to write a QStarDict plugin

From QStarDict wiki
Jump to: navigation, search

This document describes development of a simple QStarDict plugin.

Contents

Plugin directory

In the first we must get latest SVN source. In the "plugins" directory we'll create directory named "color" and files in it.

Project file

color.pro:

#############################################################################
# color.pro - QStarDict, a StarDict clone written with using Qt             #
# Copyright (C) 2008 Author                                                 #
#                                                                           #
# 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.                                       #
#                                                                           #
# This program is distributed in the hope that it will be useful,           #
# but WITHOUT ANY WARRANTY; without even the implied warranty of            #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
# GNU General Public License for more details.                              #
#                                                                           #
# You should have received a copy of the GNU General Public License along   #
# with this program; if not, write to the Free Software Foundation, Inc.,   #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               #
#############################################################################
 
TARGET = color
include(../plugin.pri)
 
HEADERS += color.h
SOURCES += color.cpp

Header file

In header file we'll create a subclass of QObject and QStarDict::DictPlugin.

color.h

/*****************************************************************************
 * color.h - QStarDict, a StarDict clone written with using Qt               *
 * Copyright (C) 2008 Author                                                 *
 *                                                                           *
 * 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.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License along   *
 * with this program; if not, write to the Free Software Foundation, Inc.,   *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
 *****************************************************************************/
 
#ifndef COLOR_H
#define COLOR_H
 
#include <qstardict/dictplugin.h>
 
class Color: public QObject, public QStarDict::DictPlugin
{
    Q_OBJECT
    Q_INTERFACES(QStarDict::DictPlugin)
 
    public:
        Color(QObject *parent = 0);
 
        QString name() const
        { return "color"; } // must be same as TARGET in the project file
        QString version() const
        { return "0.01"; }
        QString description() const
        { return tr("A simple plugin for colorizing words"); }
        QStringList authors() const
        { return QStringList("Author <author@domain.org>"); }
        Features features() const
        { return Features(SettingsDialog); }
 
        QStringList availableDicts() const;
        QStringList loadedDicts() const
        { return m_loadedDicts; }
        void setLoadedDicts(const QStringList &dicts);
        DictInfo dictInfo(const QString &dict);
 
        bool isTranslatable(const QString &dict, const QString &word)
        { Q_UNUSED(dict) Q_UNUSED(word) return true; }
 
        Translation translate(const QString &dict, const QString &word);
 
        int execSettingsDialog(QWidget *parent);
 
    private:
        QStringList m_loadedDicts;
};
 
#endif // COLOR_H
 
// vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent

Source file

color.cpp

/*****************************************************************************
 * color.cpp - QStarDict, a StarDict clone written with using Qt             *
 * Copyright (C) 2008 Author                                                 *
 *                                                                           *
 * 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.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License along   *
 * with this program; if not, write to the Free Software Foundation, Inc.,   *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
 *****************************************************************************/
 
#include "color.h"
 
#include <QSettings>
#include <QInputDialog>
 
Color::Color(QObject *parent)
    : QObject(parent)
{
}
 
QStringList Color::availableDicts() const
{
    QSettings settings(workPath() + "/dicts.ini", QSettings::IniFormat);
    QStringList defaultList;
    defaultList << "red,orange,yellow,green,cyan,blue,magenta"
                << "black,darkgray,gray,lightgray";
    return settings.value("Main/dicts", defaultList).toStringList();
}
 
void Color::setLoadedDicts(const QStringList &dicts)
{
    m_loadedDicts = dicts;
}
 
 Color::DictInfo Color::dictInfo(const QString &dict)
{
    return DictInfo(dict, "", "", "", -1);
}
 
Color::Translation Color::translate(const QString &dict, const QString &word)
{
    QStringList colorsList = dict.split(",");
    QString result;
    for (QString::const_iterator i = word.begin(); i != word.end(); ++i)
        result += "<font color=\"" + colorsList[qrand() % colorsList.size()] + "\">" + *i + "</font>";
    return Translation(word, dict, result);
}
 
int Color::execSettingsDialog(QWidget *parent)
{
    bool ok = false;
    QString dict = QInputDialog::getText(parent,
            tr("Add new color dictionary"),
            tr("Enter name of dictionary in format \"html_color1,html_color2...\""),
            QLineEdit::Normal, QString(), &ok);
    if (ok)
    {
        QStringList available = availableDicts();
        available << dict;
        QSettings settings(workPath() + "/dicts.ini", QSettings::IniFormat);
        settings.setValue("Main/dicts", dict);
        return QDialog::Accepted;
    }
    return QDialog::Rejected;
}
 
Q_EXPORT_PLUGIN2(color, Color)
 
// vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc

Compiling

We must add the "color" item to the SUBDIRS list in the plugins.pro file. So we can run

make
make install

in plugins directory to compile and install our plugin.

It works!

Now we can run QStarDict, enable color plugin and dictionaries from it and type any word. Result displayed at screenshot:

Qstardict color plugin.png

We can add own dictionaries using settings dialog:

Qstardict color plugin add new dictionary.png

What is the next?

When plugin fully written and tested it can be sent to QStarDict author.

Personal tools