src/messagelog.cpp

Fri, 01 Jul 2022 16:46:43 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 16:46:43 +0300
changeset 312
2637134bc37c
parent 264
76a025db4948
permissions
-rw-r--r--

Fix right click to delete not really working properly
Instead of removing the point that had been added, it would remove
the point that is being drawn, which would cause it to overwrite the
previous point using the new point, causing a bit of a delay

#include <QColor>
#include "src/messagelog.h"

MessageLog::MessageLog(QObject *parent) :
	QAbstractTableModel{parent}
{
	
}

void MessageLog::addMessage(const Message& message)
{
	const int row = static_cast<int>(this->messages.size());
	this->beginInsertRows({}, row, row);
	this->messages.push_back(message);
	this->endInsertRows();
}

QVariant MessageLog::headerData(int section, Qt::Orientation orientation, int role) const
{
	if (orientation != Qt::Horizontal or role != Qt::DisplayRole) {
		return {};
	}
	else {
		switch (static_cast<Column>(section)) {
		case TimeColumn:
			return tr("Time");
		case MessageColumn:
			return tr("Message");
		}
		return {};
	}
}

int MessageLog::rowCount(const QModelIndex&) const
{
	return narrow<int>(signed_cast(this->messages.size()));
}

int MessageLog::columnCount(const QModelIndex&) const
{
	return NUM_COLUMNS;
}

QVariant MessageLog::data(const QModelIndex& index, int role) const
{
	const std::size_t row = unsigned_cast(index.row());
	if (false
		or row >= this->messages.size()
		or index.column() < 0
		or index.column() >= NUM_COLUMNS
	) {
		return {};
	}
	else {
		const Message& message = this->messages[row];
		switch (role) {
		case Qt::DisplayRole:
			switch (static_cast<Column>(index.column())) {
			case TimeColumn:
				return message.time.toString(tr("hh:mm:ss"));
			case MessageColumn:
				return message.text;
			}
			break;
		case Qt::BackgroundRole:
			switch(message.type) {
			case Message::Info:
				return {};
			case Message::Warning:
				return QColor{Qt::yellow};
			case Message::Error:
				return QColor{Qt::red};
			}
			break;
		}
		return {};
	}
}

mercurial