From 064daaa77a3ec420dcaceb5adf689aa9db4457a0 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 4 Nov 2017 13:17:35 +0100 Subject: [PATCH] selections can now start outside of a message This means in the empty space under any available messages --- src/messages/messageref.cpp | 35 +++++++++++++++++++++++++++++++++++ src/messages/messageref.hpp | 1 + src/widgets/channelview.cpp | 15 +++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/messages/messageref.cpp b/src/messages/messageref.cpp index 1b54d875..8eb847ad 100644 --- a/src/messages/messageref.cpp +++ b/src/messages/messageref.cpp @@ -275,6 +275,41 @@ const Word *MessageRef::tryGetWordPart(QPoint point) return nullptr; } +// XXX(pajlada): This is probably not the optimal way to calculate this +int MessageRef::getLastCharacterIndex() const +{ + // find out in which line the cursor is + int lineNumber = 0, lineStart = 0, lineEnd = 0; + + for (size_t i = 0; i < this->wordParts.size(); i++) { + const WordPart &part = this->wordParts[i]; + + if (part.getLineNumber() != lineNumber) { + lineStart = i; + lineNumber = part.getLineNumber(); + } + + lineEnd = i + 1; + } + + // count up to the cursor + int index = 0; + + for (int i = 0; i < lineStart; i++) { + const WordPart &part = this->wordParts[i]; + + index += part.getWord().isImage() ? 2 : part.getText().length() + 1; + } + + for (int i = lineStart; i < lineEnd; i++) { + const WordPart &part = this->wordParts[i]; + + index += part.getCharacterLength(); + } + + return index; +} + int MessageRef::getSelectionIndex(QPoint position) { if (this->wordParts.size() == 0) { diff --git a/src/messages/messageref.hpp b/src/messages/messageref.hpp index 673ed00b..83de6b9d 100644 --- a/src/messages/messageref.hpp +++ b/src/messages/messageref.hpp @@ -30,6 +30,7 @@ public: bool updateBuffer = false; const Word *tryGetWordPart(QPoint point); + int getLastCharacterIndex() const; int getSelectionIndex(QPoint position); private: diff --git a/src/widgets/channelview.cpp b/src/widgets/channelview.cpp index 7c05b777..5f4d713b 100644 --- a/src/widgets/channelview.cpp +++ b/src/widgets/channelview.cpp @@ -1,6 +1,7 @@ #include "widgets/channelview.hpp" #include "channelmanager.hpp" #include "colorscheme.hpp" +#include "debug/log.hpp" #include "messages/limitedqueuesnapshot.hpp" #include "messages/message.hpp" #include "messages/messageref.hpp" @@ -760,6 +761,20 @@ void ChannelView::mousePressEvent(QMouseEvent *event) if (!tryGetMessageAt(event->pos(), message, relativePos, messageIndex)) { setCursor(Qt::ArrowCursor); + auto messages = this->getMessagesSnapshot(); + if (messages.getLength() == 0) { + return; + } + + // Start selection at the last message at its last index + auto lastMessageIndex = messages.getLength() - 1; + auto lastMessage = messages[lastMessageIndex]; + auto lastCharacterIndex = lastMessage->getLastCharacterIndex(); + + SelectionItem selectionItem(lastMessageIndex, lastCharacterIndex); + this->setSelection(selectionItem, selectionItem); + this->selecting = true; + return; }