]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBox.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
index 1e0abd625997a93a68dd85e8ae0d73c97575d3a1..6a991b5df5063e4fc6a61827c70397038ed76466 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "GuiApplication.h"
 #include "ColorCache.h"
+#include "ColorSet.h"
 #include "LengthCombo.h"
 #include "Length.h"
 #include "qt_helpers.h"
@@ -25,7 +26,6 @@
 #include "insets/InsetBox.h"
 
 #include "support/gettext.h"
-#include "support/foreach.h"
 #include "support/lstrings.h"
 
 #include <QComboBox>
@@ -76,60 +76,32 @@ static QStringList boxGuiSpecialLengthNames()
 }
 
 
-static QList<ColorPair> colorData()
+static QList<ColorCode> colors()
 {
-       QList<ColorPair> colors;
-       colors << ColorPair(qt_("none"), Color_none);
-       colors << ColorPair(qt_("black"), Color_black);
-       colors << ColorPair(qt_("white"), Color_white);
-       colors << ColorPair(qt_("blue"), Color_blue);
-       colors << ColorPair(qt_("brown"), Color_brown);
-       colors << ColorPair(qt_("cyan"), Color_cyan);
-       colors << ColorPair(qt_("darkgray"), Color_darkgray);
-       colors << ColorPair(qt_("gray"), Color_gray);
-       colors << ColorPair(qt_("green"), Color_green);
-       colors << ColorPair(qt_("lightgray"), Color_lightgray);
-       colors << ColorPair(qt_("lime"), Color_lime);
-       colors << ColorPair(qt_("magenta"), Color_magenta);
-       colors << ColorPair(qt_("olive"), Color_olive);
-       colors << ColorPair(qt_("orange"), Color_orange);
-       colors << ColorPair(qt_("pink"), Color_pink);
-       colors << ColorPair(qt_("purple"), Color_purple);
-       colors << ColorPair(qt_("red"), Color_red);
-       colors << ColorPair(qt_("teal"), Color_teal);
-       colors << ColorPair(qt_("violet"), Color_violet);
-       colors << ColorPair(qt_("yellow"), Color_yellow);
+       QList<ColorCode> colors;
+       colors << Color_black;
+       colors << Color_white;
+       colors << Color_blue;
+       colors << Color_brown;
+       colors << Color_cyan;
+       colors << Color_darkgray;
+       colors << Color_gray;
+       colors << Color_green;
+       colors << Color_lightgray;
+       colors << Color_lime;
+       colors << Color_magenta;
+       colors << Color_olive;
+       colors << Color_orange;
+       colors << Color_pink;
+       colors << Color_purple;
+       colors << Color_red;
+       colors << Color_teal;
+       colors << Color_violet;
+       colors << Color_yellow;
        return colors;
 }
 
 
-template<typename T>
-void fillComboColor(QComboBox * combo, QList<T> const & list, bool const is_none)
-{
-       QPixmap coloritem(32, 32);
-       QColor color;
-       // frameColorCO cannot be uncolored
-       if (is_none)
-               combo->addItem(qt_("none"));
-       typename QList<T>::const_iterator cit = list.begin() + 1;
-       for (; cit != list.end(); ++cit) {
-               color = QColor(guiApp->colorCache().get(cit->second, false));
-               coloritem.fill(color);
-               combo->addItem(QIcon(coloritem), cit->first);
-       }
-}
-
-
-template<class P>
-static int findPos2nd(QList<P> const & vec, QString val)
-{
-       for (int i = 0; i != vec.size(); ++i)
-               if (vec[i].first == val)
-                       return i;
-       return 0;
-}
-
-
 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
 {
        setupUi(this);
@@ -165,8 +137,8 @@ GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
        connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
        connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SIGNAL(changed()));
-       connect(frameColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
-       connect(backgroundColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
+       connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
+               this, SIGNAL(changed()));
 
        heightED->setValidator(unsignedLengthValidator(heightED));
        widthED->setValidator(unsignedLengthValidator(widthED));
@@ -181,20 +153,39 @@ GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
        addCheckedWidget(separationED, separationLA);
        addCheckedWidget(shadowsizeED, shadowsizeLA);
 
-       // initialize colors
-       color = colorData();
        // the background can be uncolored while the frame cannot
-       fillComboColor(frameColorCO, color, false);
-       fillComboColor(backgroundColorCO, color, true);
+       color_codes_ = colors();
+       qSort(color_codes_.begin(), color_codes_.end(), ColorSorter);
+       fillComboColor(backgroundColorCO, true);
+       fillComboColor(frameColorCO, false);
 
        initDialog();
 }
 
 
-void GuiBox::on_innerBoxCO_activated(int /* index */)
+void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
 {
-       QString itype =
-               innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
+       combo->clear();
+       QPixmap coloritem(32, 32);
+       QColor color;
+       // frameColorCO cannot be uncolored
+       if (is_none)
+               combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
+                              toqstr(lcolor.getLaTeXName(Color_none)));
+       QList<ColorCode>::const_iterator cit = color_codes_.begin();
+       for (; cit != color_codes_.end(); ++cit) {
+               QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
+               QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
+               color = QColor(guiApp->colorCache().get(*cit, false));
+               coloritem.fill(color);
+               combo->addItem(QIcon(coloritem), guiname, latexname);
+       }
+}
+
+
+void GuiBox::on_innerBoxCO_activated(int index)
+{
+       QString itype = innerBoxCO->itemData(index).toString();
        // handle parbox and minipage the same way
        bool const ibox = (itype != "none" && itype != "makebox");
        if (heightCB->isChecked() && !ibox)
@@ -230,23 +221,29 @@ void GuiBox::on_typeCO_activated(int index)
        }
        // assure that the frame color is black for frameless boxes to
        // provide the color "none"
