| 485 | } |
| 486 | |
| 487 | void SettingsEditor::validateSettings() |
| 488 | { |
| 489 | SettingsValidator validator; |
| 490 | { |
| 491 | auto settings = this->settings(); |
| 492 | if (validator.correct(settings, models_)) { |
| 493 | setSettings(settings); |
| 494 | return; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | for (auto i = 0, end = pageModel_->rowCount(); i < end; ++i) { |
| 499 | const auto name = pageModel_->index(i, int(PageColumn::Name)); |
| 500 | pageModel_->setData(name, QBrush(Qt::black), Qt::ForegroundRole); |
| 501 | |
| 502 | const auto error = pageModel_->index(i, int(PageColumn::Error)); |
| 503 | pageModel_->setData(error, {}); |
| 504 | } |
| 505 | |
| 506 | const auto errors = validator.check(settings(), models_); |
| 507 | if (errors.isEmpty()) |
| 508 | return; |
| 509 | |
| 510 | using E = SettingsValidator::Error; |
| 511 | QMap<E, Page> errorToPage{ |
| 512 | {E::NoSourceInstalled, Page::Update}, |
| 513 | {E::NoSourceSet, Page::Recognition}, |
| 514 | {E::NoTranslatorInstalled, Page::Update}, |
| 515 | {E::NoTranslatorSet, Page::Translation}, |
| 516 | {E::NoTargetSet, Page::Translation}, |
| 517 | }; |
| 518 | |
| 519 | QMap<Page, QStringList> summary; |
| 520 | for (const auto err : errors) { |
| 521 | SOFT_ASSERT(errorToPage.contains(err), continue); |
| 522 | auto page = errorToPage[err]; |
| 523 | summary[page].push_back(validator.toString(err)); |
| 524 | } |
| 525 | |
| 526 | for (auto it = summary.cbegin(), end = summary.cend(); it != end; ++it) { |
| 527 | const auto row = int(it.key()); |
| 528 | const auto index = pageModel_->index(row, int(PageColumn::Name)); |
| 529 | pageModel_->setData(index, QBrush(Qt::red), Qt::ForegroundRole); |
| 530 | |
| 531 | const auto error = pageModel_->index(row, int(PageColumn::Error)); |
| 532 | pageModel_->setData(error, it.value().join('\n')); |
| 533 | } |
| 534 | } |