Separate caption and label from InsetListingsParams and handle them separately in listings and Include dialog, from Jurgen

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18324 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Bo Peng 2007-05-14 20:42:14 +00:00
parent 9b5b7587a3
commit 72e0b87a90
12 changed files with 244 additions and 180 deletions

View File

@ -76,3 +76,7 @@ End
Counter
Name equation
End
Counter
Name listing
End

View File

@ -1871,6 +1871,8 @@ docstring Text::getPossibleLabel(Cursor & cur) const
name = from_ascii("thm");
else if (name == "Foot")
name = from_ascii("fn");
else if (name == "listing")
name = from_ascii("lst");
if (!name.empty())
text = name.substr(0, 3) + ':' + text;

View File

@ -417,6 +417,8 @@ void setCaptions(Paragraph & par, TextClass const & textclass)
// FIXME: are "table" and "Table" the correct type and label?
setCaptionLabels(inset, "table", from_ascii("Table"), counters);
}
else if (inset.lyxCode() == Inset::LISTINGS_CODE)
setCaptionLabels(inset, "listing", from_ascii("Listing"), counters);
}
}

View File

@ -11,6 +11,7 @@
#include <config.h>
#include "support/os.h"
#include "support/lstrings.h"
#include "QInclude.h"
@ -30,9 +31,12 @@
#include <QLineEdit>
using std::string;
using std::vector;
using lyx::support::os::internal_path;
using lyx::support::prefixIs;
using lyx::support::getStringFromVector;
using lyx::support::getVectorFromString;
namespace lyx {
namespace frontend {
@ -58,6 +62,8 @@ QIncludeDialog::QIncludeDialog(QInclude * form)
connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params()));
@ -85,13 +91,11 @@ void QIncludeDialog::validate_listings_params()
InsetListingsParams par(fromqstr(listingsED->toPlainText()));
if (!isOK) {
isOK = true;
// listingsTB->setTextColor("black");
listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters.");
listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
okPB->setEnabled(true);
}
} catch (invalidParam & e) {
isOK = false;
// listingsTB->setTextColor("red");
listingsTB->setPlainText(e.what());
okPB->setEnabled(false);
}
@ -115,7 +119,6 @@ void QIncludeDialog::typeChanged(int v)
previewCB->setEnabled(false);
previewCB->setChecked(false);
listingsGB->setEnabled(false);
listingsED->setEnabled(false);
break;
//case Input
case 1:
@ -123,7 +126,6 @@ void QIncludeDialog::typeChanged(int v)
visiblespaceCB->setChecked(false);
previewCB->setEnabled(true);
listingsGB->setEnabled(false);
listingsED->setEnabled(false);
break;
//case listings
case 3:
@ -131,7 +133,6 @@ void QIncludeDialog::typeChanged(int v)
visiblespaceCB->setChecked(false);
previewCB->setEnabled(false);
listingsGB->setEnabled(true);
listingsED->setEnabled(true);
break;
//case Verbatim
default:
@ -139,7 +140,6 @@ void QIncludeDialog::typeChanged(int v)
previewCB->setEnabled(false);
previewCB->setChecked(false);
listingsGB->setEnabled(false);
listingsED->setEnabled(false);
break;
}
}
@ -183,7 +183,7 @@ void QInclude::build_dialog()
bcview().addReadOnly(dialog_->visiblespaceCB);
bcview().addReadOnly(dialog_->typeCO);
bcview().addReadOnly(dialog_->listingsED);
dialog_->listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters.");
dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
}
@ -235,7 +235,29 @@ void QInclude::update_contents()
dialog_->listingsGB->setEnabled(true);
dialog_->listingsED->setEnabled(true);
InsetListingsParams par(params.getOptions());
dialog_->listingsED->setPlainText(toqstr(par.separatedParams()));
// extract caption and label and put them into their respective editboxes
vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
for (vector<string>::iterator it = pars.begin();
it != pars.end(); ++it) {
if (prefixIs(*it, "caption=")) {
string cap = it->substr(8);
if (cap[0] == '{' && cap[cap.size()-1] == '}')
dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
else
throw invalidParam("caption parameter is not quoted with braces");
*it = "";
} else if (prefixIs(*it, "label=")) {
string lbl = it->substr(6);
if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
else
throw invalidParam("label parameter is not quoted with braces");
*it = "";
}
}
// the rest is put to the extra edit box.
string extra = getStringFromVector(pars);
dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
}
}
@ -257,7 +279,14 @@ void QInclude::apply()
} else if (item == 3) {
params.setCmdName("lstinputlisting");
// the parameter string should have passed validation
params.setOptions(InsetListingsParams(fromqstr(dialog_->listingsED->toPlainText())).params());
InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
string caption = fromqstr(dialog_->captionLE->text());
string label = fromqstr(dialog_->labelLE->text());
if (!caption.empty())
par.addParam("caption", "{" + caption + "}");
if (!label.empty())
par.addParam("label", "{" + label + "}");
params.setOptions(par.params());
} else {
if (dialog_->visiblespaceCB->isChecked())
params.setCmdName("verbatiminput*");

View File

@ -4,6 +4,7 @@
* Licence details can be found in the file COPYING.
*
* \author Bo Peng
* \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
*/
@ -78,8 +79,6 @@ QListingsDialog::QListingsDialog(QListings * form)
connect(breaklinesCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
connect(spaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
connect(extendedcharsCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params()));
@ -108,7 +107,7 @@ string QListingsDialog::construct_params()
bool left = numberLeftCB->checkState() == Qt::Checked;
bool right = numberRightCB->checkState() == Qt::Checked;
string step = fromqstr(numberStepLE->text());
string stepnumber = fromqstr(numberStepLE->text());
string numberfontsize = fromqstr(numberFontSizeCO->currentText());
string firstline = fromqstr(firstlineLE->text());
string lastline = fromqstr(lastlineLE->text());
@ -122,11 +121,7 @@ string QListingsDialog::construct_params()
basicstyle += "\\" + fontstyle;
bool breakline = breaklinesCB->checkState() == Qt::Checked;
bool space = spaceCB->checkState() == Qt::Checked;
bool extendedchar = extendedcharsCB->checkState() == Qt::Checked;
string caption = fromqstr(captionLE->text());
string label = fromqstr(labelLE->text());
bool extendedchars = extendedcharsCB->checkState() == Qt::Checked;
string extra = fromqstr(listingsED->toPlainText());
// compose a string
@ -147,18 +142,16 @@ string QListingsDialog::construct_params()
par.addParam("firstline", firstline);
if (!lastline.empty())
par.addParam("lastline", lastline);
if (basicstyle != "")
if (!stepnumber.empty())
par.addParam("stepnumber", stepnumber);
if (!basicstyle.empty())
par.addParam("basicstyle", basicstyle);
if (breakline)
par.addParam("breaklines", "true");
if (space)
par.addParam("showspaces", "true");
if (extendedchar)
if (extendedchars)
par.addParam("extendedchars", "true");
if (!caption.empty())
par.addParam("caption", "{" + caption + "}");
if (!label.empty())
par.addParam("label", "{" + label + "}");
par.addParams(extra);
return par.params();
}
@ -171,13 +164,11 @@ void QListingsDialog::validate_listings_params()
InsetListingsParams par(construct_params());
if (!isOK) {
isOK = true;
// listingsTB->setTextColor("black");
listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters.");
listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
okPB->setEnabled(true);
}
} catch (invalidParam & e) {
isOK = false;
// listingsTB->setTextColor("red");
listingsTB->setPlainText(e.what());
okPB->setEnabled(false);
}
@ -203,7 +194,7 @@ void QListings::build_dialog()
bcview().setOK(dialog_->okPB);
bcview().setCancel(dialog_->closePB);
dialog_->listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters.");
dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
update_contents();
}
@ -257,7 +248,7 @@ void QListings::update_contents()
dialog_->placementLE->setValidator(new QRegExpValidator(QRegExp("[tbph]*"), this));
//
dialog_->listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters.");
dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
// set values from param string
InsetListingsParams & params = controller().params();
@ -345,22 +336,6 @@ void QListings::update_contents()
} else if (prefixIs(*it, "extendedchars=")) {
dialog_->extendedcharsCB->setChecked(contains(*it, "true"));
*it = "";
} else if (prefixIs(*it, "caption=")) {
string cap = it->substr(8);
if ((cap[0] == '{' && cap[cap.size()-1] == '}') ||
(cap[0] == '"' && cap[cap.size()-1] == '"') )
dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
else
dialog_->captionLE->setText(toqstr(cap));
*it = "";
} else if (prefixIs(*it, "label=")) {
string lbl = it->substr(6);
if ((lbl[0] == '{' && lbl[lbl.size()-1] == '}') ||
(lbl[0] == '"' && lbl[lbl.size()-1] == '"') )
dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
else
dialog_->labelLE->setText(toqstr(lbl));
*it = "";
}
}
// parameters that can be handled by widgets are cleared

