Use QKeySequenceEdit in Qt Designer.
authorFriedemann Kleint <Friedemann.Kleint@digia.com>
Fri, 18 Oct 2013 07:49:50 +0000 (09:49 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Mon, 21 Oct 2013 08:55:06 +0000 (10:55 +0200)
Use it in property editor and property browser solution.

Change-Id: I15f0750533748aaba3d592ad59b2063dbfc1fe62
Reviewed-by: Jarek Kobus <jaroslaw.kobus@digia.com>
src/designer/src/components/propertyeditor/designerpropertymanager.cpp
src/designer/src/components/propertyeditor/designerpropertymanager.h
src/designer/src/lib/shared/newactiondialog.ui
src/shared/qtpropertybrowser/qteditorfactory.cpp
src/shared/qtpropertybrowser/qtpropertybrowserutils.cpp
src/shared/qtpropertybrowser/qtpropertybrowserutils_p.h

index 715e41b..522ff62 100644 (file)
@@ -69,6 +69,7 @@
 #include <QtWidgets/QFileDialog>
 #include <QtWidgets/QAction>
 #include <QtWidgets/QMenu>
+#include <QtWidgets/QKeySequenceEdit>
 #include <QtGui/QContextMenuEvent>
 #include <QtWidgets/QApplication>
 #include <QtCore/QUrl>
@@ -2353,7 +2354,7 @@ void DesignerEditorFactory::slotValueChanged(QtProperty *property, const QVarian
         } else if (type == DesignerPropertyManager::designerStringListTypeId()) {
             applyToEditors(m_stringListPropertyToEditors.value(property), &StringListEditorButton::setStringList, qvariant_cast<PropertySheetStringListValue>(value).value());
         } else if (type == DesignerPropertyManager::designerKeySequenceTypeId()) {
-            applyToEditors(m_keySequencePropertyToEditors.value(property), &QtKeySequenceEdit::setKeySequence, qvariant_cast<PropertySheetKeySequenceValue>(value).value());
+            applyToEditors(m_keySequencePropertyToEditors.value(property), &QKeySequenceEdit::setKeySequence, qvariant_cast<PropertySheetKeySequenceValue>(value).value());
         }
         break;
     }
