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"
13 #include <KMessageBox>
15 const QString EXTENSION_PNG = ".png";
16 const int SCALE_RANGE = 80;
18 ColorPlaneExport::ColorPlaneExport(QWidget *parent) :
23 m_colorTools = new ColorTools();
25 tResX->setText("800");
26 tResY->setText("800");
28 cbColorspace->addItem(i18n("YUV UV plane"), QVariant(ColorPlaneExport::CPE_YUV));
29 cbColorspace->addItem(i18n("YUV Y plane"), QVariant(ColorPlaneExport::CPE_YUV_Y));
30 cbColorspace->addItem(i18n("Modified YUV (Chroma)"), QVariant(ColorPlaneExport::CPE_YUV_MOD));
31 cbColorspace->addItem(i18n("YCbCr CbCr plane"), QVariant(ColorPlaneExport::CPE_YPbPr));
32 cbColorspace->addItem(i18n("RGB plane, one component varying"), QVariant(ColorPlaneExport::CPE_RGB_CURVE));
34 sliderColor->setSliderPosition(128);
39 sliderScaling->setInvertedAppearance(true);
40 sliderScaling->setRange(0, 80);
41 sliderScaling->setSliderPosition(50);
43 connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExportPlane()));
44 connect(tResX, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
45 connect(tResY, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
46 connect(kurlrequester, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
47 connect(sliderColor, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
48 connect(sliderScaling, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
49 connect(cbColorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotColormodeChanged()));
51 kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
53 slotColormodeChanged();
57 ColorPlaneExport::~ColorPlaneExport()
64 ///// Helper functions /////
66 void ColorPlaneExport::enableSliderScaling(const bool &enable)
68 sliderScaling->setEnabled(enable);
69 lblScaling->setEnabled(enable);
70 lblScaleNr->setEnabled(enable);
73 void ColorPlaneExport::enableSliderColor(const bool &enable)
75 sliderColor->setEnabled(enable);
76 lblSliderName->setEnabled(enable);
77 lblColNr->setEnabled(enable);
80 void ColorPlaneExport::enableCbVariant(const bool &enable)
82 cbVariant->setEnabled(enable);
83 lblVariant->setEnabled(enable);
85 while (cbVariant->count() > 0) {
86 cbVariant->removeItem(0);
95 void ColorPlaneExport::slotUpdateDisplays()
97 m_scaling = 1 - (float)sliderScaling->value()/100;
99 lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
101 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
103 lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
106 lblColNr->setText(QString::number(sliderColor->value()));
110 lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
113 void ColorPlaneExport::slotValidate()
118 nr = QVariant(tResX->text()).toInt(&ok);
121 nr = QVariant(tResY->text()).toInt(&ok);
125 ok = kurlrequester->text().trimmed().length() > 0;
126 qDebug() << "File given: " << ok;
130 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
132 buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
135 slotUpdateDisplays();
138 void ColorPlaneExport::slotExportPlane()
140 qDebug() << "Exporting plane now to " << kurlrequester->text();
141 QString lower = kurlrequester->text().toLower();
142 qDebug() << "Lower: " << lower;
143 if (!lower.endsWith(".png") && !lower.endsWith(".jpg") && !lower.endsWith(".tif") && !lower.endsWith(".tiff")) {
144 if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", EXTENSION_PNG)) == KMessageBox::Yes) {
145 kurlrequester->setUrl(KUrl(kurlrequester->text() + ".png"));
149 QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
150 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
152 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
155 img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
158 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
161 img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()));
164 img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
167 img.save(kurlrequester->text());
170 void ColorPlaneExport::slotColormodeChanged()
172 qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
173 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
177 enableSliderScaling(true);
178 enableSliderColor(true);
179 enableCbVariant(false);
180 sliderColor->setRange(0,255);
181 sliderColor->setPageStep(128);
182 lblSliderName->setText(i18n("Y value"));
183 lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
186 qDebug() << "Changing slider range.";
187 enableSliderScaling(true);
188 enableSliderColor(true);
189 enableCbVariant(false);
190 sliderColor->setMaximum(321);
191 sliderColor->setRange(0,179);
192 sliderColor->setPageStep(90);
193 lblSliderName->setText(i18n("UV angle"));
194 lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
197 // deliberately fall through
199 enableSliderScaling(false);
200 enableSliderColor(false);
201 enableCbVariant(true);
202 cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
203 cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
204 cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
208 slotUpdateDisplays();