View File

@ -5,8 +5,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>389</width>
<height>385</height>
<width>315</width>
<height>379</height>
</rect>
</property>
<property name="windowTitle" >
@ -237,6 +237,69 @@
<number>6</number>
</property>
<item>
<layout class="QGridLayout" >
<property name="margin" >
<number>0</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="1" column="0" >
<widget class="QLabel" name="labelLabel" >
<property name="text" >
<string>Label</string>
</property>
</widget>
</item>
<item row="0" column="1" >
<widget class="QLineEdit" name="captionLE" >
<property name="minimumSize" >
<size>
<width>150</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QLabel" name="captionLabel" >
<property name="text" >
<string>Caption</string>
</property>
</widget>
</item>
<item row="1" column="1" >
<widget class="QLineEdit" name="labelLE" />
</item>
</layout>
</item>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_3" >
<property name="text" >
<string>More parameters</string>
</property>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<widget class="QTextBrowser" name="listingsTB" >
<property name="minimumSize" >
<size>
@ -269,8 +332,6 @@
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QTextEdit" name="listingsED" >
<property name="minimumSize" >
<size>
@ -279,8 +340,7 @@
</size>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
@ -339,6 +399,8 @@
<tabstop>loadPB</tabstop>
<tabstop>visiblespaceCB</tabstop>
<tabstop>previewCB</tabstop>
<tabstop>captionLE</tabstop>
<tabstop>labelLE</tabstop>
<tabstop>listingsTB</tabstop>
<tabstop>listingsED</tabstop>
</tabstops>

