/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

/*
Tiptop modifications
Copyright (C) 2011 Leonid Nekin: nekin@narod.ru

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, version 3.

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, see <http://www.gnu.org/licenses/>.
*/

#include "textedit.h"

#include <QAction>
#include <QApplication>
#include <QClipboard>
#include <QColorDialog>
#include <QComboBox>
#include <QFontComboBox>
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QFontDatabase>
#include <QMenu>
#include <QMenuBar>
#include <QPrintDialog>
#include <QPrinter>
#include <QTextCodec>
#include <QTextEdit>
#include <QToolBar>
#include <QTextCursor>
#include <QTextDocumentWriter>
#include <QTextList>
#include <QtDebug>
#include <QCloseEvent>
#include <QMessageBox>
#include <QPrintPreviewDialog>

#ifdef Q_WS_MAC
const QString rsrcPath = ":/images/mac";
#else
const QString rsrcPath = ":/images/win";
#endif

TextEdit::TextEdit(QWidget *parent)
    : QMainWindow(parent)
{
    setToolButtonStyle(Qt::ToolButtonIconOnly);
    setupFileActions();
    setupEditActions();
    setupTextActions();
    setupModeActions();

    {
        QMenu *helpMenu = new QMenu(tr("Help"), this);
        menuBar()->addMenu(helpMenu);
        helpMenu->addAction(tr("About"), this, SLOT(about()));
        helpMenu->addAction(tr("About Qt"), qApp, SLOT(aboutQt()));
    }

    textEdit = new MyTextEdit(this);
    connect(textEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
            this, SLOT(currentCharFormatChanged(QTextCharFormat)));
    connect(textEdit, SIGNAL(cursorPositionChanged()),
            this, SLOT(cursorPositionChanged()));

    setCentralWidget(textEdit);
    textEdit->setFocus();
    setCurrentFileName(QString());

    fontChanged(textEdit->font());
    colorChanged(textEdit->textColor());
    alignmentChanged(textEdit->alignment());

    connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
            actionSave, SLOT(setEnabled(bool)));
    connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
            this, SLOT(setWindowModified(bool)));
    connect(textEdit->document(), SIGNAL(undoAvailable(bool)),
            actionUndo, SLOT(setEnabled(bool)));
    connect(textEdit->document(), SIGNAL(redoAvailable(bool)),
            actionRedo, SLOT(setEnabled(bool)));

    setWindowModified(textEdit->document()->isModified());
    actionSave->setEnabled(textEdit->document()->isModified());
    actionUndo->setEnabled(textEdit->document()->isUndoAvailable());
    actionRedo->setEnabled(textEdit->document()->isRedoAvailable());

    connect(actionUndo, SIGNAL(triggered()), textEdit, SLOT(undo()));
    connect(actionRedo, SIGNAL(triggered()), textEdit, SLOT(redo()));

    actionCut->setEnabled(false);
    actionCopy->setEnabled(false);

    connect(actionCut, SIGNAL(triggered()), textEdit, SLOT(cut()));
    connect(actionCopy, SIGNAL(triggered()), textEdit, SLOT(copy()));
    connect(actionPaste, SIGNAL(triggered()), textEdit, SLOT(paste()));

    connect(textEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
    connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));

#ifndef QT_NO_CLIPBOARD
    connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
#endif

    //QString initialFile = ":/example.html";
    QString initialFile;
    const QStringList args = QCoreApplication::arguments();
    if (args.count() == 2)
        initialFile = args.at(1);

    if (!load(initialFile))
        fileNew();
    connect(textEdit, SIGNAL(entered(const QString &)), this, SLOT(showTitle(const QString &)) );

    readConfig();
    textEdit->document()->setDefaultFont(textEdit->currentFont());
}

void TextEdit::closeEvent(QCloseEvent *e)
{
    if (maybeSave())
        e->accept();
    else
        e->ignore();
}

