selections can now start outside of a message

This means in the empty space under any available messages
This commit is contained in:
Rasmus Karlsson 2017-11-04 13:17:35 +01:00
parent e1a31785ef
commit 064daaa77a
3 changed files with 51 additions and 0 deletions

View file

@ -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) {

View file

@ -30,6 +30,7 @@ public:
bool updateBuffer = false;
const Word *tryGetWordPart(QPoint point);
int getLastCharacterIndex() const;
int getSelectionIndex(QPoint position);
private:

View file

@ -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;
}