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