@@ -2513,7 +2514,7 @@ QWidget *DesignerEditorFactory::createEditor(QtVariantPropertyManager *manager,
             connect(ed, SIGNAL(stringListChanged(QStringList)), this, SLOT(slotStringListChanged(QStringList)));
             editor = ed;
         } else if (type == DesignerPropertyManager::designerKeySequenceTypeId()) {
-            QtKeySequenceEdit *ed = new QtKeySequenceEdit(parent);
+            QKeySequenceEdit *ed = new QKeySequenceEdit(parent);
             ed->setKeySequence(qvariant_cast<PropertySheetKeySequenceValue>(manager->value(property)).value());
             m_keySequencePropertyToEditors[property].append(ed);
             m_editorToKeySequenceProperty[ed] = property;
index 8dca888..7e22721 100644 (file)
@@ -62,7 +62,7 @@ typedef QList<DesignerIntPair> DesignerFlagList;
 class QDesignerFormEditorInterface;
 class QLineEdit;
 class QUrl;
-class QtKeySequenceEdit;
+class QKeySequenceEdit;
 
 namespace qdesigner_internal
 {
@@ -298,8 +298,8 @@ private:
 
     QMap<QtProperty *, QList<TextEditor *> >                m_stringPropertyToEditors;
     QMap<TextEditor *, QtProperty *>                        m_editorToStringProperty;
-    QMap<QtProperty *, QList<QtKeySequenceEdit *> >         m_keySequencePropertyToEditors;
-    QMap<QtKeySequenceEdit *, QtProperty *>                 m_editorToKeySequenceProperty;
+    QMap<QtProperty *, QList<QKeySequenceEdit *> >         m_keySequencePropertyToEditors;
+    QMap<QKeySequenceEdit *, QtProperty *>                 m_editorToKeySequenceProperty;
     QMap<QtProperty *, QList<PaletteEditorButton *> >       m_palettePropertyToEditors;
     QMap<PaletteEditorButton *, QtProperty *>               m_editorToPaletteProperty;
     QMap<QtProperty *, QList<PixmapEditor *> >              m_pixmapPropertyToEditors;
index c0d0132..9c7586f 100644 (file)
      <item row="6" column="1">
       <layout class="QHBoxLayout" name="keysequenceLayout">
        <item>
-        <widget class="QtKeySequenceEdit" name="keySequenceEdit" native="true">
+        <widget class="QKeySequenceEdit" name="keySequenceEdit" native="true">
          <property name="sizePolicy">
           <sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
            <horstretch>0</horstretch>
    <container>1</container>
   </customwidget>
   <customwidget>
-   <class>QtKeySequenceEdit</class>
-   <extends>QWidget</extends>
-   <header>qtpropertybrowserutils_p.h</header>
-   <container>1</container>
-  </customwidget>
-  <customwidget>
    <class>TextPropertyEditor</class>
    <extends>QWidget</extends>
    <header>textpropertyeditor_p.h</header>
index 30af069..434823a 100644 (file)
@@ -56,6 +56,7 @@
 #include <QtWidgets/QColorDialog>
 #include <QtWidgets/QFontDialog>
 #include <QtWidgets/QSpacerItem>
+#include <QtWidgets/QKeySequenceEdit>
 #include <QtCore/QMap>
 
 #if defined(Q_CC_MSVC)
@@ -1410,7 +1411,7 @@ void QtDateTimeEditFactory::disconnectPropertyManager(QtDateTimePropertyManager
 
 // QtKeySequenceEditorFactory
 
-class QtKeySequenceEditorFactoryPrivate : public EditorFactoryPrivate<QtKeySequenceEdit>
+class QtKeySequenceEditorFactoryPrivate : public EditorFactoryPrivate<QKeySequenceEdit>
 {
     QtKeySequenceEditorFactory *q_ptr;
     Q_DECLARE_PUBLIC(QtKeySequenceEditorFactory)
@@ -1426,9 +1427,9 @@ void QtKeySequenceEditorFactoryPrivate::slotPropertyChanged(QtProperty *property
     if (!m_createdEditors.contains(property))
         return;
 
-    QListIterator<QtKeySequenceEdit *> itEditor(m_createdEditors[property]);
+    QListIterator<QKeySequenceEdit *> itEditor(m_createdEditors[property]);
     while (itEditor.hasNext()) {
-        QtKeySequenceEdit *editor = itEditor.next();
+        QKeySequenceEdit *editor = itEditor.next();
         editor->blockSignals(true);
         editor->setKeySequence(value);
         editor->blockSignals(false);
@@ -1438,8 +1439,8 @@ void QtKeySequenceEditorFactoryPrivate::slotPropertyChanged(QtProperty *property
 void QtKeySequenceEditorFactoryPrivate::slotSetValue(const QKeySequence &value)
 {
     QObject *object = q_ptr->sender();
-    const  QMap<QtKeySequenceEdit *, QtProperty *>::ConstIterator ecend = m_editorToProperty.constEnd();
-    for (QMap<QtKeySequenceEdit *, QtProperty *>::ConstIterator itEditor =  m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor)
+    const  QMap<QKeySequenceEdit *, QtProperty *>::ConstIterator ecend = m_editorToProperty.constEnd();
+    for (QMap<QKeySequenceEdit *, QtProperty *>::ConstIterator itEditor =  m_editorToProperty.constBegin(); itEditor != ecend; ++itEditor)
         if (itEditor.key() == object) {
             QtProperty *property = itEditor.value();
             QtKeySequencePropertyManager *manager = q_ptr->propertyManager(property);
@@ -1499,7 +1500,7 @@ void QtKeySequenceEditorFactory::connectPropertyManager(QtKeySequencePropertyMan
 QWidget *QtKeySequenceEditorFactory::createEditor(QtKeySequencePropertyManager *manager,
         QtProperty *property, QWidget *parent)
 {
-    QtKeySequenceEdit *editor = d_ptr->createEditor(property, parent);
+    QKeySequenceEdit *editor = d_ptr->createEditor(property, parent);
     editor->setKeySequence(manager->value(property));
 
     connect(editor, SIGNAL(keySequenceChanged(QKeySequence)),
index 16a6743..2ceb0fe 100644 (file)
@@ -301,152 +301,4 @@ void QtBoolEdit::mousePressEvent(QMouseEvent *event)
     }
 }
 
-
-QtKeySequenceEdit::QtKeySequenceEdit(QWidget *parent)
-    : QWidget(parent), m_num(0), m_lineEdit(new QLineEdit(this))
-{
-    QHBoxLayout *layout = new QHBoxLayout(this);
-    layout->addWidget(m_lineEdit);
-    layout->setMargin(0);
-    m_lineEdit->installEventFilter(this);
-    m_lineEdit->setReadOnly(true);
-    m_lineEdit->setFocusProxy(this);
-    setFocusPolicy(m_lineEdit->focusPolicy());
-    setAttribute(Qt::WA_InputMethodEnabled);
-}
-
-bool QtKeySequenceEdit::eventFilter(QObject *o, QEvent *e)
-{
-    if (o == m_lineEdit && e->type() == QEvent::ContextMenu) {
-        QContextMenuEvent *c = static_cast<QContextMenuEvent *>(e);
-        QMenu *menu = m_lineEdit->createStandardContextMenu();
-        const QList<QAction *> actions = menu->actions();
-        QListIterator<QAction *> itAction(actions);
-        while (itAction.hasNext()) {
-            QAction *action = itAction.next();
-            action->setShortcut(QKeySequence());
-            QString actionString = action->text();
-            const int pos = actionString.lastIndexOf(QLatin1Char('\t'));
-            if (pos > 0)
-                actionString.remove(pos, actionString.length() - pos);
-            action->setText(actionString);
-        }
-        QAction *actionBefore = 0;
-        if (actions.count() > 0)
-            actionBefore = actions[0];
-        QAction *clearAction = new QAction(tr("Clear Shortcut"), menu);
-        menu->insertAction(actionBefore, clearAction);
-        menu->insertSeparator(actionBefore);
-        clearAction->setEnabled(!m_keySequence.isEmpty());
-        connect(clearAction, SIGNAL(triggered()), this, SLOT(slotClearShortcut()));
-        menu->exec(c->globalPos());
-        delete menu;
-        e->accept();
-        return true;
-    }
-
-    return QWidget::eventFilter(o, e);
-}
-
-void QtKeySequenceEdit::slotClearShortcut()
-{
-    if (m_keySequence.isEmpty())
-        return;
-    setKeySequence(QKeySequence());
-    emit keySequenceChanged(m_keySequence);
-}
-
-void QtKeySequenceEdit::handleKeyEvent(QKeyEvent *e)
-{
-    int nextKey = e->key();
-    if (nextKey == Qt::Key_Control || nextKey == Qt::Key_Shift ||
-            nextKey == Qt::Key_Meta || nextKey == Qt::Key_Alt ||
-            nextKey == Qt::Key_Super_L || nextKey == Qt::Key_AltGr)
-        return;
-
-    nextKey |= translateModifiers(e->modifiers(), e->text());
-    int k0 = m_keySequence[0];
-    int k1 = m_keySequence[1];
-    int k2 = m_keySequence[2];
-    int k3 = m_keySequence[3];
-    switch (m_num) {
-        case 0: k0 = nextKey; k1 = 0; k2 = 0; k3 = 0; break;
-        case 1: k1 = nextKey; k2 = 0; k3 = 0; break;
-        case 2: k2 = nextKey; k3 = 0; break;
-        case 3: k3 = nextKey; break;
-        default: break;
-    }
-    ++m_num;
-    if (m_num > 3)
-        m_num = 0;
-    m_keySequence = QKeySequence(k0, k1, k2, k3);
-    m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
-    e->accept();
-    emit keySequenceChanged(m_keySequence);
-}
-
-void QtKeySequenceEdit::setKeySequence(const QKeySequence &sequence)
-{
-    if (sequence == m_keySequence)
-        return;
-    m_num = 0;
-    m_keySequence = sequence;
-    m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));
-}
-
-QKeySequence QtKeySequenceEdit::keySequence() const
-{
-    return m_keySequence;
-}
-
-int QtKeySequenceEdit::translateModifiers(Qt::KeyboardModifiers state, const QString &text) const
-{
-    int result = 0;
-    if ((state & Qt::ShiftModifier) && (text.size() == 0 || !text.at(0).isPrint() || text.at(0).isLetter() || text.at(0).isSpace()))
-        result |= Qt::SHIFT;
-    if (state & Qt::ControlModifier)
-        result |= Qt::CTRL;
-    if (state & Qt::MetaModifier)
-        result |= Qt::META;
-    if (state & Qt::AltModifier)
-        result |= Qt::ALT;
-    return result;
-}
-
-void QtKeySequenceEdit::focusInEvent(QFocusEvent *e)
-{
-    m_lineEdit->event(e);
-    m_lineEdit->selectAll();
-    QWidget::focusInEvent(e);
-}
-
-void QtKeySequenceEdit::focusOutEvent(QFocusEvent *e)
-{
-    m_num = 0;
-    m_lineEdit->event(e);
-    QWidget::focusOutEvent(e);
-}
-
-void QtKeySequenceEdit::keyPressEvent(QKeyEvent *e)
-{
-    handleKeyEvent(e);
-    e->accept();
-}
-
-void QtKeySequenceEdit::keyReleaseEvent(QKeyEvent *e)
-{
-    m_lineEdit->event(e);
-}
-
-bool QtKeySequenceEdit::event(QEvent *e)
-{
-    if (e->type() == QEvent::Shortcut ||
-            e->type() == QEvent::ShortcutOverride  ||
-            e->type() == QEvent::KeyRelease) {
-        e->accept();
-        return true;
-    }
-    return QWidget::event(e);
-}
-
 QT_END_NAMESPACE
index c55c6ec..1245b20 100644 (file)
@@ -127,35 +127,6 @@ private:
     bool m_textVisible;
 };
 
-class QtKeySequenceEdit : public QWidget
-{
-    Q_OBJECT
-public:
-    QtKeySequenceEdit(QWidget *parent = 0);
-
-    QKeySequence keySequence() const;
-    bool eventFilter(QObject *o, QEvent *e);
-public Q_SLOTS:
-    void setKeySequence(const QKeySequence &sequence);
-Q_SIGNALS:
-    void keySequenceChanged(const QKeySequence &sequence);
-protected:
-    void focusInEvent(QFocusEvent *e);
-    void focusOutEvent(QFocusEvent *e);
-    void keyPressEvent(QKeyEvent *e);
-    void keyReleaseEvent(QKeyEvent *e);
-    bool event(QEvent *e);
-private slots:
-    void slotClearShortcut();
-private:
-    void handleKeyEvent(QKeyEvent *e);
-    int translateModifiers(Qt::KeyboardModifiers state, const QString &text) const;
-
-    int m_num;
-    QKeySequence m_keySequence;
-    QLineEdit *m_lineEdit;
-};
-
 QT_END_NAMESPACE
 
 #endif