qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
chattextedit.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "chattextedit.h"
21 
22 #include "src/widget/translator.h"
23 
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QKeyEvent>
27 #include <QMimeData>
28 
30  : QTextEdit(parent)
31 {
32  retranslateUi();
33  setAcceptRichText(false);
34  setAcceptDrops(false);
35 
37 }
38 
40 {
42 }
43 
44 void ChatTextEdit::keyPressEvent(QKeyEvent* event)
45 {
46  int key = event->key();
47  if ((key == Qt::Key_Enter || key == Qt::Key_Return) && !(event->modifiers() & Qt::ShiftModifier)) {
48  emit enterPressed();
49  return;
50  }
51  if (key == Qt::Key_Escape) {
52  emit escapePressed();
53  return;
54  }
55  if (key == Qt::Key_Tab) {
56  if (event->modifiers())
57  event->ignore();
58  else {
59  emit tabPressed();
60  event->ignore();
61  }
62  return;
63  }
64  if (key == Qt::Key_Up && this->toPlainText().isEmpty()) {
65  this->setPlainText(lastMessage);
66  this->setFocus();
67  this->moveCursor(QTextCursor::MoveOperation::End, QTextCursor::MoveMode::MoveAnchor);
68  return;
69  }
70  if (event->matches(QKeySequence::Paste) && pasteIfImage(event)) {
71  return;
72  }
73  emit keyPressed();
74  QTextEdit::keyPressEvent(event);
75 }
76 
78 {
79  lastMessage = lm;
80 }
81 
83 {
84  setPlaceholderText(tr("Type your message here..."));
85 }
86 
87 void ChatTextEdit::sendKeyEvent(QKeyEvent* event)
88 {
89  emit keyPressEvent(event);
90 }
91 
92 bool ChatTextEdit::pasteIfImage(QKeyEvent* event)
93 {
94  const QClipboard* const clipboard = QApplication::clipboard();
95  if (!clipboard) {
96  return false;
97  }
98 
99  const QMimeData* const mimeData = clipboard->mimeData();
100  if (!mimeData || !mimeData->hasImage()) {
101  return false;
102  }
103 
104  const QPixmap pixmap(clipboard->pixmap());
105  if (pixmap.isNull()) {
106  return false;
107  }
108 
109  emit pasteImage(pixmap);
110  return true;
111 }
112 
ChatTextEdit::lastMessage
QString lastMessage
Definition: chattextedit.h:48
ChatTextEdit::~ChatTextEdit
~ChatTextEdit()
Definition: chattextedit.cpp:39
ChatTextEdit::ChatTextEdit
ChatTextEdit(QWidget *parent=nullptr)
Definition: chattextedit.cpp:29
Translator::unregister
static void unregister(void *owner)
Unregisters all handlers of an owner.
Definition: translator.cpp:103
ChatTextEdit::sendKeyEvent
void sendKeyEvent(QKeyEvent *event)
Definition: chattextedit.cpp:87
ChatTextEdit::tabPressed
void tabPressed()
ChatTextEdit::keyPressEvent
void keyPressEvent(QKeyEvent *event) final
Definition: chattextedit.cpp:44
chattextedit.h
Translator::registerHandler
static void registerHandler(const std::function< void()> &, void *owner)
Register a function to be called when the UI needs to be retranslated.
Definition: translator.cpp:93
ChatTextEdit::keyPressed
void keyPressed()
ChatTextEdit::setLastMessage
void setLastMessage(QString lm)
Definition: chattextedit.cpp:77
ChatTextEdit::pasteIfImage
bool pasteIfImage(QKeyEvent *event)
Definition: chattextedit.cpp:92
ChatTextEdit::pasteImage
void pasteImage(const QPixmap &pixmap)
ChatTextEdit::escapePressed
void escapePressed()
translator.h
ChatTextEdit::retranslateUi
void retranslateUi()
Definition: chattextedit.cpp:82
ChatTextEdit::enterPressed
void enterPressed()