fixed message cursor positions

This commit is contained in:
fourtf 2017-04-24 23:00:26 +02:00
parent eb82f5c1e2
commit 95c7ae9f18
6 changed files with 230 additions and 228 deletions

View file

@ -128,7 +128,8 @@ HEADERS += \
accountmanager.h \ accountmanager.h \
twitch/twitchaccount.h \ twitch/twitchaccount.h \
ircaccount.h \ ircaccount.h \
widgets/accountpopup.h widgets/accountpopup.h \
util/distancebetweenpoints.h
PRECOMPILED_HEADER = PRECOMPILED_HEADER =

View file

@ -23,9 +23,9 @@ public:
Link(); Link();
Link(Type getType, const QString &getValue); Link(Type getType, const QString &getValue);
bool getIsValid() const bool isValid() const
{ {
return type == None; return type != None;
} }
Type getType() const Type getType() const

View file

@ -94,7 +94,7 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
int x = MARGIN_LEFT; int x = MARGIN_LEFT;
int y = MARGIN_TOP; int y = MARGIN_TOP;
int right = width - MARGIN_RIGHT - MARGIN_LEFT; int right = width - MARGIN_RIGHT;
int lineNumber = 0; int lineNumber = 0;
int lineStart = 0; int lineStart = 0;
@ -258,17 +258,16 @@ int MessageRef::getSelectionIndex(QPoint position)
for (int i = 0; i < _wordParts.size(); i++) { for (int i = 0; i < _wordParts.size(); i++) {
WordPart &part = _wordParts[i]; WordPart &part = _wordParts[i];
// return if curser under the word if (part.getLineNumber() != 0 && position.y() < part.getY()) {
if (position.y() >= part.getBottom()) {
break; break;
} }
if (part.getLineNumber() != lineNumber) { if (part.getLineNumber() != lineNumber) {
lineStart = i; lineStart = i - 1;
lineNumber = part.getLineNumber(); lineNumber = part.getLineNumber();
} }
lineEnd = i; lineEnd = part.getLineNumber() == 0 ? i : i + 1;
} }
// count up to the cursor // count up to the cursor
@ -289,7 +288,7 @@ int MessageRef::getSelectionIndex(QPoint position)
} }
// cursor is right of the word part // cursor is right of the word part
if (position.x() > part.getX()) { if (position.x() > part.getX() + part.getWidth()) {
index += part.getWord().isImage() ? 2 : part.getText().length() + 1; index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
continue; continue;
} }
@ -316,26 +315,6 @@ int MessageRef::getSelectionIndex(QPoint position)
} }
return index; return index;
// go through all the wordparts
// for (int i = 0; i < wordParts; i < wordParts.size()) {
// WordPart &part = wordParts[i];
// // return if curser under the word
// if (position.y() >= part.getBottom()) {
// break;
// }
// // increment index and continue if the curser x is bigger than the
// words
// // right edge
// if (position.x() > part.getRight()) {
// index += part.getWord().isImage() ? 2 +
// part.getText().length() + 1;
// continue;
// }
// }
} }
} }
} }

View file

@ -134,8 +134,8 @@ SharedMessage TwitchMessageBuilder::parse(const Communi::IrcPrivateMessage *ircM
userDisplayString += ": "; userDisplayString += ": ";
} }
b.appendWord( b.appendWord(Word(userDisplayString, Word::Username, usernameColor, userDisplayString,
Word(userDisplayString, Word::Username, usernameColor, userDisplayString, QString())); QString(), Link(Link::UserInfo, b.userName)));
// highlights // highlights
// TODO: implement this xD // TODO: implement this xD

View file

@ -0,0 +1,22 @@
#ifndef DISTANCEBETWEENPOINTS_H
#define DISTANCEBETWEENPOINTS_H
#include <math.h>
namespace chatterino {
namespace util {
static float distanceBetweenPoints(const QPointF &p1, const QPointF &p2)
{
QPointF tmp = p1 - p2;
float distance = 0.f;
distance += tmp.x() * tmp.x();
distance += tmp.y() * tmp.y();
return sqrt(distance);
}
}
}
#endif // DISTANCEBETWEENPOINTS_H

View file

