Implement tri-state in GuiSetBorder

A new UNDECIDED state is used if multiple cells with differing border
settings are selected.

This prevents additional borders to be set without being asked.

Fixes: #10010
This commit is contained in:
Juergen Spitzmueller 2018-07-24 13:49:27 +02:00
parent 1883c4346d
commit 99aefa5fd2
4 changed files with 189 additions and 72 deletions

View File

@ -72,29 +72,29 @@ void GuiSetBorder::mousePressEvent(QMouseEvent * e)
if (e->y() > e->x()) { if (e->y() > e->x()) {
if (e->y() < height() - e->x()) { if (e->y() < height() - e->x()) {
if (left_.enabled) { if (left_.enabled) {
setLeft(!left_.set); setLeft(left_.set == LINE_SET ? LINE_UNSET : LINE_SET);
// emit signal // emit signal
leftSet(left_.set); leftSet();
} }
} else { } else {
if (bottom_.enabled) { if (bottom_.enabled) {
setBottom(!bottom_.set); setBottom(bottom_.set == LINE_SET ? LINE_UNSET : LINE_SET);
// emit signal // emit signal
bottomSet(bottom_.set); bottomSet();
} }
} }
} else { } else {
if (e->y() < height() - e->x()) { if (e->y() < height() - e->x()) {
if (top_.enabled) { if (top_.enabled) {
setTop(!top_.set); setTop(top_.set == LINE_SET ? LINE_UNSET : LINE_SET);
// emit signal // emit signal
topSet(top_.set); topSet();
} }
} else { } else {
if (right_.enabled) { if (right_.enabled) {
setRight(!right_.set); setRight(right_.set == LINE_SET ? LINE_UNSET : LINE_SET);
// emit signal // emit signal
rightSet(right_.set); rightSet();
} }
} }
} }
@ -115,36 +115,84 @@ void GuiSetBorder::drawLine(QColor const & col, int x, int y, int x2, int y2)
} }
void GuiSetBorder::drawLeft(bool draw) void GuiSetBorder::drawLeft(BorderState draw)
{ {
QColor col(draw ? Qt::black : Qt::white); QColor col;
switch (draw) {
case LINE_SET:
col = Qt::black;
break;
case LINE_UNSET:
col = Qt::white;
break;
case LINE_UNDECIDED:
case LINE_UNDEF:
col = Qt::lightGray;
break;
}
if (!left_.enabled) if (!left_.enabled)
col = QColor(Qt::lightGray); col = QColor(Qt::lightGray);
drawLine(col, m + l, m + l + 2, m + l, h - m - l - 1); drawLine(col, m + l, m + l + 2, m + l, h - m - l - 1);
} }
void GuiSetBorder::drawRight(bool draw) void GuiSetBorder::drawRight(BorderState draw)
{ {
QColor col(draw ? Qt::black : Qt::white); QColor col;
switch (draw) {
case LINE_SET:
col = Qt::black;
break;
case LINE_UNSET:
col = Qt::white;
break;
case LINE_UNDECIDED:
case LINE_UNDEF:
col = Qt::lightGray;
break;
}
if (!right_.enabled) if (!right_.enabled)
col = QColor(Qt::lightGray); col = QColor(Qt::lightGray);
drawLine(col, h - m - l + 1, m + l + 2, h - m - l + 1, h - m - l - 1); drawLine(col, h - m - l + 1, m + l + 2, h - m - l + 1, h - m - l - 1);
} }
void GuiSetBorder::drawTop(bool draw) void GuiSetBorder::drawTop(BorderState draw)
{ {
QColor col(draw ? Qt::black : Qt::white); QColor col;
switch (draw) {
case LINE_SET:
col = Qt::black;
break;
case LINE_UNSET:
col = Qt::white;
break;
case LINE_UNDECIDED:
case LINE_UNDEF:
col = Qt::lightGray;
break;
}
if (!top_.enabled) if (!top_.enabled)
col = QColor(Qt::lightGray); col = QColor(Qt::lightGray);
drawLine(col, m + l + 2, m + l, w - m - l - 1, m + l); drawLine(col, m + l + 2, m + l, w - m - l - 1, m + l);
} }
void GuiSetBorder::drawBottom(bool draw) void GuiSetBorder::drawBottom(BorderState draw)
{ {
QColor col(draw ? Qt::black : Qt::white); QColor col;
switch (draw) {
case LINE_SET:
col = Qt::black;
break;
case LINE_UNSET:
col = Qt::white;
break;
case LINE_UNDECIDED:
case LINE_UNDEF:
col = Qt::lightGray;
break;
}
if (!bottom_.enabled) if (!bottom_.enabled)
col = QColor(Qt::lightGray); col = QColor(Qt::lightGray);
drawLine(col, m + l + 2, w - m - l + 1, w - m - l - 1, w - m - l + 1); drawLine(col, m + l + 2, w - m - l + 1, w - m - l - 1, w - m - l + 1);
@ -179,35 +227,35 @@ void GuiSetBorder::setBottomEnabled(bool enabled)
} }
void GuiSetBorder::setLeft(bool border) void GuiSetBorder::setLeft(BorderState border)
{ {
left_.set = border; left_.set = border;
drawLeft(border); drawLeft(border);
} }
void GuiSetBorder::setRight(bool border) void GuiSetBorder::setRight(BorderState border)
{ {
right_.set = border; right_.set = border;
drawRight(border); drawRight(border);
} }
void GuiSetBorder::setTop(bool border) void GuiSetBorder::setTop(BorderState border)
{ {
top_.set = border; top_.set = border;
drawTop(border); drawTop(border);
} }
void GuiSetBorder::setBottom(bool border) void GuiSetBorder::setBottom(BorderState border)
{ {
bottom_.set = border; bottom_.set = border;
drawBottom(border); drawBottom(border);
} }
void GuiSetBorder::setAll(bool border) void GuiSetBorder::setAll(BorderState border)
{ {
setLeft(border); setLeft(border);
setRight(border); setRight(border);
@ -216,25 +264,25 @@ void GuiSetBorder::setAll(bool border)
} }
bool GuiSetBorder::getLeft() GuiSetBorder::BorderState GuiSetBorder::getLeft()
{ {
return left_.set; return left_.set;
} }
bool GuiSetBorder::getRight() GuiSetBorder::BorderState GuiSetBorder::getRight()
{ {
return right_.set; return right_.set;
} }
bool GuiSetBorder::getTop() GuiSetBorder::BorderState GuiSetBorder::getTop()
{ {
return top_.set; return top_.set;
} }
bool GuiSetBorder::getBottom() GuiSetBorder::BorderState GuiSetBorder::getBottom()
{ {
return bottom_.set; return bottom_.set;
} }

