Spreadsheet: Issue #2301: Handle enter/return and tab keys the same as e.g OpenOffice and Excel.

This commit is contained in:
Eivind Kvedalen
2015-11-08 11:15:20 +01:00
committed by wmayer
parent 8345044530
commit 899956cd51
9 changed files with 192 additions and 12 deletions

View File

@@ -22,16 +22,17 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <QKeyEvent>
# include <QAction>
#endif
#include "SheetTableView.h"
#include <QKeyEvent>
#include <QAction>
#include <Gui/Command.h>
#include "PropertiesDialog.h"
#include <boost/bind.hpp>
#include "../App/Utils.h"
#include "../App/Range.h"
#include <boost/bind.hpp>
#include "SheetTableView.h"
#include "LineEdit.h"
#include "PropertiesDialog.h"
using namespace SpreadsheetGui;
using namespace Spreadsheet;
@@ -70,6 +71,7 @@ SheetTableView::SheetTableView(QWidget *parent)
addAction(cellProperties);
setContextMenuPolicy(Qt::ActionsContextMenu);
setTabKeyNavigation(false);
connect(cellProperties, SIGNAL(triggered()), this, SLOT(cellProperties()));
}
@@ -290,6 +292,53 @@ bool SheetTableView::edit ( const QModelIndex & index, EditTrigger trigger, QEve
return QTableView::edit(index, trigger, event);
}
bool SheetTableView::event(QEvent *event)
{
/* Catch key presses for navigating the table; Enter/Return (+Shift), and Tab (+Shift) */
if (event && event->type() == QEvent::KeyPress) {
QKeyEvent * kevent = static_cast<QKeyEvent*>(event);
if (kevent->key() == Qt::Key_Tab) {
QModelIndex c = currentIndex();
if (kevent->modifiers() == 0) {
setCurrentIndex(model()->index(c.row(), qMin(c.column() + 1, model()->columnCount() -1)));
return true;
}
}
else if (kevent->key() == Qt::Key_Backtab) {
QModelIndex c = currentIndex();
if (kevent->modifiers() == Qt::ShiftModifier) {
setCurrentIndex(model()->index(c.row(), qMax(c.column() - 1, 0)));
return true;
}
}
else if (kevent->key() == Qt::Key_Enter || kevent->key() == Qt::Key_Return) {
QModelIndex c = currentIndex();
if (kevent->modifiers() == 0) {
setCurrentIndex(model()->index(qMin(c.row() + 1, model()->rowCount() - 1), c.column()));
return true;
}
else if (kevent->modifiers() == Qt::ShiftModifier) {
setCurrentIndex(model()->index(qMax(c.row() - 1, 0), c.column()));
return true;
}
}
}
return QTableView::event(event);
}
void SheetTableView::closeEditor(QWidget * editor, QAbstractItemDelegate::EndEditHint hint)
{
SpreadsheetGui::LineEdit * le = qobject_cast<SpreadsheetGui::LineEdit*>(editor);
currentEditIndex = QModelIndex();
QTableView::closeEditor(editor, hint);
setCurrentIndex(le->next());
}
void SheetTableView::edit ( const QModelIndex & index )
{
currentEditIndex = index;