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 "QTabularDialog.h"
19 #include "lengthcombo.h"
20 #include "qt_helpers.h"
22 #include "controllers/ButtonController.h"
23 #include "controllers/ControlTabular.h"
25 #include "support/tostr.h"
27 #include <qcheckbox.h>
28 #include <qlineedit.h>
29 #include <qpushbutton.h>
30 #include "qsetborder.h"
37 typedef QController<ControlTabular, QView<QTabularDialog> > base_class;
39 QTabular::QTabular(Dialog & parent)
40 : base_class(parent, _("LyX: Table Settings"))
45 void QTabular::build_dialog()
47 dialog_.reset(new QTabularDialog(this));
49 bcview().setCancel(dialog_->closePB);
51 bcview().addReadOnly(dialog_->multicolumnCB);
52 bcview().addReadOnly(dialog_->rotateCellCB);
53 bcview().addReadOnly(dialog_->rotateTabularCB);
54 bcview().addReadOnly(dialog_->specialAlignmentED);
55 bcview().addReadOnly(dialog_->widthED);
56 bcview().addReadOnly(dialog_->widthUnit);
57 bcview().addReadOnly(dialog_->hAlignCB);
58 bcview().addReadOnly(dialog_->vAlignCB);
59 bcview().addReadOnly(dialog_->borderSetPB);
60 bcview().addReadOnly(dialog_->borderUnsetPB);
61 bcview().addReadOnly(dialog_->borders);
62 bcview().addReadOnly(dialog_->longTabularCB);
63 bcview().addReadOnly(dialog_->headerStatusCB);
64 bcview().addReadOnly(dialog_->headerBorderAboveCB);
65 bcview().addReadOnly(dialog_->headerBorderBelowCB);
66 bcview().addReadOnly(dialog_->firstheaderStatusCB);
67 bcview().addReadOnly(dialog_->firstheaderBorderAboveCB);
68 bcview().addReadOnly(dialog_->firstheaderBorderBelowCB);
69 bcview().addReadOnly(dialog_->firstheaderNoContentsCB);
70 bcview().addReadOnly(dialog_->footerStatusCB);
71 bcview().addReadOnly(dialog_->footerBorderAboveCB);
72 bcview().addReadOnly(dialog_->footerBorderBelowCB);
73 bcview().addReadOnly(dialog_->lastfooterStatusCB);
74 bcview().addReadOnly(dialog_->lastfooterBorderAboveCB);
75 bcview().addReadOnly(dialog_->lastfooterBorderBelowCB);
76 bcview().addReadOnly(dialog_->lastfooterNoContentsCB);
77 bcview().addReadOnly(dialog_->newpageCB);
81 bool QTabular::isValid()
87 void QTabular::update_borders()
89 LyXTabular const & tabular = controller().tabular();
90 int const cell = controller().getActiveCell();
91 bool const isMulticolumnCell = tabular.isMultiColumn(cell);
93 if (!isMulticolumnCell) {
94 dialog_->borders->setLeftEnabled(true);
95 dialog_->borders->setRightEnabled(true);
96 dialog_->borders->setTop(tabular.topLine(cell, true));
97 dialog_->borders->setBottom(tabular.bottomLine(cell, true));
98 dialog_->borders->setLeft(tabular.leftLine(cell, true));
99 dialog_->borders->setRight(tabular.rightLine(cell, true));
100 // repaint the setborder widget
101 dialog_->borders->repaint();
105 dialog_->borders->setTop(tabular.topLine(cell));
106 dialog_->borders->setBottom(tabular.bottomLine(cell));
107 // pay attention to left/right lines: they are only allowed
108 // to set if we are in first/last cell of row or if the left/right
109 // cell is also a multicolumn.
110 if (tabular.isFirstCellInRow(cell) || tabular.isMultiColumn(cell - 1)) {
111 dialog_->borders->setLeftEnabled(true);
112 dialog_->borders->setLeft(tabular.leftLine(cell));
114 dialog_->borders->setLeft(false);
115 dialog_->borders->setLeftEnabled(false);
117 if (tabular.isLastCellInRow(cell) || tabular.isMultiColumn(cell + 1)) {
118 dialog_->borders->setRightEnabled(true);
119 dialog_->borders->setRight(tabular.rightLine(cell));
121 dialog_->borders->setRight(false);
122 dialog_->borders->setRightEnabled(false);
124 // repaint the setborder widget
125 dialog_->borders->repaint();
129 void QTabular::update_contents()
131 LyXTabular const & tabular(controller().tabular());
132 int const cell = controller().getActiveCell();
134 int const row(tabular.row_of_cell(cell));
135 int const col(tabular.column_of_cell(cell));
137 dialog_->tabularRowED->setText(toqstr(tostr(row + 1)));
138 dialog_->tabularColumnED->setText(toqstr(tostr(col + 1)));
140 bool const multicol(tabular.isMultiColumn(cell));
142 dialog_->multicolumnCB->setChecked(multicol);
144 dialog_->rotateCellCB->setChecked(tabular.getRotateCell(cell));
145 dialog_->rotateTabularCB->setChecked(tabular.getRotateTabular());
147 dialog_->longTabularCB->setChecked(tabular.isLongTabular());
155 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
156 pwidth = tabular.getMColumnPWidth(cell);
158 special = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
159 pwidth = tabular.getColumnPWidth(cell);
162 dialog_->specialAlignmentED->setText(toqstr(special));
164 bool const isReadonly = bc().bp().isReadOnly();
165 dialog_->specialAlignmentED->setEnabled(!isReadonly);
167 LyXLength::UNIT default_unit = controller().useMetricUnits() ? LyXLength::CM : LyXLength::IN;
171 colwidth = pwidth.asString();
172 lengthToWidgets(dialog_->widthED, dialog_->widthUnit,
173 colwidth, default_unit);
175 dialog_->widthED->setEnabled(!isReadonly);
176 dialog_->widthUnit->setEnabled(!isReadonly);
178 dialog_->hAlignCB->clear();
179 dialog_->hAlignCB->insertItem(qt_("Left"));
180 dialog_->hAlignCB->insertItem(qt_("Center"));
181 dialog_->hAlignCB->insertItem(qt_("Right"));
182 if (!multicol && !pwidth.zero())
183 dialog_->hAlignCB->insertItem(qt_("Block"));
186 switch (tabular.getAlignment(cell)) {
190 case LYX_ALIGN_CENTER:
193 case LYX_ALIGN_RIGHT:
196 case LYX_ALIGN_BLOCK:
198 if (!multicol && !pwidth.zero())
206 dialog_->hAlignCB->setCurrentItem(align);
209 switch (tabular.getVAlignment(cell)) {
210 case LyXTabular::LYX_VALIGN_TOP:
213 case LyXTabular::LYX_VALIGN_MIDDLE:
216 case LyXTabular::LYX_VALIGN_BOTTOM:
225 dialog_->vAlignCB->setCurrentItem(valign);
227 dialog_->hAlignCB->setEnabled(true);
228 dialog_->vAlignCB->setEnabled(!pwidth.zero());
230 if (!tabular.isLongTabular()) {
231 dialog_->headerStatusCB->setChecked(false);
232 dialog_->headerBorderAboveCB->setChecked(false);
233 dialog_->headerBorderBelowCB->setChecked(false);
234 dialog_->firstheaderStatusCB->setChecked(false);
235 dialog_->firstheaderBorderAboveCB->setChecked(false);
236 dialog_->firstheaderBorderBelowCB->setChecked(false);
237 dialog_->firstheaderNoContentsCB->setChecked(false);
238 dialog_->footerStatusCB->setChecked(false);
239 dialog_->footerBorderAboveCB->setChecked(false);
240 dialog_->footerBorderBelowCB->setChecked(false);
241 dialog_->lastfooterStatusCB->setChecked(false);
242 dialog_->lastfooterBorderAboveCB->setChecked(false);
243 dialog_->lastfooterBorderBelowCB->setChecked(false);
244 dialog_->lastfooterNoContentsCB->setChecked(false);
245 dialog_->newpageCB->setChecked(false);
249 LyXTabular::ltType ltt;
251 bool row_set = tabular.getRowOfLTHead(row, ltt);
252 dialog_->headerStatusCB->setChecked(row_set);
254 dialog_->headerBorderAboveCB->setChecked(ltt.topDL);
255 dialog_->headerBorderBelowCB->setChecked(ltt.bottomDL);
258 dialog_->headerBorderAboveCB->setChecked(false);
259 dialog_->headerBorderBelowCB->setChecked(false);
260 dialog_->headerBorderAboveCB->setEnabled(false);
261 dialog_->headerBorderBelowCB->setEnabled(false);
262 dialog_->firstheaderNoContentsCB->setChecked(false);
263 dialog_->firstheaderNoContentsCB->setEnabled(false);
267 row_set = tabular.getRowOfLTFirstHead(row, ltt);
268 dialog_->firstheaderStatusCB->setChecked(row_set);
269 if (ltt.set && (!ltt.empty || !use_empty)) {
270 dialog_->firstheaderBorderAboveCB->setChecked(ltt.topDL);
271 dialog_->firstheaderBorderBelowCB->setChecked(ltt.bottomDL);
273 dialog_->firstheaderBorderAboveCB->setEnabled(false);
274 dialog_->firstheaderBorderBelowCB->setEnabled(false);
275 dialog_->firstheaderBorderAboveCB->setChecked(false);
276 dialog_->firstheaderBorderBelowCB->setChecked(false);
278 dialog_->firstheaderNoContentsCB->setChecked(ltt.empty);
280 dialog_->firstheaderStatusCB->setEnabled(false);
284 row_set = tabular.getRowOfLTFoot(row, ltt);
285 dialog_->footerStatusCB->setChecked(row_set);
287 dialog_->footerBorderAboveCB->setChecked(ltt.topDL);
288 dialog_->footerBorderBelowCB->setChecked(ltt.bottomDL);
291 dialog_->footerBorderAboveCB->setChecked(false);
292 dialog_->footerBorderBelowCB->setChecked(false);
293 dialog_->footerBorderAboveCB->setEnabled(false);
294 dialog_->footerBorderBelowCB->setEnabled(false);
295 dialog_->lastfooterNoContentsCB->setChecked(false);
296 dialog_->lastfooterNoContentsCB->setEnabled(false);
300 row_set = tabular.getRowOfLTLastFoot(row, ltt);
301 dialog_->lastfooterStatusCB->setChecked(row_set);
302 if (ltt.set && (!ltt.empty || !use_empty)) {
303 dialog_->lastfooterBorderAboveCB->setChecked(ltt.topDL);
304 dialog_->lastfooterBorderBelowCB->setChecked(ltt.bottomDL);
306 dialog_->lastfooterBorderAboveCB->setEnabled(false);
307 dialog_->lastfooterBorderBelowCB->setEnabled(false);
308 dialog_->lastfooterBorderAboveCB->setChecked(false);
309 dialog_->lastfooterBorderBelowCB->setChecked(false);
311 dialog_->lastfooterNoContentsCB->setChecked(ltt.empty);
313 dialog_->lastfooterStatusCB->setEnabled(false);
316 dialog_->newpageCB->setChecked(tabular.getLTNewPage(row));
320 void QTabular::closeGUI()
322 // ugly hack to auto-apply the stuff that hasn't been
323 // yet. don't let this continue to exist ...
325 // Subtle here, we must /not/ apply any changes and
326 // then refer to tabular, as it will have been freed
327 // since the changes update the actual controller().tabular()
328 LyXTabular const & tabular(controller().tabular());
330 // apply the fixed width values
331 int const cell = controller().getActiveCell();
332 bool const multicol = tabular.isMultiColumn(cell);
333 string width = widgetsToLength(dialog_->widthED, dialog_->widthUnit);
336 LyXLength llen = tabular.getColumnPWidth(cell);
337 LyXLength llenMulti = tabular.getMColumnPWidth(cell);
339 if (multicol && !llenMulti.zero())
340 width2 = llenMulti.asString();
341 else if (!multicol && !llen.zero())
342 width2 = llen.asString();
344 // apply the special alignment
345 string const sa1 = fromqstr(dialog_->specialAlignmentED->text());
349 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_MULTI);
351 sa2 = tabular.getAlignSpecial(cell, LyXTabular::SET_SPECIAL_COLUMN);
355 controller().set(LyXTabular::SET_SPECIAL_MULTI, sa1);
357 controller().set(LyXTabular::SET_SPECIAL_COLUMN, sa1);
360 if (width != width2) {
362 controller().set(LyXTabular::SET_MPWIDTH, width);
364 controller().set(LyXTabular::SET_PWIDTH, width);
368 } // namespace frontend