demos/textedit/textedit.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the demonstration applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "textedit.h"
       
    43 
       
    44 #include <QAction>
       
    45 #include <QApplication>
       
    46 #include <QClipboard>
       
    47 #include <QColorDialog>
       
    48 #include <QComboBox>
       
    49 #include <QFontComboBox>
       
    50 #include <QFile>
       
    51 #include <QFileDialog>
       
    52 #include <QFileInfo>
       
    53 #include <QFontDatabase>
       
    54 #include <QMenu>
       
    55 #include <QMenuBar>
       
    56 #include <QPrintDialog>
       
    57 #include <QPrinter>
       
    58 #include <QTextCodec>
       
    59 #include <QTextEdit>
       
    60 #include <QToolBar>
       
    61 #include <QTextCursor>
       
    62 #include <QTextDocumentWriter>
       
    63 #include <QTextList>
       
    64 #include <QtDebug>
       
    65 #include <QCloseEvent>
       
    66 #include <QMessageBox>
       
    67 #include <QPrintPreviewDialog>
       
    68 
       
    69 #ifdef Q_WS_MAC
       
    70 const QString rsrcPath = ":/images/mac";
       
    71 #else
       
    72 const QString rsrcPath = ":/images/win";
       
    73 #endif
       
    74 
       
    75 TextEdit::TextEdit(QWidget *parent)
       
    76     : QMainWindow(parent)
       
    77 {
       
    78     setToolButtonStyle(Qt::ToolButtonFollowStyle);
       
    79     setupFileActions();
       
    80     setupEditActions();
       
    81     setupTextActions();
       
    82 
       
    83     {
       
    84         QMenu *helpMenu = new QMenu(tr("Help"), this);
       
    85         menuBar()->addMenu(helpMenu);
       
    86         helpMenu->addAction(tr("About"), this, SLOT(about()));
       
    87         helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt()));
       
    88     }
       
    89 
       
    90     textEdit = new QTextEdit(this);
       
    91     connect(textEdit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &)),
       
    92             this, SLOT(currentCharFormatChanged(const QTextCharFormat &)));
       
    93     connect(textEdit, SIGNAL(cursorPositionChanged()),
       
    94             this, SLOT(cursorPositionChanged()));
       
    95 
       
    96     setCentralWidget(textEdit);
       
    97     textEdit->setFocus();
       
    98     setCurrentFileName(QString());
       
    99 
       
   100     fontChanged(textEdit->font());
       
   101     colorChanged(textEdit->textColor());
       
   102     alignmentChanged(textEdit->alignment());
       
   103 
       
   104     connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
       
   105             actionSave, SLOT(setEnabled(bool)));
       
   106     connect(textEdit->document(), SIGNAL(modificationChanged(bool)),
       
   107             this, SLOT(setWindowModified(bool)));
       
   108     connect(textEdit->document(), SIGNAL(undoAvailable(bool)),
       
   109             actionUndo, SLOT(setEnabled(bool)));
       
   110     connect(textEdit->document(), SIGNAL(redoAvailable(bool)),
       
   111             actionRedo, SLOT(setEnabled(bool)));
       
   112 
       
   113     setWindowModified(textEdit->document()->isModified());
       
   114     actionSave->setEnabled(textEdit->document()->isModified());
       
   115     actionUndo->setEnabled(textEdit->document()->isUndoAvailable());
       
   116     actionRedo->setEnabled(textEdit->document()->isRedoAvailable());
       
   117 
       
   118     connect(actionUndo, SIGNAL(triggered()), textEdit, SLOT(undo()));
       
   119     connect(actionRedo, SIGNAL(triggered()), textEdit, SLOT(redo()));
       
   120 
       
   121     actionCut->setEnabled(false);
       
   122     actionCopy->setEnabled(false);
       
   123 
       
   124     connect(actionCut, SIGNAL(triggered()), textEdit, SLOT(cut()));
       
   125     connect(actionCopy, SIGNAL(triggered()), textEdit, SLOT(copy()));
       
   126     connect(actionPaste, SIGNAL(triggered()), textEdit, SLOT(paste()));
       
   127 
       
   128     connect(textEdit, SIGNAL(copyAvailable(bool)), actionCut, SLOT(setEnabled(bool)));
       
   129     connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));
       
   130 
       
   131 #ifndef QT_NO_CLIPBOARD
       
   132     connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
       
   133 #endif
       
   134 
       
   135     QString initialFile = ":/example.html";
       
   136     const QStringList args = QCoreApplication::arguments();
       
   137     if (args.count() == 2)
       
   138         initialFile = args.at(1);
       
   139 
       
   140     if (!load(initialFile))
       
   141         fileNew();
       
   142 }
       
   143 
       
   144 void TextEdit::closeEvent(QCloseEvent *e)
       
   145 {
       
   146     if (maybeSave())
       
   147         e->accept();
       
   148     else
       
   149         e->ignore();
       
   150 }
       
   151 
       
   152 void TextEdit::setupFileActions()
       
   153 {
       
   154     QToolBar *tb = new QToolBar(this);
       
   155     tb->setWindowTitle(tr("File Actions"));
       
   156     addToolBar(tb);
       
   157 
       
   158     QMenu *menu = new QMenu(tr("&File"), this);
       
   159     menuBar()->addMenu(menu);
       
   160 
       
   161     QAction *a;
       
   162 
       
   163     QIcon newIcon = QIcon::fromTheme("document-new", QIcon(rsrcPath + "/filenew.png"));
       
   164     a = new QAction( newIcon, tr("&New"), this);
       
   165     a->setPriority(QAction::LowPriority);
       
   166     a->setShortcut(QKeySequence::New);
       
   167     connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
       
   168     tb->addAction(a);
       
   169     menu->addAction(a);
       
   170 
       
   171     a = new QAction(QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png")),
       
   172                     tr("&Open..."), this);
       
   173     a->setShortcut(QKeySequence::Open);
       
   174     connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
       
   175     tb->addAction(a);
       
   176     menu->addAction(a);
       
   177 
       
   178     menu->addSeparator();
       
   179 
       
   180     actionSave = a = new QAction(QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png")),
       
   181                                  tr("&Save"), this);
       
   182     a->setShortcut(QKeySequence::Save);
       
   183     connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
       
   184     a->setEnabled(false);
       
   185     tb->addAction(a);
       
   186     menu->addAction(a);
       
   187 
       
   188     a = new QAction(tr("Save &As..."), this);
       
   189     a->setPriority(QAction::LowPriority);
       
   190     connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
       
   191     menu->addAction(a);
       
   192     menu->addSeparator();
       
   193 
       
   194 #ifndef QT_NO_PRINTER
       
   195     a = new QAction(QIcon::fromTheme("document-print", QIcon(rsrcPath + "/fileprint.png")),
       
   196                     tr("&Print..."), this);
       
   197     a->setPriority(QAction::LowPriority);    
       
   198     a->setShortcut(QKeySequence::Print);
       
   199     connect(a, SIGNAL(triggered()), this, SLOT(filePrint()));
       
   200     tb->addAction(a);
       
   201     menu->addAction(a);
       
   202 
       
   203     a = new QAction(QIcon::fromTheme("fileprint", QIcon(rsrcPath + "/fileprint.png")),
       
   204                     tr("Print Preview..."), this);
       
   205     connect(a, SIGNAL(triggered()), this, SLOT(filePrintPreview()));
       
   206     menu->addAction(a);
       
   207 
       
   208     a = new QAction(QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png")),
       
   209     tr("&Export PDF..."), this);
       
   210     a->setPriority(QAction::LowPriority);
       
   211     a->setShortcut(Qt::CTRL + Qt::Key_D);
       
   212     connect(a, SIGNAL(triggered()), this, SLOT(filePrintPdf()));
       
   213     tb->addAction(a);
       
   214     menu->addAction(a);
       
   215 
       
   216     menu->addSeparator();
       
   217 #endif
       
   218 
       
   219     a = new QAction(tr("&Quit"), this);
       
   220     a->setShortcut(Qt::CTRL + Qt::Key_Q);
       
   221     connect(a, SIGNAL(triggered()), this, SLOT(close()));
       
   222     menu->addAction(a);
       
   223 }
       
   224 
       
   225 void TextEdit::setupEditActions()
       
   226 {
       
   227     QToolBar *tb = new QToolBar(this);
       
   228     tb->setWindowTitle(tr("Edit Actions"));
       
   229     addToolBar(tb);
       
   230     QMenu *menu = new QMenu(tr("&Edit"), this);
       
   231     menuBar()->addMenu(menu);
       
   232 
       
   233     QAction *a;
       
   234     a = actionUndo = new QAction(QIcon::fromTheme("edit-undo", QIcon(rsrcPath + "/editundo.png")),
       
   235                                               tr("&Undo"), this);
       
   236     a->setShortcut(QKeySequence::Undo);
       
   237     tb->addAction(a);
       
   238     menu->addAction(a);
       
   239     a = actionRedo = new QAction(QIcon::fromTheme("edit-redo", QIcon(rsrcPath + "/editredo.png")),
       
   240                                               tr("&Redo"), this);
       
   241     a->setPriority(QAction::LowPriority);
       
   242     a->setShortcut(QKeySequence::Redo);
       
   243     tb->addAction(a);
       
   244     menu->addAction(a);
       
   245     menu->addSeparator();
       
   246     a = actionCut = new QAction(QIcon::fromTheme("edit-cut", QIcon(rsrcPath + "/editcut.png")),
       
   247                                              tr("Cu&t"), this);
       
   248     a->setPriority(QAction::LowPriority);
       
   249     a->setShortcut(QKeySequence::Cut);
       
   250     tb->addAction(a);
       
   251     menu->addAction(a);
       
   252     a = actionCopy = new QAction(QIcon::fromTheme("edit-copy", QIcon(rsrcPath + "/editcopy.png")),
       
   253                                  tr("&Copy"), this);
       
   254     a->setPriority(QAction::LowPriority);
       
   255     a->setShortcut(QKeySequence::Copy);
       
   256     tb->addAction(a);
       
   257     menu->addAction(a);
       
   258     a = actionPaste = new QAction(QIcon::fromTheme("edit-paste", QIcon(rsrcPath + "/editpaste.png")),
       
   259                                   tr("&Paste"), this);
       
   260     a->setPriority(QAction::LowPriority);
       
   261     a->setShortcut(QKeySequence::Paste);
       
   262     tb->addAction(a);
       
   263     menu->addAction(a);
       
   264 #ifndef QT_NO_CLIPBOARD
       
   265     actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
       
   266 #endif
       
   267 }
       
   268 
       
   269 void TextEdit::setupTextActions()
       
   270 {
       
   271     QToolBar *tb = new QToolBar(this);
       
   272     tb->setWindowTitle(tr("Format Actions"));
       
   273     addToolBar(tb);
       
   274 
       
   275     QMenu *menu = new QMenu(tr("F&ormat"), this);
       
   276     menuBar()->addMenu(menu);
       
   277 
       
   278     actionTextBold = new QAction(QIcon::fromTheme("format-text-bold", QIcon(rsrcPath + "/textbold.png")),
       
   279                                  tr("&Bold"), this);
       
   280     actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
       
   281     actionTextBold->setPriority(QAction::LowPriority);
       
   282 	QFont bold;
       
   283     bold.setBold(true);
       
   284     actionTextBold->setFont(bold);
       
   285     connect(actionTextBold, SIGNAL(triggered()), this, SLOT(textBold()));
       
   286     tb->addAction(actionTextBold);
       
   287     menu->addAction(actionTextBold);
       
   288     actionTextBold->setCheckable(true);
       
   289 
       
   290     actionTextItalic = new QAction(QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png")),
       
   291                                    tr("&Italic"), this);
       
   292     actionTextItalic->setPriority(QAction::LowPriority);
       
   293     actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
       
   294     QFont italic;
       
   295     italic.setItalic(true);
       
   296     actionTextItalic->setFont(italic);
       
   297     connect(actionTextItalic, SIGNAL(triggered()), this, SLOT(textItalic()));
       
   298     tb->addAction(actionTextItalic);
       
   299     menu->addAction(actionTextItalic);
       
   300     actionTextItalic->setCheckable(true);
       
   301 
       
   302     actionTextUnderline = new QAction(QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png")),
       
   303                                       tr("&Underline"), this);
       
   304     actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
       
   305     actionTextUnderline->setPriority(QAction::LowPriority);
       
   306     QFont underline;
       
   307     underline.setUnderline(true);
       
   308     actionTextUnderline->setFont(underline);
       
   309     connect(actionTextUnderline, SIGNAL(triggered()), this, SLOT(textUnderline()));
       
   310     tb->addAction(actionTextUnderline);
       
   311     menu->addAction(actionTextUnderline);
       
   312     actionTextUnderline->setCheckable(true);
       
   313 
       
   314     menu->addSeparator();
       
   315 
       
   316     QActionGroup *grp = new QActionGroup(this);
       
   317     connect(grp, SIGNAL(triggered(QAction *)), this, SLOT(textAlign(QAction *)));
       
   318 
       
   319     // Make sure the alignLeft  is always left of the alignRight
       
   320     if (QApplication::isLeftToRight()) {
       
   321         actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")),
       
   322                                       tr("&Left"), grp);
       
   323         actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp);
       
   324         actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp);
       
   325     } else {
       
   326         actionAlignRight = new QAction(QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png")), tr("&Right"), grp);
       
   327         actionAlignCenter = new QAction(QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png")), tr("C&enter"), grp);
       
   328         actionAlignLeft = new QAction(QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png")), tr("&Left"), grp);
       
   329     }
       
   330     actionAlignJustify = new QAction(QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png")), tr("&Justify"), grp);
       
   331 
       
   332     actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
       
   333     actionAlignLeft->setCheckable(true);
       
   334     actionAlignLeft->setPriority(QAction::LowPriority);
       
   335     actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
       
   336     actionAlignCenter->setCheckable(true);
       
   337     actionAlignCenter->setPriority(QAction::LowPriority);
       
   338     actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
       
   339     actionAlignRight->setCheckable(true);
       
   340     actionAlignRight->setPriority(QAction::LowPriority);
       
   341     actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
       
   342     actionAlignJustify->setCheckable(true);
       
   343     actionAlignJustify->setPriority(QAction::LowPriority);
       
   344 
       
   345     tb->addActions(grp->actions());
       
   346     menu->addActions(grp->actions());
       
   347 
       
   348     menu->addSeparator();
       
   349 
       
   350     QPixmap pix(16, 16);
       
   351     pix.fill(Qt::black);
       
   352     actionTextColor = new QAction(pix, tr("&Color..."), this);
       
   353     connect(actionTextColor, SIGNAL(triggered()), this, SLOT(textColor()));
       
   354     tb->addAction(actionTextColor);
       
   355     menu->addAction(actionTextColor);
       
   356 
       
   357 
       
   358     tb = new QToolBar(this);
       
   359     tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
       
   360     tb->setWindowTitle(tr("Format Actions"));
       
   361     addToolBarBreak(Qt::TopToolBarArea);
       
   362     addToolBar(tb);
       
   363 
       
   364     comboStyle = new QComboBox(tb);
       
   365     tb->addWidget(comboStyle);
       
   366     comboStyle->addItem("Standard");
       
   367     comboStyle->addItem("Bullet List (Disc)");
       
   368     comboStyle->addItem("Bullet List (Circle)");
       
   369     comboStyle->addItem("Bullet List (Square)");
       
   370     comboStyle->addItem("Ordered List (Decimal)");
       
   371     comboStyle->addItem("Ordered List (Alpha lower)");
       
   372     comboStyle->addItem("Ordered List (Alpha upper)");
       
   373     comboStyle->addItem("Ordered List (Roman lower)");
       
   374     comboStyle->addItem("Ordered List (Roman upper)");
       
   375     connect(comboStyle, SIGNAL(activated(int)),
       
   376             this, SLOT(textStyle(int)));
       
   377 
       
   378     comboFont = new QFontComboBox(tb);
       
   379     tb->addWidget(comboFont);
       
   380     connect(comboFont, SIGNAL(activated(const QString &)),
       
   381             this, SLOT(textFamily(const QString &)));
       
   382 
       
   383     comboSize = new QComboBox(tb);
       
   384     comboSize->setObjectName("comboSize");
       
   385     tb->addWidget(comboSize);
       
   386     comboSize->setEditable(true);
       
   387 
       
   388     QFontDatabase db;
       
   389     foreach(int size, db.standardSizes())
       
   390         comboSize->addItem(QString::number(size));
       
   391 
       
   392     connect(comboSize, SIGNAL(activated(const QString &)),
       
   393             this, SLOT(textSize(const QString &)));
       
   394     comboSize->setCurrentIndex(comboSize->findText(QString::number(QApplication::font()
       
   395                                                                    .pointSize())));
       
   396 }
       
   397 
       
   398 bool TextEdit::load(const QString &f)
       
   399 {
       
   400     if (!QFile::exists(f))
       
   401         return false;
       
   402     QFile file(f);
       
   403     if (!file.open(QFile::ReadOnly))
       
   404         return false;
       
   405 
       
   406     QByteArray data = file.readAll();
       
   407     QTextCodec *codec = Qt::codecForHtml(data);
       
   408     QString str = codec->toUnicode(data);
       
   409     if (Qt::mightBeRichText(str)) {
       
   410         textEdit->setHtml(str);
       
   411     } else {
       
   412         str = QString::fromLocal8Bit(data);
       
   413         textEdit->setPlainText(str);
       
   414     }
       
   415 
       
   416     setCurrentFileName(f);
       
   417     return true;
       
   418 }
       
   419 
       
   420 bool TextEdit::maybeSave()
       
   421 {
       
   422     if (!textEdit->document()->isModified())
       
   423         return true;
       
   424     if (fileName.startsWith(QLatin1String(":/")))
       
   425         return true;
       
   426     QMessageBox::StandardButton ret;
       
   427     ret = QMessageBox::warning(this, tr("Application"),
       
   428                                tr("The document has been modified.\n"
       
   429                                   "Do you want to save your changes?"),
       
   430                                QMessageBox::Save | QMessageBox::Discard
       
   431                                | QMessageBox::Cancel);
       
   432     if (ret == QMessageBox::Save)
       
   433         return fileSave();
       
   434     else if (ret == QMessageBox::Cancel)
       
   435         return false;
       
   436     return true;
       
   437 }
       
   438 
       
   439 void TextEdit::setCurrentFileName(const QString &fileName)
       
   440 {
       
   441     this->fileName = fileName;
       
   442     textEdit->document()->setModified(false);
       
   443 
       
   444     QString shownName;
       
   445     if (fileName.isEmpty())
       
   446         shownName = "untitled.txt";
       
   447     else
       
   448         shownName = QFileInfo(fileName).fileName();
       
   449 
       
   450     setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
       
   451     setWindowModified(false);
       
   452 }
       
   453 
       
   454 void TextEdit::fileNew()
       
   455 {
       
   456     if (maybeSave()) {
       
   457         textEdit->clear();
       
   458         setCurrentFileName(QString());
       
   459     }
       
   460 }
       
   461 
       
   462 void TextEdit::fileOpen()
       
   463 {
       
   464     QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
       
   465                                               QString(), tr("HTML-Files (*.htm *.html);;All Files (*)"));
       
   466     if (!fn.isEmpty())
       
   467         load(fn);
       
   468 }
       
   469 
       
   470 bool TextEdit::fileSave()
       
   471 {
       
   472     if (fileName.isEmpty())
       
   473         return fileSaveAs();
       
   474 
       
   475     QTextDocumentWriter writer(fileName);
       
   476     bool success = writer.write(textEdit->document());
       
   477     if (success)
       
   478         textEdit->document()->setModified(false);
       
   479     return success;
       
   480 }
       
   481 
       
   482 bool TextEdit::fileSaveAs()
       
   483 {
       
   484     QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
       
   485                                               QString(), tr("ODF files (*.odt);;HTML-Files (*.htm *.html);;All Files (*)"));
       
   486     if (fn.isEmpty())
       
   487         return false;
       
   488     if (! (fn.endsWith(".odt", Qt::CaseInsensitive) || fn.endsWith(".htm", Qt::CaseInsensitive) || fn.endsWith(".html", Qt::CaseInsensitive)) )
       
   489         fn += ".odt"; // default
       
   490     setCurrentFileName(fn);
       
   491     return fileSave();
       
   492 }
       
   493 
       
   494 void TextEdit::filePrint()
       
   495 {
       
   496 #ifndef QT_NO_PRINTER
       
   497     QPrinter printer(QPrinter::HighResolution);
       
   498     QPrintDialog *dlg = new QPrintDialog(&printer, this);
       
   499     if (textEdit->textCursor().hasSelection())
       
   500         dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
       
   501     dlg->setWindowTitle(tr("Print Document"));
       
   502     if (dlg->exec() == QDialog::Accepted) {
       
   503         textEdit->print(&printer);
       
   504     }
       
   505     delete dlg;
       
   506 #endif
       
   507 }
       
   508 
       
   509 void TextEdit::filePrintPreview()
       
   510 {
       
   511 #ifndef QT_NO_PRINTER
       
   512     QPrinter printer(QPrinter::HighResolution);
       
   513     QPrintPreviewDialog preview(&printer, this);
       
   514     connect(&preview, SIGNAL(paintRequested(QPrinter *)), SLOT(printPreview(QPrinter *)));
       
   515     preview.exec();
       
   516 #endif
       
   517 }
       
   518 
       
   519 void TextEdit::printPreview(QPrinter *printer)
       
   520 {
       
   521 #ifdef QT_NO_PRINTER
       
   522     Q_UNUSED(printer);
       
   523 #else
       
   524     textEdit->print(printer);
       
   525 #endif
       
   526 }
       
   527 
       
   528 
       
   529 void TextEdit::filePrintPdf()
       
   530 {
       
   531 #ifndef QT_NO_PRINTER
       
   532 //! [0]
       
   533     QString fileName = QFileDialog::getSaveFileName(this, "Export PDF",
       
   534                                                     QString(), "*.pdf");
       
   535     if (!fileName.isEmpty()) {
       
   536         if (QFileInfo(fileName).suffix().isEmpty())
       
   537             fileName.append(".pdf");
       
   538         QPrinter printer(QPrinter::HighResolution);
       
   539         printer.setOutputFormat(QPrinter::PdfFormat);
       
   540         printer.setOutputFileName(fileName);
       
   541         textEdit->document()->print(&printer);
       
   542     }
       
   543 //! [0]
       
   544 #endif
       
   545 }
       
   546 
       
   547 void TextEdit::textBold()
       
   548 {
       
   549     QTextCharFormat fmt;
       
   550     fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
       
   551     mergeFormatOnWordOrSelection(fmt);
       
   552 }
       
   553 
       
   554 void TextEdit::textUnderline()
       
   555 {
       
   556     QTextCharFormat fmt;
       
   557     fmt.setFontUnderline(actionTextUnderline->isChecked());
       
   558     mergeFormatOnWordOrSelection(fmt);
       
   559 }
       
   560 
       
   561 void TextEdit::textItalic()
       
   562 {
       
   563     QTextCharFormat fmt;
       
   564     fmt.setFontItalic(actionTextItalic->isChecked());
       
   565     mergeFormatOnWordOrSelection(fmt);
       
   566 }
       
   567 
       
   568 void TextEdit::textFamily(const QString &f)
       
   569 {
       
   570     QTextCharFormat fmt;
       
   571     fmt.setFontFamily(f);
       
   572     mergeFormatOnWordOrSelection(fmt);
       
   573 }
       
   574 
       
   575 void TextEdit::textSize(const QString &p)
       
   576 {
       
   577     qreal pointSize = p.toFloat();
       
   578     if (p.toFloat() > 0) {
       
   579         QTextCharFormat fmt;
       
   580         fmt.setFontPointSize(pointSize);
       
   581         mergeFormatOnWordOrSelection(fmt);
       
   582     }
       
   583 }
       
   584 
       
   585 void TextEdit::textStyle(int styleIndex)
       
   586 {
       
   587     QTextCursor cursor = textEdit->textCursor();
       
   588 
       
   589     if (styleIndex != 0) {
       
   590         QTextListFormat::Style style = QTextListFormat::ListDisc;
       
   591 
       
   592         switch (styleIndex) {
       
   593             default:
       
   594             case 1:
       
   595                 style = QTextListFormat::ListDisc;
       
   596                 break;
       
   597             case 2:
       
   598                 style = QTextListFormat::ListCircle;
       
   599                 break;
       
   600             case 3:
       
   601                 style = QTextListFormat::ListSquare;
       
   602                 break;
       
   603             case 4:
       
   604                 style = QTextListFormat::ListDecimal;
       
   605                 break;
       
   606             case 5:
       
   607                 style = QTextListFormat::ListLowerAlpha;
       
   608                 break;
       
   609             case 6:
       
   610                 style = QTextListFormat::ListUpperAlpha;
       
   611                 break;
       
   612             case 7:
       
   613                 style = QTextListFormat::ListLowerRoman;
       
   614                 break;
       
   615             case 8:
       
   616                 style = QTextListFormat::ListUpperRoman;
       
   617                 break;
       
   618         }
       
   619 
       
   620         cursor.beginEditBlock();
       
   621 
       
   622         QTextBlockFormat blockFmt = cursor.blockFormat();
       
   623 
       
   624         QTextListFormat listFmt;
       
   625 
       
   626         if (cursor.currentList()) {
       
   627             listFmt = cursor.currentList()->format();
       
   628         } else {
       
   629             listFmt.setIndent(blockFmt.indent() + 1);
       
   630             blockFmt.setIndent(0);
       
   631             cursor.setBlockFormat(blockFmt);
       
   632         }
       
   633 
       
   634         listFmt.setStyle(style);
       
   635 
       
   636         cursor.createList(listFmt);
       
   637 
       
   638         cursor.endEditBlock();
       
   639     } else {
       
   640         // ####
       
   641         QTextBlockFormat bfmt;
       
   642         bfmt.setObjectIndex(-1);
       
   643         cursor.mergeBlockFormat(bfmt);
       
   644     }
       
   645 }
       
   646 
       
   647 void TextEdit::textColor()
       
   648 {
       
   649     QColor col = QColorDialog::getColor(textEdit->textColor(), this);
       
   650     if (!col.isValid())
       
   651         return;
       
   652     QTextCharFormat fmt;
       
   653     fmt.setForeground(col);
       
   654     mergeFormatOnWordOrSelection(fmt);
       
   655     colorChanged(col);
       
   656 }
       
   657 
       
   658 void TextEdit::textAlign(QAction *a)
       
   659 {
       
   660     if (a == actionAlignLeft)
       
   661         textEdit->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
       
   662     else if (a == actionAlignCenter)
       
   663         textEdit->setAlignment(Qt::AlignHCenter);
       
   664     else if (a == actionAlignRight)
       
   665         textEdit->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
       
   666     else if (a == actionAlignJustify)
       
   667         textEdit->setAlignment(Qt::AlignJustify);
       
   668 }
       
   669 
       
   670 void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
       
   671 {
       
   672     fontChanged(format.font());
       
   673     colorChanged(format.foreground().color());
       
   674 }
       
   675 
       
   676 void TextEdit::cursorPositionChanged()
       
   677 {
       
   678     alignmentChanged(textEdit->alignment());
       
   679 }
       
   680 
       
   681 void TextEdit::clipboardDataChanged()
       
   682 {
       
   683 #ifndef QT_NO_CLIPBOARD
       
   684     actionPaste->setEnabled(!QApplication::clipboard()->text().isEmpty());
       
   685 #endif
       
   686 }
       
   687 
       
   688 void TextEdit::about()
       
   689 {
       
   690     QMessageBox::about(this, tr("About"), tr("This example demonstrates Qt's "
       
   691         "rich text editing facilities in action, providing an example "
       
   692         "document for you to experiment with."));
       
   693 }
       
   694 
       
   695 void TextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
       
   696 {
       
   697     QTextCursor cursor = textEdit->textCursor();
       
   698     if (!cursor.hasSelection())
       
   699         cursor.select(QTextCursor::WordUnderCursor);
       
   700     cursor.mergeCharFormat(format);
       
   701     textEdit->mergeCurrentCharFormat(format);
       
   702 }
       
   703 
       
   704 void TextEdit::fontChanged(const QFont &f)
       
   705 {
       
   706     comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
       
   707     comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
       
   708     actionTextBold->setChecked(f.bold());
       
   709     actionTextItalic->setChecked(f.italic());
       
   710     actionTextUnderline->setChecked(f.underline());
       
   711 }
       
   712 
       
   713 void TextEdit::colorChanged(const QColor &c)
       
   714 {
       
   715     QPixmap pix(16, 16);
       
   716     pix.fill(c);
       
   717     actionTextColor->setIcon(pix);
       
   718 }
       
   719 
       
   720 void TextEdit::alignmentChanged(Qt::Alignment a)
       
   721 {
       
   722     if (a & Qt::AlignLeft) {
       
   723         actionAlignLeft->setChecked(true);
       
   724     } else if (a & Qt::AlignHCenter) {
       
   725         actionAlignCenter->setChecked(true);
       
   726     } else if (a & Qt::AlignRight) {
       
   727         actionAlignRight->setChecked(true);
       
   728     } else if (a & Qt::AlignJustify) {
       
   729         actionAlignJustify->setChecked(true);
       
   730     }
       
   731 }
       
   732