void TextEdit::setupFileActions()
{
    QToolBar *tb = new QToolBar(this);
    tb->setWindowTitle(tr("File Actions"));
    addToolBar(tb);

    QMenu *menu = new QMenu(tr("File"), this);
    menuBar()->addMenu(menu);

    QAction *a;

    QIcon newIcon = QIcon::fromTheme("document-new", QIcon(rsrcPath + "/filenew.png"));
    a = new QAction( newIcon, tr("New"), this);
    a->setPriority(QAction::LowPriority);
    //a->setShortcut(QKeySequence::New);
    connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
    tb->addAction(a);
    menu->addAction(a);

    a = new QAction(QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png")),
                    tr("Open..."), this);
    //a->setShortcut(QKeySequence::Open);
    connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
    tb->addAction(a);
    menu->addAction(a);

    menu->addSeparator();

    actionSave = a = new QAction(QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png")),
                                 tr("Save"), this);
    //a->setShortcut(QKeySequence::Save);
    connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
    a->setEnabled(false);
    tb->addAction(a);
    menu->addAction(a);

    a = new QAction(tr("Save As..."), this);
    a->setPriority(QAction::LowPriority);
    connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
    menu->addAction(a);
    menu->addSeparator();

#ifndef QT_NO_PRINTER
    a = new QAction(QIcon::fromTheme("document-print", QIcon(rsrcPath + "/fileprint.png")),
                    tr("Print..."), this);
    a->setPriority(QAction::LowPriority);    
    //a->setShortcut(QKeySequence::Print);
    connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
    tb->addAction(a);
    menu->addAction(a);

    a = new QAction(QIcon::fromTheme("fileprint", QIcon(rsrcPath + "/fileprint.png")),
                    tr("Print Preview..."), this);
    connect(a, SIGNAL(triggered()), this, SLOT(filePrintPreview()));
    menu->addAction(a);

    a = new QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")),
    tr("Export PDF..."), this);
    a->setPriority(QAction::LowPriority);
    //a->setShortcut(Qt::CTRL + Qt::Key_D);
    connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf()));
    tb->addAction(a);
    menu->addAction(a);

    menu->addSeparator();
#endif

    a = new QAction(tr("Quit"), this);
    //a->setShortcut(Qt::CTRL + Qt::Key_Q);
    connect(a, SIGNAL(triggered()), this, SLOT(close()));
    menu->addAction(a);
}

void TextEdit::setupEditActions()
{
    QToolBar *tb = new QToolBar(this);
    tb->setWindowTitle(tr("Edit Actions"));
    addToolBar(tb);
    QMenu *menu = new QMenu(tr("Edit"), this);
    menuBar()->addMenu(menu);

    QAction *a;
    a = actionUndo = new QAction(QIcon::fromTheme("edit-undo", QIcon(rsrcPath + "/editundo.png")),
                                              tr("Undo"), this);
    //a->setShortcut(QKeySequence::Undo);
    tb->addAction(a);
    menu->addAction(a);
    a = actionRedo = new QAction(QIcon::fromTheme("edit-redo", QIcon(rsrcPath + "/editredo.png")),
                                              tr("Redo"), this);
    a->setPriority(QAction::LowPriority);
    //a->setShortcut(QKeySequence::Redo);
    tb->addAction(a);
    menu->addAction(a);
    menu->addSeparator();
    a = actionCut = new QAction(QIcon::fromTheme("edit-cut", QIcon(rsrcPath + "/editcut.png")),
                                             tr("Cut"), this);
    a->setPriority(QAction::LowPriority);
    a->setShortcut(QKeySequence::Cut);
    tb->addAction(a);
    menu->addAction(a);
    a = actionCopy = new QAction(QIcon::fromTheme("edit-copy", QIcon(rsrcPath + "/editcopy.png")),
                                 tr("Copy"), this);
    a->setPriority(QAction::LowPriority);
    a->setShortcut(QKeySequence::Copy);
    tb->addAction(a);
    menu->addAction(a);
    a = actionPaste = new QAction(QIcon::fromTheme("edit-paste", QIcon(rsrcPath + "/editpaste.png")),
                                  tr("Paste"), this);
    a->setPriority(QAction::LowPriority);
    a->setShortcut(QKeySequence::Paste);
    tb->addAction(a);
    menu->addAction(a);
#ifndef QT_NO_CLIPBOARD
    actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
#endif
}

