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(CPE_YUV));
29 cbColorspace->addItem(i18n("YUV Y plane"), QVariant(CPE_YUV_Y));
30 cbColorspace->addItem(i18n("Modified YUV (Chroma)"), QVariant(CPE_YUV_MOD));
31 cbColorspace->addItem(i18n("RGB plane, one component varying"), QVariant(CPE_RGB_CURVE));
33 sliderColor->setSliderPosition(128);
38 sliderScaling->setInvertedAppearance(true);
39 sliderScaling->setRange(0, 80);
40 sliderScaling->setSliderPosition(50);
42 connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotExportPlane()));
43 connect(tResX, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
44 connect(tResY, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
45 connect(kurlrequester, SIGNAL(textChanged(QString)), this, SLOT(slotValidate()));
46 connect(sliderColor, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
47 connect(sliderScaling, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateDisplays()));
48 connect(cbColorspace, SIGNAL(currentIndexChanged(int)), this, SLOT(slotColormodeChanged()));
50 kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
52 slotColormodeChanged();
56 ColorPlaneExport::~ColorPlaneExport()
63 ///// Helper functions /////
65 void ColorPlaneExport::enableSliderScaling(const bool &enable)
67 sliderScaling->setEnabled(enable);
68 lblScaling->setEnabled(enable);
69 lblScaleNr->setEnabled(enable);
72 void ColorPlaneExport::enableSliderColor(const bool &enable)
74 sliderColor->setEnabled(enable);
75 lblSliderName->setEnabled(enable);
76 lblColNr->setEnabled(enable);
79 void ColorPlaneExport::enableCbVariant(const bool &enable)
81 cbVariant->setEnabled(enable);
82 lblVariant->setEnabled(enable);
84 while (cbVariant->count() > 0) {
85 cbVariant->removeItem(0);
94 void ColorPlaneExport::slotUpdateDisplays()
96 m_scaling = 1 - (float)sliderScaling->value()/100;
98 lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
100 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
102 lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
105 lblColNr->setText(QString::number(sliderColor->value()));
109 lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
112 void ColorPlaneExport::slotValidate()
117 nr = QVariant(tResX->text()).toInt(&ok);
120 nr = QVariant(tResY->text()).toInt(&ok);
124 ok = kurlrequester->text().trimmed().length() > 0;
125 qDebug() << "File given: " << ok;
129 buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
131 buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
134 slotUpdateDisplays();
137 void ColorPlaneExport::slotExportPlane()
139 qDebug() << "Exporting plane now to " << kurlrequester->text();
140 QString lower = kurlrequester->text().toLower();
141 qDebug() << "Lower: " << lower;
142 if (!lower.endsWith(".png") && !lower.endsWith(".jpg") && !lower.endsWith(".tif") && !lower.endsWith(".tiff")) {
143 if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", EXTENSION_PNG)) == KMessageBox::Yes) {
144 kurlrequester->setUrl(KUrl(kurlrequester->text() + ".png"));
148 QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
149 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
151 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
154 img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
157 img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
160 img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()));
163 img.save(kurlrequester->text());
166 void ColorPlaneExport::slotColormodeChanged()
168 qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
169 switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
172 enableSliderScaling(true);
173 enableSliderColor(true);
174 enableCbVariant(false);
175 sliderColor->setRange(0,255);
176 sliderColor->setPageStep(128);
177 lblSliderName->setText(i18n("Y value"));
178 lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
181 qDebug() << "Changing slider range.";
182 enableSliderScaling(true);
183 enableSliderColor(true);
184 enableCbVariant(false);
185 sliderColor->setMaximum(321);
186 sliderColor->setRange(0,179);
187 sliderColor->setPageStep(90);
188 lblSliderName->setText(i18n("UV angle"));
189 lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
192 // deliberately fall through
194 enableSliderScaling(false);
195 enableSliderColor(false);
196 enableCbVariant(true);
197 cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
198 cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
199 cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
203 slotUpdateDisplays();