Skip to content

Commit

Permalink
Shortcut settings improvements
Browse files Browse the repository at this point in the history
* Double-clicking to set a shortcut now works on the entire row, rather
  than just the Shortcut column.

* Enter/Return keys now also trigger the setting of a shortcut.

* Up/Down/PageUp/PageDown/Enter/Return keys can be used to interact
  with the shortcut list, even when the focus is in the filter widget.

Issue mapeditor#215
  • Loading branch information
bjorn authored and Ruin0x11 committed May 15, 2019
1 parent 73c59b7 commit dbba262
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 7 deletions.
64 changes: 64 additions & 0 deletions src/tiled/filteredit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* filteredit.cpp
* Copyright 2019, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "filteredit.h"

#include <QCoreApplication>
#include <QKeyEvent>

namespace Tiled {

FilterEdit::FilterEdit(QWidget *parent)
: QLineEdit(parent)
{
setClearButtonEnabled(true);
}

bool FilterEdit::event(QEvent *event)
{
if (mFilteredView) {
switch (event->type()) {
case QEvent::KeyPress:
case QEvent::KeyRelease: {
auto key = static_cast<QKeyEvent*>(event)->key();
if ( key == Qt::Key_Up ||
key == Qt::Key_Down ||
key == Qt::Key_PageUp ||
key == Qt::Key_PageDown ||
key == Qt::Key_Return ||
key == Qt::Key_Enter) {

// Forward some keys to the view, to allow changing the
// selection and activating items while the focus is in the
// filter edit.
QCoreApplication::sendEvent(mFilteredView, event);
return true;
}
break;
}
default:
break;
}
}

return QLineEdit::event(event);
}

} // namespace Tiled
46 changes: 46 additions & 0 deletions src/tiled/filteredit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* filteredit.h
* Copyright 2019, Thorbjørn Lindeijer <bjorn@lindeijer.nl>
*
* This file is part of Tiled.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QLineEdit>

namespace Tiled {

class FilterEdit : public QLineEdit
{
public:
FilterEdit(QWidget *parent = nullptr);

void setFilteredView(QWidget *view);

bool event(QEvent *event) override;

private:
QWidget *mFilteredView = nullptr;
};


inline void FilterEdit::setFilteredView(QWidget *view)
{
mFilteredView = view;
}

} // namespace Tiled
11 changes: 11 additions & 0 deletions src/tiled/shortcutsettingspage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ ShortcutSettingsPage::ShortcutSettingsPage(QWidget *parent)

ui->conflictsLabel->setVisible(false);

ui->filterEdit->setFilteredView(ui->shortcutsView);

mProxyModel->setSourceModel(mActionsModel);
mProxyModel->setSortLocaleAware(true);
mProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
Expand All @@ -582,6 +584,15 @@ ShortcutSettingsPage::ShortcutSettingsPage(QWidget *parent)
mActionsModel->refresh();
});

connect(ui->shortcutsView, &QAbstractItemView::activated,
this, [this] (const QModelIndex &index) {
if (index.isValid()) {
auto shortcutIndex = mProxyModel->index(index.row(), 2);
ui->shortcutsView->setCurrentIndex(shortcutIndex); // Makes sure editor closes when current index changes
ui->shortcutsView->edit(shortcutIndex);
}
});

connect(ui->shortcutsView->selectionModel(), &QItemSelectionModel::currentRowChanged,
this, &ShortcutSettingsPage::refreshConflicts);
connect(mProxyModel, &QAbstractItemModel::dataChanged,
Expand Down
12 changes: 8 additions & 4 deletions src/tiled/shortcutsettingspage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="filterEdit">
<widget class="Tiled::FilterEdit" name="filterEdit">
<property name="placeholderText">
<string>Filter</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item>
Expand Down Expand Up @@ -112,6 +109,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Tiled::FilterEdit</class>
<extends>QLineEdit</extends>
<header>filteredit.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>filterEdit</tabstop>
<tabstop>shortcutsView</tabstop>
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/tiled.pro
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ SOURCES += aboutdialog.cpp \
exporthelper.cpp \
filechangedwarning.cpp \
fileedit.cpp \
filteredit.cpp \
flexiblescrollbar.cpp \
flipmapobjects.cpp \
geometry.cpp \
Expand Down Expand Up @@ -367,6 +368,7 @@ HEADERS += aboutdialog.h \
exporthelper.h \
filechangedwarning.h \
fileedit.h \
filteredit.h \
flexiblescrollbar.h \
flipmapobjects.h \
geometry.h \
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/tiled.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ QtGuiApplication {
"filechangedwarning.h",
"fileedit.cpp",
"fileedit.h",
"filteredit.cpp",
"filteredit.h",
"flexiblescrollbar.cpp",
"flexiblescrollbar.h",
"flipmapobjects.cpp",
Expand Down
5 changes: 3 additions & 2 deletions src/tiled/tilestampsdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "tilestampsdock.h"

#include "documentmanager.h"
#include "filteredit.h"
#include "preferences.h"
#include "tilestamp.h"
#include "tilestampmanager.h"
Expand All @@ -44,7 +45,7 @@ TileStampsDock::TileStampsDock(TileStampManager *stampManager, QWidget *parent)
, mTileStampManager(stampManager)
, mTileStampModel(stampManager->tileStampModel())
, mProxyModel(new QSortFilterProxyModel(mTileStampModel))
, mFilterEdit(new QLineEdit(this))
, mFilterEdit(new FilterEdit(this))
, mNewStamp(new QAction(this))
, mAddVariation(new QAction(this))
, mDuplicate(new QAction(this))
Expand Down Expand Up @@ -81,7 +82,7 @@ TileStampsDock::TileStampsDock(TileStampManager *stampManager, QWidget *parent)
Utils::setThemeIcon(mDelete, "edit-delete");
Utils::setThemeIcon(mChooseFolder, "document-open");

mFilterEdit->setClearButtonEnabled(true);
mFilterEdit->setFilteredView(mTileStampView);

connect(mFilterEdit, &QLineEdit::textChanged,
mProxyModel, &QSortFilterProxyModel::setFilterFixedString);
Expand Down
3 changes: 2 additions & 1 deletion src/tiled/tilestampsdock.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Tiled {

class TileLayer;

class FilterEdit;
class TileStamp;
class TileStampManager;
class TileStampModel;
Expand Down Expand Up @@ -69,7 +70,7 @@ private slots:
TileStampModel *mTileStampModel;
QSortFilterProxyModel *mProxyModel;
TileStampView *mTileStampView;
QLineEdit *mFilterEdit;
FilterEdit *mFilterEdit;

QAction *mNewStamp;
QAction *mAddVariation;
Expand Down

0 comments on commit dbba262

Please sign in to comment.