Text Edit▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#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 <QTextCodec>
#include <QTextEdit>
#include <QStatusBar>
#include <QToolBar>
#include <QTextCursor>
#include <QTextDocumentWriter>
#include <QTextList>
#include <QtDebug>
#include <QCloseEvent>
#include <QMessageBox>
#include <QMimeData>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
#if QT_CONFIG(printdialog)
#include <QPrintDialog>
#endif
#include <QPrinter>
#if QT_CONFIG(printpreviewdialog)
#include <QPrintPreviewDialog>
#endif
#endif
#endif
#include
"textedit.h"
#ifdef Q_OS_MAC
const
QString rsrcPath =
":/images/mac"
;
#else
const
QString rsrcPath =
":/images/win"
;
#endif
TextEdit::
TextEdit(QWidget *
parent)
:
QMainWindow(parent)
{
#ifdef Q_OS_OSX
setUnifiedTitleAndToolBarOnMac(true
);
#endif
setWindowTitle(QCoreApplication::
applicationName());
textEdit =
new
QTextEdit(this
);
connect(textEdit, &
amp;QTextEdit::
currentCharFormatChanged,
this
, &
amp;TextEdit::
currentCharFormatChanged);
connect(textEdit, &
amp;QTextEdit::
cursorPositionChanged,
this
, &
amp;TextEdit::
cursorPositionChanged);
setCentralWidget(textEdit);
setToolButtonStyle(Qt::
ToolButtonFollowStyle);
setupFileActions();
setupEditActions();
setupTextActions();
{
QMenu *
helpMenu =
menuBar()-&
gt;addMenu(tr("Help"
));
helpMenu-&
gt;addAction(tr("About"
), this
, &
amp;TextEdit::
about);
helpMenu-&
gt;addAction(tr("About &Qt"
), qApp, &
amp;QApplication::
aboutQt);
}
QFont textFont("Helvetica"
);
textFont.setStyleHint(QFont::
SansSerif);
textEdit-&
gt;setFont(textFont);
fontChanged(textEdit-&
gt;font());
colorChanged(textEdit-&
gt;textColor());
alignmentChanged(textEdit-&
gt;alignment());
connect(textEdit-&
gt;document(), &
amp;QTextDocument::
modificationChanged,
actionSave, &
amp;QAction::
setEnabled);
connect(textEdit-&
gt;document(), &
amp;QTextDocument::
modificationChanged,
this
, &
amp;QWidget::
setWindowModified);
connect(textEdit-&
gt;document(), &
amp;QTextDocument::
undoAvailable,
actionUndo, &
amp;QAction::
setEnabled);
connect(textEdit-&
gt;document(), &
amp;QTextDocument::
redoAvailable,
actionRedo, &
amp;QAction::
setEnabled);
setWindowModified(textEdit-&
gt;document()-&
gt;isModified());
actionSave-&
gt;setEnabled(textEdit-&
gt;document()-&
gt;isModified());
actionUndo-&
gt;setEnabled(textEdit-&
gt;document()-&
gt;isUndoAvailable());
actionRedo-&
gt;setEnabled(textEdit-&
gt;document()-&
gt;isRedoAvailable());
#ifndef QT_NO_CLIPBOARD
actionCut-&
gt;setEnabled(false
);
connect(textEdit, &
amp;QTextEdit::
copyAvailable, actionCut, &
amp;QAction::
setEnabled);
actionCopy-&
gt;setEnabled(false
);
connect(textEdit, &
amp;QTextEdit::
copyAvailable, actionCopy, &
amp;QAction::
setEnabled);
connect(QApplication::
clipboard(), &
amp;QClipboard::
dataChanged, this
, &
amp;TextEdit::
clipboardDataChanged);
#endif
textEdit-&
gt;setFocus();
setCurrentFileName(QString());
#ifdef Q_OS_MACOS
// Use dark text on light background on macOS, also in dark mode.
QPalette pal =
textEdit-&
gt;palette();
pal.setColor(QPalette::
Base, QColor(Qt::
white));
pal.setColor(QPalette::
Text, QColor(Qt::
black));
textEdit-&
gt;setPalette(pal);
#endif
}
void
TextEdit::
closeEvent(QCloseEvent *
e)
{
if
(maybeSave())
e-&
gt;accept();
else
e-&
gt;ignore();
}
void
TextEdit::
setupFileActions()
{
QToolBar *
tb =
addToolBar(tr("File Actions"
));
QMenu *
menu =
menuBar()-&
gt;addMenu(tr("&File"
));
const
QIcon newIcon =
QIcon::
fromTheme("document-new"
, QIcon(rsrcPath +
"/filenew.png"
));
QAction *
a =
menu-&
gt;addAction(newIcon, tr("&New"
), this
, &
amp;TextEdit::
fileNew);
tb-&
gt;addAction(a);
a-&
gt;setPriority(QAction::
LowPriority);
a-&
gt;setShortcut(QKeySequence::
New);
const
QIcon openIcon =
QIcon::
fromTheme("document-open"
, QIcon(rsrcPath +
"/fileopen.png"
));
a =
menu-&
gt;addAction(openIcon, tr("&Open..."
), this
, &
amp;TextEdit::
fileOpen);
a-&
gt;setShortcut(QKeySequence::
Open);
tb-&
gt;addAction(a);
menu-&
gt;addSeparator();
const
QIcon saveIcon =
QIcon::
fromTheme("document-save"
, QIcon(rsrcPath +
"/filesave.png"
));
actionSave =
menu-&
gt;addAction(saveIcon, tr("&Save"
), this
, &
amp;TextEdit::
fileSave);
actionSave-&
gt;setShortcut(QKeySequence::
Save);
actionSave-&
gt;setEnabled(false
);
tb-&
gt;addAction(actionSave);
a =
menu-&
gt;addAction(tr("Save &As..."
), this
, &
amp;TextEdit::
fileSaveAs);
a-&
gt;setPriority(QAction::
LowPriority);
menu-&
gt;addSeparator();
#ifndef QT_NO_PRINTER
const
QIcon printIcon =
QIcon::
fromTheme("document-print"
, QIcon(rsrcPath +
"/fileprint.png"
));
a =
menu-&
gt;addAction(printIcon, tr("&Print..."
), this
, &
amp;TextEdit::
filePrint);
a-&
gt;setPriority(QAction::
LowPriority);
a-&
gt;setShortcut(QKeySequence::
Print);
tb-&
gt;addAction(a);
const
QIcon filePrintIcon =
QIcon::
fromTheme("fileprint"
, QIcon(rsrcPath +
"/fileprint.png"
));
menu-&
gt;addAction(filePrintIcon, tr("Print Preview..."
), this
, &
amp;TextEdit::
filePrintPreview);
const
QIcon exportPdfIcon =
QIcon::
fromTheme("exportpdf"
, QIcon(rsrcPath +
"/exportpdf.png"
));
a =
menu-&
gt;addAction(exportPdfIcon, tr("&Export PDF..."
), this
, &
amp;TextEdit::
filePrintPdf);
a-&
gt;setPriority(QAction::
LowPriority);
a-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_D);
tb-&
gt;addAction(a);
menu-&
gt;addSeparator();
#endif
a =
menu-&
gt;addAction(tr("&Quit"
), this
, &
amp;QWidget::
close);
a-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_Q);
}
void
TextEdit::
setupEditActions()
{
QToolBar *
tb =
addToolBar(tr("Edit Actions"
));
QMenu *
menu =
menuBar()-&
gt;addMenu(tr("&Edit"
));
const
QIcon undoIcon =
QIcon::
fromTheme("edit-undo"
, QIcon(rsrcPath +
"/editundo.png"
));
actionUndo =
menu-&
gt;addAction(undoIcon, tr("&Undo"
), textEdit, &
amp;QTextEdit::
undo);
actionUndo-&
gt;setShortcut(QKeySequence::
Undo);
tb-&
gt;addAction(actionUndo);
const
QIcon redoIcon =
QIcon::
fromTheme("edit-redo"
, QIcon(rsrcPath +
"/editredo.png"
));
actionRedo =
menu-&
gt;addAction(redoIcon, tr("&Redo"
), textEdit, &
amp;QTextEdit::
redo);
actionRedo-&
gt;setPriority(QAction::
LowPriority);
actionRedo-&
gt;setShortcut(QKeySequence::
Redo);
tb-&
gt;addAction(actionRedo);
menu-&
gt;addSeparator();
#ifndef QT_NO_CLIPBOARD
const
QIcon cutIcon =
QIcon::
fromTheme("edit-cut"
, QIcon(rsrcPath +
"/editcut.png"
));
actionCut =
menu-&
gt;addAction(cutIcon, tr("Cu&t"
), textEdit, &
amp;QTextEdit::
cut);
actionCut-&
gt;setPriority(QAction::
LowPriority);
actionCut-&
gt;setShortcut(QKeySequence::
Cut);
tb-&
gt;addAction(actionCut);
const
QIcon copyIcon =
QIcon::
fromTheme("edit-copy"
, QIcon(rsrcPath +
"/editcopy.png"
));
actionCopy =
menu-&
gt;addAction(copyIcon, tr("&Copy"
), textEdit, &
amp;QTextEdit::
copy);
actionCopy-&
gt;setPriority(QAction::
LowPriority);
actionCopy-&
gt;setShortcut(QKeySequence::
Copy);
tb-&
gt;addAction(actionCopy);
const
QIcon pasteIcon =
QIcon::
fromTheme("edit-paste"
, QIcon(rsrcPath +
"/editpaste.png"
));
actionPaste =
menu-&
gt;addAction(pasteIcon, tr("&Paste"
), textEdit, &
amp;QTextEdit::
paste);
actionPaste-&
gt;setPriority(QAction::
LowPriority);
actionPaste-&
gt;setShortcut(QKeySequence::
Paste);
tb-&
gt;addAction(actionPaste);
if
(const
QMimeData *
md =
QApplication::
clipboard()-&
gt;mimeData())
actionPaste-&
gt;setEnabled(md-&
gt;hasText());
#endif
}
void
TextEdit::
setupTextActions()
{
QToolBar *
tb =
addToolBar(tr("Format Actions"
));
QMenu *
menu =
menuBar()-&
gt;addMenu(tr("F&ormat"
));
const
QIcon boldIcon =
QIcon::
fromTheme("format-text-bold"
, QIcon(rsrcPath +
"/textbold.png"
));
actionTextBold =
menu-&
gt;addAction(boldIcon, tr("&Bold"
), this
, &
amp;TextEdit::
textBold);
actionTextBold-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_B);
actionTextBold-&
gt;setPriority(QAction::
LowPriority);
QFont bold;
bold.setBold(true
);
actionTextBold-&
gt;setFont(bold);
tb-&
gt;addAction(actionTextBold);
actionTextBold-&
gt;setCheckable(true
);
const
QIcon italicIcon =
QIcon::
fromTheme("format-text-italic"
, QIcon(rsrcPath +
"/textitalic.png"
));
actionTextItalic =
menu-&
gt;addAction(italicIcon, tr("&Italic"
), this
, &
amp;TextEdit::
textItalic);
actionTextItalic-&
gt;setPriority(QAction::
LowPriority);
actionTextItalic-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_I);
QFont italic;
italic.setItalic(true
);
actionTextItalic-&
gt;setFont(italic);
tb-&
gt;addAction(actionTextItalic);
actionTextItalic-&
gt;setCheckable(true
);
const
QIcon underlineIcon =
QIcon::
fromTheme("format-text-underline"
, QIcon(rsrcPath +
"/textunder.png"
));
actionTextUnderline =
menu-&
gt;addAction(underlineIcon, tr("&Underline"
), this
, &
amp;TextEdit::
textUnderline);
actionTextUnderline-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_U);
actionTextUnderline-&
gt;setPriority(QAction::
LowPriority);
QFont underline;
underline.setUnderline(true
);
actionTextUnderline-&
gt;setFont(underline);
tb-&
gt;addAction(actionTextUnderline);
actionTextUnderline-&
gt;setCheckable(true
);
menu-&
gt;addSeparator();
const
QIcon leftIcon =
QIcon::
fromTheme("format-justify-left"
, QIcon(rsrcPath +
"/textleft.png"
));
actionAlignLeft =
new
QAction(leftIcon, tr("&Left"
), this
);
actionAlignLeft-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_L);
actionAlignLeft-&
gt;setCheckable(true
);
actionAlignLeft-&
gt;setPriority(QAction::
LowPriority);
const
QIcon centerIcon =
QIcon::
fromTheme("format-justify-center"
, QIcon(rsrcPath +
"/textcenter.png"
));
actionAlignCenter =
new
QAction(centerIcon, tr("C&enter"
), this
);
actionAlignCenter-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_E);
actionAlignCenter-&
gt;setCheckable(true
);
actionAlignCenter-&
gt;setPriority(QAction::
LowPriority);
const
QIcon rightIcon =
QIcon::
fromTheme("format-justify-right"
, QIcon(rsrcPath +
"/textright.png"
));
actionAlignRight =
new
QAction(rightIcon, tr("&Right"
), this
);
actionAlignRight-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_R);
actionAlignRight-&
gt;setCheckable(true
);
actionAlignRight-&
gt;setPriority(QAction::
LowPriority);
const
QIcon fillIcon =
QIcon::
fromTheme("format-justify-fill"
, QIcon(rsrcPath +
"/textjustify.png"
));
actionAlignJustify =
new
QAction(fillIcon, tr("&Justify"
), this
);
actionAlignJustify-&
gt;setShortcut(Qt::
CTRL +
Qt::
Key_J);
actionAlignJustify-&
gt;setCheckable(true
);
actionAlignJustify-&
gt;setPriority(QAction::
LowPriority);
// Make sure the alignLeft is always left of the alignRight
QActionGroup *
alignGroup =
new
QActionGroup(this
);
connect(alignGroup, &
amp;QActionGroup::
triggered, this
, &
amp;TextEdit::
textAlign);
if
(QApplication::
isLeftToRight()) {
alignGroup-&
gt;addAction(actionAlignLeft);
alignGroup-&
gt;addAction(actionAlignCenter);
alignGroup-&
gt;addAction(actionAlignRight);
}
else
{
alignGroup-&
gt;addAction(actionAlignRight);
alignGroup-&
gt;addAction(actionAlignCenter);
alignGroup-&
gt;addAction(actionAlignLeft);
}
alignGroup-&
gt;addAction(actionAlignJustify);
tb-&
gt;addActions(alignGroup-&
gt;actions());
menu-&
gt;addActions(alignGroup-&
gt;actions());
menu-&
gt;addSeparator();
QPixmap pix(16
, 16
);
pix.fill(Qt::
black);
actionTextColor =
menu-&
gt;addAction(pix, tr("&Color..."
), this
, &
amp;TextEdit::
textColor);
tb-&
gt;addAction(actionTextColor);
tb =
addToolBar(tr("Format Actions"
));
tb-&
gt;setAllowedAreas(Qt::
TopToolBarArea |
Qt::
BottomToolBarArea);
addToolBarBreak(Qt::
TopToolBarArea);
addToolBar(tb);
comboStyle =
new
QComboBox(tb);
tb-&
gt;addWidget(comboStyle);
comboStyle-&
gt;addItem("Standard"
);
comboStyle-&
gt;addItem("Bullet List (Disc)"
);
comboStyle-&
gt;addItem("Bullet List (Circle)"
);
comboStyle-&
gt;addItem("Bullet List (Square)"
);
comboStyle-&
gt;addItem("Ordered List (Decimal)"
);
comboStyle-&
gt;addItem("Ordered List (Alpha lower)"
);
comboStyle-&
gt;addItem("Ordered List (Alpha upper)"
);
comboStyle-&
gt;addItem("Ordered List (Roman lower)"
);
comboStyle-&
gt;addItem("Ordered List (Roman upper)"
);
comboStyle-&
gt;addItem("Heading 1"
);
comboStyle-&
gt;addItem("Heading 2"
);
comboStyle-&
gt;addItem("Heading 3"
);
comboStyle-&
gt;addItem("Heading 4"
);
comboStyle-&
gt;addItem("Heading 5"
);
comboStyle-&
gt;addItem("Heading 6"
);
connect(comboStyle, QOverload&
lt;int
&
gt;::
of(&
amp;QComboBox::
activated), this
, &
amp;TextEdit::
textStyle);
comboFont =
new
QFontComboBox(tb);
tb-&
gt;addWidget(comboFont);
connect(comboFont, QOverload&
lt;const
QString &
amp;&
gt;::
of(&
amp;QComboBox::
activated), this
, &
amp;TextEdit::
textFamily);
comboSize =
new
QComboBox(tb);
comboSize-&
gt;setObjectName("comboSize"
);
tb-&
gt;addWidget(comboSize);
comboSize-&
gt;setEditable(true
);
const
QList&
lt;int
&
gt; standardSizes =
QFontDatabase::
standardSizes();
foreach (int
size, standardSizes)
comboSize-&
gt;addItem(QString::
number(size));
comboSize-&
gt;setCurrentIndex(standardSizes.indexOf(QApplication::
font().pointSize()));
connect(comboSize, QOverload&
lt;const
QString &
amp;&
gt;::
of(&
amp;QComboBox::
activated), this
, &
amp;TextEdit::
textSize);
}
bool
TextEdit::
load(const
QString &
amp;f)
{
if
(!
QFile::
exists(f))
return
false
;
QFile file(f);
if
(!
file.open(QFile::
ReadOnly))
return
false
;
QByteArray data =
file.readAll();
QTextCodec *
codec =
Qt::
codecForHtml(data);
QString str =
codec-&
gt;toUnicode(data);
if
(Qt::
mightBeRichText(str)) {
textEdit-&
gt;setHtml(str);
}
else
{
str =
QString::
fromLocal8Bit(data);
textEdit-&
gt;setPlainText(str);
}
setCurrentFileName(f);
return
true
;
}
bool
TextEdit::
maybeSave()
{
if
(!
textEdit-&
gt;document()-&
gt;isModified())
return
true
;
const
QMessageBox::
StandardButton ret =
QMessageBox::
warning(this
, QCoreApplication::
applicationName(),
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 &
amp;fileName)
{
this
-&
gt;fileName =
fileName;
textEdit-&
gt;document()-&
gt;setModified(false
);
QString shownName;
if
(fileName.isEmpty())
shownName =
"untitled.txt"
;
else
shownName =
QFileInfo(fileName).fileName();
setWindowTitle(tr("%1[*] - %2"
).arg(shownName, QCoreApplication::
applicationName()));
setWindowModified(false
);
}
void
TextEdit::
fileNew()
{
if
(maybeSave()) {
textEdit-&
gt;clear();
setCurrentFileName(QString());
}
}
void
TextEdit::
fileOpen()
{
QFileDialog fileDialog(this
, tr("Open File..."
));
fileDialog.setAcceptMode(QFileDialog::
AcceptOpen);
fileDialog.setFileMode(QFileDialog::
ExistingFile);
fileDialog.setMimeTypeFilters(QStringList() &
lt;&
lt; "text/html"
&
lt;&
lt; "text/plain"
);
if
(fileDialog.exec() !=
QDialog::
Accepted)
return
;
const
QString fn =
fileDialog.selectedFiles().first();
if
(load(fn))
statusBar()-&
gt;showMessage(tr("Opened
\"
%1
\"
"
).arg(QDir::
toNativeSeparators(fn)));
else
statusBar()-&
gt;showMessage(tr("Could not open
\"
%1
\"
"
).arg(QDir::
toNativeSeparators(fn)));
}
bool
TextEdit::
fileSave()
{
if
(fileName.isEmpty())
return
fileSaveAs();
if
(fileName.startsWith(QStringLiteral(":/"
)))
return
fileSaveAs();
QTextDocumentWriter writer(fileName);
bool
success =
writer.write(textEdit-&
gt;document());
if
(success) {
textEdit-&
gt;document()-&
gt;setModified(false
);
statusBar()-&
gt;showMessage(tr("Wrote
\"
%1
\"
"
).arg(QDir::
toNativeSeparators(fileName)));
}
else
{
statusBar()-&
gt;showMessage(tr("Could not write to file
\"
%1
\"
"
)
.arg(QDir::
toNativeSeparators(fileName)));
}
return
success;
}
bool
TextEdit::
fileSaveAs()
{
QFileDialog fileDialog(this
, tr("Save as..."
));
fileDialog.setAcceptMode(QFileDialog::
AcceptSave);
QStringList mimeTypes;
mimeTypes &
lt;&
lt; "application/vnd.oasis.opendocument.text"
&
lt;&
lt; "text/html"
&
lt;&
lt; "text/plain"
;
fileDialog.setMimeTypeFilters(mimeTypes);
fileDialog.setDefaultSuffix("odt"
);
if
(fileDialog.exec() !=
QDialog::
Accepted)
return
false
;
const
QString fn =
fileDialog.selectedFiles().first();
setCurrentFileName(fn);
return
fileSave();
}
void
TextEdit::
filePrint()
{
#if QT_CONFIG(printdialog)
QPrinter printer(QPrinter::
HighResolution);
QPrintDialog *
dlg =
new
QPrintDialog(&
amp;printer, this
);
if
(textEdit-&
gt;textCursor().hasSelection())
dlg-&
gt;addEnabledOption(QAbstractPrintDialog::
PrintSelection);
dlg-&
gt;setWindowTitle(tr("Print Document"
));
if
(dlg-&
gt;exec() ==
QDialog::
Accepted)
textEdit-&
gt;print(&
amp;printer);
delete
dlg;
#endif
}
void
TextEdit::
filePrintPreview()
{
#if QT_CONFIG(printpreviewdialog)
QPrinter printer(QPrinter::
HighResolution);
QPrintPreviewDialog preview(&
amp;printer, this
);
connect(&
amp;preview, &
amp;QPrintPreviewDialog::
paintRequested, this
, &
amp;TextEdit::
printPreview);
preview.exec();
#endif
}
void
TextEdit::
printPreview(QPrinter *
printer)
{
#ifdef QT_NO_PRINTER
Q_UNUSED(printer);
#else
textEdit-&
gt;print(printer);
#endif
}
void
TextEdit::
filePrintPdf()
{
#ifndef QT_NO_PRINTER
QFileDialog fileDialog(this
, tr("Export PDF"
));
fileDialog.setAcceptMode(QFileDialog::
AcceptSave);
fileDialog.setMimeTypeFilters(QStringList("application/pdf"
));
fileDialog.setDefaultSuffix("pdf"
);
if
(fileDialog.exec() !=
QDialog::
Accepted)
return
;
QString fileName =
fileDialog.selectedFiles().first();
QPrinter printer(QPrinter::
HighResolution);
printer.setOutputFormat(QPrinter::
PdfFormat);
printer.setOutputFileName(fileName);
textEdit-&
gt;document()-&
gt;print(&
amp;printer);
statusBar()-&
gt;showMessage(tr("Exported
\"
%1
\"
"
)
.arg(QDir::
toNativeSeparators(fileName)));
#endif
}
void
TextEdit::
textBold()
{
QTextCharFormat fmt;
fmt.setFontWeight(actionTextBold-&
gt;isChecked() ? QFont::
Bold : QFont::
Normal);
mergeFormatOnWordOrSelection(fmt);
}
void
TextEdit::
textUnderline()
{
QTextCharFormat fmt;
fmt.setFontUnderline(actionTextUnderline-&
gt;isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void
TextEdit::
textItalic()
{
QTextCharFormat fmt;
fmt.setFontItalic(actionTextItalic-&
gt;isChecked());
mergeFormatOnWordOrSelection(fmt);
}
void
TextEdit::
textFamily(const
QString &
amp;f)
{
QTextCharFormat fmt;
fmt.setFontFamily(f);
mergeFormatOnWordOrSelection(fmt);
}
void
TextEdit::
textSize(const
QString &
amp;p)
{
qreal pointSize =
p.toFloat();
if
(p.toFloat() &
gt; 0
) {
QTextCharFormat fmt;
fmt.setFontPointSize(pointSize);
mergeFormatOnWordOrSelection(fmt);
}
}
void
TextEdit::
textStyle(int
styleIndex)
{
QTextCursor cursor =
textEdit-&
gt;textCursor();
QTextListFormat::
Style style =
QTextListFormat::
ListStyleUndefined;
switch
(styleIndex) {
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
;
default
:
break
;
}
cursor.beginEditBlock();
QTextBlockFormat blockFmt =
cursor.blockFormat();
if
(style ==
QTextListFormat::
ListStyleUndefined) {
blockFmt.setObjectIndex(-
1
);
int
headingLevel =
styleIndex &
gt;=
9
? styleIndex -
9
+
1
: 0
; // H1 to H6, or Standard
blockFmt.setHeadingLevel(headingLevel);
cursor.setBlockFormat(blockFmt);
int
sizeAdjustment =
headingLevel ? 4
-
headingLevel : 0
; // H1 to H6: +3 to -2
QTextCharFormat fmt;
fmt.setFontWeight(headingLevel ? QFont::
Bold : QFont::
Normal);
fmt.setProperty(QTextFormat::
FontSizeAdjustment, sizeAdjustment);
cursor.select(QTextCursor::
LineUnderCursor);
cursor.mergeCharFormat(fmt);
textEdit-&
gt;mergeCurrentCharFormat(fmt);
}
else
{
QTextListFormat listFmt;
if
(cursor.currentList()) {
listFmt =
cursor.currentList()-&
gt;format();
}
else
{
listFmt.setIndent(blockFmt.indent() +
1
);
blockFmt.setIndent(0
);
cursor.setBlockFormat(blockFmt);
}
listFmt.setStyle(style);
cursor.createList(listFmt);
}
cursor.endEditBlock();
}
void
TextEdit::
textColor()
{
QColor col =
QColorDialog::
getColor(textEdit-&
gt;textColor(), this
);
if
(!
col.isValid())
return
;
QTextCharFormat fmt;
fmt.setForeground(col);
mergeFormatOnWordOrSelection(fmt);
colorChanged(col);
}
void
TextEdit::
textAlign(QAction *
a)
{
if
(a ==
actionAlignLeft)
textEdit-&
gt;setAlignment(Qt::
AlignLeft |
Qt::
AlignAbsolute);
else
if
(a ==
actionAlignCenter)
textEdit-&
gt;setAlignment(Qt::
AlignHCenter);
else
if
(a ==
actionAlignRight)
textEdit-&
gt;setAlignment(Qt::
AlignRight |
Qt::
AlignAbsolute);
else
if
(a ==
actionAlignJustify)
textEdit-&
gt;setAlignment(Qt::
AlignJustify);
}
void
TextEdit::
currentCharFormatChanged(const
QTextCharFormat &
amp;format)
{
fontChanged(format.font());
colorChanged(format.foreground().color());
}
void
TextEdit::
cursorPositionChanged()
{
alignmentChanged(textEdit-&
gt;alignment());
QTextList *
list =
textEdit-&
gt;textCursor().currentList();
if
(list) {
switch
(list-&
gt;format().style()) {
case
QTextListFormat::
ListDisc:
comboStyle-&
gt;setCurrentIndex(1
);
break
;
case
QTextListFormat::
ListCircle:
comboStyle-&
gt;setCurrentIndex(2
);
break
;
case
QTextListFormat::
ListSquare:
comboStyle-&
gt;setCurrentIndex(3
);
break
;
case
QTextListFormat::
ListDecimal:
comboStyle-&
gt;setCurrentIndex(4
);
break
;
case
QTextListFormat::
ListLowerAlpha:
comboStyle-&
gt;setCurrentIndex(5
);
break
;
case
QTextListFormat::
ListUpperAlpha:
comboStyle-&
gt;setCurrentIndex(6
);
break
;
case
QTextListFormat::
ListLowerRoman:
comboStyle-&
gt;setCurrentIndex(7
);
break
;
case
QTextListFormat::
ListUpperRoman:
comboStyle-&
gt;setCurrentIndex(8
);
break
;
default
:
comboStyle-&
gt;setCurrentIndex(-
1
);
break
;
}
}
else
{
int
headingLevel =
textEdit-&
gt;textCursor().blockFormat().headingLevel();
comboStyle-&
gt;setCurrentIndex(headingLevel ? headingLevel +
8
: 0
);
}
}
void
TextEdit::
clipboardDataChanged()
{
#ifndef QT_NO_CLIPBOARD
if
(const
QMimeData *
md =
QApplication::
clipboard()-&
gt;mimeData())
actionPaste-&
gt;setEnabled(md-&
gt;hasText());
#endif
}
void
TextEdit::
about()
{
QMessageBox::
about(this
, tr("About"
), tr("This example demonstrates Qt's "
"rich text editing facilities in action, providing an example "
"document for you to experiment with."
));
}
void
TextEdit::
mergeFormatOnWordOrSelection(const
QTextCharFormat &
amp;format)
{
QTextCursor cursor =
textEdit-&
gt;textCursor();
if
(!
cursor.hasSelection())
cursor.select(QTextCursor::
WordUnderCursor);
cursor.mergeCharFormat(format);
textEdit-&
gt;mergeCurrentCharFormat(format);
}
void
TextEdit::
fontChanged(const
QFont &
amp;f)
{
comboFont-&
gt;setCurrentIndex(comboFont-&
gt;findText(QFontInfo(f).family()));
comboSize-&
gt;setCurrentIndex(comboSize-&
gt;findText(QString::
number(f.pointSize())));
actionTextBold-&
gt;setChecked(f.bold());
actionTextItalic-&
gt;setChecked(f.italic());
actionTextUnderline-&
gt;setChecked(f.underline());
}
void
TextEdit::
colorChanged(const
QColor &
amp;c)
{
QPixmap pix(16
, 16
);
pix.fill(c);
actionTextColor-&
gt;setIcon(pix);
}
void
TextEdit::
alignmentChanged(Qt::
Alignment a)
{
if
(a &
amp; Qt::
AlignLeft)
actionAlignLeft-&
gt;setChecked(true
);
else
if
(a &
amp; Qt::
AlignHCenter)
actionAlignCenter-&
gt;setChecked(true
);
else
if
(a &
amp; Qt::
AlignRight)
actionAlignRight-&
gt;setChecked(true
);
else
if
(a &
amp; Qt::
AlignJustify)
actionAlignJustify-&
gt;setChecked(true
);
}