Sat, 25 Feb 2017 17:24:33 +0200
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
--- a/src/colors.cpp Sat Feb 25 14:30:10 2017 +0200 +++ b/src/colors.cpp Sat Feb 25 17:24:33 2017 +0200 @@ -45,6 +45,12 @@ m_index {index} {} /* + * Constructs a direct color. + */ +LDColor::LDColor(QColor color, bool transparent) : + m_index {directColorIndex(color, transparent)} {} + +/* * Returns whether or not the color is valid. */ bool LDColor::isValid() const @@ -170,6 +176,18 @@ } /* + * Returns the LDraw color index for a direct color. + */ +qint32 LDColor::directColorIndex(QColor color, bool transparent) +{ + qint32 index = transparent ? 0x03000000 : 0x02000000; + index |= color.red() << 16; + index |= color.green() << 8; + index |= color.blue(); + return index; +} + +/* * LDColors are hashed by their index. */ uint qHash(LDColor color)
--- a/src/colors.h Sat Feb 25 14:30:10 2017 +0200 +++ b/src/colors.h Sat Feb 25 17:24:33 2017 +0200 @@ -55,6 +55,7 @@ public: LDColor(); LDColor(qint32 index); + LDColor(QColor color, bool transparent = false); LDColor(const LDColor& other) = default; bool isLDConfigColor() const; @@ -68,6 +69,7 @@ QString indexString() const; static const LDColor nullColor; + static qint32 directColorIndex(QColor color, bool transparent = false); static void initColors(); LDColor& operator=(qint32 index) { m_index = index; return *this; }
--- a/src/dialogs/colorselector.cpp Sat Feb 25 14:30:10 2017 +0200 +++ b/src/dialogs/colorselector.cpp Sat Feb 25 17:24:33 2017 +0200 @@ -29,164 +29,166 @@ #include "colorselector.h" #include "ui_colorselector.h" -enum { NUM_COLUMNS = 16 }; +/* + * Constructs a color selection dialog. + */ +ColorSelector::ColorSelector(QWidget* parent, LDColor defaultColor) : + QDialog {parent}, + HierarchyElement {parent}, + ui {*new Ui_ColorSelUi}, + m_selectedColor {LDColor::nullColor} +{ + ui.setupUi(this); -ColorSelector::ColorSelector (QWidget* parent, LDColor defaultvalue) : - QDialog (parent), - HierarchyElement (parent), - ui (*new Ui_ColorSelUi), - m_selection (defaultvalue) -{ - m_firstResize = true; - ui.setupUi (this); - - QGridLayout* layout = new QGridLayout (this); + QGridLayout* gridLayout = new QGridLayout; // Spawn color selector buttons - for (LDColor ldcolor; ldcolor.isLDConfigColor(); ++ldcolor) + for (LDColor color; color.isLDConfigColor(); ++color) { - QPushButton* button = new QPushButton (this); - button->setMinimumSize (QSize (32, 32)); - button->setMaximumSize (button->minimumSize()); + QPushButton* button = new QPushButton {this}; + button->setMinimumSize({32, 32}); + button->setMaximumSize(button->minimumSize()); - if (ldcolor.isValid ()) + if (color.isValid()) { - QColor color (ldcolor.faceColor()); + QColor faceColor = {color.faceColor()}; - if (ldcolor == MainColor) + if (color == MainColor) { - color = QColor (m_config->mainColor()); - color.setAlphaF (m_config->mainColorAlpha()); + faceColor = m_config->mainColor(); + faceColor.setAlphaF(m_config->mainColorAlpha()); } - QString color2name (luma (color) < 80 ? "white" : "black"); - button->setAutoFillBackground (true); - button->setStyleSheet (format ("background-color: rgba(%1, %2, %3, %4); color: %5", - color.red(), color.green(), color.blue(), color.alpha(), color2name)); - button->setCheckable (true); - button->setText (QString::number (ldcolor.index())); - button->setToolTip (format ("%1: %2", ldcolor.index(), ldcolor.name())); - m_buttons[ldcolor] = button; - m_buttonsReversed[button] = ldcolor; - connect (button, SIGNAL (clicked(bool)), this, SLOT (colorButtonClicked())); - - if (ldcolor == selection()) - button->setChecked (true); + QString edgeColor = luma(faceColor) < 80 ? "white" : "black"; + button->setAutoFillBackground(true); + button->setStyleSheet(format("background-color: rgba(%1, %2, %3, %4); color: %5", + faceColor.red(), faceColor.green(), faceColor.blue(), faceColor.alpha(), edgeColor)); + button->setCheckable(true); + button->setText(QString::number(color.index())); + button->setToolTip(format("%1: %2", color.index(), color.name())); + m_buttons[color] = button; + m_buttonsReversed[button] = color; + connect(button, SIGNAL(clicked(bool)), this, SLOT(colorButtonClicked())); } else { - button->setEnabled (false); + button->setEnabled(false); } - layout->addWidget (button, ldcolor.index() / NUM_COLUMNS, ldcolor.index() % NUM_COLUMNS); + gridLayout->addWidget(button, color.index() / columnCount, color.index() % columnCount); } - QWidget* widget = new QWidget(); - widget->setLayout (layout); - ui.definedColors->setWidget (widget); - connect (ui.directColor, SIGNAL (clicked (bool)), this, SLOT (chooseDirectColor())); - - ui.definedColors->setMinimumWidth (ui.definedColors->widget()->width() + 16); + QWidget* gridContainerWidget = new QWidget; + gridContainerWidget->setLayout(gridLayout); + ui.definedColors->setWidget(gridContainerWidget); + connect(ui.directColor, SIGNAL(clicked(bool)), this, SLOT(chooseDirectColor())); + ui.definedColors->setMinimumWidth(ui.definedColors->widget()->width() + 16); #ifdef TRANSPARENT_DIRECT_COLORS - connect (ui.transparentDirectColor, SIGNAL (clicked (bool)), this, SLOT (transparentCheckboxClicked())); + connect(ui.transparentDirectColor, SIGNAL(clicked(bool)), this, SLOT(transparentCheckboxClicked())); #else ui.transparentDirectColor->hide(); #endif - drawColorInfo(); + setSelectedColor(defaultColor); } +/* + * Destructs the color selection dialog. + */ ColorSelector::~ColorSelector() { delete &ui; } +/* + * Handles the press of a color button. + */ void ColorSelector::colorButtonClicked() { QPushButton* button = qobject_cast<QPushButton*>(sender()); LDColor color = m_buttonsReversed.value(button, LDColor::nullColor); if (color.isValid()) - { - // Uncheck the button we previously had pressed. - if (m_selection.isValid()) - { - QPushButton* button = m_buttons.value(m_selection); + setSelectedColor(color); +} - if (button) - button->setChecked(false); - } +/* + * Asks the user for a direct color. + */ +void ColorSelector::chooseDirectColor() +{ + QColor defaultColor = selectedColor() != -1 ? selectedColor().faceColor() : Qt::white; + QColor newColor = QColorDialog::getColor(defaultColor); - // Select the new color. - m_selection = color; - button->setChecked (true); - drawColorInfo(); - } + if (newColor.isValid()) + setSelectedColor({newColor, ui.transparentDirectColor->isChecked()}); } -void ColorSelector::drawColorInfo() +/* + * Handles the click of the transparent direct color option (only available of transparent direct colors are enabled). + */ +void ColorSelector::transparentCheckboxClicked() { - if (not selection().isValid()) - { - ui.colorLabel->setText ("---"); - ui.iconLabel->setPixmap (QPixmap()); - ui.transparentDirectColor->setChecked (false); - return; - } - - ui.colorLabel->setText (format ("%1 - %2", selection().indexString(), - (selection().isDirect() ? "<direct color>" : selection().name()))); - ui.iconLabel->setPixmap (guiUtilities()->makeColorIcon (selection(), 16).pixmap (16, 16)); - -#ifdef TRANSPARENT_DIRECT_COLORS - ui.transparentDirectColor->setEnabled (selection().isDirect()); - ui.transparentDirectColor->setChecked (selection().isDirect() and selection().faceColor().alphaF() < 1.0); -#else - ui.transparentDirectColor->setChecked (false); - ui.transparentDirectColor->setEnabled (false); -#endif + if (selectedColor().isDirect()) + setSelectedColor({selectedColor().faceColor(), ui.transparentDirectColor->isChecked()}); } -void ColorSelector::selectDirectColor (QColor color) +/* + * Convenience function for invoking the color selection dialog. + */ +bool ColorSelector::selectColor(QWidget* parent, LDColor& color, LDColor defaultColor) { - qint32 colorIndex = (ui.transparentDirectColor->isChecked() ? 0x03000000 : 0x02000000); - colorIndex |= (color.red() << 16) | (color.green() << 8) | (color.blue()); - m_selection = colorIndex; - drawColorInfo(); -} - -void ColorSelector::chooseDirectColor() -{ - QColor defcolor = selection() != -1 ? selection().faceColor() : Qt::white; - QColor newcolor = QColorDialog::getColor (defcolor); - - if (not newcolor.isValid()) - return; // canceled + ColorSelector dialog {parent, defaultColor}; - selectDirectColor (newcolor); -} - -void ColorSelector::transparentCheckboxClicked() -{ - if (selection().isDirect()) - selectDirectColor (selection().faceColor()); -} - -bool ColorSelector::selectColor (QWidget* parent, LDColor& val, LDColor defaultvalue) -{ - ColorSelector dlg (parent, defaultvalue); - - if (dlg.exec() and dlg.selection().isValid()) + if (dialog.exec() and dialog.selectedColor().isValid()) { - val = dlg.selection(); + color = dialog.selectedColor(); return true; } return false; } -LDColor ColorSelector::selection() const +/* + * Returns the currently selected color. + */ +LDColor ColorSelector::selectedColor() const +{ + return m_selectedColor; +} + +/* + * Changes the selected color to the one provided, and updates all relevant widgets. + */ +void ColorSelector::setSelectedColor(LDColor newColor) { - return m_selection; + // Uncheck the button we previously had pressed. + QPushButton* button = m_buttons.value(m_selectedColor); + + if (button) + button->setChecked(false); + + // Select the new color. + m_selectedColor = newColor; + button = m_buttons.value(newColor); + + if (button) + button->setChecked(true); + + if (m_selectedColor.isValid()) + { + ui.colorLabel->setText(format("%1 - %2", + newColor.indexString(), + newColor.isDirect() ? newColor.faceColor().name() : newColor.name())); + ui.iconLabel->setPixmap(guiUtilities()->makeColorIcon(newColor, 16).pixmap(16, 16)); + ui.transparentDirectColor->setEnabled(newColor.isDirect()); + ui.transparentDirectColor->setChecked(newColor.isDirect() and newColor.faceColor().alphaF() < 1.0); + } + else + { + ui.colorLabel->setText("---"); + ui.iconLabel->setPixmap({}); + ui.transparentDirectColor->setChecked(false); + } }
--- a/src/dialogs/colorselector.h Sat Feb 25 14:30:10 2017 +0200 +++ b/src/dialogs/colorselector.h Sat Feb 25 17:24:33 2017 +0200 @@ -22,28 +22,30 @@ #include "../colors.h" #include "../hierarchyelement.h" +/* + * Implements a dialog that asks the user to choose an LDraw color from a grid of available colors. Direct colors are also supported. + */ class ColorSelector : public QDialog, public HierarchyElement { Q_OBJECT public: - explicit ColorSelector (QWidget* parent, LDColor defaultvalue = LDColor::nullColor); - virtual ~ColorSelector(); - static bool selectColor (QWidget* parent, LDColor& val, LDColor defval = LDColor::nullColor); - LDColor selection() const; + ColorSelector(QWidget* parent, LDColor defaultvalue = LDColor::nullColor); + ~ColorSelector(); + + static bool selectColor(QWidget* parent, LDColor& color, LDColor defaultColor = LDColor::nullColor); + LDColor selectedColor() const; + Q_SLOT void setSelectedColor(LDColor color); private: + static const int columnCount = 16; + + Q_SLOT void colorButtonClicked(); + Q_SLOT void chooseDirectColor(); + Q_SLOT void transparentCheckboxClicked(); + class Ui_ColorSelUi& ui; QMap<LDColor, QPushButton*> m_buttons; QMap<QPushButton*, LDColor> m_buttonsReversed; - bool m_firstResize; - LDColor m_selection; - - void drawColorInfo(); - void selectDirectColor (QColor col); - -private slots: - void colorButtonClicked(); - void chooseDirectColor(); - void transparentCheckboxClicked(); + LDColor m_selectedColor; };