X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FXGUI%2FXGUI_ColorDialog.cpp;h=184b56c68fc86c4cc9957a8f86598f7f90dd6187;hb=4de8d53fef7532b6843d79c970f55ad46f98da62;hp=625bc9fe437be7af90daa2c8cb025204bc57a86f;hpb=97917d3698f5a2f7fc9596e7c755ff8f6751e373;p=modules%2Fshaper.git diff --git a/src/XGUI/XGUI_ColorDialog.cpp b/src/XGUI/XGUI_ColorDialog.cpp index 625bc9fe4..184b56c68 100644 --- a/src/XGUI/XGUI_ColorDialog.cpp +++ b/src/XGUI/XGUI_ColorDialog.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2019 CEA/DEN, EDF R&D +// Copyright (C) 2014-2024 CEA, EDF // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -28,15 +28,56 @@ #include #include #include +#include +#include + +namespace +{ + class RadioButton: public QRadioButton + { + QWidget* myBuddy; + public: + RadioButton(const QString& text, QWidget* buddy = nullptr, QWidget* parent = nullptr): + QRadioButton(text, parent), myBuddy(buddy) + { + if (buddy != nullptr) + { + buddy->setEnabled(isEnabled()); + buddy->installEventFilter(this); + } + } + RadioButton(QWidget* buddy = nullptr, QWidget* parent = nullptr): + RadioButton(QString(), buddy, parent) {} + + bool eventFilter(QObject* sender, QEvent* event) + { + if (myBuddy != nullptr && sender == myBuddy && event->type() == QEvent::MouseButtonPress) + setChecked(true); + return QRadioButton::eventFilter(sender, event); + } + + void changeEvent(QEvent* event) + { + if (myBuddy != nullptr && event->type() == QEvent::EnabledChange) + myBuddy->setEnabled(isEnabled()); + QRadioButton::changeEvent(event); + } + }; +} XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent) - : QDialog(theParent) + : QDialog(theParent, Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint) { setWindowTitle(tr("Color")); QGridLayout* aLay = new QGridLayout(this); - QRadioButton* aRandomChoiceBtn = new QRadioButton(this); - QRadioButton* aColorChoiceBtn = new QRadioButton(this); + myColorButton = new QtxColorButton(this); + myColorButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); + + QLabel* aRandomLabel = new QLabel(tr("Random"), this); + + QRadioButton* aColorChoiceBtn = new RadioButton(myColorButton, this); + QRadioButton* aRandomChoiceBtn = new RadioButton(aRandomLabel, this); aColorChoiceBtn->setChecked(true); myButtonGroup = new QButtonGroup(this); myButtonGroup->setExclusive(true); @@ -44,13 +85,8 @@ XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent) myButtonGroup->addButton(aRandomChoiceBtn, 1); aLay->addWidget(aColorChoiceBtn, 0, 0); - aLay->addWidget(aRandomChoiceBtn, 1, 0); - - myColorButton = new QtxColorButton(this); - myColorButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); aLay->addWidget(myColorButton, 0, 1); - - QLabel* aRandomLabel = new QLabel(tr("Random"), this); + aLay->addWidget(aRandomChoiceBtn, 1, 0); aLay->addWidget(aRandomLabel, 1, 1); QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, @@ -62,8 +98,6 @@ XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent) bool XGUI_ColorDialog::isRandomColor() const { - int anId = myButtonGroup->checkedId(); - return myButtonGroup->checkedId() == 1; } @@ -86,108 +120,13 @@ std::vector XGUI_ColorDialog::getColor() const return aValues; } -// contains global cash for integer index of the color -> RGB of this color -static std::map > myColorMap; - -void appendValues(std::vector& theRGB, const int theRed, const int theGreen, const int theBlue) -{ - theRGB.push_back(theRed); - theRGB.push_back(theGreen); - theRGB.push_back(theBlue); -} - -bool containsValues(std::map >& theColorMap, std::vector& theValues) -{ - std::map >::const_iterator anIt = theColorMap.begin(), - aLast = theColorMap.end(); - bool isFound = false; - for (; anIt != aLast && !isFound; anIt++) { - std::vector aValues = anIt->second; - isFound = aValues[0] == theValues[0] && - aValues[1] == theValues[1] && - aValues[2] == theValues[2]; - } - return isFound; -} - -std::vector HSVtoRGB(int theH, int theS, int theV) -{ - std::vector aRGB; - if (theH < 0 || theH > 360 || - theS < 0 || theS > 100 || - theV < 0 || theV > 100) - return aRGB; - - int aHi = (int)theH/60; - - double aV = theV; - double aVmin = (100 - theS)*theV/100; - - double anA = (theV - aVmin)* (theH % 60) / 60; - - double aVinc = aVmin + anA; - double aVdec = theV - anA; - - double aPercentToValue = 255./100; - int aV_int = (int)(aV*aPercentToValue); - int aVinc_int = (int)(aVinc*aPercentToValue); - int aVmin_int = (int)(aVmin*aPercentToValue); - int aVdec_int = (int)(aVdec*aPercentToValue); - - switch(aHi) { - case 0: appendValues(aRGB, aV_int, aVinc_int, aVmin_int); break; - case 1: appendValues(aRGB, aVdec_int, aV_int, aVmin_int); break; - case 2: appendValues(aRGB, aVmin_int, aV_int, aVinc_int); break; - case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break; - case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break; - case 5: appendValues(aRGB, aV_int, aVmin_int, aVdec_int); break; - default: break; - } - return aRGB; -} - - -void fillColorMap() -{ - if (!myColorMap.empty()) - return; - - int i = 0; - for (int s = 100; s > 0; s = s - 50) - { - for (int v = 100; v >= 40; v = v - 20) - { - for (int h = 0; h < 359 ; h = h + 60) - { - std::vector aColor = HSVtoRGB(h, s, v); - if (containsValues(myColorMap, aColor)) - continue; - myColorMap[i] = aColor; - i++; - } - } - } -} - -void findRandomColor(std::vector& theValues) -{ - theValues.clear(); - if (myColorMap.empty()) { - fillColorMap(); - } - - size_t aSize = myColorMap.size(); - int anIndex = rand() % aSize; - if (myColorMap.find(anIndex) != myColorMap.end()) { - theValues = myColorMap.at(anIndex); - } -} std::vector XGUI_ColorDialog::getRandomColor() const { std::vector aValues; if (isRandomColor()) { - findRandomColor(aValues); + ModelAPI_Tools::findRandomColor(aValues); } return aValues; } +