]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBox.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
index 93bf7b57b7ba51494045b754b3c722d9d6946ac5..087f8663a64c36e337f878d83bacda1dbba102ce 100644 (file)
@@ -3,9 +3,9 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Jürgen Vigna (Minipage stuff)
+ * \author Jürgen Vigna (Minipage stuff)
  * \author Martin Vermeer
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "GuiBox.h"
 
 #include "FuncRequest.h"
-#include "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 "insets/InsetBox.h"
 
+#include "support/gettext.h"
+#include "support/foreach.h"
 #include "support/lstrings.h"
 
 #include <QPushButton>
 #include <QLineEdit>
-#include <QCloseEvent>
 
-using std::string;
-using std::vector;
+#ifdef IN
+#undef IN
+#endif
+
+using namespace std;
 
 
 namespace lyx {
 namespace frontend {
 
+static QStringList boxGuiIds()
+{
+       return QStringList()
+               << "Frameless" << "Boxed"
+               << "ovalbox" << "Ovalbox"
+               << "Shadowbox" << "Shaded"
+               << "Doublebox";
+}
+
 
-void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
+static QStringList boxGuiNames()
 {
-       char const * const ids_[] = {
-               "Frameless", "Boxed", "ovalbox",
-               "Ovalbox", "Shadowbox", "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 drawn"));
-       gui_names.push_back(_("Rectangular box"));
-       gui_names.push_back(_("Oval box, thin"));
-       gui_names.push_back(_("Oval box, thick"));
-       gui_names.push_back(_("Shadow box"));
-       gui_names.push_back(_("Double box"));
+       return QStringList()
+               << qt_("No frame") << qt_("Simple rectangular frame")
+               << qt_("Oval frame, thin") << qt_("Oval frame, thick")
+               << qt_("Drop shadow") << qt_("Shaded background")
+               << qt_("Double rectangular frame");
 }
 
 
-void box_gui_tokens_special_length(vector<string> & ids,
-       vector<docstring> & gui_names)
+static QStringList boxGuiSpecialLengthIds()
 {
-       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"));
+       return QStringList() << "height" << "depth"
+               << "totalheight" << "width";
 }
 
 
-GuiBox::GuiBox(LyXView & lv)
-       : GuiDialog(lv, "box"), params_("")
+static QStringList boxGuiSpecialLengthNames()
+{
+       return QStringList() << qt_("Height") << qt_("Depth")
+               << qt_("Total Height") << qt_("Width");
+}
+
+
+GuiBox::GuiBox(GuiView & lv)
+       : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
 {
        setupUi(this);
-       setViewTitle(_("Box Settings"));
 
        // fill the box type choice
-       box_gui_tokens(ids_, gui_names_);
-       for (unsigned int i = 0; i < gui_names_.size(); ++i)
-               typeCO->addItem(toqstr(gui_names_[i]));
+       ids_ = boxGuiIds();
+       gui_names_ = boxGuiNames();
+       foreach (QString const & str, gui_names_)
+               typeCO->addItem(str);
 
        // add the special units to the height choice
        // width needs different handling
-       box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
-       for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
-               heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
+       ids_spec_ = boxGuiSpecialLengthIds();
+       gui_names_spec_ = boxGuiSpecialLengthNames();
+       for (int i = 0; i != ids_spec_.size(); ++i)
+               heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
 
        connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
        connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
@@ -103,18 +104,20 @@ GuiBox::GuiBox(LyXView & lv)
                this, SLOT(change_adaptor()));
        connect(heightCB, SIGNAL(stateChanged(int)),
                this, SLOT(change_adaptor()));
-       connect(heightED, SIGNAL(textChanged(const QString &)),
+       connect(heightED, SIGNAL(textChanged(QString)),
                this, SLOT(change_adaptor()));
-       connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
+       connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(change_adaptor()));
        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(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(QString)),
+               this, SLOT(innerBoxChanged(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));
@@ -131,6 +134,7 @@ GuiBox::GuiBox(LyXView & lv)
        bc().addReadOnly(heightCB);
        bc().addReadOnly(heightED);
        bc().addReadOnly(heightUnitsLC);
+       bc().addReadOnly(pagebreakCB);
 
        bc().setRestore(restorePB);
        bc().setOK(okPB);
@@ -143,30 +147,22 @@ GuiBox::GuiBox(LyXView & lv)
 }
 
 
