]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QDelimiterDialog.C
This commit saves the need to check for lyx::use_gui in a number of places.
[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
24
25 #include <sstream>
26
27 using std::string;
28
29 namespace lyx {
30 namespace frontend {
31
32 namespace {
33
34 char const * delim[] = {
35         "(", ")", "{", "}", "[", "]",
36         "lceil", "rceil", "lfloor", "rfloor", "langle", "rangle",
37         "uparrow", "Uparrow", "downarrow", "Downarrow",
38         "|", "Vert", "slash", "backslash", ""
39 };
40
41
42 char const * const bigleft[]  = {"bigl", "Bigl", "biggl", "Biggl", ""};
43
44
45 char const * const bigright[] = {"bigr", "Bigr", "biggr", "Biggr", ""};
46
47
48 char const * const biggui[]   = {N_("big size"), N_("Big size"),
49         N_("bigg size"), N_("Bigg size"), ""};
50
51
52 string do_match(const string & str)
53 {
54         if (str == "(") return ")";
55         if (str == ")") return "(";
56         if (str == "[") return "]";
57         if (str == "]") return "[";
58         if (str == "{") return "}";
59         if (str == "}") return "{";
60         if (str == "l") return "r";
61         if (str == "rceil") return "lceil";
62         if (str == "lceil") return "rceil";
63         if (str == "rfloor") return "lfloor";
64         if (str == "lfloor") return "rfloor";
65         if (str == "rangle") return "langle";
66         if (str == "langle") return "rangle";
67         if (str == "backslash") return "slash";
68         if (str == "slash") return "backslash";
69         return str;
70 }
71
72
73 string fix_name(const string & str, bool big)
74 {
75         if (str == "slash")
76                 return "/";
77         if (str == "backslash")
78                 return "\\";
79         if (str == "empty")
80                 return ".";
81         if (!big || str == "(" || str == ")" || str == "[" || str == "]")
82                 return str;
83
84         return "\\" + str;
85 }
86
87 } // namespace anon
88
89
90 QDelimiterDialog::QDelimiterDialog(QMathDelimiter * form)
91         : form_(form)
92 {
93         setupUi(this);
94
95         connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
96         connect(insertPB, SIGNAL(clicked()), this, SLOT(insertClicked()));
97
98         setWindowTitle(qt_("LyX: Delimiters"));
99
100         for (int i = 0; *delim[i]; ++i) {
101                 QPixmap pm = QPixmap(toqstr(find_xpm(delim[i])));
102                 leftCO->addItem(QIcon(pm), "");
103                 rightCO->addItem(QIcon(pm), "");
104         }
105
106         string empty_xpm(find_xpm("empty"));
107         leftCO->addItem(QIcon(QPixmap(toqstr(empty_xpm))), qt_("(None)"));
108         rightCO->addItem(QIcon(QPixmap(toqstr(empty_xpm))), qt_("(None)"));
109
110         sizeCO->addItem(qt_(N_("Variable size")));
111
112         for (int i = 0; *biggui[i]; ++i)
113                 sizeCO->addItem(qt_(biggui[i]));
114
115         on_leftCO_activated(0);
116 }
117
118
119 void QDelimiterDialog::insertClicked()
120 {
121         string const left_ = delim[leftCO->currentIndex()];
122         string const right_ = delim[rightCO->currentIndex()];
123         int const size_ = sizeCO->currentIndex();
124
125         if (size_ == 0) {
126                 form_->controller().dispatchDelim(
127                         fix_name(left_, false) + ' ' +
128                         fix_name(right_, false));
129         } else {
130                 std::ostringstream os;
131                 os << '"' << bigleft[size_ - 1] << "\" \""
132                    << fix_name(left_, true) << "\" \""
133                    << bigright[size_ - 1] << "\" \""
134                    << fix_name(right_, true) << '"';
135                 form_->controller().dispatchBigDelim(os.str());
136         }
137
138 }
139
140
141 void QDelimiterDialog::on_leftCO_activated(int item)
142 {
143         if (matchCB->isChecked()) {
144                 string const match = do_match(delim[item]);
145                 int k = 0;
146                 while (delim[k] && delim[k] != match)
147                         ++k;
148                 rightCO->setCurrentIndex(k);
149         }
150 }
151
152
153 void QDelimiterDialog::on_rightCO_activated(int item)
154 {
155         if (matchCB->isChecked()) {
156                 string const match = do_match(delim[item]);
157                 int k = 0;
158                 while (delim[k] && delim[k] != match)
159                         ++k;
160                 leftCO->setCurrentIndex(k);
161         }
162 }
163
164
165 void QDelimiterDialog::on_matchCB_stateChanged(int state)
166 {
167         if (state == Qt::Checked)
168                 on_leftCO_activated(leftCO->currentIndex());
169 }
170
171
172 } // namespace frontend
173 } // namespace lyx
174
175 #include "QDelimiterDialog_moc.cpp"