From 0ca916717cc489811b89bb3d77a8030fb97e5510 Mon Sep 17 00:00:00 2001 From: fourtf Date: Tue, 16 Jan 2018 02:39:31 +0100 Subject: [PATCH] added basic buggy text copying --- src/messages/layouts/messagelayout.cpp | 40 +-- src/messages/layouts/messagelayout.hpp | 1 + .../layouts/messagelayoutcontainer.cpp | 35 +++ .../layouts/messagelayoutcontainer.hpp | 2 + src/messages/layouts/messagelayoutelement.cpp | 11 +- src/widgets/helper/channelview.cpp | 253 +----------------- 6 files changed, 68 insertions(+), 274 deletions(-) diff --git a/src/messages/layouts/messagelayout.cpp b/src/messages/layouts/messagelayout.cpp index cfe791608..39cf327bc 100644 --- a/src/messages/layouts/messagelayout.cpp +++ b/src/messages/layouts/messagelayout.cpp @@ -235,48 +235,20 @@ const MessageLayoutElement *MessageLayout::getElementAt(QPoint point) return this->container.getElementAt(point); } -// XXX(pajlada): This is probably not the optimal way to calculate this int MessageLayout::getLastCharacterIndex() const { - // fourtf: xD - // // 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 LayoutItem &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 LayoutItem &part = this->wordParts[i]; - - // index += part.getWord().isImage() ? 2 : part.getText().length() + 1; - // } - - // for (int i = lineStart; i < lineEnd; i++) { - // const LayoutItem &part = this->wordParts[i]; - - // index += part.getCharacterLength(); - // } - - // return index; - - return 0; + return this->container.getLastCharacterIndex(); } int MessageLayout::getSelectionIndex(QPoint position) { return this->container.getSelectionIndex(position); } + +void MessageLayout::addSelectionText(QString &str, int from, int to) +{ + this->container.addSelectionText(str, from, to); +} } // namespace layouts } // namespace messages } // namespace chatterino diff --git a/src/messages/layouts/messagelayout.hpp b/src/messages/layouts/messagelayout.hpp index 5fe40fb26..08fac98e7 100644 --- a/src/messages/layouts/messagelayout.hpp +++ b/src/messages/layouts/messagelayout.hpp @@ -49,6 +49,7 @@ public: const MessageLayoutElement *getElementAt(QPoint point); int getLastCharacterIndex() const; int getSelectionIndex(QPoint position); + void addSelectionText(QString &str, int from, int to); // Misc bool isDisabled() const; diff --git a/src/messages/layouts/messagelayoutcontainer.cpp b/src/messages/layouts/messagelayoutcontainer.cpp index 34f55e4d3..465ce2de8 100644 --- a/src/messages/layouts/messagelayoutcontainer.cpp +++ b/src/messages/layouts/messagelayoutcontainer.cpp @@ -379,6 +379,41 @@ int MessageLayoutContainer::getSelectionIndex(QPoint point) return index; } + +// fourtf: no idea if this is acurate LOL +int MessageLayoutContainer::getLastCharacterIndex() const +{ + if (this->lines.size() == 0) { + return 0; + } + return this->lines.back().endCharIndex; +} + +void MessageLayoutContainer::addSelectionText(QString &str, int from, int to) +{ + int index = 0; + bool xd = true; + + for (std::unique_ptr &ele : this->elements) { + int c = ele->getSelectionIndexCount(); + + if (xd) { + if (index + c > from) { + ele->addCopyTextToString(str, index - from, to - from); + xd = false; + } + } else { + if (index + c > from) { + ele->addCopyTextToString(str, 0, index - to); + break; + } else { + ele->addCopyTextToString(str); + } + } + + index += c; + } +} } // namespace layouts } // namespace messages } // namespace chatterino diff --git a/src/messages/layouts/messagelayoutcontainer.hpp b/src/messages/layouts/messagelayoutcontainer.hpp index 2499fd1c4..501ff06fd 100644 --- a/src/messages/layouts/messagelayoutcontainer.hpp +++ b/src/messages/layouts/messagelayoutcontainer.hpp @@ -71,6 +71,8 @@ public: // selection int getSelectionIndex(QPoint point); + int getLastCharacterIndex() const; + void addSelectionText(QString &str, int from, int to); private: struct Line { diff --git a/src/messages/layouts/messagelayoutelement.cpp b/src/messages/layouts/messagelayoutelement.cpp index c34449fc5..4cef16981 100644 --- a/src/messages/layouts/messagelayoutelement.cpp +++ b/src/messages/layouts/messagelayoutelement.cpp @@ -54,7 +54,11 @@ ImageLayoutElement::ImageLayoutElement(MessageElement &_creator, Image &_image, void ImageLayoutElement::addCopyTextToString(QString &str, int from, int to) const { - str += ""; + str += this->image.getName(); + + if (this->hasTrailingSpace()) { + str += " "; + } } int ImageLayoutElement::getSelectionIndexCount() @@ -117,6 +121,11 @@ TextLayoutElement::TextLayoutElement(MessageElement &_creator, QString &_text, Q void TextLayoutElement::addCopyTextToString(QString &str, int from, int to) const { + str += this->text.mid(from, to - from); + + if (this->hasTrailingSpace()) { + str += " "; + } } int TextLayoutElement::getSelectionIndexCount() diff --git a/src/widgets/helper/channelview.cpp b/src/widgets/helper/channelview.cpp index 08b643816..a02dfab3f 100644 --- a/src/widgets/helper/channelview.cpp +++ b/src/widgets/helper/channelview.cpp @@ -235,106 +235,26 @@ Scrollbar &ChannelView::getScrollBar() QString ChannelView::getSelectedText() { - // fourtf: xD - // auto messagesSnapshot = this->getMessagesSnapshot(); + QString result = ""; - // QString text; - // bool isSingleMessage = this->selection.isSingleMessage(); + messages::LimitedQueueSnapshot messagesSnapshot = this->getMessagesSnapshot(); - // size_t i = std::max(0, this->selection.min.messageIndex); + Selection selection = this->selection; - // int charIndex = 0; + if (selection.isEmpty()) { + return result; + } - // bool first = true; + for (int msg = selection.min.messageIndex; msg <= selection.min.messageIndex; msg++) { + MessageLayoutPtr layout = messagesSnapshot[msg]; + int from = msg == selection.min.messageIndex ? selection.min.charIndex : 0; + int to = msg == selection.max.messageIndex ? selection.max.charIndex + : layout->getLastCharacterIndex(); - // auto addPart = [&](const MessageLayoutElement &part, int from = 0, int to = -1) { - // if (part.getCopyText().isEmpty()) { - // return; - // } + layout->addSelectionText(result, from, to); + } - // if (part.getWord().isText()) { - // text += part.getText().mid(from, to); - // } else { - // text += part.getCopyText(); - // } - // }; - - // // first line - // for (const messages::MessageLayoutElement &part : messagesSnapshot[i]->getWordParts()) { - // int charLength = part.getCharacterLength(); - - // if (charIndex + charLength < this->selection.min.charIndex) { - // charIndex += charLength; - // continue; - // } - - // if (first) { - // first = false; - // bool isSingleWord = - // isSingleMessage && - // this->selection.max.charIndex - charIndex < part.getCharacterLength(); - - // if (isSingleWord) { - // // return single word - // addPart(part, this->selection.min.charIndex - charIndex, - // this->selection.max.charIndex - this->selection.min.charIndex); - // return text; - // } else { - // // add first word of the selection - // addPart(part, this->selection.min.charIndex - charIndex); - // } - // } else if (isSingleMessage && charIndex + charLength >= selection.max.charIndex) { - // addPart(part, 0, this->selection.max.charIndex - charIndex); - - // return text; - // } else { - // text += part.getCopyText() + (part.hasTrailingSpace() ? " " : ""); - // } - - // charIndex += charLength; - // } - - // text += "\n"; - - // // middle lines - // for (i++; (int)i < this->selection.max.messageIndex; i++) { - // for (const messages::MessageLayoutElement &part : messagesSnapshot[i]->getWordParts()) - // { - // if (!part.getCopyText().isEmpty()) { - // text += part.getCopyText(); - - // if (part.hasTrailingSpace()) { - // text += " "; - // } - // } - // } - // text += "\n"; - // } - - // // last line - // charIndex = 0; - - // for (const messages::MessageLayoutElement &part : - // messagesSnapshot[this->selection.max.messageIndex]->getWordParts()) { - // int charLength = part.getCharacterLength(); - - // if (charIndex + charLength >= this->selection.max.charIndex) { - // addPart(part, 0, this->selection.max.charIndex - charIndex); - - // return text; - // } - - // text += part.getCopyText(); - - // if (part.hasTrailingSpace()) { - // text += " "; - // } - - // charIndex += charLength; - // } - - // return text; - return ""; + return result; } bool ChannelView::hasSelection() @@ -588,151 +508,6 @@ void ChannelView::drawMessages(QPainter &painter) } } -// void ChannelView::drawMessageSelection(QPainter &painter, messages::MessageLayout *messageRef, -// int messageIndex, int bufferHeight) -//{ -// if (this->selection.min.messageIndex > messageIndex || -// this->selection.max.messageIndex < messageIndex) { -// return; -// } -// -// QColor selectionColor = this->themeManager.messages.selection; -// -// int charIndex = 0; -// size_t i = 0; -// auto &parts = messageRef->getWordParts(); -// -// int currentLineNumber = 0; -// QRect rect; -// -// if (parts.size() > 0) { -// if (selection.min.messageIndex == messageIndex) { -// rect.setTop(parts.at(0).getY()); -// } -// rect.setLeft(parts.at(0).getX()); -// } -// -// // skip until selection start -// if (this->selection.min.messageIndex == messageIndex && this->selection.min.charIndex != 0) { -// for (; i < parts.size(); i++) { -// const messages::MessageLayoutElement &part = parts.at(i); -// auto characterLength = part.getCharacterLength(); -// -// if (characterLength + charIndex > selection.min.charIndex) { -// break; -// } -// -// charIndex += characterLength; -// currentLineNumber = part.getLineNumber(); -// } -// -// if (i >= parts.size()) { -// return; -// } -// -// // handle word that has a cut of selection -// const messages::MessageLayoutElement &part = parts.at(i); -// -// // check if selection if single word -// int characterLength = part.getCharacterLength(); -// bool isSingleWord = charIndex + characterLength > this->selection.max.charIndex && -// this->selection.max.messageIndex == messageIndex; -// -// rect = part.getRect(); -// currentLineNumber = part.getLineNumber(); -// -// if (part.getWord().isText()) { -// int offset = this->selection.min.charIndex - charIndex; -// -// for (int j = 0; j < offset; j++) { -// rect.setLeft(rect.left() + part.getCharWidth(j, this->getDpiMultiplier())); -// } -// -// if (isSingleWord) { -// int length = (this->selection.max.charIndex - charIndex) - offset; -// -// rect.setRight(part.getX()); -// -// for (int j = 0; j < offset + length; j++) { -// rect.setRight(rect.right() + part.getCharWidth(j, this->getDpiMultiplier())); -// } -// -// painter.fillRect(rect, selectionColor); -// -// return; -// } -// } else { -// if (isSingleWord) { -// if (charIndex + 1 != this->selection.max.charIndex) { -// rect.setRight(part.getX() + part.getWord().getImage().getScaledWidth()); -// } -// painter.fillRect(rect, selectionColor); -// -// return; -// } -// -// if (charIndex != this->selection.min.charIndex) { -// rect.setLeft(part.getX() + part.getWord().getImage().getScaledWidth()); -// } -// } -// -// i++; -// charIndex += characterLength; -// } -// -// // go through lines and draw selection -// for (; i < parts.size(); i++) { -// const messages::MessageLayoutElement &part = parts.at(i); -// -// int charLength = part.getCharacterLength(); -// -// bool isLastSelectedWord = this->selection.max.messageIndex == messageIndex && -// charIndex + charLength > this->selection.max.charIndex; -// -// if (part.getLineNumber() == currentLineNumber) { -// rect.setLeft(std::min(rect.left(), part.getX())); -// rect.setTop(std::min(rect.top(), part.getY())); -// rect.setRight(std::max(rect.right(), part.getRight())); -// rect.setBottom(std::max(rect.bottom(), part.getBottom() - 1)); -// } else { -// painter.fillRect(rect, selectionColor); -// -// currentLineNumber = part.getLineNumber(); -// -// rect = part.getRect(); -// } -// -// if (isLastSelectedWord) { -// if (part.getWord().isText()) { -// int offset = this->selection.min.charIndex - charIndex; -// -// int length = (this->selection.max.charIndex - charIndex) - offset; -// -// rect.setRight(part.getX()); -// -// for (int j = 0; j < offset + length; j++) { -// rect.setRight(rect.right() + part.getCharWidth(j, this->getDpiMultiplier())); -// } -// } else { -// if (this->selection.max.charIndex == charIndex) { -// rect.setRight(part.getX()); -// } -// } -// painter.fillRect(rect, selectionColor); -// -// return; -// } -// -// charIndex += charLength; -// } -// -// if (this->selection.max.messageIndex != messageIndex) { -// rect.setBottom(bufferHeight); -// } -// -// painter.fillRect(rect, selectionColor); -//} - void ChannelView::wheelEvent(QWheelEvent *event) { if (this->scrollBar.isVisible()) {