3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "QListings.h"
16 #include "qt_helpers.h"
17 #include "controllers/ControlListings.h"
18 #include "insets/InsetListingsParams.h"
21 #include "support/convert.h"
22 #include "support/lstrings.h"
25 #include <QCloseEvent>
26 #include <QPushButton>
28 #include <QRegExpValidator>
33 using lyx::support::getVectorFromString;
34 using lyx::support::getStringFromVector;
35 using lyx::support::prefixIs;
36 using lyx::support::contains;
41 /////////////////////////////////////////////////////////////////////
45 /////////////////////////////////////////////////////////////////////
48 string const allowed_languages =
49 "no language\nBAP\nACSL\nAda\nALGOL\nC\nC++\nCaml\nClean\nCobol\n"
50 "Comal 80\ncsh\nDelphi\nEiffel\nElan\nEuphoria\nFortran\nHaskell\n"
51 "HTML\nIDL\nJava\nLisp\nLogo\nmake\nMathematica\nMatlab\nMercury\n"
52 "Miranda\nML\nModula-2\nOberon-2\nOCL\nPascal\nPerl\nPHP\nPL/I\nPOV\n"
53 "Python\nProlog\nR\nS\nSAS\nSHELXL\nSimula\ntcl\nSQL\nTeX\nVBScript\n"
55 string const allowed_fontsizes = "default\ntiny\nscriptsize\nfootnotesize\nsmall\n"
56 "normalsize\nlarge\nLarge";
57 string const allowed_fontstyles = "default\nrmfamily\nttfamily\nsffamily";
59 QListingsDialog::QListingsDialog(QListings * form)
64 connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
65 connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
67 connect(languageCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
68 connect(inlineCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(floatCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
70 connect(placementLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
71 connect(numberLeftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
72 connect(numberRightCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
73 connect(numberStepLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
74 connect(numberFontSizeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
75 connect(firstlineLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
76 connect(lastlineLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
77 connect(fontsizeCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
78 connect(fontstyleCO, SIGNAL(currentIndexChanged(int)), this, SLOT(change_adaptor()));
79 connect(breaklinesCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
80 connect(spaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
81 connect(extendedcharsCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
83 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
84 connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params()));
88 void QListingsDialog::closeEvent(QCloseEvent * e)
95 void QListingsDialog::change_adaptor()
101 string QListingsDialog::construct_params()
103 string language = fromqstr(languageCO->currentText());
105 bool float_ = floatCB->checkState() == Qt::Checked;
106 string placement = fromqstr(placementLE->text());
108 bool left = numberLeftCB->checkState() == Qt::Checked;
109 bool right = numberRightCB->checkState() == Qt::Checked;
110 string stepnumber = fromqstr(numberStepLE->text());
111 string numberfontsize = fromqstr(numberFontSizeCO->currentText());
112 string firstline = fromqstr(firstlineLE->text());
113 string lastline = fromqstr(lastlineLE->text());
115 string fontsize = fromqstr(fontsizeCO->currentText());
116 string fontstyle = fromqstr(fontstyleCO->currentText());
118 if (fontsize != "default")
119 basicstyle = "\\" + fontsize;
120 if (fontstyle != "default")
121 basicstyle += "\\" + fontstyle;
122 bool breakline = breaklinesCB->checkState() == Qt::Checked;
123 bool space = spaceCB->checkState() == Qt::Checked;
124 bool extendedchars = extendedcharsCB->checkState() == Qt::Checked;
125 string extra = fromqstr(listingsED->toPlainText());
128 InsetListingsParams par;
129 if (language != "no language")
130 par.addParam("language", language);
132 par.addParam("float", "");
133 if (!placement.empty())
134 par.addParam("floatplacement", placement);
136 par.addParam("numbers", "left");
138 par.addParam("numbers", "right");
139 if (numberfontsize != "default")
140 par.addParam("numberstyle", "\\" + numberfontsize);
141 if (!firstline.empty())
142 par.addParam("firstline", firstline);
143 if (!lastline.empty())
144 par.addParam("lastline", lastline);
145 if (!stepnumber.empty())
146 par.addParam("stepnumber", stepnumber);
147 if (!basicstyle.empty())
148 par.addParam("basicstyle", basicstyle);
150 par.addParam("breaklines", "true");
152 par.addParam("showspaces", "true");
154 par.addParam("extendedchars", "true");
155 par.addParams(extra);
160 void QListingsDialog::validate_listings_params()
162 static bool isOK = true;
164 InsetListingsParams par(construct_params());
167 listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
168 okPB->setEnabled(true);
170 } catch (invalidParam & e) {
172 listingsTB->setPlainText(e.what());
173 okPB->setEnabled(false);
177 /////////////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////////////
183 typedef QController<ControlListings, QView<QListingsDialog> > listings_wrap_base_class;
185 QListings::QListings(Dialog & parent)
186 : listings_wrap_base_class(parent, _("Program Listings Settings"))
191 void QListings::build_dialog()
193 dialog_.reset(new QListingsDialog(this));
195 bcview().setOK(dialog_->okPB);
196 bcview().setCancel(dialog_->closePB);
197 dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
203 /// not used right now.
204 void QListings::apply()
206 InsetListingsParams & params = controller().params();
207 params.setInline(dialog_->inlineCB->isChecked());
208 params.setParams(dialog_->construct_params());
209 controller().setParams(params);
213 void QListings::update_contents()
215 // first prepare all choices
216 vector<string> const languages =
217 getVectorFromString(allowed_languages, "\n");
218 vector<string> const fontstyles =
219 getVectorFromString(allowed_fontstyles, "\n");
220 vector<string> const fontsizes =
221 getVectorFromString(allowed_fontsizes, "\n");
223 dialog_->languageCO->clear();
224 for (vector<string>::const_iterator it = languages.begin();
225 it != languages.end(); ++it) {
226 dialog_->languageCO->addItem(toqstr(*it));
228 dialog_->fontstyleCO->clear();
229 dialog_->fontstyleCO->setEditable(false);
230 for (vector<string>::const_iterator it = fontstyles.begin();
231 it != fontstyles.end(); ++it) {
232 dialog_->fontstyleCO->addItem(toqstr(*it));
234 dialog_->fontsizeCO->clear();
235 dialog_->fontsizeCO->setEditable(false);
236 dialog_->numberFontSizeCO->clear();
237 dialog_->numberFontSizeCO->setEditable(false);
238 for (vector<string>::const_iterator it = fontsizes.begin();
239 it != fontsizes.end(); ++it) {
240 dialog_->fontsizeCO->addItem(toqstr(*it));
241 dialog_->numberFontSizeCO->addItem(toqstr(*it));
245 dialog_->numberStepLE->setValidator(new QIntValidator(0, 1000000, this));
246 dialog_->firstlineLE->setValidator(new QIntValidator(0, 1000000, this));
247 dialog_->lastlineLE->setValidator(new QIntValidator(0, 1000000, this));
248 dialog_->placementLE->setValidator(new QRegExpValidator(QRegExp("[tbph]*"), this));
250 // set default values
251 dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
252 dialog_->languageCO->setCurrentIndex(
253 dialog_->languageCO->findText(toqstr("no language")));
254 dialog_->floatCB->setChecked(false);
255 dialog_->placementLE->clear();
256 dialog_->numberLeftCB->setChecked(false);
257 dialog_->numberRightCB->setChecked(false);
258 dialog_->numberStepLE->clear();
259 dialog_->numberFontSizeCO->setCurrentIndex(
260 dialog_->numberFontSizeCO->findText(toqstr("default")));
261 dialog_->firstlineLE->clear();
262 dialog_->lastlineLE->clear();
263 dialog_->fontstyleCO->setCurrentIndex(
264 dialog_->fontstyleCO->findText(toqstr("default")));
265 dialog_->fontsizeCO->setCurrentIndex(
266 dialog_->fontsizeCO->findText(toqstr("default")));
267 dialog_->breaklinesCB->setChecked(false);
268 dialog_->spaceCB->setChecked(false);
269 dialog_->extendedcharsCB->setChecked(false);
271 // set values from param string
272 InsetListingsParams & params = controller().params();
273 if (params.isInline())
274 dialog_->inlineCB->setChecked(true);
276 dialog_->inlineCB->setChecked(false);
277 // break other parameters and set values
278 vector<string> pars = getVectorFromString(params.separatedParams(), "\n");
279 // process each of them
280 for (vector<string>::iterator it = pars.begin();
281 it != pars.end(); ++it) {
282 if (prefixIs(*it, "language=")) {
283 for (vector<string>::const_iterator st = languages.begin();
284 st != languages.end(); ++st) {
285 if (*it == "language=" + *st) {
286 dialog_->languageCO->setCurrentIndex(
287 dialog_->languageCO->findText(toqstr(*st)));
291 } else if (prefixIs(*it, "float")) {
292 if (prefixIs(*it, "float="))
293 dialog_->placementLE->setText(toqstr(it->substr(6)));
295 dialog_->floatCB->setChecked(true);
297 } else if (prefixIs(*it, "floatplacement=")) {
298 dialog_->placementLE->setText(toqstr(it->substr(15)));
300 } else if (prefixIs(*it, "numbers=")) {
301 if (contains(*it, "left"))
302 dialog_->numberLeftCB->setChecked(true);
303 else if (contains(*it, "right"))
304 dialog_->numberRightCB->setChecked(true);
306 } else if (prefixIs(*it, "stepnumber=")) {
307 dialog_->numberStepLE->setText(toqstr(it->substr(11)));
309 } else if (prefixIs(*it, "numberstyle=")) {
310 for (vector<string>::const_iterator st = fontsizes.begin();
311 st != fontsizes.end(); ++st) {
312 if (*it == "numberstyle=\\" + *st) {
313 dialog_->numberFontSizeCO->setCurrentIndex(
314 dialog_->numberFontSizeCO->findText(toqstr(*st)));
318 } else if (prefixIs(*it, "firstline=")) {
319 dialog_->firstlineLE->setText(toqstr(it->substr(10)));
321 } else if (prefixIs(*it, "lastline=")) {
322 dialog_->lastlineLE->setText(toqstr(it->substr(9)));
324 } else if (prefixIs(*it, "basicstyle=")) {
327 for (vector<string>::const_iterator st = fontstyles.begin();
328 st != fontstyles.end(); ++st)
329 if (contains(*it, "\\" + *st)) {
333 for (vector<string>::const_iterator st = fontsizes.begin();
334 st != fontsizes.end(); ++st)
335 if (contains(*it, "\\" + *st)) {
339 if (it->substr(11) == style + size || it->substr(11) == size + style) {
341 dialog_->fontstyleCO->setCurrentIndex(
342 dialog_->fontstyleCO->findText(toqstr(style.substr(1))));
344 dialog_->fontsizeCO->setCurrentIndex(
345 dialog_->fontsizeCO->findText(toqstr(size.substr(1))));
348 } else if (prefixIs(*it, "breaklines=")) {
349 dialog_->breaklinesCB->setChecked(contains(*it, "true"));
351 } else if (prefixIs(*it, "showspaces=")) {
352 dialog_->spaceCB->setChecked(contains(*it, "true"));
354 } else if (prefixIs(*it, "extendedchars=")) {
355 dialog_->extendedcharsCB->setChecked(contains(*it, "true"));
359 // parameters that can be handled by widgets are cleared
360 // the rest is put to the extra edit box.
361 string extra = getStringFromVector(pars);
362 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
366 } // namespace frontend
370 #include "QListings_moc.cpp"