-       if (frameless && frameColorCO->currentText() != qt_("black"))
-               frameColorCO->setCurrentIndex(0);
+       int const b = frameColorCO->findData("black");
+       if (frameless && frameColorCO->currentIndex() != b)
+               frameColorCO->setCurrentIndex(b);
        changed();
 }
 
 
-void GuiBox::on_frameColorCO_currentIndexChanged(int /* index */)
+void GuiBox::on_frameColorCO_currentIndexChanged(int index)
 {
-       // if there is a special frme color the background canot be uncolored
-       if (frameColorCO->currentText() != qt_("black")) {
-               if (backgroundColorCO->currentText() == qt_("none"))
-                       backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_("white")));
-               if (backgroundColorCO->itemText(0) == qt_("none"))
-                       backgroundColorCO->removeItem(0);
+       // if there is a non-black frame color the background cannot be uncolored
+       // therefore remove the entry "none" in this case
+       int const n = backgroundColorCO->findData("none");
+       if (index != frameColorCO->findData("black")) {
+               if (n != -1) {
+                       if (backgroundColorCO->currentIndex() == n)
+                               backgroundColorCO->setCurrentIndex(
+                                           backgroundColorCO->findData("white"));
+                       backgroundColorCO->removeItem(n);
+               }
        } else {
-               if (backgroundColorCO->itemText(0) != qt_("none"))
-                       backgroundColorCO->insertItem(0, qt_("none"));
+               if (n == -1)
+                       backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
+                                                     toqstr(lcolor.getLaTeXName(Color_none)));
        }
        changed();
 }
@@ -270,9 +267,6 @@ void GuiBox::initDialog()
        // LaTeX's default for \shadowsize is 4 pt
        shadowsizeED->setText("4");
        shadowsizeUnitsLC->setCurrentItem(Length::PT);
-       // the default color is black and none
-       frameColorCO->setCurrentIndex(findPos2nd(color, qt_("black")) - 1);
-       backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_("none")));
 }
 
 
@@ -339,14 +333,16 @@ void GuiBox::paramsToDialog(Inset const * inset)
        ialignCO->setEnabled(ibox);
        setSpecial(ibox);
 
-       // halign is only allowed if a width is used
-       halignCO->setEnabled(widthCB->isChecked());
+       // halign is only allowed without inner box and if a width is used and if
+       // pagebreak is not used
+       halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
+                            && ((!ibox && type == "Boxed") || inner_type == "makebox"));
        // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
        if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
                && halignCO->count() < 4)
-               halignCO->addItem(toqstr("Stretch"));
+               halignCO->addItem(qt_("Stretch"));
        else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
-               halignCO->removeItem(3); 
+               halignCO->removeItem(3);
        // pagebreak is only allowed for Boxed without inner box
        pagebreakCB->setEnabled(!ibox && type == "Boxed");
 
@@ -407,11 +403,8 @@ void GuiBox::paramsToDialog(Inset const * inset)
        lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
                (params.shadowsize).asString(), default_unit);
        // set color
-       frameColorCO->setCurrentIndex(findPos2nd(color, qt_(params.framecolor)) - 1);
-       if (frameColorCO->currentText() != qt_("black"))
-               backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)) - 1);
-       else
-               backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)));
+       frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
+       backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
 }
 
 
@@ -446,7 +439,7 @@ docstring GuiBox::dialogToParams() const
                if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
                        params.special = fromqstr(unit);
                        // Note: the unit is simply ignored in this case
-                       params.width = Length(value.toDouble(), Length::IN);
+                       params.width = Length(widgetToDouble(widthED), Length::IN);
                } else {
                        params.special = "none";
                        // we must specify a valid length in this case
@@ -472,7 +465,7 @@ docstring GuiBox::dialogToParams() const
                if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
                        params.height_special = fromqstr(unit);
                        // Note: the unit is simply ignored in this case
-                       params.height = Length(value.toDouble(), Length::IN);
+                       params.height = Length(widgetToDouble(heightED), Length::IN);
                } else {
                        params.height_special = "none";
                        params.height =
@@ -494,16 +487,14 @@ docstring GuiBox::dialogToParams() const
        else
                params.shadowsize = Length("4pt");
        if (frameColorCO->isEnabled())
-               params.framecolor = fromqstr(color[frameColorCO->currentIndex() + 1].first);
+               params.framecolor =
+                       fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
        else
                params.framecolor = "black";
-       if (backgroundColorCO->isEnabled()) {
-               // only if the framecolor is black the backgroundcolor has the entry "none"
-               if (frameColorCO->currentText() != qt_("black"))
-                       params.backgroundcolor = fromqstr(color[backgroundColorCO->currentIndex() + 1].first);
-               else
-                       params.backgroundcolor = fromqstr(color[backgroundColorCO->currentIndex()].first);
-       } else
+       if (backgroundColorCO->isEnabled())
+               params.backgroundcolor =
+                       fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
+       else
                params.backgroundcolor = "none";
 
        return from_ascii(InsetBox::params2string(params));
@@ -557,12 +548,14 @@ bool GuiBox::checkWidgets(bool readonly) const
                        widthED->setEnabled(false);
                        widthUnitsLC->setEnabled(false);
                }
-               // halign is only allowed if a width is used
-               halignCO->setEnabled(widthCB->isChecked());
+               // halign is only allowed without inner box and if a width is used and if
+               // pagebreak is not used
+               halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
+                                    && ((!ibox && outer == "Boxed") || itype == "makebox"));
                // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
                if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
                        && halignCO->count() < 4)
-                       halignCO->addItem(toqstr("Stretch"));
+                       halignCO->addItem(qt_("Stretch"));
                else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
                        halignCO->removeItem(3);
                // pagebreak is only allowed for Boxed without inner box