-void GuiBox::closeEvent(QCloseEvent * e)
-{
-       slotClose();
-       e->accept();
-}
-
-
 void GuiBox::change_adaptor()
 {
        changed();
 }
 
 
-void GuiBox::innerBoxChanged(const QString & str)
+void GuiBox::innerBoxChanged(QString const & str)
 {
        bool const ibox = (str != qt_("None"));
        valignCO->setEnabled(ibox);
        ialignCO->setEnabled(ibox);
        halignCO->setEnabled(!ibox);
        heightCB->setEnabled(ibox);
-       if (heightCB->checkState() == Qt::Checked && ibox) {
-               heightED->setEnabled(true);
-               heightUnitsLC->setEnabled(true);
-       }
+       pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
+       heightED->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
+       heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
        setSpecial(ibox);
 }
 
@@ -179,11 +175,18 @@ void GuiBox::typeChanged(int index)
                ialignCO->setEnabled(true);
                halignCO->setEnabled(false);
                heightCB->setEnabled(true);
-               heightED->setEnabled(true);
-               heightUnitsLC->setEnabled(true);
+               heightED->setEnabled(heightCB->checkState() == Qt::Checked);
+               heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked);
                setSpecial(true);
        }
+       if (index != 1)
+               pagebreakCB->setChecked(false);
        int itype = innerBoxCO->currentIndex();
+       if (innerBoxCO->count() == 2)
+               ++itype;
+       pagebreakCB->setEnabled(index == 1 && itype == 0);
+       widthED->setEnabled(index != 5);
+       widthUnitsLC->setEnabled(index != 5);
        setInnerType(frameless, itype);
 }
 
@@ -195,23 +198,50 @@ void GuiBox::restoreClicked()
        widthUnitsLC->setCurrentItem(Length::PCW);
        heightCB->setCheckState(Qt::Checked);
        heightED->setText("1");
