3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
16 #include "ControlTabular.h"
17 #include "qt_helpers.h"
18 #include "support/tostr.h"
20 #include "QTabularDialog.h"
23 #include "ButtonController.h"
25 #include <qpushbutton.h>
26 #include <qlineedit.h>
27 #include <qcheckbox.h>
28 #include "lengthcombo.h"
29 #include "qsetborder.h"
34 typedef QController<ControlTabular, QView<QTabularDialog> > base_class;
36 QTabular::QTabular(Dialog & parent)
37 : base_class(parent, _("LyX: Table Settings"))
42 void QTabular::build_dialog()
44 dialog_.reset(new QTabularDialog(this));
46 bcview().setCancel(dialog_->closePB);
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);
78 bool QTabular::isValid()
84 void QTabular::update_borders()
86 LyXTabular const & tabular = controller().tabular();
87 int const cell = controller().getActiveCell();
88 bool const isMulticolumnCell = tabular.isMultiColumn(cell);
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();
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));
111 dialog_->borders->setLeft(false);
112 dialog_->borders->setLeftEnabled(false);
114 if (tabular.isLastCellInRow(cell) || tabular.isMultiColumn(cell + 1)) {
115 dialog_->borders->setRightEnabled(true);
116 dialog_->borders->setRight(tabular.rightLine(cell));
118 dialog_->borders->setRight(false);
119 dialog_->borders->setRightEnabled(false);
121 // repaint the setborder widget
122 dialog_->borders->repaint();
126 void QTabular::update_contents()
128 LyXTabular const & tabular(controller().tabular());
129 int const cell = controller().getActiveCell();
131 int const row(tabular.row_of_cell(cell));
132 int const col(tabular.column_of_cell(cell));
134 dialog_->tabularRowED->setText(toqstr(tostr(row + 1)));
135 dialog_->tabularColumnED->setText(toqstr(tostr(col + 1)));
137 bool const multicol(tabular.isMultiColumn(cell));
139 dialog_->multicolumnCB->setChecked(multicol);
141 dialog_->rotateCellCB->setChecked(tabular.getRotateCell(cell));
142 dialog_->rotateTabularCB->setChecked(tabular.getRotateTabular());
144 dialog_->longTabularCB->setChecked(tabular.isLongTabular());
152 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
153 pwidth = tabular.getMColumnPWidth(cell);
155 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
156 pwidth = tabular.getColumnPWidth(cell);
159 dialog_->specialAlignmentED->setText(toqstr(special));
161 bool const isReadonly = bc().bp().isReadOnly();
162 dialog_->specialAlignmentED->setEnabled(!isReadonly);
164 LyXLength::UNIT default_unit = controller().useMetricUnits() ? LyXLength::CM : LyXLength::IN;
168 colwidth = pwidth.asString();
169 lengthToWidgets(dialog_->widthED, dialog_->widthUnit,
170 colwidth, default_unit);
172 dialog_->widthED->setEnabled(!isReadonly);
173 dialog_->widthUnit->setEnabled(!isReadonly);
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"));
183 switch (tabular.getAlignment(cell)) {
187 case LYX_ALIGN_CENTER:
190 case LYX_ALIGN_RIGHT:
193 case LYX_ALIGN_BLOCK:
195 if (!multicol && !pwidth.zero())
203 dialog_->hAlignCB->setCurrentItem(align);
206 switch (tabular.getVAlignment(cell)) {
207 case LyXTabular::LYX_VALIGN_TOP:
210 case LyXTabular::LYX_VALIGN_MIDDLE:
213 case LyXTabular::LYX_VALIGN_BOTTOM:
222 dialog_->vAlignCB->setCurrentItem(valign);
224 dialog_->hAlignCB->setEnabled(true);
225 dialog_->vAlignCB->setEnabled(!pwidth.zero());
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);
246 LyXTabular::ltType ltt;
248 bool row_set = tabular.getRowOfLTHead(row, ltt);
249 dialog_->headerStatusCB->setChecked(row_set);
251 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
252 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
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);
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);
270 dialog_->firstheaderBorderAboveCB->setEnabled(false);
271 dialog_->firstheaderBorderBelowCB->setEnabled(false);
272 dialog_->firstheaderBorderAboveCB->setChecked(false);
273 dialog_->firstheaderBorderBelowCB->setChecked(false);
275 dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
277 dialog_->firstheaderStatusCB->setEnabled(false);
281 row_set = tabular.getRowOfLTFoot(row, ltt);
282 dialog_->footerStatusCB->setChecked(row_set);
284 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
285 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
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);
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);
303 dialog_->lastfooterBorderAboveCB->setEnabled(false);
304 dialog_->lastfooterBorderBelowCB->setEnabled(false);
305 dialog_->lastfooterBorderAboveCB->setChecked(false);
306 dialog_->lastfooterBorderBelowCB->setChecked(false);
308 dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
310 dialog_->lastfooterStatusCB->setEnabled(false);
313 dialog_->newpageCB->setChecked(tabular.getLTNewPage(row));
317 void QTabular::closeGUI()
319 // ugly hack to auto-apply the stuff that hasn't been
320 // yet. don't let this continue to exist ...
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());
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);
333 LyXLength llen = tabular.getColumnPWidth(cell);
334 LyXLength llenMulti = tabular.getMColumnPWidth(cell);
336 if (multicol && !llenMulti.zero())
337 width2 = llenMulti.asString();
338 else if (!multicol && !llen.zero())
339 width2 = llen.asString();
341 // apply the special alignment
342 string const sa1 = fromqstr(dialog_->specialAlignmentED->text());
346 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
348 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
352 controller().set(LyXTabular::SET_SPECIAL_MULTI, sa1);
354 controller().set(LyXTabular::SET_SPECIAL_COLUMN, sa1);
357 if (width != width2) {
359 controller().set(LyXTabular::SET_MPWIDTH, width);
361 controller().set(LyXTabular::SET_PWIDTH, width);