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