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