repoindex/gui/mainwindow.cpp

116 lines
4.0 KiB
C++

#include "./mainwindow.h"
#include "./webpage.h"
#include "../lib/alpm/manager.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, Manager *manager) :
m_manager(manager),
m_contextMenu(nullptr)
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QStringLiteral(PROJECT_NAME));
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:
;
}
break;
} 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, QStringLiteral(PROJECT_NAME));
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 &)
{
if(!m_contextMenu) {
m_contextMenu = new QMenu(this);
auto *reloadAction = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")), tr("Reload UI"), m_contextMenu);
connect(reloadAction, &QAction::triggered, &m_webView, &REPOINDEX_WEB_VIEW::reload);
m_copyAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-copy")), tr("Copy"), m_contextMenu);
connect(m_copyAction, &QAction::triggered, this, &MainWindow::copyInfoWebViewSelection);
auto *updateAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-download")), tr("Update sync-databases"), m_contextMenu);
connect(updateAction, &QAction::triggered, m_manager, &Manager::forceUpdateAlpmDatabases);
auto *wipeCacheAction = new QAction(QIcon::fromTheme(QStringLiteral("document-revert")), tr("Wipe cached packages"), m_contextMenu);
connect(wipeCacheAction, &QAction::triggered, m_manager, &Manager::wipeCache);
m_contextMenu->addAction(m_copyAction);
m_contextMenu->addAction(reloadAction);
m_contextMenu->addSeparator();
m_contextMenu->addAction(updateAction);
m_contextMenu->addAction(wipeCacheAction);
}
m_copyAction->setDisabled(m_webView.selectedText().isEmpty());
m_contextMenu->exec(QCursor::pos());
}
/*!
* \brief Copies the current selection of the info web view.
*/
void MainWindow::copyInfoWebViewSelection()
{
QGuiApplication::clipboard()->setText(m_webView.selectedText());
}
}