Enable use of keyboard to modify screens

This commit is contained in:
albertony 2021-01-21 16:42:30 +01:00
parent d4382b9b80
commit 4ee6a54d41
4 changed files with 78 additions and 7 deletions

View File

@ -0,0 +1 @@
Made it possible to use keyboard instead of mouse to modify screen layout.

View File

@ -71,7 +71,7 @@ Qt::ItemFlags ScreenSetupModel::flags(const QModelIndex& index) const
if (!screen(index).isNull()) if (!screen(index).isNull())
return Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled; return Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
return Qt::ItemIsDropEnabled; return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
} }
Qt::DropActions ScreenSetupModel::supportedDropActions() const Qt::DropActions ScreenSetupModel::supportedDropActions() const

View File

@ -30,6 +30,7 @@ ScreenSetupView::ScreenSetupView(QWidget* parent) :
setDropIndicatorShown(true); setDropIndicatorShown(true);
setDragDropMode(DragDrop); setDragDropMode(DragDrop);
setSelectionMode(SingleSelection); setSelectionMode(SingleSelection);
setTabKeyNavigation(false);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -65,18 +66,83 @@ void ScreenSetupView::resizeEvent(QResizeEvent* event)
event->ignore(); event->ignore();
} }
void ScreenSetupView::enter(const QModelIndex& index)
{
if (!index.isValid())
return;
Screen& screen = model()->screen(index);
if (screen.isNull())
screen = Screen(tr("Unnamed"));
ScreenSettingsDialog dlg(this, &screen);
dlg.exec();
}
void ScreenSetupView::remove(const QModelIndex& index)
{
if (!index.isValid())
return;
Screen& screen = model()->screen(index);
if (!screen.isNull()) {
screen = Screen();
emit dataChanged(index, index);
}
}
void ScreenSetupView::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Delete)
{
QModelIndexList indexes = selectedIndexes();
if (indexes.count() == 1 && indexes[0].isValid())
{
if (event->key() == Qt::Key_Return)
enter(indexes[0]);
else if (event->key() == Qt::Key_Delete)
remove(indexes[0]);
}
// Do not let base handle the event, at least not for return key because it
// results in next esc/return key in the opened Screen Settings dialog not
// only closing that but also the parent Server Configuration dialog.
}
else if ((event->modifiers() & Qt::ControlModifier)
&& (event->key() == Qt::Key_Left || event->key() == Qt::Key_Right
|| event->key() == Qt::Key_Up || event->key() == Qt::Key_Down))
{
QModelIndexList indexes = selectedIndexes();
if (indexes.count() == 1 && indexes[0].isValid())
{
const QModelIndex& fromIndex = indexes[0];
QModelIndex toIndex;
if (event->key() == Qt::Key_Left)
toIndex = fromIndex.sibling(fromIndex.row(), fromIndex.column() - 1);
else if (event->key() == Qt::Key_Right)
toIndex = fromIndex.sibling(fromIndex.row(), fromIndex.column() + 1);
else if (event->key() == Qt::Key_Up)
toIndex = fromIndex.sibling(fromIndex.row() - 1, fromIndex.column());
else if (event->key() == Qt::Key_Down)
toIndex = fromIndex.sibling(fromIndex.row() + 1, fromIndex.column());
if (toIndex.isValid() && fromIndex != toIndex)
std::swap(model()->screen(fromIndex), model()->screen(toIndex));
}
// In this case let base also handle the event, because it will proceed moving
// the selection to target, update the view according to model changes etc.
QTableView::keyPressEvent(event);
}
else
{
QTableView::keyPressEvent(event);
}
}
void ScreenSetupView::mouseDoubleClickEvent(QMouseEvent* event) void ScreenSetupView::mouseDoubleClickEvent(QMouseEvent* event)
{ {
if (event->buttons() & Qt::LeftButton) if (event->buttons() & Qt::LeftButton)
{ {
int col = columnAt(event->pos().x()); int col = columnAt(event->pos().x());
int row = rowAt(event->pos().y()); int row = rowAt(event->pos().y());
enter(model()->createIndex(row, col));
if (!model()->screen(col, row).isNull())
{
ScreenSettingsDialog dlg(this, &model()->screen(col, row));
dlg.exec();
}
} }
else else
event->ignore(); event->ignore();

View File

@ -44,6 +44,7 @@ class ScreenSetupView : public QTableView
protected: protected:
void mouseDoubleClickEvent(QMouseEvent*) override; void mouseDoubleClickEvent(QMouseEvent*) override;
void keyPressEvent(QKeyEvent*) override;
void setTableSize(); void setTableSize();
void resizeEvent(QResizeEvent*) override; void resizeEvent(QResizeEvent*) override;
void dragEnterEvent(QDragEnterEvent* event) override; void dragEnterEvent(QDragEnterEvent* event) override;
@ -51,6 +52,9 @@ class ScreenSetupView : public QTableView
void startDrag(Qt::DropActions supportedActions) override; void startDrag(Qt::DropActions supportedActions) override;
QStyleOptionViewItem viewOptions() const override; QStyleOptionViewItem viewOptions() const override;
void scrollTo(const QModelIndex&, ScrollHint) override {} void scrollTo(const QModelIndex&, ScrollHint) override {}
private:
void enter(const QModelIndex&);
void remove(const QModelIndex&);
}; };
#endif #endif