2 * \file GuiDelimiterDialog.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "GuiDelimiterDialog.h"
15 #include "GuiApplication.h"
19 #include "qt_helpers.h"
20 #include "controllers/ControlMath.h"
26 #include <QListWidgetItem>
30 // Set to zero if unicode symbols are preferred.
40 QString const bigleft[] = {"", "bigl", "Bigl", "biggl", "Biggl"};
43 QString const bigright[] = {"", "bigr", "Bigr", "biggr", "Biggr"};
46 char const * const biggui[] = {N_("big[[delimiter size]]"), N_("Big[[delimiter size]]"),
47 N_("bigg[[delimiter size]]"), N_("Bigg[[delimiter size]]"), ""};
50 // FIXME: It might be better to fix the big delim LFUN to not require
51 // additional '\' prefix.
52 QString fix_name(QString const & str, bool big)
56 if (!big || str == "(" || str == ")" || str == "[" || str == "]"
57 || str == "|" || str == "/")
66 typedef QController<ControlMath, GuiView<GuiDelimiterDialog> > delimiter_base;
68 GuiMathDelimiter::GuiMathDelimiter(Dialog & parent)
69 : delimiter_base(parent, _("Math Delimiter"))
73 void GuiMathDelimiter::build_dialog()
75 dialog_.reset(new GuiDelimiterDialog(this,
76 static_cast<GuiViewBase *>(controller().view())));
80 char_type GuiDelimiterDialog::doMatch(char_type const symbol) const
82 string const & str = form_->controller().texName(symbol);
84 if (str == "(") match = ")";
85 else if (str == ")") match = "(";
86 else if (str == "[") match = "]";
87 else if (str == "]") match = "[";
88 else if (str == "{") match = "}";
89 else if (str == "}") match = "{";
90 else if (str == "l") match = "r";
91 else if (str == "rceil") match = "lceil";
92 else if (str == "lceil") match = "rceil";
93 else if (str == "rfloor") match = "lfloor";
94 else if (str == "lfloor") match = "rfloor";
95 else if (str == "rangle") match = "langle";
96 else if (str == "langle") match = "rangle";
97 else if (str == "backslash") match = "/";
98 else if (str == "/") match = "backslash";
101 return form_->controller().mathSymbol(match).unicode;
105 GuiDelimiterDialog::GuiDelimiterDialog(GuiMathDelimiter * form, QWidget * parent)
106 : QDialog(parent), form_(form)
110 connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
112 setWindowTitle(qt_("LyX: Delimiters"));
113 setFocusProxy(leftLW);
115 leftLW->setViewMode(QListView::IconMode);
116 rightLW->setViewMode(QListView::IconMode);
118 typedef std::map<char_type, QListWidgetItem *> ListItems;
119 ListItems list_items;
120 // The last element is the empty one.
121 int const end = nr_latex_delimiters - 1;
122 for (int i = 0; i < end; ++i) {
123 string const delim = latex_delimiters[i];
124 MathSymbol const & ms = form_->controller().mathSymbol(delim);
125 QString symbol(ms.fontcode?
126 QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
127 QListWidgetItem * lwi = new QListWidgetItem(symbol);
128 lwi->setToolTip(toqstr(delim));
130 lyxfont.setFamily(ms.fontfamily);
131 QFont const & symbol_font = guiApp->guiFontLoader().get(lyxfont);
132 lwi->setFont(symbol_font);
133 list_items[ms.unicode] = lwi;
134 leftLW->addItem(lwi);
137 for (int i = 0; i != leftLW->count(); ++i) {
138 MathSymbol const & ms = form_->controller().mathSymbol(
139 fromqstr(leftLW->item(i)->toolTip()));
140 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
143 // The last element is the empty one.
144 leftLW->addItem(qt_("(None)"));
145 rightLW->addItem(qt_("(None)"));
147 sizeCO->addItem(qt_("Variable"));
149 for (int i = 0; *biggui[i]; ++i)
150 sizeCO->addItem(qt_(biggui[i]));
152 on_leftLW_currentRowChanged(0);
156 void GuiDelimiterDialog::updateTeXCode(int size)
158 bool const bigsize = size != 0;
160 QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
161 QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
164 tex_code_ = left_str + ' ' + right_str;
166 tex_code_ = bigleft[size] + ' '
168 + bigright[size] + ' '
172 // Generate TeX-code for GUI display.
173 // FIXME: Instead of reconstructing the TeX code it would be nice to
174 // FIXME: retrieve the LateX code directly from mathed.
175 // In all cases, we want the '\' prefix if needed, so we pass 'true'
177 left_str = fix_name(leftLW->currentItem()->toolTip(), true);
178 right_str = fix_name(rightLW->currentItem()->toolTip(), true);
181 code_str = "\\left" + left_str + " \\right" + right_str;
183 // There should be nothing in the TeX-code when the delimiter is "None".
185 code_str = "\\" + bigleft[size] + left_str + ' ';
186 if (right_str != ".")
187 code_str += "\\" + bigright[size] + right_str;
190 texCodeL->setText(qt_("TeX Code: ") + code_str);
194 void GuiDelimiterDialog::on_insertPB_clicked()
196 if (sizeCO->currentIndex() == 0)
197 form_->controller().dispatchDelim(fromqstr(tex_code_));
199 QString command = '"' + tex_code_ + '"';
200 command.replace(' ', "\" \"");
201 form_->controller().dispatchBigDelim(fromqstr(command));
206 void GuiDelimiterDialog::on_sizeCO_activated(int index)
208 updateTeXCode(index);
212 void GuiDelimiterDialog::on_leftLW_itemActivated(QListWidgetItem *)
214 on_insertPB_clicked();
219 void GuiDelimiterDialog::on_rightLW_itemActivated(QListWidgetItem *)
221 on_insertPB_clicked();
226 void GuiDelimiterDialog::on_leftLW_currentRowChanged(int item)
228 if (matchCB->isChecked())
229 rightLW->setCurrentRow(item);
231 updateTeXCode(sizeCO->currentIndex());
235 void GuiDelimiterDialog::on_rightLW_currentRowChanged(int item)
237 if (matchCB->isChecked())
238 leftLW->setCurrentRow(item);
240 updateTeXCode(sizeCO->currentIndex());
244 void GuiDelimiterDialog::on_matchCB_stateChanged(int state)
246 if (state == Qt::Checked)
247 on_leftLW_currentRowChanged(leftLW->currentRow());
249 updateTeXCode(sizeCO->currentIndex());
253 } // namespace frontend
256 #include "GuiDelimiterDialog_moc.cpp"