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

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;
}
_userPopupWidget.setName(user); auto &link = hoverWord.getLink();
_userPopupWidget.move(event->screenPos().toPoint());
_userPopupWidget.show(); switch (link.getType()) {
_userPopupWidget.setFocus(); 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, 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;