#include "./mainwindow.h" #include "./webpage.h" #include "resources/config.h" #include #include #include #include #include #include #include #include namespace RepoIndex { /*! * \brief Constructs a new main window. */ MainWindow::MainWindow(const QString &webdir) { QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName()); setWindowTitle(QStringLiteral(APP_NAME)); setWindowIcon(QIcon::fromTheme(QStringLiteral(PROJECT_NAME))); m_webView.setPage(new WebPage(&m_webView)); QUrl url(QStringLiteral("file://") % webdir % QStringLiteral("/index.html")); url.setFragment(settings.value(QStringLiteral("fragment"), QStringLiteral("packages")).toString()); m_webView.setUrl(url); m_webView.setContextMenuPolicy(Qt::CustomContextMenu); connect(&m_webView, &QWidget::customContextMenuRequested, this, &MainWindow::showInfoWebViewContextMenu); setCentralWidget(&m_webView); restoreGeometry(settings.value(QStringLiteral("geometry")).toByteArray()); } bool MainWindow::event(QEvent *event) { switch(event->type()) { case QEvent::KeyRelease: { QKeyEvent *keyEvent = static_cast(event); switch(keyEvent->key()) { case Qt::Key_Left: case Qt::Key_Back: m_webView.back(); return true; case Qt::Key_Right: case Qt::Key_Forward: m_webView.forward(); return true; default: ; } } case QEvent::MouseButtonPress: { QMouseEvent *mouseEvent = static_cast(event); switch(mouseEvent->button()) { case Qt::BackButton: m_webView.back(); return true; case Qt::ForwardButton: m_webView.forward(); return true; default: ; } break; } case QEvent::Close: { // save settings QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName()); settings.setValue(QStringLiteral("geometry"), saveGeometry()); settings.setValue(QStringLiteral("fragment"), m_webView.url().fragment()); break; } default: ; } return QMainWindow::event(event); } /*! * \brief Shows the context menu for the info web view. */ void MainWindow::showInfoWebViewContextMenu(const QPoint &) { QAction copyAction(QIcon::fromTheme(QStringLiteral("edit-copy")), tr("Copy"), nullptr); copyAction.setDisabled(m_webView.selectedText().isEmpty()); connect(©Action, &QAction::triggered, this, &MainWindow::copyInfoWebViewSelection); QMenu menu; menu.addAction(©Action); menu.exec(QCursor::pos()); } /*! * \brief Copies the current selection of the info web view. */ void MainWindow::copyInfoWebViewSelection() { QGuiApplication::clipboard()->setText(m_webView.selectedText()); } }