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.
15 #include "checkedwidgets.h"
17 #include "QTabularDialog.h"
20 #include "lengthcombo.h"
21 #include "qt_helpers.h"
23 #include "controllers/ButtonController.h"
24 #include "controllers/ControlTabular.h"
26 #include "support/convert.h"
28 #include <qcheckbox.h>
29 #include <qlineedit.h>
30 #include <qpushbutton.h>
31 #include <qradiobutton.h>
32 #include "qsetborder.h"
39 typedef QController<ControlTabular, QView<QTabularDialog> > base_class;
41 QTabular::QTabular(Dialog & parent)
42 : base_class(parent, _("Table Settings"))
47 void QTabular::build_dialog()
49 dialog_.reset(new QTabularDialog(this));
51 bcview().setCancel(dialog_->closePB);
53 bcview().addReadOnly(dialog_->topspaceED);
54 bcview().addReadOnly(dialog_->topspaceUnit);
55 bcview().addReadOnly(dialog_->topspaceCO);
56 bcview().addReadOnly(dialog_->bottomspaceED);
57 bcview().addReadOnly(dialog_->bottomspaceUnit);
58 bcview().addReadOnly(dialog_->bottomspaceCO);
59 bcview().addReadOnly(dialog_->interlinespaceED);
60 bcview().addReadOnly(dialog_->interlinespaceUnit);
61 bcview().addReadOnly(dialog_->interlinespaceCO);
62 bcview().addReadOnly(dialog_->borderDefaultRB);
63 bcview().addReadOnly(dialog_->booktabsRB);
65 bcview().addReadOnly(dialog_->multicolumnCB);
66 bcview().addReadOnly(dialog_->rotateCellCB);
67 bcview().addReadOnly(dialog_->rotateTabularCB);
68 bcview().addReadOnly(dialog_->specialAlignmentED);
69 bcview().addReadOnly(dialog_->widthED);
70 bcview().addReadOnly(dialog_->widthUnit);
71 bcview().addReadOnly(dialog_->hAlignCB);
72 bcview().addReadOnly(dialog_->vAlignCB);
73 bcview().addReadOnly(dialog_->borderSetPB);
74 bcview().addReadOnly(dialog_->borderUnsetPB);
75 bcview().addReadOnly(dialog_->borders);
76 bcview().addReadOnly(dialog_->longTabularCB);
77 bcview().addReadOnly(dialog_->headerStatusCB);
78 bcview().addReadOnly(dialog_->headerBorderAboveCB);
79 bcview().addReadOnly(dialog_->headerBorderBelowCB);
80 bcview().addReadOnly(dialog_->firstheaderStatusCB);
81 bcview().addReadOnly(dialog_->firstheaderBorderAboveCB);
82 bcview().addReadOnly(dialog_->firstheaderBorderBelowCB);
83 bcview().addReadOnly(dialog_->firstheaderNoContentsCB);
84 bcview().addReadOnly(dialog_->footerStatusCB);
85 bcview().addReadOnly(dialog_->footerBorderAboveCB);
86 bcview().addReadOnly(dialog_->footerBorderBelowCB);
87 bcview().addReadOnly(dialog_->lastfooterStatusCB);
88 bcview().addReadOnly(dialog_->lastfooterBorderAboveCB);
89 bcview().addReadOnly(dialog_->lastfooterBorderBelowCB);
90 bcview().addReadOnly(dialog_->lastfooterNoContentsCB);
91 bcview().addReadOnly(dialog_->newpageCB);
93 // initialize the length validator
94 addCheckedLineEdit(bcview(), dialog_->widthED,
95 dialog_->fixedWidthColLA);
96 addCheckedLineEdit(bcview(), dialog_->topspaceED,
98 addCheckedLineEdit(bcview(), dialog_->bottomspaceED,
99 dialog_->bottomspaceLA);
100 addCheckedLineEdit(bcview(), dialog_->interlinespaceED,
101 dialog_->interlinespaceLA);
105 bool QTabular::isValid()
111 void QTabular::update_borders()
113 LyXTabular const & tabular = controller().tabular();
114 LyXTabular::idx_type const cell = controller().getActiveCell();
115 bool const isMulticolumnCell = tabular.isMultiColumn(cell);
117 if (!isMulticolumnCell) {
118 dialog_->borders->setLeftEnabled(true);
119 dialog_->borders->setRightEnabled(true);
120 dialog_->borders->setTop(tabular.topLine(cell, true));
121 dialog_->borders->setBottom(tabular.bottomLine(cell, true));
122 dialog_->borders->setLeft(tabular.leftLine(cell, true));
123 dialog_->borders->setRight(tabular.rightLine(cell, true));
124 // repaint the setborder widget
125 dialog_->borders->update();
129 dialog_->borders->setTop(tabular.topLine(cell));
130 dialog_->borders->setBottom(tabular.bottomLine(cell));
131 // pay attention to left/right lines: they are only allowed
132 // to set if we are in first/last cell of row or if the left/right
133 // cell is also a multicolumn.
134 if (tabular.isFirstCellInRow(cell) || tabular.isMultiColumn(cell - 1)) {
135 dialog_->borders->setLeftEnabled(true);
136 dialog_->borders->setLeft(tabular.leftLine(cell));
138 dialog_->borders->setLeft(false);
139 dialog_->borders->setLeftEnabled(false);
141 if (tabular.isLastCellInRow(cell) || tabular.isMultiColumn(cell + 1)) {
142 dialog_->borders->setRightEnabled(true);
143 dialog_->borders->setRight(tabular.rightLine(cell));
145 dialog_->borders->setRight(false);
146 dialog_->borders->setRightEnabled(false);
148 // repaint the setborder widget
149 dialog_->borders->update();
153 void QTabular::update_contents()
155 LyXTabular const & tabular(controller().tabular());
156 LyXTabular::idx_type const cell = controller().getActiveCell();
158 LyXTabular::row_type const row(tabular.row_of_cell(cell));
159 LyXTabular::col_type const col(tabular.column_of_cell(cell));
161 dialog_->tabularRowED->setText(toqstr(convert<string>(row + 1)));
162 dialog_->tabularColumnED->setText(toqstr(convert<string>(col + 1)));
164 bool const multicol(tabular.isMultiColumn(cell));
166 dialog_->multicolumnCB->setChecked(multicol);
168 dialog_->rotateCellCB->setChecked(tabular.getRotateCell(cell));
169 dialog_->rotateTabularCB->setChecked(tabular.getRotateTabular());
171 dialog_->longTabularCB->setChecked(tabular.isLongTabular());
179 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
180 pwidth = tabular.getMColumnPWidth(cell);
182 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
183 pwidth = tabular.getColumnPWidth(cell);
186 dialog_->specialAlignmentED->setText(toqstr(special));
188 bool const isReadonly = bc().bp().isReadOnly();
189 dialog_->specialAlignmentED->setEnabled(!isReadonly);
191 LyXLength::UNIT default_unit = controller().useMetricUnits() ? LyXLength::CM : LyXLength::IN;
193 dialog_->borderDefaultRB->setChecked(!tabular.useBookTabs());
194 dialog_->booktabsRB->setChecked(tabular.useBookTabs());
196 if (tabular.row_info[row].top_space.empty()
197 && !tabular.row_info[row].top_space_default) {
198 dialog_->topspaceCO->setCurrentItem(0);
199 } else if (tabular.row_info[row].top_space_default) {
200 dialog_->topspaceCO->setCurrentItem(1);
202 dialog_->topspaceCO->setCurrentItem(2);
203 lengthToWidgets(dialog_->topspaceED,
204 dialog_->topspaceUnit,
205 tabular.row_info[row].top_space.asString(),
208 dialog_->topspaceED->setEnabled(!isReadonly
209 && (dialog_->topspaceCO->currentItem() == 2));
210 dialog_->topspaceUnit->setEnabled(!isReadonly
211 && (dialog_->topspaceCO->currentItem() == 2));
212 dialog_->topspaceCO->setEnabled(!isReadonly);
214 if (tabular.row_info[row].bottom_space.empty()
215 && !tabular.row_info[row].bottom_space_default) {
216 dialog_->bottomspaceCO->setCurrentItem(0);
217 } else if (tabular.row_info[row].bottom_space_default) {
218 dialog_->bottomspaceCO->setCurrentItem(1);
220 dialog_->bottomspaceCO->setCurrentItem(2);
221 lengthToWidgets(dialog_->bottomspaceED,
222 dialog_->bottomspaceUnit,
223 tabular.row_info[row].bottom_space.asString(),
226 dialog_->bottomspaceED->setEnabled(!isReadonly
227 && (dialog_->bottomspaceCO->currentItem() == 2));
228 dialog_->bottomspaceUnit->setEnabled(!isReadonly
229 && (dialog_->bottomspaceCO->currentItem() == 2));
230 dialog_->bottomspaceCO->setEnabled(!isReadonly);
232 if (tabular.row_info[row].interline_space.empty()
233 && !tabular.row_info[row].interline_space_default) {
234 dialog_->interlinespaceCO->setCurrentItem(0);
235 } else if (tabular.row_info[row].interline_space_default) {
236 dialog_->interlinespaceCO->setCurrentItem(1);
238 dialog_->interlinespaceCO->setCurrentItem(2);
239 lengthToWidgets(dialog_->interlinespaceED,
240 dialog_->interlinespaceUnit,
241 tabular.row_info[row].interline_space.asString(),
244 dialog_->interlinespaceED->setEnabled(!isReadonly
245 && (dialog_->interlinespaceCO->currentItem() == 2));
246 dialog_->interlinespaceUnit->setEnabled(!isReadonly
247 && (dialog_->interlinespaceCO->currentItem() == 2));
248 dialog_->interlinespaceCO->setEnabled(!isReadonly);
252 colwidth = pwidth.asString();
253 lengthToWidgets(dialog_->widthED, dialog_->widthUnit,
254 colwidth, default_unit);
256 dialog_->widthED->setEnabled(!isReadonly);
257 dialog_->widthUnit->setEnabled(!isReadonly);
259 dialog_->hAlignCB->clear();
260 dialog_->hAlignCB->insertItem(qt_("Left"));
261 dialog_->hAlignCB->insertItem(qt_("Center"));
262 dialog_->hAlignCB->insertItem(qt_("Right"));
263 if (!multicol && !pwidth.zero())
264 dialog_->hAlignCB->insertItem(qt_("Block"));
267 switch (tabular.getAlignment(cell)) {
271 case LYX_ALIGN_CENTER:
274 case LYX_ALIGN_RIGHT:
277 case LYX_ALIGN_BLOCK:
279 if (!multicol && !pwidth.zero())
287 dialog_->hAlignCB->setCurrentItem(align);
290 switch (tabular.getVAlignment(cell)) {
291 case LyXTabular::LYX_VALIGN_TOP:
294 case LyXTabular::LYX_VALIGN_MIDDLE:
297 case LyXTabular::LYX_VALIGN_BOTTOM:
306 dialog_->vAlignCB->setCurrentItem(valign);
308 dialog_->hAlignCB->setEnabled(true);
309 dialog_->vAlignCB->setEnabled(!pwidth.zero());
311 if (!tabular.isLongTabular()) {
312 dialog_->headerStatusCB->setChecked(false);
313 dialog_->headerBorderAboveCB->setChecked(false);
314 dialog_->headerBorderBelowCB->setChecked(false);
315 dialog_->firstheaderStatusCB->setChecked(false);
316 dialog_->firstheaderBorderAboveCB->setChecked(false);
317 dialog_->firstheaderBorderBelowCB->setChecked(false);
318 dialog_->firstheaderNoContentsCB->setChecked(false);
319 dialog_->footerStatusCB->setChecked(false);
320 dialog_->footerBorderAboveCB->setChecked(false);
321 dialog_->footerBorderBelowCB->setChecked(false);
322 dialog_->lastfooterStatusCB->setChecked(false);
323 dialog_->lastfooterBorderAboveCB->setChecked(false);
324 dialog_->lastfooterBorderBelowCB->setChecked(false);
325 dialog_->lastfooterNoContentsCB->setChecked(false);
326 dialog_->newpageCB->setChecked(false);
327 dialog_->newpageCB->setEnabled(false);
331 LyXTabular::ltType ltt;
333 bool row_set = tabular.getRowOfLTHead(row, ltt);
334 dialog_->headerStatusCB->setChecked(row_set);
336 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
337 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
340 dialog_->headerBorderAboveCB->setChecked(false);
341 dialog_->headerBorderBelowCB->setChecked(false);
342 dialog_->headerBorderAboveCB->setEnabled(false);
343 dialog_->headerBorderBelowCB->setEnabled(false);
344 dialog_->firstheaderNoContentsCB->setChecked(false);
345 dialog_->firstheaderNoContentsCB->setEnabled(false);
349 row_set = tabular.getRowOfLTFirstHead(row, ltt);
350 dialog_->firstheaderStatusCB->setChecked(row_set);
351 if (ltt.set && (!ltt.empty || !use_empty)) {
352 dialog_->firstheaderBorderAboveCB->setChecked(ltt.topDL);
353 dialog_->firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
355 dialog_->firstheaderBorderAboveCB->setEnabled(false);
356 dialog_->firstheaderBorderBelowCB->setEnabled(false);
357 dialog_->firstheaderBorderAboveCB->setChecked(false);
358 dialog_->firstheaderBorderBelowCB->setChecked(false);
360 dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
362 dialog_->firstheaderStatusCB->setEnabled(false);
366 row_set = tabular.getRowOfLTFoot(row, ltt);
367 dialog_->footerStatusCB->setChecked(row_set);
369 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
370 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
373 dialog_->footerBorderAboveCB->setChecked(false);
374 dialog_->footerBorderBelowCB->setChecked(false);
375 dialog_->footerBorderAboveCB->setEnabled(false);
376 dialog_->footerBorderBelowCB->setEnabled(false);
377 dialog_->lastfooterNoContentsCB->setChecked(false);
378 dialog_->lastfooterNoContentsCB->setEnabled(false);
382 row_set = tabular.getRowOfLTLastFoot(row, ltt);
383 dialog_->lastfooterStatusCB->setChecked(row_set);
384 if (ltt.set && (!ltt.empty || !use_empty)) {
385 dialog_->lastfooterBorderAboveCB->setChecked(ltt.topDL);
386 dialog_->lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
388 dialog_->lastfooterBorderAboveCB->setEnabled(false);
389 dialog_->lastfooterBorderBelowCB->setEnabled(false);
390 dialog_->lastfooterBorderAboveCB->setChecked(false);
391 dialog_->lastfooterBorderBelowCB->setChecked(false);
393 dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
395 dialog_->lastfooterStatusCB->setEnabled(false);
398 dialog_->newpageCB->setChecked(tabular.getLTNewPage(row));
402 void QTabular::closeGUI()
404 // ugly hack to auto-apply the stuff that hasn't been
405 // yet. don't let this continue to exist ...
407 // Subtle here, we must /not/ apply any changes and
408 // then refer to tabular, as it will have been freed
409 // since the changes update the actual controller().tabular()
410 LyXTabular const & tabular(controller().tabular());
412 // apply the fixed width values
413 LyXTabular::idx_type const cell = controller().getActiveCell();
414 bool const multicol = tabular.isMultiColumn(cell);
415 string width = widgetsToLength(dialog_->widthED, dialog_->widthUnit);
418 LyXLength llen = tabular.getColumnPWidth(cell);
419 LyXLength llenMulti = tabular.getMColumnPWidth(cell);
421 if (multicol && !llenMulti.zero())
422 width2 = llenMulti.asString();
423 else if (!multicol && !llen.zero())
424 width2 = llen.asString();
426 // apply the special alignment
427 string const sa1 = fromqstr(dialog_->specialAlignmentED->text());
431 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
433 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
437 controller().set(LyXTabular::SET_SPECIAL_MULTI, sa1);
439 controller().set(LyXTabular::SET_SPECIAL_COLUMN, sa1);
442 if (width != width2) {
444 controller().set(LyXTabular::SET_MPWIDTH, width);
446 controller().set(LyXTabular::SET_PWIDTH, width);
450 switch (dialog_->topspaceCO->currentItem()) {
452 controller().set(LyXTabular::SET_TOP_SPACE, "");
455 controller().set(LyXTabular::SET_TOP_SPACE, "default");
458 controller().set(LyXTabular::SET_TOP_SPACE,
459 widgetsToLength(dialog_->topspaceED,
460 dialog_->topspaceUnit));
464 switch (dialog_->bottomspaceCO->currentItem()) {
466 controller().set(LyXTabular::SET_BOTTOM_SPACE, "");
469 controller().set(LyXTabular::SET_BOTTOM_SPACE, "default");
472 controller().set(LyXTabular::SET_BOTTOM_SPACE,
473 widgetsToLength(dialog_->bottomspaceED,
474 dialog_->bottomspaceUnit));
478 switch (dialog_->interlinespaceCO->currentItem()) {
480 controller().set(LyXTabular::SET_INTERLINE_SPACE, "");
483 controller().set(LyXTabular::SET_INTERLINE_SPACE, "default");
486 controller().set(LyXTabular::SET_INTERLINE_SPACE,
487 widgetsToLength(dialog_->interlinespaceED,
488 dialog_->interlinespaceUnit));
494 } // namespace frontend