// Network specification stuff void MainWindow::parseAndRouteNetwork(const QString& description) { // Attempt to build the graph int buildResult = buildNetwork(description); if (buildResult < 0) { // Error occurred; just clear everything and abort clearNetwork(); return; } // Load complete, lay it out if ((buildResult & WarningAbort) == 0) { postInfoMessage("Laying out graph..."); applySpringLayout(); postSuccessMessage("Graph loaded successfully!"); // Enable the UI m_controlsDock->enableClearNetwork(true); m_controlsDock->enableGraphLayoutOptions(true); // Extract the start and end points if (buildResult == Success) { postInfoMessage("Beginning routing step..."); routeNetwork(); // Enable the graph display controls and the report generator m_controlsDock->enableGraphDisplayOptions(true); m_controlsDock->enableGenerateReport(true); } else { // Make sure the route does not exist m_routeStart = m_routeEnd = nullptr; m_route.clear(); // Disable the UI stuff that depends on the route being generated m_controlsDock->enableGraphDisplayOptions(false); m_controlsDock->enableGenerateReport(false); // Inform the user postErrorMessage("Routing skipped step due to warnings."); } } }
int main(int argc, char** argv) { std::string basePath = inviwo::filesystem::findBasePath(); inviwo::LogCentral::init(); auto filelogger = std::make_shared<inviwo::FileLogger>(basePath); inviwo::LogCentral::getPtr()->registerLogger(filelogger); inviwo::InviwoApplicationQt inviwoApp("Inviwo v" + IVW_VERSION, argc, argv); inviwoApp.setWindowIcon(QIcon(":/icons/inviwo_light.png")); inviwoApp.setAttribute(Qt::AA_NativeWindows); QFile styleSheetFile(":/stylesheets/inviwo.qss"); styleSheetFile.open(QFile::ReadOnly); QString styleSheet = QString::fromUtf8(styleSheetFile.readAll()); inviwoApp.setStyleSheet(styleSheet); styleSheetFile.close(); auto& clp = inviwoApp.getCommandLineParser(); inviwo::InviwoMainWindow mainWin(&inviwoApp); // setup core application inviwoApp.setMainWindow(&mainWin); // initialize and show splash screen inviwo::InviwoSplashScreen splashScreen(&mainWin, clp.getShowSplashScreen()); inviwoApp.setProgressCallback([&splashScreen](std::string s) { splashScreen.showMessage(s); }); splashScreen.show(); splashScreen.showMessage("Loading application..."); // Initialize application and register modules splashScreen.showMessage("Initializing modules..."); inviwoApp.registerModules(&inviwo::registerAllModules); inviwoApp.processEvents(); // Do this after registerModules if some arguments were added clp.parse(inviwo::CommandLineParser::Mode::Normal); // setup main window mainWin.initialize(); inviwoApp.processEvents(); splashScreen.showMessage("Loading workspace..."); inviwoApp.processEvents(); mainWin.showWindow(); inviwoApp.processEvents(); // Make sure the gui is done loading before loading workspace mainWin.openLastWorkspace(clp.getWorkspacePath()); // open last workspace splashScreen.finish(&mainWin); inviwoApp.processEvents(); clp.processCallbacks(); // run any command line callbacks from modules. inviwoApp.processEvents(); inviwo::util::OnScopeExit clearNetwork([&](){ inviwoApp.getProcessorNetwork()->clear(); }); // process last arguments if (!clp.getQuitApplicationAfterStartup()) { return inviwoApp.exec(); } else { mainWin.exitInviwo(false); return 0; } }
int MainWindow::buildNetwork(const QString& description) { int result = Success; // Split into lines QStringList lines = description.split(QRegExp("[\\n|\\r]"), QString::SkipEmptyParts); if (lines.isEmpty()) { postErrorMessage("Problem specification is empty after whitespace " "removed!"); return ErrorEmpty; } // Validate the length of the specification int nodeCount = lines[0].toInt(); postInfoMessage(QString("Expecting %1x%1 adjacency matrix...") .arg(nodeCount)); if (lines.length() != (nodeCount + 2)) { postErrorMessage(QString("Expecting %1 lines in specification; read %2") .arg(nodeCount + 2) .arg(lines.length())); return ErrorSpecTooSmall; } // Clear the existing graph and scene if (boost::num_vertices(m_graph) != 0) { postWarningMessage("Existing network already loaded; must be cleared " "in order to continue."); int response = QMessageBox::question(this, "NetRoute", "There is " "already a graph in the explorer; the current data will have to " "be discared. Continue?"); if (response == QMessageBox::No) { postErrorMessage("Aborted by user."); return WarningAbort; } postInfoMessage("Discarding network."); clearNetwork(); } // Create the nodes postInfoMessage("Creating nodes..."); for (int i = 0; i < nodeCount; ++i) { QString name = QString("%1").arg(QChar('A' + i)); NodeItem* node = new NodeItem; node->setText(name); m_graphNodes[name] = node; boost::add_vertex(NodeProperties(node), m_graph); m_graphScene->addItem(node); } // Create the edges postInfoMessage("Creating edges from adjacency matrix..."); for (int i = 0; i < nodeCount; ++i) { QString line = lines[i + 1].trimmed(); QStringList weights = line.split(',', QString::SkipEmptyParts); // Sanity check if (weights.length() != nodeCount) { postErrorMessage( QString("Matrix row %1 has %2 columns; expecting %3.") .arg(i) .arg(weights.length()) .arg(nodeCount)); return ErrorRowTooShort; } // Actually create the edges postInfoMessage(QString("Creating edges for node %1") .arg(QChar('A' + i))); DigraphVertex vStart = boost::vertex(i, m_graph); for (int j = 0; j < nodeCount; ++j) { bool ok; int weight = weights[j].trimmed().toInt(&ok); if (ok && weight >= 0) { DigraphVertex vEnd = boost::vertex(j, m_graph); // Create the new edge item EdgeItem* edge = new EdgeItem; edge->setStartNode(m_graph[vStart].item); edge->setEndNode(m_graph[vEnd].item); edge->setWeight(weight); m_graphScene->addItem(edge); // Add it to the graph boost::add_edge(vStart, vEnd, EdgeProperties(edge), m_graph); } else if (!ok) { postWarningMessage(QString("Weight (%1,%2) is malformed: %3.") .arg(i) .arg(j) .arg(weights[j])); result |= WarningBadCell; } } } // Parse the final line of the description: the start/end nodes QStringList nodes = lines[lines.length() - 1].split(QRegExp("\\s+"), QString::SkipEmptyParts); if (nodes.length() != 2) { postWarningMessage("Start and end nodes line is malformed; " "routing will not take place."); result |= WarningBadStartEnd; } else { QString startNodeName = nodes[0]; QString endNodeName = nodes[1]; m_routeStart = m_graphNodes[startNodeName]; m_routeEnd = m_graphNodes[endNodeName]; if (!m_routeStart) { postWarningMessage(QString("Failed to find start node '%1'; " "routing will not take place.") .arg(startNodeName)); result |= WarningNoStartNode; } if (!m_routeEnd) { postWarningMessage(QString("Failed to find end node '%1'; " "routing will not take place.") .arg(endNodeName)); result |= WarningNoEndNode; } } // Graph was built successfully, even if some parsing errors arose. return result; }