Compare commits

...

4 Commits

6 changed files with 49 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -25,8 +25,11 @@ 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, static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, TypeRole,
TypeRole, AmountRole, FactorRole}; AmountRole, FactorRole, IdRole};
static QList<UserRoles> READ_ONLY_ROLES = {IdRole};
static QHash<int, QByteArray> ROLE_NAMES = { static QHash<int, QByteArray> ROLE_NAMES = {
{NameRole, "name"}, {DescriptionRole, "description"}, {NameRole, "name"}, {DescriptionRole, "description"},
{InfoRole, "info"}, {TypeRole, "type"}, {InfoRole, "info"}, {TypeRole, "type"},
@ -37,10 +40,13 @@ static QHash<int, QByteArray> ROLE_NAMES = {
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 QList<UserRoles> NUMBER_ROLES = INT_ROLES + DOUBLE_ROLES;
static const QList<UserRoles> TYPE_ROLES = {TypeRole}; static const QList<UserRoles> TYPE_ROLES = {TypeRole};
static const QList<QString> TYPES = {"A", "B", "C", ""}; static const QList<QString> TYPES = {"A", "B", "C", ""};
static const QStringList OPTIONAL_CSV_HEADERS = {"description", "info"};
/// 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";
@ -69,6 +75,9 @@ static UserRoles GET_ROLE_FOR_COLUMN(const int column) {
case 5: case 5:
return FactorRole; return FactorRole;
break; break;
case 6:
return IdRole;
break;
default: default:
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column); qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
return NameRole; return NameRole;

View File

@ -40,10 +40,19 @@ TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
, m_undoStack(undoStack) {} , m_undoStack(undoStack) {}
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const { Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
Qt::ItemFlags result = QAbstractTableModel::flags(index);
if (!index.isValid()) { 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; } QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
@ -302,13 +311,6 @@ QMap<int, QVariant> TableModel::onlyChangedValues(const QModelIndex& index,
const QVariant oldValue = index.data(role); const QVariant oldValue = index.data(role);
// 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);
// 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; qDebug() << "oldValue:" << oldValue << "!= newValue:" << newValue;
result.insert(role, newValue); result.insert(role, newValue);
} else { } else {
@ -348,8 +350,6 @@ int TableModel::getAppropriateRoleForIndex(const QModelIndex& index, const int r
} }
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const { 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) { for (int row = 0; row < rowCount(); ++row) {
qDebug() << "Processing item at row" << row << "..."; qDebug() << "Processing item at row" << row << "...";
QModelIndex itemIndex = index(row, 0); QModelIndex itemIndex = index(row, 0);