Revamp of the character dialog:

- many simplification all over the place.
- improvement to the "Immediate apply" feature.

AFAICS all functionality is restored but please test and report.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24661 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-05-07 18:53:48 +00:00
parent 281487872f
commit 9d23d76c8e
8 changed files with 155 additions and 280 deletions

View File

@ -33,6 +33,7 @@ Language latex_lang;
Language const * default_language; Language const * default_language;
Language const * ignore_language = &ignore_lang; Language const * ignore_language = &ignore_lang;
Language const * latex_language = &latex_lang; Language const * latex_language = &latex_lang;
Language const * reset_language = 0;
bool Language::read(Lexer & lex) bool Language::read(Lexer & lex)
@ -90,8 +91,12 @@ void Languages::read(FileName const & filename)
Language const * Languages::getLanguage(string const & language) const Language const * Languages::getLanguage(string const & language) const
{ {
if (language == "reset")
return reset_language;
if (language == "ignore")
return ignore_language;
const_iterator it = languagelist.find(language); const_iterator it = languagelist.find(language);
return it == languagelist.end() ? 0 : &it->second; return it == languagelist.end() ? reset_language : &it->second;
} }

View File

@ -94,10 +94,18 @@ private:
LanguageList languagelist; LanguageList languagelist;
}; };
/// Global singleton instance.
extern Languages languages; extern Languages languages;
/// Default language defined in LyXRC
extern Language const * default_language; extern Language const * default_language;
/// Used to indicate that the language should be left unchanged when
/// applying a font change.
extern Language const * ignore_language; extern Language const * ignore_language;
/// Default language defined in LyXRC
extern Language const * latex_language; extern Language const * latex_language;
/// Used to indicate that the language should be reset to the Buffer
// language when applying a font change.
extern Language const * reset_language;
} // namespace lyx } // namespace lyx

View File

