]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBox.cpp
Use QFontMetrics information for underlines (and friends) width and position
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
index 2da69472aafb974cb87becc7092eaae3a6e353c7..c76aeaf2b5ecdccbf5121b1b9c6be08c75b146fd 100644 (file)
@@ -15,6 +15,9 @@
 
 #include "GuiBox.h"
 
+#include "GuiApplication.h"
+#include "ColorCache.h"
+#include "ColorSet.h"
 #include "LengthCombo.h"
 #include "Length.h"
 #include "qt_helpers.h"
@@ -26,6 +29,7 @@
 #include "support/foreach.h"
 #include "support/lstrings.h"
 
+#include <QComboBox>
 #include <QLineEdit>
 #include <QPushButton>
 
@@ -73,6 +77,42 @@ static QStringList boxGuiSpecialLengthNames()
 }
 
 
+static QList<ColorCode> colors()
+{
+       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;
+}
+
+
+namespace {
+
+bool ColorSorter(ColorCode lhs, ColorCode rhs)
+{
+       return support::compare_no_case(lcolor.getGUIName(lhs), lcolor.getGUIName(rhs)) < 0;
+}
+
+} // namespace anon
+
+
 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
 {
        setupUi(this);
@@ -108,6 +148,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(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
+               this, SIGNAL(changed()));
 
        heightED->setValidator(unsignedLengthValidator(heightED));
        widthED->setValidator(unsignedLengthValidator(widthED));
@@ -122,14 +164,39 @@ GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
        addCheckedWidget(separationED, separationLA);
        addCheckedWidget(shadowsizeED, shadowsizeLA);
 
+       // the background can be uncolored while the frame cannot
+       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)
@@ -163,6 +230,32 @@ void GuiBox::on_typeCO_activated(int index)
                        widthCB->setChecked(itype != "none");
                pagebreakCB->setChecked(false);
        }
+       // assure that the frame color is black for frameless boxes to
+       // provide the color "none"
+       int const b = frameColorCO->findData("black");
+       if (frameless && frameColorCO->currentIndex() != b)
+               frameColorCO->setCurrentIndex(b);
+       changed();
+}
+
+
+void GuiBox::on_frameColorCO_currentIndexChanged(int index)
+{
+       // 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 (n == -1)
+                       backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
+                                                     toqstr(lcolor.getLaTeXName(Color_none)));
+       }
        changed();
 }
 
@@ -318,6 +411,9 @@ void GuiBox::paramsToDialog(Inset const * inset)
        shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
        lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
                (params.shadowsize).asString(), default_unit);
+       // set color
+       frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
+       backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
 }
 
 
@@ -399,6 +495,16 @@ docstring GuiBox::dialogToParams() const
                params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
        else
                params.shadowsize = Length("4pt");
+       if (frameColorCO->isEnabled())
+               params.framecolor =
+                       fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
+       else
+               params.framecolor = "black";
+       if (backgroundColorCO->isEnabled())
+               params.backgroundcolor =
+                       fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
+       else
+               params.backgroundcolor = "none";
 
        return from_ascii(InsetBox::params2string(params));
 }
@@ -492,6 +598,9 @@ bool GuiBox::checkWidgets(bool readonly) const
                        shadowsizeED->setText("4");
                        shadowsizeUnitsLC->setCurrentItem(Length::PT);
                }
+               // \fboxcolor and \colorbox cannot be used for fancybox boxes
+               frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
+               backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
        }
 
        return InsetParamsWidget::checkWidgets();