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 <KPIMTextEdit/SlideContainer>
16 #include <KPIMTextEdit/TextToSpeechInterface>
17 #include <KPIMTextEdit/TextToSpeechWidget>
18 #include <PimCommon/PimUtil>
19 
20 #include <KSyntaxHighlighting/Definition>
21 #include <KSyntaxHighlighting/SyntaxHighlighter>
22 #include <KSyntaxHighlighting/Theme>
23 
24 #include <KLocalizedString>
25 #include <KStandardAction>
26 #include <QAction>
27 #include <QIcon>
28 
29 #include <QContextMenuEvent>
30 #include <QShortcut>
31 #include <QVBoxLayout>
32 
33 #include <QFontDatabase>
34 #include <QMenu>
35 #include <QPushButton>
36 
37 using namespace MessageViewer;
38 MailSourceViewTextBrowserWidget::MailSourceViewTextBrowserWidget(const QString &syntax, QWidget *parent)
39  : QWidget(parent)
40  , mSliderContainer(new KPIMTextEdit::SlideContainer(this))
41  , mTextToSpeechWidget(new KPIMTextEdit::TextToSpeechWidget(this))
42 {
43  auto lay = new QVBoxLayout(this);
44  lay->setContentsMargins({});
45  mTextToSpeechWidget->setObjectName(QStringLiteral("texttospeech"));
46  lay->addWidget(mTextToSpeechWidget);
47 
48  auto textToSpeechInterface = new KPIMTextEdit::TextToSpeechInterface(mTextToSpeechWidget, this);
49 
50  mTextBrowser = new MailSourceViewTextBrowser(textToSpeechInterface);
51  mTextBrowser->setObjectName(QStringLiteral("textbrowser"));
52  mTextBrowser->setLineWrapMode(QPlainTextEdit::NoWrap);
53  mTextBrowser->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
54 
55  const KSyntaxHighlighting::Definition def = mRepo.definitionForName(syntax);
56  if (!def.isValid()) {
57  qCWarning(MESSAGEVIEWER_LOG) << "Invalid definition name";
58  }
59 
60  auto hl = new KSyntaxHighlighting::SyntaxHighlighter(mTextBrowser->document());
61  hl->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
62  : mRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
63  hl->setDefinition(def);
64 
65  connect(mTextBrowser, &MailSourceViewTextBrowser::findText, this, &MailSourceViewTextBrowserWidget::slotFind);
66  lay->addWidget(mTextBrowser);
67 
68  mFindBar = new FindBarSourceView(mTextBrowser, this);
69  mFindBar->setObjectName(QStringLiteral("findbar"));
70  connect(mFindBar, &FindBarSourceView::hideFindBar, mSliderContainer, &KPIMTextEdit::SlideContainer::slideOut);
71  mSliderContainer->setContent(mFindBar);
72 
73  lay->addWidget(mSliderContainer);
74  auto shortcut = new QShortcut(this);
75  shortcut->setKey(Qt::Key_F | Qt::CTRL);
76  connect(shortcut, &QShortcut::activated, this, &MailSourceViewTextBrowserWidget::slotFind);
77 }
78 
79 void MailSourceViewTextBrowserWidget::slotFind()
80 {
81  if (mTextBrowser->textCursor().hasSelection()) {
82  mFindBar->setText(mTextBrowser->textCursor().selectedText());
83  }
84  mSliderContainer->slideIn();
85  mFindBar->focusAndSetCursor();
86 }
87 
88 void MailSourceViewTextBrowserWidget::setText(const QString &text)
89 {
90  mTextBrowser->setPlainText(text);
91 }
92 
93 void MailSourceViewTextBrowserWidget::setPlainText(const QString &text)
94 {
95  mTextBrowser->setPlainText(text);
96 }
97 
98 void MailSourceViewTextBrowserWidget::setFixedFont()
99 {
100  mTextBrowser->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
101 }
102 
103 MessageViewer::MailSourceViewTextBrowser *MailSourceViewTextBrowserWidget::textBrowser() const
104 {
105  return mTextBrowser;
106 }
107 
108 MailSourceViewTextBrowser::MailSourceViewTextBrowser(KPIMTextEdit::TextToSpeechInterface *textToSpeechInterface, QWidget *parent)
109  : QPlainTextEdit(parent)
110  , mTextToSpeechInterface(textToSpeechInterface)
111 {
112 }
113 
114 void MailSourceViewTextBrowser::contextMenuEvent(QContextMenuEvent *event)
115 {
116  QMenu *popup = createStandardContextMenu();
117  if (popup) {
118  popup->addSeparator();
119  popup->addAction(KStandardAction::find(this, &MailSourceViewTextBrowser::findText, this));
120  // Code from KTextBrowser
121  if (mTextToSpeechInterface->isReady()) {
122  popup->addSeparator();
123  popup->addAction(QIcon::fromTheme(QStringLiteral("preferences-desktop-text-to-speech")),
124  i18n("Speak Text"),
125  this,
126  &MailSourceViewTextBrowser::slotSpeakText);
127  }
128  popup->addSeparator();
129  popup->addAction(KStandardAction::saveAs(this, &MailSourceViewTextBrowser::slotSaveAs, this));
130 
131  popup->exec(event->globalPos());
132  delete popup;
133  }
134 }
135 
136 void MailSourceViewTextBrowser::slotSaveAs()
137 {
138  PimCommon::Util::saveTextAs(toPlainText(), QString(), this);
139 }
140 
141 void MailSourceViewTextBrowser::slotSpeakText()
142 {
143  QString text;
144  if (textCursor().hasSelection()) {
145  text = textCursor().selectedText();
146  } else {
147  text = toPlainText();
148  }
149  mTextToSpeechInterface->say(text);
150 }
QAction * saveAs(const QObject *recvr, const char *slot, QObject *parent)
const QPoint & globalPos() const const
QAction * addAction(const QString &text)
QAction * addSeparator()
TextSelectableByMouse
QAction * exec()
void activated()
QString i18n(const char *text, const TYPE &arg...)
QFont systemFont(QFontDatabase::SystemFont type)
QIcon fromTheme(const QString &name)
const QList< QKeySequence > & shortcut(StandardShortcut id)
QAction * find(const QObject *recvr, const char *slot, QObject *parent)
virtual void setTheme(const Theme &theme)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Nov 30 2021 23:05:47 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.