Show alert windows when a fatal map-loading error occurs

This commit is contained in:
Marcus Huderle 2020-02-12 11:23:28 -06:00
parent 8b4970413d
commit 2df0ac7d16
4 changed files with 61 additions and 13 deletions

View file

@ -18,5 +18,7 @@ void logInfo(QString message);
void logWarn(QString message); void logWarn(QString message);
void logError(QString message); void logError(QString message);
void log(QString message, LogType type); void log(QString message, LogType type);
QString getLogPath();
QString getMostRecentError();
#endif // LOG_H #endif // LOG_H

View file

@ -669,10 +669,13 @@ void Editor::setBorderItemsVisible(bool visible, qreal opacity) {
void Editor::setCurrentConnectionDirection(QString curDirection) { void Editor::setCurrentConnectionDirection(QString curDirection) {
if (!selected_connection_item) if (!selected_connection_item)
return; return;
Map *connected_map = project->getMap(selected_connection_item->connection->map_name);
if (!connected_map) {
return;
}
selected_connection_item->connection->direction = curDirection; selected_connection_item->connection->direction = curDirection;
Map *connected_map = project->getMap(selected_connection_item->connection->map_name);
QPixmap pixmap = connected_map->renderConnection(*selected_connection_item->connection, map->layout); QPixmap pixmap = connected_map->renderConnection(*selected_connection_item->connection, map->layout);
int offset = selected_connection_item->connection->offset.toInt(nullptr, 0); int offset = selected_connection_item->connection->offset.toInt(nullptr, 0);
selected_connection_item->initialOffset = offset; selected_connection_item->initialOffset = offset;
@ -1282,6 +1285,10 @@ void Editor::displayMapConnections() {
void Editor::createConnectionItem(MapConnection* connection, bool hide) { void Editor::createConnectionItem(MapConnection* connection, bool hide) {
Map *connected_map = project->getMap(connection->map_name); Map *connected_map = project->getMap(connection->map_name);
if (!connected_map) {
return;
}
QPixmap pixmap = connected_map->renderConnection(*connection, map->layout); QPixmap pixmap = connected_map->renderConnection(*connection, map->layout);
int offset = connection->offset.toInt(nullptr, 0); int offset = connection->offset.toInt(nullptr, 0);
int x = 0, y = 0; int x = 0, y = 0;
@ -1452,6 +1459,9 @@ void Editor::removeMirroredConnection(MapConnection* connection) {
void Editor::updateMirroredConnection(MapConnection* connection, QString originalDirection, QString originalMapName, bool isDelete) { void Editor::updateMirroredConnection(MapConnection* connection, QString originalDirection, QString originalMapName, bool isDelete) {
if (!ui->checkBox_MirrorConnections->isChecked()) if (!ui->checkBox_MirrorConnections->isChecked())
return; return;
Map* otherMap = project->getMap(originalMapName);
if (!otherMap)
return;
static QMap<QString, QString> oppositeDirections = QMap<QString, QString>({ static QMap<QString, QString> oppositeDirections = QMap<QString, QString>({
{"up", "down"}, {"right", "left"}, {"up", "down"}, {"right", "left"},
@ -1461,7 +1471,6 @@ void Editor::updateMirroredConnection(MapConnection* connection, QString origina
// Find the matching connection in the connected map. // Find the matching connection in the connected map.
MapConnection* mirrorConnection = nullptr; MapConnection* mirrorConnection = nullptr;
Map* otherMap = project->getMap(originalMapName);
for (MapConnection* conn : otherMap->connections) { for (MapConnection* conn : otherMap->connections) {
if (conn->direction == oppositeDirection && conn->map_name == map->name) { if (conn->direction == oppositeDirection && conn->map_name == map->name) {
mirrorConnection = conn; mirrorConnection = conn;

View file

@ -26,7 +26,10 @@ void logWarn(QString message) {
log(message, LogType::LOG_WARN); log(message, LogType::LOG_WARN);
} }
static QString mostRecentError;
void logError(QString message) { void logError(QString message) {
mostRecentError = message;
log(message, LogType::LOG_ERROR); log(message, LogType::LOG_ERROR);
} }
@ -65,16 +68,21 @@ void log(QString message, LogType type) {
message = QString("%1 %2 %3").arg(now).arg(typeString).arg(message); message = QString("%1 %2 %3").arg(now).arg(typeString).arg(message);
QString settingsPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(settingsPath);
if (!dir.exists())
dir.mkpath(settingsPath);
QString logPath = dir.absoluteFilePath("porymap.log");
qDebug().noquote() << colorizeMessage(message, type); qDebug().noquote() << colorizeMessage(message, type);
QFile outFile(logPath); QFile outFile(getLogPath());
outFile.open(QIODevice::WriteOnly | QIODevice::Append); outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream ts(&outFile); QTextStream ts(&outFile);
ts << message << endl; ts << message << endl;
} }
QString getLogPath() {
QString settingsPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(settingsPath);
if (!dir.exists())
dir.mkpath(settingsPath);
return dir.absoluteFilePath("porymap.log");
}
QString getMostRecentError() {
return mostRecentError;
}

View file

@ -293,6 +293,13 @@ bool MainWindow::openProject(QString dir) {
this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir)); this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir));
} else { } else {
this->statusBar()->showMessage(QString("Failed to open project %1").arg(nativeDir)); this->statusBar()->showMessage(QString("Failed to open project %1").arg(nativeDir));
QMessageBox msgBox(this);
QString errorMsg = QString("There was an error opening the project %1. Please see %2 for full error details.\n%3")
.arg(dir)
.arg(getLogPath())
.arg(getMostRecentError());
msgBox.critical(nullptr, "Error Opening Project", errorMsg);
} }
return success; return success;
@ -339,7 +346,9 @@ void MainWindow::on_action_Open_Project_triggered()
QString dir = getExistingDirectory(recent); QString dir = getExistingDirectory(recent);
if (!dir.isEmpty()) { if (!dir.isEmpty()) {
porymapConfig.setRecentProject(dir); porymapConfig.setRecentProject(dir);
openProject(dir); if (!openProject(dir)) {
this->initWindow();
}
} }
} }
@ -350,7 +359,7 @@ bool MainWindow::setMap(QString map_name, bool scrollTreeView) {
} }
if (!editor->setMap(map_name)) { if (!editor->setMap(map_name)) {
logError(QString("Failed to set map to '%1'").arg(map_name)); logWarn(QString("Failed to set map to '%1'").arg(map_name));
return false; return false;
} }
@ -441,6 +450,12 @@ void MainWindow::openWarpMap(QString map_name, QString warp_num) {
// Open the destination map, and select the target warp event. // Open the destination map, and select the target warp event.
if (!setMap(map_name, true)) { if (!setMap(map_name, true)) {
QMessageBox msgBox(this);
QString errorMsg = QString("There was an error opening map %1. Please see %2 for full error details.\n%3")
.arg(map_name)
.arg(getLogPath())
.arg(getMostRecentError());
msgBox.critical(nullptr, "Error Opening Map", errorMsg);
return; return;
} }
@ -1026,7 +1041,15 @@ void MainWindow::on_mapList_activated(const QModelIndex &index)
{ {
QVariant data = index.data(Qt::UserRole); QVariant data = index.data(Qt::UserRole);
if (index.data(MapListUserRoles::TypeRole) == "map_name" && !data.isNull()) { if (index.data(MapListUserRoles::TypeRole) == "map_name" && !data.isNull()) {
setMap(data.toString()); QString mapName = data.toString();
if (!setMap(mapName)) {
QMessageBox msgBox(this);
QString errorMsg = QString("There was an error opening map %1. Please see %2 for full error details.\n%3")
.arg(mapName)
.arg(getLogPath())
.arg(getMostRecentError());
msgBox.critical(nullptr, "Error Opening Map", errorMsg);
}
} }
} }
@ -1937,6 +1960,12 @@ void MainWindow::checkToolButtons() {
void MainWindow::onLoadMapRequested(QString mapName, QString fromMapName) { void MainWindow::onLoadMapRequested(QString mapName, QString fromMapName) {
if (!setMap(mapName, true)) { if (!setMap(mapName, true)) {
QMessageBox msgBox(this);
QString errorMsg = QString("There was an error opening map %1. Please see %2 for full error details.\n%3")
.arg(mapName)
.arg(getLogPath())
.arg(getMostRecentError());
msgBox.critical(nullptr, "Error Opening Map", errorMsg);
return; return;
} }
editor->setSelectedConnectionFromMap(fromMapName); editor->setSelectedConnectionFromMap(fromMapName);