View File

@ -8,8 +8,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>499</width>
<height>355</height>
<width>511</width>
<height>325</height>
</rect>
</property>
<property name="windowTitle" >
@ -213,9 +213,6 @@
<property name="wordWrap" >
<bool>false</bool>
</property>
<property name="buddy" >
<cstring>captionLE</cstring>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2" >
@ -404,79 +401,6 @@
</layout>
</widget>
</item>
<item row="2" column="0" colspan="3" >
<widget class="QGroupBox" name="captionGB_3" >
<property name="title" >
<string>Display</string>
</property>
<layout class="QGridLayout" >
<property name="margin" >
<number>9</number>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item row="1" column="0" >
<widget class="QLabel" name="labelL_4" >
<property name="text" >
<string>&amp;Label:</string>
</property>
<property name="wordWrap" >
<bool>false</bool>
</property>
<property name="buddy" >
<cstring>labelLE</cstring>
</property>
</widget>
</item>
<item row="0" column="0" >
<widget class="QLabel" name="captionL_4" >
<property name="text" >
<string>&amp;Caption:</string>
</property>
<property name="wordWrap" >
<bool>false</bool>
</property>
<property name="buddy" >
<cstring>captionLE</cstring>
</property>
</widget>
</item>
<item row="0" column="1" >
<widget class="QLineEdit" name="captionLE" >
<property name="minimumSize" >
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="toolTip" >
<string>A caption for the List of Listings</string>
</property>
<property name="text" >
<string/>
</property>
</widget>
</item>
<item row="1" column="1" >
<widget class="QLineEdit" name="labelLE" >
<property name="minimumSize" >
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="toolTip" >
<string>A Label for the caption</string>
</property>
<property name="text" >
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2" >
@ -599,10 +523,11 @@
<tabstops>
<tabstop>okPB</tabstop>
<tabstop>closePB</tabstop>
<tabstop>languageCO</tabstop>
<tabstop>listingsTW</tabstop>
<tabstop>inlineCB</tabstop>
<tabstop>floatCB</tabstop>
<tabstop>placementLE</tabstop>
<tabstop>languageCO</tabstop>
<tabstop>numberLeftCB</tabstop>
<tabstop>numberRightCB</tabstop>
<tabstop>numberStepLE</tabstop>
@ -614,8 +539,6 @@
<tabstop>breaklinesCB</tabstop>
<tabstop>spaceCB</tabstop>
<tabstop>extendedcharsCB</tabstop>
<tabstop>captionLE</tabstop>
<tabstop>labelLE</tabstop>
<tabstop>listingsTB</tabstop>
<tabstop>listingsED</tabstop>
</tabstops>

View File

