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