void TextEdit::setupTextActions()
{
    QToolBar *tb = new QToolBar(this);
    tb->setWindowTitle(tr("Format Actions"));
    addToolBar(tb);

    QMenu *menu = new QMenu(tr("Format"), this);
    menuBar()->addMenu(menu);

    actionTextBold = new QAction(QIcon::fromTheme("format-text-bold", QIcon(rsrcPath + "/textbold.png")),
                                 tr("Bold"), this);
    //actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
    actionTextBold->setPriority(QAction::LowPriority);
	QFont bold;
    bold.setBold(true);
    actionTextBold->setFont(bold);
    connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
    tb->addAction(actionTextBold);
    menu->addAction(actionTextBold);
    actionTextBold->setCheckable(true);

    actionTextItalic = new QAction(QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png")),
                                   tr("Italic"), this);
    actionTextItalic->setPriority(QAction::LowPriority);
    //actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
    QFont italic;
    italic.setItalic(true);
    actionTextItalic->setFont(italic);
    connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
    tb->addAction(actionTextItalic);
    menu->addAction(actionTextItalic);
    actionTextItalic->setCheckable(true);

    actionTextUnderline = new QAction(QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png")),
                                      tr("Underline"), this);
    //actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
    actionTextUnderline->setPriority(QAction::LowPriority);
    QFont underline;
    underline.setUnderline(true);
    actionTextUnderline->setFont(underline);
    connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
    tb->addAction(actionTextUnderline);
    menu->addAction(actionTextUnderline);
    actionTextUnderline->setCheckable(true);

    menu->addSeparator();

    QActionGroup *grp = new QActionGroup(this);
    connect(grp, SIGNAL(triggered(QAction*)), this, SLOT(textAlign(QAction*)));

    // Make sure the alignLeft  is always left of the alignRight
    if (QApplication::isLeftToRight()) {
        actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")),
                                      tr("Left"), grp);
        actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("Center"), grp);
        actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("Right"), grp);
    } else {
        actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("Right"), grp);
        actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("Center"), grp);
        actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("Left"), grp);
    }
    actionAlignJustify = new QAction(QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png")), tr("Justify"), grp);

    //actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
    actionAlignLeft->setCheckable(true);
    actionAlignLeft->setPriority(QAction::LowPriority);
    //actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
    actionAlignCenter->setCheckable(true);
    actionAlignCenter->setPriority(QAction::LowPriority);
    //actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
    actionAlignRight->setCheckable(true);
    actionAlignRight->setPriority(QAction::LowPriority);
    //actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
    actionAlignJustify->setCheckable(true);
    actionAlignJustify->setPriority(QAction::LowPriority);

    tb->addActions(grp->actions());
    menu->addActions(grp->actions());

    menu->addSeparator();

    QPixmap pix(16, 16);
    pix.fill(Qt::black);
    actionTextColor = new QAction(pix, tr("Ink color..."), this);
    connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
    //tb->addAction(actionTextColor);
    menu->addAction(actionTextColor);

    pix.fill(Qt::white);
    actionEraserColor = new QAction(pix, tr("Eraser color..."), this);
    connect(actionEraserColor, SIGNAL(triggered()), this, SLOT(eraserColor()));
    menu->addAction(actionEraserColor);

    pix.fill(Qt::white);
    actionBackground = new QAction(pix, tr("Background..."), this);
    connect(actionBackground, SIGNAL(triggered()), this, SLOT(background()));
    menu->addAction(actionBackground);

    tb = new QToolBar(this);
    tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
    tb->setWindowTitle(tr("Format Actions"));
    addToolBarBreak(Qt::TopToolBarArea);
    addToolBar(tb);
/*
    comboStyle = new QComboBox(tb);
    tb->addWidget(comboStyle);
    comboStyle->addItem("Standard");
    comboStyle->addItem("Bullet List (Disc)");
    comboStyle->addItem("Bullet List (Circle)");
    comboStyle->addItem("Bullet List (Square)");
    comboStyle->addItem("Ordered List (Decimal)");
    comboStyle->addItem("Ordered List (Alpha lower)");
    comboStyle->addItem("Ordered List (Alpha upper)");
    comboStyle->addItem("Ordered List (Roman lower)");
    comboStyle->addItem("Ordered List (Roman upper)");
    connect(comboStyle, SIGNAL(activated(int)),
            this, SLOT(textStyle(int)));
*/
    comboFont = new QFontComboBox(tb);
    tb->addWidget(comboFont);
    connect(comboFont, SIGNAL(activated(QString)),
            this, SLOT(textFamily(QString)));

    comboSize = new QComboBox(tb);
    comboSize->setObjectName("comboSize");
    tb->addWidget(comboSize);
    comboSize->setEditable(true);

    QFontDatabase db;
    foreach(int size, db.standardSizes())
        comboSize->addItem(QString::number(size));

    connect(comboSize, SIGNAL(activated(QString)),
            this, SLOT(textSize(QString)));
    comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
                                                                   .pointSize())));
}

