Op 21-2-2012 22:49, Scott Kostyshak schreef:
From: lyx-devel@lists.lyx.org [lyx-devel@lists.lyx.org] on behalf of Jean-Marc 
Lasgouttes [lasgout...@lyx.org]
Sent: Tuesday, February 21, 2012 3:55 AM

What I would do is to study the code of the Insert->Citation dialog,
which has the correct behaviour in the search (find?) field, if I
understand correctly.
Ah, I was wondering why my grepping for setDefault didn't return many results.

I'm getting closer. I realize that I need to connect the signal returnPressed from 
newBranchLE to on_addBranchPB_pressed. I did this, and it seems to have partially 
done what I want, but when I enter text into the LE and press return the dialog 
goes away. The branch is correctly entered (which can be seen by going back to 
document settings>  branches). What did I do wrong? Why is it closing? Is 
updateView() doing that?

It is because the Ok button is the default button of the dialog. This button will be 'pressed' if the user presses return in the dialog.

See:
  https://qt-project.org/doc/qt-4.8/qpushbutton.html#autoDefault-prop
  https://qt-project.org/doc/qt-4.8/qpushbutton.html#default-prop

The only way to prevent the default button to be pressed is to add an eventFilter. See GuiSelectionManager::eventFilter().

The attached implements this (part 1). Part 2 also implements the behaviour of GuiSelectionManager that Ctrl+Enter does quit the dialog.

Can you send an e-mail to lyx-devel list like the following to grant your permission to use your contributions:

http://marc.info/?l=lyx-devel&m=130523685427995 ?


Vincent
diff --git a/src/frontends/qt4/GuiBranches.cpp 
b/src/frontends/qt4/GuiBranches.cpp
index 7759be2..d0ba467 100644
--- a/src/frontends/qt4/GuiBranches.cpp
+++ b/src/frontends/qt4/GuiBranches.cpp
@@ -76,15 +76,25 @@ GuiBranches::GuiBranches(QWidget * parent)
        newBranchLE->setValidator(new NoNewLineValidator(newBranchLE));
 }
 
-bool GuiBranches::eventFilter(QObject * obj, QEvent * event)
+bool GuiBranches::eventFilter(QObject * obj, QEvent * event) 
 {
        QEvent::Type etype = event->type();
        if (etype == QEvent::KeyPress && obj == newBranchLE) {
                QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
                int const keyPressed = keyEvent->key();
+               Qt::KeyboardModifiers const keyModifiers = 
keyEvent->modifiers();
 
                if (keyPressed == Qt::Key_Enter || keyPressed == 
Qt::Key_Return) {
-                       on_addBranchPB_pressed();
+                       if (!keyModifiers) {
+                               on_addBranchPB_pressed();
+                       } else if (keyModifiers == Qt::ControlModifier
+                                 || keyModifiers == Qt::KeypadModifier
+                                 || keyModifiers == (Qt::ControlModifier | 
Qt::KeypadModifier)) {
+                               if (addBranchPB->isEnabled()) {
+                                       on_addBranchPB_pressed();
+                                       okPressed();
+                               }
+                       }
                        event->accept();
                        return true;
                }
diff --git a/src/frontends/qt4/GuiBranches.h b/src/frontends/qt4/GuiBranches.h
index 3801f5c..fc29167 100644
--- a/src/frontends/qt4/GuiBranches.h
+++ b/src/frontends/qt4/GuiBranches.h
@@ -54,6 +54,7 @@ public:
 Q_SIGNALS:
        void changed();
        void renameBranches(docstring const &, docstring const &);
+       void okPressed();
 
 protected:
        void toggleBranch(QTreeWidgetItem *);
diff --git a/src/frontends/qt4/GuiDocument.cpp 
b/src/frontends/qt4/GuiDocument.cpp
index a388a86..aa33c8f 100644
--- a/src/frontends/qt4/GuiDocument.cpp
+++ b/src/frontends/qt4/GuiDocument.cpp
@@ -1289,6 +1289,7 @@ GuiDocument::GuiDocument(GuiView & lv)
                this, SLOT(change_adaptor()));
        connect(branchesModule, SIGNAL(renameBranches(docstring const &, 
docstring const &)),
                this, SLOT(branchesRename(docstring const &, docstring const 
&)));
+       connect(branchesModule, SIGNAL(okPressed()), this, SLOT(slotOK()));
        updateUnknownBranches();
 
 
diff --git a/src/frontends/qt4/GuiBranches.cpp 
b/src/frontends/qt4/GuiBranches.cpp
index c79a42d..7759be2 100644
--- a/src/frontends/qt4/GuiBranches.cpp
+++ b/src/frontends/qt4/GuiBranches.cpp
@@ -28,6 +28,7 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
+#include <QKeyEvent>
 #include <QListWidget>
 #include <QTreeWidget>
 #include <QTreeWidgetItem>
@@ -71,9 +72,26 @@ GuiBranches::GuiBranches(QWidget * parent)
        connect(undef_->cancelPB, SIGNAL(clicked()),
                undef_, SLOT(reject()));
 
+       newBranchLE->installEventFilter(this);
        newBranchLE->setValidator(new NoNewLineValidator(newBranchLE));
 }
 