-       for (int j = 0; j < heightUnitsLC->count(); j++) {
-               if (heightUnitsLC->itemText(j) == qt_("Total Height"))
-                       heightUnitsLC->setCurrentItem(j);
+       heightUnitsLC->setCurrentItem("totalheight");
+}
+
+
+void GuiBox::pagebreakClicked()
+{
+       bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
+       innerBoxCO->setEnabled(!pbreak);
+       widthED->setEnabled(!pbreak);
+       widthUnitsLC->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 = params_.type;
-       for (unsigned int i = 0; i < gui_names_.size(); ++i) {
+       QString type = toqstr(params_.type);
+       if (type == "Framed") {
+               pagebreakCB->setChecked(true);
+               type = "Boxed";
+       } else {
+               pagebreakCB->setChecked(false);
+       }
+
+       pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
+
+       for (int i = 0; i != gui_names_.size(); ++i) {
                if (type == ids_[i])
                        typeCO->setCurrentIndex(i);
        }
 
        // default: minipage
-       unsigned int inner_type = 2;
+       int inner_type = 2;
        if (!params_.inner_box)
                // none
                inner_type = 0;
@@ -234,44 +264,24 @@ void GuiBox::updateContents()
        halignCO->setEnabled(!ibox);
        setSpecial(ibox);
 
-       Length::UNIT default_unit =
-               (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
+       Length::UNIT const default_unit = Length::defaultUnit();
 
        lengthToWidgets(widthED, widthUnitsLC,
                (params_.width).asString(), default_unit);
 
-       string const special = params_.special;
-       if (!special.empty() && special != "none") {
-               QString spc;
-               for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
-                       if (special == ids_spec_[i])
-                               spc = toqstr(gui_names_spec_[i].c_str());
-               }
-               for (int j = 0; j < widthUnitsLC->count(); j++) {
-                       if (widthUnitsLC->itemText(j) == spc)
-                               widthUnitsLC->setCurrentIndex(j);
-               }
-       }
+       QString const special = toqstr(params_.special);
+       if (!special.isEmpty() && special != "none")
+               widthUnitsLC->setCurrentItem(special);
 
        lengthToWidgets(heightED, heightUnitsLC,
                (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])
-                               hspc = toqstr(gui_names_spec_[i].c_str());
-               }
-               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"
+       QString const height_special = toqstr(params_.height_special);
+       if (!height_special.isEmpty() && height_special != "none")
+               heightUnitsLC->setCurrentItem(height_special);
+       // set no optional height if the value is the default "1\height"
        // (special units like \height are handled as "in",
-       if (height_special == "totalheight" &&  
-               params_.height == Length("1in"))
+       if (height_special == "totalheight" && params_.height == Length("1in"))
                heightCB->setCheckState(Qt::Unchecked);
        else
                heightCB->setCheckState(Qt::Checked);
@@ -282,114 +292,78 @@ void GuiBox::updateContents()
 
 void GuiBox::applyView()
 {
-       params_.type = ids_[typeCO->currentIndex()];
+       bool pagebreak =
+               pagebreakCB->isEnabled() && pagebreakCB->isChecked();
+       if (pagebreak)
+               params_.type = "Framed";
+       else
+               params_.type = fromqstr(ids_[typeCO->currentIndex()]);
 
-       params_.inner_box = innerBoxCO->currentText() != qt_("None");
-       params_.use_parbox = innerBoxCO->currentText() ==  qt_("Parbox");
+       params_.inner_box =
+               (!pagebreak && innerBoxCO->currentText() != qt_("None"));
+       params_.use_parbox =
+               (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
 
        params_.pos = "tcb"[valignCO->currentIndex()];
        params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
        params_.hor_pos = "lcrs"[halignCO->currentIndex()];
 
-       int i = 0;
-       bool spec = false;
-       QString special = widthUnitsLC->currentText();
+       QString unit =
+               widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
        QString value = widthED->text();
-       if (special == qt_("Height")) {
-               i = 1;
-               spec = true;
-       } else if (special == qt_("Depth")) {
-               i = 2;
-               spec = true;
-       } else if (special == qt_("Total Height")) {
-               i = 3;
-               spec = true;
-       } else if (special == qt_("Width")) {
-               i = 4;
-               spec = true;
-       }
-       // the user might insert a non-special value in the line edit
-       if (isValidLength(fromqstr(value))) {
-               i = 0;
-               spec = false;
-       }
-       params_.special = ids_spec_[i];
-
-       string width;
-       if (spec) {
-               width = fromqstr(value);
-               // beware: bogosity! the unit is simply ignored in this case
-               width += "in";
+       if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
+               params_.special = fromqstr(unit);
+               // Note: the unit is simply ignored in this case
+               params_.width = Length(value.toDouble(), Length::IN);
        } else {
-               width = widgetsToLength(widthED, widthUnitsLC);
-       }
-
-       params_.width = Length(width);
-
-       i = 0;
-       spec = false;
-       special = heightUnitsLC->currentText();
-       value = heightED->text();
-       if (special == qt_("Height")) {
-               i = 1;
-               spec = true;
-       } else if (special == qt_("Depth")) {
-               i = 2;
-               spec = true;
-       } else if (special == qt_("Total Height")) {
-               i = 3;
-               spec = true;
-       } else if (special == qt_("Width")) {
-               i = 4;
-               spec = true;
+               params_.special = "none";
+               params_.width = Length(widgetsToLength(widthED, widthUnitsLC));
        }
-       // the user might insert a non-special value in the line edit
-       if (isValidLength(fromqstr(value))) {
-               i = 0;
-               spec = false;
-       }
-       params_.height_special = ids_spec_[i];
-
-       string height;
-       if (spec  && !isValidLength(fromqstr(heightED->text()))) {
-               height = fromqstr(value);
-               // beware: bogosity! the unit is simply ignored in this case
-               height += "in";
-       } else
-               height = widgetsToLength(heightED, heightUnitsLC);
 
-       // the height parameter is omitted in InsetBox.cpp when the value
+       // the height parameter is omitted if the value
        // is "1in" and "Total Height" is used as unit.
-       // 1in + "Total Height" means "1\height" which is the LaTeX default when
-       // no height is given
-       if (heightCB->checkState() == Qt::Checked)
-               params_.height = Length(height);
-       else {
+       // 1in + "Total Height" means "1\height" which is the LaTeX default
+       // if no height is given
+       if (heightCB->checkState() == Qt::Unchecked) {
                params_.height = Length("1in");
-               params_.height_special = ids_spec_[3];
+               params_.height_special = "totalheight";
+       } else {
+               unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
+               value = heightED->text();
+               if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
+                       params_.height_special = fromqstr(unit);
+                       // Note: the unit is simply ignored in this case
+                       params_.height = Length(value.toDouble(), Length::IN);
+               } else {
+                       params_.height_special = "none";
+                       params_.height =
+                               Length(widgetsToLength(heightED, heightUnitsLC));
+               }
        }
 }
 
 
 void GuiBox::setSpecial(bool ibox)
 {
-       box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
+       QString const last_item =
+               widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
+
        // check if the widget contains the special units
-       int count = widthUnitsLC->count();
-       bool has_special = false;
-       for (int i = 0; i < count; i++)
-               if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
-                       has_special = true;
+       bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
        // insert 'em if needed...
        if (!ibox && !has_special) {
-               for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
-                       widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
+               for (int i = 1; i < ids_spec_.size(); ++i)
+                       widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
        // ... or remove 'em if needed
        } else if (ibox && has_special) {
-               widthUnitsLC->clear();
-               for (int i = 0; i < num_units; i++)
-                       widthUnitsLC->addItem(qt_(unit_name_gui[i]));
+               for (int i = 1; i < ids_spec_.size(); ++i) {
+                       int n = widthUnitsLC->findData(ids_spec_[i]);
+                       if (n != -1)
+                               widthUnitsLC->removeItem(n);
+               }
        }
+       // restore selected text, if possible
+       widthUnitsLC->setCurrentItem(last_item);
 }
 
 
@@ -406,8 +380,6 @@ void GuiBox::setInnerType(bool frameless, int i)
                else
                        innerBoxCO->setCurrentIndex(i);
        } else {
-               if (innerBoxCO->count() == 2)
-                       ++i;
                innerBoxCO->clear();
                innerBoxCO->addItem(qt_("None"));
                innerBoxCO->addItem(qt_("Parbox"));
@@ -418,9 +390,8 @@ void GuiBox::setInnerType(bool frameless, int i)
 
 bool GuiBox::initialiseParams(string const & data)
 {
-       InsetBoxMailer::string2params(data, params_);
+       InsetBox::string2params(data, params_);
        return true;
-
 }
 
 
@@ -432,15 +403,15 @@ void GuiBox::clearParams()
 
 void GuiBox::dispatchParams()
 {
-       dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
+       dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
 }
 
 
-Dialog * createGuiBox(LyXView & lv) { return new GuiBox(lv); }
+Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
 
 
 } // namespace frontend
 } // namespace lyx
 
 
-#include "GuiBox_moc.cpp"
+#include "moc_GuiBox.cpp"