]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QDelimiterDialog.C
Add vertical spacer.
[lyx.git] / src / frontends / qt4 / QDelimiterDialog.C
1 /**
2  * \file QDelimiterDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "QDelimiterDialog.h"
14 #include "QMath.h"
15
16 #include "qt_helpers.h"
17 #include "controllers/ControlMath.h"
18
19 #include "gettext.h"
20
21 #include <QPixmap>
22 #include <QCheckBox>
23 #include <QListWidgetItem>
24
25 #include <sstream>
26
27 using std::string;
28
29 namespace lyx {
30 namespace frontend {
31
32 namespace {
33
34 char const * const bigleft[]  = {"bigl", "Bigl", "biggl", "Biggl", ""};
35
36
37 char const * const bigright[] = {"bigr", "Bigr", "biggr", "Biggr", ""};
38
39
40 char const * const biggui[]   = {N_("big[[delimiter size]]"), N_("Big[[delimiter size]]"),
41         N_("bigg[[delimiter size]]"), N_("Bigg[[delimiter size]]"), ""};
42
43
44 string do_match(string const & str)
45 {
46         if (str == "(") return ")";
47         if (str == ")") return "(";
48         if (str == "[") return "]";
49         if (str == "]") return "[";
50         if (str == "{") return "}";
51         if (str == "}") return "{";
52         if (str == "l") return "r";
53         if (str == "rceil") return "lceil";
54         if (str == "lceil") return "rceil";
55         if (str == "rfloor") return "lfloor";
56         if (str == "lfloor") return "rfloor";
57         if (str == "rangle") return "langle";
58         if (str == "langle") return "rangle";
59         if (str == "\\") return "/";
60         if (str == "/") return "\\";
61         return str;
62 }
63
64
65 string fix_name(string const & str, bool big)
66 {
67         if (str.empty())
68                 return ".";
69         if (!big || str == "(" || str == ")" || str == "[" || str == "]"
70             || str == "|" || str == "/")
71                 return str;
72
73         return "\\" + str;
74 }
75
76 } // namespace anon
77
78
79 QDelimiterDialog::QDelimiterDialog(QMathDelimiter * form)
80         : form_(form)
81 {
82         setupUi(this);
83
84         connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
85         connect(insertPB, SIGNAL(clicked()), this, SLOT(insertClicked()));
86
87         setWindowTitle(qt_("LyX: Delimiters"));
88         setFocusProxy(leftLW);
89         
90         // The last element is the empty one.
91         for (int i = 0; i < nr_latex_delimiters - 1; ++i) {
92                 docstring const left_d(1,
93                         form_->controller().mathSymbol(latex_delimiters[i]));
94                 docstring const right_d(1,
95                         form_->controller().mathSymbol(do_match(latex_delimiters[i])));
96                 leftLW->addItem(toqstr(left_d));
97                 rightLW->addItem(toqstr(right_d));
98         }
99
100         leftLW->addItem(qt_("(None)"));
101         rightLW->addItem(qt_("(None)"));
102
103         sizeCO->addItem(qt_("Variable"));
104
105         for (int i = 0; *biggui[i]; ++i)
106                 sizeCO->addItem(qt_(biggui[i]));
107
108         on_leftLW_currentRowChanged(0);
109 }
110
111
112 void QDelimiterDialog::insertClicked()
113 {
114         string left_str;
115         string right_str;
116         if (leftLW->currentRow() < leftLW->count() - 1)
117                 left_str = form_->controller().texName(qstring_to_ucs4(leftLW->currentItem()->text())[0]);
118         if (rightLW->currentRow() < rightLW->count() - 1)
119                 right_str = form_->controller().texName(qstring_to_ucs4(rightLW->currentItem()->text())[0]);
120
121         int const size_ = sizeCO->currentIndex();
122         if (size_ == 0) {
123                 form_->controller().dispatchDelim(
124                         fix_name(left_str, false) + ' ' +
125                         fix_name(right_str, false));
126         } else {
127                 std::ostringstream os;
128                 os << '"' << bigleft[size_ - 1] << "\" \""
129                    << fix_name(left_str, true) << "\" \""
130                    << bigright[size_ - 1] << "\" \""
131                    << fix_name(right_str, true) << '"';
132                 form_->controller().dispatchBigDelim(os.str());
133         }
134 }
135
136
137 void QDelimiterDialog::on_leftLW_itemActivated(QListWidgetItem *)
138 {
139         insertClicked();
140         accept();
141 }
142
143
144 void QDelimiterDialog::on_rightLW_itemActivated(QListWidgetItem *)
145 {
146         insertClicked();
147         accept();
148 }
149
150
151 void QDelimiterDialog::on_leftLW_currentRowChanged(int item)
152 {
153         if (matchCB->isChecked())
154                 rightLW->setCurrentRow(item);
155
156         // Display the associated TeX name.
157         if (leftLW->currentRow() == leftLW->count() - 1)
158                 texCodeL->clear();
159         else {
160                 QString const str = toqstr(form_->controller().texName(
161                         qstring_to_ucs4(leftLW->currentItem()->text())[0]));
162                 texCodeL->setText("TeX code: \\" + str);
163         }
164 }
165
166
167 void QDelimiterDialog::on_rightLW_currentRowChanged(int item)
168 {
169         if (matchCB->isChecked())
170                 leftLW->setCurrentRow(item);
171
172         // Display the associated TeX name.
173         if (rightLW->currentRow() == leftLW->count() - 1)
174                 texCodeL->clear();
175         else {
176                 QString const str = toqstr(form_->controller().texName(
177                         qstring_to_ucs4(rightLW->currentItem()->text())[0]));
178                 texCodeL->setText("TeX code: \\" + str);
179         }
180 }
181
182
183 void QDelimiterDialog::on_matchCB_stateChanged(int state)
184 {
185         if (state == Qt::Checked)
186                 on_leftLW_currentRowChanged(leftLW->currentRow());
187 }
188
189
190 } // namespace frontend
191 } // namespace lyx
192
193 #include "QDelimiterDialog_moc.cpp"