]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBox.cpp
do what the FIXME suggested
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
index a90238b3cddb829de3ae9966cce66040a29bcd65..b52d1ea3529c490f393760fe0135caf7ee875787 100644 (file)
 
 #include "GuiBox.h"
 
+#include "FuncRequest.h"
+#include "support/gettext.h"
+
 #include "LengthCombo.h"
+#include "Length.h"
 #include "qt_helpers.h"
-#include "lengthcommon.h"
 #include "LyXRC.h" // to set the default length values
 #include "Validator.h"
 
 
 #include <QPushButton>
 #include <QLineEdit>
-#include <QCloseEvent>
-
 
-using std::string;
+using namespace std;
 
 namespace lyx {
 namespace frontend {
 
-GuiBoxDialog::GuiBoxDialog(LyXView & lv)
-       : GuiDialog(lv, "box")
+
+void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
+{
+       char const * const ids_[] = {
+               "Frameless", "Boxed", "ovalbox",
+               "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
+       size_t const ids_size = sizeof(ids_) / sizeof(char *);
+       ids = vector<string>(ids_, ids_ + ids_size);
+       gui_names.clear();
+       gui_names.push_back(_("No frame"));
+       gui_names.push_back(_("Simple rectangular frame"));
+       gui_names.push_back(_("Oval frame, thin"));
+       gui_names.push_back(_("Oval frame, thick"));
+       gui_names.push_back(_("Drop shadow"));
+       gui_names.push_back(_("Shaded background"));
+       gui_names.push_back(_("Double rectangular frame"));
+}
+
+
+void box_gui_tokens_special_length(vector<string> & ids,
+       vector<docstring> & gui_names)
+{
+       char const * const ids_[] = {
+               "none", "height", "depth",
+               "totalheight", "width"};
+       size_t const ids_size = sizeof(ids_) / sizeof(char *);
+       ids = vector<string>(ids_, ids_ + ids_size);
+       gui_names.clear();
+       gui_names.push_back(_("None"));
+       gui_names.push_back(_("Height"));
+       gui_names.push_back(_("Depth"));
+       gui_names.push_back(_("Total Height"));
+       gui_names.push_back(_("Width"));
+}
+
+
+GuiBox::GuiBox(GuiView & lv)
+       : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
 {
        setupUi(this);
-       setViewTitle(_("Box Settings"));
-       setController(new ControlBox(*this));
 
        // fill the box type choice
        box_gui_tokens(ids_, gui_names_);
@@ -57,11 +92,11 @@ GuiBoxDialog::GuiBoxDialog(LyXView & lv)
        connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
        connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
 
-       connect(widthED, SIGNAL(textChanged(const QString &)),
+       connect(widthED, SIGNAL(textChanged(QString)),
                this, SLOT(change_adaptor()));
        connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(change_adaptor()));
-       connect(valignCO, SIGNAL(highlighted(const QString &)),
+       connect(valignCO, SIGNAL(highlighted(QString)),
                this, SLOT(change_adaptor()));
        connect(heightCB, SIGNAL(stateChanged(int)),
                this, SLOT(change_adaptor()));
@@ -72,12 +107,13 @@ GuiBoxDialog::GuiBoxDialog(LyXView & lv)
        connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
        connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
        connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
-       connect(heightCB, SIGNAL(stateChanged(int)), this, SLOT(heightChecked(int)));
        connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
        connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
        connect(innerBoxCO, SIGNAL(activated(const QString&)),
                this, SLOT(innerBoxChanged(const QString &)));
        connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
+       connect(pagebreakCB, SIGNAL(stateChanged(int)),
+               this, SLOT(pagebreakClicked()));
 
        heightED->setValidator(unsignedLengthValidator(heightED));
        widthED->setValidator(unsignedLengthValidator(widthED));
@@ -94,6 +130,7 @@ GuiBoxDialog::GuiBoxDialog(LyXView & lv)
        bc().addReadOnly(heightCB);
        bc().addReadOnly(heightED);
        bc().addReadOnly(heightUnitsLC);
+       bc().addReadOnly(pagebreakCB);
 
        bc().setRestore(restorePB);
        bc().setOK(okPB);
@@ -106,26 +143,13 @@ GuiBoxDialog::GuiBoxDialog(LyXView & lv)
 }
 
 
-ControlBox & GuiBoxDialog::controller()
-{
-       return static_cast<ControlBox &>(GuiDialog::controller());
-}
-
-
-void GuiBoxDialog::closeEvent(QCloseEvent * e)
-{
-       slotClose();
-       e->accept();
-}
-
-
-void GuiBoxDialog::change_adaptor()
+void GuiBox::change_adaptor()
 {
        changed();
 }
 
 
-void GuiBoxDialog::innerBoxChanged(const QString & str)
+void GuiBox::innerBoxChanged(const QString & str)
 {
        bool const ibox = (str != qt_("None"));
        valignCO->setEnabled(ibox);
@@ -140,7 +164,7 @@ void GuiBoxDialog::innerBoxChanged(const QString & str)
 }
 
 
-void GuiBoxDialog::typeChanged(int index)
+void GuiBox::typeChanged(int index)
 {
        bool const frameless = (index == 0);
        if (frameless) {
@@ -152,23 +176,15 @@ void GuiBoxDialog::typeChanged(int index)
                heightUnitsLC->setEnabled(true);
                setSpecial(true);
        }
+       if (index != 1)
+               pagebreakCB->setChecked(false);
+       pagebreakCB->setEnabled(index == 1);
        int itype = innerBoxCO->currentIndex();
        setInnerType(frameless, itype);
 }
 
 
-void GuiBoxDialog::heightChecked(int checkState)
-{
-       if (checkState == Qt::Unchecked) {
-               heightED->setEnabled(false);
-               heightUnitsLC->setEnabled(false);
-       } else { 
-               heightED->setEnabled(true);
-               heightUnitsLC->setEnabled(true);
-       }
-}
-
-void GuiBoxDialog::restoreClicked()
+void GuiBox::restoreClicked()
 {
        setInnerType(true, 2);
        widthED->setText("100");
@@ -182,9 +198,34 @@ void GuiBoxDialog::restoreClicked()
 }
 
 
-void GuiBoxDialog::updateContents()
+void GuiBox::pagebreakClicked()
+{
+       bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
+       innerBoxCO->setEnabled(!pbreak);
+       if (pbreak) {
+               valignCO->setEnabled(false);
+               ialignCO->setEnabled(false);
+               halignCO->setEnabled(false);
+               heightCB->setEnabled(false);
+               heightED->setEnabled(false);
+               heightUnitsLC->setEnabled(false);
+               setSpecial(false);
+       } else
+               typeChanged(typeCO->currentIndex());
+       change_adaptor();
+}
+
+void GuiBox::updateContents()
 {
-       string type(controller().params().type);
+       string type = params_.type;
+       if (type == "Framed") {
+               pagebreakCB->setChecked(true);
+               type = "Boxed";
+       } else
+               pagebreakCB->setChecked(false);
+
+       pagebreakCB->setEnabled(type == "Boxed");
+
        for (unsigned int i = 0; i < gui_names_.size(); ++i) {
                if (type == ids_[i])
                        typeCO->setCurrentIndex(i);
@@ -192,23 +233,23 @@ void GuiBoxDialog::updateContents()
 
        // default: minipage
        unsigned int inner_type = 2;
-       if (!controller().params().inner_box)
+       if (!params_.inner_box)
                // none
                inner_type = 0;
-       if (controller().params().use_parbox)
+       if (params_.use_parbox)
                // parbox
                inner_type = 1;
-       bool frameless = (controller().params().type == "Frameless");
+       bool frameless = (params_.type == "Frameless");
        setInnerType(frameless, inner_type);
 
-       char c = controller().params().pos;
+       char c = params_.pos;
        valignCO->setCurrentIndex(string("tcb").find(c, 0));
-       c = controller().params().inner_pos;
+       c = params_.inner_pos;
        ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
-       c = controller().params().hor_pos;
+       c = params_.hor_pos;
        halignCO->setCurrentIndex(string("lcrs").find(c, 0));
 
-       bool ibox = controller().params().inner_box;
+       bool ibox = params_.inner_box;
        valignCO->setEnabled(ibox);
        ialignCO->setEnabled(ibox);
        halignCO->setEnabled(!ibox);
@@ -218,9 +259,9 @@ void GuiBoxDialog::updateContents()
                (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
 
        lengthToWidgets(widthED, widthUnitsLC,
-               (controller().params().width).asString(), default_unit);
+               (params_.width).asString(), default_unit);
 
-       string const special = controller().params().special;
+       string const special = params_.special;
        if (!special.empty() && special != "none") {
                QString spc;
                for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
@@ -234,42 +275,46 @@ void GuiBoxDialog::updateContents()
        }
 
        lengthToWidgets(heightED, heightUnitsLC,
-               (controller().params().height).asString(), default_unit);
-
-       string const height_special = controller().params().height_special;
+               (params_.height).asString(), default_unit);
+       
+       string const height_special = params_.height_special;
        if (!height_special.empty() && height_special != "none") {
                QString hspc;
-               for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
-                       if (height_special == ids_spec_[i]) {
+               for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
+                       if (height_special == ids_spec_[i])
                                hspc = toqstr(gui_names_spec_[i].c_str());
-                       }
                }
-               for (int j = 0; j < heightUnitsLC->count(); j++) {
-                       if (heightUnitsLC->itemText(j) == hspc) {
+               for (int j = 0; j != heightUnitsLC->count(); j++) {
+                       if (heightUnitsLC->itemText(j) == hspc)
                                heightUnitsLC->setCurrentIndex(j);
-                       }
                }
        }
+       // set no optional height when the value is the default "1\height"
+       // (special units like \height are handled as "in",
+       if (height_special == "totalheight" &&  
+               params_.height == Length("1in"))
+               heightCB->setCheckState(Qt::Unchecked);
+       else
+               heightCB->setCheckState(Qt::Checked);
+
        heightCB->setEnabled(ibox);
 }
 
 
-void GuiBoxDialog::applyView()
+void GuiBox::applyView()
 {
-       controller().params().type =
-               ids_[typeCO->currentIndex()];
+       bool pagebreak = pagebreakCB->isChecked();
+       if (pagebreak)
+               params_.type = "Framed";
+       else
+               params_.type = ids_[typeCO->currentIndex()];
 
-       controller().params().inner_box =
-               innerBoxCO->currentText() != qt_("None");
-       controller().params().use_parbox =
-               innerBoxCO->currentText() ==  qt_("Parbox");
+       params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
+       params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
 
-       controller().params().pos =
-               "tcb"[valignCO->currentIndex()];
-       controller().params().inner_pos =
-               "tcbs"[ialignCO->currentIndex()];
-       controller().params().hor_pos =
-               "lcrs"[halignCO->currentIndex()];
+       params_.pos = "tcb"[valignCO->currentIndex()];
+       params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
+       params_.hor_pos = "lcrs"[halignCO->currentIndex()];
 
        int i = 0;
        bool spec = false;
@@ -293,17 +338,18 @@ void GuiBoxDialog::applyView()
                i = 0;
                spec = false;
        }
-       controller().params().special = ids_spec_[i];
+       params_.special = ids_spec_[i];
 
        string width;
        if (spec) {
                width = fromqstr(value);
                // beware: bogosity! the unit is simply ignored in this case
                width += "in";
-       } else
+       } else {
                width = widgetsToLength(widthED, widthUnitsLC);
+       }
 
-       controller().params().width = Length(width);
+       params_.width = Length(width);
 
        i = 0;
        spec = false;
@@ -327,7 +373,7 @@ void GuiBoxDialog::applyView()
                i = 0;
                spec = false;
        }
-       controller().params().height_special = ids_spec_[i];
+       params_.height_special = ids_spec_[i];
 
        string height;
        if (spec  && !isValidLength(fromqstr(heightED->text()))) {
@@ -342,15 +388,15 @@ void GuiBoxDialog::applyView()
        // 1in + "Total Height" means "1\height" which is the LaTeX default when
        // no height is given
        if (heightCB->checkState() == Qt::Checked)
-               controller().params().height = Length(height);
+               params_.height = Length(height);
        else {
-               controller().params().height = Length("1in");
-               controller().params().height_special = ids_spec_[3];
+               params_.height = Length("1in");
+               params_.height_special = ids_spec_[3];
        }
 }
 
 
-void GuiBoxDialog::setSpecial(bool ibox)
+void GuiBox::setSpecial(bool ibox)
 {
        box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
        // check if the widget contains the special units
@@ -372,7 +418,7 @@ void GuiBoxDialog::setSpecial(bool ibox)
 }
 
 
-void GuiBoxDialog::setInnerType(bool frameless, int i)
+void GuiBox::setInnerType(bool frameless, int i)
 {
        // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
        // we have to remove "none" then and adjust the combo
@@ -395,6 +441,29 @@ void GuiBoxDialog::setInnerType(bool frameless, int i)
        }
 }
 
+bool GuiBox::initialiseParams(string const & data)
+{
+       InsetBoxMailer::string2params(data, params_);
+       return true;
+
+}
+
+
+void GuiBox::clearParams()
+{
+       params_ = InsetBoxParams("");
+}
+
+
+void GuiBox::dispatchParams()
+{
+       dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
+}
+
+
+Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
+
+
 } // namespace frontend
 } // namespace lyx