View File

@ -28,16 +28,34 @@ class GuiSetBorder : public QWidget
public: public:
GuiSetBorder(QWidget * parent = 0, Qt::WindowFlags fl = 0); GuiSetBorder(QWidget * parent = 0, Qt::WindowFlags fl = 0);
bool getLeft(); // We need tristate for multi-cell selection
bool getRight(); enum BorderState {
bool getTop(); LINE_UNSET,
bool getBottom(); LINE_SET,
LINE_UNDECIDED,
LINE_UNDEF
};
BorderState getLeft();
BorderState getRight();
BorderState getTop();
BorderState getBottom();
bool leftLineSet() { return getLeft() == LINE_SET; }
bool rightLineSet() { return getRight() == LINE_SET; }
bool topLineSet() { return getTop() == LINE_SET; }
bool bottomLineSet() { return getBottom() == LINE_SET; }
bool leftLineUnset() { return getLeft() == LINE_UNSET; }
bool rightLineUnset() { return getRight() == LINE_UNSET; }
bool topLineUnset() { return getTop() == LINE_UNSET; }
bool bottomLineUnset() { return getBottom() == LINE_UNSET; }
Q_SIGNALS: Q_SIGNALS:
void rightSet(bool); void rightSet();
void leftSet(bool); void leftSet();
void topSet(bool); void topSet();
void bottomSet(bool); void bottomSet();
void clicked(); void clicked();
public Q_SLOTS: public Q_SLOTS:
@ -45,11 +63,11 @@ public Q_SLOTS:
void setRightEnabled(bool); void setRightEnabled(bool);
void setTopEnabled(bool); void setTopEnabled(bool);
void setBottomEnabled(bool); void setBottomEnabled(bool);
void setLeft(bool); void setLeft(BorderState);
void setRight(bool); void setRight(BorderState);
void setTop(bool); void setTop(BorderState);
void setBottom(bool); void setBottom(BorderState);
void setAll(bool); void setAll(BorderState);
protected: protected:
void mousePressEvent(QMouseEvent * e); void mousePressEvent(QMouseEvent * e);
@ -60,15 +78,15 @@ private:
void drawLine(QColor const & col, int x, int y, int x2, int y2); void drawLine(QColor const & col, int x, int y, int x2, int y2);
void drawLeft(bool); void drawLeft(BorderState);
void drawRight(bool); void drawRight(BorderState);
void drawTop(bool); void drawTop(BorderState);
void drawBottom(bool); void drawBottom(BorderState);
class Border { class Border {
public: public:
Border() : set(true), enabled(true) {} Border() : set(LINE_SET), enabled(true) {}
bool set; BorderState set;
bool enabled; bool enabled;
}; };

View File

