added text selection

This commit is contained in:
fourtf
2017-09-12 19:06:16 +02:00
parent 8b40393023
commit 81b1a8774b
18 changed files with 585 additions and 239 deletions
+97 -25
View File
@@ -22,14 +22,14 @@ LazyLoadedImage::LazyLoadedImage(EmoteManager &_emoteManager, WindowManager &_wi
const QString &tooltip, const QMargins &margin, bool isHat)
: emoteManager(_emoteManager)
, windowManager(_windowManager)
, _currentPixmap(nullptr)
, _url(url)
, _name(name)
, _tooltip(tooltip)
, _margin(margin)
, _ishat(isHat)
, _scale(scale)
, _isLoading(false)
, currentPixmap(nullptr)
, url(url)
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(false)
{
}
@@ -38,19 +38,19 @@ LazyLoadedImage::LazyLoadedImage(EmoteManager &_emoteManager, WindowManager &_wi
const QString &tooltip, const QMargins &margin, bool isHat)
: emoteManager(_emoteManager)
, windowManager(_windowManager)
, _currentPixmap(image)
, _name(name)
, _tooltip(tooltip)
, _margin(margin)
, _ishat(isHat)
, _scale(scale)
, _isLoading(true)
, currentPixmap(image)
, name(name)
, tooltip(tooltip)
, margin(margin)
, ishat(isHat)
, scale(scale)
, isLoading(true)
{
}
void LazyLoadedImage::loadImage()
{
util::urlFetch(_url, [=](QNetworkReply &reply) {
util::urlFetch(this->url, [=](QNetworkReply &reply) {
QByteArray array = reply.readAll();
QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly);
@@ -66,19 +66,19 @@ void LazyLoadedImage::loadImage()
if (first) {
first = false;
_currentPixmap = pixmap;
this->currentPixmap = pixmap;
}
FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
_allFrames.push_back(data);
this->allFrames.push_back(data);
}
}
if (_allFrames.size() > 1) {
_animated = true;
if (this->allFrames.size() > 1) {
this->animated = true;
this->emoteManager.getGifUpdateSignal().connect([this] {
gifUpdateTimout(); //
@@ -92,18 +92,90 @@ void LazyLoadedImage::loadImage()
void LazyLoadedImage::gifUpdateTimout()
{
_currentFrameOffset += GIF_FRAME_LENGTH;
this->currentFrameOffset += GIF_FRAME_LENGTH;
while (true) {
if (_currentFrameOffset > _allFrames.at(_currentFrame).duration) {
_currentFrameOffset -= _allFrames.at(_currentFrame).duration;
_currentFrame = (_currentFrame + 1) % _allFrames.size();
if (this->currentFrameOffset > this->allFrames.at(this->currentFrame).duration) {
this->currentFrameOffset -= this->allFrames.at(this->currentFrame).duration;
this->currentFrame = (this->currentFrame + 1) % this->allFrames.size();
} else {
break;
}
}
_currentPixmap = _allFrames[_currentFrame].image;
this->currentPixmap = this->allFrames[this->currentFrame].image;
}
const QPixmap *LazyLoadedImage::getPixmap()
{
if (!this->isLoading) {
this->isLoading = true;
loadImage();
}
return this->currentPixmap;
}
qreal LazyLoadedImage::getScale() const
{
return this->scale;
}
const QString &LazyLoadedImage::getUrl() const
{
return this->url;
}
const QString &LazyLoadedImage::getName() const
{
return this->name;
}
const QString &LazyLoadedImage::getTooltip() const
{
return this->tooltip;
}
const QMargins &LazyLoadedImage::getMargin() const
{
return this->margin;
}
bool LazyLoadedImage::getAnimated() const
{
return this->animated;
}
bool LazyLoadedImage::isHat() const
{
return this->ishat;
}
int LazyLoadedImage::getWidth() const
{
if (this->currentPixmap == nullptr) {
return 16;
}
return this->currentPixmap->width();
}
int LazyLoadedImage::getScaledWidth() const
{
return static_cast<int>(getWidth() * this->scale);
}
int LazyLoadedImage::getHeight() const
{
if (this->currentPixmap == nullptr) {
return 16;
}
return this->currentPixmap->height();
}
int LazyLoadedImage::getScaledHeight() const
{
return static_cast<int>(getHeight() * this->scale);
}
} // namespace messages
} // namespace chatterino
+24 -72
View File
@@ -25,66 +25,18 @@ public:
const QString &_tooltip = "", const QMargins &_margin = QMargins(),
bool isHat = false);
const QPixmap *getPixmap()
{
if (!_isLoading) {
_isLoading = true;
loadImage();
}
return _currentPixmap;
}
qreal getScale() const
{
return _scale;
}
const QString &getUrl() const
{
return _url;
}
const QString &getName() const
{
return _name;
}
const QString &getTooltip() const
{
return _tooltip;
}
const QMargins &getMargin() const
{
return _margin;
}
bool getAnimated() const
{
return _animated;
}
bool isHat() const
{
return _ishat;
}
int getWidth() const
{
if (_currentPixmap == nullptr) {
return 16;
}
return _currentPixmap->width();
}
int getHeight() const
{
if (_currentPixmap == nullptr) {
return 16;
}
return _currentPixmap->height();
}
const QPixmap *getPixmap();
qreal getScale() const;
const QString &getUrl() const;
const QString &getName() const;
const QString &getTooltip() const;
const QMargins &getMargin() const;
bool getAnimated() const;
bool isHat() const;
int getWidth() const;
int getScaledWidth() const;
int getHeight() const;
int getScaledHeight() const;
private:
EmoteManager &emoteManager;
@@ -95,20 +47,20 @@ private:
int duration;
};
QPixmap *_currentPixmap;
std::vector<FrameData> _allFrames;
int _currentFrame = 0;
int _currentFrameOffset = 0;
QPixmap *currentPixmap;
std::vector<FrameData> allFrames;
int currentFrame = 0;
int currentFrameOffset = 0;
QString _url;
QString _name;
QString _tooltip;
bool _animated = false;
QMargins _margin;
bool _ishat;
qreal _scale;
QString url;
QString name;
QString tooltip;
bool animated = false;
QMargins margin;
bool ishat;
qreal scale;
bool _isLoading;
bool isLoading;
void loadImage();
+6 -6
View File
@@ -29,12 +29,12 @@ Message::Message(const QString &text)
}
*/
Message::Message(const QString &text, const std::vector<Word> &words, const bool &highlight)
: text(text)
, highlightTab(highlight)
, words(words)
{
}
//Message::Message(const QString &text, const std::vector<Word> &words, const bool &highlight)
// : text(text)
// , highlightTab(highlight)
// , words(words)
//{
//}
bool Message::getCanHighlightTab() const
{
+2 -2
View File
@@ -24,8 +24,8 @@ class Message
{
public:
// explicit Message(const QString &text);
explicit Message(const QString &text, const std::vector<messages::Word> &words,
const bool &highlight);
//explicit Message(const QString &text, const std::vector<messages::Word> &words,
// const bool &highlight);
bool getCanHighlightTab() const;
const QString &getTimeoutUser() const;
+8 -7
View File
@@ -7,21 +7,19 @@ namespace chatterino {
namespace messages {
MessageBuilder::MessageBuilder()
: _words()
: message(new Message)
{
_parseTime = std::chrono::system_clock::now();
linkRegex.setPattern("[[:ascii:]]*\\.[a-zA-Z]+\\/?[[:ascii:]]*");
}
SharedMessage MessageBuilder::build()
{
return SharedMessage(new Message(this->originalMessage, _words, highlight));
return this->message;
}
void MessageBuilder::appendWord(const Word &word)
void MessageBuilder::appendWord(const Word &&word)
{
_words.push_back(word);
this->message->getWords().push_back(word);
}
void MessageBuilder::appendTimestamp()
@@ -59,6 +57,9 @@ void MessageBuilder::appendTimestamp(time_t time)
QString MessageBuilder::matchLink(const QString &string)
{
static QRegularExpression linkRegex("[[:ascii:]]*\\.[a-zA-Z]+\\/?[[:ascii:]]*");
static QRegularExpression httpRegex("\\bhttps?://");
auto match = linkRegex.match(string);
if (!match.hasMatch()) {
@@ -67,7 +68,7 @@ QString MessageBuilder::matchLink(const QString &string)
QString captured = match.captured();
if (!captured.contains(QRegularExpression("\\bhttps?://"))) {
if (!captured.contains(httpRegex)) {
captured.insert(0, "http://");
}
return captured;
+2 -1
View File
@@ -16,7 +16,7 @@ public:
SharedMessage build();
void appendWord(const Word &word);
void appendWord(const Word &&word);
void appendTimestamp();
void appendTimestamp(std::time_t time);
void setHighlight(const bool &value);
@@ -27,6 +27,7 @@ public:
QString originalMessage;
private:
std::shared_ptr<messages::Message> message;
std::vector<Word> _words;
bool highlight = false;
std::chrono::time_point<std::chrono::system_clock> _parseTime;
+20 -21
View File
@@ -6,8 +6,8 @@
#define MARGIN_LEFT 8
#define MARGIN_RIGHT 8
#define MARGIN_TOP 8
#define MARGIN_BOTTOM 8
#define MARGIN_TOP 4
#define MARGIN_BOTTOM 4
using namespace chatterino::messages;
@@ -142,18 +142,12 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
std::vector<short> &charWidths = word.getCharacterWidthCache();
if (charWidths.size() == 0) {
for (int i = 0; i < text.length(); i++) {
charWidths.push_back(metrics.charWidth(text, i));
}
}
for (int i = 2; i <= text.length(); i++) {
if ((width = width + charWidths[i - 1]) + MARGIN_LEFT > right) {
QString mid = text.mid(start, i - start - 1);
_wordParts.push_back(WordPart(word, MARGIN_LEFT, y, width, word.getHeight(),
lineNumber, mid, mid));
lineNumber, mid, mid, false));
y += metrics.height();
@@ -193,6 +187,8 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
y += lineHeight;
lineNumber++;
_wordParts.push_back(
WordPart(word, MARGIN_LEFT, y - word.getHeight(), lineNumber, word.getCopyText()));
@@ -202,8 +198,6 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
x = word.getWidth() + MARGIN_LEFT;
x += spaceWidth;
lineNumber++;
}
}
@@ -214,6 +208,8 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
_height = y + lineHeight;
}
_height += MARGIN_BOTTOM;
if (sizeChanged) {
buffer = nullptr;
}
@@ -237,17 +233,16 @@ void MessageRef::alignWordParts(int lineStart, int lineHeight)
}
}
bool MessageRef::tryGetWordPart(QPoint point, Word &word)
const Word *MessageRef::tryGetWordPart(QPoint point)
{
// go through all words and return the first one that contains the point.
for (WordPart &wordPart : _wordParts) {
if (wordPart.getRect().contains(point)) {
word = wordPart.getWord();
return true;
return &wordPart.getWord();
}
}
return false;
return nullptr;
}
int MessageRef::getSelectionIndex(QPoint position)
@@ -259,7 +254,7 @@ int MessageRef::getSelectionIndex(QPoint position)
// find out in which line the cursor is
int lineNumber = 0, lineStart = 0, lineEnd = 0;
for (int i = 0; i < _wordParts.size(); i++) {
for (size_t i = 0; i < _wordParts.size(); i++) {
WordPart &part = _wordParts[i];
if (part.getLineNumber() != 0 && position.y() < part.getY()) {
@@ -267,11 +262,11 @@ int MessageRef::getSelectionIndex(QPoint position)
}
if (part.getLineNumber() != lineNumber) {
lineStart = i - 1;
lineStart = i;
lineNumber = part.getLineNumber();
}
lineEnd = part.getLineNumber() == 0 ? i : i + 1;
lineEnd = i + 1;
}
// count up to the cursor
@@ -293,15 +288,19 @@ int MessageRef::getSelectionIndex(QPoint position)
// cursor is right of the word part
if (position.x() > part.getX() + part.getWidth()) {
index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
index += part.getCharacterLength();
continue;
}
// cursor is over the word part
if (part.getWord().isImage()) {
index++;
if (position.x() - part.getX() > part.getWidth() / 2) {
index++;
}
} else {
auto text = part.getWord().getText();
// TODO: use word.getCharacterWidthCache();
auto text = part.getText();
int x = part.getX();
+1 -1
View File
@@ -28,7 +28,7 @@ public:
std::shared_ptr<QPixmap> buffer = nullptr;
bool updateBuffer = false;
bool tryGetWordPart(QPoint point, messages::Word &word);
const messages::Word *tryGetWordPart(QPoint point);
int getSelectionIndex(QPoint position);
+48 -37
View File
@@ -6,15 +6,12 @@ namespace messages {
// Image word
Word::Word(LazyLoadedImage *image, Type type, const QString &copytext, const QString &tooltip,
const Link &link)
: _image(image)
, _text()
, _color()
: image(image)
, _isImage(true)
, _type(type)
, _copyText(copytext)
, _tooltip(tooltip)
, _link(link)
, _characterWidthCache()
, type(type)
, copyText(copytext)
, tooltip(tooltip)
, link(link)
{
image->getWidth(); // professional segfault test
}
@@ -22,113 +19,127 @@ Word::Word(LazyLoadedImage *image, Type type, const QString &copytext, const QSt
// Text word
Word::Word(const QString &text, Type type, const QColor &color, const QString &copytext,
const QString &tooltip, const Link &link)
: _image(nullptr)
, _text(text)
, _color(color)
: image(nullptr)
, text(text)
, color(color)
, _isImage(false)
, _type(type)
, _copyText(copytext)
, _tooltip(tooltip)
, _link(link)
, _characterWidthCache()
, type(type)
, copyText(copytext)
, tooltip(tooltip)
, link(link)
{
}
LazyLoadedImage &Word::getImage() const
{
return *_image;
return *this->image;
}
const QString &Word::getText() const
{
return _text;
return this->text;
}
int Word::getWidth() const
{
return _width;
return this->width;
}
int Word::getHeight() const
{
return _height;
return this->height;
}
void Word::setSize(int width, int height)
{
_width = width;
_height = height;
this->width = width;
this->height = height;
}
bool Word::isImage() const
{
return _isImage;
return this->_isImage;
}
bool Word::isText() const
{
return !_isImage;
return !this->_isImage;
}
const QString &Word::getCopyText() const
{
return _copyText;
return this->copyText;
}
bool Word::hasTrailingSpace() const
{
return _hasTrailingSpace;
return this->_hasTrailingSpace;
}
QFont &Word::getFont() const
{
return FontManager::getInstance().getFont(_font);
return FontManager::getInstance().getFont(this->font);
}
QFontMetrics &Word::getFontMetrics() const
{
return FontManager::getInstance().getFontMetrics(_font);
return FontManager::getInstance().getFontMetrics(this->font);
}
Word::Type Word::getType() const
{
return _type;
return this->type;
}
const QString &Word::getTooltip() const
{
return _tooltip;
return this->tooltip;
}
const QColor &Word::getColor() const
{
return _color;
return this->color;
}
const Link &Word::getLink() const
{
return _link;
return this->link;
}
int Word::getXOffset() const
{
return _xOffset;
return this->xOffset;
}
int Word::getYOffset() const
{
return _yOffset;
return this->yOffset;
}
void Word::setOffset(int xOffset, int yOffset)
{
_xOffset = std::max(0, xOffset);
_yOffset = std::max(0, yOffset);
this->xOffset = std::max(0, xOffset);
this->yOffset = std::max(0, yOffset);
}
int Word::getCharacterLength() const
{
return this->isImage() ? 2 : this->getText().length() + 1;
}
std::vector<short> &Word::getCharacterWidthCache() const
{
return _characterWidthCache;
// lock not required because there is only one gui thread
// std::lock_guard<std::mutex> lock(this->charWidthCacheMutex);
if (this->charWidthCache.size() == 0 && this->isText()) {
for (int i = 0; i < this->getText().length(); i++) {
this->charWidthCache.push_back(this->getFontMetrics().charWidth(this->getText(), i));
}
}
// TODO: on font change
return this->charWidthCache;
}
} // namespace messages
+15 -14
View File
@@ -110,29 +110,30 @@ public:
int getXOffset() const;
int getYOffset() const;
void setOffset(int _xOffset, int _yOffset);
int getCharacterLength() const;
std::vector<short> &getCharacterWidthCache() const;
private:
LazyLoadedImage *_image;
QString _text;
QColor _color;
LazyLoadedImage *image;
QString text;
QColor color;
bool _isImage;
Type _type;
QString _copyText;
QString _tooltip;
Type type;
QString copyText;
QString tooltip;
int _width = 16;
int _height = 16;
int _xOffset = 0;
int _yOffset = 0;
int width = 16;
int height = 16;
int xOffset = 0;
int yOffset = 0;
bool _hasTrailingSpace;
FontManager::Type _font = FontManager::Medium;
Link _link;
bool _hasTrailingSpace = true;
FontManager::Type font = FontManager::Medium;
Link link;
mutable std::vector<short> _characterWidthCache;
mutable std::vector<short> charWidthCache;
};
} // namespace messages
+11 -4
View File
@@ -14,7 +14,7 @@ WordPart::WordPart(Word &word, int x, int y, int lineNumber, const QString &copy
, _width(word.getWidth())
, _height(word.getHeight())
, _lineNumber(lineNumber)
, _trailingSpace(word.hasTrailingSpace() & allowTrailingSpace)
, _trailingSpace(!word.getCopyText().isEmpty() && word.hasTrailingSpace() & allowTrailingSpace)
{
}
@@ -28,7 +28,7 @@ WordPart::WordPart(Word &word, int x, int y, int width, int height, int lineNumb
, _width(width)
, _height(height)
, _lineNumber(lineNumber)
, _trailingSpace(word.hasTrailingSpace() & allowTrailingSpace)
, _trailingSpace(!word.getCopyText().isEmpty() && word.hasTrailingSpace() & allowTrailingSpace)
{
}
@@ -80,7 +80,7 @@ int WordPart::getBottom() const
QRect WordPart::getRect() const
{
return QRect(_x, _y, _width, _height);
return QRect(_x, _y, _width, _height - 1);
}
const QString WordPart::getCopyText() const
@@ -98,10 +98,17 @@ const QString &WordPart::getText() const
return _text;
}
int WordPart::getLineNumber()
int WordPart::getLineNumber() const
{
return _lineNumber;
}
int WordPart::getCharacterLength() const
{
// return (this->getWord().isImage() ? 1 : this->getText().length()) + (_trailingSpace ? 1 :
// 0);
return this->getWord().isImage() ? 2 : this->getText().length() + 1;
}
} // namespace messages
} // namespace chatterino
+2 -1
View File
@@ -30,7 +30,8 @@ public:
const QString getCopyText() const;
int hasTrailingSpace() const;
const QString &getText() const;
int getLineNumber();
int getLineNumber() const;
int getCharacterLength() const;
private:
Word &_word;