From 7493c8f5276c74660c6c13e6f4fba4928d19a45a Mon Sep 17 00:00:00 2001 From: "G.K.MacGregor" Date: Sun, 26 Feb 2023 17:29:09 +0000 Subject: [PATCH] Add re-export option if .t42 file is loaded or exported This allows a single keypress or menu click to repeatedly export a subpage over the same .t42 file, like "Save" does for TTI files. If further teletext page formats are added in the future this option should remember which format was exported. This option is deliberately different from "Save" as .t42 files are only exported as the current subpage on view, and some metadata stored in TTI files but not in .t42 files could be lost. When exporting .t42 files the document is only marked as cleanly saved if it consisted of a single subpage. Documents of more than one subpage will still cause a confirmation dialog to be shown if unsaved, as the other subpages will be lost unless the document is saved again as TTI. --- mainwindow.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++-------- mainwindow.h | 7 ++++-- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index 00240f8..d618f77 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -371,9 +371,16 @@ void MainWindow::createActions() setRecentFilesVisible(MainWindow::hasRecentFiles()); + m_exportAutoAct = fileMenu->addAction(tr("Export subpage...")); + m_exportAutoAct->setEnabled(false); + m_exportAutoAct->setShortcut(tr("Ctrl+E")); + m_exportAutoAct->setStatusTip("Export this subpage back to the imported file"); + connect(fileMenu, &QMenu::aboutToShow, this, &MainWindow::updateExportAutoAction); + connect(m_exportAutoAct, &QAction::triggered, this, &MainWindow::exportAuto); + QAction *exportT42Act = fileMenu->addAction(tr("Export subpage as t42...")); exportT42Act->setStatusTip("Export this subpage as a t42 file"); - connect(exportT42Act, &QAction::triggered, this, &MainWindow::exportT42); + connect(exportT42Act, &QAction::triggered, this, [=]() { exportT42(false); }); QMenu *exportHashStringSubMenu = fileMenu->addMenu(tr("Export subpage to online editor")); @@ -934,10 +941,13 @@ void MainWindow::loadFile(const QString &fileName) QApplication::setOverrideCursor(Qt::WaitCursor); - if (fileInfo.suffix() == "t42") + if (fileInfo.suffix() == "t42") { importT42(&file, m_textWidget->document()); - else + m_exportAutoFileName = fileName; + } else { loadTTI(&file, m_textWidget->document()); + m_exportAutoFileName.clear(); + } levelSeen = m_textWidget->document()->levelRequired(); m_levelRadioButton[levelSeen]->toggle(); @@ -1021,6 +1031,18 @@ void MainWindow::updateRecentFileActions() m_recentFileActs[i]->setVisible(false); } +void MainWindow::updateExportAutoAction() +{ + if (m_exportAutoFileName.isEmpty()) { + m_exportAutoAct->setText(tr("Export subpage...")); + m_exportAutoAct->setEnabled(false); + return; + } + + m_exportAutoAct->setText(tr("Overwrite &%1").arg(MainWindow::strippedName(m_exportAutoFileName))); + m_exportAutoAct->setEnabled(true); +} + void MainWindow::openRecentFile() { if (const QAction *action = qobject_cast(sender())) @@ -1051,16 +1073,30 @@ bool MainWindow::saveFile(const QString &fileName) return true; } -void MainWindow::exportT42() +void MainWindow::exportAuto() +{ + // Menu should be disabled if m_exportAutoFileName is empty, but just in case... + if (m_exportAutoFileName.isEmpty()) + return; + + exportT42(true); +} + +void MainWindow::exportT42(bool fromAuto) { QString errorMessage; - QString exportFileName = m_curFile; + QString exportFileName; - changeSuffixFromTTI(exportFileName, "t42"); + if (fromAuto) + exportFileName = m_exportAutoFileName; + else { + exportFileName = m_curFile; + changeSuffixFromTTI(exportFileName, "t42"); - exportFileName = QFileDialog::getSaveFileName(this, tr("Export t42"), exportFileName, "t42 stream (*.t42)"); - if (exportFileName.isEmpty()) - return; + exportFileName = QFileDialog::getSaveFileName(this, tr("Export t42"), exportFileName, "t42 stream (*.t42)"); + if (exportFileName.isEmpty()) + return; + } QApplication::setOverrideCursor(Qt::WaitCursor); QSaveFile file(exportFileName); @@ -1072,8 +1108,20 @@ void MainWindow::exportT42() errorMessage = tr("Cannot open file %1 for writing:\n%2.").arg(QDir::toNativeSeparators(exportFileName), file.errorString()); QApplication::restoreOverrideCursor(); - if (!errorMessage.isEmpty()) + if (!errorMessage.isEmpty()) { QMessageBox::warning(this, QApplication::applicationDisplayName(), errorMessage); + return; + } + + // Only mark as cleanly saved if the document was a single subpage + // otherwise the other subpages could be lost if not saved as TTI + if (m_textWidget->document()->numberOfSubPages() == 1) { + setCurrentFile(exportFileName); + } else + MainWindow::prependToRecentFiles(exportFileName); + + m_exportAutoFileName = exportFileName; + statusBar()->showMessage(tr("File exported"), 2000); } void MainWindow::exportM29() diff --git a/mainwindow.h b/mainwindow.h index daba107..5aa5aca 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -60,12 +60,14 @@ private slots: bool save(); bool saveAs(); void reload(); - void exportT42(); + void exportAuto(); + void exportT42(bool); void exportZXNet(); void exportEditTF(); void exportPNG(); void exportM29(); void updateRecentFileActions(); + void updateExportAutoAction(); void openRecentFile(); void about(); void updatePageWidgets(); @@ -121,6 +123,7 @@ private: QAction *m_recentFileActs[m_MaxRecentFiles]; QAction *m_recentFileSeparator; QAction *m_recentFileSubMenuAct; + QAction *m_exportAutoAct; QAction *m_deleteSubPageAction; QAction *m_borderActs[3]; QAction *m_aspectRatioActs[4]; @@ -131,7 +134,7 @@ private: QPushButton *m_insertModePushButton; QRadioButton *m_levelRadioButton[4]; - QString m_curFile; + QString m_curFile, m_exportAutoFileName; bool m_isUntitled; };