]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QTabular.C
24bbc8a62afc77ce939ff593fcb0a91060b87c43
[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         int align = 0;
129         switch (tabular->GetAlignment(cell)) {
130         case LYX_ALIGN_LEFT:
131                 align = 1;
132                 break;
133         case LYX_ALIGN_CENTER:
134                 align = 2;
135                 break;
136         case LYX_ALIGN_RIGHT:
137                 align = 3;
138                 break;
139         default:
140                 align = 0;
141                 break;
142         }
143         dialog_->hAlignCB->setCurrentItem(align);
144
145         int valign = 0;
146         switch (tabular->GetVAlignment(cell)) {
147         case LyXTabular::LYX_VALIGN_TOP:
148                 valign = 0;
149                 break;
150         case LyXTabular::LYX_VALIGN_CENTER:
151                 valign = 1;
152                 break;
153         case LyXTabular::LYX_VALIGN_BOTTOM:
154                 valign = 2;
155                 break;
156         default:
157                 valign = 1;
158                 break;
159         }
160         if (pwidth.zero())
161                 valign = 1;
162         dialog_->vAlignCB->setCurrentItem(valign);
163
164         dialog_->hAlignCB->setEnabled(true);
165         dialog_->vAlignCB->setEnabled(!pwidth.zero());
166
167         if (!tabular->IsLongTabular()) {
168                 dialog_->headerStatusCB->setChecked(false);
169                 dialog_->headerBorderAboveCB->setChecked(false);
170                 dialog_->headerBorderBelowCB->setChecked(false);
171                 dialog_->firstheaderStatusCB->setChecked(false);
172                 dialog_->firstheaderBorderAboveCB->setChecked(false);
173                 dialog_->firstheaderBorderBelowCB->setChecked(false);
174                 dialog_->firstheaderNoContentsCB->setChecked(false);
175                 dialog_->footerStatusCB->setChecked(false);
176                 dialog_->footerBorderAboveCB->setChecked(false);
177                 dialog_->footerBorderBelowCB->setChecked(false);
178                 dialog_->lastfooterStatusCB->setChecked(false);
179                 dialog_->lastfooterBorderAboveCB->setChecked(false);
180                 dialog_->lastfooterBorderBelowCB->setChecked(false);
181                 dialog_->lastfooterNoContentsCB->setChecked(false);
182                 dialog_->newpageCB->setChecked(false);
183                 return;
184         }
185  
186         LyXTabular::ltType ltt;
187         bool use_empty;
188         bool row_set = tabular->GetRowOfLTHead(row, ltt);
189         dialog_->headerStatusCB->setChecked(row_set);
190         if (ltt.set) {
191                 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
192                 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
193                 use_empty = true;
194         } else {
195                 dialog_->headerBorderAboveCB->setChecked(false);
196                 dialog_->headerBorderBelowCB->setChecked(false);
197                 dialog_->headerBorderAboveCB->setEnabled(false);
198                 dialog_->headerBorderBelowCB->setEnabled(false);
199                 dialog_->firstheaderNoContentsCB->setChecked(false);
200                 dialog_->firstheaderNoContentsCB->setEnabled(false);
201                 use_empty = false;
202         }
203
204         row_set = tabular->GetRowOfLTFirstHead(row, ltt);
205         dialog_->firstheaderStatusCB->setChecked(row_set);
206         if (ltt.set && (!ltt.empty || !use_empty)) {
207                 dialog_->firstheaderBorderAboveCB->setChecked(ltt.topDL);
208                 dialog_->firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
209         } else {
210                 dialog_->firstheaderBorderAboveCB->setEnabled(false);
211                 dialog_->firstheaderBorderBelowCB->setEnabled(false);
212                 dialog_->firstheaderBorderAboveCB->setChecked(false);
213                 dialog_->firstheaderBorderBelowCB->setChecked(false);
214                 if (use_empty) {
215                         dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
216                         if (ltt.empty)
217                                 dialog_->firstheaderStatusCB->setEnabled(false);
218                 }
219         }
220
221         row_set = tabular->GetRowOfLTFoot(row, ltt);
222         dialog_->footerStatusCB->setChecked(row_set);
223         if (ltt.set) {
224                 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
225                 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
226                 use_empty = true;
227         } else {
228                 dialog_->footerBorderAboveCB->setChecked(false);
229                 dialog_->footerBorderBelowCB->setChecked(false);
230                 dialog_->footerBorderAboveCB->setEnabled(false);
231                 dialog_->footerBorderBelowCB->setEnabled(false);
232                 dialog_->lastfooterNoContentsCB->setChecked(false);
233                 dialog_->lastfooterNoContentsCB->setEnabled(false);
234                 use_empty = false;
235         }
236
237         row_set = tabular->GetRowOfLTLastFoot(row, ltt);
238                 dialog_->lastfooterStatusCB->setChecked(row_set);
239         if (ltt.set && (!ltt.empty || !use_empty)) {
240                 dialog_->lastfooterBorderAboveCB->setChecked(ltt.topDL);
241                 dialog_->lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
242         } else {
243                 dialog_->lastfooterBorderAboveCB->setEnabled(false);
244                 dialog_->lastfooterBorderBelowCB->setEnabled(false);
245                 dialog_->lastfooterBorderAboveCB->setChecked(false);
246                 dialog_->lastfooterBorderBelowCB->setChecked(false);
247                 if (use_empty) {
248                         dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
249                         if (ltt.empty)
250                                 dialog_->lastfooterStatusCB->setEnabled(false);
251                 }
252         }
253         dialog_->newpageCB->setChecked(tabular->GetLTNewPage(row));
254 }
255
256
257 void QTabular::closeGUI()
258 {
259         // ugly hack to auto-apply the stuff that hasn't been
260         // yet. don't let this continue to exist ...
261
262         InsetTabular * inset(controller().inset());
263         LyXTabular * tabular(controller().tabular());
264
265         // apply the fixed width values
266         int cell = inset->getActCell();
267         string str1 = LyXLength(dialog_->widthED->text().toDouble(),
268                         dialog_->widthUnit->currentLengthItem()).asString();
269         string str2;
270         LyXLength llen(tabular->GetColumnPWidth(cell));
271         if (llen.zero())
272                 str2 = "";
273         else
274                 str2 = llen.asString();
275
276         if (str1 != str2) {
277                 if (controller().isMulticolumnCell())
278                         controller().set(LyXTabular::SET_MPWIDTH, str1);
279                 else
280                         controller().set(LyXTabular::SET_PWIDTH, str1);
281         }
282
283         // apply the special alignment
284         str1 = dialog_->specialAlignmentED->text().latin1();
285         if (controller().isMulticolumnCell())
286                 str2 = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
287         else
288                 str2 = tabular->GetAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
289  
290         if (str1 != str2) {
291                 if (controller().isMulticolumnCell())
292                         controller().set(LyXTabular::SET_SPECIAL_MULTI, str1);
293                 else
294                         controller().set(LyXTabular::SET_SPECIAL_COLUMN, str1);
295         }
296 }