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"));
171 QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
172 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
174 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
177 img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
180 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
183 img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()),
184 (double)sliderScaling->value()/255);
187 img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
189 case CPE_HSV_HUESHIFT:
190 img = m_colorTools->hsvHueShiftPlane(size, sliderColor->value(), sliderScaling->value(), -180, 180);
192 case CPE_HSV_SATURATION:
193 img = m_colorTools->hsvSaturationPlane(size, sliderColor->value(), 0, 255);
198 img.save(kurlrequester->text());
201 void ColorPlaneExport::slotColormodeChanged()
204 qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
206 lblScaling->setText(i18n("Scaling"));
207 sliderScaling->setInvertedAppearance(true);
208 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
212 enableSliderScaling(true);
213 enableSliderColor(true);
214 enableCbVariant(false);
215 sliderColor->setRange(0,255);
216 sliderColor->setPageStep(128);
217 lblSliderName->setText(i18n("Y value"));
218 lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
222 qDebug() << "Changing slider range.";
224 enableSliderScaling(true);
225 enableSliderColor(true);
226 enableCbVariant(false);
227 sliderColor->setMaximum(321);
228 sliderColor->setRange(0,179);
229 sliderColor->setPageStep(90);
230 lblSliderName->setText(i18n("UV angle"));
231 lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
234 enableSliderScaling(true);
235 enableSliderColor(false);
236 enableCbVariant(true);
237 sliderScaling->setRange(1,255);
238 sliderScaling->setValue(255);
239 cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
240 cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
241 cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
242 cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
244 case CPE_HSV_HUESHIFT:
245 enableSliderScaling(true);
246 enableSliderColor(true);
247 enableCbVariant(false);
248 sliderScaling->setRange(0,255);
249 sliderScaling->setValue(200);
250 sliderScaling->setInvertedAppearance(false);
251 sliderColor->setRange(0,255);
252 sliderColor->setValue(200);
253 lblSliderName->setText(i18n("HSV Saturation"));
254 lblScaling->setText(i18n("HSV Value"));
256 case CPE_HSV_SATURATION:
257 enableSliderScaling(false);
258 enableSliderColor(true);
259 sliderColor->setRange(0, 255);
260 sliderColor->setValue(200);
261 lblSliderName->setText(i18n("HSV Value"));
264 enableSliderScaling(false);
265 enableSliderColor(false);
266 enableCbVariant(true);
267 cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
268 cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
269 cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
270 cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
274 slotUpdateDisplays();