void TextEdit::setupModeActions()
{
    QToolBar *tb = new QToolBar(this);
    tb->setWindowTitle(tr("Mode Actions"));
    addToolBar(tb);

    actionSwitch = new QAction(tr("Switch mode <Esc>"), this);
    actionSwitch->setShortcut(Qt::Key_Escape);
    connect(actionSwitch, SIGNAL(triggered()), this, SLOT(switching()));
    tb->addAction(actionSwitch);

    QMenu *menu = new QMenu(tr("Mode"), this);
    menuBar()->addMenu(menu);

    actionEditing = new QAction(tr("Editing    <Esc>"), this);
    connect(actionEditing, SIGNAL(triggered()), this, SLOT(editing()));
    menu->addAction(actionEditing);

    actionTraining = new QAction(tr("Training  <Esc>"), this);
    connect(actionTraining, SIGNAL(triggered()), this, SLOT(training()));
    menu->addAction(actionTraining);

    actionEditing->setEnabled(false);

    menu->addSeparator();

    actionSoundOn = new QAction(tr("Sound On"), this);
    connect(actionSoundOn, SIGNAL(triggered()), this, SLOT(soundOn()));
    menu->addAction(actionSoundOn);

    actionSoundOff = new QAction(tr("Sound Off"), this);
    connect(actionSoundOff, SIGNAL(triggered()), this, SLOT(soundOff()));
    menu->addAction(actionSoundOff);

    actionSoundOn->setEnabled(false);
}

bool TextEdit::load(const QString &f)
{
    if (!QFile::exists(f))
        return false;
    QFile file(f);
    if (!file.open(QFile::ReadOnly))
        return false;

    QByteArray data = file.readAll();
    if (f.endsWith(".htm", Qt::CaseInsensitive)
        || f.endsWith(".html", Qt::CaseInsensitive) ) {
      QTextCodec *codec = Qt::codecForHtml(data);
      QString str = codec->toUnicode(data);
      textEdit->setHtml(str);
    }
    else {
      QTextCodec *codec = QTextCodec::codecForName("UTF-8");
      QString str = codec->toUnicode(data);
      textEdit->setPlainText(str);
    }

    //if (Qt::mightBeRichText(str)) {
    //    textEdit->setHtml(str);
    //} else {
    //    str = QString::fromLocal8Bit(data);
    //    textEdit->setPlainText(str);
    //}

    setCurrentFileName(f);
    textEdit->setMode(false);
    return true;
}

bool TextEdit::maybeSave()
{
    if (!textEdit->document()->isModified()) {
        return true;
    }	
    //if (fileName.startsWith(QLatin1String(":/"))) {
    //    return true;
    //}	
    QMessageBox::StandardButton ret;
    ret = QMessageBox::warning(this, tr("Application"),
                               tr("The document has been modified.\n"
                                  "Do you want to save your changes?"),
                               QMessageBox::Save | QMessageBox::Discard
                               | QMessageBox::Cancel);
    if (ret == QMessageBox::Save)
        return fileSave();
    else if (ret == QMessageBox::Cancel)
        return false;
    return true;
}

void TextEdit::setCurrentFileName(const QString &fileName)
{
    this->fileName = fileName;
    textEdit->document()->setModified(false);

    //QString shownName;
    //if (fileName.isEmpty())
    //    shownName = "untitled.txt";
    //else
    //    shownName = QFileInfo(fileName).fileName();

    //setWindowTitle(tr("Tiptop: %1[*]").arg(shownName));

    showTitle();
    setWindowModified(false);
}

void TextEdit::fileNew()
{
    if (maybeSave()) {
        textEdit->clear();
        setCurrentFileName(QString());
        textEdit->setMode(false);
    }
}

void TextEdit::fileOpen()
{
    QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
                                              QString(), tr("Plaintext files UTF-8 (*.txt);;HTML-Files (*.htm *.html);;All Files (*)"));
    if (!fn.isEmpty())
        load(fn);
}

