From 9fb13d345d5a6ca22ccbe4a2c65406c7db580e72 Mon Sep 17 00:00:00 2001 From: manongjohn Date: Mon, 15 Feb 2021 16:14:11 -0500 Subject: [PATCH] Change shortcut usage and navigation while renaming cell --- toonz/sources/toonz/xshcellviewer.cpp | 88 +++++++++++++++++++++------ toonz/sources/toonz/xshcellviewer.h | 10 +++ 2 files changed, 79 insertions(+), 19 deletions(-) diff --git a/toonz/sources/toonz/xshcellviewer.cpp b/toonz/sources/toonz/xshcellviewer.cpp index 834f6579..711fdbbf 100644 --- a/toonz/sources/toonz/xshcellviewer.cpp +++ b/toonz/sources/toonz/xshcellviewer.cpp @@ -74,6 +74,7 @@ #include #include #include +#include namespace { @@ -556,7 +557,11 @@ namespace XsheetGUI { //----------------------------------------------------------------------------- RenameCellField::RenameCellField(QWidget *parent, XsheetViewer *viewer) - : QLineEdit(parent), m_viewer(viewer), m_row(-1), m_col(-1) { + : QLineEdit(parent) + , m_viewer(viewer) + , m_row(-1) + , m_col(-1) + , m_isRenamingCell(false) { connect(this, SIGNAL(returnPressed()), SLOT(onReturnPressed())); setContextMenuPolicy(Qt::PreventContextMenu); setObjectName("RenameCellField"); @@ -882,24 +887,39 @@ void RenameCellField::renameCell() { //----------------------------------------------------------------------------- -void RenameCellField::onReturnPressed() { - renameCell(); - +void RenameCellField::moveCellSelection(int direction) { // move the cell selection TCellSelection *cellSelection = dynamic_cast( TApp::instance()->getCurrentSelection()->getSelection()); if (!cellSelection) return; TCellSelection::Range range = cellSelection->getSelectedCells(); - int offset = range.m_r1 - range.m_r0 + 1; + int offset = range.m_r1 - range.m_r0 + direction; + if ((m_row + offset) < 0) return; cellSelection->selectCells(range.m_r0 + offset, range.m_c0, range.m_r1 + offset, range.m_c1); showInRowCol(m_row + offset, m_col, range.getColCount() > 1); m_viewer->updateCells(); + m_viewer->setCurrentRow(m_row); TApp::instance()->getCurrentSelection()->notifySelectionChanged(); } //----------------------------------------------------------------------------- +void RenameCellField::onReturnPressed() { + renameCell(); + moveCellSelection(1); +} + +//----------------------------------------------------------------------------- + +void RenameCellField::onTabPressed() { moveCellSelection(1); } + +//----------------------------------------------------------------------------- + +void RenameCellField::onBacktabPressed() { moveCellSelection(-1); } + +//----------------------------------------------------------------------------- + void RenameCellField::focusOutEvent(QFocusEvent *e) { hide(); @@ -910,9 +930,34 @@ void RenameCellField::focusOutEvent(QFocusEvent *e) { // Override shortcut keys for cell selection commands bool RenameCellField::eventFilter(QObject *obj, QEvent *e) { + if (e->type() == QEvent::KeyPress) { + QKeyEvent *keyEvent = static_cast(e); + int key = keyEvent->key(); + switch (key) { + case Qt::Key_Tab: + onTabPressed(); + return true; + break; + case Qt::Key_Backtab: + onBacktabPressed(); + return true; + break; + } + } + if (e->type() != QEvent::ShortcutOverride) return QLineEdit::eventFilter(obj, e); // return false; + // If we aren't allowing any shortcuts while renaming, use default editing + // commands. + if (!Preferences::instance()->isShortcutCommandsWhileRenamingCellEnabled()) + return QLineEdit::eventFilter(obj, e); + + // No shortcuts in Note Levels + TXshColumn *col = m_viewer->getXsheet()->getColumn(m_col); + bool noShortcuts = (col && col->getSoundTextColumn()) ? true : false; + if (noShortcuts) return QLineEdit::eventFilter(obj, e); + TCellSelection *cellSelection = dynamic_cast( TApp::instance()->getCurrentSelection()->getSelection()); if (!cellSelection) return QLineEdit::eventFilter(obj, e); @@ -923,19 +968,13 @@ bool RenameCellField::eventFilter(QObject *obj, QEvent *e) { QAction *action = CommandManager::instance()->getActionFromShortcut(keyStr); if (!action) return QLineEdit::eventFilter(obj, e); - std::string actionId = CommandManager::instance()->getIdFromAction(action); - // These are usally standard ctrl/command strokes for text editing. // Default to standard behavior and don't execute OT's action while renaming // cell if users prefer to do so. - // Or, always invoke OT's commands when renaming cell even the standard - // command strokes for text editing. - // The latter option is demanded by Japanese animation industry in order to - // gain efficiency for inputting xsheet. - if (!Preferences::instance()->isShortcutCommandsWhileRenamingCellEnabled() && - (actionId == "MI_Undo" || actionId == "MI_Redo" || - actionId == "MI_Clear" || actionId == "MI_Copy" || - actionId == "MI_Paste" || actionId == "MI_Cut")) + std::string actionId = CommandManager::instance()->getIdFromAction(action); + if (actionId == "MI_Undo" || actionId == "MI_Redo" || + actionId == "MI_Clear" || actionId == "MI_Copy" || + actionId == "MI_Paste" || actionId == "MI_Cut") return QLineEdit::eventFilter(obj, e); return TCellSelection::isEnabledCommand(actionId); @@ -946,6 +985,7 @@ bool RenameCellField::eventFilter(QObject *obj, QEvent *e) { void RenameCellField::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Escape) { clearFocus(); + m_viewer->setFocus(); return; } @@ -963,7 +1003,8 @@ void RenameCellField::keyPressEvent(QKeyEvent *event) { stride.setFrame(cellSelection->getSelectedCells().getRowCount()); CellPosition offset; - switch (int key = event->key()) { + int key = event->key(); + switch (key) { case Qt::Key_Up: case Qt::Key_Down: offset = m_viewer->orientation()->arrowShift(key); @@ -971,10 +1012,16 @@ void RenameCellField::keyPressEvent(QKeyEvent *event) { case Qt::Key_Left: case Qt::Key_Right: // ctrl+left/right arrow for moving cursor to the end in the field - if (isCtrlPressed && + if (!isCtrlPressed || !Preferences::instance()->isUseArrowKeyToShiftCellSelectionEnabled()) { - QLineEdit::keyPressEvent(event); - return; + // Allow left/right movement inside field. If you go too far, you will + // shift cells + int curPos = cursorPosition(); + if ((key == Qt::Key_Left && curPos > 0) || + (key == Qt::Key_Right && curPos < text().size())) { + QLineEdit::keyPressEvent(event); + return; + } } offset = m_viewer->orientation()->arrowShift(key); break; @@ -1017,6 +1064,8 @@ void RenameCellField::showEvent(QShowEvent *) { bool ret = connect(TApp::instance()->getCurrentXsheet(), SIGNAL(xsheetChanged()), this, SLOT(onXsheetChanged())); assert(ret); + + m_isRenamingCell = true; } //----------------------------------------------------------------------------- @@ -1024,6 +1073,7 @@ void RenameCellField::showEvent(QShowEvent *) { void RenameCellField::hideEvent(QHideEvent *) { disconnect(TApp::instance()->getCurrentXsheet(), SIGNAL(xsheetChanged()), this, SLOT(onXsheetChanged())); + m_isRenamingCell = false; } //----------------------------------------------------------------------------- diff --git a/toonz/sources/toonz/xshcellviewer.h b/toonz/sources/toonz/xshcellviewer.h index 43a219a7..0090e7a8 100644 --- a/toonz/sources/toonz/xshcellviewer.h +++ b/toonz/sources/toonz/xshcellviewer.h @@ -26,6 +26,7 @@ class RenameCellField final : public QLineEdit { int m_row; int m_col; XsheetViewer *m_viewer; + bool m_isRenamingCell; public: RenameCellField(QWidget *parent, XsheetViewer *viewer); @@ -35,6 +36,8 @@ public: bool isLocatedAt(int row, int col) { return row == m_row && col == m_col; } + bool isRenamingCell() { return m_isRenamingCell; } + protected: void focusOutEvent(QFocusEvent *) override; void keyPressEvent(QKeyEvent *event) override; @@ -46,6 +49,10 @@ protected: void renameCell(); void renameSoundTextColumn(TXshSoundTextColumn *sndTextCol, const QString &s); + void moveCellSelection(int direction); + void onTabPressed(); + void onBacktabPressed(); + protected slots: void onReturnPressed(); void onXsheetChanged(); @@ -134,6 +141,9 @@ public: m_renameCell->showInRowCol(row, col, multiColumnSelected); } void hideRenameField() { m_renameCell->hide(); } + bool isRenamingCell() { + return m_renameCell && m_renameCell->isRenamingCell(); + } protected: void paintEvent(QPaintEvent *) override;