Customizing of action shortcuts

This commit is contained in:
Jakub Melka 2019-12-16 19:46:29 +01:00
parent 7dbae1c3dc
commit eefa687e3d
7 changed files with 310 additions and 15 deletions

View File

@ -23,5 +23,6 @@
<file>resources/zoom-fit-vertical.svg</file>
<file>resources/synchronize.svg</file>
<file>resources/cache.svg</file>
<file>resources/shortcuts.svg</file>
</qresource>
</RCC>

View File

@ -87,11 +87,12 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
connect(ui->actionClose, &QAction::triggered, this, &PDFViewerMainWindow::onActionCloseTriggered);
connect(ui->actionQuit, &QAction::triggered, this, &PDFViewerMainWindow::onActionQuitTriggered);
auto createGoToAction = [this](QMenu* menu, QString text, QKeySequence::StandardKey key, pdf::PDFDrawWidgetProxy::Operation operation, QString iconPath)
auto createGoToAction = [this](QMenu* menu, QString name, QString text, QKeySequence::StandardKey key, pdf::PDFDrawWidgetProxy::Operation operation, QString iconPath)
{
QIcon icon;
icon.addFile(iconPath);
QAction* action = new QAction(icon, text, this);
action->setObjectName(name);
action->setShortcut(key);
menu->addAction(action);
@ -103,12 +104,12 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
return action;
};
QAction* actionGoToDocumentStart = createGoToAction(ui->menuGoTo, tr("Go to document start"), QKeySequence::MoveToStartOfDocument, pdf::PDFDrawWidgetProxy::NavigateDocumentStart, ":/resources/previous-start.svg");
QAction* actionGoToDocumentEnd = createGoToAction(ui->menuGoTo, tr("Go to document end"), QKeySequence::MoveToEndOfDocument, pdf::PDFDrawWidgetProxy::NavigateDocumentEnd, ":/resources/next-end.svg");
QAction* actionGoToNextPage = createGoToAction(ui->menuGoTo, tr("Go to next page"), QKeySequence::MoveToNextPage, pdf::PDFDrawWidgetProxy::NavigateNextPage, ":/resources/next-page.svg");
QAction* actionGoToPreviousPage = createGoToAction(ui->menuGoTo, tr("Go to previous page"), QKeySequence::MoveToPreviousPage, pdf::PDFDrawWidgetProxy::NavigatePreviousPage, ":/resources/previous-page.svg");
createGoToAction(ui->menuGoTo, tr("Go to next line"), QKeySequence::MoveToNextLine, pdf::PDFDrawWidgetProxy::NavigateNextStep, ":/resources/next.svg");
createGoToAction(ui->menuGoTo, tr("Go to previous line"), QKeySequence::MoveToPreviousLine, pdf::PDFDrawWidgetProxy::NavigatePreviousStep, ":/resources/previous.svg");
QAction* actionGoToDocumentStart = createGoToAction(ui->menuGoTo, "actionGoToDocumentStart", tr("Go to document start"), QKeySequence::MoveToStartOfDocument, pdf::PDFDrawWidgetProxy::NavigateDocumentStart, ":/resources/previous-start.svg");
QAction* actionGoToDocumentEnd = createGoToAction(ui->menuGoTo, "actionGoToDocumentEnd", tr("Go to document end"), QKeySequence::MoveToEndOfDocument, pdf::PDFDrawWidgetProxy::NavigateDocumentEnd, ":/resources/next-end.svg");
QAction* actionGoToNextPage = createGoToAction(ui->menuGoTo, "actionGoToNextPage", tr("Go to next page"), QKeySequence::MoveToNextPage, pdf::PDFDrawWidgetProxy::NavigateNextPage, ":/resources/next-page.svg");
QAction* actionGoToPreviousPage = createGoToAction(ui->menuGoTo, "actionGoToPreviousPage", tr("Go to previous page"), QKeySequence::MoveToPreviousPage, pdf::PDFDrawWidgetProxy::NavigatePreviousPage, ":/resources/previous-page.svg");
createGoToAction(ui->menuGoTo, "actionGoToNextLine", tr("Go to next line"), QKeySequence::MoveToNextLine, pdf::PDFDrawWidgetProxy::NavigateNextStep, ":/resources/next.svg");
createGoToAction(ui->menuGoTo, "actionGoToPreviousLine", tr("Go to previous line"), QKeySequence::MoveToPreviousLine, pdf::PDFDrawWidgetProxy::NavigatePreviousStep, ":/resources/previous.svg");
m_pageNumberSpinBox = new QSpinBox(this);
m_pageNumberLabel = new QLabel(this);
@ -176,6 +177,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
ui->menuView->addSeparator();
ui->menuView->addAction(m_sidebarDockWidget->toggleViewAction());
m_sidebarDockWidget->toggleViewAction()->setObjectName("actionSidebar");
connect(m_pdfWidget->getDrawWidgetProxy(), &pdf::PDFDrawWidgetProxy::drawSpaceChanged, this, &PDFViewerMainWindow::onDrawSpaceChanged);
connect(m_pdfWidget->getDrawWidgetProxy(), &pdf::PDFDrawWidgetProxy::pageLayoutChanged, this, &PDFViewerMainWindow::onPageLayoutChanged);
@ -185,6 +187,7 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget *parent) :
connect(m_progress, &pdf::PDFProgress::progressStep, this, &PDFViewerMainWindow::onProgressStep);
connect(m_progress, &pdf::PDFProgress::progressFinished, this, &PDFViewerMainWindow::onProgressFinished);
readActionSettings();
updatePageLayoutActions();
updateUI(true);
onViewerSettingsChanged();
@ -536,6 +539,25 @@ void PDFViewerMainWindow::readSettings()
}
m_settings->readSettings(settings);
}
void PDFViewerMainWindow::readActionSettings()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
// Load action shortcuts
settings.beginGroup("Actions");
for (QAction* action : getActions())
{
QString name = action->objectName();
if (!name.isEmpty() && settings.contains(name))
{
QKeySequence sequence = QKeySequence::fromString(settings.value(name, action->shortcut().toString(QKeySequence::PortableText)).toString(), QKeySequence::PortableText);
action->setShortcut(sequence);
}
}
settings.endGroup();
}
void PDFViewerMainWindow::writeSettings()
@ -545,6 +567,19 @@ void PDFViewerMainWindow::writeSettings()
settings.setValue("windowState", saveState());
m_settings->writeSettings(settings);
// Save action shortcuts
settings.beginGroup("Actions");
for (QAction* action : getActions())
{
QString name = action->objectName();
if (!name.isEmpty())
{
QString accelerator = action->shortcut().toString(QKeySequence::PortableText);
settings.setValue(name, accelerator);
}
}
settings.endGroup();
}
void PDFViewerMainWindow::updateTitle()
@ -773,6 +808,11 @@ std::vector<QAction*> PDFViewerMainWindow::getRenderingOptionActions() const
return { ui->actionRenderOptionAntialiasing, ui->actionRenderOptionTextAntialiasing, ui->actionRenderOptionSmoothPictures, ui->actionRenderOptionIgnoreOptionalContentSettings };
}
QList<QAction*> PDFViewerMainWindow::getActions() const
{
return findChildren<QAction*>(QString(), Qt::FindChildrenRecursively);
}
int PDFViewerMainWindow::adjustDpiX(int value)
{
const int physicalDpiX = this->physicalDpiX();
@ -859,7 +899,7 @@ void PDFViewerMainWindow::on_actionRendering_Errors_triggered()
void PDFViewerMainWindow::on_actionOptions_triggered()
{
PDFViewerSettingsDialog dialog(m_settings->getSettings(), this);
PDFViewerSettingsDialog dialog(m_settings->getSettings(), getActions(), this);
if (dialog.exec() == QDialog::Accepted)
{
m_settings->setSettings(dialog.getSettings());

View File

@ -71,13 +71,9 @@ private slots:
void on_actionRendering_Errors_triggered();
void on_actionOptions_triggered();
void on_actionAbout_triggered();
void on_actionFitPage_triggered();
void on_actionFitWidth_triggered();
void on_actionFitHeight_triggered();
private:
@ -96,6 +92,7 @@ private:
void onProgressFinished();
void readSettings();
void readActionSettings();
void writeSettings();
void updateTitle();
@ -113,6 +110,7 @@ private:
void setPageLayout(pdf::PageLayout pageLayout);
std::vector<QAction*> getRenderingOptionActions() const;
QList<QAction*> getActions() const;
int adjustDpiX(int value);

View File

@ -21,16 +21,19 @@
#include "pdfglobal.h"
#include "pdfutils.h"
#include <QAction>
#include <QLineEdit>
#include <QMessageBox>
#include <QListWidgetItem>
namespace pdfviewer
{
PDFViewerSettingsDialog::PDFViewerSettingsDialog(const PDFViewerSettings::Settings& settings, QWidget *parent) :
PDFViewerSettingsDialog::PDFViewerSettingsDialog(const PDFViewerSettings::Settings& settings, QList<QAction*> actions, QWidget *parent) :
QDialog(parent),
ui(new Ui::PDFViewerSettingsDialog),
m_settings(settings),
m_actions(),
m_isLoadingData(false)
{
ui->setupUi(this);
@ -39,6 +42,7 @@ PDFViewerSettingsDialog::PDFViewerSettingsDialog(const PDFViewerSettings::Settin
new QListWidgetItem(QIcon(":/resources/rendering.svg"), tr("Rendering"), ui->optionsPagesWidget, RenderingSettings);
new QListWidgetItem(QIcon(":/resources/shading.svg"), tr("Shading"), ui->optionsPagesWidget, ShadingSettings);
new QListWidgetItem(QIcon(":/resources/cache.svg"), tr("Cache"), ui->optionsPagesWidget, CacheSettings);
new QListWidgetItem(QIcon(":/resources/shortcuts.svg"), tr("Shortcuts"), ui->optionsPagesWidget, ShortcutSettings);
new QListWidgetItem(QIcon(":/resources/security.svg"), tr("Security"), ui->optionsPagesWidget, SecuritySettings);
ui->renderingEngineComboBox->addItem(tr("Software"), static_cast<int>(pdf::RendererEngine::Software));
@ -71,9 +75,18 @@ PDFViewerSettingsDialog::PDFViewerSettingsDialog(const PDFViewerSettings::Settin
connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &PDFViewerSettingsDialog::saveData);
}
for (QAction* action : actions)
{
if (!action->objectName().isEmpty())
{
m_actions.append(action);
}
}
ui->optionsPagesWidget->setCurrentRow(0);
adjustSize();
loadData();
loadActionShortcutsTable();
}
PDFViewerSettingsDialog::~PDFViewerSettingsDialog()
@ -103,6 +116,10 @@ void PDFViewerSettingsDialog::on_optionsPagesWidget_currentItemChanged(QListWidg
ui->stackedWidget->setCurrentWidget(ui->cachePage);
break;
case ShortcutSettings:
ui->stackedWidget->setCurrentWidget(ui->shortcutsPage);
break;
case SecuritySettings:
ui->stackedWidget->setCurrentWidget(ui->securityPage);
break;
@ -257,4 +274,64 @@ void PDFViewerSettingsDialog::saveData()
loadData();
}
void PDFViewerSettingsDialog::loadActionShortcutsTable()
{
ui->shortcutsTableWidget->setRowCount(m_actions.size());
ui->shortcutsTableWidget->setColumnCount(2);
ui->shortcutsTableWidget->setHorizontalHeaderLabels({ tr("Action"), tr("Shortcut")});
ui->shortcutsTableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
for (int i = 0; i < m_actions.size(); ++i)
{
QAction* action = m_actions[i];
// Action name and icon
QTableWidgetItem* actionItem = new QTableWidgetItem(action->icon(), action->text());
actionItem->setFlags(Qt::ItemIsEnabled);
ui->shortcutsTableWidget->setItem(i, 0, actionItem);
// Action shortcut
QTableWidgetItem* shortcutItem = new QTableWidgetItem(action->shortcut().toString(QKeySequence::NativeText));
ui->shortcutsTableWidget->setItem(i, 1, shortcutItem);
}
}
bool PDFViewerSettingsDialog::saveActionShortcutsTable()
{
// Jakub Melka: we need validation here
for (int i = 0; i < m_actions.size(); ++i)
{
QString shortcut = ui->shortcutsTableWidget->item(i, 1)->data(Qt::DisplayRole).toString();
if (!shortcut.isEmpty())
{
QKeySequence sequence = QKeySequence::fromString(shortcut, QKeySequence::NativeText);
if (sequence.toString(QKeySequence::PortableText).isEmpty())
{
QMessageBox::critical(this, tr("Error"), tr("Shortcut '%1' is invalid for action %2.").arg(shortcut, m_actions[i]->text()));
return false;
}
}
}
for (int i = 0; i < m_actions.size(); ++i)
{
QAction* action = m_actions[i];
// Set shortcut to the action
QString shortcut = ui->shortcutsTableWidget->item(i, 1)->data(Qt::DisplayRole).toString();
QKeySequence sequence = QKeySequence::fromString(shortcut, QKeySequence::NativeText);
action->setShortcut(sequence);
}
return true;
}
void PDFViewerSettingsDialog::accept()
{
if (saveActionShortcutsTable())
{
QDialog::accept();
}
}
} // namespace pdfviewer

View File

@ -37,15 +37,18 @@ class PDFViewerSettingsDialog : public QDialog
Q_OBJECT
public:
explicit PDFViewerSettingsDialog(const PDFViewerSettings::Settings& settings, QWidget* parent);
explicit PDFViewerSettingsDialog(const PDFViewerSettings::Settings& settings, QList<QAction*> actions, QWidget* parent);
virtual ~PDFViewerSettingsDialog() override;
virtual void accept() override;
enum Page : int
{
EngineSettings,
RenderingSettings,
ShadingSettings,
CacheSettings,
ShortcutSettings,
SecuritySettings
};
@ -58,8 +61,12 @@ private:
void loadData();
void saveData();
void loadActionShortcutsTable();
bool saveActionShortcutsTable();
Ui::PDFViewerSettingsDialog* ui;
PDFViewerSettings::Settings m_settings;
QList<QAction*> m_actions;
bool m_isLoadingData;
};

View File

@ -26,7 +26,7 @@
<item>
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>3</number>
<number>4</number>
</property>
<widget class="QWidget" name="enginePage">
<layout class="QVBoxLayout" name="enginePageLayout">
@ -532,6 +532,38 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="shortcutsPage">
<layout class="QVBoxLayout" name="shortcutsPageLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Shortcuts</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QTableWidget" name="shortcutsTableWidget">
<property name="editTriggers">
<set>QAbstractItemView::AllEditTriggers</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="securityPage">
<layout class="QVBoxLayout" name="securityPageLayout">
<property name="leftMargin">

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 23 KiB