bool TextEdit::fileSave()
{
    if (fileName.isEmpty())
        return fileSaveAs();
    QByteArray format;
    if ( fileName.endsWith(".htm", Qt::CaseInsensitive)
      || fileName.endsWith(".html", Qt::CaseInsensitive) )
      format = "HTML";
    else format = "plaintext";

    QTextDocumentWriter writer(fileName, format);

    //QList<QByteArray> ba = writer.supportedDocumentFormats();
    //for (int i = 0; i < ba.size(); ++i)
    //  std::cout << QString(ba[i]).toStdString() << " ";
    //std::cout << std::endl;

    bool success = writer.write(textEdit->document());
    if (success)
        textEdit->document()->setModified(false);
    return success;
}

bool TextEdit::fileSaveAs()
{
    QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
        QString(), tr("Plaintext files UTF-8 (*.txt);;HTML-Files (*.htm *.html);;All Files (*)"));

    if (fn.isEmpty())
        return false;
    //if (! (fn.endsWith(".htm", Qt::CaseInsensitive)
    //    || fn.endsWith(".html", Qt::CaseInsensitive)
    //    || fn.endsWith(".txt", Qt::CaseInsensitive)
    //    ) )
    //    fn += ".txt"; // default
    setCurrentFileName(fn);
    return fileSave();
}

void TextEdit::filePrint()
{
#ifndef QT_NO_PRINTER
    QPrinter printer(QPrinter::HighResolution);
    QPrintDialog *dlg = new QPrintDialog(&printer, this);
    if (textEdit->textCursor().hasSelection())
        dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
    dlg->setWindowTitle(tr("Print Document"));
    if (dlg->exec() == QDialog::Accepted) {
        textEdit->print(&printer);
    }
    delete dlg;
#endif
}

void TextEdit::filePrintPreview()
{
#ifndef QT_NO_PRINTER
    QPrinter printer(QPrinter::HighResolution);
    QPrintPreviewDialog preview(&printer, this);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printPreview(QPrinter*)));
    preview.exec();
#endif
}

void TextEdit::printPreview(QPrinter *printer)
{
#ifdef QT_NO_PRINTER
    Q_UNUSED(printer);
#else
    textEdit->print(printer);
#endif
}


void TextEdit::filePrintPdf()
{
#ifndef QT_NO_PRINTER
//! [0]
    QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
                                                    QString(), "*.pdf");
    if (!fileName.isEmpty()) {
        if (QFileInfo(fileName).suffix().isEmpty())
            fileName.append(".pdf");
        QPrinter printer(QPrinter::HighResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setOutputFileName(fileName);
        textEdit->document()->print(&printer);
    }
//! [0]
#endif
}

void TextEdit::textBold()
{
    QTextCharFormat fmt;
    fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
    mergeFormatOnWordOrSelection(fmt);
    fontB = actionTextBold->isChecked();
}

void TextEdit::textUnderline()
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(actionTextUnderline->isChecked());
    mergeFormatOnWordOrSelection(fmt);
    fontU = actionTextUnderline->isChecked();
}

void TextEdit::textItalic()
{
    QTextCharFormat fmt;
    fmt.setFontItalic(actionTextItalic->isChecked());
    mergeFormatOnWordOrSelection(fmt);
    fontI = actionTextItalic->isChecked();
}

void TextEdit::textFamily(const QString &f)
{
    QTextCharFormat fmt;
    fmt.setFontFamily(f);
    mergeFormatOnWordOrSelection(fmt);
    fontFam = f;
}

