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

@ -1,167 +1,168 @@
#-------------------------------------------------
#
# Project created by QtCreator 2016-12-28T18:23:35
#
#-------------------------------------------------
QT += core gui network
CONFIG += communi
COMMUNI += core model util
CONFIG += c++14
include(lib/libcommuni/src/src.pri)
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = chatterino
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += IRC_NAMESPACE=Communi
# do not use windows min/max macros
win32 {
DEFINES += NOMINMAX
}
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
channel.cpp \
colorscheme.cpp \
emojis.cpp \
ircmanager.cpp \
messages/lazyloadedimage.cpp \
messages/link.cpp \
messages/message.cpp \
messages/word.cpp \
messages/wordpart.cpp \
resources.cpp \
widgets/chatwidget.cpp \
widgets/chatwidgetheader.cpp \
widgets/chatwidgetheaderbutton.cpp \
widgets/chatwidgetinput.cpp \
widgets/chatwidgetview.cpp \
widgets/mainwindow.cpp \
widgets/notebook.cpp \
widgets/notebookbutton.cpp \
widgets/notebookpage.cpp \
widgets/notebookpagedroppreview.cpp \
widgets/notebooktab.cpp \
widgets/scrollbar.cpp \
widgets/scrollbarhighlight.cpp \
widgets/settingsdialog.cpp \
widgets/settingsdialogtab.cpp \
widgets/textinputdialog.cpp \
messages/messageref.cpp \
logging/loggingmanager.cpp \
logging/loggingchannel.cpp \
windowmanager.cpp \
channelmanager.cpp \
fontmanager.cpp \
settingsmanager.cpp \
emotemanager.cpp \
messages/messagebuilder.cpp \
twitch/twitchmessagebuilder.cpp \
twitch/twitchparsemessage.cpp \
widgets/fancybutton.cpp \
widgets/titlebar.cpp \
appdatapath.cpp \
accountmanager.cpp \
twitch/twitchaccount.cpp \
ircaccount.cpp \
widgets/accountpopup.cpp
HEADERS += \
asyncexec.h \
channel.h \
colorscheme.h \
common.h \
concurrentmap.h \
emojis.h \
ircmanager.h \
messages/lazyloadedimage.h \
messages/link.h \
messages/message.h \
messages/word.h \
messages/wordpart.h \
resources.h \
setting.h \
twitch/emotevalue.h \
widgets/chatwidget.h \
widgets/chatwidgetheader.h \
widgets/chatwidgetheaderbutton.h \
widgets/chatwidgetinput.h \
widgets/chatwidgetview.h \
widgets/mainwindow.h \
widgets/notebook.h \
widgets/notebookbutton.h \
widgets/notebookpage.h \
widgets/notebookpagedroppreview.h \
widgets/notebooktab.h \
widgets/scrollbar.h \
widgets/scrollbarhighlight.h \
widgets/settingsdialog.h \
widgets/settingsdialogtab.h \
widgets/signallabel.h \
widgets/textinputdialog.h \
widgets/resizingtextedit.h \
settingssnapshot.h \
messages/limitedqueue.h \
messages/limitedqueuesnapshot.h \
messages/messageref.h \
logging/loggingmanager.h \
logging/loggingchannel.h \
channelmanager.h \
windowmanager.h \
settingsmanager.h \
fontmanager.h \
emotemanager.h \
util/urlfetch.h \
messages/messageparseargs.h \
messages/messagebuilder.h \
twitch/twitchmessagebuilder.h \
twitch/twitchparsemessage.h \
widgets/fancybutton.h \
widgets/titlebar.h \
appdatapath.h \
accountmanager.h \
twitch/twitchaccount.h \
ircaccount.h \
widgets/accountpopup.h
PRECOMPILED_HEADER =
RESOURCES += \
resources.qrc
DISTFILES +=
# Include boost
win32 {
INCLUDEPATH += C:\local\boost\
}
# Optional dependency on windows sdk 7.1
win32:exists(C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\Windows.h) {
LIBS += -L"C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib" \
-ldwmapi \
-lgdi32
SOURCES += platform/borderless/qwinwidget.cpp \
platform/borderless/winnativewindow.cpp \
platform/borderless/widget.cpp
HEADERS += platform/borderless/qwinwidget.h \
platform/borderless/winnativewindow.h \
platform/borderless/widget.h
DEFINES += "USEWINSDK"
}
macx {
INCLUDEPATH += /usr/local/include
}
FORMS += \
forms/accountpopupform.ui
#-------------------------------------------------
#
# Project created by QtCreator 2016-12-28T18:23:35
#
#-------------------------------------------------
QT += core gui network
CONFIG += communi
COMMUNI += core model util
CONFIG += c++14
include(lib/libcommuni/src/src.pri)
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = chatterino
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += IRC_NAMESPACE=Communi
# do not use windows min/max macros
win32 {
DEFINES += NOMINMAX
}
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
channel.cpp \
colorscheme.cpp \
emojis.cpp \
ircmanager.cpp \
messages/lazyloadedimage.cpp \
messages/link.cpp \
messages/message.cpp \
messages/word.cpp \
messages/wordpart.cpp \
resources.cpp \
widgets/chatwidget.cpp \
widgets/chatwidgetheader.cpp \
widgets/chatwidgetheaderbutton.cpp \
widgets/chatwidgetinput.cpp \
widgets/chatwidgetview.cpp \
widgets/mainwindow.cpp \
widgets/notebook.cpp \
widgets/notebookbutton.cpp \
widgets/notebookpage.cpp \
widgets/notebookpagedroppreview.cpp \
widgets/notebooktab.cpp \
widgets/scrollbar.cpp \
widgets/scrollbarhighlight.cpp \
widgets/settingsdialog.cpp \
widgets/settingsdialogtab.cpp \
widgets/textinputdialog.cpp \
messages/messageref.cpp \
logging/loggingmanager.cpp \
logging/loggingchannel.cpp \
windowmanager.cpp \
channelmanager.cpp \
fontmanager.cpp \
settingsmanager.cpp \
emotemanager.cpp \
messages/messagebuilder.cpp \
twitch/twitchmessagebuilder.cpp \
twitch/twitchparsemessage.cpp \
widgets/fancybutton.cpp \
widgets/titlebar.cpp \
appdatapath.cpp \
accountmanager.cpp \
twitch/twitchaccount.cpp \
ircaccount.cpp \
widgets/accountpopup.cpp
HEADERS += \
asyncexec.h \
channel.h \
colorscheme.h \
common.h \
concurrentmap.h \
emojis.h \
ircmanager.h \
messages/lazyloadedimage.h \
messages/link.h \
messages/message.h \
messages/word.h \
messages/wordpart.h \
resources.h \
setting.h \
twitch/emotevalue.h \
widgets/chatwidget.h \
widgets/chatwidgetheader.h \
widgets/chatwidgetheaderbutton.h \
widgets/chatwidgetinput.h \
widgets/chatwidgetview.h \
widgets/mainwindow.h \
widgets/notebook.h \
widgets/notebookbutton.h \
widgets/notebookpage.h \
widgets/notebookpagedroppreview.h \
widgets/notebooktab.h \
widgets/scrollbar.h \
widgets/scrollbarhighlight.h \
widgets/settingsdialog.h \
widgets/settingsdialogtab.h \
widgets/signallabel.h \
widgets/textinputdialog.h \
widgets/resizingtextedit.h \
settingssnapshot.h \
messages/limitedqueue.h \
messages/limitedqueuesnapshot.h \
messages/messageref.h \
logging/loggingmanager.h \
logging/loggingchannel.h \
channelmanager.h \
windowmanager.h \
settingsmanager.h \
fontmanager.h \
emotemanager.h \
util/urlfetch.h \
messages/messageparseargs.h \
messages/messagebuilder.h \
twitch/twitchmessagebuilder.h \
twitch/twitchparsemessage.h \
widgets/fancybutton.h \
widgets/titlebar.h \
appdatapath.h \
accountmanager.h \
twitch/twitchaccount.h \
ircaccount.h \
widgets/accountpopup.h \
util/distancebetweenpoints.h
PRECOMPILED_HEADER =
RESOURCES += \
resources.qrc
DISTFILES +=
# Include boost
win32 {
INCLUDEPATH += C:\local\boost\
}
# Optional dependency on windows sdk 7.1
win32:exists(C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\Windows.h) {
LIBS += -L"C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib" \
-ldwmapi \
-lgdi32
SOURCES += platform/borderless/qwinwidget.cpp \
platform/borderless/winnativewindow.cpp \
platform/borderless/widget.cpp
HEADERS += platform/borderless/qwinwidget.h \
platform/borderless/winnativewindow.h \
platform/borderless/widget.h
DEFINES += "USEWINSDK"
}
macx {
INCLUDEPATH += /usr/local/include
}
FORMS += \
forms/accountpopupform.ui

