Skip to content

Commit

Permalink
Merge pull request #1031 from albertony/keyboard_ui
Browse files Browse the repository at this point in the history
Enable use of keyboard to modify screens
  • Loading branch information
p12tic authored Jan 25, 2021
2 parents 8202fe1 + 4ee6a54 commit 12024b9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made it possible to use keyboard instead of mouse to modify screen layout.
2 changes: 1 addition & 1 deletion src/gui/src/ScreenSetupModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Qt::ItemFlags ScreenSetupModel::flags(const QModelIndex& index) const
if (!screen(index).isNull())
return Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;

return Qt::ItemIsDropEnabled;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
}

Qt::DropActions ScreenSetupModel::supportedDropActions() const
Expand Down
78 changes: 72 additions & 6 deletions src/gui/src/ScreenSetupView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ ScreenSetupView::ScreenSetupView(QWidget* parent) :
setDropIndicatorShown(true);
setDragDropMode(DragDrop);
setSelectionMode(SingleSelection);
setTabKeyNavigation(false);

setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Expand Down Expand Up @@ -65,18 +66,83 @@ void ScreenSetupView::resizeEvent(QResizeEvent* event)
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)
{
if (event->buttons() & Qt::LeftButton)
{
int col = columnAt(event->pos().x());
int row = rowAt(event->pos().y());

if (!model()->screen(col, row).isNull())
{
ScreenSettingsDialog dlg(this, &model()->screen(col, row));
dlg.exec();
}
enter(model()->createIndex(row, col));
}
else
event->ignore();
Expand Down
4 changes: 4 additions & 0 deletions src/gui/src/ScreenSetupView.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ class ScreenSetupView : public QTableView

protected:
void mouseDoubleClickEvent(QMouseEvent*) override;
void keyPressEvent(QKeyEvent*) override;
void setTableSize();
void resizeEvent(QResizeEvent*) override;
void dragEnterEvent(QDragEnterEvent* event) override;
void dragMoveEvent(QDragMoveEvent* event) override;
void startDrag(Qt::DropActions supportedActions) override;
QStyleOptionViewItem viewOptions() const override;
void scrollTo(const QModelIndex&, ScrollHint) override {}
private:
void enter(const QModelIndex&);
void remove(const QModelIndex&);
};

#endif
Expand Down

0 comments on commit 12024b9

Please sign in to comment.