1 /***************************************************************************
2 * Copyright (C) 2005 by Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "colorvalue.h"
23 #include <QHBoxLayout>
26 #include <QDoubleSpinBox>
27 #include <QGridLayout>
31 #include <dcore/debug.h>
32 #include <dgui/comboboxf.h>
36 namespace Components
{
38 struct ItemColorValue::Private
47 ItemColorValue::ItemColorValue( const QString
&text
, QWidget
*parent
) :QFrame(parent
), d(new Private
)
49 QHBoxLayout
* layout
= new QHBoxLayout
;
50 layout
->setSpacing(0);
54 QLabel
*labelText
= new QLabel( text
, this);
55 labelText
->setSizePolicy ( QSizePolicy::Fixed
, QSizePolicy::Fixed
);
56 d
->value
= new QSpinBox(this);
57 d
->value
->setMaximum ( 255 );
58 d
->value
->setMinimum ( 0 );
59 d
->value
->setSizePolicy ( QSizePolicy::Fixed
, QSizePolicy::Fixed
);
60 connect(d
->value
, SIGNAL(valueChanged( int)) , this, SIGNAL(valueChanged( int)));
61 connect(d
->value
, SIGNAL(valueChanged( const QString
&)), this, SIGNAL(valueChanged( const QString
&)));
62 connect(d
->value
, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
63 layout
->addWidget(labelText
);
64 layout
->addWidget(d
->value
);
67 ItemColorValue::~ItemColorValue()
72 void ItemColorValue::setValue( int val
)
74 d
->value
->setValue(val
);
77 int ItemColorValue::value()
79 return d
->value
->value();
82 void ItemColorValue::setMax(int max
)
84 d
->value
->setMaximum( max
);
87 void ItemColorValue::setRange(int minimum
, int maximum
)
89 d
->value
->setRange( minimum
, maximum
);
92 void ItemColorValue::setSuffix(const QString
&suffix
)
94 d
->value
->setSuffix(suffix
);
97 struct ColorValue::Private
99 ItemColorValue
*valueR
, *valueG
, *valueB
, *valueH
, *valueS
, *valueV
;
100 DGui::ComboBoxF
*valueA
;
101 // QGridLayout *layout;
105 ColorValue::ColorValue(QWidget
*parent
) : QFrame(parent
), d(new Private
)
110 setLayout(new QVBoxLayout());
111 // d->layout = new QGridLayout;
112 // setLayout(d->layout);
118 ColorValue::~ColorValue()
123 void ColorValue::setupForm()
125 QGridLayout
*gridLayout
= new QGridLayout
;
127 d
->valueR
= new ItemColorValue("R");
128 connect(d
->valueR
, SIGNAL(editingFinished()), this, SLOT(syncValuesRgb()));
130 d
->valueG
= new ItemColorValue("G", this);
131 connect(d
->valueG
, SIGNAL(editingFinished()), this, SLOT(syncValuesRgb()));
133 d
->valueB
= new ItemColorValue("B", this);
134 connect(d
->valueB
, SIGNAL(editingFinished()), this, SLOT(syncValuesRgb()));
136 d
->valueH
= new ItemColorValue("H", this);
137 d
->valueH
->setMax(359);
138 connect(d
->valueH
, SIGNAL(valueChanged(int)), this, SIGNAL(hueChanged(int)));
140 d
->valueS
= new ItemColorValue("S", this);
141 connect(d
->valueS
, SIGNAL(valueChanged(int)), this, SIGNAL(saturationChanged( int)));
143 d
->valueV
= new ItemColorValue("V", this);
144 connect(d
->valueV
, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged( int)));
146 QHBoxLayout
*boxLayout
= new QHBoxLayout
;
147 boxLayout
->setSpacing(1);
149 d
->valueA
= new DGui::ComboBoxF(0, 255, this);
150 d
->valueA
->setDecimals(0);
152 QLabel
* label
= new QLabel("A");
153 label
->setSizePolicy ( QSizePolicy::Fixed
, QSizePolicy::Fixed
);
154 boxLayout
->addWidget(label
);
156 boxLayout
->addWidget(d
->valueA
);
158 for(int i
= 0; i
<= 100; i
+= 25)
160 d
->valueA
->addPercent(i
);
163 connect(d
->valueA
, SIGNAL(editingFinished()), this, SLOT(syncValuesRgb()));
164 connect(d
->valueA
, SIGNAL(activated(int)), this, SLOT(syncValuesRgb(int)));
166 QCheckBox
*show
= new QCheckBox(tr("percent"));
167 show
->setChecked(d
->valueA
->showAsPercent());
169 gridLayout
->addWidget(d
->valueR
, 0, 0,Qt::AlignTop
| Qt::AlignLeft
);
170 gridLayout
->addWidget(d
->valueG
, 1, 0,Qt::AlignTop
| Qt::AlignLeft
);
171 gridLayout
->addWidget(d
->valueB
, 2, 0,Qt::AlignTop
| Qt::AlignLeft
);
172 gridLayout
->addWidget(d
->valueH
, 0, 1,Qt::AlignTop
| Qt::AlignLeft
);
173 gridLayout
->addWidget(d
->valueS
, 1, 1,Qt::AlignTop
| Qt::AlignLeft
);
174 gridLayout
->addWidget(d
->valueV
, 2, 1,Qt::AlignTop
| Qt::AlignLeft
);
177 boxLayout
->addWidget(show
);
179 static_cast<QHBoxLayout
*>(layout())->addLayout(gridLayout
);
180 static_cast<QHBoxLayout
*>(layout())->addLayout(boxLayout
);
182 connect(show
, SIGNAL(toggled( bool )), d
->valueA
, SLOT(setShowAsPercent( bool )));
186 void ColorValue::setColor(const QBrush
&brush
)
188 QColor color
= brush
.color();
190 d
->valueR
->setValue(color
.red());
191 d
->valueG
->setValue(color
.green());
192 d
->valueB
->setValue(color
.blue());
193 d
->valueH
->setValue(color
.hue ());
194 d
->valueS
->setValue(color
.saturation());
195 d
->valueV
->setValue(color
.value ());
197 d
->valueA
->setValue( d
->valueA
->currentIndex(), color
.alpha() );
202 void ColorValue::syncValuesRgb(int)
206 int r
= d
->valueR
->value();
207 int g
= d
->valueG
->value();
208 int b
= d
->valueB
->value();
209 int a
= (int) ::ceil(d
->valueA
->value());
211 QColor tmp
= QColor::fromRgb(r
,g
,b
,a
);
212 d
->valueH
->setValue( tmp
.hue ());
213 d
->valueS
->setValue( tmp
.saturation());
214 d
->valueV
->setValue( tmp
.value ());
215 emit
brushChanged(QColor::fromRgb(r
,g
,b
,a
));
219 int ColorValue::hue()
221 return d
->valueH
->value();
224 int ColorValue::saturation()
226 return d
->valueS
->value();
229 int ColorValue::value()
231 return d
->valueV
->value();
234 int ColorValue::alpha()
236 return (int) ::ceil(d
->valueA
->value());