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.

<hr> mainwindow.h:

 /****************************************************************************
 **
 ** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of a Qt Solutions component.
 **
 ** Licensees holding a valid Qt Solutions License Agreement may use this
 ** file in accordance with the rights, responsibilities, and obligations
 ** contained therein. Please consult your licensing agreement or contact
 ** sales@trolltech.com if any conditions of this licensing are not clear
 ** to you.
 **
 ** Further information about Qt Solutions licensing is available at:
 ** http://www.trolltech.com/products/qt/addon/solutions/
 ** or by contacting info@trolltech.com.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/
 #ifndef MAIN_WINDOW_H
 #define MAIN_WINDOW_H

 #include <QtGui/QMainWindow>
 //Added by qt3to4:
 #include <QtGui/QLabel>

 class QtMmlWidget;
 class QLabel;
 class FileBrowser;

 class MainWindow : public QMainWindow
 {
     Q_OBJECT

     public:
         MainWindow(QWidget *parent = 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

<hr> mainwindow.cpp:

 /****************************************************************************
 **
 ** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of a Qt Solutions component.
 **
 ** Licensees holding a valid Qt Solutions License Agreement may use this
 ** file in accordance with the rights, responsibilities, and obligations
 ** contained therein. Please consult your licensing agreement or contact
 ** sales@trolltech.com if any conditions of this licensing are not clear
 ** to you.
 **
 ** Further information about Qt Solutions licensing is available at:
 ** http://www.trolltech.com/products/qt/addon/solutions/
 ** or by contacting info@trolltech.com.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/
 #include <stdlib.h>
 #include <errno.h>

 #include <QtCore/QFile>
 #include <QtCore/QTextStream>
 #include <QtGui/QLabel>
 #include <QtGui/QLayout>
 #include <QtGui/QPixmap>
 #include <QtGui/QToolButton>
 #include <QtGui/QToolBar>
 #include <QtGui/QSplitter>
 #include <QtGui/QMessageBox>

 #include <qtmmlwidget.h>
 #include <QtGui/QVBoxLayout>

 #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)
     : QMainWindow(parent)
 {
     setWindowTitle("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;
     layout->setSpacing(2);
     layout->setMargin(2);
     m_mml_widget = new QtMmlWidget(formula_widget);
     layout->addWidget(m_mml_widget);
     m_mml_widget->setBackgroundRole(QPalette::Base);
     m_compare_image = new QLabel;
     layout->addWidget(m_compare_image);
     m_compare_image->setAlignment(Qt::AlignCenter);
     m_compare_image->setBackgroundRole(QPalette::Base);

     formula_widget->setLayout(layout);
     setCentralWidget(splitter);

     QToolBar *tool_bar = new QToolBar(this);
     tool_bar->addAction(QIcon(":/images/zoom_in.png"),
                     "Zoom in", this, SLOT(zoomIn()));
     tool_bar->addAction(QIcon(":/images/zoom_out.png"),
                     "Zoom out", this, SLOT(zoomOut()));
     tool_bar->addAction(QIcon(":/images/frames.png"),
                     "Toggle frames", this, SLOT(toggleDrawFrames()));

     addToolBar(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(QIODevice::ReadOnly)) {
         showWarning("File error: Could not open \""
                         + mml_file_name
                         + "\": " + file.errorString());
         return;
     }

     QTextStream stream(&file);
 //    stream.setEncoding(QTextStream::UnicodeUTF8);
     QString text = stream.readAll();
     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.lastIndexOf('.');
     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 © 2008 Trolltech Trademarks
Qt Solutions