diff --git a/src/app/MainWindow.cpp b/src/app/MainWindow.cpp index e69de29..b31e068 100644 --- a/src/app/MainWindow.cpp +++ b/src/app/MainWindow.cpp @@ -0,0 +1,203 @@ +#include "MainWindow.h" +#include "ui_MainWindow.h" + +//#include "viewerfactory.h" +//#include "abstractviewer.h" +//#include "recentfiles.h" +//#include "recentfilemenu.h" + +#include +#include +#include + +#include +#include + +using namespace Qt::StringLiterals; + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + connect(ui->actionOpen, &QAction::triggered, this, &MainWindow::onActionOpenTriggered); + connect(ui->actionAbout, &QAction::triggered, this, &MainWindow::onActionAboutTriggered); + connect(ui->actionAboutQt, &QAction::triggered, this, &MainWindow::onActionAboutQtTriggered); + + /* + m_recentFiles.reset(new RecentFiles(ui->actionRecent)); + connect(m_recentFiles.get(), &RecentFiles::countChanged, this, [&](int count){ + ui->actionRecent->setText(tr("%n recent files", nullptr, count)); + }); + + readSettings(); + m_factory.reset(new ViewerFactory(ui->viewArea, this)); + const QStringList &viewers = m_factory->viewerNames(); + + const QString msg = tr("Available viewers: %1").arg(viewers.join(", "_L1)); + statusBar()->showMessage(msg); + + auto *menu = new RecentFileMenu(this, m_recentFiles.get()); + ui->actionRecent->setMenu(menu); + connect(menu, &RecentFileMenu::fileOpened, this, &MainWindow::openFile); + QWidget *w = ui->mainToolBar->widgetForAction(ui->actionRecent); + auto *button = qobject_cast(w); + if (button) + connect(ui->actionRecent, &QAction::triggered, button, &QToolButton::showMenu); + */ +} + +MainWindow::~MainWindow() +{ + saveSettings(); +} + +void MainWindow::onActionOpenTriggered() +{ + QFileDialog fileDialog(this, tr("Open Document"), m_currentDir.absolutePath()); + while (fileDialog.exec() == QDialog::Accepted + && !openFile(fileDialog.selectedFiles().constFirst())) { + } +} + +bool MainWindow::openFile(const QString &fileName) +{ + /* + QFile *file = new QFile(fileName); + if (!file->exists()) { + statusBar()->showMessage(tr("File %1 could not be opened") + .arg(QDir::toNativeSeparators(fileName))); + delete file; + return false; + } + + QFileInfo fileInfo(*file); + m_currentDir = fileInfo.dir(); + m_recentFiles->addFile(fileInfo.absoluteFilePath()); + + // If a viewer is already open, clean it up and save its settings + resetViewer(); + m_viewer = m_factory->viewer(file); + if (!m_viewer) { + statusBar()->showMessage(tr("File %1 can't be opened.") + .arg(QDir::toNativeSeparators(fileName))); + return false; + } + + ui->actionPrint->setEnabled(m_viewer->hasContent()); + connect(m_viewer, &AbstractViewer::printingEnabledChanged, ui->actionPrint, &QAction::setEnabled); + connect(ui->actionPrint, &QAction::triggered, m_viewer, &AbstractViewer::print); + connect(m_viewer, &AbstractViewer::showMessage, statusBar(), &QStatusBar::showMessage); + + m_viewer->initViewer(ui->actionBack, ui->actionForward, ui->menuHelp->menuAction(), ui->tabWidget); + restoreViewerSettings(); + ui->scrollArea->setWidget(m_viewer->widget()); + */ + return true; +} + +void MainWindow::onActionAboutTriggered() +{ + /* + const QString viewerNames = m_factory->viewerNames().join(", "_L1); + const QString mimeTypes = m_factory->supportedMimeTypes().join(u'\n'); + QString text = tr("A Widgets application to display and print JSON, " + "text and PDF files. Demonstrates various features to use " + "in widget applications: Using QSettings, query and save " + "user preferences, manage file histories and control cursor " + "behavior when hovering over widgets.\n\n" + "This version has loaded the following plugins:\n%1\n" + "\n\nIt supports the following mime types:\n%2") + .arg(viewerNames, mimeTypes); + + if (auto *def = m_factory->defaultViewer()) + text += tr("\n\nOther mime types will be displayed with %1.").arg(def->viewerName()); + + */ + //QMessageBox::about(this, tr("About Document Viewer Demo"), text); +} + +void MainWindow::onActionAboutQtTriggered() +{ + QMessageBox::aboutQt(this); +} + +void MainWindow::readSettings() +{ + /* + QSettings settings; + + // Restore working directory + if (settings.contains(settingsDir)) + m_currentDir = QDir(settings.value(settingsDir).toString()); + else + m_currentDir = QDir::current(); + + // Restore QMainWindow state + if (settings.contains(settingsMainWindow)) { + QByteArray mainWindowState = settings.value(settingsMainWindow).toByteArray(); + restoreState(mainWindowState); + } + + // Restore recent files + m_recentFiles->restoreFromSettings(settings, settingsFiles); + */ +} + +void MainWindow::saveSettings() const +{ + /* + QSettings settings; + + // Save working directory + settings.setValue(settingsDir, m_currentDir.absolutePath()); + + // Save QMainWindow state + settings.setValue(settingsMainWindow, saveState()); + + // Save recent files + m_recentFiles->saveSettings(settings, settingsFiles); + + settings.sync(); + */ +} + +void MainWindow::saveViewerSettings() const +{ + /* + if (!m_viewer) + return; + + QSettings settings; + settings.beginGroup(settingsViewers); + settings.setValue(m_viewer->viewerName(), m_viewer->saveState()); + settings.endGroup(); + settings.sync(); + */ +} + +void MainWindow::resetViewer() const +{ + /* + if (!m_viewer) + return; + + saveViewerSettings(); + m_viewer->cleanup(); + */ +} + +void MainWindow::restoreViewerSettings() +{ + /* + if (!m_viewer) + return; + + QSettings settings; + settings.beginGroup(settingsViewers); + QByteArray viewerSettings = settings.value(m_viewer->viewerName(), QByteArray()).toByteArray(); + settings.endGroup(); + if (!viewerSettings.isEmpty()) + m_viewer->restoreState(viewerSettings); + */ +} \ No newline at end of file diff --git a/src/app/MainWindow.h b/src/app/MainWindow.h index e69de29..8fee9e0 100644 --- a/src/app/MainWindow.h +++ b/src/app/MainWindow.h @@ -0,0 +1,53 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + +class AbstractViewer; +class RecentFiles; +class ViewerFactory; + +QT_BEGIN_NAMESPACE +namespace Ui { +class MainWindow; +} +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +public slots: + bool openFile(const QString &fileName); + +private slots: + void onActionOpenTriggered(); + void onActionAboutTriggered(); + void onActionAboutQtTriggered(); + +private: + void readSettings(); + void saveSettings() const; + void restoreViewerSettings(); + void resetViewer() const; + void saveViewerSettings() const; + + QDir m_currentDir; + AbstractViewer *m_viewer = nullptr; + std::unique_ptr ui; + //std::unique_ptr m_recentFiles; + //std::unique_ptr m_factory; + + static constexpr QLatin1StringView settingsDir = QLatin1StringView("WorkingDir"); + static constexpr QLatin1StringView settingsMainWindow = QLatin1StringView("MainWindow"); + static constexpr QLatin1StringView settingsViewers = QLatin1StringView("Viewers"); + static constexpr QLatin1StringView settingsFiles = QLatin1StringView("RecentFiles"); +}; + +#endif // MAINWINDOW_H \ No newline at end of file diff --git a/src/app/MainWindow.ui b/src/app/MainWindow.ui index e69de29..088dd3f 100644 --- a/src/app/MainWindow.ui +++ b/src/app/MainWindow.ui @@ -0,0 +1,272 @@ + + + MainWindow + + + + 0 + 0 + 983 + 602 + + + + Document Viewer Demo + + + + :/demos/documentviewer/images/qt-logo.png:/demos/documentviewer/images/qt-logo.png + + + + true + + + + + + + + + Qt::Horizontal + + + + QTabWidget::West + + + 0 + + + + Pages + + + + + Bookmarks + + + + + + + 0 + 0 + + + + + 800 + 0 + + + + true + + + + + 0 + 0 + 798 + 479 + + + + + + + + + + + + + + + 0 + 0 + 983 + 23 + + + + + File + + + + + + + + + Help + + + + + + + + + + + toolBar + + + TopToolBarArea + + + false + + + + + + + + + + + + + :/demos/documentviewer/images/open.png:/demos/documentviewer/images/open.png + + + Open + + + Ctrl+O + + + + + + :/demos/documentviewer/images/help-about.svgz:/demos/documentviewer/images/help-about.svgz + + + about documentviewer + + + Show information about the Document Viewer deomo. + + + Ctrl+H + + + + + + :/demos/documentviewer/images/go-next.svgz:/demos/documentviewer/images/go-next.svgz + + + actionForward + + + One step forward + + + Right + + + + + + :/demos/documentviewer/images/go-previous.svgz:/demos/documentviewer/images/go-previous.svgz + + + actionBack + + + One step back + + + Left + + + + + false + + + + :/demos/documentviewer/images/print2x.png:/demos/documentviewer/images/print2x.png + + + Print + + + Print current file + + + Ctrl+P + + + + + + :/demos/documentviewer/images/qt-logo.png + :/demos/documentviewer/images/qt-logo.png:/demos/documentviewer/images/qt-logo.png + + + About Qt + + + Show Qt license information + + + Ctrl+I + + + + + + :/demos/documentviewer/images/document-open-recent.svgz:/demos/documentviewer/images/document-open-recent.svgz + + + Recently opened... + + + Meta+R + + + + + + .. + + + Quit + + + Quit the application + + + Ctrl+Q + + + + + + + + + actionQuit + triggered() + MainWindow + close() + + + -1 + -1 + + + 491 + 300 + + + + + \ No newline at end of file diff --git a/src/app/main.cpp b/src/app/main.cpp index 45e8413..bbc6e54 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -3,6 +3,8 @@ #include "MainWindow.h" +using namespace Qt::StringLiterals; + int main(int argc, char *argv[]) { QApplication app(argc, argv);