2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the demonstration applications of the Qt Toolkit.
|
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-18 10:30:20 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** BSD License Usage
|
|
|
|
** Alternatively, you may use this file under the terms of the BSD license
|
|
|
|
** as follows:
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-18 10:30:20 +00:00
|
|
|
** "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."
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $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>
|
2015-08-25 12:19:37 +00:00
|
|
|
#include <QStatusBar>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QToolBar>
|
|
|
|
#include <QTextCursor>
|
|
|
|
#include <QTextDocumentWriter>
|
|
|
|
#include <QTextList>
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QCloseEvent>
|
|
|
|
#include <QMessageBox>
|
2011-09-28 18:30:25 +00:00
|
|
|
#include <QMimeData>
|
2012-06-13 10:59:54 +00:00
|
|
|
#ifndef QT_NO_PRINTER
|
|
|
|
#include <QPrintDialog>
|
|
|
|
#include <QPrinter>
|
|
|
|
#include <QPrintPreviewDialog>
|
|
|
|
#endif
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-11-23 12:36:40 +00:00
|
|
|
#include "textedit.h"
|
|
|
|
|
2011-10-18 07:54:31 +00:00
|
|
|
#ifdef Q_OS_MAC
|
2011-04-27 10:05:43 +00:00
|
|
|
const QString rsrcPath = ":/images/mac";
|
|
|
|
#else
|
|
|
|
const QString rsrcPath = ":/images/win";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TextEdit::TextEdit(QWidget *parent)
|
|
|
|
: QMainWindow(parent)
|
|
|
|
{
|
2014-05-06 03:49:21 +00:00
|
|
|
#ifdef Q_OS_OSX
|
|
|
|
setUnifiedTitleAndToolBarOnMac(true);
|
|
|
|
#endif
|
2015-08-25 12:19:37 +00:00
|
|
|
setWindowTitle(QCoreApplication::applicationName());
|
|
|
|
|
|
|
|
textEdit = new QTextEdit(this);
|
|
|
|
connect(textEdit, &QTextEdit::currentCharFormatChanged,
|
|
|
|
this, &TextEdit::currentCharFormatChanged);
|
|
|
|
connect(textEdit, &QTextEdit::cursorPositionChanged,
|
|
|
|
this, &TextEdit::cursorPositionChanged);
|
|
|
|
setCentralWidget(textEdit);
|
2014-05-06 03:49:21 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
setToolButtonStyle(Qt::ToolButtonFollowStyle);
|
|
|
|
setupFileActions();
|
|
|
|
setupEditActions();
|
|
|
|
setupTextActions();
|
|
|
|
|
|
|
|
{
|
2015-08-25 12:19:37 +00:00
|
|
|
QMenu *helpMenu = menuBar()->addMenu(tr("Help"));
|
|
|
|
helpMenu->addAction(tr("About"), this, &TextEdit::about);
|
|
|
|
helpMenu->addAction(tr("About &Qt"), qApp, &QApplication::aboutQt);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
Font Database: Add support for private, system UI font families
We introduce QPlatformFontDatabase::isPrivateFontFamily() to allow
testing for private, system UI font families. Both QFontComboBox
and QFontDialog need to filter out those private font families
which, by definition, should be hidden from the end user.
(The textedit example had to be updated to fix the issue where the
default font would be private. In 5.4, we will be adding an equivalent,
public API in QFontDatabase, and a better solution for the textedit
example and QTexEdit in general).
In particular, on OS X and iOS, private fonts are used for the system
UI font. Those have their font family name prefixed by a dot.
QCoreTextFontDatabase knows about this, and makes sure those are
tested positive as private font families. In order to have a cleaner
layer separation, we moved the QPA theme font resolution from the
platform theme classes into QCoreTextFontDatabase for both Cocoa and
iOS QPA plugins.
In both cases, we use CoreText's CTFontCreateUIFontForLanguage(), that
nicely maps to the HITheme API we were using so far on Mac. That means
one HITheme dependency less. We also cache the font descriptors we get
for these font for each time QCTFD::populateFamilies() gets called.
(While not common, this currently happens in auto-tests, like
tst_QFontDatabase, and could happen in actual applications -- specially
when adding and removing application fonts.)
Change-Id: Ic6f0b60f9f597afee1a43596a669742dc546b97f
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
2014-07-02 11:18:50 +00:00
|
|
|
QFont textFont("Helvetica");
|
|
|
|
textFont.setStyleHint(QFont::SansSerif);
|
|
|
|
textEdit->setFont(textFont);
|
2011-04-27 10:05:43 +00:00
|
|
|
fontChanged(textEdit->font());
|
|
|
|
colorChanged(textEdit->textColor());
|
|
|
|
alignmentChanged(textEdit->alignment());
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
connect(textEdit->document(), &QTextDocument::modificationChanged,
|
|
|
|
actionSave, &QAction::setEnabled);
|
|
|
|
connect(textEdit->document(), &QTextDocument::modificationChanged,
|
|
|
|
this, &QWidget::setWindowModified);
|
|
|
|
connect(textEdit->document(), &QTextDocument::undoAvailable,
|
|
|
|
actionUndo, &QAction::setEnabled);
|
|
|
|
connect(textEdit->document(), &QTextDocument::redoAvailable,
|
|
|
|
actionRedo, &QAction::setEnabled);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
setWindowModified(textEdit->document()->isModified());
|
|
|
|
actionSave->setEnabled(textEdit->document()->isModified());
|
|
|
|
actionUndo->setEnabled(textEdit->document()->isUndoAvailable());
|
|
|
|
actionRedo->setEnabled(textEdit->document()->isRedoAvailable());
|
|
|
|
|
2015-10-19 12:03:38 +00:00
|
|
|
#ifndef QT_NO_CLIPBOARD
|
2011-04-27 10:05:43 +00:00
|
|
|
actionCut->setEnabled(false);
|
|
|
|
actionCopy->setEnabled(false);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
connect(QApplication::clipboard(), &QClipboard::dataChanged, this, &TextEdit::clipboardDataChanged);
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
textEdit->setFocus();
|
|
|
|
setCurrentFileName(QString());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::closeEvent(QCloseEvent *e)
|
|
|
|
{
|
|
|
|
if (maybeSave())
|
|
|
|
e->accept();
|
|
|
|
else
|
|
|
|
e->ignore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::setupFileActions()
|
|
|
|
{
|
2015-08-25 12:19:37 +00:00
|
|
|
QToolBar *tb = addToolBar(tr("File Actions"));
|
|
|
|
QMenu *menu = menuBar()->addMenu(tr("&File"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon newIcon = QIcon::fromTheme("document-new", QIcon(rsrcPath + "/filenew.png"));
|
|
|
|
QAction *a = menu->addAction(newIcon, tr("&New"), this, &TextEdit::fileNew);
|
|
|
|
tb->addAction(a);
|
2011-04-27 10:05:43 +00:00
|
|
|
a->setPriority(QAction::LowPriority);
|
|
|
|
a->setShortcut(QKeySequence::New);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png"));
|
|
|
|
a = menu->addAction(openIcon, tr("&Open..."), this, &TextEdit::fileOpen);
|
2011-04-27 10:05:43 +00:00
|
|
|
a->setShortcut(QKeySequence::Open);
|
|
|
|
tb->addAction(a);
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon saveIcon = QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png"));
|
|
|
|
actionSave = menu->addAction(saveIcon, tr("&Save"), this, &TextEdit::fileSave);
|
|
|
|
actionSave->setShortcut(QKeySequence::Save);
|
|
|
|
actionSave->setEnabled(false);
|
|
|
|
tb->addAction(actionSave);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
a = menu->addAction(tr("Save &As..."), this, &TextEdit::fileSaveAs);
|
2011-04-27 10:05:43 +00:00
|
|
|
a->setPriority(QAction::LowPriority);
|
|
|
|
menu->addSeparator();
|
|
|
|
|
|
|
|
#ifndef QT_NO_PRINTER
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon printIcon = QIcon::fromTheme("document-print", QIcon(rsrcPath + "/fileprint.png"));
|
|
|
|
a = menu->addAction(printIcon, tr("&Print..."), this, &TextEdit::filePrint);
|
2013-03-14 23:42:15 +00:00
|
|
|
a->setPriority(QAction::LowPriority);
|
2011-04-27 10:05:43 +00:00
|
|
|
a->setShortcut(QKeySequence::Print);
|
|
|
|
tb->addAction(a);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon filePrintIcon = QIcon::fromTheme("fileprint", QIcon(rsrcPath + "/fileprint.png"));
|
|
|
|
menu->addAction(filePrintIcon, tr("Print Preview..."), this, &TextEdit::filePrintPreview);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon exportPdfIcon = QIcon::fromTheme("exportpdf", QIcon(rsrcPath + "/exportpdf.png"));
|
|
|
|
a = menu->addAction(exportPdfIcon, tr("&Export PDF..."), this, &TextEdit::filePrintPdf);
|
2011-04-27 10:05:43 +00:00
|
|
|
a->setPriority(QAction::LowPriority);
|
|
|
|
a->setShortcut(Qt::CTRL + Qt::Key_D);
|
|
|
|
tb->addAction(a);
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
#endif
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
a = menu->addAction(tr("&Quit"), this, &QWidget::close);
|
2011-04-27 10:05:43 +00:00
|
|
|
a->setShortcut(Qt::CTRL + Qt::Key_Q);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::setupEditActions()
|
|
|
|
{
|
2015-08-25 12:19:37 +00:00
|
|
|
QToolBar *tb = addToolBar(tr("Edit Actions"));
|
|
|
|
QMenu *menu = menuBar()->addMenu(tr("&Edit"));
|
|
|
|
|
|
|
|
const QIcon undoIcon = QIcon::fromTheme("edit-undo", QIcon(rsrcPath + "/editundo.png"));
|
|
|
|
actionUndo = menu->addAction(undoIcon, tr("&Undo"), textEdit, &QTextEdit::undo);
|
|
|
|
actionUndo->setShortcut(QKeySequence::Undo);
|
|
|
|
tb->addAction(actionUndo);
|
|
|
|
|
|
|
|
const QIcon redoIcon = QIcon::fromTheme("edit-redo", QIcon(rsrcPath + "/editredo.png"));
|
|
|
|
actionRedo = menu->addAction(redoIcon, tr("&Redo"), textEdit, &QTextEdit::redo);
|
|
|
|
actionRedo->setPriority(QAction::LowPriority);
|
|
|
|
actionRedo->setShortcut(QKeySequence::Redo);
|
|
|
|
tb->addAction(actionRedo);
|
2011-04-27 10:05:43 +00:00
|
|
|
menu->addSeparator();
|
2015-08-25 12:19:37 +00:00
|
|
|
|
2015-10-19 12:03:38 +00:00
|
|
|
#ifndef QT_NO_CLIPBOARD
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon cutIcon = QIcon::fromTheme("edit-cut", QIcon(rsrcPath + "/editcut.png"));
|
|
|
|
actionCut = menu->addAction(cutIcon, tr("Cu&t"), textEdit, &QTextEdit::cut);
|
|
|
|
actionCut->setPriority(QAction::LowPriority);
|
|
|
|
actionCut->setShortcut(QKeySequence::Cut);
|
|
|
|
tb->addAction(actionCut);
|
|
|
|
|
|
|
|
const QIcon copyIcon = QIcon::fromTheme("edit-copy", QIcon(rsrcPath + "/editcopy.png"));
|
|
|
|
actionCopy = menu->addAction(copyIcon, tr("&Copy"), textEdit, &QTextEdit::copy);
|
|
|
|
actionCopy->setPriority(QAction::LowPriority);
|
|
|
|
actionCopy->setShortcut(QKeySequence::Copy);
|
|
|
|
tb->addAction(actionCopy);
|
|
|
|
|
|
|
|
const QIcon pasteIcon = QIcon::fromTheme("edit-paste", QIcon(rsrcPath + "/editpaste.png"));
|
|
|
|
actionPaste = menu->addAction(pasteIcon, tr("&Paste"), textEdit, &QTextEdit::paste);
|
|
|
|
actionPaste->setPriority(QAction::LowPriority);
|
|
|
|
actionPaste->setShortcut(QKeySequence::Paste);
|
|
|
|
tb->addAction(actionPaste);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (const QMimeData *md = QApplication::clipboard()->mimeData())
|
|
|
|
actionPaste->setEnabled(md->hasText());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::setupTextActions()
|
|
|
|
{
|
2015-08-25 12:19:37 +00:00
|
|
|
QToolBar *tb = addToolBar(tr("Format Actions"));
|
|
|
|
QMenu *menu = menuBar()->addMenu(tr("F&ormat"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon boldIcon = QIcon::fromTheme("format-text-bold", QIcon(rsrcPath + "/textbold.png"));
|
|
|
|
actionTextBold = menu->addAction(boldIcon, tr("&Bold"), this, &TextEdit::textBold);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionTextBold->setShortcut(Qt::CTRL + Qt::Key_B);
|
|
|
|
actionTextBold->setPriority(QAction::LowPriority);
|
2014-01-13 14:48:44 +00:00
|
|
|
QFont bold;
|
2011-04-27 10:05:43 +00:00
|
|
|
bold.setBold(true);
|
|
|
|
actionTextBold->setFont(bold);
|
|
|
|
tb->addAction(actionTextBold);
|
|
|
|
actionTextBold->setCheckable(true);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon italicIcon = QIcon::fromTheme("format-text-italic", QIcon(rsrcPath + "/textitalic.png"));
|
|
|
|
actionTextItalic = menu->addAction(italicIcon, tr("&Italic"), this, &TextEdit::textItalic);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionTextItalic->setPriority(QAction::LowPriority);
|
|
|
|
actionTextItalic->setShortcut(Qt::CTRL + Qt::Key_I);
|
|
|
|
QFont italic;
|
|
|
|
italic.setItalic(true);
|
|
|
|
actionTextItalic->setFont(italic);
|
|
|
|
tb->addAction(actionTextItalic);
|
|
|
|
actionTextItalic->setCheckable(true);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon underlineIcon = QIcon::fromTheme("format-text-underline", QIcon(rsrcPath + "/textunder.png"));
|
|
|
|
actionTextUnderline = menu->addAction(underlineIcon, tr("&Underline"), this, &TextEdit::textUnderline);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionTextUnderline->setShortcut(Qt::CTRL + Qt::Key_U);
|
|
|
|
actionTextUnderline->setPriority(QAction::LowPriority);
|
|
|
|
QFont underline;
|
|
|
|
underline.setUnderline(true);
|
|
|
|
actionTextUnderline->setFont(underline);
|
|
|
|
tb->addAction(actionTextUnderline);
|
|
|
|
actionTextUnderline->setCheckable(true);
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon leftIcon = QIcon::fromTheme("format-justify-left", QIcon(rsrcPath + "/textleft.png"));
|
|
|
|
actionAlignLeft = new QAction(leftIcon, tr("&Left"), this);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignLeft->setShortcut(Qt::CTRL + Qt::Key_L);
|
|
|
|
actionAlignLeft->setCheckable(true);
|
|
|
|
actionAlignLeft->setPriority(QAction::LowPriority);
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon centerIcon = QIcon::fromTheme("format-justify-center", QIcon(rsrcPath + "/textcenter.png"));
|
|
|
|
actionAlignCenter = new QAction(centerIcon, tr("C&enter"), this);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignCenter->setShortcut(Qt::CTRL + Qt::Key_E);
|
|
|
|
actionAlignCenter->setCheckable(true);
|
|
|
|
actionAlignCenter->setPriority(QAction::LowPriority);
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon rightIcon = QIcon::fromTheme("format-justify-right", QIcon(rsrcPath + "/textright.png"));
|
|
|
|
actionAlignRight = new QAction(rightIcon, tr("&Right"), this);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignRight->setShortcut(Qt::CTRL + Qt::Key_R);
|
|
|
|
actionAlignRight->setCheckable(true);
|
|
|
|
actionAlignRight->setPriority(QAction::LowPriority);
|
2015-08-25 12:19:37 +00:00
|
|
|
const QIcon fillIcon = QIcon::fromTheme("format-justify-fill", QIcon(rsrcPath + "/textjustify.png"));
|
|
|
|
actionAlignJustify = new QAction(fillIcon, tr("&Justify"), this);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignJustify->setShortcut(Qt::CTRL + Qt::Key_J);
|
|
|
|
actionAlignJustify->setCheckable(true);
|
|
|
|
actionAlignJustify->setPriority(QAction::LowPriority);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
// Make sure the alignLeft is always left of the alignRight
|
|
|
|
QActionGroup *alignGroup = new QActionGroup(this);
|
|
|
|
connect(alignGroup, &QActionGroup::triggered, this, &TextEdit::textAlign);
|
|
|
|
|
|
|
|
if (QApplication::isLeftToRight()) {
|
|
|
|
alignGroup->addAction(actionAlignLeft);
|
|
|
|
alignGroup->addAction(actionAlignCenter);
|
|
|
|
alignGroup->addAction(actionAlignRight);
|
|
|
|
} else {
|
|
|
|
alignGroup->addAction(actionAlignRight);
|
|
|
|
alignGroup->addAction(actionAlignCenter);
|
|
|
|
alignGroup->addAction(actionAlignLeft);
|
|
|
|
}
|
|
|
|
alignGroup->addAction(actionAlignJustify);
|
|
|
|
|
|
|
|
tb->addActions(alignGroup->actions());
|
|
|
|
menu->addActions(alignGroup->actions());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
|
|
|
QPixmap pix(16, 16);
|
|
|
|
pix.fill(Qt::black);
|
2015-08-25 12:19:37 +00:00
|
|
|
actionTextColor = menu->addAction(pix, tr("&Color..."), this, &TextEdit::textColor);
|
2011-04-27 10:05:43 +00:00
|
|
|
tb->addAction(actionTextColor);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
tb = addToolBar(tr("Format Actions"));
|
2011-04-27 10:05:43 +00:00
|
|
|
tb->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
|
|
|
|
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)");
|
|
|
|
|
2016-12-20 18:37:37 +00:00
|
|
|
connect(comboStyle, QOverload<int>::of(&QComboBox::activated), this, &TextEdit::textStyle);
|
2015-08-25 12:19:37 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
comboFont = new QFontComboBox(tb);
|
|
|
|
tb->addWidget(comboFont);
|
2016-12-20 18:37:37 +00:00
|
|
|
connect(comboFont, QOverload<const QString &>::of(&QComboBox::activated), this, &TextEdit::textFamily);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
comboSize = new QComboBox(tb);
|
|
|
|
comboSize->setObjectName("comboSize");
|
|
|
|
tb->addWidget(comboSize);
|
|
|
|
comboSize->setEditable(true);
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
const QList<int> standardSizes = QFontDatabase::standardSizes();
|
|
|
|
foreach (int size, standardSizes)
|
2011-04-27 10:05:43 +00:00
|
|
|
comboSize->addItem(QString::number(size));
|
2015-08-25 12:19:37 +00:00
|
|
|
comboSize->setCurrentIndex(standardSizes.indexOf(QApplication::font().pointSize()));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-12-20 18:37:37 +00:00
|
|
|
connect(comboSize, QOverload<const QString &>::of(&QComboBox::activated), this, &TextEdit::textSize);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
QTextCodec *codec = Qt::codecForHtml(data);
|
|
|
|
QString str = codec->toUnicode(data);
|
|
|
|
if (Qt::mightBeRichText(str)) {
|
|
|
|
textEdit->setHtml(str);
|
|
|
|
} else {
|
|
|
|
str = QString::fromLocal8Bit(data);
|
|
|
|
textEdit->setPlainText(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentFileName(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextEdit::maybeSave()
|
|
|
|
{
|
|
|
|
if (!textEdit->document()->isModified())
|
|
|
|
return true;
|
2013-08-27 10:03:42 +00:00
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
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);
|
2011-04-27 10:05:43 +00:00
|
|
|
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();
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
setWindowTitle(tr("%1[*] - %2").arg(shownName, QCoreApplication::applicationName()));
|
2011-04-27 10:05:43 +00:00
|
|
|
setWindowModified(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::fileNew()
|
|
|
|
{
|
|
|
|
if (maybeSave()) {
|
|
|
|
textEdit->clear();
|
|
|
|
setCurrentFileName(QString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::fileOpen()
|
|
|
|
{
|
2015-08-25 12:19:37 +00:00
|
|
|
QFileDialog fileDialog(this, tr("Open File..."));
|
|
|
|
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
|
|
|
|
fileDialog.setFileMode(QFileDialog::ExistingFile);
|
|
|
|
fileDialog.setMimeTypeFilters(QStringList() << "text/html" << "text/plain");
|
|
|
|
if (fileDialog.exec() != QDialog::Accepted)
|
|
|
|
return;
|
|
|
|
const QString fn = fileDialog.selectedFiles().first();
|
|
|
|
if (load(fn))
|
|
|
|
statusBar()->showMessage(tr("Opened \"%1\"").arg(QDir::toNativeSeparators(fn)));
|
|
|
|
else
|
|
|
|
statusBar()->showMessage(tr("Could not open \"%1\"").arg(QDir::toNativeSeparators(fn)));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextEdit::fileSave()
|
|
|
|
{
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
return fileSaveAs();
|
2013-08-27 10:03:42 +00:00
|
|
|
if (fileName.startsWith(QStringLiteral(":/")))
|
|
|
|
return fileSaveAs();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QTextDocumentWriter writer(fileName);
|
|
|
|
bool success = writer.write(textEdit->document());
|
2015-08-25 12:19:37 +00:00
|
|
|
if (success) {
|
2011-04-27 10:05:43 +00:00
|
|
|
textEdit->document()->setModified(false);
|
2015-08-25 12:19:37 +00:00
|
|
|
statusBar()->showMessage(tr("Wrote \"%1\"").arg(QDir::toNativeSeparators(fileName)));
|
|
|
|
} else {
|
|
|
|
statusBar()->showMessage(tr("Could not write to file \"%1\"")
|
|
|
|
.arg(QDir::toNativeSeparators(fileName)));
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextEdit::fileSaveAs()
|
|
|
|
{
|
2015-08-25 12:19:37 +00:00
|
|
|
QFileDialog fileDialog(this, tr("Save as..."));
|
|
|
|
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
|
|
|
|
QStringList mimeTypes;
|
|
|
|
mimeTypes << "application/vnd.oasis.opendocument.text" << "text/html" << "text/plain";
|
|
|
|
fileDialog.setMimeTypeFilters(mimeTypes);
|
|
|
|
fileDialog.setDefaultSuffix("odt");
|
|
|
|
if (fileDialog.exec() != QDialog::Accepted)
|
2011-04-27 10:05:43 +00:00
|
|
|
return false;
|
2015-08-25 12:19:37 +00:00
|
|
|
const QString fn = fileDialog.selectedFiles().first();
|
2011-04-27 10:05:43 +00:00
|
|
|
setCurrentFileName(fn);
|
|
|
|
return fileSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::filePrint()
|
|
|
|
{
|
2013-02-18 14:59:14 +00:00
|
|
|
#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
|
2011-04-27 10:05:43 +00:00
|
|
|
QPrinter printer(QPrinter::HighResolution);
|
|
|
|
QPrintDialog *dlg = new QPrintDialog(&printer, this);
|
|
|
|
if (textEdit->textCursor().hasSelection())
|
|
|
|
dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
|
|
|
|
dlg->setWindowTitle(tr("Print Document"));
|
2012-11-23 12:36:40 +00:00
|
|
|
if (dlg->exec() == QDialog::Accepted)
|
2011-04-27 10:05:43 +00:00
|
|
|
textEdit->print(&printer);
|
|
|
|
delete dlg;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::filePrintPreview()
|
|
|
|
{
|
2013-02-18 14:59:14 +00:00
|
|
|
#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
|
2011-04-27 10:05:43 +00:00
|
|
|
QPrinter printer(QPrinter::HighResolution);
|
|
|
|
QPrintPreviewDialog preview(&printer, this);
|
2015-08-25 12:19:37 +00:00
|
|
|
connect(&preview, &QPrintPreviewDialog::paintRequested, this, &TextEdit::printPreview);
|
2011-04-27 10:05:43 +00:00
|
|
|
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]
|
2015-08-25 12:19:37 +00:00
|
|
|
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->document()->print(&printer);
|
|
|
|
statusBar()->showMessage(tr("Exported \"%1\"")
|
|
|
|
.arg(QDir::toNativeSeparators(fileName)));
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [0]
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::textBold()
|
|
|
|
{
|
|
|
|
QTextCharFormat fmt;
|
|
|
|
fmt.setFontWeight(actionTextBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::textUnderline()
|
|
|
|
{
|
|
|
|
QTextCharFormat fmt;
|
|
|
|
fmt.setFontUnderline(actionTextUnderline->isChecked());
|
|
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::textItalic()
|
|
|
|
{
|
|
|
|
QTextCharFormat fmt;
|
|
|
|
fmt.setFontItalic(actionTextItalic->isChecked());
|
|
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::textFamily(const QString &f)
|
|
|
|
{
|
|
|
|
QTextCharFormat fmt;
|
|
|
|
fmt.setFontFamily(f);
|
|
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::textSize(const QString &p)
|
|
|
|
{
|
|
|
|
qreal pointSize = p.toFloat();
|
|
|
|
if (p.toFloat() > 0) {
|
|
|
|
QTextCharFormat fmt;
|
|
|
|
fmt.setFontPointSize(pointSize);
|
|
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(-1);
|
|
|
|
cursor.mergeBlockFormat(bfmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::textColor()
|
|
|
|
{
|
|
|
|
QColor col = QColorDialog::getColor(textEdit->textColor(), this);
|
|
|
|
if (!col.isValid())
|
|
|
|
return;
|
|
|
|
QTextCharFormat fmt;
|
|
|
|
fmt.setForeground(col);
|
|
|
|
mergeFormatOnWordOrSelection(fmt);
|
|
|
|
colorChanged(col);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (const QMimeData *md = QApplication::clipboard()->mimeData())
|
|
|
|
actionPaste->setEnabled(md->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 &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)
|
|
|
|
{
|
2012-11-23 12:36:40 +00:00
|
|
|
if (a & Qt::AlignLeft)
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignLeft->setChecked(true);
|
2012-11-23 12:36:40 +00:00
|
|
|
else if (a & Qt::AlignHCenter)
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignCenter->setChecked(true);
|
2012-11-23 12:36:40 +00:00
|
|
|
else if (a & Qt::AlignRight)
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignRight->setChecked(true);
|
2012-11-23 12:36:40 +00:00
|
|
|
else if (a & Qt::AlignJustify)
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignJustify->setChecked(true);
|
|
|
|
}
|
|
|
|
|