View file

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

View file

@ -94,7 +94,7 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
int x = MARGIN_LEFT;
int y = MARGIN_TOP;
int right = width - MARGIN_RIGHT - MARGIN_LEFT;
int right = width - MARGIN_RIGHT;
int lineNumber = 0;
int lineStart = 0;
@ -258,17 +258,16 @@ int MessageRef::getSelectionIndex(QPoint position)
for (int i = 0; i < _wordParts.size(); i++) {
WordPart &part = _wordParts[i];
// return if curser under the word
if (position.y() >= part.getBottom()) {
if (part.getLineNumber() != 0 && position.y() < part.getY()) {
break;
}
if (part.getLineNumber() != lineNumber) {
lineStart = i;
lineStart = i - 1;
lineNumber = part.getLineNumber();
}
lineEnd = i;
lineEnd = part.getLineNumber() == 0 ? i : i + 1;
}
// count up to the cursor
@ -289,7 +288,7 @@ int MessageRef::getSelectionIndex(QPoint position)
}
// 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;
continue;
}
@ -316,26 +315,6 @@ int MessageRef::getSelectionIndex(QPoint position)
}
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 += ": ";
}
b.appendWord(
Word(userDisplayString, Word::Username, usernameColor, userDisplayString, QString()));
b.appendWord(Word(userDisplayString, Word::Username, usernameColor, userDisplayString,
QString(), Link(Link::UserInfo, b.userName)));
// highlights
// 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 "settingsmanager.h"
#include "ui_accountpopupform.h"
#include "util/distancebetweenpoints.h"
#include "widgets/chatwidget.h"
#include <math.h>
@ -55,6 +56,7 @@ bool ChatWidgetView::layoutMessages()
bool showScrollbar = false, redraw = false;
int start = _scrollbar.getCurrentValue();
int layoutWidth = _scrollbar.isVisible() ? width() - _scrollbar.width() : width();
// layout the visible messages in the view
if (messages.getSize() > start) {
@ -63,7 +65,7 @@ bool ChatWidgetView::layoutMessages()
for (int i = start; i < messages.getSize(); ++i) {
auto message = messages[i];
redraw |= message->layout(width(), true);
redraw |= message->layout(layoutWidth, true);
y += message->getHeight();
@ -79,7 +81,7 @@ bool ChatWidgetView::layoutMessages()
for (int i = messages.getSize() - 1; i >= 0; i--) {
auto *message = messages[i].get();
message->layout(width(), true);
message->layout(layoutWidth, true);
h -= message->getHeight();
@ -298,6 +300,9 @@ void ChatWidgetView::mouseMoveEvent(QMouseEvent *event)
return;
}
int index = message->getSelectionIndex(relativePos);
qDebug() << index;
messages::Word hoverWord;
if (!message->tryGetWordPart(relativePos, hoverWord)) {
@ -305,11 +310,7 @@ void ChatWidgetView::mouseMoveEvent(QMouseEvent *event)
return;
}
int index = message->getSelectionIndex(relativePos);
qDebug() << index;
if (hoverWord.getLink().getIsValid()) {
if (hoverWord.getLink().isValid()) {
setCursor(Qt::PointingHandCursor);
} else {
setCursor(Qt::ArrowCursor);
@ -322,17 +323,6 @@ void ChatWidgetView::mousePressEvent(QMouseEvent *event)
_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)
{
if (!_mouseDown) {
@ -343,7 +333,7 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
_mouseDown = false;
float distance = distanceBetweenPoints(_lastPressPosition, event->screenPos());
float distance = util::distanceBetweenPoints(_lastPressPosition, event->screenPos());
qDebug() << "Distance: " << distance;
@ -366,15 +356,25 @@ void ChatWidgetView::mouseReleaseEvent(QMouseEvent *event)
return;
}
auto _message = message->getMessage();
auto user = _message->getUserName();
messages::Word hoverWord;
qDebug() << "Clicked " << user << "s message";
if (!message->tryGetWordPart(relativePos, hoverWord)) {
return;
}
_userPopupWidget.setName(user);
_userPopupWidget.move(event->screenPos().toPoint());
_userPopupWidget.show();
_userPopupWidget.setFocus();
auto &link = hoverWord.getLink();
switch (link.getType()) {
case messages::Link::UserInfo:
auto user = message->getMessage()->getUserName();
_userPopupWidget.setName(user);
_userPopupWidget.move(event->screenPos().toPoint());
_userPopupWidget.show();
_userPopupWidget.setFocus();
qDebug() << "Clicked " << user << "s message";
break;
}
}
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;
}
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) {
auto message = messages[i];
y += message->getHeight();
if (p.y() < y) {
relativePos = QPoint(p.x(), y - p.y());
if (p.y() < y + message->getHeight()) {
relativePos = QPoint(p.x(), p.y() - y);
_message = message;
return true;
}
y += message->getHeight();
}
return false;