]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QTabular.C
patch from Juergen
[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         // FIXME: add widgets to read only
50 }
51
52
53 bool QTabular::isValid()
54 {
55         return true;
56 }
57
58
59 void QTabular::update_borders()
60 {
61         LyXTabular * tabular(controller().tabular());
62         int cell(controller().inset()->getActCell());
63  
64         if (!controller().isMulticolumnCell()) {
65                 dialog_->borders->setTop(tabular->TopLine(cell, true));
66                 dialog_->borders->setBottom(tabular->BottomLine(cell, true));
67                 dialog_->borders->setLeft(tabular->LeftLine(cell, true));
68                 dialog_->borders->setRight(tabular->RightLine(cell, true));
69                 return;
70         }
71  
72         dialog_->borders->setTop(tabular->TopLine(cell));
73         dialog_->borders->setBottom(tabular->BottomLine(cell));
74         dialog_->borders->setLeft(tabular->LeftLine(cell));
75         dialog_->borders->setRight(tabular->RightLine(cell));
76 }
77
78
79 void QTabular::update_contents()
80 {
81         LyXTabular * tabular(controller().tabular());
82         int cell(controller().inset()->getActCell());
83
84         int const row(tabular->row_of_cell(cell));
85         int const col(tabular->column_of_cell(cell));
86  
87         dialog_->tabularRowED->setText(tostr(row + 1).c_str());
88         dialog_->tabularColumnED->setText(tostr(col + 1).c_str());
89
90         bool const multicol(controller().isMulticolumnCell());
91
92         dialog_->multicolumnCB->setChecked(multicol);
93
94         dialog_->rotateCellCB->setChecked(tabular->GetRotateCell(cell));
95         dialog_->rotateTabularCB->setChecked(tabular->GetRotateTabular());
96
97         dialog_->longTabularCB->setChecked(tabular->IsLongTabular());
98
99         update_borders();
100
101         LyXLength pwidth;
102         string special;
103         
104         if (multicol) {
105                 special = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
106                 pwidth = tabular->GetMColumnPWidth(cell);
107         } else {
108                 special = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
109                 pwidth = tabular->GetColumnPWidth(cell);
110         }
111         
112         dialog_->specialAlignmentED->setText(special.c_str());
113
114         bool const isReadonly = bc().bp().isReadOnly();
115         dialog_->specialAlignmentED->setEnabled(!isReadonly);
116
117         LyXLength::UNIT default_unit = controller().metric() ? LyXLength::CM : LyXLength::IN;
118         if (!pwidth.zero()) {
119                 dialog_->widthED->setText(tostr(pwidth.value()).c_str());
120                 dialog_->widthUnit->setCurrentItem(pwidth.unit());
121         } else {
122                 dialog_->widthED->setText("");
123                 dialog_->widthUnit->setCurrentItem(default_unit);
124         }
125         dialog_->widthED->setEnabled(!isReadonly);
126         dialog_->widthUnit->setEnabled(!isReadonly);
127
128         dialog_->hAlignCB->clear();
129         dialog_->hAlignCB->insertItem(_("Left"));
130         dialog_->hAlignCB->insertItem(_("Center"));
131         dialog_->hAlignCB->insertItem(_("Right"));
132         if (!multicol && !pwidth.zero())
133                 dialog_->hAlignCB->insertItem(_("Block"));
134
135         int align = 0;
136         switch (tabular->GetAlignment(cell)) {
137         case LYX_ALIGN_LEFT:
138                 align = 0;
139                 break;
140         case LYX_ALIGN_CENTER:
141                 align = 1;
142                 break;
143         case LYX_ALIGN_RIGHT:
144                 align = 2;
145                 break;
146         case LYX_ALIGN_BLOCK:
147         {
148                 if (!multicol && !pwidth.zero())
149                         align = 3;
150                 break;
151         }
152         default:
153                 align = 0;
154                 break;
155         }
156         dialog_->hAlignCB->setCurrentItem(align);
157
158         int valign = 0;
159         switch (tabular->GetVAlignment(cell)) {
160         case LyXTabular::LYX_VALIGN_TOP:
161                 valign = 0;
162                 break;
163         case LyXTabular::LYX_VALIGN_CENTER:
164                 valign = 1;
165                 break;
166         case LyXTabular::LYX_VALIGN_BOTTOM:
167                 valign = 2;
168                 break;
169         default:
170                 valign = 1;
171                 break;
172         }
173         if (pwidth.zero())
174                 valign = 1;
175         dialog_->vAlignCB->setCurrentItem(valign);
176
177         dialog_->hAlignCB->setEnabled(true);
178         dialog_->vAlignCB->setEnabled(!pwidth.zero());
179
180         if (!tabular->IsLongTabular()) {
181                 dialog_->headerStatusCB->setChecked(false);
182                 dialog_->headerBorderAboveCB->setChecked(false);
183                 dialog_->headerBorderBelowCB->setChecked(false);
184                 dialog_->firstheaderStatusCB->setChecked(false);
185                 dialog_->firstheaderBorderAboveCB->setChecked(false);
186                 dialog_->firstheaderBorderBelowCB->setChecked(false);
187                 dialog_->firstheaderNoContentsCB->setChecked(false);
188                 dialog_->footerStatusCB->setChecked(false);
189                 dialog_->footerBorderAboveCB->setChecked(false);
190                 dialog_->footerBorderBelowCB->setChecked(false);
191                 dialog_->lastfooterStatusCB->setChecked(false);
192                 dialog_->lastfooterBorderAboveCB->setChecked(false);
193                 dialog_->lastfooterBorderBelowCB->setChecked(false);
194                 dialog_->lastfooterNoContentsCB->setChecked(false);
195                 dialog_->newpageCB->setChecked(false);
196                 return;
197         }
198  
199         LyXTabular::ltType ltt;
200         bool use_empty;
201         bool row_set = tabular->GetRowOfLTHead(row, ltt);
202         dialog_->headerStatusCB->setChecked(row_set);
203         if (ltt.set) {
204                 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
205                 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
206                 use_empty = true;
207         } else {
208                 dialog_->headerBorderAboveCB->setChecked(false);
209                 dialog_->headerBorderBelowCB->setChecked(false);
210                 dialog_->headerBorderAboveCB->setEnabled(false);
211                 dialog_->headerBorderBelowCB->setEnabled(false);
212                 dialog_->firstheaderNoContentsCB->setChecked(false);
213                 dialog_->firstheaderNoContentsCB->setEnabled(false);
214                 use_empty = false;
215         }
216
217         row_set = tabular->GetRowOfLTFirstHead(row, ltt);
218         dialog_->firstheaderStatusCB->setChecked(row_set);
219         if (ltt.set && (!ltt.empty || !use_empty)) {
220                 dialog_->firstheaderBorderAboveCB->setChecked(ltt.topDL);
221                 dialog_->firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
222         } else {
223                 dialog_->firstheaderBorderAboveCB->setEnabled(false);
224                 dialog_->firstheaderBorderBelowCB->setEnabled(false);
225                 dialog_->firstheaderBorderAboveCB->setChecked(false);
226                 dialog_->firstheaderBorderBelowCB->setChecked(false);
227                 if (use_empty) {
228                         dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
229                         if (ltt.empty)
230                                 dialog_->firstheaderStatusCB->setEnabled(false);
231                 }
232         }
233
234         row_set = tabular->GetRowOfLTFoot(row, ltt);
235         dialog_->footerStatusCB->setChecked(row_set);
236         if (ltt.set) {
237                 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
238                 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
239                 use_empty = true;
240         } else {
241                 dialog_->footerBorderAboveCB->setChecked(false);
242                 dialog_->footerBorderBelowCB->setChecked(false);
243                 dialog_->footerBorderAboveCB->setEnabled(false);
244                 dialog_->footerBorderBelowCB->setEnabled(false);
245                 dialog_->lastfooterNoContentsCB->setChecked(false);
246                 dialog_->lastfooterNoContentsCB->setEnabled(false);
247                 use_empty = false;
248         }
249
250         row_set = tabular->GetRowOfLTLastFoot(row, ltt);
251                 dialog_->lastfooterStatusCB->setChecked(row_set);
252         if (ltt.set && (!ltt.empty || !use_empty)) {
253                 dialog_->lastfooterBorderAboveCB->setChecked(ltt.topDL);
254                 dialog_->lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
255         } else {
256                 dialog_->lastfooterBorderAboveCB->setEnabled(false);
257                 dialog_->lastfooterBorderBelowCB->setEnabled(false);
258                 dialog_->lastfooterBorderAboveCB->setChecked(false);
259                 dialog_->lastfooterBorderBelowCB->setChecked(false);
260                 if (use_empty) {
261                         dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
262                         if (ltt.empty)
263                                 dialog_->lastfooterStatusCB->setEnabled(false);
264                 }
265         }
266         dialog_->newpageCB->setChecked(tabular->GetLTNewPage(row));
267 }
268
269
270 void QTabular::closeGUI()
271 {
272         // ugly hack to auto-apply the stuff that hasn't been
273         // yet. don't let this continue to exist ...
274
275         InsetTabular * inset(controller().inset());
276         LyXTabular * tabular(controller().tabular());
277
278         // apply the fixed width values
279         int cell = inset->getActCell();
280         string str1 = LyXLength(dialog_->widthED->text().toDouble(),
281                         dialog_->widthUnit->currentLengthItem()).asString();
282         string str2;
283         LyXLength llen(tabular->GetColumnPWidth(cell));
284         if (llen.zero())
285                 str2 = "";
286         else
287                 str2 = llen.asString();
288
289         if (str1 != str2) {
290                 if (controller().isMulticolumnCell())
291                         controller().set(LyXTabular::SET_MPWIDTH, str1);
292                 else
293                         controller().set(LyXTabular::SET_PWIDTH, str1);
294         }
295
296         // apply the special alignment
297         str1 = dialog_->specialAlignmentED->text().latin1();
298         if (controller().isMulticolumnCell())
299                 str2 = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
300         else
301                 str2 = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
302  
303         if (str1 != str2) {
304                 if (controller().isMulticolumnCell())
305                         controller().set(LyXTabular::SET_SPECIAL_MULTI, str1);
306                 else
307                         controller().set(LyXTabular::SET_SPECIAL_COLUMN, str1);
308         }
309 }