Messagelib

mailsourceviewtextbrowserwidget.cpp
1/*
2 *
3 * This file is part of KMail, the KDE mail client.
4 *
5 * SPDX-FileCopyrightText: 2002-2003 Carsten Pfeiffer <pfeiffer@kde.org>
6 * SPDX-FileCopyrightText: 2003 Zack Rusin <zack@kde.org>
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11#include "mailsourceviewtextbrowserwidget.h"
12#include "findbar/findbarsourceview.h"
13#include "messageviewer/messageviewerutil.h"
14#include "messageviewer_debug.h"
15#include <TextAddonsWidgets/SlideContainer>
16#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
17#include <TextEditTextToSpeech/TextToSpeechContainerWidget>
18#endif
19#include <PimCommon/PimUtil>
20
21#include <KSyntaxHighlighting/Definition>
22#include <KSyntaxHighlighting/SyntaxHighlighter>
23#include <KSyntaxHighlighting/Theme>
24
25#include <KLocalizedString>
26#include <KStandardActions>
27#include <QAction>
28#include <QIcon>
29
30#include <QContextMenuEvent>
31#include <QShortcut>
32#include <QVBoxLayout>
33
34#include <QFontDatabase>
35#include <QMenu>
36#include <QPushButton>
37
38using namespace MessageViewer;
39MailSourceViewTextBrowserWidget::MailSourceViewTextBrowserWidget(const QString &syntax, QWidget *parent)
40 : QWidget(parent)
41 , mSliderContainer(new TextAddonsWidgets::SlideContainer(this))
42#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
43 , mTextToSpeechContainerWidget(new TextEditTextToSpeech::TextToSpeechContainerWidget(this))
44#endif
45{
46 auto lay = new QVBoxLayout(this);
47#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
48 lay->setContentsMargins({});
49 lay->setSpacing(0);
50 mTextToSpeechContainerWidget->setObjectName(QLatin1StringView("texttospeech"));
51 lay->addWidget(mTextToSpeechContainerWidget);
52 mTextBrowser = new MailSourceViewTextBrowser(mTextToSpeechContainerWidget);
53#else
54 mTextBrowser = new MailSourceViewTextBrowser(this);
55#endif
56 mTextBrowser->setObjectName(QLatin1StringView("textbrowser"));
57 mTextBrowser->setLineWrapMode(QPlainTextEdit::NoWrap);
58 mTextBrowser->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
59
60 const KSyntaxHighlighting::Definition def = mRepo.definitionForName(syntax);
61 if (!def.isValid()) {
62 qCWarning(MESSAGEVIEWER_LOG) << "Invalid definition name";
63 }
64
65 auto hl = new KSyntaxHighlighting::SyntaxHighlighter(mTextBrowser->document());
66 hl->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
68 hl->setDefinition(def);
69
70 connect(mTextBrowser, &MailSourceViewTextBrowser::findText, this, &MailSourceViewTextBrowserWidget::slotFind);
71 lay->addWidget(mTextBrowser);
72
73 mFindBar = new FindBarSourceView(mTextBrowser, this);
74 mFindBar->setObjectName(QLatin1StringView("findbar"));
75 connect(mFindBar, &FindBarSourceView::hideFindBar, mSliderContainer, &TextAddonsWidgets::SlideContainer::slideOut);
76 mSliderContainer->setContent(mFindBar);
77
78 lay->addWidget(mSliderContainer);
79 auto shortcut = new QShortcut(this);
80 shortcut->setKey(Qt::Key_F | Qt::CTRL);
81 connect(shortcut, &QShortcut::activated, this, &MailSourceViewTextBrowserWidget::slotFind);
82}
83
84void MailSourceViewTextBrowserWidget::slotFind()
85{
86 if (mTextBrowser->textCursor().hasSelection()) {
87 mFindBar->setText(mTextBrowser->textCursor().selectedText());
88 }
89 mSliderContainer->slideIn();
90 mFindBar->focusAndSetCursor();
91}
92
93void MailSourceViewTextBrowserWidget::setText(const QString &text)
94{
95 mTextBrowser->setPlainText(text);
96}
97
98void MailSourceViewTextBrowserWidget::setPlainText(const QString &text)
99{
100 mTextBrowser->setPlainText(text);
101}
102
103void MailSourceViewTextBrowserWidget::setFixedFont()
104{
106}
107
108MessageViewer::MailSourceViewTextBrowser *MailSourceViewTextBrowserWidget::textBrowser() const
109{
110 return mTextBrowser;
111}
112#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
113MailSourceViewTextBrowser::MailSourceViewTextBrowser(TextEditTextToSpeech::TextToSpeechContainerWidget *TextToSpeechContainerWidget, QWidget *parent)
114 : QPlainTextEdit(parent)
115 , mTextToSpeechContainerWidget(TextToSpeechContainerWidget)
116{
117}
118#endif
119MailSourceViewTextBrowser::MailSourceViewTextBrowser(QWidget *parent)
120 : QPlainTextEdit(parent)
121{
122}
123
124void MailSourceViewTextBrowser::contextMenuEvent(QContextMenuEvent *event)
125{
127 if (popup) {
128 popup->addSeparator();
129 popup->addAction(KStandardActions::find(this, &MailSourceViewTextBrowser::findText, this));
130#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
131 popup->addSeparator();
132 popup->addAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-text-to-speech")),
133 i18n("Speak Text"),
134 this,
135 &MailSourceViewTextBrowser::slotSpeakText);
136#endif
137 popup->addSeparator();
138 popup->addAction(KStandardActions::saveAs(this, &MailSourceViewTextBrowser::slotSaveAs, this));
139
140 popup->exec(event->globalPos());
141 delete popup;
142 }
143}
144
145void MailSourceViewTextBrowser::slotSaveAs()
146{
147 PimCommon::Util::saveTextAs(toPlainText(), QString(), this);
148}
149
150void MailSourceViewTextBrowser::slotSpeakText()
151{
152 QString text;
153 if (textCursor().hasSelection()) {
154 text = textCursor().selectedText();
155 } else {
156 text = toPlainText();
157 }
158#ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
159 mTextToSpeechContainerWidget->say(text);
160#endif
161}
162
163#include "moc_mailsourceviewtextbrowserwidget.cpp"
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & shortcut(StandardShortcut id)
virtual bool event(QEvent *event) override
QFont systemFont(SystemFont type)
QIcon fromTheme(const QString &name)
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * addSeparator()
QAction * exec()
QMenu * createStandardContextMenu()
void setPlainText(const QString &text)
QTextCursor textCursor() const const
QString toPlainText() const const
void activated()
TextSelectableByMouse
bool hasSelection() const const
QString selectedText() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setFont(const QFont &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.