@ -18,8 +18,11 @@
#include "Font.h" #include "Font.h"
#include "Buffer.h" #include "Buffer.h"
#include "BufferParams.h" #include "BufferParams.h"
#include "BufferView.h"
#include "Cursor.h"
#include "FuncRequest.h" #include "FuncRequest.h"
#include "Language.h" #include "Language.h"
#include "Paragraph.h"
using namespace std; using namespace std;
@ -113,6 +116,17 @@ static QList<FamilyPair> familyData()
return families; return families;
} }
namespace {
template<class T>
void fillCombo(QComboBox * combo, QList<T> list)
{
QList<T>::const_iterator cit = list.begin();
for (; cit != list.end(); ++cit)
combo->addItem(cit->first);
}
}
GuiCharacter::GuiCharacter(GuiView & lv) GuiCharacter::GuiCharacter(GuiView & lv)
: GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language), : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font, ignore_language),
@ -146,37 +160,18 @@ GuiCharacter::GuiCharacter(GuiView & lv)
size = sizeData(); size = sizeData();
bar = barData(); bar = barData();
color = colorData(); color = colorData();
language = languageData(true);
for (QList<FamilyPair>::const_iterator cit = family.begin(); language = languageData();
cit != family.end(); ++cit) { language.prepend(LanguagePair(qt_("Reset"), "reset"));
familyCO->addItem(cit->first); language.prepend(LanguagePair(qt_("No change"), "ignore"));
}
for (QList<SeriesPair>::const_iterator cit = series.begin(); fillCombo(familyCO, family);
cit != series.end(); ++cit) { fillCombo(seriesCO, series);
seriesCO->addItem(cit->first); fillCombo(sizeCO, size);
} fillCombo(shapeCO, shape);
for (QList<ShapePair>::const_iterator cit = shape.begin(); fillCombo(miscCO, bar);
cit != shape.end(); ++cit) { fillCombo(colorCO, color);
shapeCO->addItem(cit->first); fillCombo(langCO, language);
}
for (QList<SizePair>::const_iterator cit = size.begin();
cit != size.end(); ++cit) {
sizeCO->addItem(cit->first);
}
for (QList<BarPair>::const_iterator cit = bar.begin();
cit != bar.end(); ++cit) {
miscCO->addItem(cit->first);
}
for (QList<ColorPair>::const_iterator cit = color.begin();
cit != color.end(); ++cit) {
colorCO->addItem(cit->first);
}
for (QList<LanguagePair>::const_iterator cit = language.begin();
cit != language.end(); ++cit) {
langCO->addItem(cit->first);
}
bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy); bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
bc().setOK(okPB); bc().setOK(okPB);
@ -213,13 +208,6 @@ void GuiCharacter::change_adaptor()
// stay the same between applys. Might be difficult though wrt to a // stay the same between applys. Might be difficult though wrt to a
// moved cursor - jbl // moved cursor - jbl
slotApply(); slotApply();
familyCO->setCurrentIndex(0);
seriesCO->setCurrentIndex(0);
sizeCO->setCurrentIndex(0);
shapeCO->setCurrentIndex(0);
miscCO->setCurrentIndex(0);
langCO->setCurrentIndex(0);
colorCO->setCurrentIndex(0);
} }
@ -235,13 +223,82 @@ static int findPos2nd(QList<P> const & vec, B const & val)
void GuiCharacter::updateContents() void GuiCharacter::updateContents()
{ {
familyCO->setCurrentIndex(findPos2nd(family, getFamily())); if (!autoapplyCB->isChecked())
seriesCO->setCurrentIndex(findPos2nd(series, getSeries())); return;
shapeCO->setCurrentIndex(findPos2nd(shape, getShape())); if (bufferview()->cursor().selection()) {
sizeCO->setCurrentIndex(findPos2nd(size, getSize())); //FIXME: it would be better to check if each font attribute is constant
miscCO->setCurrentIndex(findPos2nd(bar, getBar())); // for the selection range.
colorCO->setCurrentIndex(findPos2nd(color, getColor())); font_ = Font(ignore_font, ignore_language);
langCO->setCurrentIndex(findPos2nd(language, getLanguage())); } else
font_ = bufferview()->cursor().current_font;
paramsToDialog(font_);
}
static FontState getBar(FontInfo const & fi)
{
if (fi.emph() == FONT_TOGGLE)
return EMPH_TOGGLE;
if (fi.underbar() == FONT_TOGGLE)
return UNDERBAR_TOGGLE;
if (fi.noun() == FONT_TOGGLE)
return NOUN_TOGGLE;
if (fi.emph() == FONT_IGNORE
&& fi.underbar() == FONT_IGNORE
&& fi.noun() == FONT_IGNORE)
return IGNORE;
return INHERIT;
}
static void setBar(FontInfo & fi, FontState val)
{
switch (val) {
case IGNORE:
fi.setEmph(FONT_IGNORE);
fi.setUnderbar(FONT_IGNORE);
fi.setNoun(FONT_IGNORE);
break;
case EMPH_TOGGLE:
fi.setEmph(FONT_TOGGLE);
break;
case UNDERBAR_TOGGLE:
fi.setUnderbar(FONT_TOGGLE);
break;
case NOUN_TOGGLE:
fi.setNoun(FONT_TOGGLE);
break;
case INHERIT:
fi.setEmph(FONT_INHERIT);
fi.setUnderbar(FONT_INHERIT);
fi.setNoun(FONT_INHERIT);
break;
}
}
void GuiCharacter::paramsToDialog(Font const & font)
{
FontInfo const & fi = font.fontInfo();
familyCO->setCurrentIndex(findPos2nd(family, fi.family()));
seriesCO->setCurrentIndex(findPos2nd(series, fi.series()));
shapeCO->setCurrentIndex(findPos2nd(shape, fi.shape()));
sizeCO->setCurrentIndex(findPos2nd(size, fi.size()));
miscCO->setCurrentIndex(findPos2nd(bar, getBar(fi)));
colorCO->setCurrentIndex(findPos2nd(color, fi.color()));
QString const lang = (font.language() == ignore_language)
? "ignore" : toqstr(font.language()->lang());
langCO->setCurrentIndex(findPos2nd(language, lang));
toggleallCB->setChecked(toggleall_); toggleallCB->setChecked(toggleall_);
} }
@ -249,13 +306,16 @@ void GuiCharacter::updateContents()
void GuiCharacter::applyView() void GuiCharacter::applyView()
{ {
setFamily(family[familyCO->currentIndex()].second); FontInfo & fi = font_.fontInfo();
setSeries(series[seriesCO->currentIndex()].second); fi.setFamily(family[familyCO->currentIndex()].second);
setShape(shape[shapeCO->currentIndex()].second); fi.setSeries(series[seriesCO->currentIndex()].second);
setSize(size[sizeCO->currentIndex()].second); fi.setShape(shape[shapeCO->currentIndex()].second);
setBar(bar[miscCO->currentIndex()].second); fi.setSize(size[sizeCO->currentIndex()].second);
setColor(color[colorCO->currentIndex()].second); setBar(fi, bar[miscCO->currentIndex()].second);
setLanguage(language[langCO->currentIndex()].second); fi.setColor(color[colorCO->currentIndex()].second);
font_.setLanguage(languages.getLanguage(
fromqstr(language[langCO->currentIndex()].second)));
toggleall_ = toggleallCB->isChecked(); toggleall_ = toggleallCB->isChecked();
} }
@ -263,16 +323,19 @@ void GuiCharacter::applyView()
bool GuiCharacter::initialiseParams(string const &) bool GuiCharacter::initialiseParams(string const &)
{ {
FontInfo & fi = font_.fontInfo();
// so that the user can press Ok // so that the user can press Ok
if (getFamily() != IGNORE_FAMILY if (fi.family() != IGNORE_FAMILY
|| getSeries() != IGNORE_SERIES || fi.series() != IGNORE_SERIES
|| getShape() != IGNORE_SHAPE || fi.shape() != IGNORE_SHAPE
|| getSize() != FONT_SIZE_IGNORE || fi.size() != FONT_SIZE_IGNORE
|| getBar() != IGNORE || getBar(fi) != IGNORE
|| getColor() != Color_ignore || fi.color() != Color_ignore
|| font_.language() != ignore_language) || font_.language() != ignore_language)
setButtonsValid(true); setButtonsValid(true);
paramsToDialog(font_);
return true; return true;
} }
@ -283,156 +346,6 @@ void GuiCharacter::dispatchParams()
} }
FontFamily GuiCharacter::getFamily() const
{
return font_.fontInfo().family();
}
void GuiCharacter::setFamily(FontFamily val)
{
font_.fontInfo().setFamily(val);
}
FontSeries GuiCharacter::getSeries() const
{
return font_.fontInfo().series();
}
void GuiCharacter::setSeries(FontSeries val)
{
font_.fontInfo().setSeries(val);
}
FontShape GuiCharacter::getShape() const
{
return font_.fontInfo().shape();
}
void GuiCharacter::setShape(FontShape val)
{
font_.fontInfo().setShape(val);
}
FontSize GuiCharacter::getSize() const
{
return font_.fontInfo().size();
}
void GuiCharacter::setSize(FontSize val)
{
font_.fontInfo().setSize(val);
}
FontState GuiCharacter::getBar() const
{
if (font_.fontInfo().emph() == FONT_TOGGLE)
return EMPH_TOGGLE;
if (font_.fontInfo().underbar() == FONT_TOGGLE)
return UNDERBAR_TOGGLE;
if (font_.fontInfo().noun() == FONT_TOGGLE)
return NOUN_TOGGLE;
if (font_.fontInfo().emph() == FONT_IGNORE
&& font_.fontInfo().underbar() == FONT_IGNORE
&& font_.fontInfo().noun() == FONT_IGNORE)
return IGNORE;
return INHERIT;
}
void GuiCharacter::setBar(FontState val)
{
switch (val) {
case IGNORE:
font_.fontInfo().setEmph(FONT_IGNORE);
font_.fontInfo().setUnderbar(FONT_IGNORE);
font_.fontInfo().setNoun(FONT_IGNORE);
break;
case EMPH_TOGGLE:
font_.fontInfo().setEmph(FONT_TOGGLE);
break;
case UNDERBAR_TOGGLE:
font_.fontInfo().setUnderbar(FONT_TOGGLE);
break;
case NOUN_TOGGLE:
font_.fontInfo().setNoun(FONT_TOGGLE);
break;
case INHERIT:
font_.fontInfo().setEmph(FONT_INHERIT);
font_.fontInfo().setUnderbar(FONT_INHERIT);
font_.fontInfo().setNoun(FONT_INHERIT);
break;
}
}
ColorCode GuiCharacter::getColor() const
{
return font_.fontInfo().color();
}
void GuiCharacter::setColor(ColorCode val)
{
switch (val) {
case Color_ignore:
case Color_none:
case Color_black:
case Color_white:
case Color_red:
case Color_green:
case Color_blue:
case Color_cyan:
case Color_magenta:
case Color_yellow:
case Color_inherit:
font_.fontInfo().setColor(val);
break;
default:
break;
}
}
QString GuiCharacter::getLanguage() const
{
if (reset_lang_)
return "reset";
if (font_.language())
return toqstr(font_.language()->lang());
return "ignore";
}
void GuiCharacter::setLanguage(QString const & val)
{
if (val == "ignore")
font_.setLanguage(ignore_language);
else if (val == "reset") {
reset_lang_ = true;
// Ignored in getLanguage, but needed for dispatchParams
font_.setLanguage(buffer().params().language);
} else {
font_.setLanguage(languages.getLanguage(fromqstr(val)));
}
}
Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); } Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }

