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

KDE's Doxygen guidelines are available online.