Start pulling together main window from samples.

This commit is contained in:
jgrogan 2024-03-28 16:52:31 +00:00
parent dddb296a3a
commit 989ce4e9a7
4 changed files with 530 additions and 0 deletions

View file

@ -0,0 +1,203 @@
#include "MainWindow.h"
#include "ui_MainWindow.h"
//#include "viewerfactory.h"
//#include "abstractviewer.h"
//#include "recentfiles.h"
//#include "recentfilemenu.h"
#include <QFileDialog>
#include <QToolButton>
#include <QMessageBox>
#include <QDir>
#include <QSettings>
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<QToolButton *>(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);
*/
}

View file

@ -0,0 +1,53 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDir>
#include <QStringLiteral>
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::MainWindow> ui;
//std::unique_ptr<RecentFiles> m_recentFiles;
//std::unique_ptr<ViewerFactory> 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

View file

@ -0,0 +1,272 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>983</width>
<height>602</height>
</rect>
</property>
<property name="windowTitle">
<string>Document Viewer Demo</string>
</property>
<property name="windowIcon">
<iconset resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/qt-logo.png</normaloff>:/demos/documentviewer/images/qt-logo.png</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<property name="enabled">
<bool>true</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="viewArea" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="bookmarkTab">
<attribute name="title">
<string>Pages</string>
</attribute>
</widget>
<widget class="QWidget" name="pagesTab">
<attribute name="title">
<string>Bookmarks</string>
</attribute>
</widget>
</widget>
<widget class="QScrollArea" name="scrollArea">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>800</width>
<height>0</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>798</width>
<height>479</height>
</rect>
</property>
</widget>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>983</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="qtFileMenu">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionRecent"/>
<addaction name="actionPrint"/>
<addaction name="actionQuit"/>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
<addaction name="actionAbout"/>
<addaction name="actionAboutQt"/>
</widget>
<addaction name="qtFileMenu"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<widget class="QToolBar" name="mainToolBar">
<property name="windowTitle">
<string>toolBar</string>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionOpen"/>
<addaction name="actionRecent"/>
<addaction name="actionPrint"/>
<addaction name="separator"/>
<addaction name="actionBack"/>
<addaction name="actionForward"/>
<addaction name="separator"/>
</widget>
<action name="actionOpen">
<property name="icon">
<iconset resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/open.png</normaloff>:/demos/documentviewer/images/open.png</iconset>
</property>
<property name="text">
<string>Open</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action>
<action name="actionAbout">
<property name="icon">
<iconset theme="help-about" resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/help-about.svgz</normaloff>:/demos/documentviewer/images/help-about.svgz</iconset>
</property>
<property name="text">
<string>about documentviewer</string>
</property>
<property name="toolTip">
<string>Show information about the Document Viewer deomo.</string>
</property>
<property name="shortcut">
<string>Ctrl+H</string>
</property>
</action>
<action name="actionForward">
<property name="icon">
<iconset resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/go-next.svgz</normaloff>:/demos/documentviewer/images/go-next.svgz</iconset>
</property>
<property name="text">
<string>actionForward</string>
</property>
<property name="toolTip">
<string>One step forward</string>
</property>
<property name="shortcut">
<string>Right</string>
</property>
</action>
<action name="actionBack">
<property name="icon">
<iconset resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/go-previous.svgz</normaloff>:/demos/documentviewer/images/go-previous.svgz</iconset>
</property>
<property name="text">
<string>actionBack</string>
</property>
<property name="toolTip">
<string>One step back</string>
</property>
<property name="shortcut">
<string>Left</string>
</property>
</action>
<action name="actionPrint">
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset theme="document-print" resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/print2x.png</normaloff>:/demos/documentviewer/images/print2x.png</iconset>
</property>
<property name="text">
<string>Print</string>
</property>
<property name="toolTip">
<string>Print current file</string>
</property>
<property name="shortcut">
<string>Ctrl+P</string>
</property>
</action>
<action name="actionAboutQt">
<property name="icon">
<iconset resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/qt-logo.png</normaloff>
<normalon>:/demos/documentviewer/images/qt-logo.png</normalon>:/demos/documentviewer/images/qt-logo.png</iconset>
</property>
<property name="text">
<string>About Qt</string>
</property>
<property name="toolTip">
<string>Show Qt license information</string>
</property>
<property name="shortcut">
<string>Ctrl+I</string>
</property>
</action>
<action name="actionRecent">
<property name="icon">
<iconset resource="documentviewer.qrc">
<normaloff>:/demos/documentviewer/images/document-open-recent.svgz</normaloff>:/demos/documentviewer/images/document-open-recent.svgz</iconset>
</property>
<property name="text">
<string>Recently opened...</string>
</property>
<property name="shortcut">
<string>Meta+R</string>
</property>
</action>
<action name="actionQuit">
<property name="icon">
<iconset theme="application-exit">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Quit</string>
</property>
<property name="toolTip">
<string>Quit the application</string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
</widget>
<resources>
<include location="documentviewer.qrc"/>
</resources>
<connections>
<connection>
<sender>actionQuit</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>491</x>
<y>300</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View file

@ -3,6 +3,8 @@
#include "MainWindow.h"
using namespace Qt::StringLiterals;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);