]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiTabular.cpp
Fix reloading of local layout file (bug #11120)
[lyx.git] / src / frontends / qt4 / GuiTabular.cpp
index 5b70ba129ada7e0d3d08a2cee469ffb4ad3569b0..63cd3836c0c67d19b8c8a7f1129e9b55acec691c 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "support/convert.h"
 #include "support/debug.h"
+#include "support/lstrings.h"
 
 #include <QCheckBox>
 #include <QPushButton>
@@ -45,7 +46,9 @@ namespace lyx {
 namespace frontend {
 
 GuiTabular::GuiTabular(QWidget * parent)
-       : InsetParamsWidget(parent)
+       : InsetParamsWidget(parent), firstheader_suppressable_(false),
+         lastfooter_suppressable_(false)
+
 {
        setupUi(this);
 
@@ -127,19 +130,19 @@ GuiTabular::GuiTabular(QWidget * parent)
                this, SLOT(checkEnabled()));
        connect(captionStatusCB, SIGNAL(clicked()),
                this, SLOT(checkEnabled()));
-       connect(specialAlignmentED, SIGNAL(editingFinished()),
+       connect(specialAlignmentED, SIGNAL(textEdited(const QString &)),
                this, SLOT(checkEnabled()));
        connect(columnWidthED, SIGNAL(textEdited(const QString &)),
                this, SLOT(checkEnabled()));
        connect(columnWidthUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(topSet(bool)),
+       connect(borders, SIGNAL(topSet()),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(bottomSet(bool)),
+       connect(borders, SIGNAL(bottomSet()),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(rightSet(bool)),
+       connect(borders, SIGNAL(rightSet()),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(leftSet(bool)),
+       connect(borders, SIGNAL(leftSet()),
                this, SLOT(checkEnabled()));
        connect(rotateTabularCB, SIGNAL(clicked()),
                this, SLOT(checkEnabled()));
@@ -199,11 +202,12 @@ void GuiTabular::on_interlinespaceCO_activated(int index)
 }
 
 
-void GuiTabular::checkEnabled()
+void GuiTabular::enableWidgets() const
 {
        // if there is a LaTeX argument, the width and alignment will be overwritten
        // therefore disable them in this case
        columnWidthED->setEnabled(specialAlignmentED->text().isEmpty());
+       columnWidthUnitLC->setEnabled(specialAlignmentED->text().isEmpty());
        // if the column has a width, multirows are always left-aligned
        // therefore disable hAlignCB in this case
        hAlignCO->setEnabled(!(multirowCB->isChecked()
@@ -211,12 +215,12 @@ void GuiTabular::checkEnabled()
                && specialAlignmentED->text().isEmpty());
        // decimal alignment is only possible for non-multicol and non-multirow cells
        if ((multicolumnCB->isChecked() || multirowCB->isChecked())
-               && hAlignCO->findText(qt_("At Decimal Separator")))
-               hAlignCO->removeItem(hAlignCO->findText(qt_("At Decimal Separator")));
+               && hAlignCO->findData(toqstr("decimal")))
+               hAlignCO->removeItem(hAlignCO->findData(toqstr("decimal")));
        else if (!multicolumnCB->isChecked() && !multirowCB->isChecked()
-               && hAlignCO->findText(qt_("At Decimal Separator")) == -1)
+               && hAlignCO->findData(toqstr("decimal")) == -1)
                hAlignCO->addItem(qt_("At Decimal Separator"), toqstr("decimal"));
-       bool const dalign = 
+       bool const dalign =
                hAlignCO->itemData(hAlignCO->currentIndex()).toString() == QString("decimal");
        decimalPointED->setEnabled(dalign);
        decimalLA->setEnabled(dalign);
@@ -227,7 +231,8 @@ void GuiTabular::checkEnabled()
        tabularWidthED->setEnabled(setwidth);
        tabularWidthUnitLC->setEnabled(setwidth);
 
-       rotateTabularAngleSB->setEnabled(rotateTabularCB->isChecked());
+       rotateTabularAngleSB->setEnabled(rotateTabularCB->isChecked()
+                                        && !longTabularCB->isChecked());
        rotateCellAngleSB->setEnabled(rotateCellCB->isChecked());
 
        bool const enable_valign =
@@ -255,6 +260,9 @@ void GuiTabular::checkEnabled()
        // longtables and tabular* cannot have a vertical alignment
        TableAlignLA->setDisabled(is_tabular_star || longtabular);
        TableAlignCO->setDisabled(is_tabular_star || longtabular);
+       // longtable cannot be rotated with rotating package, only
+       // with [pdf]lscape, which only supports 90 deg.
+       rotateTabularAngleSB->setDisabled(longtabular);
 
        // FIXME: This Dialog is really horrible, disabling/enabling a checkbox
        // depending on the cursor position is very very unintuitive...
@@ -320,16 +328,25 @@ void GuiTabular::checkEnabled()
        multirowOffsetED->setEnabled(enable_mr);
        multirowOffsetUnitLC->setEnabled(enable_mr);
 
+       // Vertical lines cannot be set in formal tables
+       borders->setLeftEnabled(!booktabsRB->isChecked());
+       borders->setRightEnabled(!booktabsRB->isChecked());
+}
+
+
+void GuiTabular::checkEnabled()
+{
+       enableWidgets();
        changed();
 }
 
 
 void GuiTabular::borderSet_clicked()
 {
-       borders->setTop(true);
-       borders->setBottom(true);
-       borders->setLeft(true);
-       borders->setRight(true);
+       borders->setTop(GuiSetBorder::LINE_SET);
+       borders->setBottom(GuiSetBorder::LINE_SET);
+       borders->setLeft(GuiSetBorder::LINE_SET);
+       borders->setRight(GuiSetBorder::LINE_SET);
        // repaint the setborder widget
        borders->update();
        checkEnabled();
@@ -338,10 +355,10 @@ void GuiTabular::borderSet_clicked()
 
 void GuiTabular::borderUnset_clicked()
 {
-       borders->setTop(false);
-       borders->setBottom(false);
-       borders->setLeft(false);
-       borders->setRight(false);
+       borders->setTop(GuiSetBorder::LINE_UNSET);
+       borders->setBottom(GuiSetBorder::LINE_UNSET);
+       borders->setLeft(GuiSetBorder::LINE_UNSET);
+       borders->setRight(GuiSetBorder::LINE_UNSET);
        // repaint the setborder widget
        borders->update();
        checkEnabled();
@@ -437,8 +454,7 @@ void GuiTabular::setTableAlignment(string & param_str) const
 
 docstring GuiTabular::dialogToParams() const
 {
-       // FIXME: We should use Tabular directly.
-       string param_str = "tabular from-dialog";
+       string param_str = "tabular";
 
        // table width
        string tabwidth = widgetsToLength(tabularWidthED, tabularWidthUnitLC);
@@ -526,26 +542,30 @@ docstring GuiTabular::dialogToParams() const
        }
 
        //
-       if (borders->getTop() && borders->getBottom() && borders->getLeft()
-               && borders->getRight())
+       if (borders->topLineSet() && borders->bottomLineSet() && borders->leftLineSet()
+               && borders->rightLineSet())
                setParam(param_str, Tabular::SET_ALL_LINES);
-       else if (!borders->getTop() && !borders->getBottom() && !borders->getLeft()
-               && !borders->getRight())
+       else if (borders->topLineUnset() && borders->bottomLineUnset() && borders->leftLineUnset()
+               && borders->rightLineUnset())
                setParam(param_str, Tabular::UNSET_ALL_LINES);
        else {
-               setParam(param_str, Tabular::SET_LINE_LEFT,
-                        borders->getLeft() ? "true" : "false");
-               setParam(param_str, Tabular::SET_LINE_RIGHT,
-                        borders->getRight() ? "true" : "false");
-               setParam(param_str, Tabular::SET_LINE_TOP,
-                        borders->getTop() ? "true" : "false");
-               setParam(param_str, Tabular::SET_LINE_BOTTOM,
-                        borders->getBottom() ? "true" : "false");
+               if (borders->getLeft() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_LEFT,
+                                borders->leftLineSet() ? "true" : "false");
+               if (borders->getRight() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_RIGHT,
+                                borders->rightLineSet() ? "true" : "false");
+               if (borders->getTop() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_TOP,
+                                borders->topLineSet() ? "true" : "false");
+               if (borders->getBottom() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_BOTTOM,
+                                borders->bottomLineSet() ? "true" : "false");
        }
 
        // apply the special alignment
        string special = fromqstr(specialAlignmentED->text());
-       if (special.empty())
+       if (support::trim(special).empty())
                special = "none";
        if (multicolumnCB->isChecked())
                setParam(param_str, Tabular::SET_SPECIAL_MULTICOLUMN, special);
@@ -697,6 +717,18 @@ static docstring getAlignSpecial(Tabular const & t, size_t cell, int what)
 }
 
 
+GuiSetBorder::BorderState GuiTabular::borderState(GuiSetBorder::BorderState bs,
+                                                 bool const line)
+{
+       if (bs == GuiSetBorder::LINE_UNDEF)
+               bs = line ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+       else if ((bs == GuiSetBorder::LINE_SET && !line)
+                || (bs == GuiSetBorder::LINE_UNSET && line))
+               bs = GuiSetBorder::LINE_UNDECIDED;
+       return bs;
+}
+
+
 void GuiTabular::paramsToDialog(Inset const * inset)
 {
        InsetTabular const * itab = static_cast<InsetTabular const *>(inset);
@@ -726,16 +758,52 @@ void GuiTabular::paramsToDialog(Inset const * inset)
                        rotateCellAngleSB->setValue(90);
        }
 
-       rotateTabularCB->setChecked(tabular.rotate != 0);
-       if (rotateTabularCB->isChecked())
-               rotateTabularAngleSB->setValue(tabular.rotate != 0 ? tabular.rotate : 90);
-
        longTabularCB->setChecked(tabular.is_long_tabular);
 
-       borders->setTop(tabular.topLine(cell));
-       borders->setBottom(tabular.bottomLine(cell));
-       borders->setLeft(tabular.leftLine(cell));
-       borders->setRight(tabular.rightLine(cell));
+       rotateTabularCB->setChecked(tabular.rotate != 0);
+       if (rotateTabularCB->isChecked()) {
+               if (longTabularCB->isChecked())
+                       rotateTabularAngleSB->setValue(90);
+               else
+                       rotateTabularAngleSB->setValue(tabular.rotate != 0 ? tabular.rotate : 90);
+       }
+
+       // In what follows, we check the borders of all selected cells,
+       // and if there are diverging settings, we use the LINE_UNDECIDED
+       // border status.
+       GuiSetBorder::BorderState lt = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lb = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState ll = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lr = GuiSetBorder::LINE_UNDEF;
+       CursorSlice const & beg = bv->cursor().selBegin();
+       CursorSlice const & end = bv->cursor().selEnd();
+       if (beg != end) {
+               Tabular::col_type cs = tabular.cellColumn(beg.idx());
+               Tabular::col_type ce = tabular.cellColumn(end.idx());
+               if (cs > ce)
+                       swap(cs, ce);
+               Tabular::row_type rs = tabular.cellRow(beg.idx());
+               Tabular::row_type re = tabular.cellRow(end.idx());
+               if (rs > re)
+                       swap(rs, re);
+               for (Tabular::row_type r = rs; r <= re; ++r)
+                       for (Tabular::col_type c = cs; c <= ce; ++c) {
+                               idx_type const cc = tabular.cellIndex(r, c);
+                               lt = borderState(lt, tabular.topLine(cc));
+                               lb = borderState(lb, tabular.bottomLine(cc));
+                               ll = borderState(ll, tabular.leftLine(cc));
+                               lr = borderState(lr, tabular.rightLine(cc));
+                       }
+       } else {
+               lt = tabular.topLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               lb = tabular.bottomLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               ll = tabular.leftLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               lr = tabular.rightLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+       }
+       borders->setTop(lt);
+       borders->setBottom(lb);
+       borders->setLeft(ll);
+       borders->setRight(lr);
        // repaint the setborder widget
        borders->update();
 
@@ -745,7 +813,8 @@ void GuiTabular::paramsToDialog(Inset const * inset)
        // Set width and alignment
 
        Length const tabwidth = tabular.tabularWidth();
-       if (tabwidth.zero())
+       if (tabwidth.zero()
+           && !(tabularWidthED->hasFocus() && tabularWidthED->text() == "0"))
                tabularWidthED->clear();
        else
                lengthToWidgets(tabularWidthED, tabularWidthUnitLC,
@@ -763,7 +832,8 @@ void GuiTabular::paramsToDialog(Inset const * inset)
                pwidth = getColumnPWidth(tabular, cell);
        }
        string colwidth;
-       if (pwidth.zero())
+       if (pwidth.zero()
+           && !(columnWidthED->hasFocus() && columnWidthED->text() == "0"))
                columnWidthED->clear();
        else {
                colwidth = pwidth.asString();
@@ -774,7 +844,8 @@ void GuiTabular::paramsToDialog(Inset const * inset)
        if (multirow)
                mroffset = getMROffset(tabular, cell);
        string offset;
-       if (mroffset.zero())
+       if (mroffset.zero()
+           && !(multirowOffsetED->hasFocus() && multirowOffsetED->text() == "0"))
                multirowOffsetED->clear();
        else {
                offset = mroffset.asString();
@@ -1011,10 +1082,54 @@ void GuiTabular::paramsToDialog(Inset const * inset)
 }
 
 
+bool GuiTabular::checkWidgets(bool readonly) const
+{
+       tabularRowED->setReadOnly(readonly);
+       tabularColumnED->setReadOnly(readonly);
+       tabularWidthED->setReadOnly(readonly);
+       specialAlignmentED->setReadOnly(readonly);
+       columnWidthED->setReadOnly(readonly);
+       multirowOffsetED->setReadOnly(readonly);
+       decimalPointED->setReadOnly(readonly);
+
+       if (readonly) {
+               multicolumnCB->setEnabled(false);
+               multirowCB->setEnabled(false);
+               rotateCellCB->setEnabled(false);
+               rotateCellAngleSB->setEnabled(false);
+               rotateTabularCB->setEnabled(false);
+               rotateTabularAngleSB->setEnabled(false);
+               longTabularCB->setEnabled(false);
+               borders->setEnabled(false);
+               tabularWidthUnitLC->setEnabled(false);
+               columnWidthUnitLC->setEnabled(false);
+               multirowOffsetUnitLC->setEnabled(false);
+               setBordersGB->setEnabled(false);
+               allBordersGB->setEnabled(false);
+               borderStyleGB->setEnabled(false);
+               booktabsRB->setEnabled(false);
+               topspaceCO->setEnabled(false);
+               topspaceUnitLC->setEnabled(false);
+               bottomspaceCO->setEnabled(false);
+               bottomspaceUnitLC->setEnabled(false);
+               interlinespaceCO->setEnabled(false);
+               interlinespaceUnitLC->setEnabled(false);
+               hAlignCO->setEnabled(false);
+               vAlignCO->setEnabled(false);
+               TableAlignCO->setEnabled(false);
+               longtableGB->setEnabled(false);
+               alignmentGB->setEnabled(false);
+       } else
+               enableWidgets();
+
+       return InsetParamsWidget::checkWidgets();
+}
+
+
 bool GuiTabular::funcEnabled(Tabular::Feature f) const
 {
-       string cmd = "tabular " + featureAsString(f);
-       return getStatus(FuncRequest(LFUN_INSET_MODIFY, cmd)).enabled();
+       FuncRequest r(LFUN_INSET_MODIFY, "tabular for-dialog" + featureAsString(f));
+       return getStatus(r).enabled();
 }