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_->multicolumnCB);
54 bcview().addReadOnly(dialog_->rotateCellCB);
55 bcview().addReadOnly(dialog_->rotateTabularCB);
56 bcview().addReadOnly(dialog_->specialAlignmentED);
57 bcview().addReadOnly(dialog_->widthED);
58 bcview().addReadOnly(dialog_->widthUnit);
59 bcview().addReadOnly(dialog_->hAlignCB);
60 bcview().addReadOnly(dialog_->vAlignCB);
61 bcview().addReadOnly(dialog_->borderSetPB);
62 bcview().addReadOnly(dialog_->borderUnsetPB);
63 bcview().addReadOnly(dialog_->borders);
64 bcview().addReadOnly(dialog_->booktabsRB);
65 bcview().addReadOnly(dialog_->borderDefaultRB);
66 bcview().addReadOnly(dialog_->longTabularCB);
67 bcview().addReadOnly(dialog_->headerStatusCB);
68 bcview().addReadOnly(dialog_->headerBorderAboveCB);
69 bcview().addReadOnly(dialog_->headerBorderBelowCB);
70 bcview().addReadOnly(dialog_->firstheaderStatusCB);
71 bcview().addReadOnly(dialog_->firstheaderBorderAboveCB);
72 bcview().addReadOnly(dialog_->firstheaderBorderBelowCB);
73 bcview().addReadOnly(dialog_->firstheaderNoContentsCB);
74 bcview().addReadOnly(dialog_->footerStatusCB);
75 bcview().addReadOnly(dialog_->footerBorderAboveCB);
76 bcview().addReadOnly(dialog_->footerBorderBelowCB);
77 bcview().addReadOnly(dialog_->lastfooterStatusCB);
78 bcview().addReadOnly(dialog_->lastfooterBorderAboveCB);
79 bcview().addReadOnly(dialog_->lastfooterBorderBelowCB);
80 bcview().addReadOnly(dialog_->lastfooterNoContentsCB);
81 bcview().addReadOnly(dialog_->newpageCB);
82 bcview().addReadOnly(dialog_->topspaceED);
83 bcview().addReadOnly(dialog_->topspaceUnit);
84 bcview().addReadOnly(dialog_->topspaceCO);
85 bcview().addReadOnly(dialog_->bottomspaceED);
86 bcview().addReadOnly(dialog_->bottomspaceUnit);
87 bcview().addReadOnly(dialog_->bottomspaceCO);
88 bcview().addReadOnly(dialog_->interlinespaceED);
89 bcview().addReadOnly(dialog_->interlinespaceUnit);
90 bcview().addReadOnly(dialog_->interlinespaceCO);
92 // initialize the length validator
93 addCheckedLineEdit(bcview(), dialog_->widthED,
94 dialog_->fixedWidthColLA);
95 addCheckedLineEdit(bcview(), dialog_->topspaceED,
97 addCheckedLineEdit(bcview(), dialog_->bottomspaceED,
98 dialog_->bottomspaceLA);
99 addCheckedLineEdit(bcview(), dialog_->interlinespaceED,
100 dialog_->interlinespaceLA);
104 bool QTabular::isValid()
110 void QTabular::update_borders()
112 LyXTabular const & tabular = controller().tabular();
113 LyXTabular::idx_type const cell = controller().getActiveCell();
114 bool const isMulticolumnCell = tabular.isMultiColumn(cell);
115 bool const useBookTabs = tabular.useBookTabs();
117 if (!isMulticolumnCell) {
118 dialog_->borders->setLeftEnabled(!useBookTabs);
119 dialog_->borders->setRightEnabled(!useBookTabs);
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->repaint();
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 to set
132 // if we don't use booktabs and if we are in first/last cell of row
133 // or if the left/right cell is also a multicolumn.
134 if (tabular.isFirstCellInRow(cell) || tabular.isMultiColumn(cell - 1)) {
135 dialog_->borders->setLeftEnabled(!useBookTabs);
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(!useBookTabs);
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->repaint();
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());
173 dialog_->borderDefaultRB->setChecked(!tabular.useBookTabs());
175 dialog_->booktabsRB->setChecked(tabular.useBookTabs());
183 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
184 pwidth = tabular.getMColumnPWidth(cell);
186 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
187 pwidth = tabular.getColumnPWidth(cell);
190 dialog_->specialAlignmentED->setText(toqstr(special));
192 bool const isReadonly = bc().bp().isReadOnly();
193 dialog_->specialAlignmentED->setEnabled(!isReadonly);
195 LyXLength::UNIT default_unit = controller().useMetricUnits() ? LyXLength::CM : LyXLength::IN;
199 colwidth = pwidth.asString();
200 lengthToWidgets(dialog_->widthED, dialog_->widthUnit,
201 colwidth, default_unit);
203 dialog_->widthED->setEnabled(!isReadonly);
204 dialog_->widthUnit->setEnabled(!isReadonly);
206 dialog_->hAlignCB->clear();
207 dialog_->hAlignCB->insertItem(qt_("Left"));
208 dialog_->hAlignCB->insertItem(qt_("Center"));
209 dialog_->hAlignCB->insertItem(qt_("Right"));
210 if (!multicol && !pwidth.zero())
211 dialog_->hAlignCB->insertItem(qt_("Block"));
214 switch (tabular.getAlignment(cell)) {
218 case LYX_ALIGN_CENTER:
221 case LYX_ALIGN_RIGHT:
224 case LYX_ALIGN_BLOCK:
226 if (!multicol && !pwidth.zero())
234 dialog_->hAlignCB->setCurrentItem(align);
237 switch (tabular.getVAlignment(cell)) {
238 case LyXTabular::LYX_VALIGN_TOP:
241 case LyXTabular::LYX_VALIGN_MIDDLE:
244 case LyXTabular::LYX_VALIGN_BOTTOM:
253 dialog_->vAlignCB->setCurrentItem(valign);
255 dialog_->hAlignCB->setEnabled(true);
256 dialog_->vAlignCB->setEnabled(!pwidth.zero());
258 if (tabular.row_info[row].top_space.empty()
259 && !tabular.row_info[row].top_space_default) {
260 dialog_->topspaceCO->setCurrentItem(0);
261 } else if (tabular.row_info[row].top_space_default) {
262 dialog_->topspaceCO->setCurrentItem(1);
264 dialog_->topspaceCO->setCurrentItem(2);
265 lengthToWidgets(dialog_->topspaceED,
266 dialog_->topspaceUnit,
267 tabular.row_info[row].top_space.asString(),
270 dialog_->topspaceED->setEnabled(!isReadonly
271 && (dialog_->topspaceCO->currentItem() == 2));
272 dialog_->topspaceUnit->setEnabled(!isReadonly
273 && (dialog_->topspaceCO->currentItem() == 2));
274 dialog_->topspaceCO->setEnabled(!isReadonly);
276 if (tabular.row_info[row].bottom_space.empty()
277 && !tabular.row_info[row].bottom_space_default) {
278 dialog_->bottomspaceCO->setCurrentItem(0);
279 } else if (tabular.row_info[row].bottom_space_default) {
280 dialog_->bottomspaceCO->setCurrentItem(1);
282 dialog_->bottomspaceCO->setCurrentItem(2);
283 lengthToWidgets(dialog_->bottomspaceED,
284 dialog_->bottomspaceUnit,
285 tabular.row_info[row].bottom_space.asString(),
288 dialog_->bottomspaceED->setEnabled(!isReadonly
289 && (dialog_->bottomspaceCO->currentItem() == 2));
290 dialog_->bottomspaceUnit->setEnabled(!isReadonly
291 && (dialog_->bottomspaceCO->currentItem() == 2));
292 dialog_->bottomspaceCO->setEnabled(!isReadonly);
294 if (tabular.row_info[row].interline_space.empty()
295 && !tabular.row_info[row].interline_space_default) {
296 dialog_->interlinespaceCO->setCurrentItem(0);
297 } else if (tabular.row_info[row].interline_space_default) {
298 dialog_->interlinespaceCO->setCurrentItem(1);
300 dialog_->interlinespaceCO->setCurrentItem(2);
301 lengthToWidgets(dialog_->interlinespaceED,
302 dialog_->interlinespaceUnit,
303 tabular.row_info[row].interline_space.asString(),
306 dialog_->interlinespaceED->setEnabled(!isReadonly
307 && (dialog_->interlinespaceCO->currentItem() == 2));
308 dialog_->interlinespaceUnit->setEnabled(!isReadonly
309 && (dialog_->interlinespaceCO->currentItem() == 2));
310 dialog_->interlinespaceCO->setEnabled(!isReadonly);
312 if (!tabular.isLongTabular()) {
313 dialog_->headerStatusCB->setChecked(false);
314 dialog_->headerBorderAboveCB->setChecked(false);
315 dialog_->headerBorderBelowCB->setChecked(false);
316 dialog_->firstheaderStatusCB->setChecked(false);
317 dialog_->firstheaderBorderAboveCB->setChecked(false);
318 dialog_->firstheaderBorderBelowCB->setChecked(false);
319 dialog_->firstheaderNoContentsCB->setChecked(false);
320 dialog_->footerStatusCB->setChecked(false);
321 dialog_->footerBorderAboveCB->setChecked(false);
322 dialog_->footerBorderBelowCB->setChecked(false);
323 dialog_->lastfooterStatusCB->setChecked(false);
324 dialog_->lastfooterBorderAboveCB->setChecked(false);
325 dialog_->lastfooterBorderBelowCB->setChecked(false);
326 dialog_->lastfooterNoContentsCB->setChecked(false);
327 dialog_->newpageCB->setChecked(false);
328 dialog_->newpageCB->setEnabled(false);
332 LyXTabular::ltType ltt;
334 bool row_set = tabular.getRowOfLTHead(row, ltt);
335 dialog_->headerStatusCB->setChecked(row_set);
337 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
338 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
341 dialog_->headerBorderAboveCB->setChecked(false);
342 dialog_->headerBorderBelowCB->setChecked(false);
343 dialog_->headerBorderAboveCB->setEnabled(false);
344 dialog_->headerBorderBelowCB->setEnabled(false);
345 dialog_->firstheaderNoContentsCB->setChecked(false);
346 dialog_->firstheaderNoContentsCB->setEnabled(false);
350 row_set = tabular.getRowOfLTFirstHead(row, ltt);
351 dialog_->firstheaderStatusCB->setChecked(row_set);
352 if (ltt.set && (!ltt.empty || !use_empty)) {
353 dialog_->firstheaderBorderAboveCB->setChecked(ltt.topDL);
354 dialog_->firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
356 dialog_->firstheaderBorderAboveCB->setEnabled(false);
357 dialog_->firstheaderBorderBelowCB->setEnabled(false);
358 dialog_->firstheaderBorderAboveCB->setChecked(false);
359 dialog_->firstheaderBorderBelowCB->setChecked(false);
361 dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
363 dialog_->firstheaderStatusCB->setEnabled(false);
367 row_set = tabular.getRowOfLTFoot(row, ltt);
368 dialog_->footerStatusCB->setChecked(row_set);
370 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
371 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
374 dialog_->footerBorderAboveCB->setChecked(false);
375 dialog_->footerBorderBelowCB->setChecked(false);
376 dialog_->footerBorderAboveCB->setEnabled(false);
377 dialog_->footerBorderBelowCB->setEnabled(false);
378 dialog_->lastfooterNoContentsCB->setChecked(false);
379 dialog_->lastfooterNoContentsCB->setEnabled(false);
383 row_set = tabular.getRowOfLTLastFoot(row, ltt);
384 dialog_->lastfooterStatusCB->setChecked(row_set);
385 if (ltt.set && (!ltt.empty || !use_empty)) {
386 dialog_->lastfooterBorderAboveCB->setChecked(ltt.topDL);
387 dialog_->lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
389 dialog_->lastfooterBorderAboveCB->setEnabled(false);
390 dialog_->lastfooterBorderBelowCB->setEnabled(false);
391 dialog_->lastfooterBorderAboveCB->setChecked(false);
392 dialog_->lastfooterBorderBelowCB->setChecked(false);
394 dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
396 dialog_->lastfooterStatusCB->setEnabled(false);
399 dialog_->newpageCB->setChecked(tabular.getLTNewPage(row));
403 void QTabular::closeGUI()
405 // ugly hack to auto-apply the stuff that hasn't been
406 // yet. don't let this continue to exist ...
408 // Subtle here, we must /not/ apply any changes and
409 // then refer to tabular, as it will have been freed
410 // since the changes update the actual controller().tabular()
411 LyXTabular const & tabular(controller().tabular());
413 // apply the fixed width values
414 LyXTabular::idx_type const cell = controller().getActiveCell();
415 bool const multicol = tabular.isMultiColumn(cell);
416 string width = widgetsToLength(dialog_->widthED, dialog_->widthUnit);
419 LyXLength llen = tabular.getColumnPWidth(cell);
420 LyXLength llenMulti = tabular.getMColumnPWidth(cell);
422 if (multicol && !llenMulti.zero())
423 width2 = llenMulti.asString();
424 else if (!multicol && !llen.zero())
425 width2 = llen.asString();
427 // apply the special alignment
428 string const sa1 = fromqstr(dialog_->specialAlignmentED->text());
432 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
434 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
438 controller().set(LyXTabular::SET_SPECIAL_MULTI, sa1);
440 controller().set(LyXTabular::SET_SPECIAL_COLUMN, sa1);
443 if (width != width2) {
445 controller().set(LyXTabular::SET_MPWIDTH, width);
447 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));
493 } // namespace frontend