View File

@ -57,10 +57,19 @@ protected Q_SLOTS:
void change_adaptor(); void change_adaptor();
private: private:
/// Apply changes /// Dialog inherited methods
//@{
void applyView(); void applyView();
/// update
void updateContents(); void updateContents();
bool initialiseParams(std::string const & data);
void clearParams() {}
void dispatchParams();
bool isBufferDependent() const { return true; }
FuncCode getLfun() const { return LFUN_FONT_FREE_UPDATE; }
//@}
///
void paramsToDialog(Font const & font);
QList<FamilyPair> family; QList<FamilyPair> family;
QList<SeriesPair> series; QList<SeriesPair> series;
@ -71,47 +80,6 @@ private:
QList<LanguagePair> language; QList<LanguagePair> language;
/// ///
bool initialiseParams(std::string const & data);
///
void clearParams() {}
///
void dispatchParams();
///
bool isBufferDependent() const { return true; }
///
FuncCode getLfun() const { return LFUN_FONT_FREE_UPDATE; }
///
void setFamily(FontFamily);
///
void setSeries(FontSeries);
///
void setShape(FontShape);
///
void setSize(FontSize);
///
void setBar(FontState);
///
void setColor(ColorCode);
///
void setLanguage(QString const &);
///
FontFamily getFamily() const;
///
FontSeries getSeries() const;
///
FontShape getShape() const;
///
FontSize getSize() const;
///
FontState getBar() const;
///
ColorCode getColor() const;
///
QString getLanguage() const;
private:
/// ///
Font font_; Font font_;
/// ///

