2 * \file GuiDelimiter.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 "GuiDelimiter.h"
15 #include "GuiApplication.h"
18 #include "qt_helpers.h"
23 #include <QListWidgetItem>
25 // Set to zero if unicode symbols are preferred.
35 QString const bigleft[] = {"", "bigl", "Bigl", "biggl", "Biggl"};
38 QString const bigright[] = {"", "bigr", "Bigr", "biggr", "Biggr"};
41 char const * const biggui[] = {N_("big[[delimiter size]]"), N_("Big[[delimiter size]]"),
42 N_("bigg[[delimiter size]]"), N_("Bigg[[delimiter size]]"), ""};
45 // FIXME: It might be better to fix the big delim LFUN to not require
46 // additional '\' prefix.
47 QString fix_name(QString const & str, bool big)
51 if (!big || str == "(" || str == ")" || str == "[" || str == "]"
52 || str == "|" || str == "/")
61 GuiDelimiter::GuiDelimiter(GuiDialog & parent)
62 : GuiView<GuiDelimiterDialog>(parent, _("Math Delimiter"))
66 void GuiDelimiter::build_dialog()
68 dialog_.reset(new GuiDelimiterDialog(this,
69 static_cast<GuiViewBase *>(controller().view())));
73 char_type GuiDelimiterDialog::doMatch(char_type const symbol) const
75 string const & str = form_->controller().texName(symbol);
77 if (str == "(") match = ")";
78 else if (str == ")") match = "(";
79 else if (str == "[") match = "]";
80 else if (str == "]") match = "[";
81 else if (str == "{") match = "}";
82 else if (str == "}") match = "{";
83 else if (str == "l") match = "r";
84 else if (str == "rceil") match = "lceil";
85 else if (str == "lceil") match = "rceil";
86 else if (str == "rfloor") match = "lfloor";
87 else if (str == "lfloor") match = "rfloor";
88 else if (str == "rangle") match = "langle";
89 else if (str == "langle") match = "rangle";
90 else if (str == "backslash") match = "/";
91 else if (str == "/") match = "backslash";
94 return form_->controller().mathSymbol(match).unicode;
98 GuiDelimiterDialog::GuiDelimiterDialog(GuiDelimiter * form, QWidget * parent)
99 : QDialog(parent), form_(form)
103 connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
105 setWindowTitle(qt_("LyX: Delimiters"));
106 setFocusProxy(leftLW);
108 leftLW->setViewMode(QListView::IconMode);
109 rightLW->setViewMode(QListView::IconMode);
111 typedef std::map<char_type, QListWidgetItem *> ListItems;
112 ListItems list_items;
113 // The last element is the empty one.
114 int const end = nr_latex_delimiters - 1;
115 for (int i = 0; i < end; ++i) {
116 string const delim = latex_delimiters[i];
117 MathSymbol const & ms = form_->controller().mathSymbol(delim);
118 QString symbol(ms.fontcode?
119 QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
120 QListWidgetItem * lwi = new QListWidgetItem(symbol);
121 lwi->setToolTip(toqstr(delim));
123 lyxfont.setFamily(ms.fontfamily);
124 QFont const & symbol_font = guiApp->guiFontLoader().get(lyxfont);
125 lwi->setFont(symbol_font);
126 list_items[ms.unicode] = lwi;
127 leftLW->addItem(lwi);
130 for (int i = 0; i != leftLW->count(); ++i) {
131 MathSymbol const & ms = form_->controller().mathSymbol(
132 fromqstr(leftLW->item(i)->toolTip()));
133 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
136 // The last element is the empty one.
137 leftLW->addItem(qt_("(None)"));
138 rightLW->addItem(qt_("(None)"));
140 sizeCO->addItem(qt_("Variable"));
142 for (int i = 0; *biggui[i]; ++i)
143 sizeCO->addItem(qt_(biggui[i]));
145 on_leftLW_currentRowChanged(0);
149 void GuiDelimiterDialog::updateTeXCode(int size)
151 bool const bigsize = size != 0;
153 QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
154 QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
157 tex_code_ = left_str + ' ' + right_str;
159 tex_code_ = bigleft[size] + ' '
161 + bigright[size] + ' '
165 // Generate TeX-code for GUI display.
166 // FIXME: Instead of reconstructing the TeX code it would be nice to
167 // FIXME: retrieve the LateX code directly from mathed.
168 // In all cases, we want the '\' prefix if needed, so we pass 'true'
170 left_str = fix_name(leftLW->currentItem()->toolTip(), true);
171 right_str = fix_name(rightLW->currentItem()->toolTip(), true);
174 code_str = "\\left" + left_str + " \\right" + right_str;
176 // There should be nothing in the TeX-code when the delimiter is "None".
178 code_str = "\\" + bigleft[size] + left_str + ' ';
179 if (right_str != ".")
180 code_str += "\\" + bigright[size] + right_str;
183 texCodeL->setText(qt_("TeX Code: ") + code_str);
187 void GuiDelimiterDialog::on_insertPB_clicked()
189 if (sizeCO->currentIndex() == 0)
190 form_->controller().dispatchDelim(fromqstr(tex_code_));
192 QString command = '"' + tex_code_ + '"';
193 command.replace(' ', "\" \"");
194 form_->controller().dispatchBigDelim(fromqstr(command));
199 void GuiDelimiterDialog::on_sizeCO_activated(int index)
201 updateTeXCode(index);
205 void GuiDelimiterDialog::on_leftLW_itemActivated(QListWidgetItem *)
207 on_insertPB_clicked();
212 void GuiDelimiterDialog::on_rightLW_itemActivated(QListWidgetItem *)
214 on_insertPB_clicked();
219 void GuiDelimiterDialog::on_leftLW_currentRowChanged(int item)
221 if (matchCB->isChecked())
222 rightLW->setCurrentRow(item);
224 updateTeXCode(sizeCO->currentIndex());
228 void GuiDelimiterDialog::on_rightLW_currentRowChanged(int item)
230 if (matchCB->isChecked())
231 leftLW->setCurrentRow(item);
233 updateTeXCode(sizeCO->currentIndex());
237 void GuiDelimiterDialog::on_matchCB_stateChanged(int state)
239 if (state == Qt::Checked)
240 on_leftLW_currentRowChanged(leftLW->currentRow());
242 updateTeXCode(sizeCO->currentIndex());
246 } // namespace frontend
249 #include "GuiDelimiter_moc.cpp"