Skip to content

Commit

Permalink
[gui] Fix issues with keyboard shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jun 26, 2024
1 parent 9559cdb commit ffaccc7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
19 changes: 19 additions & 0 deletions src/lib/score/actions/ActionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "ActionManager.hpp"

#include <score/plugins/documentdelegate/DocumentDelegateView.hpp>
#include <score/tools/Bind.hpp>

#include <core/document/Document.hpp>
#include <core/document/DocumentView.hpp>

namespace score
{
Expand Down Expand Up @@ -62,6 +64,23 @@ void ActionManager::reset(score::Document* doc)
}

// Reset all the actions

if(doc && doc->view())
{
if(auto widg = doc->view()->viewDelegate().getWidget())

for(auto& cond : m_container)
{
QAction* act = cond.second.action();
if(act->objectName().isEmpty())
act->setObjectName(act->text());
if(act->shortcutContext() == Qt::WidgetWithChildrenShortcut
|| act->shortcutContext() == Qt::WidgetShortcut)
{
widg->addAction(cond.second.action());
}
}
}
documentChanged(mdoc);
focusChanged(mdoc);
selectionChanged(mdoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,9 @@ Qt::ItemFlags AddressItemModel::flags(const QModelIndex& index) const

Qt::ItemFlags f = QAbstractItemModel::flags(index);
static const constexpr std::array<Qt::ItemFlags, Rows::Count> flags{{
{} // name
{Qt::ItemIsEnabled | Qt::ItemIsSelectable} // name
,
{} // address
{Qt::ItemIsEnabled | Qt::ItemIsSelectable} // address
,
{Qt::ItemIsEditable} // value
,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

#include <QAbstractProxyModel>
#include <QAction>
#include <QClipboard>
#include <QComboBox>
#include <QContextMenuEvent>
#include <QDialog>
Expand All @@ -73,6 +74,7 @@
#include <QMenu>
#include <QPair>
#include <QRegularExpression>
#include <QShortcut>
#include <QSize>
#include <QStackedLayout>
#include <QString>
Expand Down Expand Up @@ -241,6 +243,14 @@ void DeviceExplorerWidget::buildGUI()

m_addressModel = new AddressItemModel{this};
m_addressView = new QTableView{this};
{
auto qs = new QShortcut(QKeySequence::Copy, m_addressView, m_addressView, [this]() {
auto idx = m_addressView->currentIndex();
auto data = m_addressView->model()->data(idx);
qApp->clipboard()->setText(data.toString());
}, [] {});
qs->setContext(Qt::WidgetShortcut);
}
auto delegate = new AddressItemDelegate{m_addressView};
m_addressView->setItemDelegate(delegate);
m_addressView->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <Scenario/Document/BaseScenario/BaseScenario.hpp>
#include <Scenario/Document/ScenarioDocument/ScenarioDocumentModel.hpp>
#include <Scenario/Document/ScenarioDocument/ScenarioDocumentPresenter.hpp>
#include <Scenario/Document/ScenarioDocument/ScenarioDocumentView.hpp>
#include <Scenario/Document/ScenarioEditor.hpp>
#include <Scenario/Palette/ScenarioPoint.hpp>
#include <Scenario/Process/Algorithms/ContainersAccessors.hpp>
Expand All @@ -46,6 +47,8 @@

#include <core/application/ApplicationSettings.hpp>
#include <core/document/Document.hpp>
#include <core/document/DocumentView.hpp>
#include <core/presenter/DocumentManager.hpp>

#include <QAction>
#include <QClipboard>
Expand Down Expand Up @@ -78,8 +81,11 @@ ObjectMenuActions::ObjectMenuActions(ScenarioApplicationPlugin* parent)
// COPY/CUT
m_copyContent = new QAction{tr("Copy"), this};
m_copyContent->setShortcut(QKeySequence::Copy);
m_copyContent->setShortcutContext(Qt::ApplicationShortcut);
m_copyContent->setShortcutContext(Qt::WidgetWithChildrenShortcut);
connect(m_copyContent, &QAction::triggered, this, [this]() {
if(!isFocusingScenario())
return;

JSONReader r;
copySelectedElementsToJson(r);
if(r.empty())
Expand All @@ -91,8 +97,11 @@ ObjectMenuActions::ObjectMenuActions(ScenarioApplicationPlugin* parent)

m_cutContent = new QAction{tr("Cut"), this};
m_cutContent->setShortcut(QKeySequence::Cut);
m_cutContent->setShortcutContext(Qt::ApplicationShortcut);
m_cutContent->setShortcutContext(Qt::WidgetWithChildrenShortcut);
connect(m_cutContent, &QAction::triggered, this, [this] {
if(!isFocusingScenario())
return;

JSONReader r;
cutSelectedElementsToJson(r);
if(r.empty())
Expand All @@ -111,6 +120,9 @@ ObjectMenuActions::ObjectMenuActions(ScenarioApplicationPlugin* parent)

m_pasteElementsAfter = new QAction{tr("Paste (after)"), this};
connect(m_pasteElementsAfter, &QAction::triggered, [this] {
if(!isFocusingScenario())
return;

auto pres = m_parent->focusedPresenter();
if(!pres)
return;
Expand Down Expand Up @@ -192,28 +204,33 @@ ObjectMenuActions::ObjectMenuActions(ScenarioApplicationPlugin* parent)

// Selection actions
m_selectAll = new QAction{tr("Select all"), this};
m_selectAll->setToolTip("Ctrl+A");
connect(m_selectAll, &QAction::triggered, [this]() {
if(!isFocusingScenario())
return;
if(auto pres = getScenarioDocPresenter())
pres->selectAll();
});

m_deselectAll = new QAction{tr("Deselect all"), this};
m_deselectAll->setToolTip("Ctrl+Shift+A");
connect(m_deselectAll, &QAction::triggered, [this]() {
if(!isFocusingScenario())
return;
if(auto pres = getScenarioDocPresenter())
pres->deselectAll();
});

m_selectTop = new QAction{this};
connect(m_selectTop, &QAction::triggered, [this] {
if(!isFocusingScenario())
return;
if(auto pres = getScenarioDocPresenter())
pres->selectTop();
});

m_goToParent = new QAction{this};
m_goToParent->setToolTip("Ctrl+Shift+Up");
connect(m_goToParent, &QAction::triggered, [this]() {
if(!isFocusingScenario())
return;
if(auto pres = getScenarioDocPresenter())
{
auto* cur = (QObject*)&pres->displayedInterval();
Expand All @@ -227,16 +244,6 @@ ObjectMenuActions::ObjectMenuActions(ScenarioApplicationPlugin* parent)
}
}
});

if(parent->context.mainWindow)
{
auto doc
= parent->context.mainWindow->centralWidget()->findChild<QWidget*>("Documents");
SCORE_ASSERT(doc);
doc->addAction(m_removeElements);
doc->addAction(m_pasteElements);
doc->addAction(m_goToParent);
}
}

void ObjectMenuActions::makeGUIElements(score::GUIElements& e)
Expand Down Expand Up @@ -458,6 +465,8 @@ void ObjectMenuActions::pasteElements(QPoint pos)

void ObjectMenuActions::pasteElements()
{
if(!isFocusingScenario())
return;
pasteElements(QCursor::pos());
}

Expand Down Expand Up @@ -487,6 +496,26 @@ void ObjectMenuActions::pasteElementsAfter(const rapidjson::Value& obj)
}
}

bool ObjectMenuActions::isFocusingScenario() const noexcept
{
auto doc = m_parent->context.documents.currentDocument();
if(!doc)
return false;

auto widg = qApp->focusWidget();
if(!widg)
return false;

auto delegate = qobject_cast<ScenarioDocumentView*>(&doc->view()->viewDelegate());
if(!delegate)
return false;

if(widg != &delegate->view())
return false;

return true;
}

ScenarioDocumentModel* ObjectMenuActions::getScenarioDocModel() const
{
if(auto doc = m_parent->currentDocument())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ScenarioPresenter;
class SCORE_PLUGIN_SCENARIO_EXPORT ObjectMenuActions final : public QObject
{
public:
ObjectMenuActions(ScenarioApplicationPlugin* parent);
explicit ObjectMenuActions(ScenarioApplicationPlugin* parent);

void makeGUIElements(score::GUIElements& ref);
void setupContextMenu(Process::LayerContextMenuManager& ctxm);
Expand All @@ -39,6 +39,8 @@ class SCORE_PLUGIN_SCENARIO_EXPORT ObjectMenuActions final : public QObject
void pasteElementsAfter(const rapidjson::Value& obj);
void writeJsonToSelectedElements(const rapidjson::Value& obj);

bool isFocusingScenario() const noexcept;

ScenarioDocumentModel* getScenarioDocModel() const;
ScenarioDocumentPresenter* getScenarioDocPresenter() const;
ScenarioApplicationPlugin* m_parent{};
Expand Down

0 comments on commit ffaccc7

Please sign in to comment.