Home

MML Browser Example

This example demonstrates how to use QtMmlWidget. It allows the user to browse MML files, displaying their content. If a directory contains an MML file and a PNG image with the same name, the image is assumed to be a reference of how the MML file should be rendered, and is also displayed. The complete sources may be found in the example directory. The most rellevant files are shown below.


mainwindow.h:
#ifndef MAIN_WINDOW_H
#define MAIN_WINDOW_H

#include <qmainwindow.h>

class QtMmlWidget;
class QLabel;
class FileBrowser;

class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
        MainWindow(QWidget *parent = 0, const char *name = 0);

    public slots:
        void zoomIn();
        void zoomOut();
        void toggleDrawFrames();
        void openFile(const QString &mml_file_name);

    private:
        QtMmlWidget *m_mml_widget;
        QLabel *m_compare_image;
        FileBrowser *m_file_browser;
};

void showWarning(const QString &msg);

#endif


mainwindow.cpp:
#include <stdlib.h>
#include <errno.h>

#include <qfile.h>
#include <qtextstream.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpixmap.h>
#include <qtoolbutton.h>
#include <qtoolbar.h>
#include <qsplitter.h>
#include <qmessagebox.h>

#include <qtmmlwidget.h>

#include "mainwindow.h"
#include "filebrowser.h"

void showWarning(const QString &msg)
{
    QMessageBox::warning(0, "Warning", msg,
                            QMessageBox::Ok,
                            QMessageBox::NoButton,
                            QMessageBox::NoButton);
}

MainWindow::MainWindow(QWidget *parent, const char *name)
    : QMainWindow(parent, name)
{
    setCaption("MathML Browser");

    QSplitter *splitter = new QSplitter(this);
    splitter->setChildrenCollapsible(false);

    m_file_browser = new FileBrowser("*.mml", splitter);

    QWidget *formula_widget = new QWidget(splitter);
    QVBoxLayout *layout = new QVBoxLayout(formula_widget);
    layout->setAutoAdd(true);
    layout->setSpacing(3);
    m_mml_widget = new QtMmlWidget(formula_widget);
    m_mml_widget->setPaletteBackgroundColor(Qt::white);
    m_compare_image = new QLabel(formula_widget);
    m_compare_image->setAlignment(Qt::AlignCenter);
    m_compare_image->setPaletteBackgroundColor(Qt::white);

    setCentralWidget(splitter);

    QToolBar *tool_bar = new QToolBar(this);
    new QToolButton(QPixmap::fromMimeSource("zoom_in.png"), "Zoom in",
                    "Zoom in", this, SLOT(zoomIn()), tool_bar);
    new QToolButton(QPixmap::fromMimeSource("zoom_out.png"), "Zoom out",
                    "Zoom out", this, SLOT(zoomOut()), tool_bar);
    new QToolButton(QPixmap::fromMimeSource("frames.png"), "Toggle frames",
                    "Toggle frames", this, SLOT(toggleDrawFrames()), tool_bar);

    connect(m_file_browser, SIGNAL(fileSelected(const QString &)), this, SLOT(openFile(const QString &)));
}

void MainWindow::zoomIn()
{
    m_mml_widget->setBaseFontPointSize(m_mml_widget->baseFontPointSize() + 2);
}

void MainWindow::zoomOut()
{
    m_mml_widget->setBaseFontPointSize(m_mml_widget->baseFontPointSize() - 2);
}

void MainWindow::toggleDrawFrames()
{
    m_mml_widget->setDrawFrames(!m_mml_widget->drawFrames());
}

void MainWindow::openFile(const QString &mml_file_name)
{
    m_mml_widget->clear();
    m_compare_image->clear();

    QFile file(mml_file_name);
    if (!file.open(IO_ReadOnly)) {
        showWarning("File error: Could not open \""
                        + mml_file_name
                        + "\": " + file.errorString());
        return;
    }

    QTextStream stream(&file);
    stream.setEncoding(QTextStream::UnicodeUTF8);
    QString text = stream.read();
    file.close();

    QString error_msg;
    int error_line, error_column;
    bool result = m_mml_widget->setContent(text, &error_msg, &error_line,
                                                &error_column);

    if (!result) {
        showWarning("Parse error: line " + QString::number(error_line)
                        + ", col " + QString::number(error_column)
                        + ": " + error_msg);
        return;
    }

    QPixmap pm;
    int idx = mml_file_name.findRev('.');
    if (idx != -1) {
        QString image_file_name = mml_file_name.mid(0, idx + 1) + "png";
        if (pm.load(image_file_name))
            m_compare_image->setPixmap(pm);
    }
}


Copyright © 2003-2004 TrolltechTrademarks
Qt Solutions