1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
/**************************************************************************
* *
* Copyright (C) 2017 Kitsune Ral <kitsune-ral@users.sf.net>
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 3 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "chatedit.h"
#include "chatroomwidget.h"
#include <QtWidgets/QMenu>
#include <QtWidgets/QShortcut>
#include <QtGui/QKeyEvent>
#include <QtCore/QMimeData>
#include <QtCore/QStringBuilder>
#include <util.h>
static const QKeySequence ResetFormatShortcut("Ctrl+M");
ChatEdit::ChatEdit(ChatRoomWidget* c)
: KChatEdit(c), chatRoomWidget(c), matchesListPosition(0)
{
new QShortcut(ResetFormatShortcut, this,
this, &KChatEdit::resetCurrentFormat);
}
void ChatEdit::keyPressEvent(QKeyEvent* event)
{
pickingMentions = false;
if (event->key() == Qt::Key_Tab) {
triggerCompletion();
return;
}
cancelCompletion();
KChatEdit::keyPressEvent(event);
}
void ChatEdit::contextMenuEvent(QContextMenuEvent *event)
{
auto* menu = createStandardContextMenu();
// The shortcut here is in order to show it to the user; it's the QShortcut
// in the constructor that actually triggers on Ctrl+M (no idea
// why the QAction doesn't work - because it's not in the main menu?)
menu->addAction(tr("Reset formatting"),
this, &KChatEdit::resetCurrentFormat, ResetFormatShortcut)
->setStatusTip(
tr("Reset the current character formatting to the default"));
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->popup(event->globalPos());
}
void ChatEdit::switchContext(QObject* contextKey)
{
cancelCompletion();
KChatEdit::switchContext(contextKey);
}
bool ChatEdit::canInsertFromMimeData(const QMimeData *source) const
{
return source->hasImage() || QTextEdit::canInsertFromMimeData(source);
}
void ChatEdit::insertFromMimeData(const QMimeData *source)
{
if (source->hasImage())
emit insertFromMimeDataRequested(source);
else
QTextEdit::insertFromMimeData(source);
}
void ChatEdit::appendMentionAt(QTextCursor& cursor, QString mention,
QUrl mentionUrl, bool select)
{
Q_ASSERT(!mention.isEmpty() && mentionUrl.isValid());
if (cursor.atStart() && mention.startsWith('/'))
mention.push_front('/');
const auto posBeforeMention = cursor.position();
const auto& safeMention = Quotient::sanitized(mention.toHtmlEscaped());
// The most concise way to add a link is by QTextCursor::insertHtml()
// as QTextDocument API is unwieldy (get to the block, make a fragment... -
// just merging a char format with setAnchor()/setAnchorHref() doesn't work)
if (Quotient::Settings().get("UI/hyperlink_users", true))
cursor.insertHtml("<a href=\"" % mentionUrl.toEncoded() % "\">"
% safeMention % "</a>");
else
cursor.insertText(safeMention);
cursor.setPosition(posBeforeMention, select ? QTextCursor::KeepAnchor
: QTextCursor::MoveAnchor);
ensureCursorVisible(); // The real one, not completionCursor
}
bool ChatEdit::initCompletion()
{
completionCursor = textCursor();
completionCursor.clearSelection();
while (completionCursor.movePosition(QTextCursor::PreviousCharacter,
QTextCursor::KeepAnchor)) {
const auto& firstChar = completionCursor.selectedText().at(0);
if (!firstChar.isLetterOrNumber() && firstChar != '@') {
completionCursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor);
break;
}
}
completionMatches =
chatRoomWidget->findCompletionMatches(completionCursor.selectedText());
if (completionMatches.isEmpty())
return false;
matchesListPosition = 0;
// Add (constant) punctuation right away, in preparation for the cycle
// of rotating completion matches (that are placed before this punctuation).
auto punct = QStringLiteral(" ");
static const auto ColonSpace = QStringLiteral(": ");
auto lookBehindCursor = completionCursor;
if (lookBehindCursor.atStart())
punct = QStringLiteral(": "); // Salutation
else {
for (auto i = 1; i <= ColonSpace.size(); ++i) {
lookBehindCursor.movePosition(QTextCursor::PreviousCharacter,
QTextCursor::KeepAnchor);
if (lookBehindCursor.selectedText().startsWith(
ColonSpace.leftRef(i))) {
// Replace the colon (with an optional space) before the place
// of completion with a comma (with a huge assumption that
// this colon ends a salutation; TODO: using the fact that
// mentions are linkified now, we can reliably detect
// salutations even to several users).
lookBehindCursor.insertText(QStringLiteral(", "));
punct = QStringLiteral(": ");
break;
}
}
}
const auto beforePunct = completionCursor.position();
completionCursor.insertText(punct);
completionCursor.setPosition(beforePunct);
return true;
}
void ChatEdit::triggerCompletion()
{
if (!isCompletionActive() && !initCompletion())
return;
Q_ASSERT(!completionMatches.empty()
&& matchesListPosition < completionMatches.size());
const auto& completionMatch = completionMatches.at(matchesListPosition);
appendMentionAt(completionCursor, completionMatch.first,
completionMatch.second, true);
Q_ASSERT(!completionCursor.selectedText().isEmpty());
auto completionHL = completionCursor.charFormat();
completionHL.setUnderlineStyle(QTextCharFormat::DashUnderline);
setExtraSelections({ { completionCursor, completionHL } });
QStringList matchesForSignal;
for (const auto& p: completionMatches)
matchesForSignal.push_back(p.first);
emit proposedCompletion(matchesForSignal, matchesListPosition);
matchesListPosition = (matchesListPosition + 1) % completionMatches.length();
}
void ChatEdit::cancelCompletion()
{
completionMatches.clear();
setExtraSelections({});
Q_ASSERT(!isCompletionActive());
emit cancelledCompletion();
}
bool ChatEdit::isCompletionActive() { return !completionMatches.isEmpty(); }
void ChatEdit::insertMention(QString author, QUrl url)
{
// The order of inserting text below is such to be convenient for the user
// to undo in case the primitive intelligence below fails.
auto cursor = textCursor();
// The mention may be hyperlinked, possibly changing the default
// character format as a result if the mention happens to be at the end
// of the block (which is almost always the case). So remember the format
// at the point, and apply it later when printing the postfix.
// triggerCompletion() doesn't have that problem because it inserts
// the postfix before inserting the mention.
auto textFormat = cursor.charFormat();
appendMentionAt(cursor, author, url, false);
// Add spaces and a colon around the inserted string if necessary.
if (cursor.position() > 0 &&
document()->characterAt(cursor.position() - 1).isLetterOrNumber())
cursor.insertText(QStringLiteral(" "));
while (cursor.movePosition(QTextCursor::PreviousCharacter) &&
document()->characterAt(cursor.position()).isSpace());
QString postfix;
if (cursor.atStart())
postfix = QStringLiteral(":");
if ((pickingMentions || isCompletionActive())
&& document()->characterAt(cursor.position()) == ':') {
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
cursor.insertText(QStringLiteral(","));
postfix = QStringLiteral(":");
}
auto editCursor = textCursor();
auto currentChar = document()->characterAt(editCursor.position());
if (editCursor.atBlockEnd() || currentChar.isLetterOrNumber()
|| currentChar == '.')
postfix.push_back(' ');
if (!postfix.isEmpty())
editCursor.insertText(postfix, textFormat);
pickingMentions = true;
cancelCompletion();
}
|