Messagelib

mailsourceviewtextbrowserwidget.cpp
1 /*
2  *
3  * This file is part of KMail, the KDE mail client.
4  *
5  * SPDX-FileCopyrightText: 2002-2003 Carsten Pfeiffer <[email protected]>
6  * SPDX-FileCopyrightText: 2003 Zack Rusin <[email protected]>
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 <KStandardAction>
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 
38 using namespace MessageViewer;
39 MailSourceViewTextBrowserWidget::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  mTextToSpeechContainerWidget->setObjectName(QLatin1StringView("texttospeech"));
50  lay->addWidget(mTextToSpeechContainerWidget);
51  mTextBrowser = new MailSourceViewTextBrowser(mTextToSpeechContainerWidget);
52 #else
53  mTextBrowser = new MailSourceViewTextBrowser(this);
54 #endif
55  mTextBrowser->setObjectName(QLatin1StringView("textbrowser"));
56  mTextBrowser->setLineWrapMode(QPlainTextEdit::NoWrap);
57  mTextBrowser->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
58 
59  const KSyntaxHighlighting::Definition def = mRepo.definitionForName(syntax);
60  if (!def.isValid()) {
61  qCWarning(MESSAGEVIEWER_LOG) << "Invalid definition name";
62  }
63 
64  auto hl = new KSyntaxHighlighting::SyntaxHighlighter(mTextBrowser->document());
65  hl->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
66  : mRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
67  hl->setDefinition(def);
68 
69  connect(mTextBrowser, &MailSourceViewTextBrowser::findText, this, &MailSourceViewTextBrowserWidget::slotFind);
70  lay->addWidget(mTextBrowser);
71 
72  mFindBar = new FindBarSourceView(mTextBrowser, this);
73  mFindBar->setObjectName(QLatin1StringView("findbar"));
74  connect(mFindBar, &FindBarSourceView::hideFindBar, mSliderContainer, &TextAddonsWidgets::SlideContainer::slideOut);
75  mSliderContainer->setContent(mFindBar);
76 
77  lay->addWidget(mSliderContainer);
78  auto shortcut = new QShortcut(this);
79  shortcut->setKey(Qt::Key_F | Qt::CTRL);
80  connect(shortcut, &QShortcut::activated, this, &MailSourceViewTextBrowserWidget::slotFind);
81 }
82 
83 void MailSourceViewTextBrowserWidget::slotFind()
84 {
85  if (mTextBrowser->textCursor().hasSelection()) {
86  mFindBar->setText(mTextBrowser->textCursor().selectedText());
87  }
88  mSliderContainer->slideIn();
89  mFindBar->focusAndSetCursor();
90 }
91 
92 void MailSourceViewTextBrowserWidget::setText(const QString &text)
93 {
94  mTextBrowser->setPlainText(text);
95 }
96 
97 void MailSourceViewTextBrowserWidget::setPlainText(const QString &text)
98 {
99  mTextBrowser->setPlainText(text);
100 }
101 
102 void MailSourceViewTextBrowserWidget::setFixedFont()
103 {
104  mTextBrowser->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
105 }
106 
107 MessageViewer::MailSourceViewTextBrowser *MailSourceViewTextBrowserWidget::textBrowser() const
108 {
109  return mTextBrowser;
110 }
111 #ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
112 MailSourceViewTextBrowser::MailSourceViewTextBrowser(TextEditTextToSpeech::TextToSpeechContainerWidget *TextToSpeechContainerWidget, QWidget *parent)
113  : QPlainTextEdit(parent)
114  , mTextToSpeechContainerWidget(TextToSpeechContainerWidget)
115 {
116 }
117 #endif
118 MailSourceViewTextBrowser::MailSourceViewTextBrowser(QWidget *parent)
119  : QPlainTextEdit(parent)
120 {
121 }
122 
123 void MailSourceViewTextBrowser::contextMenuEvent(QContextMenuEvent *event)
124 {
125  QMenu *popup = createStandardContextMenu();
126  if (popup) {
127  popup->addSeparator();
128  popup->addAction(KStandardAction::find(this, &MailSourceViewTextBrowser::findText, this));
129 #ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
130  popup->addSeparator();
131  popup->addAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-text-to-speech")),
132  i18n("Speak Text"),
133  this,
134  &MailSourceViewTextBrowser::slotSpeakText);
135 #endif
136  popup->addSeparator();
137  popup->addAction(KStandardAction::saveAs(this, &MailSourceViewTextBrowser::slotSaveAs, this));
138 
139  popup->exec(event->globalPos());
140  delete popup;
141  }
142 }
143 
144 void MailSourceViewTextBrowser::slotSaveAs()
145 {
146  PimCommon::Util::saveTextAs(toPlainText(), QString(), this);
147 }
148 
149 void MailSourceViewTextBrowser::slotSpeakText()
150 {
151  QString text;
152  if (textCursor().hasSelection()) {
153  text = textCursor().selectedText();
154  } else {
155  text = toPlainText();
156  }
157 #ifdef HAVE_KTEXTADDONS_TEXT_TO_SPEECH_SUPPORT
158  mTextToSpeechContainerWidget->say(text);
159 #endif
160 }
161 
162 #include "moc_mailsourceviewtextbrowserwidget.cpp"
const QList< QKeySequence > & shortcut(StandardShortcut id)
void activated()
QIcon fromTheme(const QString &name)
QFont systemFont(QFontDatabase::SystemFont type)
QAction * addSeparator()
QAction * addAction(const QString &text)
TextSelectableByMouse
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QAction * find(const QObject *recvr, const char *slot, QObject *parent)
QAction * saveAs(const QObject *recvr, const char *slot, QObject *parent)
QAction * exec()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Dec 6 2023 03:56:40 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.