MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent), mGraphicsScene(0), mFontPath("fonts/"),
    mFreeTypeInitialized(false), mRendering(false),
    mLibrary(0), mFace(0), mResourceFace(0), mRawGlyphString(0),
    mShapedGlyphString(0), mRawVisualizer(0), mShapedVisualizer(0),
    mShapedRenderer(0), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("BidiRenderer");
    statusBar();

    loadSamples();
    ui->fontPathLE->setText(mFontPath);
    listFonts();

    ui->levelsColorPickerLabel->setColor(Qt::blue);
    mFontSize = ui->fontSizeSB->value();
    mPenWidth = ui->penWidthSB->value();
    ui->lineWidthSlider->setRange(mFontSize * 4, mFontSize * 40);
    ui->lineWidthSlider->setValue(mFontSize * 40);

    if (FT_Init_FreeType(&mLibrary)) {
        statusBar()->showMessage("Error initializing FreeType", messageTimeout);
        return;
    }
    mFreeTypeInitialized = true;

    QFile fontFile(":/files/fonts/DejaVuSans.ttf");
    if (fontFile.open(QIODevice::ReadOnly))
        mResourceFaceData = fontFile.readAll();

    if (FT_New_Memory_Face(mLibrary,
                           reinterpret_cast<const FT_Byte*>(mResourceFaceData.constData()),
                           fontFile.size(), 0, &mResourceFace))
    {
        statusBar()->showMessage("Error loading DejaVuSans.ttf", messageTimeout);
    }
    else
    {
        if (FT_Set_Pixel_Sizes(mResourceFace, 0, mFontSize)) {
            statusBar()->showMessage("Error setting font pixel size", messageTimeout);
            FT_Done_Face(mResourceFace);
            mResourceFace = 0;
        } else {
            mFace = mResourceFace;
        }
    }


    mGraphicsScene = new QGraphicsScene(this);
    ui->graphicsView->setScene(mGraphicsScene);

    connect(ui->actionE_xit, SIGNAL(triggered()), this, SLOT(close()));
    connect(ui->action_Panel, SIGNAL(toggled(bool)),
            ui->dockWidget, SLOT(setVisible(bool)));
    connect(ui->dockWidget, SIGNAL(visibilityChanged(bool)),
            ui->action_Panel, SLOT(setChecked(bool)));
    connect(ui->textCombo->lineEdit(), SIGNAL(returnPressed()),
            ui->renderButton, SLOT(animateClick()));
    connect(ui->renderButton, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->rtlRB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->ltrRB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->autoRB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->resolveScriptsCB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->breakRunsCB, SIGNAL(clicked()), this, SLOT(render()));

    connect(ui->linesCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->levelsCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->runsCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->codePointsCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->glyphIndicesCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->charTypesCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->scriptsCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->geometriesCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->indicesCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));
    connect(ui->reorderedIndicesCB, SIGNAL(clicked()), this, SLOT(setVisualizerFlags()));

    connect(ui->paragraphCB, SIGNAL(clicked()), this, SLOT(setRendererFlags()));
    connect(ui->levelsGB, SIGNAL(clicked()), this, SLOT(setRendererFlags()));
    connect(ui->runsGB, SIGNAL(clicked()), this, SLOT(setRendererFlags()));
    connect(ui->levelsColorPickerLabel, SIGNAL(colorChanged(QColor)),
            this, SLOT(setRendererLevelColor(QColor)));
    connect(ui->runsColorPickerLabel, SIGNAL(colorChanged(QColor)),
            this, SLOT(setRendererRunColor(QColor)));

    connect(ui->shapeHarfBuzzRB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->shapeFriBidiRB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->removeZeroWidthCB, SIGNAL(clicked()), this, SLOT(render()));
    connect(ui->shapeFriBidiRB, SIGNAL(toggled(bool)),
            ui->removeZeroWidthCB, SLOT(setEnabled(bool)));

    connect(ui->fontPathButton, SIGNAL(clicked()), this, SLOT(setFontPath()));
    connect(ui->fontsCombo, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(setFontFile(QString)));
    connect(ui->zoomSlider, SIGNAL(valueChanged(int)),
            ui->graphicsView, SLOT(setScale(int)));
    connect(ui->graphicsView, SIGNAL(scaleChanged(int)),
            ui->zoomSlider, SLOT(setValue(int)));
    connect(ui->lineWidthSlider, SIGNAL(valueChanged(int)),
            this, SLOT(setMaxLineWidth(int)));
    connect(ui->fontSizeSB, SIGNAL(valueChanged(int)),
            this, SLOT(setFontSize(int)));
    connect(ui->fontColorPickerLabel, SIGNAL(colorChanged(QColor)),
            this, SLOT(render()));
    connect(ui->penWidthSB, SIGNAL(valueChanged(int)),
            this, SLOT(setPenWidth(int)));
}
void LabelTTF::setSystemFontName(const std::string& systemFont)
{
	setFontFile(systemFont);
}