comparison main/MainWindow.cpp @ 82:d82e332cb178

* Fix #1491849 strange behaviour on Cancel during query on new file load
author Chris Cannam
date Fri, 05 Jan 2007 12:59:51 +0000
parents 911c0bd745cd
children 41c491657587
comparison
equal deleted inserted replaced
81:911c0bd745cd 82:d82e332cb178
2048 MainWindow::importAudio() 2048 MainWindow::importAudio()
2049 { 2049 {
2050 QString path = getOpenFileName(AudioFile); 2050 QString path = getOpenFileName(AudioFile);
2051 2051
2052 if (path != "") { 2052 if (path != "") {
2053 if (!openAudioFile(path, ReplaceMainModel)) { 2053 if (openAudioFile(path, ReplaceMainModel) == FileOpenFailed) {
2054 QMessageBox::critical(this, tr("Failed to open file"), 2054 QMessageBox::critical(this, tr("Failed to open file"),
2055 tr("Audio file \"%1\" could not be opened").arg(path)); 2055 tr("Audio file \"%1\" could not be opened").arg(path));
2056 } 2056 }
2057 } 2057 }
2058 } 2058 }
2061 MainWindow::importMoreAudio() 2061 MainWindow::importMoreAudio()
2062 { 2062 {
2063 QString path = getOpenFileName(AudioFile); 2063 QString path = getOpenFileName(AudioFile);
2064 2064
2065 if (path != "") { 2065 if (path != "") {
2066 if (!openAudioFile(path, CreateAdditionalModel)) { 2066 if (openAudioFile(path, CreateAdditionalModel) == FileOpenFailed) {
2067 QMessageBox::critical(this, tr("Failed to open file"), 2067 QMessageBox::critical(this, tr("Failed to open file"),
2068 tr("Audio file \"%1\" could not be opened").arg(path)); 2068 tr("Audio file \"%1\" could not be opened").arg(path));
2069 } 2069 }
2070 } 2070 }
2071 } 2071 }
2196 2196
2197 QString path = getOpenFileName(LayerFile); 2197 QString path = getOpenFileName(LayerFile);
2198 2198
2199 if (path != "") { 2199 if (path != "") {
2200 2200
2201 if (!openLayerFile(path)) { 2201 if (openLayerFile(path) == FileOpenFailed) {
2202 QMessageBox::critical(this, tr("Failed to open file"), 2202 QMessageBox::critical(this, tr("Failed to open file"),
2203 tr("File %1 could not be opened.").arg(path)); 2203 tr("File %1 could not be opened.").arg(path));
2204 return; 2204 return;
2205 } 2205 }
2206 } 2206 }
2207 } 2207 }
2208 2208
2209 bool 2209 MainWindow::FileOpenStatus
2210 MainWindow::openLayerFile(QString path) 2210 MainWindow::openLayerFile(QString path)
2211 { 2211 {
2212 Pane *pane = m_paneStack->getCurrentPane(); 2212 Pane *pane = m_paneStack->getCurrentPane();
2213 2213
2214 if (!pane) { 2214 if (!pane) {
2215 // shouldn't happen, as the menu action should have been disabled 2215 // shouldn't happen, as the menu action should have been disabled
2216 std::cerr << "WARNING: MainWindow::openLayerFile: no current pane" << std::endl; 2216 std::cerr << "WARNING: MainWindow::openLayerFile: no current pane" << std::endl;
2217 return false; 2217 return FileOpenFailed;
2218 } 2218 }
2219 2219
2220 if (!getMainModel()) { 2220 if (!getMainModel()) {
2221 // shouldn't happen, as the menu action should have been disabled 2221 // shouldn't happen, as the menu action should have been disabled
2222 std::cerr << "WARNING: MainWindow::openLayerFile: No main model -- hence no default sample rate available" << std::endl; 2222 std::cerr << "WARNING: MainWindow::openLayerFile: No main model -- hence no default sample rate available" << std::endl;
2223 return false; 2223 return FileOpenFailed;
2224 } 2224 }
2225 2225
2226 if (path.endsWith(".svl") || path.endsWith(".xml")) { 2226 if (path.endsWith(".svl") || path.endsWith(".xml")) {
2227 2227
2228 PaneCallback callback(this); 2228 PaneCallback callback(this);
2230 2230
2231 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { 2231 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
2232 std::cerr << "ERROR: MainWindow::openLayerFile(" 2232 std::cerr << "ERROR: MainWindow::openLayerFile("
2233 << path.toStdString() 2233 << path.toStdString()
2234 << "): Failed to open file for reading" << std::endl; 2234 << "): Failed to open file for reading" << std::endl;
2235 return false; 2235 return FileOpenFailed;
2236 } 2236 }
2237 2237
2238 SVFileReader reader(m_document, callback); 2238 SVFileReader reader(m_document, callback);
2239 reader.setCurrentPane(pane); 2239 reader.setCurrentPane(pane);
2240 2240
2244 if (!reader.isOK()) { 2244 if (!reader.isOK()) {
2245 std::cerr << "ERROR: MainWindow::openLayerFile(" 2245 std::cerr << "ERROR: MainWindow::openLayerFile("
2246 << path.toStdString() 2246 << path.toStdString()
2247 << "): Failed to read XML file: " 2247 << "): Failed to read XML file: "
2248 << reader.getErrorString().toStdString() << std::endl; 2248 << reader.getErrorString().toStdString() << std::endl;
2249 return false; 2249 return FileOpenFailed;
2250 } 2250 }
2251 2251
2252 m_recentFiles.addFile(path); 2252 m_recentFiles.addFile(path);
2253 registerLastOpenedFilePath(LayerFile, path); // for file dialog 2253 registerLastOpenedFilePath(LayerFile, path); // for file dialog
2254 return true; 2254 return FileOpenSucceeded;
2255 2255
2256 } else { 2256 } else {
2257 2257
2258 Model *model = DataFileReaderFactory::load(path, getMainModel()->getSampleRate()); 2258 Model *model = DataFileReaderFactory::load(path, getMainModel()->getSampleRate());
2259 2259
2260 if (model) { 2260 if (model) {
2261 Layer *newLayer = m_document->createImportedLayer(model); 2261 Layer *newLayer = m_document->createImportedLayer(model);
2262 if (newLayer) { 2262 if (newLayer) {
2263 m_document->addLayerToView(pane, newLayer); 2263 m_document->addLayerToView(pane, newLayer);
2264 m_recentFiles.addFile(path); 2264 m_recentFiles.addFile(path);
2265 return true; 2265 return FileOpenSucceeded;
2266 } 2266 }
2267 } 2267 }
2268 } 2268 }
2269 2269
2270 return false; 2270 return FileOpenFailed;
2271 } 2271 }
2272 2272
2273 void 2273 void
2274 MainWindow::exportLayer() 2274 MainWindow::exportLayer()
2275 { 2275 {
2329 } else { 2329 } else {
2330 m_recentFiles.addFile(path); 2330 m_recentFiles.addFile(path);
2331 } 2331 }
2332 } 2332 }
2333 2333
2334 bool 2334 MainWindow::FileOpenStatus
2335 MainWindow::openAudioFile(QString path, AudioFileOpenMode mode) 2335 MainWindow::openAudioFile(QString path, AudioFileOpenMode mode)
2336 { 2336 {
2337 if (!(QFileInfo(path).exists() && 2337 if (!(QFileInfo(path).exists() &&
2338 QFileInfo(path).isFile() && 2338 QFileInfo(path).isFile() &&
2339 QFileInfo(path).isReadable())) { 2339 QFileInfo(path).isReadable())) {
2340 return false; 2340 return FileOpenFailed;
2341 } 2341 }
2342 2342
2343 m_openingAudioFile = true; 2343 m_openingAudioFile = true;
2344 2344
2345 WaveFileModel *newModel = new WaveFileModel(path); 2345 WaveFileModel *newModel = new WaveFileModel(path);
2346 2346
2347 if (!newModel->isOK()) { 2347 if (!newModel->isOK()) {
2348 delete newModel; 2348 delete newModel;
2349 m_openingAudioFile = false; 2349 m_openingAudioFile = false;
2350 return false; 2350 return FileOpenFailed;
2351 } 2351 }
2352 2352
2353 bool setAsMain = true; 2353 bool setAsMain = true;
2354 static bool prevSetAsMain = true; 2354 static bool prevSetAsMain = true;
2355 2355
2368 items, prevSetAsMain ? 0 : 1, &ok); 2368 items, prevSetAsMain ? 0 : 1, &ok);
2369 2369
2370 if (!ok || item.isEmpty()) { 2370 if (!ok || item.isEmpty()) {
2371 delete newModel; 2371 delete newModel;
2372 m_openingAudioFile = false; 2372 m_openingAudioFile = false;
2373 return false; 2373 return FileOpenCancelled;
2374 } 2374 }
2375 2375
2376 setAsMain = (item == items[0]); 2376 setAsMain = (item == items[0]);
2377 prevSetAsMain = setAsMain; 2377 prevSetAsMain = setAsMain;
2378 } 2378 }
2441 updateMenuStates(); 2441 updateMenuStates();
2442 m_recentFiles.addFile(path); 2442 m_recentFiles.addFile(path);
2443 registerLastOpenedFilePath(AudioFile, path); // for file dialog 2443 registerLastOpenedFilePath(AudioFile, path); // for file dialog
2444 m_openingAudioFile = false; 2444 m_openingAudioFile = false;
2445 2445
2446 return true; 2446 return FileOpenSucceeded;
2447 } 2447 }
2448 2448
2449 void 2449 void
2450 MainWindow::createPlayTarget() 2450 MainWindow::createPlayTarget()
2451 { 2451 {
2580 2580
2581 QString path = getOpenFileName(SessionFile); 2581 QString path = getOpenFileName(SessionFile);
2582 2582
2583 if (path.isEmpty()) return; 2583 if (path.isEmpty()) return;
2584 2584
2585 if (!openSessionFile(path)) { 2585 if (openSessionFile(path) == FileOpenFailed) {
2586 QMessageBox::critical(this, tr("Failed to open file"), 2586 QMessageBox::critical(this, tr("Failed to open file"),
2587 tr("Session file \"%1\" could not be opened").arg(path)); 2587 tr("Session file \"%1\" could not be opened").arg(path));
2588 } 2588 }
2589 } 2589 }
2590 2590
2605 2605
2606 if (path.endsWith(".sv")) { 2606 if (path.endsWith(".sv")) {
2607 2607
2608 if (!checkSaveModified()) return; 2608 if (!checkSaveModified()) return;
2609 2609
2610 if (!openSessionFile(path)) { 2610 if (openSessionFile(path) == FileOpenFailed) {
2611 QMessageBox::critical(this, tr("Failed to open file"), 2611 QMessageBox::critical(this, tr("Failed to open file"),
2612 tr("Session file \"%1\" could not be opened").arg(path)); 2612 tr("Session file \"%1\" could not be opened").arg(path));
2613 } 2613 }
2614 2614
2615 } else { 2615 } else {
2616 2616
2617 if (!openAudioFile(path, AskUser)) { 2617 if (openAudioFile(path, AskUser) == FileOpenFailed) {
2618 2618
2619 if (!canImportLayer || !openLayerFile(path)) { 2619 if (!canImportLayer || (openLayerFile(path) == FileOpenFailed)) {
2620 2620
2621 QMessageBox::critical(this, tr("Failed to open file"), 2621 QMessageBox::critical(this, tr("Failed to open file"),
2622 tr("File \"%1\" could not be opened").arg(path)); 2622 tr("File \"%1\" could not be opened").arg(path));
2623 } 2623 }
2624 } 2624 }
2642 2642
2643 if (path.endsWith("sv")) { 2643 if (path.endsWith("sv")) {
2644 2644
2645 if (!checkSaveModified()) return ; 2645 if (!checkSaveModified()) return ;
2646 2646
2647 if (!openSessionFile(path)) { 2647 if (openSessionFile(path) == FileOpenFailed) {
2648 QMessageBox::critical(this, tr("Failed to open file"), 2648 QMessageBox::critical(this, tr("Failed to open file"),
2649 tr("Session file \"%1\" could not be opened").arg(path)); 2649 tr("Session file \"%1\" could not be opened").arg(path));
2650 } 2650 }
2651 2651
2652 } else { 2652 } else {
2653 2653
2654 if (!openAudioFile(path, AskUser)) { 2654 if (openAudioFile(path, AskUser) == FileOpenFailed) {
2655 2655
2656 bool canImportLayer = (getMainModel() != 0 && 2656 bool canImportLayer = (getMainModel() != 0 &&
2657 m_paneStack != 0 && 2657 m_paneStack != 0 &&
2658 m_paneStack->getCurrentPane() != 0); 2658 m_paneStack->getCurrentPane() != 0);
2659 2659
2660 if (!canImportLayer || !openLayerFile(path)) { 2660 if (!canImportLayer || (openLayerFile(path) == FileOpenFailed)) {
2661 2661
2662 QMessageBox::critical(this, tr("Failed to open file"), 2662 QMessageBox::critical(this, tr("Failed to open file"),
2663 tr("File \"%1\" could not be opened").arg(path)); 2663 tr("File \"%1\" could not be opened").arg(path));
2664 } 2664 }
2665 } 2665 }
2666 } 2666 }
2667 } 2667 }
2668 2668
2669 bool 2669 MainWindow::FileOpenStatus
2670 MainWindow::openSomeFile(QString path, AudioFileOpenMode mode) 2670 MainWindow::openSomeFile(QString path, AudioFileOpenMode mode)
2671 { 2671 {
2672 if (openAudioFile(path, mode)) { 2672 FileOpenStatus status;
2673 return true; 2673
2674 } else if (openSessionFile(path)) { 2674 if ((status = openAudioFile(path, mode)) != FileOpenFailed) {
2675 return true; 2675 return status;
2676 } else if ((status = openSessionFile(path)) != FileOpenFailed) {
2677 return status;
2676 } else { 2678 } else {
2677 return false; 2679 return FileOpenFailed;
2678 } 2680 }
2679 } 2681 }
2680 2682
2681 bool 2683 MainWindow::FileOpenStatus
2682 MainWindow::openSessionFile(QString path) 2684 MainWindow::openSessionFile(QString path)
2683 { 2685 {
2684 BZipFileDevice bzFile(path); 2686 BZipFileDevice bzFile(path);
2685 if (!bzFile.open(QIODevice::ReadOnly)) { 2687 if (!bzFile.open(QIODevice::ReadOnly)) {
2686 std::cerr << "Failed to open session file \"" << path.toStdString() 2688 std::cerr << "Failed to open session file \"" << path.toStdString()
2687 << "\": " << bzFile.errorString().toStdString() << std::endl; 2689 << "\": " << bzFile.errorString().toStdString() << std::endl;
2688 return false; 2690 return FileOpenFailed;
2689 } 2691 }
2690 2692
2691 QString error; 2693 QString error;
2692 closeSession(); 2694 closeSession();
2693 createDocument(); 2695 createDocument();
2720 registerLastOpenedFilePath(SessionFile, path); // for file dialog 2722 registerLastOpenedFilePath(SessionFile, path); // for file dialog
2721 } else { 2723 } else {
2722 setWindowTitle(tr("Sonic Visualiser")); 2724 setWindowTitle(tr("Sonic Visualiser"));
2723 } 2725 }
2724 2726
2725 return ok; 2727 return ok ? FileOpenSucceeded : FileOpenFailed;
2726 } 2728 }
2727 2729
2728 void 2730 void
2729 MainWindow::closeEvent(QCloseEvent *e) 2731 MainWindow::closeEvent(QCloseEvent *e)
2730 { 2732 {
3729 if (message.getMethod() == "open") { 3731 if (message.getMethod() == "open") {
3730 3732
3731 if (message.getArgCount() == 1 && 3733 if (message.getArgCount() == 1 &&
3732 message.getArg(0).canConvert(QVariant::String)) { 3734 message.getArg(0).canConvert(QVariant::String)) {
3733 QString path = message.getArg(0).toString(); 3735 QString path = message.getArg(0).toString();
3734 if (!openSomeFile(path, ReplaceMainModel)) { 3736 if (openSomeFile(path, ReplaceMainModel) != FileOpenSucceeded) {
3735 std::cerr << "MainWindow::handleOSCMessage: File open failed for path \"" 3737 std::cerr << "MainWindow::handleOSCMessage: File open failed for path \""
3736 << path.toStdString() << "\"" << std::endl; 3738 << path.toStdString() << "\"" << std::endl;
3737 } 3739 }
3738 //!!! we really need to spin here and not return until the 3740 //!!! we really need to spin here and not return until the
3739 // file has been completely decoded... 3741 // file has been completely decoded...
3742 } else if (message.getMethod() == "openadditional") { 3744 } else if (message.getMethod() == "openadditional") {
3743 3745
3744 if (message.getArgCount() == 1 && 3746 if (message.getArgCount() == 1 &&
3745 message.getArg(0).canConvert(QVariant::String)) { 3747 message.getArg(0).canConvert(QVariant::String)) {
3746 QString path = message.getArg(0).toString(); 3748 QString path = message.getArg(0).toString();
3747 if (!openSomeFile(path, CreateAdditionalModel)) { 3749 if (openSomeFile(path, CreateAdditionalModel) != FileOpenSucceeded) {
3748 std::cerr << "MainWindow::handleOSCMessage: File open failed for path \"" 3750 std::cerr << "MainWindow::handleOSCMessage: File open failed for path \""
3749 << path.toStdString() << "\"" << std::endl; 3751 << path.toStdString() << "\"" << std::endl;
3750 } 3752 }
3751 } 3753 }
3752 3754
3759 message.getArg(0).canConvert(QVariant::Int)) { 3761 message.getArg(0).canConvert(QVariant::Int)) {
3760 n = message.getArg(0).toInt() - 1; 3762 n = message.getArg(0).toInt() - 1;
3761 } 3763 }
3762 std::vector<QString> recent = m_recentFiles.getRecent(); 3764 std::vector<QString> recent = m_recentFiles.getRecent();
3763 if (n >= 0 && n < recent.size()) { 3765 if (n >= 0 && n < recent.size()) {
3764 if (!openSomeFile(recent[n], ReplaceMainModel)) { 3766 if (openSomeFile(recent[n], ReplaceMainModel) != FileOpenSucceeded) {
3765 std::cerr << "MainWindow::handleOSCMessage: File open failed for path \"" 3767 std::cerr << "MainWindow::handleOSCMessage: File open failed for path \""
3766 << recent[n].toStdString() << "\"" << std::endl; 3768 << recent[n].toStdString() << "\"" << std::endl;
3767 } 3769 }
3768 } 3770 }
3769 3771