@ -278,6 +278,20 @@ int InsetCaption::docbook(Buffer const & buf, odocstream & os,
}
int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
OutputParams const & runparams) const
{
return InsetText::latex(buf, os, runparams);
}
int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
OutputParams const & runparams) const
{
return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
}
void InsetCaption::computeFullLabel(Buffer const & buf) const
{
if (type_.empty())

View File

@ -69,6 +69,12 @@ public:
///
int docbook(Buffer const & buf, odocstream & os,
OutputParams const & runparams) const;
/// return the mandatory argument (LaTeX format) only
int getArgument(Buffer const & buf, odocstream & os,
OutputParams const &) const;
/// return the optional argument(s) only
int getOptArg(Buffer const & buf, odocstream & os,
OutputParams const &) const;
///
void setCount(int c) { counter_ = c; }
///

View File

@ -4,6 +4,7 @@
* Licence details can be found in the file COPYING.
*
* \author Bo Peng
* \author Jürgen Spitzmüller
*
* Full author contact details are available in file CREDITS.
*/
@ -11,6 +12,7 @@
#include <config.h>
#include "InsetListings.h"
#include "InsetCaption.h"
#include "Language.h"
#include "gettext.h"
@ -127,8 +129,8 @@ docstring const InsetListings::editMessage() const
}
int InsetListings::latex(Buffer const &, odocstream & os,
OutputParams const &) const
int InsetListings::latex(Buffer const & buf, odocstream & os,
OutputParams const & runparams) const
{
string param_string = params().encodedString();
// NOTE: I use {} to quote text, which is an experimental feature
@ -141,10 +143,18 @@ int InsetListings::latex(Buffer const &, odocstream & os,
else
os << "\\lstinline[" << from_ascii(param_string) << "]{";
} else {
if (param_string.empty())
docstring const caption = getCaption(buf, runparams);
if (param_string.empty() && caption.empty())
os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
else
os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[" << from_ascii(param_string) << "]\n";
else {
os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
if (!caption.empty()) {
os << "caption={" << caption << '}';
if (!param_string.empty())
os << ',';
}
os << from_ascii(param_string) << "]\n";
}
lines += 4;
}
ParagraphList::const_iterator par = paragraphs().begin();
@ -152,16 +162,19 @@ int InsetListings::latex(Buffer const &, odocstream & os,
while (par != end) {
pos_type siz = par->size();
bool captionline = false;
for (pos_type i = 0; i < siz; ++i) {
// ignore all struck out text
if (par->isDeleted(i))
if (i == 0 && par->isInset(i) && i + 1 == siz)
captionline = true;
// ignore all struck out text and (caption) insets
if (par->isDeleted(i) || par->isInset(i))
continue;
os.put(par->getChar(i));
}
++par;
// for the inline case, if there are multiple paragraphs
// they are simply joined. Otherwise, expect latex errors.
if (par != end && !lstinline) {
if (par != end && !lstinline && !captionline) {
os << "\n";
++lines;
}
@ -210,6 +223,9 @@ bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
case LFUN_INSET_DIALOG_UPDATE:
status.enabled(true);
return true;
case LFUN_CAPTION_INSERT:
status.enabled(!params().isInline());
return true;
default:
return InsetERT::getStatus(cur, cmd, status);
}
@ -245,6 +261,31 @@ void InsetListings::getDrawFont(Font & font) const
}
docstring InsetListings::getCaption(Buffer const & buf,
OutputParams const & runparams) const
{
if (paragraphs().empty())
return docstring();
ParagraphList::const_iterator pit = paragraphs().begin();
for (; pit != paragraphs().end(); ++pit) {
InsetList::const_iterator it = pit->insetlist.begin();
for (; it != pit->insetlist.end(); ++it) {
Inset & inset = *it->inset;
if (inset.lyxCode() == Inset::CAPTION_CODE) {
odocstringstream ods;
InsetCaption * ins =
static_cast<InsetCaption *>(it->inset);
ins->getOptArg(buf, ods, runparams);
ins->getArgument(buf, ods, runparams);
return ods.str();
}
}
}
return docstring();
}
string const InsetListingsMailer::name_("listings");
InsetListingsMailer::InsetListingsMailer(InsetListings & inset)

View File

@ -67,6 +67,8 @@ private:
///
void setButtonLabel();
///
docstring getCaption(Buffer const &, OutputParams const &) const;
///
InsetListingsParams params_;
};

View File

@ -33,12 +33,12 @@ namespace lyx
{
enum param_type {
ALL,
TRUEFALSE,
INTEGER,
LENGTH,
ONEOF,
SUBSETOF,
ALL, // accept all
TRUEFALSE, // accept 'true' or 'false'
INTEGER, // accept an integer
LENGTH, // accept an latex length
ONEOF, // accept one of a few values
SUBSETOF, // accept a string composed of given characters
};
@ -148,8 +148,12 @@ listings_param_info const listings_param_table[] = {
{ "name", "", false, ALL, "", "" },
{ "thelstnumber", "", false, ALL, "", "" },
{ "title", "", false, ALL, "", "" },
{ "caption", "", false, ALL, "", "" },
{ "label", "", false, ALL, "", "" },
// this option is not handled in the parameter box
{ "caption", "", false, ALL, "", "This parameter should not be entered here. "
"Please use caption editbox (Include dialog) or insert->caption (listings inset)" },
// this option is not handled in the parameter box
{ "label", "", false, ALL, "", "This parameter should not be entered here."
"Please use label editbox (Include dialog) or insert->caption (listings inset)"},
{ "nolol", "", false, TRUEFALSE, "", "" },
{ "captionpos", "", false, SUBSETOF, "tb", "" },
{ "abovecaptionskip", "", false, LENGTH, "", "" },