+bool GuiBranches::eventFilter(QObject * obj, QEvent * event)
+{
+       QEvent::Type etype = event->type();
+       if (etype == QEvent::KeyPress && obj == newBranchLE) {
+               QKeyEvent * keyEvent = static_cast<QKeyEvent *>(event);
+               int const keyPressed = keyEvent->key();
+
+               if (keyPressed == Qt::Key_Enter || keyPressed == 
Qt::Key_Return) {
+                       on_addBranchPB_pressed();
+                       event->accept();
+                       return true;
+               }
+       }
+       return QObject::eventFilter(obj, event);
+}
+
 void GuiBranches::update(BufferParams const & params)
 {
        branchlist_ = params.branchlist();
@@ -132,14 +150,20 @@ void GuiBranches::apply(BufferParams & params) const
 }
 
 
+void GuiBranches::on_newBranchLE_textChanged(QString)
+{
+       QString const new_branch = newBranchLE->text();
+       addBranchPB->setEnabled(!new_branch.isEmpty());
+}
+
+
 void GuiBranches::on_addBranchPB_pressed()
 {
        QString const new_branch = newBranchLE->text();
-       if (!new_branch.isEmpty()) {
-               branchlist_.add(qstring_to_ucs4(new_branch));
-               newBranchLE->clear();
-               updateView();
-       }
+       branchlist_.add(qstring_to_ucs4(new_branch));
+       newBranchLE->clear();
+       addBranchPB->setEnabled(false);
+       updateView();
 }
 
 
diff --git a/src/frontends/qt4/GuiBranches.h b/src/frontends/qt4/GuiBranches.h
index 803a701..3801f5c 100644
--- a/src/frontends/qt4/GuiBranches.h
+++ b/src/frontends/qt4/GuiBranches.h
@@ -49,6 +49,8 @@ public:
        void apply(BufferParams & params) const;
        void setUnknownBranches(QStringList const & b) { unknown_branches_ = b; 
}
 
+       bool eventFilter(QObject * obj, QEvent * event);
+
 Q_SIGNALS:
        void changed();
        void renameBranches(docstring const &, docstring const &);
@@ -60,6 +62,7 @@ protected:
        void updateView();
 
 protected Q_SLOTS:
+       void on_newBranchLE_textChanged(QString);
        void on_addBranchPB_pressed();
        void on_removePB_pressed();
        void on_renamePB_pressed();
diff --git a/src/frontends/qt4/ui/BranchesUi.ui 
b/src/frontends/qt4/ui/BranchesUi.ui
index d8e97e2..4c83b18 100644
--- a/src/frontends/qt4/ui/BranchesUi.ui
+++ b/src/frontends/qt4/ui/BranchesUi.ui
@@ -103,6 +103,9 @@
    </item>
    <item row="0" column="3" >
     <widget class="QPushButton" name="addBranchPB" >
+     <property name="enabled">
+      <bool>false</bool>
+     </property>
      <property name="toolTip" >
       <string>Add a new branch to the list</string>
      </property>

Reply via email to