@ -136,13 +136,13 @@ GuiTabular::GuiTabular(QWidget * parent)
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
connect(columnWidthUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)), connect(columnWidthUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
connect(borders, SIGNAL(topSet(bool)), connect(borders, SIGNAL(topSet()),
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
connect(borders, SIGNAL(bottomSet(bool)), connect(borders, SIGNAL(bottomSet()),
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
connect(borders, SIGNAL(rightSet(bool)), connect(borders, SIGNAL(rightSet()),
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
connect(borders, SIGNAL(leftSet(bool)), connect(borders, SIGNAL(leftSet()),
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
connect(rotateTabularCB, SIGNAL(clicked()), connect(rotateTabularCB, SIGNAL(clicked()),
this, SLOT(checkEnabled())); this, SLOT(checkEnabled()));
@ -352,10 +352,10 @@ void GuiTabular::checkEnabled()
void GuiTabular::borderSet_clicked() void GuiTabular::borderSet_clicked()
{ {
borders->setTop(true); borders->setTop(GuiSetBorder::LINE_SET);
borders->setBottom(true); borders->setBottom(GuiSetBorder::LINE_SET);
borders->setLeft(true); borders->setLeft(GuiSetBorder::LINE_SET);
borders->setRight(true); borders->setRight(GuiSetBorder::LINE_SET);
// repaint the setborder widget // repaint the setborder widget
borders->update(); borders->update();
checkEnabled(); checkEnabled();
@ -364,10 +364,10 @@ void GuiTabular::borderSet_clicked()
void GuiTabular::borderUnset_clicked() void GuiTabular::borderUnset_clicked()
{ {
borders->setTop(false); borders->setTop(GuiSetBorder::LINE_UNSET);
borders->setBottom(false); borders->setBottom(GuiSetBorder::LINE_UNSET);
borders->setLeft(false); borders->setLeft(GuiSetBorder::LINE_UNSET);
borders->setRight(false); borders->setRight(GuiSetBorder::LINE_UNSET);
// repaint the setborder widget // repaint the setborder widget
borders->update(); borders->update();
checkEnabled(); checkEnabled();
@ -558,21 +558,25 @@ docstring GuiTabular::dialogToParams() const
} }
// //
if (borders->getTop() && borders->getBottom() && borders->getLeft() if (borders->topLineSet() && borders->bottomLineSet() && borders->leftLineSet()
&& borders->getRight()) && borders->rightLineSet())
setParam(param_str, Tabular::SET_ALL_LINES); setParam(param_str, Tabular::SET_ALL_LINES);
else if (!borders->getTop() && !borders->getBottom() && !borders->getLeft() else if (borders->topLineUnset() && borders->bottomLineUnset() && borders->leftLineUnset()
&& !borders->getRight()) && borders->rightLineUnset())
setParam(param_str, Tabular::UNSET_ALL_LINES); setParam(param_str, Tabular::UNSET_ALL_LINES);
else { else {
if (borders->getLeft() != GuiSetBorder::LINE_UNDECIDED)
setParam(param_str, Tabular::SET_LINE_LEFT, setParam(param_str, Tabular::SET_LINE_LEFT,
borders->getLeft() ? "true" : "false"); borders->leftLineSet() ? "true" : "false");
if (borders->getRight() != GuiSetBorder::LINE_UNDECIDED)
setParam(param_str, Tabular::SET_LINE_RIGHT, setParam(param_str, Tabular::SET_LINE_RIGHT,
borders->getRight() ? "true" : "false"); borders->rightLineSet() ? "true" : "false");
if (borders->getTop() != GuiSetBorder::LINE_UNDECIDED)
setParam(param_str, Tabular::SET_LINE_TOP, setParam(param_str, Tabular::SET_LINE_TOP,
borders->getTop() ? "true" : "false"); borders->topLineSet() ? "true" : "false");
if (borders->getBottom() != GuiSetBorder::LINE_UNDECIDED)
setParam(param_str, Tabular::SET_LINE_BOTTOM, setParam(param_str, Tabular::SET_LINE_BOTTOM,
borders->getBottom() ? "true" : "false"); borders->bottomLineSet() ? "true" : "false");
} }
// apply the special alignment // apply the special alignment
@ -729,6 +733,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) void GuiTabular::paramsToDialog(Inset const * inset)
{ {
InsetTabular const * itab = static_cast<InsetTabular const *>(inset); InsetTabular const * itab = static_cast<InsetTabular const *>(inset);
@ -768,10 +784,42 @@ void GuiTabular::paramsToDialog(Inset const * inset)
rotateTabularAngleSB->setValue(tabular.rotate != 0 ? tabular.rotate : 90); rotateTabularAngleSB->setValue(tabular.rotate != 0 ? tabular.rotate : 90);
} }
borders->setTop(tabular.topLine(cell)); // In what follows, we check the borders of all selected cells,
borders->setBottom(tabular.bottomLine(cell)); // and if there are diverging settings, we use the LINE_UNDECIDED
borders->setLeft(tabular.leftLine(cell)); // border status.
borders->setRight(tabular.rightLine(cell)); 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 // repaint the setborder widget
borders->update(); borders->update();

View File

@ -62,6 +62,9 @@ private:
/// ///
bool funcEnabled(Tabular::Feature f) const; bool funcEnabled(Tabular::Feature f) const;
/// ///
GuiSetBorder::BorderState borderState(GuiSetBorder::BorderState bs,
bool const line);
///
bool firstheader_suppressable_; bool firstheader_suppressable_;
/// ///
bool lastfooter_suppressable_; bool lastfooter_suppressable_;