void TextEdit::textSize(const QString &p)
{
    qreal pointSize = p.toFloat();
    if (p.toFloat() > 0) {
        QTextCharFormat fmt;
        fmt.setFontPointSize(pointSize);
        mergeFormatOnWordOrSelection(fmt);
        fontSize = p;
    }
}
/*
void TextEdit::textStyle(int styleIndex)
{
    QTextCursor cursor = textEdit->textCursor();

    if (styleIndex != 0) {
        QTextListFormat::Style style = QTextListFormat::ListDisc;

        switch (styleIndex) {
            default:
            case 1:
                style = QTextListFormat::ListDisc;
                break;
            case 2:
                style = QTextListFormat::ListCircle;
                break;
            case 3:
                style = QTextListFormat::ListSquare;
                break;
            case 4:
                style = QTextListFormat::ListDecimal;
                break;
            case 5:
                style = QTextListFormat::ListLowerAlpha;
                break;
            case 6:
                style = QTextListFormat::ListUpperAlpha;
                break;
            case 7:
                style = QTextListFormat::ListLowerRoman;
                break;
            case 8:
                style = QTextListFormat::ListUpperRoman;
                break;
        }

        cursor.beginEditBlock();

        QTextBlockFormat blockFmt = cursor.blockFormat();

        QTextListFormat listFmt;

        if (cursor.currentList()) {
            listFmt = cursor.currentList()->format();
        } else {
            listFmt.setIndent(blockFmt.indent() + 1);
            blockFmt.setIndent(0);
            cursor.setBlockFormat(blockFmt);
        }

        listFmt.setStyle(style);

        cursor.createList(listFmt);

        cursor.endEditBlock();
    } else {
        // ####
        QTextBlockFormat bfmt;
        bfmt.setObjectIndex(0);
        cursor.mergeBlockFormat(bfmt);
    }
}
*/
void TextEdit::textColor(QColor col)
{
    if (!col.isValid()) col = QColorDialog::getColor(textEdit->getEditColor(), this);
    if (!col.isValid()) return;
    QTextCharFormat fmt;
    fmt.setForeground(col);
    mergeFormatOnWordOrSelection(fmt);
    textEdit->setEditColor(col);

    QPalette p = textEdit->palette();
    p.setColor(QPalette::Text, col);
    textEdit->setPalette(p);

    QPixmap pix(16, 16);
    pix.fill(col);
    actionTextColor->setIcon(pix);

    //colorChanged(col);
}

void TextEdit::eraserColor(QColor col)
{
    if (!col.isValid()) col = QColorDialog::getColor(textEdit->getEraserColor(), this);
    if (!col.isValid()) return;
    textEdit->setEraserColor(col);

    QPixmap pix(16, 16);
    pix.fill(col);
    actionEraserColor->setIcon(pix);
}

void TextEdit::background(QColor col)
{
    if (!col.isValid()) col = QColorDialog::getColor(bgColor, this);
    if (!col.isValid()) return;
    QPalette p = textEdit->palette();
    p.setColor(QPalette::Base, col);
    //p.setColor(static_cast<QPalette::ColorRole>(9), col);
    textEdit->setPalette(p);

    QPixmap pix(16, 16);
    pix.fill(col);
    actionBackground->setIcon(pix);

    bgColor = col;
}

void TextEdit::editing()
{
    textEdit->setMode(false);
    actionEditing->setEnabled(false);
    actionTraining->setEnabled(true);
}

void TextEdit::training()
{
    textEdit->setMode(true);
    actionEditing->setEnabled(true);
    actionTraining->setEnabled(false);
}

void TextEdit::soundOn()
{
    textEdit->setSound(true);
    actionSoundOn->setEnabled(false);
    actionSoundOff->setEnabled(true);
}

void TextEdit::soundOff()
{
    textEdit->setSound(false);
    actionSoundOn->setEnabled(true);
    actionSoundOff->setEnabled(false);
}

void TextEdit::switching()
{
    if (textEdit->getMode()) editing();
    else training();
}

void TextEdit::textAlign(QAction *a)
{
    if (a == actionAlignLeft)
        textEdit->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
    else if (a == actionAlignCenter)
        textEdit->setAlignment(Qt::AlignHCenter);
    else if (a == actionAlignRight)
        textEdit->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
    else if (a == actionAlignJustify)
        textEdit->setAlignment(Qt::AlignJustify);
}

void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
{
    fontChanged(format.font());
    colorChanged(format.foreground().color());
}

void TextEdit::cursorPositionChanged()
{
    alignmentChanged(textEdit->alignment());
}

void TextEdit::clipboardDataChanged()
{
#ifndef QT_NO_CLIPBOARD
    actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
#endif
}

void TextEdit::about()
{
    QMessageBox::about(this, tr("About"), tr("\nTiptop. Version 1.1. \nCopyright (c) 2011 Leonid Nekin."));
}

