added basic buggy text copying

This commit is contained in:
fourtf 2018-01-16 02:39:31 +01:00
parent a33ac76f99
commit 0ca916717c
6 changed files with 68 additions and 274 deletions

View file

@ -235,48 +235,20 @@ const MessageLayoutElement *MessageLayout::getElementAt(QPoint point)
return this->container.getElementAt(point); return this->container.getElementAt(point);
} }
// XXX(pajlada): This is probably not the optimal way to calculate this
int MessageLayout::getLastCharacterIndex() const int MessageLayout::getLastCharacterIndex() const
{ {
// fourtf: xD return this->container.getLastCharacterIndex();
// // 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;
} }
int MessageLayout::getSelectionIndex(QPoint position) int MessageLayout::getSelectionIndex(QPoint position)
{ {
return this->container.getSelectionIndex(position); return this->container.getSelectionIndex(position);
} }
void MessageLayout::addSelectionText(QString &str, int from, int to)
{
this->container.addSelectionText(str, from, to);
}
} // namespace layouts } // namespace layouts
} // namespace messages } // namespace messages
} // namespace chatterino } // namespace chatterino

View file

@ -49,6 +49,7 @@ public:
const MessageLayoutElement *getElementAt(QPoint point); const MessageLayoutElement *getElementAt(QPoint point);
int getLastCharacterIndex() const; int getLastCharacterIndex() const;
int getSelectionIndex(QPoint position); int getSelectionIndex(QPoint position);
void addSelectionText(QString &str, int from, int to);
// Misc // Misc
bool isDisabled() const; bool isDisabled() const;

View file

@ -379,6 +379,41 @@ int MessageLayoutContainer::getSelectionIndex(QPoint point)
return index; 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<MessageLayoutElement> &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 layouts
} // namespace messages } // namespace messages
} // namespace chatterino } // namespace chatterino

View file

@ -71,6 +71,8 @@ public:
// selection // selection
int getSelectionIndex(QPoint point); int getSelectionIndex(QPoint point);
int getLastCharacterIndex() const;
void addSelectionText(QString &str, int from, int to);
private: private:
struct Line { struct Line {

View file

@ -54,7 +54,11 @@ ImageLayoutElement::ImageLayoutElement(MessageElement &_creator, Image &_image,
void ImageLayoutElement::addCopyTextToString(QString &str, int from, int to) const void ImageLayoutElement::addCopyTextToString(QString &str, int from, int to) const
{ {
str += "<image>"; str += this->image.getName();
if (this->hasTrailingSpace()) {
str += " ";
}
} }
int ImageLayoutElement::getSelectionIndexCount() int ImageLayoutElement::getSelectionIndexCount()
@ -117,6 +121,11 @@ TextLayoutElement::TextLayoutElement(MessageElement &_creator, QString &_text, Q
void TextLayoutElement::addCopyTextToString(QString &str, int from, int to) const void TextLayoutElement::addCopyTextToString(QString &str, int from, int to) const
{ {
str += this->text.mid(from, to - from);
if (this->hasTrailingSpace()) {
str += " ";
}
} }
int TextLayoutElement::getSelectionIndexCount() int TextLayoutElement::getSelectionIndexCount()

View file

@ -235,106 +235,26 @@ Scrollbar &ChannelView::getScrollBar()
QString ChannelView::getSelectedText() QString ChannelView::getSelectedText()
{ {
// fourtf: xD QString result = "";
// auto messagesSnapshot = this->getMessagesSnapshot();
// QString text; messages::LimitedQueueSnapshot<MessageLayoutPtr> messagesSnapshot = this->getMessagesSnapshot();
// bool isSingleMessage = this->selection.isSingleMessage();
// 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) { layout->addSelectionText(result, from, to);
// if (part.getCopyText().isEmpty()) { }
// return;
// }
// if (part.getWord().isText()) { return result;
// 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 "";
} }
bool ChannelView::hasSelection() 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) void ChannelView::wheelEvent(QWheelEvent *event)
{ {
if (this->scrollBar.isVisible()) { if (this->scrollBar.isVisible()) {