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