fixed project code style in some files

This commit is contained in:
fourtf
2017-09-21 12:15:01 +02:00
parent 8eaca85402
commit 3e24752477
23 changed files with 395 additions and 294 deletions
+32 -32
View File
@@ -13,61 +13,61 @@ template <typename T>
class LimitedQueue
{
public:
LimitedQueue(int limit = 1000, int buffer = 250)
: _offset(0)
, _limit(limit)
, _buffer(buffer)
LimitedQueue(int _limit = 1000, int _buffer = 250)
: offset(0)
, limit(_limit)
, buffer(_buffer)
{
_vector = std::make_shared<std::vector<T>>();
_vector->reserve(_limit + _buffer);
this->vector = std::make_shared<std::vector<T>>();
this->vector->reserve(this->limit + this->buffer);
}
void clear()
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(this->mutex);
_vector = std::make_shared<std::vector<T>>();
_vector->reserve(_limit + _buffer);
this->vector = std::make_shared<std::vector<T>>();
this->vector->reserve(this->limit + this->buffer);
_offset = 0;
this->offset = 0;
}
// return true if an item was deleted
// deleted will be set if the item was deleted
bool appendItem(const T &item, T &deleted)
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(this->mutex);
if (_vector->size() >= _limit) {
if (this->vector->size() >= this->limit) {
// vector is full
if (_offset == _buffer) {
deleted = _vector->at(_offset);
if (this->offset == this->buffer) {
deleted = this->vector->at(this->offset);
// create new vector
auto newVector = std::make_shared<std::vector<T>>();
newVector->reserve(_limit + _buffer);
newVector->reserve(this->limit + this->buffer);
for (unsigned int i = 0; i < _limit; ++i) {
newVector->push_back(_vector->at(i + _offset));
for (unsigned int i = 0; i < this->limit; ++i) {
newVector->push_back(this->vector->at(i + this->offset));
}
newVector->push_back(item);
_offset = 0;
_vector = newVector;
this->offset = 0;
this->vector = newVector;
return true;
} else {
deleted = _vector->at(_offset);
deleted = this->vector->at(this->offset);
// append item and increment offset("deleting" first element)
_vector->push_back(item);
_offset++;
this->vector->push_back(item);
this->offset++;
return true;
}
} else {
// append item
_vector->push_back(item);
this->vector->push_back(item);
return false;
}
@@ -75,22 +75,22 @@ public:
messages::LimitedQueueSnapshot<T> getSnapshot()
{
std::lock_guard<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(this->mutex);
if (_vector->size() < _limit) {
return LimitedQueueSnapshot<T>(_vector, _offset, _vector->size());
if (this->vector->size() < this->limit) {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->vector->size());
} else {
return LimitedQueueSnapshot<T>(_vector, _offset, _limit);
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->limit);
}
}
private:
std::shared_ptr<std::vector<T>> _vector;
std::mutex _mutex;
std::shared_ptr<std::vector<T>> vector;
std::mutex mutex;
unsigned int _offset;
unsigned int _limit;
unsigned int _buffer;
unsigned int offset;
unsigned int limit;
unsigned int buffer;
};
} // namespace messages
+97
View File
@@ -0,0 +1,97 @@
#pragma once
#include "messages/limitedqueuesnapshot.hpp"
#include <memory>
#include <mutex>
#include <vector>
namespace chatterino {
namespace messages {
template <typename T>
class LimitedQueue
{
public:
LimitedQueue(int _limit = 1000, int _buffer = 250)
: offset(0)
, limit(_limit)
, buffer(_buffer)
{
this->vector = std::make_shared<std::vector<T>>();
this->vector->reserve(this->limit + this->buffer);
}
void clear()
{
std::lockthis->guard<std::mutex> lock(this->mutex);
this->vector = std::make_shared<std::vector<T>>();
this->vector->reserve(this->limit + this->buffer);
this->offset = 0;
}
// return true if an item was deleted
// deleted will be set if the item was deleted
bool appendItem(const T &item, T &deleted)
{
std::lock_guard<std::mutex> lock(this->mutex);
if (this->vector->size() >= this->limit) {
// vector is full
if (this->offset == this->buffer) {
deleted = this->vector->at(this->offset);
// create new vector
auto newVector = std::make_shared<std::vector<T>>();
newVector->reserve(this->limit + this->buffer);
for (unsigned int i = 0; i < this->limit; ++i) {
newVector->pushthis->back(this->vector->at(i + this->offset));
}
newVector->push_back(item);
this->offset = 0;
this->vector = newVector;
return true;
} else {
deleted = this->vector->at(this->offset);
// append item and increment offset("deleting" first element)
this->vector->push_back(item);
this->offset++;
return true;
}
} else {
// append item
this->vector->pushthis->back(item);
return false;
}
}
messages::LimitedQueueSnapshot<T> getSnapshot()
{
std::lockthis->guard<std::mutex> lock(this->mutex);
if (this->vector->size() < this->limit) {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->vector->size());
} else {
return LimitedQueueSnapshot<T>(this->vector, this->offset, this->limit);
}
}
private:
std::shared_ptr<std::vector<T>> vector;
std::mutex mutex;
unsigned int offset;
unsigned int limit;
unsigned int buffer;
};
} // namespace messages
} // namespace chatterino
+18 -3
View File
@@ -9,11 +9,26 @@ Link::Link()
{
}
Link::Link(Type type, const QString &value)
: type(type)
, value(value)
Link::Link(Type _type, const QString &_value)
: type(_type)
, value(_value)
{
}
bool Link::isValid() const
{
return this->type != None;
}
Link::Type Link::getType() const
{
return this->type;
}
const QString &Link::getValue() const
{
return this->value;
}
} // namespace messages
} // namespace chatterino
+3 -14
View File
@@ -22,20 +22,9 @@ public:
Link();
Link(Type getType, const QString &getValue);
bool isValid() const
{
return type != None;
}
Type getType() const
{
return type;
}
const QString &getValue() const
{
return value;
}
bool isValid() const;
Type getType() const;
const QString &getValue() const;
private:
Type type;
+5 -5
View File
@@ -1,15 +1,15 @@
#include "messagecolor.h"
#include "messagecolor.hpp"
namespace chatterino {
namespace messages {
MessageColor::MessageColor(const QColor &color)
MessageColor::MessageColor(const QColor &_color)
: type(Type::Custom)
, color(color)
, color(_color)
{
}
MessageColor::MessageColor(Type type)
: type(type)
MessageColor::MessageColor(Type _type)
: type(_type)
{
}
+40 -39
View File
@@ -14,40 +14,40 @@ using namespace chatterino::messages;
namespace chatterino {
namespace messages {
MessageRef::MessageRef(SharedMessage message)
: _message(message)
, _wordParts()
MessageRef::MessageRef(SharedMessage _message)
: message(_message)
, wordParts()
{
}
Message *MessageRef::getMessage()
{
return _message.get();
return this->message.get();
}
int MessageRef::getHeight() const
{
return _height;
return this->height;
}
bool MessageRef::layout(int width, bool enableEmoteMargins)
{
auto &settings = SettingsManager::getInstance();
bool sizeChanged = width != _currentLayoutWidth;
bool redraw = width != _currentLayoutWidth;
bool sizeChanged = width != this->currentLayoutWidth;
bool redraw = width != this->currentLayoutWidth;
int spaceWidth = 4;
int mediumTextLineHeight =
FontManager::getInstance().getFontMetrics(FontManager::Medium).height();
/* TODO(pajlada): Re-implement
bool recalculateImages = _emoteGeneration != EmoteManager::getInstance().getGeneration();
bool recalculateImages = this->emoteGeneration != EmoteManager::getInstance().getGeneration();
*/
bool recalculateImages = true;
bool recalculateText = _fontGeneration != FontManager::getInstance().getGeneration();
bool newWordTypes = _currentWordTypes != SettingsManager::getInstance().getWordTypeMask();
bool recalculateText = this->fontGeneration != FontManager::getInstance().getGeneration();
bool newWordTypes = this->currentWordTypes != SettingsManager::getInstance().getWordTypeMask();
qreal emoteScale = settings.emoteScale.get();
bool scaleEmotesByLineHeight = settings.scaleEmotesByLineHeight.get();
@@ -57,10 +57,10 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
return false;
}
// _emoteGeneration = EmoteManager::getInstance().getGeneration();
_fontGeneration = FontManager::getInstance().getGeneration();
// this->emoteGeneration = EmoteManager::getInstance().getGeneration();
this->fontGeneration = FontManager::getInstance().getGeneration();
for (auto &word : _message->getWords()) {
for (auto &word : this->message->getWords()) {
if (word.isImage()) {
if (!recalculateImages) {
continue;
@@ -88,11 +88,11 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
}
if (newWordTypes) {
_currentWordTypes = settings.getWordTypeMask();
this->currentWordTypes = settings.getWordTypeMask();
}
// layout
_currentLayoutWidth = width;
this->currentLayoutWidth = width;
int x = MARGIN_LEFT;
int y = MARGIN_TOP;
@@ -104,11 +104,11 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
int lineHeight = 0;
bool first = true;
_wordParts.clear();
this->wordParts.clear();
uint32_t flags = settings.getWordTypeMask();
for (auto it = _message->getWords().begin(); it != _message->getWords().end(); ++it) {
for (auto it = this->message->getWords().begin(); it != this->message->getWords().end(); ++it) {
Word &word = *it;
// Check if given word is supposed to be rendered by comparing it to the current setting
@@ -147,8 +147,9 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
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, false, charOffset));
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y, width,
word.getHeight(), lineNumber, mid, mid,
false, charOffset));
charOffset = i;
@@ -164,18 +165,18 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
QString mid(text.mid(start));
width = metrics.width(mid);
_wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(), width,
word.getHeight(), lineNumber, mid, mid, charOffset));
this->wordParts.push_back(WordPart(word, MARGIN_LEFT, y - word.getHeight(), width,
word.getHeight(), lineNumber, mid, mid, charOffset));
x = width + MARGIN_LEFT + spaceWidth;
lineHeight = word.getHeight();
lineStart = _wordParts.size() - 1;
lineStart = this->wordParts.size() - 1;
first = false;
} else if (first || x + word.getWidth() + xOffset <= right) {
// fits in the line
_wordParts.push_back(
this->wordParts.push_back(
WordPart(word, x, y - word.getHeight(), lineNumber, word.getCopyText()));
x += word.getWidth() + xOffset;
@@ -192,10 +193,10 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
lineNumber++;
_wordParts.push_back(
this->wordParts.push_back(
WordPart(word, MARGIN_LEFT, y - word.getHeight(), lineNumber, word.getCopyText()));
lineStart = _wordParts.size() - 1;
lineStart = this->wordParts.size() - 1;
lineHeight = word.getHeight();
@@ -206,12 +207,12 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
alignWordParts(lineStart, lineHeight, width);
if (_height != y + lineHeight) {
if (this->height != y + lineHeight) {
sizeChanged = true;
_height = y + lineHeight;
this->height = y + lineHeight;
}
_height += MARGIN_BOTTOM;
this->height += MARGIN_BOTTOM;
if (sizeChanged) {
buffer = nullptr;
@@ -224,19 +225,19 @@ bool MessageRef::layout(int width, bool enableEmoteMargins)
const std::vector<WordPart> &MessageRef::getWordParts() const
{
return _wordParts;
return this->wordParts;
}
void MessageRef::alignWordParts(int lineStart, int lineHeight, int width)
{
int xOffset = 0;
if (this->_message->centered && _wordParts.size() > 0) {
xOffset = (width - this->_wordParts.at(_wordParts.size() - 1).getRight()) / 2;
if (this->message->centered && this->wordParts.size() > 0) {
xOffset = (width - this->wordParts.at(this->wordParts.size() - 1).getRight()) / 2;
}
for (size_t i = lineStart; i < this->_wordParts.size(); i++) {
WordPart &wordPart2 = this->_wordParts.at(i);
for (size_t i = lineStart; i < this->wordParts.size(); i++) {
WordPart &wordPart2 = this->wordParts.at(i);
wordPart2.setPosition(wordPart2.getX() + xOffset, wordPart2.getY() + lineHeight);
}
@@ -245,7 +246,7 @@ void MessageRef::alignWordParts(int lineStart, int lineHeight, int width)
const Word *MessageRef::tryGetWordPart(QPoint point)
{
// go through all words and return the first one that contains the point.
for (WordPart &wordPart : _wordParts) {
for (WordPart &wordPart : this->wordParts) {
if (wordPart.getRect().contains(point)) {
return &wordPart.getWord();
}
@@ -256,15 +257,15 @@ const Word *MessageRef::tryGetWordPart(QPoint point)
int MessageRef::getSelectionIndex(QPoint position)
{
if (_wordParts.size() == 0) {
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 < _wordParts.size(); i++) {
WordPart &part = _wordParts[i];
for (size_t i = 0; i < this->wordParts.size(); i++) {
WordPart &part = this->wordParts[i];
if (part.getLineNumber() != 0 && position.y() < part.getY()) {
break;
@@ -282,13 +283,13 @@ int MessageRef::getSelectionIndex(QPoint position)
int index = 0;
for (int i = 0; i < lineStart; i++) {
WordPart &part = _wordParts[i];
WordPart &part = this->wordParts[i];
index += part.getWord().isImage() ? 2 : part.getText().length() + 1;
}
for (int i = lineStart; i < lineEnd; i++) {
WordPart &part = _wordParts[i];
WordPart &part = this->wordParts[i];
// curser is left of the word part
if (position.x() < part.getX()) {
+7 -7
View File
@@ -34,17 +34,17 @@ public:
private:
// variables
SharedMessage _message;
std::vector<messages::WordPart> _wordParts;
SharedMessage message;
std::vector<messages::WordPart> wordParts;
int _height = 0;
int height = 0;
int _currentLayoutWidth = -1;
int _fontGeneration = -1;
int currentLayoutWidth = -1;
int fontGeneration = -1;
/* TODO(pajlada): Re-implement
int _emoteGeneration = -1;
int emoteGeneration = -1;
*/
Word::Type _currentWordTypes = Word::None;
Word::Type currentWordTypes = Word::None;
// methods
void alignWordParts(int lineStart, int lineHeight, int width);
+1 -1
View File
@@ -3,7 +3,7 @@
#include "fontmanager.hpp"
#include "messages/lazyloadedimage.hpp"
#include "messages/link.hpp"
#include "messages/messagecolor.h"
#include "messages/messagecolor.hpp"
#include <stdint.h>
#include <QRect>
+41 -39
View File
@@ -4,106 +4,108 @@
namespace chatterino {
namespace messages {
WordPart::WordPart(Word &word, int x, int y, int lineNumber, const QString &copyText,
bool allowTrailingSpace)
: _word(word)
, _copyText(copyText)
, _text(word.isText() ? _word.getText() : QString())
, _x(x)
, _y(y)
, _width(word.getWidth())
, _height(word.getHeight())
, _lineNumber(lineNumber)
, _trailingSpace(!word.getCopyText().isEmpty() && word.hasTrailingSpace() & allowTrailingSpace)
WordPart::WordPart(Word &_word, int _x, int _y, int _lineNumber, const QString &_copyText,
bool _allowTrailingSpace)
: word(_word)
, copyText(_copyText)
, text(_word.isText() ? _word.getText() : QString())
, x(_x)
, y(_y)
, width(_word.getWidth())
, height(_word.getHeight())
, lineNumber(_lineNumber)
, _trailingSpace(!_word.getCopyText().isEmpty() &&
_word.hasTrailingSpace() & _allowTrailingSpace)
, wordCharOffset(0)
{
}
WordPart::WordPart(Word &word, int x, int y, int width, int height, int lineNumber,
const QString &copyText, const QString &customText, bool allowTrailingSpace,
int wordCharOffset)
: _word(word)
, _copyText(copyText)
, _text(customText)
, _x(x)
, _y(y)
, _width(width)
, _height(height)
, _lineNumber(lineNumber)
, _trailingSpace(!word.getCopyText().isEmpty() && word.hasTrailingSpace() & allowTrailingSpace)
, wordCharOffset(wordCharOffset)
WordPart::WordPart(Word &_word, int _x, int _y, int _width, int _height, int _lineNumber,
const QString &_copyText, const QString &_customText, bool _allowTrailingSpace,
int _wordCharOffset)
: word(_word)
, copyText(_copyText)
, text(_customText)
, x(_x)
, y(_y)
, width(_width)
, height(_height)
, lineNumber(_lineNumber)
, _trailingSpace(!_word.getCopyText().isEmpty() &&
_word.hasTrailingSpace() & _allowTrailingSpace)
, wordCharOffset(_wordCharOffset)
{
}
const Word &WordPart::getWord() const
{
return _word;
return this->word;
}
int WordPart::getWidth() const
{
return _width;
return this->width;
}
int WordPart::getHeight() const
{
return _height;
return this->height;
}
int WordPart::getX() const
{
return _x;
return this->x;
}
int WordPart::getY() const
{
return _y;
return this->y;
}
void WordPart::setPosition(int x, int y)
{
_x = x;
_y = y;
this->x = x;
this->y = y;
}
void WordPart::setY(int y)
{
_y = y;
this->y = y;
}
int WordPart::getRight() const
{
return _x + _width;
return this->x + this->width;
}
int WordPart::getBottom() const
{
return _y + _height;
return this->y + this->height;
}
QRect WordPart::getRect() const
{
return QRect(_x, _y, _width, _height - 1);
return QRect(this->x, this->y, this->width, this->height - 1);
}
const QString WordPart::getCopyText() const
{
return _copyText;
return this->copyText;
}
int WordPart::hasTrailingSpace() const
{
return _trailingSpace;
return this->_trailingSpace;
}
const QString &WordPart::getText() const
{
return _text;
return this->text;
}
int WordPart::getLineNumber() const
{
return _lineNumber;
return this->lineNumber;
}
int WordPart::getCharacterLength() const
+8 -8
View File
@@ -36,17 +36,17 @@ public:
short getCharacterWidth(int index) const;
private:
Word &_word;
Word &word;
QString _copyText;
QString _text;
QString copyText;
QString text;
int _x;
int _y;
int _width;
int _height;
int x;
int y;
int width;
int height;
int _lineNumber;
int lineNumber;
bool _trailingSpace;
int wordCharOffset;