void TextEdit::showTitle(const QString & s)
{
    QString shownName = fileName.isEmpty() ? "untitled.txt" : QFileInfo(fileName).fileName();
    QString mode;
    if (textEdit->getMode()) {
      mode = tr("Training") + " (";
      QString n;
      n.setNum(textEdit->getNTyped());
      mode = mode + n + " + ";
      n.setNum(textEdit->getNAddedTyped());
      mode = mode + n + ")/(";
      n.setNum(textEdit->getNTotal());
      mode = mode + n + " + ";
      n.setNum(textEdit->getNAdded());
      mode = mode + n + ")";
    }
    else mode = tr("Editing");

    setWindowTitle(QString("Tiptop - %1[*] - %2 - %3").arg(shownName).arg(mode).arg(s));
}  

void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
    QTextCursor cursor = textEdit->textCursor();
    if (!cursor.hasSelection())
        cursor.select(QTextCursor::WordUnderCursor);
    cursor.mergeCharFormat(format);
    textEdit->mergeCurrentCharFormat(format);
}

void TextEdit::fontChanged(const QFont &f)
{
    comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
    comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
    actionTextBold->setChecked(f.bold());
    actionTextItalic->setChecked(f.italic());
    actionTextUnderline->setChecked(f.underline());
}

void TextEdit::colorChanged(const QColor &/*c*/)
{
    //QPixmap pix(16, 16);
    //pix.fill(c);
    //actionTextColor->setIcon(pix);
}

void TextEdit::alignmentChanged(Qt::Alignment a)
{
    if (a & Qt::AlignLeft) {
        actionAlignLeft->setChecked(true);
    } else if (a & Qt::AlignHCenter) {
        actionAlignCenter->setChecked(true);
    } else if (a & Qt::AlignRight) {
        actionAlignRight->setChecked(true);
    } else if (a & Qt::AlignJustify) {
        actionAlignJustify->setChecked(true);
    }
}

void TextEdit::readConfig()
{
    const QStringList args = QCoreApplication::arguments();
    //QFile configFile(args.at(0) + ".config");
    QFile configFile(QFileInfo(args.at(0)).canonicalPath() + "/tiptop.config");
    if (configFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
      QTextStream in(&configFile);

      QString line;
      line = in.readLine();
      textFamily(line);

      line = in.readLine();
      textSize(line);

      int state;
      in >> state;
      actionTextBold->setChecked(state);
      textBold();
      in >> state;
      actionTextUnderline->setChecked(state);
      textUnderline();
      in >> state;
      actionTextItalic->setChecked(state);
      textItalic();

      int r, g, b;
      in >> r >> g >> b;
      textColor(QColor(r,g,b));
      in >> r >> g >> b;
      eraserColor(QColor(r,g,b));
      in >> r >> g >> b;
      background(QColor(r,g,b));

      int w, h;
      in >> w >> h;
      resize(w, h);

      in >> state;
      if (state) soundOn(); else soundOff();

      int ctrl;
      in >> ctrl;
      configFile.close();

      if (ctrl == 1234567) return;
    }

    textFamily("Courier New");
    textSize("28");
    actionTextBold->setChecked(false);
    textBold();
    actionTextUnderline->setChecked(false);
    textUnderline();
    actionTextItalic->setChecked(false);
    textItalic();
    textColor(Qt::black);
    eraserColor(Qt::white);
    background(Qt::white);
    soundOn();
    resize(700,800);
}

void TextEdit::writeConfig()
{
    const QStringList args = QCoreApplication::arguments();
    QFile configFile(QFileInfo(args.at(0)).canonicalPath() + "/tiptop.config");
    if (!configFile.open(QIODevice::WriteOnly | QIODevice::Text)) return;
    QTextStream out(&configFile);

    out << fontFam << "\n";
    out << fontSize << "\n";
    out << fontB << "\n";
    out << fontU << "\n";
    out << fontI << "\n";
    QColor col = textEdit->getEditColor();
    out << col.red() << " " << col.green() << " " << col.blue() << "\n";
    col = textEdit->getEraserColor();
    out << col.red() << " " << col.green() << " " << col.blue() << "\n";
    col = bgColor;
    out << col.red() << " " << col.green() << " " << col.blue() << "\n";
    out << size().width() << " " << size().height() << "\n";
    out << textEdit->getSound() << "\n";
    out << 1234567 << "\n";
    configFile.close();
}

