From 4d30bc757931ddb6f3a35809f48c4f6528004060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20St=C3=B6hr?= Date: Sat, 11 Apr 2009 21:40:11 +0000 Subject: [PATCH] support for \pagecolor, fileformat change git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29220 a592a061-630c-0410-9148-cb99ea01b6c8 --- development/FORMAT | 3 + lib/lyx2lyx/lyx_2_0.py | 43 ++- src/Buffer.cpp | 2 +- src/BufferParams.cpp | 16 +- src/BufferParams.h | 2 + src/Color.cpp | 23 ++ src/Color.h | 1 + src/LaTeXFeatures.cpp | 10 + src/frontends/qt4/GuiDocument.cpp | 53 ++- src/frontends/qt4/GuiDocument.h | 2 + src/frontends/qt4/ui/PageLayoutUi.ui | 470 +++++++++++++++------------ 11 files changed, 408 insertions(+), 217 deletions(-) diff --git a/development/FORMAT b/development/FORMAT index 218517e637..a038fab6be 100644 --- a/development/FORMAT +++ b/development/FORMAT @@ -1,6 +1,9 @@ LyX file-format changes ----------------------- +2009-04-11 Uwe Stöhr + * Format incremented to 351: support to set a page background color. + 2009-04-06 Jürgen Spitzmüller * Format incremented to 350: new param \default_output_format. diff --git a/lib/lyx2lyx/lyx_2_0.py b/lib/lyx2lyx/lyx_2_0.py index 2ee691feba..646a7c519a 100644 --- a/lib/lyx2lyx/lyx_2_0.py +++ b/lib/lyx2lyx/lyx_2_0.py @@ -18,7 +18,7 @@ """ Convert files to the file format generated by lyx 2.0""" -import re +import re, string from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string @@ -296,6 +296,41 @@ def revert_outputformat(document): del document.header[i] +def revert_backgroundcolor(document): + " Reverts backgrund color to preamble code " + i = 0 + colorcode = "" + while True: + i = find_token(document.header, "\\backgroundcolor", i) + if i == -1: + return + colorcode = get_value(document.header, '\\backgroundcolor', 0) + del document.header[i] + # the color code is in the form #rrggbb where every character denotes a hex number + # convert the string to an int + red = string.atoi(colorcode[1:3],16) + # we want the output "0.5" for the value "127" therefore add here + if red != 0: + red = red + 1 + redout = float(red) / 256 + green = string.atoi(colorcode[3:5],16) + if green != 0: + green = green + 1 + greenout = float(green) / 256 + blue = string.atoi(colorcode[5:7],16) + if blue != 0: + blue = blue + 1 + blueout = float(blue) / 256 + # write the preamble + insert_to_preamble(0, document, + '% Commands inserted by lyx2lyx to set the background color\n' + + '\\@ifundefined{definecolor}{\\usepackage{color}}{}\n' + + '\\definecolor{page_backgroundcolor}{rgb}{' + + str(redout) + ', ' + str(greenout) + + ', ' + str(blueout) + '}\n' + + '\\pagecolor{page_backgroundcolor}\n') + + ## # Conversion hub # @@ -305,10 +340,12 @@ convert = [[346, []], [347, []], [348, []], [349, []], - [350, []] + [350, []], + [351, []] ] -revert = [[349, [revert_outputformat]], +revert = [[350, [revert_backgroundcolor]], + [349, [revert_outputformat]], [348, [revert_xetex]], [347, [revert_phantom, revert_hphantom, revert_vphantom]], [346, [revert_tabularvalign]], diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 4de45635e1..eb03f06bf6 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -124,7 +124,7 @@ namespace { // Do not remove the comment below, so we get merge conflict in // independent branches. Instead add your own. -int const LYX_FORMAT = 350; // jspitzm: default output format +int const LYX_FORMAT = 351; // uwestoehr: support for page background color typedef map DepClean; typedef map > RefCache; diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 7e8c16512a..9492f00f15 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -356,6 +356,8 @@ BufferParams::BufferParams() columns = 1; listings_params = string(); pagestyle = "default"; + // white is equal to no background color + backgroundcolor = lyx::rgbFromHexName("#ffffff"); compressed = false; for (int iter = 0; iter < 4; ++iter) { user_defined_bullet(iter) = ITEMIZE_DEFAULTS[iter]; @@ -600,7 +602,6 @@ string BufferParams::readToken(Lexer & lex, string const & token, color = lcolor.getX11Name(Color_background); // FIXME UNICODE lcolor.setColor(to_utf8(branch), color); - } } } else if (token == "\\author") { @@ -613,6 +614,9 @@ string BufferParams::readToken(Lexer & lex, string const & token, string orient; lex >> orient; orientation = paperorientationtranslator().find(orient); + } else if (token == "\\backgroundcolor") { + lex.eatLine(); + backgroundcolor = lyx::rgbFromHexName(lex.getString()); } else if (token == "\\paperwidth") { lex >> paperwidth; } else if (token == "\\paperheight") { @@ -778,6 +782,7 @@ void BufferParams::writeFile(ostream & os) const << "\n\\cite_engine " << citeenginetranslator().find(cite_engine_) << "\n\\use_bibtopic " << convert(use_bibtopic) << "\n\\paperorientation " << string_orientation[orientation] + << "\n\\backgroundcolor " << lyx::X11hexname(backgroundcolor) << '\n'; BranchList::const_iterator it = branchlist().begin(); @@ -1264,6 +1269,15 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features, texrow.newline(); } + // only output when the background color is not white + if (backgroundcolor != lyx::rgbFromHexName("#ffffff")) { + // only require color here, the background color will be defined + // in LaTeXFeatures.cpp to avoid interferences with the LaTeX + // package pdfpages + features.require("color"); + features.require("pagecolor"); + } + // Only if class has a ToC hierarchy if (tclass.hasTocLevels()) { if (secnumdepth != tclass.secnumdepth()) { diff --git a/src/BufferParams.h b/src/BufferParams.h index 7c8c815041..40fee96f60 100644 --- a/src/BufferParams.h +++ b/src/BufferParams.h @@ -254,6 +254,8 @@ public: PageSides sides; /// std::string pagestyle; + /// + RGBColor backgroundcolor; /// \param index should lie in the range 0 <= \c index <= 3. Bullet & temp_bullet(size_type index); Bullet const & temp_bullet(size_type index) const; diff --git a/src/Color.cpp b/src/Color.cpp index 79953e3339..45181294dc 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -19,6 +19,7 @@ #include "Color.h" #include "ColorSet.h" +#include "support/convert.h" #include "support/debug.h" #include "support/gettext.h" #include "support/lstrings.h" @@ -84,6 +85,28 @@ RGBColor rgbFromHexName(string const & x11hexname) return c; } +string const outputLaTeXColor(RGBColor const & color) +{ + // this routine returns a LaTeX readable color string in the form + // "red, green, blue" where the colors are a number in the range 0-1 + int red = color.r; + int green = color.g; + int blue = color.b; + // the color values are given in the range of 0-255, so to get + // an output of "0.5" for the value 127 we need to do the following + if (red != 0) + ++red; + if (green != 0) + ++green; + if (blue != 0) + ++blue; + string output; + output = convert(float(red) / 256) + ", " + + convert(float(green) / 256) + ", " + + convert(float(blue) / 256); + return output; +} + Color::Color(ColorCode base_color) : baseColor(base_color), mergeColor(Color_ignore) diff --git a/src/Color.h b/src/Color.h index ce65b14438..fbfe10fb22 100644 --- a/src/Color.h +++ b/src/Color.h @@ -61,6 +61,7 @@ std::ostream & operator<<(std::ostream & os, Color color); std::string const X11hexname(RGBColor const & col); RGBColor rgbFromHexName(std::string const & x11hexname); +std::string const outputLaTeXColor(RGBColor const & color); } // namespace lyx diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp index af1ed2eb26..79ef3e4520 100644 --- a/src/LaTeXFeatures.cpp +++ b/src/LaTeXFeatures.cpp @@ -545,6 +545,16 @@ string const LaTeXFeatures::getColorOptions() const if (mustProvide("pdfcolmk")) colors << "\\usepackage{pdfcolmk}\n"; + if (mustProvide("pagecolor")) { + // the \pagecolor command must be set after color is loaded and + // before pdfpages, therefore add the command here + // define the set color + colors << "\\definecolor{page_backgroundcolor}{rgb}{"; + colors << outputLaTeXColor(params_.backgroundcolor) << "}\n"; + // set the page color + colors << "\\pagecolor{page_backgroundcolor}\n"; + } + return colors.str(); } diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index 8bf88f61f2..2d6590e7e3 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -28,6 +28,7 @@ #include "BufferParams.h" #include "BufferView.h" #include "Color.h" +#include "ColorCache.h" #include "Encoding.h" #include "FloatPlacement.h" #include "Format.h" @@ -54,6 +55,8 @@ #include "frontends/alert.h" #include +#include +#include #include #include #include @@ -67,6 +70,19 @@ #endif +// a style sheet for buttons +// this is for example used for the background color setting button +static inline QString colorButtonStyleSheet(const QColor &bgColor) +{ + if (bgColor.isValid()) { + QString rc = QLatin1String("background:"); + rc += bgColor.name(); + return rc; + } + return QLatin1String(""); +} + + using namespace std; using namespace lyx::support; @@ -157,6 +173,8 @@ vector > pagestyles; namespace lyx { +RGBColor set_backgroundcolor; + namespace { // used when sorting the textclass list. class less_textclass_avail_desc @@ -661,6 +679,10 @@ GuiDocument::GuiDocument(GuiView & lv) this, SLOT(change_adaptor())); connect(pageLayoutModule->pagestyleCO, SIGNAL(activated(int)), this, SLOT(change_adaptor())); + connect(pageLayoutModule->backgroundTB, SIGNAL(clicked()), + this, SLOT(changeBackgroundColor())); + connect(pageLayoutModule->delbackgroundTB, SIGNAL(clicked()), + this, SLOT(deleteBackgroundColor())); pageLayoutModule->pagestyleCO->addItem(qt_("Default")); pageLayoutModule->pagestyleCO->addItem(qt_("empty")); @@ -1165,6 +1187,31 @@ void GuiDocument::setCustomMargins(bool custom) marginsModule->columnsepUnit->setEnabled(enableColSep); } +void GuiDocument::changeBackgroundColor() +{ + const QColor newColor = QColorDialog::getColor( + rgb2qcolor(set_backgroundcolor), qApp->focusWidget()); + if (!newColor.isValid()) + return; + // set the button color + pageLayoutModule->backgroundTB->setStyleSheet( + colorButtonStyleSheet(newColor)); + // save white as the set color + set_backgroundcolor = rgbFromHexName(fromqstr(newColor.name())); + changed(); +} + + +void GuiDocument::deleteBackgroundColor() +{ + // set the button color back to white + pageLayoutModule->backgroundTB->setStyleSheet( + colorButtonStyleSheet(QColor(Qt::white))); + // save white as the set color + set_backgroundcolor = rgbFromHexName("#ffffff"); + changed(); +} + void GuiDocument::xetexChanged(bool xetex) { @@ -1928,6 +1975,8 @@ void GuiDocument::applyView() else bp_.orientation = ORIENTATION_PORTRAIT; + bp_.backgroundcolor = set_backgroundcolor; + // margins bp_.use_geometry = !marginsModule->marginCB->isChecked() || geom_papersize; @@ -2267,10 +2316,12 @@ void GuiDocument::paramsToDialog() pageLayoutModule->facingPagesCB->setChecked( bp_.sides == TwoSides); + pageLayoutModule->backgroundTB->setStyleSheet( + colorButtonStyleSheet(QColor(rgb2qcolor(bp_.backgroundcolor)))); + set_backgroundcolor = bp_.backgroundcolor; lengthToWidgets(pageLayoutModule->paperwidthLE, pageLayoutModule->paperwidthUnitCO, bp_.paperwidth, defaultUnit); - lengthToWidgets(pageLayoutModule->paperheightLE, pageLayoutModule->paperheightUnitCO, bp_.paperheight, defaultUnit); diff --git a/src/frontends/qt4/GuiDocument.h b/src/frontends/qt4/GuiDocument.h index 7dd64cc0d9..43cb9a265b 100644 --- a/src/frontends/qt4/GuiDocument.h +++ b/src/frontends/qt4/GuiDocument.h @@ -100,6 +100,8 @@ private Q_SLOTS: void classChanged(); void updateModuleInfo(); void modulesChanged(); + void changeBackgroundColor(); + void deleteBackgroundColor(); void xetexChanged(bool); private: /// validate listings parameters and return an error message, if any diff --git a/src/frontends/qt4/ui/PageLayoutUi.ui b/src/frontends/qt4/ui/PageLayoutUi.ui index 10d8619940..0c7178cd06 100644 --- a/src/frontends/qt4/ui/PageLayoutUi.ui +++ b/src/frontends/qt4/ui/PageLayoutUi.ui @@ -1,245 +1,277 @@ - + + PageLayoutUi - - + + 0 0 - 287 - 264 + 373 + 377 - + - - + + 9 - + 6 - - - - 0 - - + + + 6 - - - - Page Layout - - - - - - - - 7 - 0 - 0 - 0 - - - - QFrame::HLine - - - QFrame::Sunken - - - - - - - - + 0 - - 6 - - - + + Paper Format - - - - 7 - 0 + + + 0 0 - + QFrame::HLine - + QFrame::Sunken - - - - - 7 - 0 - 0 - 0 - - - - Choose a particular paper size, or set your own with "Custom" - - - 20 - - - - - - - - 7 - 0 - 0 - 0 - - - - Style used for the page header and footer - - - - - - - Headings &style: - - - pagestyleCO - - - - - - - &Landscape - - - - - - - &Portrait - - - true - - - - - - - - - - - - - false - - - - 5 - 0 - 0 - 0 - - - - - - - - false - - - - 5 - 0 - 0 - 0 - - - - - - - + + + &Format: - + papersizeCO - - - + + + + + 0 + 0 + + + + Choose a particular paper size, or set your own with "Custom" + + + 20 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + false - + &Height: - + paperheightLE - - - + + + false - + + + 0 + 0 + + + + + + + + + + + false + + &Width: - + paperwidthLE - - - + + + + false + + + + 0 + 0 + + + + + + + + + + &Orientation: - + portraitRB - + + + + &Portrait + + + true + + + + + + + &Landscape + + + + + + + 6 + + + 0 + + + + + Page Layout + + + + + + + + 0 + 0 + + + + QFrame::HLine + + + QFrame::Sunken + + + + + + + + + Headings &style: + + + pagestyleCO + + + + + + + + 0 + 0 + + + + Style used for the page header and footer + + + + - + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Lay out the page for double-sided printing + + + &Two-sided document + + + + + + + Background Color: + + + + + + Qt::Vertical - + QSizePolicy::Expanding - + 51 131 @@ -247,41 +279,57 @@ - - - - Lay out the page for double-sided printing - - - &Two-sided document - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - + + + + + + + 0 + 0 + + + + + 33 + 23 + + + + + + + + + + + + 23 + 23 + + + + ... + + + Qt::LeftArrow + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + @@ -304,7 +352,7 @@ facingPagesCB - qt_i18n.h + qt_i18n.h