@ -5,6 +5,7 @@
#include "messages/wordpart.h" #include "messages/wordpart.h"
#include "settingsmanager.h" #include "settingsmanager.h"
#include "ui_accountpopupform.h" #include "ui_accountpopupform.h"
#include "util/distancebetweenpoints.h"
#include "widgets/chatwidget.h" #include "widgets/chatwidget.h"
#include <math.h> #include <math.h>
@ -55,6 +56,7 @@ bool ChatWidgetView::layoutMessages()
bool showScrollbar = false, redraw = false; bool showScrollbar = false, redraw = false;
int start = _scrollbar.getCurrentValue(); int start = _scrollbar.getCurrentValue();
int layoutWidth = _scrollbar.isVisible() ? width() - _scrollbar.width() : width();
// layout the visible messages in the view // layout the visible messages in the view
if (messages.getSize() > start) { if (messages.getSize() > start) {
@ -63,7 +65,7 @@ bool ChatWidgetView::layoutMessages()
for (int i = start; i < messages.getSize(); ++i) { for (int i = start; i < messages.getSize(); ++i) {
auto message = messages[i]; auto message = messages[i];
redraw |= message->layout(width(), true); redraw |= message->layout(layoutWidth, true);
y += message->getHeight(); y += message->getHeight();
@ -79,7 +81,7 @@ bool ChatWidgetView::layoutMessages()
for (int i = messages.getSize() - 1; i >= 0; i--) { for (int i = messages.getSize() - 1; i >= 0; i--) {
auto *message = messages[i].get(); auto *message = messages[i].get();
message->layout(width(), true); message->layout(layoutWidth, true);
h -= message->getHeight(); h -= message->getHeight();
@ -298,6 +300,9 @@ void ChatWidgetView::mouseMoveEvent(QMouseEvent *event)
return; return;
} }
int index = message->getSelectionIndex(relativePos);
qDebug() << index;
messages::Word hoverWord; messages::Word hoverWord;
if (!message->tryGetWordPart(relativePos, hoverWord)) { if (!message->tryGetWordPart(relativePos, hoverWord)) {
@ -305,11 +310,7 @@ void ChatWidgetView::mouseMoveEvent(QMouseEvent *event)
return; return;
} }
int index = message->getSelectionIndex(relativePos); if (hoverWord.getLink().isValid()) {
qDebug() << index;
if (hoverWord.getLink().getIsValid()) {
setCursor(Qt::PointingHandCursor); setCursor(Qt::PointingHandCursor);
} else { } else {
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
@ -322,17 +323,6 @@ void ChatWidgetView::mousePressEvent(QMouseEvent *event)
_lastPressPosition = event->screenPos(); _lastPressPosition = event->screenPos();
} }
static float distanceBetweenPoints(const QPointF &p1, const QPointF &p2)
{
QPointF tmp = p1 - p2;
float distance = 0.f;
distance += tmp.x() * tmp.x();
distance += tmp.y() * tmp.y();
return std::sqrt(distance);
}
void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event) void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
{ {
if (!_mouseDown) { if (!_mouseDown) {
@ -343,7 +333,7 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
_mouseDown = false; _mouseDown = false;
float distance = distanceBetweenPoints(_lastPressPosition, event->screenPos()); float distance = util::distanceBetweenPoints(_lastPressPosition, event->screenPos());
qDebug() << "Distance: " << distance; qDebug() << "Distance: " << distance;
@ -366,15 +356,25 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
return; return;
} }
auto _message = message->getMessage(); messages::Word hoverWord;
auto user = _message->getUserName();
qDebug() << "Clicked " << user << "s message"; if (!message->tryGetWordPart(relativePos, hoverWord)) {
return;
}
auto &link = hoverWord.getLink();
switch (link.getType()) {
case messages::Link::UserInfo:
auto user = message->getMessage()->getUserName();
_userPopupWidget.setName(user); _userPopupWidget.setName(user);
_userPopupWidget.move(event->screenPos().toPoint()); _userPopupWidget.move(event->screenPos().toPoint());
_userPopupWidget.show(); _userPopupWidget.show();
_userPopupWidget.setFocus(); _userPopupWidget.setFocus();
qDebug() << "Clicked " << user << "s message";
break;
}
} }
bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::MessageRef> &_message, bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::MessageRef> &_message,
@ -388,18 +388,18 @@ bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::Message
return false; return false;
} }
int y = -(messages[start]->getHeight() * (fmod(_scrollbar.getCurrentValue(), 1))) + 12; int y = -(messages[start]->getHeight() * (fmod(_scrollbar.getCurrentValue(), 1)));
for (int i = start; i < messages.getSize(); ++i) { for (int i = start; i < messages.getSize(); ++i) {
auto message = messages[i]; auto message = messages[i];
y += message->getHeight(); if (p.y() < y + message->getHeight()) {
relativePos = QPoint(p.x(), p.y() - y);
if (p.y() < y) {
relativePos = QPoint(p.x(), y - p.y());
_message = message; _message = message;
return true; return true;
} }
y += message->getHeight();
} }
return false; return false;