C

Adding Views

You can write plugins to add views that contain your own controls to the emulator. The plugins display the controls and act as servers that accept connections from the emulated applications. You must also write a target library or application that receives data from the controls.

Writing Emulator Plugins

Write a simple plugin as described in How to Create Qt Plugins.

The plugin class has a single function that returns all views to show in the emulator, as illustrated by the following code snippet:

class ExamplePlugin : public QObject, public PluginInterface
{
    Q_OBJECT
    Q_INTERFACES(PluginInterface)

public:
    QList<ToolBoxPage *> pages(QWidget *);
};

The following code snippet illustrates how to return a view created from the ExamplePage class:

QList<ToolBoxPage *> ExamplePlugin::pages(QWidget *parent)
{
    return QList<ToolBoxPage *>() << new ExamplePage(parent);
}

Q_EXPORT_PLUGIN2(example_plugin, ExamplePlugin)

Creating Views

The implementation of an emulator view must contain all the visible widgets. The following code snippet implements a view that contains an editable QLineEdit widget:

class ExamplePage : public ToolBoxPage
{
    Q_OBJECT

public:
    explicit ExamplePage(QWidget *parent = 0);
    virtual ~ExamplePage();

    void writeSettings(QSettings &) const;
    void readSettings(QSettings &);

    QIcon icon() const;

public slots:
    void textEdited(const QString &);
    void showText(const QString &);

private slots:
    void newConnection(Simulator::ConnectionWorker *);
    void disconnected();

private:
    Simulator::Connection *mConnection;
    QLineEdit *target;
    QList<Simulator::ConnectionWorker *> mWorkers;
};

The constructor sets up a QWidget that contains the contents to display. The connection server is set up using the Connection class:

ExamplePage::ExamplePage(QWidget *parent)
    : ToolBoxPage(parent)
    , mConnection(0)
{
    QList<OptionsItem *> optionsList;
    target = new QLineEdit(this);
    optionsList << new OptionsItem(tr("Example control"), target);
    setOptions(optionsList);

    setTitle(tr("Example Page"));

    mConnection = new Connection(Connection::Server, "example", 0xbeef+3, Version(1,0,0,0), this);

    connect(target, SIGNAL(textEdited ( const QString &)), this, SLOT(textEdited(const QString &)));
    connect(mConnection, SIGNAL(newConnection(Simulator::ConnectionWorker *)), this, SLOT(newConnection(Simulator::ConnectionWorker *)));
}

The following code snippet is an example of a slot that is called when the text in QLineEdit changes. The slot notifies the connected clients about the change. If multiple clients are connected, all of them have to be notified:

void ExamplePage::textEdited(const QString &text)
{
    foreach (ConnectionWorker *worker, mWorkers)
        worker->call("showText", text);
}

Reimplement the following functions to store persistent values in the emulator configuration. Make sure you call the the inherited functions from the base class as early as possible, because ToolBoxPage needs to save its window state:

void ExamplePage::writeSettings(QSettings &settings) const
{
    ToolBoxPage::writeSettings(settings);

    settings.beginGroup(title());
    settings.setValue("myValue", 5);
    settings.endGroup();
}

void ExamplePage::readSettings(QSettings &settings)
{
    ToolBoxPage::readSettings(settings);

    settings.beginGroup(title());
    if (settings.contains("myValue"))
        int myValue = settings.value("myValue").toInt();
    settings.endGroup();
}

The server uses the following two slots to manage the connected clients by connecting and disconnecting them as necessary:

void ExamplePage::newConnection(ConnectionWorker *worker)
{
    qDebug() << "New connection requested";
    connect(worker, SIGNAL(disconnected()), this, SLOT(disconnected()));
    mWorkers.append(worker);
    worker->addReceiver(this);
}

void ExamplePage::disconnected()
{
    ConnectionWorker *worker = qobject_cast<ConnectionWorker *>(sender());
    worker->deleteLater();
    mWorkers.removeAll(worker);
}

The emulator displays icons that you can select to open views. To display an icon for your view, implement the function QIcon icon() const and make sure that it returns a valid icon with the size of 32x32 pixels. If you do not implement the function, the name of the page is used instead. You can set the page name using void setName(const QString &). The name is mainly used in the Menu.

Available under certain Qt licenses.
Find out more.