]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QDelimiterDialog.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / 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
15 #include "iconpalette.h"
16 #include "QMath.h"
17 #include "qt_helpers.h"
18
19 #include "controllers/ControlMath.h"
20
21 #include "gettext.h"
22
23 #include <qlabel.h>
24 #include <qpixmap.h>
25 #include <qcheckbox.h>
26 #include <qcombobox.h>
27
28 #include <sstream>
29
30
31 using std::string;
32
33 namespace lyx {
34 namespace frontend {
35
36 namespace {
37
38 char const * delim[] = {
39         "(", ")", "{", "}", "[", "]",
40         "lceil", "rceil", "lfloor", "rfloor", "langle", "rangle",
41         "uparrow", "Uparrow", "downarrow", "Downarrow",
42         "|", "Vert", "slash", "backslash", ""
43 };
44
45
46 char const * const bigleft[]  = {"bigl", "Bigl", "biggl", "Biggl", ""};
47 char const * const bigright[] = {"bigr", "Bigr", "biggr", "Biggr", ""};
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         : QDelimiterDialogBase(0, 0, false, 0),
92         form_(form)
93 {
94         setCaption(qt_("LyX: Delimiters"));
95
96         for (int i = 0; *delim[i]; ++i) {
97                 string xpm(find_xpm(delim[i]));
98                 leftIP->add(QPixmap(toqstr(xpm)), delim[i], delim[i]);
99                 rightIP->add(QPixmap(toqstr(xpm)), delim[i], delim[i]);
100         }
101
102         string empty_xpm(find_xpm("empty"));
103
104         leftIP->add(QPixmap(toqstr(empty_xpm)), "empty", "empty");
105         rightIP->add(QPixmap(toqstr(empty_xpm)), "empty", "empty");
106         delimSize->insertItem(qt_("Variable size"));
107         for (int i = 0; *biggui[i]; ++i)
108                 delimSize->insertItem(qt_(biggui[i]));
109         size_ = 0;
110         // Leave these std:: qualifications alone !
111         connect(leftIP, SIGNAL(button_clicked(const std::string &)),
112                 this, SLOT(ldelim_clicked(const std::string &)));
113         connect(rightIP, SIGNAL(button_clicked(const std::string &)),
114                 this, SLOT(rdelim_clicked(const std::string &)));
115         connect(delimSize, SIGNAL(activated(int)),
116                 this, SLOT(size_selected(int)) );
117         ldelim_clicked("(");
118         rdelim_clicked(")");
119 }
120
121
122 void QDelimiterDialog::insertClicked()
123 {
124         if (size_ == 0) {
125                 form_->controller().dispatchDelim(
126                         fix_name(left_, false) + ' ' +
127                         fix_name(right_, false));
128         } else {
129                 std::ostringstream os;
130                 os << '"' << bigleft[size_ - 1] << "\" \""
131                    << fix_name(left_, true) << "\" \""
132                    << bigright[size_ - 1] << "\" \""
133                    << fix_name(right_, true) << '"';
134                 form_->controller().dispatchBigDelim(os.str());
135         }
136 }
137
138
139 void QDelimiterDialog::set_label(QLabel * label, const string & str)
140 {
141         label->setUpdatesEnabled(false);
142         label->setPixmap(QPixmap(toqstr(find_xpm(str))));
143         label->setUpdatesEnabled(true);
144         label->update();
145 }
146
147
148 void QDelimiterDialog::ldelim_clicked(const string & str)
149 {
150         left_ = str;
151
152         set_label(leftPI, left_);
153         if (matchCB->isChecked()) {
154                 right_ = do_match(left_);
155                 set_label(rightPI, right_);
156         }
157 }
158
159
160 void QDelimiterDialog::rdelim_clicked(const string & str)
161 {
162         right_ = str;
163
164         set_label(rightPI, right_);
165         if (matchCB->isChecked()) {
166                 left_ = do_match(right_);
167                 set_label(leftPI, left_);
168         }
169 }
170
171
172 void QDelimiterDialog::size_selected(int index)
173 {
174         size_ = index;
175 }
176
177 } // namespace frontend
178 } // namespace lyx