]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiTabular.cpp
Handle correctly zero table special arguments.
[lyx.git] / src / frontends / qt4 / GuiTabular.cpp
1 /**
2  * \file GuiTabular.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  * \author Herbert Voß
9  * \author Uwe Stöhr
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiTabular.h"
17
18 #include "GuiApplication.h"
19 #include "GuiSetBorder.h"
20 #include "GuiView.h"
21 #include "LengthCombo.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
24
25 #include "BufferView.h"
26 #include "Cursor.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "LyX.h"
30 #include "LyXRC.h"
31
32 #include "insets/InsetTabular.h"
33
34 #include "support/convert.h"
35 #include "support/debug.h"
36 #include "support/lstrings.h"
37
38 #include <QCheckBox>
39 #include <QPushButton>
40 #include <QRadioButton>
41 #include <QLineEdit>
42
43 using namespace std;
44
45 namespace lyx {
46 namespace frontend {
47
48 GuiTabular::GuiTabular(QWidget * parent)
49         : InsetParamsWidget(parent), firstheader_suppressable_(false),
50           lastfooter_suppressable_(false)
51
52 {
53         setupUi(this);
54
55         tabularWidthED->setValidator(unsignedLengthValidator(tabularWidthED));
56         columnWidthED->setValidator(unsignedLengthValidator(columnWidthED));
57         multirowOffsetED->setValidator(new LengthValidator(multirowOffsetED));
58         topspaceED->setValidator(new LengthValidator(topspaceED));
59         bottomspaceED->setValidator(new LengthValidator(bottomspaceED));
60         interlinespaceED->setValidator(new LengthValidator(interlinespaceED));
61
62         tabularWidthUnitLC->setCurrentItem(Length::defaultUnit());
63         columnWidthUnitLC->setCurrentItem(Length::defaultUnit());
64         multirowOffsetUnitLC->setCurrentItem(Length::defaultUnit());
65         topspaceUnitLC->setCurrentItem(Length::defaultUnit());
66         bottomspaceUnitLC->setCurrentItem(Length::defaultUnit());
67         interlinespaceUnitLC->setCurrentItem(Length::defaultUnit());
68
69         connect(topspaceED, SIGNAL(textEdited(const QString &)),
70                 this, SLOT(checkEnabled()));
71         connect(topspaceUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
72                 this, SLOT(checkEnabled()));
73         connect(bottomspaceED, SIGNAL(textEdited(const QString &)),
74                 this, SLOT(checkEnabled()));
75         connect(bottomspaceUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
76                 this, SLOT(checkEnabled()));
77         connect(interlinespaceED, SIGNAL(textEdited(const QString &)),
78                 this, SLOT(checkEnabled()));
79         connect(interlinespaceUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
80                 this, SLOT(checkEnabled()));
81         connect(booktabsRB, SIGNAL(clicked(bool)),
82                 this, SLOT(checkEnabled()));
83         connect(borderDefaultRB, SIGNAL(clicked(bool)),
84                 this, SLOT(checkEnabled()));
85         connect(borderSetPB, SIGNAL(clicked()),
86                 this, SLOT(borderSet_clicked()));
87         connect(borderUnsetPB, SIGNAL(clicked()),
88                 this, SLOT(borderUnset_clicked()));
89         connect(hAlignCO, SIGNAL(activated(int)),
90                 this, SLOT(checkEnabled()));
91         connect(vAlignCO, SIGNAL(activated(int)),
92                 this, SLOT(checkEnabled()));
93         connect(multicolumnCB, SIGNAL(clicked()),
94                 this, SLOT(checkEnabled()));
95         connect(multirowCB, SIGNAL(clicked()),
96                 this, SLOT(checkEnabled()));
97         connect(multirowOffsetED, SIGNAL(textEdited(const QString &)),
98                 this, SLOT(checkEnabled()));
99         connect(multirowOffsetUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
100                 this, SLOT(checkEnabled()));
101         connect(newpageCB, SIGNAL(clicked()),
102                 this, SLOT(checkEnabled()));
103         connect(headerStatusCB, SIGNAL(clicked()),
104                 this, SLOT(checkEnabled()));
105         connect(headerBorderAboveCB, SIGNAL(clicked()),
106                 this, SLOT(checkEnabled()));
107         connect(headerBorderBelowCB, SIGNAL(clicked()),
108                 this, SLOT(checkEnabled()));
109         connect(firstheaderStatusCB, SIGNAL(clicked()),
110                 this, SLOT(checkEnabled()));
111         connect(firstheaderBorderAboveCB, SIGNAL(clicked()),
112                 this, SLOT(checkEnabled()));
113         connect(firstheaderBorderBelowCB, SIGNAL(clicked()),
114                 this, SLOT(checkEnabled()));
115         connect(firstheaderNoContentsCB, SIGNAL(clicked()),
116                 this, SLOT(checkEnabled()));
117         connect(footerStatusCB, SIGNAL(clicked()),
118                 this, SLOT(checkEnabled()));
119         connect(footerBorderAboveCB, SIGNAL(clicked()),
120                 this, SLOT(checkEnabled()));
121         connect(footerBorderBelowCB, SIGNAL(clicked()),
122                 this, SLOT(checkEnabled()));
123         connect(lastfooterStatusCB, SIGNAL(clicked()),
124                 this, SLOT(checkEnabled()));
125         connect(lastfooterBorderAboveCB, SIGNAL(clicked()),
126                 this, SLOT(checkEnabled()));
127         connect(lastfooterBorderBelowCB, SIGNAL(clicked()),
128                 this, SLOT(checkEnabled()));
129         connect(lastfooterNoContentsCB, SIGNAL(clicked()),
130                 this, SLOT(checkEnabled()));
131         connect(captionStatusCB, SIGNAL(clicked()),
132                 this, SLOT(checkEnabled()));
133         connect(specialAlignmentED, SIGNAL(textEdited(const QString &)),
134                 this, SLOT(checkEnabled()));
135         connect(columnWidthED, SIGNAL(textEdited(const QString &)),
136                 this, SLOT(checkEnabled()));
137         connect(columnWidthUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138                 this, SLOT(checkEnabled()));
139         connect(borders, SIGNAL(topSet(bool)),
140                 this, SLOT(checkEnabled()));
141         connect(borders, SIGNAL(bottomSet(bool)),
142                 this, SLOT(checkEnabled()));
143         connect(borders, SIGNAL(rightSet(bool)),
144                 this, SLOT(checkEnabled()));
145         connect(borders, SIGNAL(leftSet(bool)),
146                 this, SLOT(checkEnabled()));
147         connect(rotateTabularCB, SIGNAL(clicked()),
148                 this, SLOT(checkEnabled()));
149         connect(rotateTabularAngleSB, SIGNAL(valueChanged(int)),
150                 this, SLOT(checkEnabled()));
151         connect(rotateCellCB, SIGNAL(clicked()),
152                 this, SLOT(checkEnabled()));
153         connect(rotateCellAngleSB, SIGNAL(valueChanged(int)),
154                 this, SLOT(checkEnabled()));
155         connect(TableAlignCO, SIGNAL(activated(int)),
156                 this, SLOT(checkEnabled()));
157         connect(longTabularCB, SIGNAL(clicked()),
158                 this, SLOT(checkEnabled()));
159         connect(leftRB, SIGNAL(clicked()),
160                 this, SLOT(checkEnabled()));
161         connect(centerRB, SIGNAL(clicked()),
162                 this, SLOT(checkEnabled()));
163         connect(rightRB, SIGNAL(clicked()),
164                 this, SLOT(checkEnabled()));
165         connect(tabularWidthED, SIGNAL(textEdited(const QString &)),
166                 this, SLOT(checkEnabled()));
167
168         decimalPointED->setInputMask("X; ");
169         decimalPointED->setMaxLength(1);
170
171         // initialize the length validator
172         addCheckedWidget(columnWidthED, columnWidthLA);
173         addCheckedWidget(multirowOffsetED, multirowOffsetLA);
174         addCheckedWidget(topspaceED, topspaceLA);
175         addCheckedWidget(bottomspaceED, bottomspaceLA);
176         addCheckedWidget(interlinespaceED, interlinespaceLA);
177         addCheckedWidget(tabularWidthED, tabularWidthLA);
178 }
179
180
181 void GuiTabular::on_topspaceCO_activated(int index)
182 {
183         bool const enable = (index == 2);
184         topspaceED->setEnabled(enable);
185         topspaceUnitLC->setEnabled(enable);
186 }
187
188
189 void GuiTabular::on_bottomspaceCO_activated(int index)
190 {
191         bool const enable = (index == 2);
192         bottomspaceED->setEnabled(enable);
193         bottomspaceUnitLC->setEnabled(enable);
194 }
195
196
197 void GuiTabular::on_interlinespaceCO_activated(int index)
198 {
199         bool const enable = (index == 2);
200         interlinespaceED->setEnabled(enable);
201         interlinespaceUnitLC->setEnabled(enable);
202 }
203
204
205 void GuiTabular::enableWidgets() const
206 {
207         // if there is a LaTeX argument, the width and alignment will be overwritten
208         // therefore disable them in this case
209         columnWidthED->setEnabled(specialAlignmentED->text().isEmpty());
210         columnWidthUnitLC->setEnabled(specialAlignmentED->text().isEmpty());
211         // if the column has a width, multirows are always left-aligned
212         // therefore disable hAlignCB in this case
213         hAlignCO->setEnabled(!(multirowCB->isChecked()
214                 && !widgetsToLength(columnWidthED, columnWidthUnitLC).empty())
215                 && specialAlignmentED->text().isEmpty());
216         // decimal alignment is only possible for non-multicol and non-multirow cells
217         if ((multicolumnCB->isChecked() || multirowCB->isChecked())
218                 && hAlignCO->findData(toqstr("decimal")))
219                 hAlignCO->removeItem(hAlignCO->findData(toqstr("decimal")));
220         else if (!multicolumnCB->isChecked() && !multirowCB->isChecked()
221                 && hAlignCO->findData(toqstr("decimal")) == -1)
222                 hAlignCO->addItem(qt_("At Decimal Separator"), toqstr("decimal"));
223         bool const dalign =
224                 hAlignCO->itemData(hAlignCO->currentIndex()).toString() == QString("decimal");
225         decimalPointED->setEnabled(dalign);
226         decimalLA->setEnabled(dalign);
227
228         bool const setwidth = TableAlignCO->currentText() == qt_("Middle")
229                 && !longTabularCB->isChecked();
230         tabularWidthLA->setEnabled(setwidth);
231         tabularWidthED->setEnabled(setwidth);
232         tabularWidthUnitLC->setEnabled(setwidth);
233
234         rotateTabularAngleSB->setEnabled(rotateTabularCB->isChecked()
235                                          && !longTabularCB->isChecked());
236         rotateCellAngleSB->setEnabled(rotateCellCB->isChecked());
237
238         bool const enable_valign =
239                 !multirowCB->isChecked()
240                 && !widgetsToLength(columnWidthED, columnWidthUnitLC).empty()
241                 && specialAlignmentED->text().isEmpty();
242         vAlignCO->setEnabled(enable_valign);
243         vAlignLA->setEnabled(enable_valign);
244
245         topspaceED->setEnabled(topspaceCO->currentIndex() == 2);
246         topspaceED->setEnabled(topspaceCO->currentIndex() == 2);
247         topspaceUnitLC->setEnabled(topspaceCO->currentIndex() == 2);
248         bottomspaceED->setEnabled(bottomspaceCO->currentIndex() == 2);
249         bottomspaceUnitLC->setEnabled(bottomspaceCO->currentIndex() == 2);
250         interlinespaceED->setEnabled(interlinespaceCO->currentIndex() == 2);
251         interlinespaceUnitLC->setEnabled(interlinespaceCO->currentIndex() == 2);
252
253         // setting as longtable is not allowed when table is inside a float
254         bool const is_tabular_star = !tabularWidthED->text().isEmpty();
255         longTabularCB->setEnabled(!is_tabular_star && funcEnabled(Tabular::SET_LONGTABULAR));
256         bool const longtabular = longTabularCB->isChecked();
257         longtableGB->setEnabled(true);
258         newpageCB->setEnabled(longtabular);
259         alignmentGB->setEnabled(longtabular);
260         // longtables and tabular* cannot have a vertical alignment
261         TableAlignLA->setDisabled(is_tabular_star || longtabular);
262         TableAlignCO->setDisabled(is_tabular_star || longtabular);
263         // longtable cannot be rotated with rotating package, only
264         // with [pdf]lscape, which only supports 90 deg.
265         rotateTabularAngleSB->setDisabled(longtabular);
266
267         // FIXME: This Dialog is really horrible, disabling/enabling a checkbox
268         // depending on the cursor position is very very unintuitive...
269         // We need some edit boxes to show which rows are header/footer/etc
270         // without having to move the cursor first.
271         headerStatusCB->setEnabled(longtabular
272                 && (headerStatusCB->isChecked() ?
273                     funcEnabled(Tabular::UNSET_LTHEAD) :
274                     funcEnabled(Tabular::SET_LTHEAD)));
275         headerBorderAboveCB->setEnabled(longtabular
276                 && headerStatusCB->isChecked());
277         headerBorderBelowCB->setEnabled(longtabular
278                 && headerStatusCB->isChecked());
279
280         // first header can only be suppressed when there is a header
281         // firstheader_suppressable_ is set in paramsToDialog
282         firstheaderNoContentsCB->setEnabled(longtabular && firstheader_suppressable_);
283         // check if setting a first header is allowed
284         // additionally check firstheaderNoContentsCB because when this is
285         // the case a first header makes no sense
286         firstheaderStatusCB->setEnabled((firstheaderStatusCB->isChecked() ?
287                    funcEnabled(Tabular::UNSET_LTFIRSTHEAD) :
288                    funcEnabled(Tabular::SET_LTFIRSTHEAD))
289                 && longtabular && !firstheaderNoContentsCB->isChecked());
290         firstheaderBorderAboveCB->setEnabled(longtabular
291                 && firstheaderStatusCB->isChecked());
292         firstheaderBorderBelowCB->setEnabled(longtabular
293                 && firstheaderStatusCB->isChecked());
294
295         footerStatusCB->setEnabled(longtabular
296                 && (footerStatusCB->isChecked() ?
297                     funcEnabled(Tabular::UNSET_LTFOOT) :
298                     funcEnabled(Tabular::SET_LTFOOT)));
299         footerBorderAboveCB->setEnabled(longtabular
300                 && footerBorderAboveCB->isChecked());
301         footerBorderBelowCB->setEnabled(longtabular
302                 && footerBorderAboveCB->isChecked());
303
304         // last footer can only be suppressed when there is a footer
305         // lastfooter_suppressable_ is set in paramsToDialog
306         lastfooterNoContentsCB->setEnabled(longtabular && lastfooter_suppressable_);
307         // check if setting a last footer is allowed
308         // additionally check lastfooterNoContentsCB because when this is
309         // the case a last footer makes no sense
310         lastfooterStatusCB->setEnabled((lastfooterStatusCB->isChecked() ?
311                    funcEnabled(Tabular::UNSET_LTLASTFOOT) :
312                    funcEnabled(Tabular::SET_LTLASTFOOT))
313                 && longtabular && !lastfooterNoContentsCB->isChecked());
314         lastfooterBorderAboveCB->setEnabled(longtabular
315                 && lastfooterBorderAboveCB->isChecked());
316         lastfooterBorderBelowCB->setEnabled(longtabular
317                 && lastfooterBorderAboveCB->isChecked());
318
319         captionStatusCB->setEnabled(funcEnabled(Tabular::TOGGLE_LTCAPTION)
320                 && longtabular);
321
322         multicolumnCB->setEnabled(funcEnabled(Tabular::MULTICOLUMN)
323                 && !dalign && !multirowCB->isChecked());
324         multirowCB->setEnabled(funcEnabled(Tabular::MULTIROW)
325                 && !dalign && !multicolumnCB->isChecked());
326         bool const enable_mr = multirowCB->isChecked();
327         multirowOffsetLA->setEnabled(enable_mr);
328         multirowOffsetED->setEnabled(enable_mr);
329         multirowOffsetUnitLC->setEnabled(enable_mr);
330
331         // Vertical lines cannot be set in formal tables
332         borders->setLeftEnabled(!booktabsRB->isChecked());
333         borders->setRightEnabled(!booktabsRB->isChecked());
334 }
335
336
337 void GuiTabular::checkEnabled()
338 {
339         enableWidgets();
340         changed();
341 }
342
343
344 void GuiTabular::borderSet_clicked()
345 {
346         borders->setTop(true);
347         borders->setBottom(true);
348         borders->setLeft(true);
349         borders->setRight(true);
350         // repaint the setborder widget
351         borders->update();
352         checkEnabled();
353 }
354
355
356 void GuiTabular::borderUnset_clicked()
357 {
358         borders->setTop(false);
359         borders->setBottom(false);
360         borders->setLeft(false);
361         borders->setRight(false);
362         // repaint the setborder widget
363         borders->update();
364         checkEnabled();
365 }
366
367
368 static void setParam(string & param_str, Tabular::Feature f, string const & arg = string())
369 {
370         param_str += ' ';
371         param_str += featureAsString(f) + ' ' + arg;
372 }
373
374
375 void GuiTabular::setHAlign(string & param_str) const
376 {
377         Tabular::Feature num = Tabular::ALIGN_LEFT;
378         Tabular::Feature multi_num = Tabular::M_ALIGN_LEFT;
379         string const align =
380                 fromqstr(hAlignCO->itemData(hAlignCO->currentIndex()).toString());
381         if (align == "left") {
382                 num = Tabular::ALIGN_LEFT;
383                 multi_num = Tabular::M_ALIGN_LEFT;
384         } else if (align == "center") {
385                 num = Tabular::ALIGN_CENTER;
386                 multi_num = Tabular::M_ALIGN_CENTER;
387         } else if (align == "right") {
388                 num = Tabular::ALIGN_RIGHT;
389                 multi_num = Tabular::M_ALIGN_RIGHT;
390         } else if (align == "justified") {
391                 num = Tabular::ALIGN_BLOCK;
392                 //multi_num: no equivalent
393         } else if (align == "decimal") {
394                 num = Tabular::ALIGN_DECIMAL;
395                 //multi_num: no equivalent
396         }
397
398         if (multicolumnCB->isChecked())
399                 setParam(param_str, multi_num);
400         else
401                 setParam(param_str, num);
402 }
403
404
405 void GuiTabular::setVAlign(string & param_str) const
406 {
407         int const align = vAlignCO->currentIndex();
408         enum VALIGN { TOP, MIDDLE, BOTTOM };
409         VALIGN v = TOP;
410
411         switch (align) {
412                 case 0: v = TOP; break;
413                 case 1: v = MIDDLE; break;
414                 case 2: v = BOTTOM; break;
415         }
416
417         Tabular::Feature num = Tabular::VALIGN_MIDDLE;
418         Tabular::Feature multi_num = Tabular::M_VALIGN_MIDDLE;
419
420         switch (v) {
421                 case TOP:
422                         num = Tabular::VALIGN_TOP;
423                         multi_num = Tabular::M_VALIGN_TOP;
424                         break;
425                 case MIDDLE:
426                         num = Tabular::VALIGN_MIDDLE;
427                         multi_num = Tabular::M_VALIGN_MIDDLE;
428                         break;
429                 case BOTTOM:
430                         num = Tabular::VALIGN_BOTTOM;
431                         multi_num = Tabular::M_VALIGN_BOTTOM;
432                         break;
433         }
434         if (multicolumnCB->isChecked() || multirowCB->isChecked())
435                 setParam(param_str, multi_num);
436         else
437                 setParam(param_str, num);
438 }
439
440
441 void GuiTabular::setTableAlignment(string & param_str) const
442 {
443         int const align = TableAlignCO->currentIndex();
444         switch (align) {
445                 case 0: setParam(param_str, Tabular::TABULAR_VALIGN_TOP);
446                         break;
447                 case 1: setParam(param_str, Tabular::TABULAR_VALIGN_MIDDLE);
448                         break;
449                 case 2: setParam(param_str, Tabular::TABULAR_VALIGN_BOTTOM);
450                         break;
451         }
452 }
453
454
455 docstring GuiTabular::dialogToParams() const
456 {
457         string param_str = "tabular";
458
459         // table width
460         string tabwidth = widgetsToLength(tabularWidthED, tabularWidthUnitLC);
461         if (tabwidth.empty())
462                 tabwidth = "0pt";
463         setParam(param_str, Tabular::SET_TABULAR_WIDTH, tabwidth);
464
465         // apply the fixed width values
466         // this must be done before applying the column alignment
467         // because its value influences the alignment of multirow cells
468         string width = widgetsToLength(columnWidthED, columnWidthUnitLC);
469         if (width.empty())
470                 width = "0pt";
471         if (multicolumnCB->isChecked())
472                 setParam(param_str, Tabular::SET_MPWIDTH, width);
473         else
474                 setParam(param_str, Tabular::SET_PWIDTH, width);
475
476         // apply the column alignment
477         // multirows inherit the alignment from the column; if a column width
478         // is set, multirows are always left-aligned so that in this case
479         // its alignment must not be applied (see bug #8084)
480         if (!(multirowCB->isChecked() && width != "0pt"))
481                 setHAlign(param_str);
482
483         // SET_DECIMAL_POINT must come after setHAlign() (ALIGN_DECIMAL)
484         string decimal_point = fromqstr(decimalPointED->text());
485         if (decimal_point.empty())
486                 decimal_point = lyxrc.default_decimal_point;
487         setParam(param_str, Tabular::SET_DECIMAL_POINT, decimal_point);
488
489         setVAlign(param_str);
490         setTableAlignment(param_str);
491         //
492         if (booktabsRB->isChecked())
493                 setParam(param_str, Tabular::SET_BOOKTABS);
494         else
495                 setParam(param_str, Tabular::UNSET_BOOKTABS);
496
497         //
498         switch (topspaceCO->currentIndex()) {
499                 case 0:
500                         setParam(param_str, Tabular::SET_TOP_SPACE, "none");
501                         break;
502                 case 1:
503                         setParam(param_str, Tabular::SET_TOP_SPACE, "default");
504                         break;
505                 case 2:
506                         if (!topspaceED->text().isEmpty())
507                                 setParam(param_str, Tabular::SET_TOP_SPACE,
508                                          widgetsToLength(topspaceED, topspaceUnitLC));
509                         break;
510         }
511
512         //
513         switch (bottomspaceCO->currentIndex()) {
514                 case 0:
515                         setParam(param_str, Tabular::SET_BOTTOM_SPACE, "none");
516                         break;
517                 case 1:
518                         setParam(param_str, Tabular::SET_BOTTOM_SPACE, "default");
519                         break;
520                 case 2:
521                         if (!bottomspaceED->text().isEmpty())
522                                 setParam(param_str, Tabular::SET_BOTTOM_SPACE,
523                                         widgetsToLength(bottomspaceED,
524                                                         bottomspaceUnitLC));
525                         break;
526         }
527
528         //
529         switch (interlinespaceCO->currentIndex()) {
530                 case 0:
531                         setParam(param_str, Tabular::SET_INTERLINE_SPACE, "none");
532                         break;
533                 case 1:
534                         setParam(param_str, Tabular::SET_INTERLINE_SPACE, "default");
535                         break;
536                 case 2:
537                         if (!interlinespaceED->text().isEmpty())
538                                 setParam(param_str, Tabular::SET_INTERLINE_SPACE,
539                                         widgetsToLength(interlinespaceED,
540                                                         interlinespaceUnitLC));
541                         break;
542         }
543
544         //
545         if (borders->getTop() && borders->getBottom() && borders->getLeft()
546                 && borders->getRight())
547                 setParam(param_str, Tabular::SET_ALL_LINES);
548         else if (!borders->getTop() && !borders->getBottom() && !borders->getLeft()
549                 && !borders->getRight())
550                 setParam(param_str, Tabular::UNSET_ALL_LINES);
551         else {
552                 setParam(param_str, Tabular::SET_LINE_LEFT,
553                          borders->getLeft() ? "true" : "false");
554                 setParam(param_str, Tabular::SET_LINE_RIGHT,
555                          borders->getRight() ? "true" : "false");
556                 setParam(param_str, Tabular::SET_LINE_TOP,
557                          borders->getTop() ? "true" : "false");
558                 setParam(param_str, Tabular::SET_LINE_BOTTOM,
559                          borders->getBottom() ? "true" : "false");
560         }
561
562         // apply the special alignment
563         string special = fromqstr(specialAlignmentED->text());
564         if (support::trim(special).empty())
565                 special = "none";
566         if (multicolumnCB->isChecked())
567                 setParam(param_str, Tabular::SET_SPECIAL_MULTICOLUMN, special);
568         else
569                 setParam(param_str, Tabular::SET_SPECIAL_COLUMN, special);
570
571         //
572         if (multicolumnCB->isChecked())
573                 setParam(param_str, Tabular::SET_MULTICOLUMN);
574         else
575                 setParam(param_str, Tabular::UNSET_MULTICOLUMN);
576
577         // apply the multirow offset
578         string mroffset = widgetsToLength(multirowOffsetED, multirowOffsetUnitLC);
579         if (mroffset.empty())
580                 mroffset = "0pt";
581         if (multirowCB->isChecked())
582                 setParam(param_str, Tabular::SET_MROFFSET, mroffset);
583         //
584         if (multirowCB->isChecked())
585                 setParam(param_str, Tabular::SET_MULTIROW);
586         else
587                 setParam(param_str, Tabular::UNSET_MULTIROW);
588         // store the table rotation angle
589         string const tabular_angle = convert<string>(rotateTabularAngleSB->value());
590         if (rotateTabularCB->isChecked())
591                 setParam(param_str, Tabular::SET_ROTATE_TABULAR, tabular_angle);
592         else
593                 setParam(param_str, Tabular::UNSET_ROTATE_TABULAR, tabular_angle);
594         // store the cell rotation angle
595         string const cell_angle = convert<string>(rotateCellAngleSB->value());
596         if (rotateCellCB->isChecked())
597                 setParam(param_str, Tabular::SET_ROTATE_CELL, cell_angle);
598         else
599                 setParam(param_str, Tabular::UNSET_ROTATE_CELL, cell_angle);
600         //
601         if (longTabularCB->isChecked())
602                 setParam(param_str, Tabular::SET_LONGTABULAR);
603         else
604                 setParam(param_str, Tabular::UNSET_LONGTABULAR);
605         //
606         if (newpageCB->isChecked())
607                 setParam(param_str, Tabular::SET_LTNEWPAGE);
608     else
609                 setParam(param_str, Tabular::UNSET_LTNEWPAGE);
610         //
611         if (captionStatusCB->isChecked())
612                 setParam(param_str, Tabular::SET_LTCAPTION);
613         else
614                 setParam(param_str, Tabular::UNSET_LTCAPTION);
615         //
616         if (headerStatusCB->isChecked())
617                 setParam(param_str, Tabular::SET_LTHEAD, "none");
618         else
619                 setParam(param_str, Tabular::UNSET_LTHEAD, "none");
620         //
621         if (headerBorderAboveCB->isChecked())
622                 setParam(param_str, Tabular::SET_LTHEAD, "dl_above");
623         else
624                 setParam(param_str, Tabular::UNSET_LTHEAD, "dl_above");
625         //
626         if (headerBorderBelowCB->isChecked())
627                 setParam(param_str, Tabular::SET_LTHEAD, "dl_below");
628         else
629                 setParam(param_str, Tabular::UNSET_LTHEAD, "dl_below");
630         if (firstheaderBorderAboveCB->isChecked())
631                 setParam(param_str, Tabular::SET_LTFIRSTHEAD, "dl_above");
632         else
633                 setParam(param_str, Tabular::UNSET_LTFIRSTHEAD, "dl_above");
634         if (firstheaderBorderBelowCB->isChecked())
635                 setParam(param_str, Tabular::SET_LTFIRSTHEAD, "dl_below");
636         else
637                 setParam(param_str, Tabular::UNSET_LTFIRSTHEAD, "dl_below");
638         if (firstheaderStatusCB->isChecked())
639                 setParam(param_str, Tabular::SET_LTFIRSTHEAD, "none");
640         else
641                 setParam(param_str, Tabular::UNSET_LTFIRSTHEAD, "none");
642         if (firstheaderNoContentsCB->isChecked())
643                 setParam(param_str, Tabular::SET_LTFIRSTHEAD, "empty");
644         else
645                 setParam(param_str, Tabular::UNSET_LTFIRSTHEAD, "empty");
646         if (footerStatusCB->isChecked())
647                 setParam(param_str, Tabular::SET_LTFOOT, "none");
648         else
649                 setParam(param_str, Tabular::UNSET_LTFOOT, "none");
650         if (footerBorderAboveCB->isChecked())
651                 setParam(param_str, Tabular::SET_LTFOOT, "dl_above");
652         else
653                 setParam(param_str, Tabular::UNSET_LTFOOT, "dl_above");
654         if (footerBorderBelowCB->isChecked())
655                 setParam(param_str, Tabular::SET_LTFOOT, "dl_below");
656         else
657                 setParam(param_str, Tabular::UNSET_LTFOOT, "dl_below");
658         if (lastfooterStatusCB->isChecked())
659                 setParam(param_str, Tabular::SET_LTLASTFOOT, "none");
660         else
661                 setParam(param_str, Tabular::UNSET_LTLASTFOOT, "none");
662         if (lastfooterBorderAboveCB->isChecked())
663                 setParam(param_str, Tabular::SET_LTLASTFOOT, "dl_above");
664         else
665                 setParam(param_str, Tabular::UNSET_LTLASTFOOT, "dl_above");
666         if (lastfooterBorderBelowCB->isChecked())
667                 setParam(param_str, Tabular::SET_LTLASTFOOT, "dl_below");
668         else
669                 setParam(param_str, Tabular::UNSET_LTLASTFOOT, "dl_below");
670         if (lastfooterNoContentsCB->isChecked())
671                 setParam(param_str, Tabular::SET_LTLASTFOOT, "empty");
672         else
673                 setParam(param_str, Tabular::UNSET_LTLASTFOOT, "empty");
674
675         if (leftRB->isChecked())
676                 setParam(param_str, Tabular::LONGTABULAR_ALIGN_LEFT);
677         else if (centerRB->isChecked())
678                 setParam(param_str, Tabular::LONGTABULAR_ALIGN_CENTER);
679         else if (rightRB->isChecked())
680                 setParam(param_str, Tabular::LONGTABULAR_ALIGN_RIGHT);
681
682         return from_utf8(param_str);
683 }
684
685
686 static Length getColumnPWidth(Tabular const & t, size_t cell)
687 {
688         return t.column_info[t.cellColumn(cell)].p_width;
689 }
690
691
692 static Length getMColumnPWidth(Tabular const & t, size_t cell)
693 {
694         if (t.isMultiColumn(cell) || t.isMultiRow(cell))
695                 return t.cellInfo(cell).p_width;
696         return Length();
697 }
698
699
700 static Length getMROffset(Tabular const & t, size_t cell)
701 {
702         if (t.isMultiRow(cell))
703                 return t.cellInfo(cell).mroffset;
704         return Length();
705 }
706
707
708 static docstring getAlignSpecial(Tabular const & t, size_t cell, int what)
709 {
710         if (what == Tabular::SET_SPECIAL_MULTICOLUMN)
711                 return t.cellInfo(cell).align_special;
712         return t.column_info[t.cellColumn(cell)].align_special;
713 }
714
715
716 void GuiTabular::paramsToDialog(Inset const * inset)
717 {
718         InsetTabular const * itab = static_cast<InsetTabular const *>(inset);
719         // Copy Tabular of current inset.
720         Tabular const & tabular = itab->tabular;
721
722         BufferView const * bv = guiApp->currentView()->currentBufferView();
723         size_t const cell = bv->cursor().idx();
724
725         Tabular::row_type const row = tabular.cellRow(cell);
726         Tabular::col_type const col = tabular.cellColumn(cell);
727
728         tabularRowED->setText(QString::number(row + 1));
729         tabularColumnED->setText(QString::number(col + 1));
730
731         bool const multicol = tabular.isMultiColumn(cell);
732         multicolumnCB->setChecked(multicol);
733
734         bool const multirow = tabular.isMultiRow(cell);
735         multirowCB->setChecked(multirow);
736
737         rotateCellCB->setChecked(tabular.getRotateCell(cell) != 0);
738         if (rotateCellCB->isChecked()) {
739                 if (tabular.getRotateCell(cell) != 0)
740                         rotateCellAngleSB->setValue(tabular.getRotateCell(cell));
741                 else
742                         rotateCellAngleSB->setValue(90);
743         }
744
745         longTabularCB->setChecked(tabular.is_long_tabular);
746
747         rotateTabularCB->setChecked(tabular.rotate != 0);
748         if (rotateTabularCB->isChecked()) {
749                 if (longTabularCB->isChecked())
750                         rotateTabularAngleSB->setValue(90);
751                 else
752                         rotateTabularAngleSB->setValue(tabular.rotate != 0 ? tabular.rotate : 90);
753         }
754
755         borders->setTop(tabular.topLine(cell));
756         borders->setBottom(tabular.bottomLine(cell));
757         borders->setLeft(tabular.leftLine(cell));
758         borders->setRight(tabular.rightLine(cell));
759         // repaint the setborder widget
760         borders->update();
761
762         Length::UNIT const default_unit = Length::defaultUnit();
763
764         ///////////////////////////////////
765         // Set width and alignment
766
767         Length const tabwidth = tabular.tabularWidth();
768         if (tabwidth.zero()
769             && !(tabularWidthED->hasFocus() && tabularWidthED->text() == "0"))
770                 tabularWidthED->clear();
771         else
772                 lengthToWidgets(tabularWidthED, tabularWidthUnitLC,
773                         tabwidth.asString(), default_unit);
774
775         Length pwidth;
776         docstring special;
777         if (multicol) {
778                 special = getAlignSpecial(tabular, cell,
779                         Tabular::SET_SPECIAL_MULTICOLUMN);
780                 pwidth = getMColumnPWidth(tabular, cell);
781         } else {
782                 special = getAlignSpecial(tabular, cell,
783                         Tabular::SET_SPECIAL_COLUMN);
784                 pwidth = getColumnPWidth(tabular, cell);
785         }
786         string colwidth;
787         if (pwidth.zero()
788             && !(columnWidthED->hasFocus() && columnWidthED->text() == "0"))
789                 columnWidthED->clear();
790         else {
791                 colwidth = pwidth.asString();
792                 lengthToWidgets(columnWidthED, columnWidthUnitLC,
793                         colwidth, default_unit);
794         }
795         Length mroffset;
796         if (multirow)
797                 mroffset = getMROffset(tabular, cell);
798         string offset;
799         if (mroffset.zero()
800             && !(multirowOffsetED->hasFocus() && multirowOffsetED->text() == "0"))
801                 multirowOffsetED->clear();
802         else {
803                 offset = mroffset.asString();
804                 lengthToWidgets(multirowOffsetED, multirowOffsetUnitLC,
805                         offset, default_unit);
806         }
807         specialAlignmentED->setText(toqstr(special));
808         ///////////////////////////////////
809
810
811         borderDefaultRB->setChecked(!tabular.use_booktabs);
812         booktabsRB->setChecked(tabular.use_booktabs);
813
814         if (tabular.row_info[row].top_space.empty()
815             && !tabular.row_info[row].top_space_default) {
816                 topspaceCO->setCurrentIndex(0);
817         } else if (tabular.row_info[row].top_space_default) {
818                 topspaceCO->setCurrentIndex(1);
819         } else {
820                 topspaceCO->setCurrentIndex(2);
821                 lengthToWidgets(topspaceED,
822                                 topspaceUnitLC,
823                                 tabular.row_info[row].top_space.asString(),
824                                 default_unit);
825         }
826
827         if (tabular.row_info[row].bottom_space.empty()
828             && !tabular.row_info[row].bottom_space_default) {
829                 bottomspaceCO->setCurrentIndex(0);
830         } else if (tabular.row_info[row].bottom_space_default) {
831                 bottomspaceCO->setCurrentIndex(1);
832         } else {
833                 bottomspaceCO->setCurrentIndex(2);
834                 lengthToWidgets(bottomspaceED,
835                                 bottomspaceUnitLC,
836                                 tabular.row_info[row].bottom_space.asString(),
837                                 default_unit);
838         }
839
840         if (tabular.row_info[row].interline_space.empty()
841             && !tabular.row_info[row].interline_space_default) {
842                 interlinespaceCO->setCurrentIndex(0);
843         } else if (tabular.row_info[row].interline_space_default) {
844                 interlinespaceCO->setCurrentIndex(1);
845         } else {
846                 interlinespaceCO->setCurrentIndex(2);
847                 lengthToWidgets(interlinespaceED,
848                                 interlinespaceUnitLC,
849                                 tabular.row_info[row].interline_space.asString(),
850                                 default_unit);
851         }
852
853         hAlignCO->clear();
854         hAlignCO->addItem(qt_("Left"), toqstr("left"));
855         hAlignCO->addItem(qt_("Center"), toqstr("center"));
856         hAlignCO->addItem(qt_("Right"), toqstr("right"));
857         if (!multicol && !pwidth.zero())
858                 hAlignCO->addItem(qt_("Justified"), toqstr("justified"));
859         if (!multicol && !multirow)
860                 hAlignCO->addItem(qt_("At Decimal Separator"), toqstr("decimal"));
861
862         string align;
863         switch (tabular.getAlignment(cell)) {
864                 case LYX_ALIGN_LEFT:
865                         align = "left";
866                         break;
867                 case LYX_ALIGN_CENTER:
868                         align = "center";
869                         break;
870                 case LYX_ALIGN_RIGHT:
871                         align = "right";
872                         break;
873                 case LYX_ALIGN_BLOCK:
874                 {
875                         if (!multicol && !pwidth.zero())
876                                 align = "justified";
877                         break;
878                 }
879                 case LYX_ALIGN_DECIMAL:
880                 {
881                         if (!multicol && !multirow)
882                                 align = "decimal";
883                         break;
884                 }
885                 default:
886                         // we should never end up here
887                         break;
888         }
889         hAlignCO->setCurrentIndex(hAlignCO->findData(toqstr(align)));
890
891         //
892         QString decimal_point = toqstr(tabular.column_info[col].decimal_point);
893         if (decimal_point.isEmpty())
894                 decimal_point = toqstr(from_utf8(lyxrc.default_decimal_point));
895         decimalPointED->setText(decimal_point);
896
897         int valign = 0;
898         switch (tabular.getVAlignment(cell)) {
899         case Tabular::LYX_VALIGN_TOP:
900                 valign = 0;
901                 break;
902         case Tabular::LYX_VALIGN_MIDDLE:
903                 valign = 1;
904                 break;
905         case Tabular::LYX_VALIGN_BOTTOM:
906                 valign = 2;
907                 break;
908         default:
909                 valign = 0;
910                 break;
911         }
912         if (pwidth.zero())
913                 valign = 0;
914         vAlignCO->setCurrentIndex(valign);
915
916         int tableValign = 1;
917         switch (tabular.tabular_valignment) {
918         case Tabular::LYX_VALIGN_TOP:
919                 tableValign = 0;
920                 break;
921         case Tabular::LYX_VALIGN_MIDDLE:
922                 tableValign = 1;
923                 break;
924         case Tabular::LYX_VALIGN_BOTTOM:
925                 tableValign = 2;
926                 break;
927         default:
928                 tableValign = 0;
929                 break;
930         }
931         TableAlignCO->setCurrentIndex(tableValign);
932
933         if (!tabular.is_long_tabular) {
934                 headerStatusCB->setChecked(false);
935                 headerBorderAboveCB->setChecked(false);
936                 headerBorderBelowCB->setChecked(false);
937                 firstheaderStatusCB->setChecked(false);
938                 firstheaderBorderAboveCB->setChecked(false);
939                 firstheaderBorderBelowCB->setChecked(false);
940                 firstheaderNoContentsCB->setChecked(false);
941                 footerStatusCB->setChecked(false);
942                 footerBorderAboveCB->setChecked(false);
943                 footerBorderBelowCB->setChecked(false);
944                 lastfooterStatusCB->setChecked(false);
945                 lastfooterBorderAboveCB->setChecked(false);
946                 lastfooterBorderBelowCB->setChecked(false);
947                 lastfooterNoContentsCB->setChecked(false);
948                 newpageCB->setChecked(false);
949                 captionStatusCB->blockSignals(true);
950                 captionStatusCB->setChecked(false);
951                 captionStatusCB->blockSignals(false);
952                 checkEnabled();
953                 return;
954         } else {
955                 // longtables cannot have a vertical alignment
956                 TableAlignCO->setCurrentIndex(Tabular::LYX_VALIGN_MIDDLE);
957         }
958         switch (tabular.longtabular_alignment) {
959         case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
960                 leftRB->setChecked(true);
961                 break;
962         case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
963                 centerRB->setChecked(true);
964                 break;
965         case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
966                 rightRB->setChecked(true);
967                 break;
968         default:
969                 centerRB->setChecked(true);
970                 break;
971         }
972         captionStatusCB->blockSignals(true);
973         captionStatusCB->setChecked(tabular.ltCaption(row));
974         captionStatusCB->blockSignals(false);
975
976         Tabular::ltType ltt;
977         bool use_empty;
978         bool row_set = tabular.getRowOfLTHead(row, ltt);
979         headerStatusCB->setChecked(row_set);
980         if (ltt.set) {
981                 headerBorderAboveCB->setChecked(ltt.topDL);
982                 headerBorderBelowCB->setChecked(ltt.bottomDL);
983                 use_empty = true;
984         } else {
985                 headerBorderAboveCB->setChecked(false);
986                 headerBorderBelowCB->setChecked(false);
987                 firstheaderNoContentsCB->setChecked(false);
988                 use_empty = false;
989         }
990
991         row_set = tabular.getRowOfLTFirstHead(row, ltt);
992         firstheaderStatusCB->setChecked(row_set);
993         if (ltt.set && (!ltt.empty || !use_empty)) {
994                 firstheaderBorderAboveCB->setChecked(ltt.topDL);
995                 firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
996         } else {
997                 firstheaderBorderAboveCB->setChecked(false);
998                 firstheaderBorderBelowCB->setChecked(false);
999         }
1000
1001         row_set = tabular.getRowOfLTFoot(row, ltt);
1002         footerStatusCB->setChecked(row_set);
1003         if (ltt.set) {
1004                 footerBorderAboveCB->setChecked(ltt.topDL);
1005                 footerBorderBelowCB->setChecked(ltt.bottomDL);
1006                 use_empty = true;
1007         } else {
1008                 footerBorderAboveCB->setChecked(false);
1009                 footerBorderBelowCB->setChecked(false);
1010                 lastfooterNoContentsCB->setChecked(false);
1011                 use_empty = false;
1012         }
1013
1014         row_set = tabular.getRowOfLTLastFoot(row, ltt);
1015         lastfooterStatusCB->setChecked(row_set);
1016         if (ltt.set && (!ltt.empty || !use_empty)) {
1017                 lastfooterBorderAboveCB->setChecked(ltt.topDL);
1018                 lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
1019         } else {
1020                 lastfooterBorderAboveCB->setChecked(false);
1021                 lastfooterBorderBelowCB->setChecked(false);
1022         }
1023         newpageCB->setChecked(tabular.getLTNewPage(row));
1024
1025         // first header can only be suppressed when there is a header
1026         firstheader_suppressable_ = tabular.haveLTHead()
1027                         && !tabular.haveLTFirstHead();
1028         // last footer can only be suppressed when there is a footer
1029         lastfooter_suppressable_ = tabular.haveLTFoot()
1030                         && !tabular.haveLTLastFoot();
1031
1032         // after setting the features, check if they are enabled
1033         checkEnabled();
1034 }
1035
1036
1037 bool GuiTabular::checkWidgets(bool readonly) const
1038 {
1039         tabularRowED->setReadOnly(readonly);
1040         tabularColumnED->setReadOnly(readonly);
1041         tabularWidthED->setReadOnly(readonly);
1042         specialAlignmentED->setReadOnly(readonly);
1043         columnWidthED->setReadOnly(readonly);
1044         multirowOffsetED->setReadOnly(readonly);
1045         decimalPointED->setReadOnly(readonly);
1046
1047         if (readonly) {
1048                 multicolumnCB->setEnabled(false);
1049                 multirowCB->setEnabled(false);
1050                 rotateCellCB->setEnabled(false);
1051                 rotateCellAngleSB->setEnabled(false);
1052                 rotateTabularCB->setEnabled(false);
1053                 rotateTabularAngleSB->setEnabled(false);
1054                 longTabularCB->setEnabled(false);
1055                 borders->setEnabled(false);
1056                 tabularWidthUnitLC->setEnabled(false);
1057                 columnWidthUnitLC->setEnabled(false);
1058                 multirowOffsetUnitLC->setEnabled(false);
1059                 setBordersGB->setEnabled(false);
1060                 allBordersGB->setEnabled(false);
1061                 borderStyleGB->setEnabled(false);
1062                 booktabsRB->setEnabled(false);
1063                 topspaceCO->setEnabled(false);
1064                 topspaceUnitLC->setEnabled(false);
1065                 bottomspaceCO->setEnabled(false);
1066                 bottomspaceUnitLC->setEnabled(false);
1067                 interlinespaceCO->setEnabled(false);
1068                 interlinespaceUnitLC->setEnabled(false);
1069                 hAlignCO->setEnabled(false);
1070                 vAlignCO->setEnabled(false);
1071                 TableAlignCO->setEnabled(false);
1072                 longtableGB->setEnabled(false);
1073                 alignmentGB->setEnabled(false);
1074         } else
1075                 enableWidgets();
1076
1077         return InsetParamsWidget::checkWidgets();
1078 }
1079
1080
1081 bool GuiTabular::funcEnabled(Tabular::Feature f) const
1082 {
1083         FuncRequest r(LFUN_INSET_MODIFY, "tabular for-dialog" + featureAsString(f));
1084         return getStatus(r).enabled();
1085 }
1086
1087
1088 } // namespace frontend
1089 } // namespace lyx
1090
1091 #include "moc_GuiTabular.cpp"