View File

@ -510,7 +510,7 @@ GuiDocument::GuiDocument(GuiView & lv)
{ {
setupUi(this); setupUi(this);
QList<LanguagePair> langs = languageData(false); QList<LanguagePair> langs = languageData();
for (int i = 0; i != langs.size(); ++i) for (int i = 0; i != langs.size(); ++i)
lang_.append(langs[i].second); lang_.append(langs[i].second);

View File

@ -1660,7 +1660,7 @@ PrefLanguage::PrefLanguage(QWidget * parent)
// store the lang identifiers for later // store the lang identifiers for later
//foreach (LanguagePair const & lpair, languageData(false)) { //foreach (LanguagePair const & lpair, languageData(false)) {
QList<LanguagePair> l = languageData(false); QList<LanguagePair> l = languageData();
foreach (LanguagePair const & lpair, l) { foreach (LanguagePair const & lpair, l) {
defaultLanguageCO->addItem(lpair.first); defaultLanguageCO->addItem(lpair.first);
lang_.append(lpair.second); lang_.append(lpair.second);

View File

@ -198,31 +198,14 @@ private:
} // namespace anon } // namespace anon
QList<LanguagePair> languageData(bool character_dlg) QList<LanguagePair> languageData()
{ {
size_t const offset = character_dlg ? 2 : 0;
vector<LanguagePair> langs(languages.size() + offset);
if (character_dlg) {
langs[0].first = qt_("No change");
langs[0].second = "ignore";
langs[1].first = qt_("Reset");
langs[1].second = "reset";
}
Languages::const_iterator it = languages.begin();
for (size_t i = 0; i != languages.size(); ++i, ++it) {
langs[i + offset].first = qt_(it->second.display());
langs[i + offset].second = toqstr(it->second.lang());
}
// Don't sort "ignore" and "reset"
vector<LanguagePair>::iterator begin = langs.begin() + offset;
sort(begin, langs.end(), Sorter());
QList<LanguagePair> list; QList<LanguagePair> list;
foreach (LanguagePair const & l, langs) Languages::const_iterator it = languages.begin();
list.append(l); for (; it != languages.end(); ++it) {
list << LanguagePair(
qt_(it->second.display()), toqstr(it->second.lang()));
}
return list; return list;
} }

View File

@ -68,12 +68,10 @@ void setValid(QWidget * widget, bool valid);
QString const qt_(std::string const & str); QString const qt_(std::string const & str);
/// ///
struct LanguagePair { QString first; QString second; }; typedef std::pair<QString, QString> LanguagePair;
/** If the caller is the character dialog, add "No change" and "Reset" ///
* to the vector. QList<LanguagePair> languageData();
*/
QList<LanguagePair> languageData(bool character_dlg);
support::FileName libFileSearch(QString const & dir, QString const & name, support::FileName libFileSearch(QString const & dir, QString const & name,
QString const & ext = QString()); QString const & ext = QString());