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