]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/ButtonController.cpp
Make code a bit easier to read
[lyx.git] / src / frontends / qt / ButtonController.cpp
index c8644abb85b1673b5b06e3db24505aff6b5124e5..e347f2df052bff196ead504e3defeebf578e016e 100644 (file)
@@ -11,6 +11,8 @@
 #include <config.h>
 
 #include "ButtonController.h"
+#include "GuiApplication.h"
+#include "PanelStack.h"
 
 #include "qt_helpers.h"
 
 #include <QLineEdit>
 #include <QLabel>
 #include <QList>
+#include <QTabWidget>
 #include <QValidator>
 
 
 namespace lyx {
 namespace frontend {
 
-static void setWidgetEnabled(QWidget * obj, bool enabled)
-{
-       if (QLineEdit * le = qobject_cast<QLineEdit*>(obj))
-               le->setReadOnly(!enabled);
-       else
-               obj->setEnabled(enabled);
-
-       obj->setFocusPolicy(enabled ? Qt::StrongFocus : Qt::NoFocus);
-}
-
-
 /////////////////////////////////////////////////////////////////////////
 //
 // CheckedLineEdit
@@ -47,23 +39,37 @@ static void setWidgetEnabled(QWidget * obj, bool enabled)
 class CheckedLineEdit
 {
 public:
-       CheckedLineEdit(QLineEdit * input, QWidget * label = 0);
+       CheckedLineEdit(QLineEdit * input, QWidget * label = nullptr,
+                       int tabindex = -1, QString const panel = QString());
+       /// check the widget and do visual marking
        bool check() const;
+       /// reset all visual markings for tabs or panel sections
+       void setSectionsValid() const;
 
 private:
        // non-owned
        QLineEdit * input_;
-       QWidget * label_;
+       QWidget * target_;
+       int tab_index_;
+       QString panel_name_;
 };
 
 
-CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label)
-       : input_(input), label_(label)
+CheckedLineEdit::CheckedLineEdit(QLineEdit * input, QWidget * label,
+                                int tabindex, QString const panel)
+       : input_(input), target_(label), tab_index_(tabindex), panel_name_(panel)
 {}
 
 
 bool CheckedLineEdit::check() const
 {
+       if (!input_->isEnabled()) {
+               // we do not check diabled widgets
+               if (target_)
+                       setValid(target_, true);
+               return true;
+       }
+
        QValidator const * validator = input_->validator();
        if (!validator)
                return true;
@@ -74,13 +80,38 @@ bool CheckedLineEdit::check() const
 
        // Visual feedback.
        setValid(input_, valid);
-       if (label_)
-               setValid(label_, valid);
+       if (target_) {
+               if (!valid && !panel_name_.isEmpty() && qobject_cast<PanelStack*>(target_) != nullptr) {
+                       qobject_cast<PanelStack*>(target_)->markPanelValid(panel_name_, false);
+                       // this is a panel, so stop here.
+                       return valid;
+               }
+               setValid(target_, valid);
+               if (!valid && tab_index_ >= 0 && qobject_cast<QTabWidget*>(target_) != nullptr) {
+                       QIcon warn(guiApp ? guiApp->getScaledPixmap("images/", "emblem-shellescape")
+                                         : getPixmap("images/", "emblem-shellescape", "svgz,png"));
+                       QTabBar * tb = qobject_cast<QTabWidget*>(target_)->tabBar();
+                       tb->setTabIcon(tab_index_, warn);
+                       tb->setTabToolTip(tab_index_, qt_("This tab contains invalid input. Please fix!"));
+               }
+       }
 
        return valid;
 }
 
 
+void CheckedLineEdit::setSectionsValid() const
+{
+       if (target_ && tab_index_ >= 0 && qobject_cast<QTabWidget*>(target_) != nullptr) {
+               QTabBar * tb = qobject_cast<QTabWidget*>(target_)->tabBar();
+               tb->setTabIcon(tab_index_, QIcon());
+               tb->setTabToolTip(tab_index_, QString());
+       }
+       else if (!panel_name_.isEmpty() && qobject_cast<PanelStack*>(target_) != nullptr)
+               qobject_cast<PanelStack*>(target_)->markPanelValid(panel_name_, true);
+}
+
+
 /////////////////////////////////////////////////////////////////////////
 //
 // ButtonController::Private
@@ -92,15 +123,15 @@ class ButtonController::Private
 public:
        typedef QList<CheckedLineEdit> CheckedWidgetList;
 
-       Private()
-               : okay_(0), apply_(0), cancel_(0), restore_(0), auto_apply_(0), default_(0),
-                       policy_(ButtonPolicy::IgnorantPolicy)
-       {}
+       Private() {}
 
        /// \return true if all CheckedWidgets are in a valid state.
        bool checkWidgets() const
        {
                bool valid = true;
+               for (const CheckedLineEdit & w : checked_widgets_) {
+                       w.setSectionsValid();
+               }
                for (const CheckedLineEdit & w : checked_widgets_)
                        valid &= w.check();
                return valid;
@@ -109,17 +140,17 @@ public:
 public:
        CheckedWidgetList checked_widgets_;
 
-       QPushButton * okay_;
-       QPushButton * apply_;
-       QPushButton * cancel_;
-       QPushButton * restore_;
-       QCheckBox * auto_apply_;
-       QPushButton * default_;
+       QPushButton * okay_ = nullptr;
+       QPushButton * apply_ = nullptr;
+       QPushButton * cancel_ = nullptr;
+       QPushButton * restore_ = nullptr;
+       QCheckBox * auto_apply_ = nullptr;
+       QPushButton * default_ = nullptr;
 
        typedef QList<QWidget *> Widgets;
        Widgets read_only_;
 
-       ButtonPolicy policy_;
+       ButtonPolicy policy_ {ButtonPolicy::IgnorantPolicy};
 };
 
 
@@ -203,10 +234,7 @@ bool ButtonController::setReadOnly(bool ro)
 
        d->policy_.input(ro ?
                ButtonPolicy::SMI_READ_ONLY : ButtonPolicy::SMI_READ_WRITE);
-       // refreshReadOnly(); This will enable all widgets in dialogs, no matter if
-       //                    they allowed to be enabled, so when you plan to
-       //                    reenable this call, read this before:
-       // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg128222.html
+
        refresh();
        return ro;
 }
@@ -250,19 +278,15 @@ void ButtonController::refresh() const
 }
 
 
-void ButtonController::refreshReadOnly() const
+void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * target, int tabindex)
 {
-       if (d->read_only_.empty())
-               return;
-       bool const enable = !policy().isReadOnly();
-       for(QWidget * w : d->read_only_)
-               setWidgetEnabled(w, enable);
+       d->checked_widgets_.append(CheckedLineEdit(input, target, tabindex));
 }
 
 
-void ButtonController::addCheckedLineEdit(QLineEdit * input, QWidget * label)
+void ButtonController::addCheckedLineEditPanel(QLineEdit * input, QWidget * target, QString const panel)
 {
-       d->checked_widgets_.append(CheckedLineEdit(input, label));
+       d->checked_widgets_.append(CheckedLineEdit(input, target, -1, panel));
 }