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.
33 static QString const bigleft[] = {"", "bigl", "Bigl", "biggl", "Biggl"};
35 static QString const bigright[] = {"", "bigr", "Bigr", "biggr", "Biggr"};
37 static char const * const biggui[] = {
38 N_("big[[delimiter size]]"),
39 N_("Big[[delimiter size]]"),
40 N_("bigg[[delimiter size]]"),
41 N_("Bigg[[delimiter size]]"),
46 // FIXME: It might be better to fix the big delim LFUN to not require
47 // additional '\' prefix.
48 static QString fix_name(QString const & str, bool big)
52 if (!big || str == "(" || str == ")" || str == "[" || str == "]"
53 || str == "|" || str == "/")
60 GuiDelimiterDialog::GuiDelimiterDialog(LyXView & lv)
61 : GuiDialog(lv, "mathdelimiter")
64 setViewTitle(_("Math Delimiter"));
65 setController(new ControlMath(*this));
67 connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
69 setWindowTitle(qt_("LyX: Delimiters"));
70 setFocusProxy(leftLW);
72 leftLW->setViewMode(QListView::IconMode);
73 rightLW->setViewMode(QListView::IconMode);
75 typedef std::map<char_type, QListWidgetItem *> ListItems;
77 // The last element is the empty one.
78 int const end = nr_latex_delimiters - 1;
79 for (int i = 0; i < end; ++i) {
80 string const delim = latex_delimiters[i];
81 MathSymbol const & ms = controller().mathSymbol(delim);
82 QString symbol(ms.fontcode?
83 QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
84 QListWidgetItem * lwi = new QListWidgetItem(symbol);
85 lwi->setToolTip(toqstr(delim));
87 lyxfont.setFamily(ms.fontfamily);
88 QFont const & symbol_font = guiApp->guiFontLoader().get(lyxfont);
89 lwi->setFont(symbol_font);
90 list_items[ms.unicode] = lwi;
94 for (int i = 0; i != leftLW->count(); ++i) {
95 MathSymbol const & ms = controller().mathSymbol(
96 fromqstr(leftLW->item(i)->toolTip()));
97 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
100 // The last element is the empty one.
101 leftLW->addItem(qt_("(None)"));
102 rightLW->addItem(qt_("(None)"));
104 sizeCO->addItem(qt_("Variable"));
106 for (int i = 0; *biggui[i]; ++i)
107 sizeCO->addItem(qt_(biggui[i]));
109 on_leftLW_currentRowChanged(0);
110 bc().setPolicy(ButtonPolicy::IgnorantPolicy);
114 ControlMath & GuiDelimiterDialog::controller() const
116 return static_cast<ControlMath &>(GuiDialog::controller());
120 char_type GuiDelimiterDialog::doMatch(char_type const symbol) const
122 string const & str = controller().texName(symbol);
124 if (str == "(") match = ")";
125 else if (str == ")") match = "(";
126 else if (str == "[") match = "]";
127 else if (str == "]") match = "[";
128 else if (str == "{") match = "}";
129 else if (str == "}") match = "{";
130 else if (str == "l") match = "r";
131 else if (str == "rceil") match = "lceil";
132 else if (str == "lceil") match = "rceil";
133 else if (str == "rfloor") match = "lfloor";
134 else if (str == "lfloor") match = "rfloor";
135 else if (str == "rangle") match = "langle";
136 else if (str == "langle") match = "rangle";
137 else if (str == "backslash") match = "/";
138 else if (str == "/") match = "backslash";
141 return controller().mathSymbol(match).unicode;
145 void GuiDelimiterDialog::updateTeXCode(int size)
147 bool const bigsize = size != 0;
149 QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
150 QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
153 tex_code_ = left_str + ' ' + right_str;
155 tex_code_ = bigleft[size] + ' '
157 + bigright[size] + ' '
161 // Generate TeX-code for GUI display.
162 // FIXME: Instead of reconstructing the TeX code it would be nice to
163 // FIXME: retrieve the LateX code directly from mathed.
164 // In all cases, we want the '\' prefix if needed, so we pass 'true'
166 left_str = fix_name(leftLW->currentItem()->toolTip(), true);
167 right_str = fix_name(rightLW->currentItem()->toolTip(), true);
170 code_str = "\\left" + left_str + " \\right" + right_str;
172 // There should be nothing in the TeX-code when the delimiter is "None".
174 code_str = "\\" + bigleft[size] + left_str + ' ';
175 if (right_str != ".")
176 code_str += "\\" + bigright[size] + right_str;
179 texCodeL->setText(qt_("TeX Code: ") + code_str);
183 void GuiDelimiterDialog::on_insertPB_clicked()
185 if (sizeCO->currentIndex() == 0)
186 controller().dispatchDelim(fromqstr(tex_code_));
188 QString command = '"' + tex_code_ + '"';
189 command.replace(' ', "\" \"");
190 controller().dispatchBigDelim(fromqstr(command));
195 void GuiDelimiterDialog::on_sizeCO_activated(int index)
197 updateTeXCode(index);
201 void GuiDelimiterDialog::on_leftLW_itemActivated(QListWidgetItem *)
203 on_insertPB_clicked();
208 void GuiDelimiterDialog::on_rightLW_itemActivated(QListWidgetItem *)
210 on_insertPB_clicked();
215 void GuiDelimiterDialog::on_leftLW_currentRowChanged(int item)
217 if (matchCB->isChecked())
218 rightLW->setCurrentRow(item);
220 updateTeXCode(sizeCO->currentIndex());
224 void GuiDelimiterDialog::on_rightLW_currentRowChanged(int item)
226 if (matchCB->isChecked())
227 leftLW->setCurrentRow(item);
229 updateTeXCode(sizeCO->currentIndex());
233 void GuiDelimiterDialog::on_matchCB_stateChanged(int state)
235 if (state == Qt::Checked)
236 on_leftLW_currentRowChanged(leftLW->currentRow());
238 updateTeXCode(sizeCO->currentIndex());
242 } // namespace frontend
245 #include "GuiDelimiter_moc.cpp"