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