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