Commit a1034af2 authored by hjk's avatar hjk

Core: Mark shortcut conflicts in keyboard settings input box

Task-number: QTCREATORBUG-6

Change-Id: Ife1f97d24733814e7512dd8bc6584a1d72f66fa8
Reviewed-by: default avatarChristian Stenger <christian.stenger@digia.com>
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 437e593a
......@@ -28,142 +28,254 @@
****************************************************************************/
#include "commandmappings.h"
#include "ui_commandmappings.h"
#include "commandsfile.h"
#include <coreplugin/dialogs/shortcutsettings.h>
#include <utils/hostosinfo.h>
#include <utils/headerviewstretcher.h>
#include <utils/fancylineedit.h>
#include <utils/qtcassert.h>
#include <QTreeWidgetItem>
#include <QDebug>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QPointer>
#include <QPushButton>
#include <QTreeWidgetItem>
#include <QVBoxLayout>
Q_DECLARE_METATYPE(Core::Internal::ShortcutItem*)
using namespace Core;
using namespace Core::Internal;
using namespace Utils;
namespace Core {
namespace Internal {
class KeySequenceValidator : public FancyLineEdit
{
public:
KeySequenceValidator(QWidget *parent, CommandMappings *mappings)
: FancyLineEdit(parent), m_mappings(mappings)
{}
bool validate(const QString &, QString *) const
{
return !m_mappings->hasConflicts();
}
CommandMappings *m_mappings;
};
class CommandMappingsPrivate
{
public:
CommandMappingsPrivate(CommandMappings *parent)
: q(parent), m_widget(0)
{}
void setupWidget()
{
QTC_CHECK(m_widget == 0);
m_widget = new QWidget;
groupBox = new QGroupBox(m_widget);
groupBox->setTitle(CommandMappings::tr("Command Mappings"));
filterEdit = new FancyLineEdit(groupBox);
filterEdit->setFiltering(true);
commandList = new QTreeWidget(groupBox);
commandList->setRootIsDecorated(false);
commandList->setUniformRowHeights(true);
commandList->setSortingEnabled(true);
commandList->setColumnCount(3);
QTreeWidgetItem *item = commandList->headerItem();
item->setText(2, CommandMappings::tr("Target"));
item->setText(1, CommandMappings::tr("Label"));
item->setText(0, CommandMappings::tr("Command"));
defaultButton = new QPushButton(CommandMappings::tr("Reset All"), groupBox);
defaultButton->setToolTip(CommandMappings::tr("Reset all to default"));
importButton = new QPushButton(CommandMappings::tr("Import..."), groupBox);
exportButton = new QPushButton(CommandMappings::tr("Export..."), groupBox);
targetEditGroup = new QGroupBox(CommandMappings::tr("Target Identifier"), m_widget);
targetEdit = new KeySequenceValidator(targetEditGroup, q);
targetEdit->setAutoHideButton(FancyLineEdit::Right, true);
targetEdit->setPlaceholderText(QString());
targetEdit->installEventFilter(q);
targetEdit->setFiltering(true);
resetButton = new QPushButton(targetEditGroup);
resetButton->setToolTip(CommandMappings::tr("Reset to default"));
resetButton->setText(CommandMappings::tr("Reset"));
QLabel *infoLabel = new QLabel(targetEditGroup);
infoLabel->setTextFormat(Qt::RichText);
QHBoxLayout *hboxLayout1 = new QHBoxLayout();
hboxLayout1->addWidget(defaultButton);
hboxLayout1->addStretch();
hboxLayout1->addWidget(importButton);
hboxLayout1->addWidget(exportButton);
QHBoxLayout *hboxLayout = new QHBoxLayout();
hboxLayout->addWidget(filterEdit);
QVBoxLayout *vboxLayout1 = new QVBoxLayout(groupBox);
vboxLayout1->addLayout(hboxLayout);
vboxLayout1->addWidget(commandList);
vboxLayout1->addLayout(hboxLayout1);
QHBoxLayout *hboxLayout2 = new QHBoxLayout();
hboxLayout2->addWidget(new QLabel(CommandMappings::tr("Target:"), targetEditGroup));
hboxLayout2->addWidget(targetEdit);
hboxLayout2->addWidget(resetButton);
QVBoxLayout *vboxLayout2 = new QVBoxLayout(targetEditGroup);
vboxLayout2->addLayout(hboxLayout2);
vboxLayout2->addWidget(infoLabel);
QVBoxLayout *vboxLayout = new QVBoxLayout(m_widget);
vboxLayout->addWidget(groupBox);
vboxLayout->addWidget(targetEditGroup);
q->connect(targetEdit, SIGNAL(buttonClicked(Utils::FancyLineEdit::Side)),
SLOT(removeTargetIdentifier()));
q->connect(resetButton, SIGNAL(clicked()),
SLOT(resetTargetIdentifier()));
q->connect(exportButton, SIGNAL(clicked()),
SLOT(exportAction()));
q->connect(importButton, SIGNAL(clicked()),
SLOT(importAction()));
q->connect(defaultButton, SIGNAL(clicked()),
SLOT(defaultAction()));
q->initialize();
commandList->sortByColumn(0, Qt::AscendingOrder);
q->connect(filterEdit, SIGNAL(textChanged(QString)),
SLOT(filterChanged(QString)));
q->connect(commandList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
SLOT(commandChanged(QTreeWidgetItem*)));
q->connect(targetEdit, SIGNAL(textChanged(QString)),
SLOT(targetIdentifierChanged()));
new HeaderViewStretcher(commandList->header(), 1);
q->commandChanged(0);
}
CommandMappings *q;
QPointer<QWidget> m_widget;
QGroupBox *groupBox;
FancyLineEdit *filterEdit;
QTreeWidget *commandList;
QPushButton *defaultButton;
QPushButton *importButton;
QPushButton *exportButton;
QGroupBox *targetEditGroup;
FancyLineEdit *targetEdit;
QPushButton *resetButton;
};
} // namespace Internal
CommandMappings::CommandMappings(QObject *parent)
: IOptionsPage(parent), m_page(0)
: IOptionsPage(parent), d(new Internal::CommandMappingsPrivate(this))
{
}
// IOptionsPage
CommandMappings::~CommandMappings()
{
delete d;
}
QWidget *CommandMappings::widget()
{
if (!m_widget) {
m_page = new Ui::CommandMappings();
m_widget = new QWidget;
m_page->setupUi(m_widget);
m_page->targetEdit->setAutoHideButton(Utils::FancyLineEdit::Right, true);
m_page->targetEdit->setPlaceholderText(QString());
m_page->targetEdit->installEventFilter(this);
m_page->targetEdit->setFiltering(true);
m_page->filterEdit->setFiltering(true);
connect(m_page->targetEdit, SIGNAL(buttonClicked(Utils::FancyLineEdit::Side)),
this, SLOT(removeTargetIdentifier()));
connect(m_page->resetButton, SIGNAL(clicked()),
this, SLOT(resetTargetIdentifier()));
connect(m_page->exportButton, SIGNAL(clicked()),
this, SLOT(exportAction()));
connect(m_page->importButton, SIGNAL(clicked()),
this, SLOT(importAction()));
connect(m_page->defaultButton, SIGNAL(clicked()),
this, SLOT(defaultAction()));
initialize();
m_page->commandList->sortByColumn(0, Qt::AscendingOrder);
connect(m_page->filterEdit, SIGNAL(textChanged(QString)),
this, SLOT(filterChanged(QString)));
connect(m_page->commandList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
this, SLOT(commandChanged(QTreeWidgetItem*)));
connect(m_page->targetEdit, SIGNAL(textChanged(QString)),
this, SLOT(targetIdentifierChanged()));
new Utils::HeaderViewStretcher(m_page->commandList->header(), 1);
commandChanged(0);
}
return m_widget;
if (!d->m_widget)
d->setupWidget();
return d->m_widget;
}
void CommandMappings::setImportExportEnabled(bool enabled)
{
m_page->importButton->setVisible(enabled);
m_page->exportButton->setVisible(enabled);
d->importButton->setVisible(enabled);
d->exportButton->setVisible(enabled);
}
QTreeWidget *CommandMappings::commandList() const
{
return m_page->commandList;
return d->commandList;
}
QLineEdit *CommandMappings::targetEdit() const
{
return m_page->targetEdit;
return d->targetEdit;
}
void CommandMappings::setPageTitle(const QString &s)
{
m_page->groupBox->setTitle(s);
d->groupBox->setTitle(s);
}
void CommandMappings::setTargetLabelText(const QString &s)
{
m_page->targetEditLabel->setText(s);
d->targetEdit->setText(s);
}
void CommandMappings::setTargetEditTitle(const QString &s)
{
m_page->targetEditGroup->setTitle(s);
d->targetEditGroup->setTitle(s);
}
void CommandMappings::setTargetHeader(const QString &s)
{
m_page->commandList->setHeaderLabels(QStringList() << tr("Command") << tr("Label") << s);
d->commandList->setHeaderLabels(QStringList() << tr("Command") << tr("Label") << s);
}
void CommandMappings::finish()
{
delete m_widget;
if (!m_page) // page was never shown
return;
delete m_page;
m_page = 0;
delete d->m_widget;
}
void CommandMappings::commandChanged(QTreeWidgetItem *current)
{
if (!current || !current->data(0, Qt::UserRole).isValid()) {
m_page->targetEdit->setText(QString());
m_page->targetEditGroup->setEnabled(false);
d->targetEdit->setText(QString());
d->targetEditGroup->setEnabled(false);
return;
}
m_page->targetEditGroup->setEnabled(true);
d->targetEditGroup->setEnabled(true);
}
void CommandMappings::filterChanged(const QString &f)
{
if (!m_page)
return;
for (int i=0; i<m_page->commandList->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = m_page->commandList->topLevelItem(i);
for (int i = 0; i < d->commandList->topLevelItemCount(); ++i) {
QTreeWidgetItem *item = d->commandList->topLevelItem(i);
filter(f, item);
}
}
bool CommandMappings::hasConflicts() const
{
return true;
}
bool CommandMappings::filter(const QString &filterString, QTreeWidgetItem *item)
{
bool visible = filterString.isEmpty();
int columnCount = item->columnCount();
for (int i = 0; !visible && i < columnCount; ++i) {
QString text = item->text(i);
if (Utils::HostOsInfo::isMacHost()) {
if (HostOsInfo::isMacHost()) {
// accept e.g. Cmd+E in the filter. the text shows special fancy characters for Cmd
if (i == columnCount - 1) {
QKeySequence key = QKeySequence::fromString(text, QKeySequence::NativeText);
......@@ -203,7 +315,7 @@ void CommandMappings::setModified(QTreeWidgetItem *item , bool modified)
QString CommandMappings::filterText() const
{
if (!m_page)
return QString();
return m_page->filterEdit->text();
return d->filterEdit ? d->filterEdit->text() : QString();
}
} // namespace Core
......@@ -33,7 +33,6 @@
#include <coreplugin/dialogs/ioptionspage.h>
#include <QObject>
#include <QPointer>
QT_BEGIN_NAMESPACE
class QLineEdit;
......@@ -41,9 +40,11 @@ class QTreeWidget;
class QTreeWidgetItem;
QT_END_NAMESPACE
namespace Utils { class FancyLineEdit; }
namespace Core {
namespace Internal { namespace Ui { class CommandMappings; } }
namespace Internal { class CommandMappingsPrivate; }
class CORE_EXPORT CommandMappings : public Core::IOptionsPage
{
......@@ -51,6 +52,8 @@ class CORE_EXPORT CommandMappings : public Core::IOptionsPage
public:
CommandMappings(QObject *parent = 0);
~CommandMappings();
virtual bool hasConflicts() const;
protected slots:
void commandChanged(QTreeWidgetItem *current);
......@@ -78,11 +81,10 @@ protected:
void setTargetEditTitle(const QString &s);
void setTargetHeader(const QString &s);
void setModified(QTreeWidgetItem *item, bool modified);
virtual void markPossibleCollisions(QTreeWidgetItem *) {}
virtual void resetCollisionMarkers() {}
private:
QPointer<QWidget> m_widget;
Internal::Ui::CommandMappings *m_page;
friend class Internal::CommandMappingsPrivate;
Internal::CommandMappingsPrivate *d;
};
} // namespace Core
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Core::Internal::CommandMappings</class>
<widget class="QWidget" name="Core::Internal::CommandMappings">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>568</width>
<height>451</height>
</rect>
</property>
<layout class="QVBoxLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Command Mappings</string>
</property>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="Utils::FancyLineEdit" name="filterEdit"/>
</item>
</layout>
</item>
<item>
<widget class="QTreeWidget" name="commandList">
<property name="rootIsDecorated">
<bool>false</bool>
</property>
<property name="uniformRowHeights">
<bool>true</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<property name="columnCount">
<number>3</number>
</property>
<column>
<property name="text">
<string>Command</string>
</property>
</column>
<column>
<property name="text">
<string>Label</string>
</property>
</column>
<column>
<property name="text">
<string>Target</string>
</property>
</column>
</widget>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QPushButton" name="defaultButton">
<property name="toolTip">
<string>Reset all to default</string>
</property>
<property name="text">
<string>Reset All</string>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="importButton">
<property name="text">
<string>Import...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="exportButton">
<property name="text">
<string>Export...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="targetEditGroup">
<property name="title">
<string>Target Identifier</string>
</property>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="targetEditLabel">
<property name="text">
<string>Target:</string>
</property>
</widget>
</item>
<item>
<widget class="Utils::FancyLineEdit" name="targetEdit"/>
</item>
<item>
<widget class="QPushButton" name="resetButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Reset to default</string>
</property>
<property name="text">
<string>Reset</string>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="infoLabel">
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>Utils::FancyLineEdit</class>
<extends>QLineEdit</extends>
<header location="global">utils/fancylineedit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
......@@ -197,7 +197,6 @@ HEADERS += mainwindow.h \
dialogs/addtovcsdialog.h
FORMS += dialogs/newdialog.ui \
actionmanager/commandmappings.ui \
dialogs/saveitemsdialog.ui \
dialogs/readonlyfilesdialog.ui \
dialogs/openwithdialog.ui \
......
......@@ -102,7 +102,7 @@ QtcPlugin {
"actionmanager.cpp", "actionmanager.h", "actionmanager_p.h",
"command.cpp", "command.h", "command_p.h",
"commandbutton.cpp", "commandbutton.h",
"commandmappings.cpp", "commandmappings.h", "commandmappings.ui",
"commandmappings.cpp", "commandmappings.h",
"commandsfile.cpp", "commandsfile.h",
]
}
......
......@@ -37,6 +37,8 @@
#include <coreplugin/actionmanager/command_p.h>
#include <coreplugin/actionmanager/commandsfile.h>
#include <utils/fancylineedit.h>
#include <QKeyEvent>
#include <QFileDialog>
#include <QLineEdit>
......@@ -127,6 +129,7 @@ void ShortcutSettings::commandChanged(QTreeWidgetItem *current)
return;
ShortcutItem *scitem = qvariant_cast<ShortcutItem *>(current->data(0, Qt::UserRole));
setKeySequence(scitem->m_key);
markCollisions(scitem);
}
void ShortcutSettings::targetIdentifierChanged()
......@@ -140,11 +143,47 @@ void ShortcutSettings::targetIdentifierChanged()
else
setModified(current, false);
current->setText(2, scitem->m_key.toString(QKeySequence::NativeText));
resetCollisionMarker(scitem);
markPossibleCollisions(scitem);
markCollisions(scitem);
}
}
bool ShortcutSettings::hasConflicts() const
{
QTreeWidgetItem *current = commandList()->currentItem();
if (!current || !current->data(0, Qt::UserRole).isValid())
return false;
ShortcutItem *item = qvariant_cast<ShortcutItem *>(current->data(0, Qt::UserRole));
if (!item)
return false;