1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
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. *
9 ***************************************************************************/
11 #include "colorplaneexport.h"
12 #include <KMessageBox>
19 const QString EXTENSION_PNG = ".png";
20 const int SCALE_RANGE = 80;
22 ColorPlaneExport::ColorPlaneExport(QWidget *parent) :
27 m_colorTools = new ColorTools();
29 tResX->setText("800");
30 tResY->setText("800");
32 cbColorspace->addItem(i18n("YUV UV plane"), QVariant(ColorPlaneExport::CPE_YUV));
33 cbColorspace->addItem(i18n("YUV Y plane"), QVariant(ColorPlaneExport::CPE_YUV_Y));
34 cbColorspace->addItem(i18n("Modified YUV (Chroma)"), QVariant(ColorPlaneExport::CPE_YUV_MOD));
35 cbColorspace->addItem(i18n("YCbCr CbCr plane"), QVariant(ColorPlaneExport::CPE_YPbPr));
36 cbColorspace->addItem(i18n("RGB plane, one component varying"), QVariant(ColorPlaneExport::CPE_RGB_CURVE));
37 cbColorspace->addItem(i18n("HSV Hue Shift"), QVariant(ColorPlaneExport::CPE_HSV_HUESHIFT));
38 cbColorspace->addItem(i18n("HSV Saturation"), QVariant(ColorPlaneExport::CPE_HSV_SATURATION));
40 sliderColor->setSliderPosition(128);
45 sliderScaling->setInvertedAppearance(true);
46 sliderScaling->setRange(0, 80);
47 sliderScaling->setSliderPosition(50);
49 connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExportPlane()));
50 connect(tResX, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
51 connect(tResY, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
52 connect(kurlrequester, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
53 connect(sliderColor, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
54 connect(sliderScaling, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
55 connect(cbColorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotColormodeChanged()));
57 kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
59 slotColormodeChanged();
63 ColorPlaneExport::~ColorPlaneExport()
70 ///// Helper functions /////
72 void ColorPlaneExport::enableSliderScaling(const bool &enable)
74 sliderScaling->setEnabled(enable);
75 lblScaling->setEnabled(enable);
76 lblScaleNr->setEnabled(enable);
79 void ColorPlaneExport::enableSliderColor(const bool &enable)
81 sliderColor->setEnabled(enable);
82 lblSliderName->setEnabled(enable);
83 lblColNr->setEnabled(enable);
86 void ColorPlaneExport::enableCbVariant(const bool &enable)
88 cbVariant->setEnabled(enable);
89 lblVariant->setEnabled(enable);
91 while (cbVariant->count() > 0) {
92 cbVariant->removeItem(0);
101 void ColorPlaneExport::slotUpdateDisplays()
103 m_scaling = 1 - (float)sliderScaling->value()/100;
105 switch(cbColorspace->itemData(cbColorspace->currentIndex()).toInt()){
107 lblScaleNr->setText(QChar(0xb1) + QString::number(sliderScaling->value(), 'f', 2));;
109 case CPE_HSV_HUESHIFT:
110 lblScaleNr->setText(QString::number(sliderScaling->value()));
113 lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
117 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
119 lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
122 lblColNr->setText(QString::number(sliderColor->value()));
126 lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
129 void ColorPlaneExport::slotValidate()
134 nr = QVariant(tResX->text()).toInt(&ok);
137 nr = QVariant(tResY->text()).toInt(&ok);
141 ok = kurlrequester->text().trimmed().length() > 0;
143 qDebug() << "File given: " << ok;
148 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
150 buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
153 slotUpdateDisplays();
156 void ColorPlaneExport::slotExportPlane()
159 qDebug() << "Exporting plane now to " << kurlrequester->text();
161 QString lower = kurlrequester->text().toLower();
163 qDebug() << "Lower: " << lower;
165 if (!lower.endsWith(".png") && !lower.endsWith(".jpg") && !lower.endsWith(".tif") && !lower.endsWith(".tiff")) {
166 if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", EXTENSION_PNG)) == KMessageBox::Yes) {
167 kurlrequester->setUrl(KUrl(kurlrequester->text() + ".png"));
172 QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
173 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
175 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
178 img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
181 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
184 img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()),
185 (double)sliderScaling->value()/255);
188 img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
190 case CPE_HSV_HUESHIFT:
191 img = m_colorTools->hsvHueShiftPlane(size, sliderColor->value(), sliderScaling->value(), -180, 180);
193 case CPE_HSV_SATURATION:
194 col.setHsv(0, 0, sliderColor->value());
195 img = m_colorTools->hsvCurvePlane(size, col, ColorTools::COM_H, ColorTools::COM_S);
200 img.save(kurlrequester->text());
203 void ColorPlaneExport::slotColormodeChanged()
206 qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
208 lblScaling->setText(i18n("Scaling"));
209 sliderScaling->setInvertedAppearance(true);
210 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
214 enableSliderScaling(true);
215 enableSliderColor(true);
216 enableCbVariant(false);
217 sliderColor->setRange(0,255);
218 sliderColor->setPageStep(128);
219 lblSliderName->setText(i18n("Y value"));
220 lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
224 qDebug() << "Changing slider range.";
226 enableSliderScaling(true);
227 enableSliderColor(true);
228 enableCbVariant(false);
229 sliderColor->setMaximum(321);
230 sliderColor->setRange(0,179);
231 sliderColor->setPageStep(90);
232 lblSliderName->setText(i18n("UV angle"));
233 lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
236 enableSliderScaling(true);
237 enableSliderColor(false);
238 enableCbVariant(true);
239 sliderScaling->setRange(1,255);
240 sliderScaling->setValue(255);
241 cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
242 cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
243 cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
244 cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
246 case CPE_HSV_HUESHIFT:
247 enableSliderScaling(true);
248 enableSliderColor(true);
249 enableCbVariant(false);
250 sliderScaling->setRange(0,255);
251 sliderScaling->setValue(200);
252 sliderScaling->setInvertedAppearance(false);
253 sliderColor->setRange(0,255);
254 sliderColor->setValue(200);
255 lblSliderName->setText(i18n("HSV Saturation"));
256 lblScaling->setText(i18n("HSV Value"));
258 case CPE_HSV_SATURATION:
259 enableSliderScaling(false);
260 enableSliderColor(true);
261 sliderColor->setRange(0, 255);
262 sliderColor->setValue(200);
263 lblSliderName->setText(i18n("HSV Value"));
266 enableSliderScaling(false);
267 enableSliderColor(false);
268 enableCbVariant(true);
269 cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
270 cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
271 cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
272 cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
276 slotUpdateDisplays();