repoindex/gui/mainwindow.cpp

99 lines
3.0 KiB
C++

#include "./mainwindow.h"
#include "./webpage.h"
#include "resources/config.h"
#include <QGuiApplication>
#include <QClipboard>
#include <QSettings>
#include <QStringBuilder>
#include <QAction>
#include <QMenu>
#include <QKeyEvent>
#include <QMouseEvent>
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<QKeyEvent *>(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<QMouseEvent *>(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(&copyAction, &QAction::triggered, this, &MainWindow::copyInfoWebViewSelection);
QMenu menu;
menu.addAction(&copyAction);
menu.exec(QCursor::pos());
}
/*!
* \brief Copies the current selection of the info web view.
*/
void MainWindow::copyInfoWebViewSelection()
{
QGuiApplication::clipboard()->setText(m_webView.selectedText());
}
}