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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-18 16:02:11 +00:00
|
|
|
#include <QActionGroup>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QColorDialog>
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QFontComboBox>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QFontDatabase>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QMenuBar>
|
|
|
|
#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>
|
2017-12-18 10:28:33 +00:00
|
|
|
#include <QMimeDatabase>
|
2017-05-29 15:35:58 +00:00
|
|
|
#if defined(QT_PRINTSUPPORT_LIB)
|
|
|
|
#include <QtPrintSupport/qtprintsupportglobal.h>
|
|
|
|
#if QT_CONFIG(printer)
|
|
|
|
#if QT_CONFIG(printdialog)
|
2012-06-13 10:59:54 +00:00
|
|
|
#include <QPrintDialog>
|
2017-05-29 15:35:58 +00:00
|
|
|
#endif
|
2012-06-13 10:59:54 +00:00
|
|
|
#include <QPrinter>
|
2017-05-29 15:35:58 +00:00
|
|
|
#if QT_CONFIG(printpreviewdialog)
|
2012-06-13 10:59:54 +00:00
|
|
|
#include <QPrintPreviewDialog>
|
|
|
|
#endif
|
2017-05-29 15:35:58 +00:00
|
|
|
#endif
|
|
|
|
#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)
|
|
|
|
{
|
2020-04-03 10:45:36 +00:00
|
|
|
#ifdef Q_OS_MACOS
|
2014-05-06 03:49:21 +00:00
|
|
|
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);
|
2018-04-10 00:07:03 +00:00
|
|
|
connect(textEdit, &QTextEdit::copyAvailable, actionCut, &QAction::setEnabled);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionCopy->setEnabled(false);
|
2018-04-10 00:07:03 +00:00
|
|
|
connect(textEdit, &QTextEdit::copyAvailable, actionCopy, &QAction::setEnabled);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
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());
|
2018-09-12 09:50:16 +00:00
|
|
|
|
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
// Use dark text on light background on macOS, also in dark mode.
|
|
|
|
QPalette pal = textEdit->palette();
|
|
|
|
pal.setColor(QPalette::Base, QColor(Qt::white));
|
|
|
|
pal.setColor(QPalette::Text, QColor(Qt::black));
|
|
|
|
textEdit->setPalette(pal);
|
|
|
|
#endif
|
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();
|
|
|
|
|
2020-03-05 09:06:24 +00:00
|
|
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
a->setShortcut(Qt::CTRL | Qt::Key_D);
|
2011-04-27 10:05:43 +00:00
|
|
|
tb->addAction(a);
|
|
|
|
|
|
|
|
menu->addSeparator();
|
|
|
|
#endif
|
|
|
|
|
2015-08-25 12:19:37 +00:00
|
|
|
a = menu->addAction(tr("&Quit"), this, &QWidget::close);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
a->setShortcut(Qt::CTRL | Qt::Key_Q);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionTextBold->setShortcut(Qt::CTRL | Qt::Key_B);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionTextItalic->setShortcut(Qt::CTRL | Qt::Key_I);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionTextUnderline->setShortcut(Qt::CTRL | Qt::Key_U);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionAlignLeft->setShortcut(Qt::CTRL | Qt::Key_L);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionAlignCenter->setShortcut(Qt::CTRL | Qt::Key_E);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionAlignRight->setShortcut(Qt::CTRL | Qt::Key_R);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionAlignJustify->setShortcut(Qt::CTRL | Qt::Key_J);
|
2011-04-27 10:05:43 +00:00
|
|
|
actionAlignJustify->setCheckable(true);
|
|
|
|
actionAlignJustify->setPriority(QAction::LowPriority);
|
2019-05-04 06:48:41 +00:00
|
|
|
const QIcon indentMoreIcon = QIcon::fromTheme("format-indent-more", QIcon(rsrcPath + "/format-indent-more.png"));
|
|
|
|
actionIndentMore = menu->addAction(indentMoreIcon, tr("&Indent"), this, &TextEdit::indent);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionIndentMore->setShortcut(Qt::CTRL | Qt::Key_BracketRight);
|
2019-05-04 06:48:41 +00:00
|
|
|
actionIndentMore->setPriority(QAction::LowPriority);
|
|
|
|
const QIcon indentLessIcon = QIcon::fromTheme("format-indent-less", QIcon(rsrcPath + "/format-indent-less.png"));
|
|
|
|
actionIndentLess = menu->addAction(indentLessIcon, tr("&Unindent"), this, &TextEdit::unindent);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionIndentLess->setShortcut(Qt::CTRL | Qt::Key_BracketLeft);
|
2019-05-04 06:48:41 +00:00
|
|
|
actionIndentLess->setPriority(QAction::LowPriority);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
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());
|
2019-05-04 06:48:41 +00:00
|
|
|
tb->addAction(actionIndentMore);
|
|
|
|
tb->addAction(actionIndentLess);
|
|
|
|
menu->addAction(actionIndentMore);
|
|
|
|
menu->addAction(actionIndentLess);
|
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);
|
|
|
|
|
2019-02-18 14:40:04 +00:00
|
|
|
menu->addSeparator();
|
|
|
|
|
|
|
|
const QIcon checkboxIcon = QIcon::fromTheme("status-checkbox-checked", QIcon(rsrcPath + "/checkbox-checked.png"));
|
|
|
|
actionToggleCheckState = menu->addAction(checkboxIcon, tr("Chec&ked"), this, &TextEdit::setChecked);
|
Long live QKeyCombination!
C++20 via P1120 is deprecating arithmetic operations between
unrelated enumeration types, and GCC 10 is already complaining.
Hence, these operations might become illegal in C++23 or C++26 at
the latest.
A case of this that affects Qt is in key combinations: a
QKeySequence can be constructed by summing / ORing modifiers and a
key, for instance:
Qt::CTRL + Qt::Key_A
Qt::SHIFT | Qt::CTRL | Qt::Key_G (recommended, see below)
The problem is that the modifiers and the key belong to different
enumerations (and there's 2 enumerations for the modifier, and one
for the key).
To solve this: add a dedicated class to represent a combination of
keys, and operators between those enumerations to build instances
of this class.
I would've simply defined operator|, but again docs and pre-existing
code use operator+ as well, so added both to at least tackle simple
cases (modifier + key).
Multiple modifiers create a problem: operator+ between them yields
int, not the corresponding flags type (because operator+ is not
overloaded for this use case):
Qt::CTRL + Qt::SHIFT + Qt::Key_A
\__________________/ /
int /
\______________/
int
Not only this loses track of the datatypes involved, but it would
also then "add" the key (with NO warnings, now its int + enum, so
it's not mixing enums!) and yielding int again.
I don't want to special-case this; the point of the class is
that int is the wrong datatype. Everything works just fine when
using operator| instead:
Qt::CTRL | Qt::SHIFT | Qt::Key_A
\__________________/ /
Qt::Modifiers /
\______________/
QKeyCombination
So I'm defining operator+ so that the simple cases still work,
but also deprecating it.
Port some code around Qt to the new class. In certain cases,
it's a huge win for clarity. In some others, I've just added
the necessary casts to make it still compile without warnings,
without attempting refactorings.
[ChangeLog][QtCore][QKeyCombination] New class to represent
a combination of a key and zero or more modifiers, to be used
when defining shortcuts or similar.
[ChangeLog][Potentially Source-Incompatible Changes] A keyboard
modifier (such as Qt::CTRL, Qt::AltModifier, etc.) should be
combined with a key (such as Qt::Key_A, Qt::Key_F1, etc.) by using
operator|, not operator+. The result is now an object of type
QKeyCombination, that stores the key and the modifiers.
Change-Id: I657a3a328232f059023fff69c5031ee31cc91dd6
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2020-04-19 17:56:18 +00:00
|
|
|
actionToggleCheckState->setShortcut(Qt::CTRL | Qt::Key_K);
|
2019-02-18 14:40:04 +00:00
|
|
|
actionToggleCheckState->setCheckable(true);
|
|
|
|
actionToggleCheckState->setPriority(QAction::LowPriority);
|
|
|
|
tb->addAction(actionToggleCheckState);
|
|
|
|
|
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)");
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->addItem("Task List (Unchecked)");
|
|
|
|
comboStyle->addItem("Task List (Checked)");
|
2011-04-27 10:05:43 +00:00
|
|
|
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)");
|
2017-12-18 12:05:20 +00:00
|
|
|
comboStyle->addItem("Heading 1");
|
|
|
|
comboStyle->addItem("Heading 2");
|
|
|
|
comboStyle->addItem("Heading 3");
|
|
|
|
comboStyle->addItem("Heading 4");
|
|
|
|
comboStyle->addItem("Heading 5");
|
|
|
|
comboStyle->addItem("Heading 6");
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-02-04 14:31:25 +00:00
|
|
|
connect(comboStyle, &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);
|
2019-03-03 18:31:26 +00:00
|
|
|
connect(comboFont, &QComboBox::textActivated, 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();
|
2018-12-07 11:01:42 +00:00
|
|
|
for (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
|
|
|
|
2019-03-03 18:31:26 +00:00
|
|
|
connect(comboSize, &QComboBox::textActivated, 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();
|
2020-04-30 12:31:46 +00:00
|
|
|
QMimeDatabase db;
|
|
|
|
if (db.mimeTypeForFileNameAndData(f, data).name() == QLatin1String("text/html")) {
|
2020-07-30 15:36:14 +00:00
|
|
|
auto encoding = QStringDecoder::encodingForHtml(data);
|
2020-04-30 12:31:46 +00:00
|
|
|
QString str = QStringDecoder(encoding ? *encoding : QStringDecoder::Utf8)(data);
|
2020-01-31 13:30:20 +00:00
|
|
|
QUrl baseUrl = (f.front() == QLatin1Char(':') ? QUrl(f) : QUrl::fromLocalFile(f)).adjusted(QUrl::RemoveFilename);
|
|
|
|
textEdit->document()->setBaseUrl(baseUrl);
|
2011-04-27 10:05:43 +00:00
|
|
|
textEdit->setHtml(str);
|
2017-12-18 10:28:33 +00:00
|
|
|
#if QT_CONFIG(textmarkdownreader)
|
2020-04-30 12:31:46 +00:00
|
|
|
} else if (db.mimeTypeForFileNameAndData(f, data).name() == QLatin1String("text/markdown")) {
|
|
|
|
textEdit->setMarkdown(QString::fromUtf8(data));
|
2017-12-18 10:28:33 +00:00
|
|
|
#endif
|
2020-04-30 12:31:46 +00:00
|
|
|
} else {
|
|
|
|
textEdit->setPlainText(QString::fromUtf8(data));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-12-18 10:28:33 +00:00
|
|
|
fileDialog.setMimeTypeFilters(QStringList()
|
|
|
|
#if QT_CONFIG(texthtmlparser)
|
|
|
|
<< "text/html"
|
|
|
|
#endif
|
|
|
|
#if QT_CONFIG(textmarkdownreader)
|
|
|
|
|
|
|
|
<< "text/markdown"
|
|
|
|
#endif
|
|
|
|
<< "text/plain");
|
2015-08-25 12:19:37 +00:00
|
|
|
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;
|
2017-12-18 10:28:33 +00:00
|
|
|
mimeTypes << "text/plain"
|
|
|
|
#if QT_CONFIG(textodfwriter)
|
|
|
|
<< "application/vnd.oasis.opendocument.text"
|
|
|
|
#endif
|
|
|
|
#if QT_CONFIG(textmarkdownwriter)
|
|
|
|
<< "text/markdown"
|
|
|
|
#endif
|
|
|
|
<< "text/html";
|
2015-08-25 12:19:37 +00:00
|
|
|
fileDialog.setMimeTypeFilters(mimeTypes);
|
2017-12-18 10:28:33 +00:00
|
|
|
#if QT_CONFIG(textodfwriter)
|
2015-08-25 12:19:37 +00:00
|
|
|
fileDialog.setDefaultSuffix("odt");
|
2017-12-18 10:28:33 +00:00
|
|
|
#endif
|
2015-08-25 12:19:37 +00:00
|
|
|
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()
|
|
|
|
{
|
2020-01-29 18:58:01 +00:00
|
|
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)
|
2011-04-27 10:05:43 +00:00
|
|
|
QPrinter printer(QPrinter::HighResolution);
|
|
|
|
QPrintDialog *dlg = new QPrintDialog(&printer, this);
|
|
|
|
if (textEdit->textCursor().hasSelection())
|
2020-09-07 08:59:58 +00:00
|
|
|
dlg->setOption(QAbstractPrintDialog::PrintSelection);
|
2011-04-27 10:05:43 +00:00
|
|
|
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()
|
|
|
|
{
|
2020-03-05 09:06:24 +00:00
|
|
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printpreviewdialog)
|
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)
|
|
|
|
{
|
2020-03-05 09:06:24 +00:00
|
|
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
|
2011-04-27 10:05:43 +00:00
|
|
|
textEdit->print(printer);
|
2020-03-05 09:06:24 +00:00
|
|
|
#else
|
2020-06-27 12:18:09 +00:00
|
|
|
Q_UNUSED(printer);
|
2011-04-27 10:05:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TextEdit::filePrintPdf()
|
|
|
|
{
|
2020-03-05 09:06:24 +00:00
|
|
|
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [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();
|
2017-12-18 12:05:20 +00:00
|
|
|
QTextListFormat::Style style = QTextListFormat::ListStyleUndefined;
|
2019-10-02 10:19:11 +00:00
|
|
|
QTextBlockFormat::MarkerType marker = QTextBlockFormat::MarkerType::NoMarker;
|
2017-12-18 12:05:20 +00:00
|
|
|
|
|
|
|
switch (styleIndex) {
|
|
|
|
case 1:
|
|
|
|
style = QTextListFormat::ListDisc;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
style = QTextListFormat::ListCircle;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
style = QTextListFormat::ListSquare;
|
|
|
|
break;
|
|
|
|
case 4:
|
2019-02-18 14:40:04 +00:00
|
|
|
if (cursor.currentList())
|
|
|
|
style = cursor.currentList()->format().style();
|
|
|
|
else
|
|
|
|
style = QTextListFormat::ListDisc;
|
2019-10-02 10:19:11 +00:00
|
|
|
marker = QTextBlockFormat::MarkerType::Unchecked;
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2019-02-18 14:40:04 +00:00
|
|
|
if (cursor.currentList())
|
|
|
|
style = cursor.currentList()->format().style();
|
|
|
|
else
|
|
|
|
style = QTextListFormat::ListDisc;
|
2019-10-02 10:19:11 +00:00
|
|
|
marker = QTextBlockFormat::MarkerType::Checked;
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case 6:
|
2019-02-18 14:40:04 +00:00
|
|
|
style = QTextListFormat::ListDecimal;
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case 7:
|
2019-02-18 14:40:04 +00:00
|
|
|
style = QTextListFormat::ListLowerAlpha;
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case 8:
|
2019-02-18 14:40:04 +00:00
|
|
|
style = QTextListFormat::ListUpperAlpha;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
style = QTextListFormat::ListLowerRoman;
|
|
|
|
break;
|
|
|
|
case 10:
|
2017-12-18 12:05:20 +00:00
|
|
|
style = QTextListFormat::ListUpperRoman;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-12-18 12:05:20 +00:00
|
|
|
cursor.beginEditBlock();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-12-18 12:05:20 +00:00
|
|
|
QTextBlockFormat blockFmt = cursor.blockFormat();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-12-18 12:05:20 +00:00
|
|
|
if (style == QTextListFormat::ListStyleUndefined) {
|
|
|
|
blockFmt.setObjectIndex(-1);
|
2019-05-06 15:03:45 +00:00
|
|
|
int headingLevel = styleIndex >= 11 ? styleIndex - 11 + 1 : 0; // H1 to H6, or Standard
|
2017-12-18 12:05:20 +00:00
|
|
|
blockFmt.setHeadingLevel(headingLevel);
|
|
|
|
cursor.setBlockFormat(blockFmt);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-12-18 12:05:20 +00:00
|
|
|
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->mergeCurrentCharFormat(fmt);
|
|
|
|
} else {
|
2019-02-18 14:40:04 +00:00
|
|
|
blockFmt.setMarker(marker);
|
|
|
|
cursor.setBlockFormat(blockFmt);
|
2011-04-27 10:05:43 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-12-18 12:05:20 +00:00
|
|
|
|
|
|
|
cursor.endEditBlock();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-18 14:40:04 +00:00
|
|
|
void TextEdit::setChecked(bool checked)
|
|
|
|
{
|
|
|
|
textStyle(checked ? 5 : 4);
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:48:41 +00:00
|
|
|
void TextEdit::indent()
|
|
|
|
{
|
|
|
|
modifyIndentation(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::unindent()
|
|
|
|
{
|
|
|
|
modifyIndentation(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::modifyIndentation(int amount)
|
|
|
|
{
|
|
|
|
QTextCursor cursor = textEdit->textCursor();
|
|
|
|
cursor.beginEditBlock();
|
|
|
|
if (cursor.currentList()) {
|
|
|
|
QTextListFormat listFmt = cursor.currentList()->format();
|
|
|
|
// See whether the line above is the list we want to move this item into,
|
|
|
|
// or whether we need a new list.
|
|
|
|
QTextCursor above(cursor);
|
|
|
|
above.movePosition(QTextCursor::Up);
|
|
|
|
if (above.currentList() && listFmt.indent() + amount == above.currentList()->format().indent()) {
|
|
|
|
above.currentList()->add(cursor.block());
|
|
|
|
} else {
|
|
|
|
listFmt.setIndent(listFmt.indent() + amount);
|
|
|
|
cursor.createList(listFmt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QTextBlockFormat blockFmt = cursor.blockFormat();
|
|
|
|
blockFmt.setIndent(blockFmt.indent() + amount);
|
|
|
|
cursor.setBlockFormat(blockFmt);
|
|
|
|
}
|
|
|
|
cursor.endEditBlock();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
void TextEdit::currentCharFormatChanged(const QTextCharFormat &format)
|
|
|
|
{
|
|
|
|
fontChanged(format.font());
|
|
|
|
colorChanged(format.foreground().color());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextEdit::cursorPositionChanged()
|
|
|
|
{
|
|
|
|
alignmentChanged(textEdit->alignment());
|
2017-12-18 12:05:20 +00:00
|
|
|
QTextList *list = textEdit->textCursor().currentList();
|
|
|
|
if (list) {
|
|
|
|
switch (list->format().style()) {
|
|
|
|
case QTextListFormat::ListDisc:
|
|
|
|
comboStyle->setCurrentIndex(1);
|
|
|
|
break;
|
|
|
|
case QTextListFormat::ListCircle:
|
|
|
|
comboStyle->setCurrentIndex(2);
|
|
|
|
break;
|
|
|
|
case QTextListFormat::ListSquare:
|
|
|
|
comboStyle->setCurrentIndex(3);
|
|
|
|
break;
|
|
|
|
case QTextListFormat::ListDecimal:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(6);
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case QTextListFormat::ListLowerAlpha:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(7);
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case QTextListFormat::ListUpperAlpha:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(8);
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case QTextListFormat::ListLowerRoman:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(9);
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case QTextListFormat::ListUpperRoman:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(10);
|
2017-12-18 12:05:20 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
comboStyle->setCurrentIndex(-1);
|
|
|
|
break;
|
|
|
|
}
|
2019-02-18 14:40:04 +00:00
|
|
|
switch (textEdit->textCursor().block().blockFormat().marker()) {
|
2019-10-02 10:19:11 +00:00
|
|
|
case QTextBlockFormat::MarkerType::NoMarker:
|
2019-02-18 14:40:04 +00:00
|
|
|
actionToggleCheckState->setChecked(false);
|
|
|
|
break;
|
2019-10-02 10:19:11 +00:00
|
|
|
case QTextBlockFormat::MarkerType::Unchecked:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(4);
|
|
|
|
actionToggleCheckState->setChecked(false);
|
|
|
|
break;
|
2019-10-02 10:19:11 +00:00
|
|
|
case QTextBlockFormat::MarkerType::Checked:
|
2019-02-18 14:40:04 +00:00
|
|
|
comboStyle->setCurrentIndex(5);
|
|
|
|
actionToggleCheckState->setChecked(true);
|
|
|
|
break;
|
|
|
|
}
|
2017-12-18 12:05:20 +00:00
|
|
|
} else {
|
|
|
|
int headingLevel = textEdit->textCursor().blockFormat().headingLevel();
|
2019-05-06 15:03:45 +00:00
|
|
|
comboStyle->setCurrentIndex(headingLevel ? headingLevel + 10 : 0);
|
2017-12-18 12:05:20 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|