]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBranches.cpp
Do not compute caret geometry when we are not ready to do so.
[lyx.git] / src / frontends / qt4 / GuiBranches.cpp
index b3af3ed2936cb9f9ca0d979e7d1da38dd94b4684..ab91587098bc2c4f3d28ea2df73d0eb7325df729 100644 (file)
@@ -28,6 +28,7 @@
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
+#include <QKeyEvent>
 #include <QListWidget>
 #include <QTreeWidget>
 #include <QTreeWidgetItem>
@@ -36,6 +37,9 @@
 #include <QColor>
 #include <QColorDialog>
 
+#ifdef KeyPress
+#undef KeyPress
+#endif
 
 namespace lyx {
 namespace frontend {
@@ -49,7 +53,10 @@ GuiBranches::GuiBranches(QWidget * parent)
        branchesTW->headerItem()->setText(0, qt_("Branch"));
        branchesTW->headerItem()->setText(1, qt_("Activated"));
        branchesTW->headerItem()->setText(2, qt_("Color"));
+       branchesTW->headerItem()->setText(3, qt_("Filename Suffix"));
        branchesTW->setSortingEnabled(true);
+       branchesTW->resizeColumnToContents(1);
+       branchesTW->resizeColumnToContents(2);
 
        undef_ = new BranchesUnknownDialog(this);
        undef_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
@@ -67,8 +74,40 @@ GuiBranches::GuiBranches(QWidget * parent)
                undef_, SLOT(accept()));
        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
+                 && addBranchPB->isEnabled()) {
+               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) {
+                       if (!keyModifiers) {
+                               on_addBranchPB_pressed();
+                       } else if (keyModifiers == Qt::ControlModifier
+                                 || keyModifiers == Qt::KeypadModifier
+                                 || keyModifiers == (Qt::ControlModifier | Qt::KeypadModifier)) {
+                               on_addBranchPB_pressed();
+                               newBranchLE->clearFocus();
+                               okPressed();
+                       }
+                       event->accept();
+                       return true;
+               }
+       }
+       return QObject::eventFilter(obj, event);
 }
 
+
 void GuiBranches::update(BufferParams const & params)
 {
        branchlist_ = params.branchlist();
@@ -101,6 +140,7 @@ void GuiBranches::updateView()
                        coloritem.fill(itemcolor);
                        newItem->setIcon(2, QIcon(coloritem));
                }
+               newItem->setText(3, it->hasFileNameSuffix() ? qt_("Yes") : qt_("No"));
                // restore selected branch
                if (bname == sel_branch) {
                        branchesTW->setCurrentItem(newItem);
@@ -114,6 +154,7 @@ void GuiBranches::updateView()
        renamePB->setEnabled(have_sel);
        colorPB->setEnabled(have_sel);
        activatePB->setEnabled(have_sel);
+       suffixPB->setEnabled(have_sel);
        // emit signal
        changed();
 }
@@ -125,14 +166,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();
 }
 
 
@@ -159,10 +206,10 @@ void GuiBranches::on_renamePB_pressed()
        if (!sel_branch.isEmpty()) {
                docstring newname;
                docstring const oldname = qstring_to_ucs4(sel_branch);
-               bool success = false;
                if (Alert::askForText(newname, _("Enter new branch name"), oldname)) {
                        if (newname.empty() || oldname == newname)
                                return;
+                       bool success = false;
                        if (branchlist_.find(newname)) {
                                docstring text = support::bformat(
                                        _("A branch with the name \"%1$s\" already exists.\n"
@@ -177,7 +224,7 @@ void GuiBranches::on_renamePB_pressed()
                        updateView();
 
                        if (!success)
-                               Alert::error(_("Renaming failed"), 
+                               Alert::error(_("Renaming failed"),
                                      _("The branch could not be renamed."));
                        else
                                // emit signal
@@ -193,12 +240,20 @@ void GuiBranches::on_activatePB_pressed()
 }
 
 
+void GuiBranches::on_suffixPB_pressed()
+{
+       toggleSuffix(branchesTW->currentItem());
+}
+
+
 void GuiBranches::on_branchesTW_itemDoubleClicked(QTreeWidgetItem * item, int col)
 {
        if (col < 2)
                toggleBranch(item);
-       else
+       else if (col == 2)
                toggleColor(item);
+       else if (col == 3)
+               toggleSuffix(item);
 }
 
 
@@ -210,6 +265,7 @@ void GuiBranches::on_branchesTW_itemSelectionChanged()
        renamePB->setEnabled(have_sel);
        colorPB->setEnabled(have_sel);
        activatePB->setEnabled(have_sel);
+       suffixPB->setEnabled(have_sel);
 }
 
 
@@ -222,9 +278,8 @@ void GuiBranches::toggleBranch(QTreeWidgetItem * item)
        if (sel_branch.isEmpty())
                return;
 
-       bool const selected = (item->text(1) == qt_("Yes"));
        Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
-       if (branch && branch->setSelected(!selected)) {
+       if (branch && branch->setSelected(!branch->isSelected())) {
                newBranchLE->clear();
                updateView();
        }
@@ -263,6 +318,24 @@ void GuiBranches::toggleColor(QTreeWidgetItem * item)
 }
 
 
+void GuiBranches::toggleSuffix(QTreeWidgetItem * item)
+{
+       if (item == 0)
+               return;
+
+       QString sel_branch = item->text(0);
+       if (sel_branch.isEmpty())
+               return;
+
+       Branch * branch = branchlist_.find(qstring_to_ucs4(sel_branch));
+       if (branch) {
+               branch->setFileNameSuffix(!branch->hasFileNameSuffix());
+               newBranchLE->clear();
+               updateView();
+       }
+}
+
+
 void GuiBranches::on_unknownPB_pressed()
 {
        undef_->branchesLW->clear();
@@ -279,7 +352,7 @@ void GuiBranches::addUnknown()
 {
        QList<QListWidgetItem *> selItems =
                undef_->branchesLW->selectedItems();
-       
+
        QList<QListWidgetItem *>::const_iterator it = selItems.begin();
        for (; it != selItems.end() ; ++it) {
                QListWidgetItem const * new_branch = *it;