Compare commits

..

8 Commits

10 changed files with 244 additions and 38 deletions

View File

@ -38,6 +38,7 @@ add_library(${TARGET_APP} STATIC
network/apiroutes.h
# 3rd party libraries
../3rdParty/rapidcsv/src/rapidcsv.h
utils/messagehandler.h
)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

View File

@ -45,6 +45,7 @@ QList<ModelItemValues> FileHandler::getItemValuesFromCSVFile(const QString& file
QFile file;
file.setFileName(filePath);
if (file.exists()) {
// TODO inform UI on CSV import errors
result = CsvParser::getItemsFromCSVFile(filePath);
}
return result;

View File

@ -52,7 +52,11 @@ bool CsvParser::isCsvCompatible(const rapidcsv::Document& doc) {
qInfo() << "Checking CSV document for compatiblity...";
const std::vector<std::string> columnNames = doc.GetColumnNames();
for (const QString& headerName : GET_HEADER_NAMES()) {
bool isHeaderNameFound = false;
if (OPTIONAL_CSV_HEADERS.contains(headerName)) {
/// no need to have a column for the optional values
continue;
}
/// these column must be found in CSV document
if (std::find(columnNames.begin(), columnNames.end(), headerName) != columnNames.end()) {
qDebug() << QString("Header found in column names: %1").arg(headerName);
} else {
@ -86,14 +90,13 @@ QHash<QString, std::vector<std::string>> CsvParser::extractColumnValues(
const rapidcsv::Document& doc) {
QHash<QString, std::vector<std::string>> columnValueMap;
for (const QString& columnName : headerNames) {
// TODO add support for optional columns
// if (optionalCsvHeaderNames.contains(columnName)) {
// const std::vector<std::string> columnNames = doc.GetColumnNames();
// int columnIdx = doc.GetColumnIdx(columnName.toStdString());
// if (columnIdx == -1) {
// continue;
// }
// }
if (OPTIONAL_CSV_HEADERS.contains(columnName)) {
const std::vector<std::string> columnNames = doc.GetColumnNames();
int columnIdx = doc.GetColumnIdx(columnName.toStdString());
if (columnIdx == -1) {
continue;
}
}
const std::vector<std::string> columnValues =
doc.GetColumn<std::string>(columnName.toStdString());
columnValueMap.insert(columnName, columnValues);

View File

@ -98,12 +98,16 @@ ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonOb
values.insert(keyValuePair.first, keyValuePair.second);
}
QListIterator<UserRoles> i(USER_FACING_ROLES);
while (i.hasNext()) {
const UserRoles role = i.next();
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
values.insert(keyValuePair.first, keyValuePair.second);
for (auto iter = itemJsonObject.constBegin(), end = itemJsonObject.constEnd(); iter != end;
++iter) {
const QString roleName = iter.key();
if (ROLE_NAMES.values().contains(roleName)) {
const int role = ROLE_NAMES.key(roleName.toLatin1());
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
values.insert(keyValuePair.first, keyValuePair.second);
}
}
return values;
}

View File

@ -119,13 +119,16 @@ void GenericCore::importCSVFile(const QString& filePath) {
qInfo() << "importing items from CSV...";
qDebug() << "filePath:" << filePath;
const QList<ModelItemValues> itemValuesList = FileHandler::getItemValuesFromCSVFile(filePath);
// TODO inform UI on errors
if (itemValuesList.isEmpty()) {
qDebug() << "No items found. Doing nothing...";
displayStatusMessage("No items found in CSV file. Either empty or not compatible.");
return;
}
// qDebug() << "CSV file content:" << itemValuesList;
m_mainModel->insertItems(m_mainModel->rowCount(), itemValuesList);
const QString messageString =
QString(tr("Imported %1 item(s) from CSV file.")).arg(itemValuesList.size());
displayStatusMessage(messageString);
}
bool GenericCore::exportCSVFile(const QString& filePath) {

View File

@ -29,7 +29,6 @@ class GenericCore : public QObject {
std::shared_ptr<TableModel> getModel() const;
std::shared_ptr<GeneralSortFilterModel> getSortFilterModel() const;
void saveItems();
void importCSVFile(const QString& filePath);
bool exportCSVFile(const QString& filePath);
@ -38,6 +37,8 @@ class GenericCore : public QObject {
bool isSyncServerSetup() const;
public slots:
void saveItems();
void onSendItemTriggered(const QByteArray& jsonData);
void onItemsFetched(const QByteArray jsonData);
void onItemsFetchFailure(const QString errorString);

View File

@ -25,21 +25,28 @@ enum UserRoles {
static UserRoles DEFAULT_ROLE = NameRole;
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole,
TypeRole, AmountRole, FactorRole};
static QHash<int, QByteArray> ROLE_NAMES = {
{NameRole, "name"}, {DescriptionRole, "description"},
{InfoRole, "info"}, {TypeRole, "type"},
{AmountRole, "amount"}, {FactorRole, "factor"},
{ToStringRole, "ToString"}, {IdRole, "id"}};
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, TypeRole,
AmountRole, FactorRole, IdRole};
static QList<UserRoles> READ_ONLY_ROLES = {IdRole};
static QHash<int, QByteArray> ROLE_NAMES = {
{NameRole, "name"}, {DescriptionRole, "description"},
{InfoRole, "info"}, {TypeRole, "type"},
{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> INT_ROLES = {AmountRole};
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
static QList<UserRoles> NUMBER_ROLES = INT_ROLES + DOUBLE_ROLES;
static const QList<UserRoles> TYPE_ROLES = {TypeRole};
static const QList<QString> TYPES = {"A", "B", "C", ""};
static const QStringList OPTIONAL_CSV_HEADERS = {"description", "info"};
/// JSON keys
static const QString ITEMS_KEY_STRING = "items";
static const QString ITEM_KEY_STRING = "item";
@ -68,6 +75,9 @@ static UserRoles GET_ROLE_FOR_COLUMN(const int column) {
case 5:
return FactorRole;
break;
case 6:
return IdRole;
break;
default:
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
return NameRole;

View File

@ -40,10 +40,19 @@ TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
, m_undoStack(undoStack) {}
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
Qt::ItemFlags result = QAbstractTableModel::flags(index);
if (!index.isValid()) {
return QAbstractTableModel::flags(index);
return result;
}
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
const int column = index.column();
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
/// roles which aren't editable by the user
if (READ_ONLY_ROLES.contains(roleForColumn)) {
return result;
}
return result | Qt::ItemIsEditable;
}
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
@ -104,10 +113,9 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
}
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (role == Qt::EditRole && checkIndex(index)) {
const int column = index.column();
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
return setItemData(index, {{roleForColumn, value}});
if (checkIndex(index)) {
const int adjustedRole = getAppropriateRoleForIndex(index, role);
return setItemData(index, {{adjustedRole, value}});
}
return false;
}
@ -303,13 +311,6 @@ QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
const QVariant oldValue = index.data(role);
// TODO check if role is a editable role?
if (oldValue != newValue) {
bool emptyValueIsEqualToZero = isEmptyValueEqualToZero(role);
// REFACTOR the next if statement is too complex
if (emptyValueIsEqualToZero && oldValue == QVariant() && newValue == 0) {
qDebug() << "oldValue:" << oldValue << "& newValue:" << newValue
<< "mean the same. Ignoring...";
continue;
}
qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
result.insert(role, newValue);
} else {
@ -329,9 +330,26 @@ 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 {
// iterate over indexes to search item : see searchItem(...);
// for (const shared_ptr<ModelItem>& item : m_items) {
for (int row = 0; row < rowCount(); ++row) {
qDebug() << "Processing item at row" << row << "...";
QModelIndex itemIndex = index(row, 0);

View File

@ -71,6 +71,7 @@ class TableModel : public QAbstractTableModel {
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
const QMap<int, QVariant>& roleValueMap) const;
bool isEmptyValueEqualToZero(const int role) const;
int getAppropriateRoleForIndex(const QModelIndex& index, const int role) const;
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
const ModelItemValues givenItemValues) const;

164
utils/messagehandler.h Normal file
View 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