Compare commits
11 Commits
1db5d9022a
...
c1ee2135df
| Author | SHA1 | Date | |
|---|---|---|---|
| c1ee2135df | |||
| a6648b7d1e | |||
| dbfaee27dc | |||
| f392efb54f | |||
| 057bd244bd | |||
| c9d67ff2cc | |||
| 13dc22de9f | |||
| e68b446407 | |||
| 6fc3ffc537 | |||
| f694e0e5ed | |||
| becde8c794 |
@ -38,6 +38,7 @@ add_library(${TARGET_APP} STATIC
|
|||||||
network/apiroutes.h
|
network/apiroutes.h
|
||||||
# 3rd party libraries
|
# 3rd party libraries
|
||||||
../3rdParty/rapidcsv/src/rapidcsv.h
|
../3rdParty/rapidcsv/src/rapidcsv.h
|
||||||
|
utils/messagehandler.h
|
||||||
)
|
)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||||
|
|||||||
@ -127,6 +127,10 @@ QVariant CsvParser::parseItemValue(const int role, const std::string& valueStrin
|
|||||||
if (STRING_ROLES.contains(role)) {
|
if (STRING_ROLES.contains(role)) {
|
||||||
/// string values
|
/// string values
|
||||||
result = QString::fromStdString(valueString);
|
result = QString::fromStdString(valueString);
|
||||||
|
} else if (TYPE_ROLES.contains(role)) {
|
||||||
|
/// type values
|
||||||
|
// TODO validate string is allowed
|
||||||
|
result = QString::fromStdString(valueString);
|
||||||
} else if (INT_ROLES.contains(role)) {
|
} else if (INT_ROLES.contains(role)) {
|
||||||
/// int values
|
/// int values
|
||||||
|
|
||||||
|
|||||||
@ -43,24 +43,7 @@ QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemVa
|
|||||||
QJsonObject rootObject;
|
QJsonObject rootObject;
|
||||||
QJsonArray itemArray;
|
QJsonArray itemArray;
|
||||||
for (const ModelItemValues& itemValues : itemValuesList) {
|
for (const ModelItemValues& itemValues : itemValuesList) {
|
||||||
QJsonObject itemObject;
|
QJsonObject itemObject = itemValuesToJsonObject(itemValues);
|
||||||
|
|
||||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
|
||||||
while (i.hasNext()) {
|
|
||||||
const UserRoles role = i.next();
|
|
||||||
const QString roleName = ROLE_NAMES.value(role);
|
|
||||||
const QVariant value = itemValues.value(role);
|
|
||||||
if (STRING_ROLES.contains(role)) {
|
|
||||||
itemObject.insert(roleName, value.toString());
|
|
||||||
} else if (INT_ROLES.contains(role)) {
|
|
||||||
itemObject.insert(roleName, value.toInt());
|
|
||||||
} else if (DOUBLE_ROLES.contains(role)) {
|
|
||||||
itemObject.insert(roleName, value.toDouble());
|
|
||||||
} else {
|
|
||||||
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
itemArray.append(itemObject);
|
itemArray.append(itemObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +53,27 @@ QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemVa
|
|||||||
return jsonDoc.toJson(QJsonDocument::Compact);
|
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject JsonParser::itemValuesToJsonObject(const ModelItemValues& itemValues) {
|
||||||
|
QJsonObject result;
|
||||||
|
|
||||||
|
// TODO add dates (entry, modification, end)
|
||||||
|
const UserRoles idRole = IdRole;
|
||||||
|
const QJsonValue idValue = extractJsonValue(itemValues, idRole);
|
||||||
|
if (!idValue.isNull()) {
|
||||||
|
const QString idRoleName = ROLE_NAMES.value(idRole);
|
||||||
|
result.insert(idRoleName, idValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
|
while (i.hasNext()) {
|
||||||
|
const UserRoles role = i.next();
|
||||||
|
const QJsonValue jsonValue = extractJsonValue(itemValues, role);
|
||||||
|
const QString roleName = ROLE_NAMES.value(role);
|
||||||
|
result.insert(roleName, jsonValue);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
JsonParser::JsonParser() {}
|
JsonParser::JsonParser() {}
|
||||||
|
|
||||||
QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
|
QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
|
||||||
@ -112,8 +116,28 @@ pair<int, QVariant> JsonParser::getKeyValuePair(const QJsonObject& itemJsonObjec
|
|||||||
result = jsonValue.toInt();
|
result = jsonValue.toInt();
|
||||||
} else if (DOUBLE_ROLES.contains(role)) {
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
result = jsonValue.toDouble();
|
result = jsonValue.toDouble();
|
||||||
|
} else if (TYPE_ROLES.contains(role)) {
|
||||||
|
result = jsonValue.toString();
|
||||||
} else {
|
} else {
|
||||||
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
||||||
}
|
}
|
||||||
return pair<int, QVariant>(role, result);
|
return pair<int, QVariant>(role, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonValue JsonParser::extractJsonValue(const ModelItemValues& itemValues, const int role) {
|
||||||
|
QJsonValue result;
|
||||||
|
const QString roleName = ROLE_NAMES.value(role);
|
||||||
|
const QVariant value = itemValues.value(role);
|
||||||
|
if (STRING_ROLES.contains(role)) {
|
||||||
|
result = value.toString();
|
||||||
|
} else if (INT_ROLES.contains(role)) {
|
||||||
|
result = value.toInt();
|
||||||
|
} else if (DOUBLE_ROLES.contains(role)) {
|
||||||
|
result = value.toDouble();
|
||||||
|
} else if (TYPE_ROLES.contains(role)) {
|
||||||
|
result = value.toString();
|
||||||
|
} else {
|
||||||
|
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ class JsonParser {
|
|||||||
const QString& rootValueName = "");
|
const QString& rootValueName = "");
|
||||||
static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
static QByteArray itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
||||||
const QString& objectName = "");
|
const QString& objectName = "");
|
||||||
|
static QJsonObject itemValuesToJsonObject(const ModelItemValues& itemValues);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit JsonParser();
|
explicit JsonParser();
|
||||||
@ -26,6 +27,8 @@ class JsonParser {
|
|||||||
static ModelItemValues jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
static ModelItemValues jsonObjectToItemValues(const QJsonObject& itemJsonObject);
|
||||||
|
|
||||||
static pair<int, QVariant> getKeyValuePair(const QJsonObject& itemJsonObject, const int role);
|
static pair<int, QVariant> getKeyValuePair(const QJsonObject& itemJsonObject, const int role);
|
||||||
|
|
||||||
|
static QJsonValue extractJsonValue(const ModelItemValues& itemValues, const int role);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // JSONPARSER_H
|
#endif // JSONPARSER_H
|
||||||
|
|||||||
@ -59,6 +59,12 @@ bool GeneralSortFilterModel::lessThan(const QModelIndex& source_left,
|
|||||||
const QString rightString = rightData.toString();
|
const QString rightString = rightData.toString();
|
||||||
return m_collator.compare(leftString, rightString) > 0;
|
return m_collator.compare(leftString, rightString) > 0;
|
||||||
}
|
}
|
||||||
|
const bool isType = TYPE_ROLES.contains(role);
|
||||||
|
if (isType) {
|
||||||
|
const QString leftString = leftData.toString();
|
||||||
|
const QString rightString = rightData.toString();
|
||||||
|
return m_collator.compare(leftString, rightString) > 0;
|
||||||
|
}
|
||||||
const bool isInt = INT_ROLES.contains(role);
|
const bool isInt = INT_ROLES.contains(role);
|
||||||
if (isInt) {
|
if (isInt) {
|
||||||
const int leftInt = leftData.toInt();
|
const int leftInt = leftData.toInt();
|
||||||
|
|||||||
@ -13,6 +13,7 @@ enum UserRoles {
|
|||||||
NameRole = Qt::UserRole + 1,
|
NameRole = Qt::UserRole + 1,
|
||||||
DescriptionRole,
|
DescriptionRole,
|
||||||
InfoRole,
|
InfoRole,
|
||||||
|
TypeRole,
|
||||||
AmountRole,
|
AmountRole,
|
||||||
FactorRole,
|
FactorRole,
|
||||||
/// Non user facing
|
/// Non user facing
|
||||||
@ -24,16 +25,22 @@ enum UserRoles {
|
|||||||
|
|
||||||
static UserRoles DEFAULT_ROLE = NameRole;
|
static UserRoles DEFAULT_ROLE = NameRole;
|
||||||
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
|
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
|
||||||
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
|
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole,
|
||||||
FactorRole};
|
TypeRole, AmountRole, FactorRole};
|
||||||
static QHash<int, QByteArray> ROLE_NAMES = {
|
static QHash<int, QByteArray> ROLE_NAMES = {
|
||||||
{NameRole, "name"}, {DescriptionRole, "description"}, {InfoRole, "info"},
|
{NameRole, "name"}, {DescriptionRole, "description"},
|
||||||
{AmountRole, "amount"}, {FactorRole, "factor"}, {ToStringRole, "ToString"},
|
{InfoRole, "info"}, {TypeRole, "type"},
|
||||||
{IdRole, "id"}};
|
{AmountRole, "amount"}, {FactorRole, "factor"},
|
||||||
|
{ToStringRole, "ToString"}, {IdRole, "id"},
|
||||||
|
{Qt::DisplayRole, "display"}, {Qt::EditRole, "edit"}};
|
||||||
|
|
||||||
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole};
|
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole};
|
||||||
static QList<UserRoles> INT_ROLES = {AmountRole};
|
static QList<UserRoles> INT_ROLES = {AmountRole};
|
||||||
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
||||||
|
|
||||||
|
static const QList<UserRoles> TYPE_ROLES = {TypeRole};
|
||||||
|
static const QList<QString> TYPES = {"A", "B", "C", ""};
|
||||||
|
|
||||||
/// JSON keys
|
/// JSON keys
|
||||||
static const QString ITEMS_KEY_STRING = "items";
|
static const QString ITEMS_KEY_STRING = "items";
|
||||||
static const QString ITEM_KEY_STRING = "item";
|
static const QString ITEM_KEY_STRING = "item";
|
||||||
@ -42,7 +49,7 @@ static const QString ITEM_KEY_STRING = "item";
|
|||||||
static const QString ITEMS_FILE_NAME = ITEMS_KEY_STRING + ".json";
|
static const QString ITEMS_FILE_NAME = ITEMS_KEY_STRING + ".json";
|
||||||
|
|
||||||
/// functions
|
/// functions
|
||||||
static int GET_ROLE_FOR_COLUMN(const int column) {
|
static UserRoles GET_ROLE_FOR_COLUMN(const int column) {
|
||||||
switch (column) {
|
switch (column) {
|
||||||
case 0:
|
case 0:
|
||||||
return NameRole;
|
return NameRole;
|
||||||
@ -54,9 +61,12 @@ static int GET_ROLE_FOR_COLUMN(const int column) {
|
|||||||
return InfoRole;
|
return InfoRole;
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
return AmountRole;
|
return TypeRole;
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
|
return AmountRole;
|
||||||
|
break;
|
||||||
|
case 5:
|
||||||
return FactorRole;
|
return FactorRole;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -65,6 +75,9 @@ static int GET_ROLE_FOR_COLUMN(const int column) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int GET_COLUMN_FOR_ROLE(const UserRoles role) { return USER_FACING_ROLES.indexOf(role); }
|
||||||
|
|
||||||
static QList<QString> GET_HEADER_NAMES() {
|
static QList<QString> GET_HEADER_NAMES() {
|
||||||
QList<QString> result;
|
QList<QString> result;
|
||||||
for (const UserRoles& role : USER_FACING_ROLES) {
|
for (const UserRoles& role : USER_FACING_ROLES) {
|
||||||
@ -74,4 +87,10 @@ static QList<QString> GET_HEADER_NAMES() {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QString GET_HEADER_FOR_COLUMN(const int column) {
|
||||||
|
const UserRoles role = GET_ROLE_FOR_COLUMN(column);
|
||||||
|
const QString headerName = ROLE_NAMES.value(role);
|
||||||
|
return headerName;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // METADATA_H
|
#endif // METADATA_H
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
#include "modelitem.h"
|
#include "modelitem.h"
|
||||||
|
|
||||||
#include "metadata.h"
|
|
||||||
|
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
|
|
||||||
|
#include "../formats/jsonparser.h"
|
||||||
|
#include "metadata.h"
|
||||||
|
|
||||||
ModelItem::ModelItem(const ModelItemValues values)
|
ModelItem::ModelItem(const ModelItemValues values)
|
||||||
: m_values(values) {}
|
: m_values(values) {}
|
||||||
|
|
||||||
@ -66,29 +67,6 @@ QString ModelItem::toString() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject ModelItem::toJsonObject() const {
|
QJsonObject ModelItem::toJsonObject() const {
|
||||||
QJsonObject itemObject;
|
QJsonObject itemObject = JsonParser::itemValuesToJsonObject(m_values);
|
||||||
// TODO add UUID and dates (entry, modification, end)
|
|
||||||
const UserRoles idRole = IdRole;
|
|
||||||
QVariant idValue = data(idRole);
|
|
||||||
if (!idValue.isNull()) {
|
|
||||||
const QString idRoleName = ROLE_NAMES.value(idRole);
|
|
||||||
itemObject.insert(idRoleName, idValue.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
|
||||||
while (i.hasNext()) {
|
|
||||||
const UserRoles role = i.next();
|
|
||||||
const QString roleName = ROLE_NAMES.value(role);
|
|
||||||
const QVariant value = data(role);
|
|
||||||
if (STRING_ROLES.contains(role)) {
|
|
||||||
itemObject.insert(roleName, value.toString());
|
|
||||||
} else if (INT_ROLES.contains(role)) {
|
|
||||||
itemObject.insert(roleName, value.toInt());
|
|
||||||
} else if (DOUBLE_ROLES.contains(role)) {
|
|
||||||
itemObject.insert(roleName, value.toDouble());
|
|
||||||
} else {
|
|
||||||
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return itemObject;
|
return itemObject;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,19 +78,15 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
return m_items.at(row)->data(roleForColumn);
|
return m_items.at(row)->data(roleForColumn);
|
||||||
case NameRole:
|
|
||||||
case DescriptionRole:
|
|
||||||
case InfoRole:
|
|
||||||
case AmountRole:
|
|
||||||
case FactorRole:
|
|
||||||
case IdRole:
|
|
||||||
return m_items.at(row)->data(role);
|
|
||||||
case ToStringRole:
|
case ToStringRole:
|
||||||
return m_items.at(row)->toString();
|
return m_items.at(row)->toString();
|
||||||
case ToJsonRole:
|
case ToJsonRole:
|
||||||
return m_items.at(row)->toJsonObject();
|
return m_items.at(row)->toJsonObject();
|
||||||
|
case IdRole:
|
||||||
|
return m_items.at(row)->data(role);
|
||||||
|
default:
|
||||||
|
return m_items.at(row)->data(role);
|
||||||
}
|
}
|
||||||
|
|
||||||
return QVariant();
|
return QVariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,10 +104,9 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||||
if (role == Qt::EditRole && checkIndex(index)) {
|
if (checkIndex(index)) {
|
||||||
const int column = index.column();
|
const int adjustedRole = getAppropriateRoleForIndex(index, role);
|
||||||
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
return setItemData(index, {{adjustedRole, value}});
|
||||||
return setItemData(index, {{roleForColumn, value}});
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -308,6 +303,7 @@ QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
|
|||||||
// TODO check if role is a editable role?
|
// TODO check if role is a editable role?
|
||||||
if (oldValue != newValue) {
|
if (oldValue != newValue) {
|
||||||
bool emptyValueIsEqualToZero = isEmptyValueEqualToZero(role);
|
bool emptyValueIsEqualToZero = isEmptyValueEqualToZero(role);
|
||||||
|
// REFACTOR the next if statement is too complex
|
||||||
if (emptyValueIsEqualToZero && oldValue == QVariant() && newValue == 0) {
|
if (emptyValueIsEqualToZero && oldValue == QVariant() && newValue == 0) {
|
||||||
qDebug() << "oldValue:" << oldValue << "& newValue:" << newValue
|
qDebug() << "oldValue:" << oldValue << "& newValue:" << newValue
|
||||||
<< "mean the same. Ignoring...";
|
<< "mean the same. Ignoring...";
|
||||||
@ -332,6 +328,25 @@ bool TableModel::isEmptyValueEqualToZero(const int role) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int TableModel::getAppropriateRoleForIndex(const QModelIndex& index, const int role) const {
|
||||||
|
/// cases:
|
||||||
|
/// 1. Qt::DisplayRole, Qt::EditRole
|
||||||
|
/// -> get role for column
|
||||||
|
/// 2. other roles
|
||||||
|
/// -> use role as given
|
||||||
|
const int column = index.column();
|
||||||
|
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||||
|
switch (role) {
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
case Qt::EditRole:
|
||||||
|
return roleForColumn;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return role;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
|
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
|
||||||
// iterate over indexes to search item : see searchItem(...);
|
// iterate over indexes to search item : see searchItem(...);
|
||||||
// for (const shared_ptr<ModelItem>& item : m_items) {
|
// for (const shared_ptr<ModelItem>& item : m_items) {
|
||||||
@ -373,6 +388,10 @@ bool TableModel::isItemEqualToItemValues(const QModelIndex& itemIndex,
|
|||||||
if (valueOfItem.toString() != givenValue.toString()) {
|
if (valueOfItem.toString() != givenValue.toString()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else if (TYPE_ROLES.contains(role)) {
|
||||||
|
if (valueOfItem.toString() != givenValue.toString()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else if (INT_ROLES.contains(role)) {
|
} else if (INT_ROLES.contains(role)) {
|
||||||
if (valueOfItem.toInt() != givenValue.toInt()) {
|
if (valueOfItem.toInt() != givenValue.toInt()) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -71,6 +71,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
|
||||||
const QMap<int, QVariant>& roleValueMap) const;
|
const QMap<int, QVariant>& roleValueMap) const;
|
||||||
bool isEmptyValueEqualToZero(const int role) const;
|
bool isEmptyValueEqualToZero(const int role) const;
|
||||||
|
int getAppropriateRoleForIndex(const QModelIndex& index, const int role) const;
|
||||||
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
|
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
|
||||||
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
|
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
|
||||||
const ModelItemValues givenItemValues) const;
|
const ModelItemValues givenItemValues) const;
|
||||||
|
|||||||
164
utils/messagehandler.h
Normal file
164
utils/messagehandler.h
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
#ifndef MESSAGEHANDLER_H
|
||||||
|
#define MESSAGEHANDLER_H
|
||||||
|
/**
|
||||||
|
* Color and formatting codes
|
||||||
|
* @see: http://misc.flogisoft.com/bash/tip_colors_and_formatting
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
// qSetMessagePattern("%{file}(%{line}): %{message}");
|
||||||
|
// qSetMessagePattern("%{type}(%{line}):\t%{message}");
|
||||||
|
// qSetMessagePattern("%{type}%{file}(%{line}):\t%{message}");
|
||||||
|
|
||||||
|
void consoleHandlerColoredVerbose(QtMsgType type,
|
||||||
|
const QMessageLogContext& context,
|
||||||
|
const QString& msg) {
|
||||||
|
QByteArray localMsg = msg.toLocal8Bit();
|
||||||
|
switch (type) {
|
||||||
|
case QtDebugMsg:
|
||||||
|
// fprintf(stderr, "\033[1;30mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file,
|
||||||
|
// context.line, context.function, localMsg.constData()); // bold
|
||||||
|
fprintf(stderr, "\033[107;30mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtInfoMsg:
|
||||||
|
fprintf(stderr, "\033[107;32mInfo: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtWarningMsg:
|
||||||
|
fprintf(stderr, "\033[43;30mWarning: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtCriticalMsg:
|
||||||
|
fprintf(stderr, "\033[41;30mCritical: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtFatalMsg:
|
||||||
|
fprintf(stderr, "\033[41;30mFatal: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void consoleHandlerColoredVerboseInDarkTheme(QtMsgType type,
|
||||||
|
const QMessageLogContext& context,
|
||||||
|
const QString& msg) {
|
||||||
|
QByteArray localMsg = msg.toLocal8Bit();
|
||||||
|
switch (type) {
|
||||||
|
case QtDebugMsg:
|
||||||
|
// fprintf(stderr, "\033[1;30mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file,
|
||||||
|
// context.line, context.function, localMsg.constData()); // bold
|
||||||
|
fprintf(stderr, "\033[107;37mDebug: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtInfoMsg:
|
||||||
|
fprintf(stderr, "\033[107;32mInfo: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtWarningMsg:
|
||||||
|
fprintf(stderr, "\033[43;30mWarning: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtCriticalMsg:
|
||||||
|
fprintf(stderr, "\033[41;30mCritical: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtFatalMsg:
|
||||||
|
fprintf(stderr, "\033[41;30mFatal: (%s:%u, %s) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
context.function, localMsg.constData());
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void consoleHandlerColored(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
||||||
|
QByteArray localMsg = msg.toLocal8Bit();
|
||||||
|
switch (type) {
|
||||||
|
case QtDebugMsg:
|
||||||
|
fprintf(stderr, "\033[1;30mDebug: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtInfoMsg:
|
||||||
|
fprintf(stderr, "\033[0;30mInfo: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtWarningMsg:
|
||||||
|
fprintf(stderr, "\033[1;33mWarning: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtCriticalMsg:
|
||||||
|
fprintf(stderr, "\033[31mCritical: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
break;
|
||||||
|
case QtFatalMsg:
|
||||||
|
fprintf(stderr, "\033[31mFatal: (%s:%u) \t%s\n\033[0m", context.file, context.line,
|
||||||
|
localMsg.constData());
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
||||||
|
QByteArray localMsg = msg.toLocal8Bit();
|
||||||
|
switch (type) {
|
||||||
|
case QtDebugMsg:
|
||||||
|
fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||||
|
context.function);
|
||||||
|
break;
|
||||||
|
case QtInfoMsg:
|
||||||
|
fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||||
|
context.function);
|
||||||
|
break;
|
||||||
|
case QtWarningMsg:
|
||||||
|
fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||||
|
context.function);
|
||||||
|
break;
|
||||||
|
case QtCriticalMsg:
|
||||||
|
fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file,
|
||||||
|
context.line, context.function);
|
||||||
|
break;
|
||||||
|
case QtFatalMsg:
|
||||||
|
fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line,
|
||||||
|
context.function);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef Q_OS_ANDROID
|
||||||
|
#include <android/log.h>
|
||||||
|
|
||||||
|
const char* const applicationName = "Pensieve";
|
||||||
|
void androidMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) {
|
||||||
|
QString report = msg;
|
||||||
|
if (context.file && !QString(context.file).isEmpty()) {
|
||||||
|
report += " in file ";
|
||||||
|
report += QString(context.file);
|
||||||
|
report += " line ";
|
||||||
|
report += QString::number(context.line);
|
||||||
|
}
|
||||||
|
if (context.function && !QString(context.function).isEmpty()) {
|
||||||
|
report += +" function ";
|
||||||
|
report += QString(context.function);
|
||||||
|
}
|
||||||
|
const char* const local = report.toLocal8Bit().constData();
|
||||||
|
switch (type) {
|
||||||
|
case QtDebugMsg:
|
||||||
|
__android_log_write(ANDROID_LOG_DEBUG, applicationName, local);
|
||||||
|
break;
|
||||||
|
case QtInfoMsg:
|
||||||
|
__android_log_write(ANDROID_LOG_INFO, applicationName, local);
|
||||||
|
break;
|
||||||
|
case QtWarningMsg:
|
||||||
|
__android_log_write(ANDROID_LOG_WARN, applicationName, local);
|
||||||
|
break;
|
||||||
|
case QtCriticalMsg:
|
||||||
|
__android_log_write(ANDROID_LOG_ERROR, applicationName, local);
|
||||||
|
break;
|
||||||
|
case QtFatalMsg:
|
||||||
|
default:
|
||||||
|
__android_log_write(ANDROID_LOG_FATAL, applicationName, local);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MESSAGEHANDLER_H
|
||||||
Reference in New Issue
Block a user