]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QTabular.C
get rid of broken_header.h and some unneeded tests
[lyx.git] / src / frontends / qt2 / QTabular.C
1 /**
2  * \file QTabular.C
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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "QTabular.h"
16 #include "QTabularDialog.h"
17 #include "Qt2BC.h"
18
19 #include "lengthcombo.h"
20 #include "qt_helpers.h"
21
22 #include "controllers/ButtonController.h"
23 #include "controllers/ControlTabular.h"
24
25 #include "support/tostr.h"
26
27 #include <qcheckbox.h>
28 #include <qlineedit.h>
29 #include <qpushbutton.h>
30 #include "qsetborder.h"
31
32 using std::string;
33
34 namespace lyx {
35 namespace frontend {
36
37 typedef QController<ControlTabular, QView<QTabularDialog> > base_class;
38
39 QTabular::QTabular(Dialog & parent)
40         : base_class(parent, _("LyX: Table Settings"))
41 {
42 }
43
44
45 void QTabular::build_dialog()
46 {
47         dialog_.reset(new QTabularDialog(this));
48
49         bcview().setCancel(dialog_->closePB);
50
51         bcview().addReadOnly(dialog_->multicolumnCB);
52         bcview().addReadOnly(dialog_->rotateCellCB);
53         bcview().addReadOnly(dialog_->rotateTabularCB);
54         bcview().addReadOnly(dialog_->specialAlignmentED);
55         bcview().addReadOnly(dialog_->widthED);
56         bcview().addReadOnly(dialog_->widthUnit);
57         bcview().addReadOnly(dialog_->hAlignCB);
58         bcview().addReadOnly(dialog_->vAlignCB);
59         bcview().addReadOnly(dialog_->borderSetPB);
60         bcview().addReadOnly(dialog_->borderUnsetPB);
61         bcview().addReadOnly(dialog_->borders);
62         bcview().addReadOnly(dialog_->longTabularCB);
63         bcview().addReadOnly(dialog_->headerStatusCB);
64         bcview().addReadOnly(dialog_->headerBorderAboveCB);
65         bcview().addReadOnly(dialog_->headerBorderBelowCB);
66         bcview().addReadOnly(dialog_->firstheaderStatusCB);
67         bcview().addReadOnly(dialog_->firstheaderBorderAboveCB);
68         bcview().addReadOnly(dialog_->firstheaderBorderBelowCB);
69         bcview().addReadOnly(dialog_->firstheaderNoContentsCB);
70         bcview().addReadOnly(dialog_->footerStatusCB);
71         bcview().addReadOnly(dialog_->footerBorderAboveCB);
72         bcview().addReadOnly(dialog_->footerBorderBelowCB);
73         bcview().addReadOnly(dialog_->lastfooterStatusCB);
74         bcview().addReadOnly(dialog_->lastfooterBorderAboveCB);
75         bcview().addReadOnly(dialog_->lastfooterBorderBelowCB);
76         bcview().addReadOnly(dialog_->lastfooterNoContentsCB);
77         bcview().addReadOnly(dialog_->newpageCB);
78 }
79
80
81 bool QTabular::isValid()
82 {
83         return true;
84 }
85
86
87 void QTabular::update_borders()
88 {
89         LyXTabular const & tabular = controller().tabular();
90         int const cell = controller().getActiveCell();
91         bool const isMulticolumnCell = tabular.isMultiColumn(cell);
92
93         if (!isMulticolumnCell) {
94                 dialog_->borders->setLeftEnabled(true);
95                 dialog_->borders->setRightEnabled(true);
96                 dialog_->borders->setTop(tabular.topLine(cell, true));
97                 dialog_->borders->setBottom(tabular.bottomLine(cell, true));
98                 dialog_->borders->setLeft(tabular.leftLine(cell, true));
99                 dialog_->borders->setRight(tabular.rightLine(cell, true));
100                 // repaint the setborder widget
101                 dialog_->borders->repaint();
102                 return;
103         }
104
105         dialog_->borders->setTop(tabular.topLine(cell));
106         dialog_->borders->setBottom(tabular.bottomLine(cell));
107         // pay attention to left/right lines: they are only allowed
108         // to set if we are in first/last cell of row or if the left/right
109         // cell is also a multicolumn.
110         if (tabular.isFirstCellInRow(cell) || tabular.isMultiColumn(cell - 1)) {
111                 dialog_->borders->setLeftEnabled(true);
112                 dialog_->borders->setLeft(tabular.leftLine(cell));
113         } else {
114                 dialog_->borders->setLeft(false);
115                 dialog_->borders->setLeftEnabled(false);
116         }
117         if (tabular.isLastCellInRow(cell) || tabular.isMultiColumn(cell + 1)) {
118                 dialog_->borders->setRightEnabled(true);
119                 dialog_->borders->setRight(tabular.rightLine(cell));
120         } else {
121                 dialog_->borders->setRight(false);
122                 dialog_->borders->setRightEnabled(false);
123         }
124         // repaint the setborder widget
125         dialog_->borders->repaint();
126 }
127
128
129 void QTabular::update_contents()
130 {
131         LyXTabular const & tabular(controller().tabular());
132         int const cell = controller().getActiveCell();
133
134         int const row(tabular.row_of_cell(cell));
135         int const col(tabular.column_of_cell(cell));
136
137         dialog_->tabularRowED->setText(toqstr(tostr(row + 1)));
138         dialog_->tabularColumnED->setText(toqstr(tostr(col + 1)));
139
140         bool const multicol(tabular.isMultiColumn(cell));
141
142         dialog_->multicolumnCB->setChecked(multicol);
143
144         dialog_->rotateCellCB->setChecked(tabular.getRotateCell(cell));
145         dialog_->rotateTabularCB->setChecked(tabular.getRotateTabular());
146
147         dialog_->longTabularCB->setChecked(tabular.isLongTabular());
148
149         update_borders();
150
151         LyXLength pwidth;
152         string special;
153
154         if (multicol) {
155                 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
156                 pwidth = tabular.getMColumnPWidth(cell);
157         } else {
158                 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
159                 pwidth = tabular.getColumnPWidth(cell);
160         }
161
162         dialog_->specialAlignmentED->setText(toqstr(special));
163
164         bool const isReadonly = bc().bp().isReadOnly();
165         dialog_->specialAlignmentED->setEnabled(!isReadonly);
166
167         LyXLength::UNIT default_unit = controller().useMetricUnits() ? LyXLength::CM : LyXLength::IN;
168
169         string colwidth;
170         if (!pwidth.zero())
171                 colwidth = pwidth.asString();
172         lengthToWidgets(dialog_->widthED, dialog_->widthUnit,
173                 colwidth, default_unit);
174
175         dialog_->widthED->setEnabled(!isReadonly);
176         dialog_->widthUnit->setEnabled(!isReadonly);
177
178         dialog_->hAlignCB->clear();
179         dialog_->hAlignCB->insertItem(qt_("Left"));
180         dialog_->hAlignCB->insertItem(qt_("Center"));
181         dialog_->hAlignCB->insertItem(qt_("Right"));
182         if (!multicol && !pwidth.zero())
183                 dialog_->hAlignCB->insertItem(qt_("Block"));
184
185         int align = 0;
186         switch (tabular.getAlignment(cell)) {
187         case LYX_ALIGN_LEFT:
188                 align = 0;
189                 break;
190         case LYX_ALIGN_CENTER:
191                 align = 1;
192                 break;
193         case LYX_ALIGN_RIGHT:
194                 align = 2;
195                 break;
196         case LYX_ALIGN_BLOCK:
197         {
198                 if (!multicol && !pwidth.zero())
199                         align = 3;
200                 break;
201         }
202         default:
203                 align = 0;
204                 break;
205         }
206         dialog_->hAlignCB->setCurrentItem(align);
207
208         int valign = 0;
209         switch (tabular.getVAlignment(cell)) {
210         case LyXTabular::LYX_VALIGN_TOP:
211                 valign = 0;
212                 break;
213         case LyXTabular::LYX_VALIGN_MIDDLE:
214                 valign = 1;
215                 break;
216         case LyXTabular::LYX_VALIGN_BOTTOM:
217                 valign = 2;
218                 break;
219         default:
220                 valign = 1;
221                 break;
222         }
223         if (pwidth.zero())
224                 valign = 1;
225         dialog_->vAlignCB->setCurrentItem(valign);
226
227         dialog_->hAlignCB->setEnabled(true);
228         dialog_->vAlignCB->setEnabled(!pwidth.zero());
229
230         if (!tabular.isLongTabular()) {
231                 dialog_->headerStatusCB->setChecked(false);
232                 dialog_->headerBorderAboveCB->setChecked(false);
233                 dialog_->headerBorderBelowCB->setChecked(false);
234                 dialog_->firstheaderStatusCB->setChecked(false);
235                 dialog_->firstheaderBorderAboveCB->setChecked(false);
236                 dialog_->firstheaderBorderBelowCB->setChecked(false);
237                 dialog_->firstheaderNoContentsCB->setChecked(false);
238                 dialog_->footerStatusCB->setChecked(false);
239                 dialog_->footerBorderAboveCB->setChecked(false);
240                 dialog_->footerBorderBelowCB->setChecked(false);
241                 dialog_->lastfooterStatusCB->setChecked(false);
242                 dialog_->lastfooterBorderAboveCB->setChecked(false);
243                 dialog_->lastfooterBorderBelowCB->setChecked(false);
244                 dialog_->lastfooterNoContentsCB->setChecked(false);
245                 dialog_->newpageCB->setChecked(false);
246                 return;
247         }
248
249         LyXTabular::ltType ltt;
250         bool use_empty;
251         bool row_set = tabular.getRowOfLTHead(row, ltt);
252         dialog_->headerStatusCB->setChecked(row_set);
253         if (ltt.set) {
254                 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
255                 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
256                 use_empty = true;
257         } else {
258                 dialog_->headerBorderAboveCB->setChecked(false);
259                 dialog_->headerBorderBelowCB->setChecked(false);
260                 dialog_->headerBorderAboveCB->setEnabled(false);
261                 dialog_->headerBorderBelowCB->setEnabled(false);
262                 dialog_->firstheaderNoContentsCB->setChecked(false);
263                 dialog_->firstheaderNoContentsCB->setEnabled(false);
264                 use_empty = false;
265         }
266
267         row_set = tabular.getRowOfLTFirstHead(row, ltt);
268         dialog_->firstheaderStatusCB->setChecked(row_set);
269         if (ltt.set && (!ltt.empty || !use_empty)) {
270                 dialog_->firstheaderBorderAboveCB->setChecked(ltt.topDL);
271                 dialog_->firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
272         } else {
273                 dialog_->firstheaderBorderAboveCB->setEnabled(false);
274                 dialog_->firstheaderBorderBelowCB->setEnabled(false);
275                 dialog_->firstheaderBorderAboveCB->setChecked(false);
276                 dialog_->firstheaderBorderBelowCB->setChecked(false);
277                 if (use_empty) {
278                         dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
279                         if (ltt.empty)
280                                 dialog_->firstheaderStatusCB->setEnabled(false);
281                 }
282         }
283
284         row_set = tabular.getRowOfLTFoot(row, ltt);
285         dialog_->footerStatusCB->setChecked(row_set);
286         if (ltt.set) {
287                 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
288                 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
289                 use_empty = true;
290         } else {
291                 dialog_->footerBorderAboveCB->setChecked(false);
292                 dialog_->footerBorderBelowCB->setChecked(false);
293                 dialog_->footerBorderAboveCB->setEnabled(false);
294                 dialog_->footerBorderBelowCB->setEnabled(false);
295                 dialog_->lastfooterNoContentsCB->setChecked(false);
296                 dialog_->lastfooterNoContentsCB->setEnabled(false);
297                 use_empty = false;
298         }
299
300         row_set = tabular.getRowOfLTLastFoot(row, ltt);
301                 dialog_->lastfooterStatusCB->setChecked(row_set);
302         if (ltt.set && (!ltt.empty || !use_empty)) {
303                 dialog_->lastfooterBorderAboveCB->setChecked(ltt.topDL);
304                 dialog_->lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
305         } else {
306                 dialog_->lastfooterBorderAboveCB->setEnabled(false);
307                 dialog_->lastfooterBorderBelowCB->setEnabled(false);
308                 dialog_->lastfooterBorderAboveCB->setChecked(false);
309                 dialog_->lastfooterBorderBelowCB->setChecked(false);
310                 if (use_empty) {
311                         dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
312                         if (ltt.empty)
313                                 dialog_->lastfooterStatusCB->setEnabled(false);
314                 }
315         }
316         dialog_->newpageCB->setChecked(tabular.getLTNewPage(row));
317 }
318
319
320 void QTabular::closeGUI()
321 {
322         // ugly hack to auto-apply the stuff that hasn't been
323         // yet. don't let this continue to exist ...
324
325         // Subtle here, we must /not/ apply any changes and
326         // then refer to tabular, as it will have been freed
327         // since the changes update the actual controller().tabular()
328         LyXTabular const & tabular(controller().tabular());
329
330         // apply the fixed width values
331         int const cell = controller().getActiveCell();
332         bool const multicol = tabular.isMultiColumn(cell);
333         string width = widgetsToLength(dialog_->widthED, dialog_->widthUnit);
334         string width2;
335
336         LyXLength llen = tabular.getColumnPWidth(cell);
337         LyXLength llenMulti = tabular.getMColumnPWidth(cell);
338
339         if (multicol && !llenMulti.zero())
340                         width2 = llenMulti.asString();
341         else if (!multicol && !llen.zero())
342                         width2 = llen.asString();
343
344         // apply the special alignment
345         string const sa1 = fromqstr(dialog_->specialAlignmentED->text());
346         string sa2;
347
348         if (multicol)
349                 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
350         else
351                 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
352
353         if (sa1 != sa2) {
354                 if (multicol)
355                         controller().set(LyXTabular::SET_SPECIAL_MULTI, sa1);
356                 else
357                         controller().set(LyXTabular::SET_SPECIAL_COLUMN, sa1);
358         }
359
360         if (width != width2) {
361                 if (multicol)
362                         controller().set(LyXTabular::SET_MPWIDTH, width);
363                 else
364                         controller().set(LyXTabular::SET_PWIDTH, width);
365         }
366 }
367
368 } // namespace frontend
369 } // namespace lyx