From 6701c3d0b2164a964060e2e5c78c06a467d64f96 Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 15 Jan 2018 04:08:48 +0100 Subject: [PATCH] fixed compiling --- src/messages/layouts/messagelayout.cpp | 75 +------------------ .../layouts/messagelayoutcontainer.cpp | 55 ++++++++++++++ .../layouts/messagelayoutcontainer.hpp | 11 +++ src/widgets/basewindow.cpp | 5 +- src/widgets/window.cpp | 4 - 5 files changed, 70 insertions(+), 80 deletions(-) diff --git a/src/messages/layouts/messagelayout.cpp b/src/messages/layouts/messagelayout.cpp index a297b4beb..f275f9def 100644 --- a/src/messages/layouts/messagelayout.cpp +++ b/src/messages/layouts/messagelayout.cpp @@ -275,80 +275,7 @@ int MessageLayout::getLastCharacterIndex() const int MessageLayout::getSelectionIndex(QPoint position) { - // if (this->wordParts.size() == 0) { - // return 0; - // } - - // // 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++) { - // LayoutItem &part = this->wordParts[i]; - - // if (part.getLineNumber() != 0 && position.y() < part.getY()) { - // break; - // } - - // 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++) { - // LayoutItem &part = this->wordParts[i]; - - // index += part.getWord().isImage() ? 2 : part.getText().length() + 1; - // } - - // for (int i = lineStart; i < lineEnd; i++) { - // LayoutItem &part = this->wordParts[i]; - - // // curser is left of the word part - // if (position.x() < part.getX()) { - // break; - // } - - // // cursor is right of the word part - // if (position.x() > part.getX() + part.getWidth()) { - // index += part.getCharacterLength(); - // continue; - // } - - // // cursor is over the word part - // if (part.getWord().isImage()) { - // if (position.x() - part.getX() > part.getWidth() / 2) { - // index++; - // } - // } else { - // // TODO: use word.getCharacterWidthCache(); - - // auto text = part.getText(); - - // int x = part.getX(); - - // for (int j = 0; j < text.length(); j++) { - // if (x > position.x()) { - // break; - // } - - // index++; - // x = part.getX() + part.getWord().getFontMetrics(this->scale).width(text, j + - // 1); - // } - // } - - // break; - // } - - // return index; - - return 0; + return this->container.getSelectionIndex(position); } } // namespace layouts } // namespace messages diff --git a/src/messages/layouts/messagelayoutcontainer.cpp b/src/messages/layouts/messagelayoutcontainer.cpp index 4b3417c93..d6203306e 100644 --- a/src/messages/layouts/messagelayoutcontainer.cpp +++ b/src/messages/layouts/messagelayoutcontainer.cpp @@ -4,6 +4,7 @@ #include "messages/selection.hpp" #include "singletons/settingsmanager.hpp" +#include #include #define COMPACT_EMOTES_OFFSET 6 @@ -105,6 +106,8 @@ void MessageLayoutContainer::breakLine() element->getRect().y() + this->lineHeight + yExtra)); } + this->lines.push_back({(int)lineStart, QRect(0, this->currentY, this->width, lineHeight)}); + this->lineStart = this->elements.size(); this->currentX = 0; this->currentY += this->lineHeight; @@ -127,6 +130,11 @@ void MessageLayoutContainer::finish() if (!this->atStartOfLine()) { this->breakLine(); } + + if (this->lines.size() != 0) { + this->lines[0].rect.setTop(0); + this->lines.back().rect.setBottom(this->height); + } } MessageLayoutElement *MessageLayoutContainer::getElementAt(QPoint point) @@ -159,6 +167,53 @@ void MessageLayoutContainer::paintSelection(QPainter &painter, int messageIndex, Selection &selection) { } + +// selection +int MessageLayoutContainer::getSelectionIndex(QPoint point) +{ + if (this->elements.size() == 0) { + return 0; + } + + auto line = this->lines.begin(); + + for (; line != this->lines.end(); line++) { + if (line->rect.contains(point)) { + break; + } + } + + assert(line != this->lines.end()); + + int lineStart = line->startIndex; + line++; + int lineEnd = line == this->lines.end() ? this->elements.size() : line->startIndex; + + int index = 0; + + for (int i = 0; i < lineEnd; i++) { + // end of line + if (i == lineEnd) { + break; + } + + // before line + if (i < lineStart) { + index += this->elements[i]->getSelectionIndexCount(); + continue; + } + + // this is the word + if (point.x() > this->elements[i]->getRect().left()) { + index += this->elements[i]->getMouseOverIndex(point); + break; + } + } + + qDebug() << index; + + return index; +} } // namespace layouts } // namespace messages } // namespace chatterino diff --git a/src/messages/layouts/messagelayoutcontainer.hpp b/src/messages/layouts/messagelayoutcontainer.hpp index 8341884e6..362036b20 100644 --- a/src/messages/layouts/messagelayoutcontainer.hpp +++ b/src/messages/layouts/messagelayoutcontainer.hpp @@ -4,6 +4,7 @@ #include #include +#include class QPainter; @@ -67,6 +68,9 @@ public: void paintAnimatedElements(QPainter &painter, int yOffset); void paintSelection(QPainter &painter, int messageIndex, Selection &selection); + // selection + int getSelectionIndex(QPoint point); + private: // helpers void _addElement(MessageLayoutElement *element); @@ -79,6 +83,13 @@ private: int lineHeight = 0; int spaceWidth = 4; std::vector> elements; + + struct Line { + int startIndex; + QRect rect; + }; + + std::vector lines; }; } // namespace layouts } // namespace messages diff --git a/src/widgets/basewindow.cpp b/src/widgets/basewindow.cpp index 0a2c1c57a..36f693db1 100644 --- a/src/widgets/basewindow.cpp +++ b/src/widgets/basewindow.cpp @@ -18,11 +18,12 @@ #include #include -#include "widgets/helper/rippleeffectlabel.hpp" #define WM_DPICHANGED 0x02E0 #endif +#include "widgets/helper/rippleeffectlabel.hpp" + namespace chatterino { namespace widgets { @@ -114,7 +115,7 @@ void BaseWindow::init() QWidget *BaseWindow::getLayoutContainer() { - if (this->enableCustomFrame) { + if (this->hasCustomWindowFrame()) { return this->layoutBase; } else { return this; diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index ba78195b3..cf87c21fb 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -50,11 +50,7 @@ Window::Window(const QString &windowName, singletons::ThemeManager &_themeManage this->getLayoutContainer()->setLayout(layout); // set margin - // if (SettingsManager::getInstance().useCustomWindowFrame.get()) { - // layout->setMargin(1); - // } else { layout->setMargin(0); - // } this->refreshTheme();