]> git.sesse.net Git - kdenlive/blob - src/colorplaneexport.cpp
Fix label
[kdenlive] / src / colorplaneexport.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
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  ***************************************************************************/
10
11 #include "colorplaneexport.h"
12 #include <KMessageBox>
13
14 //#define DEBUG_CTE
15 #ifdef DEBUG_CTE
16 #include <QDebug>
17 #endif
18
19 const QString EXTENSION_PNG = ".png";
20 const int SCALE_RANGE = 80;
21
22 ColorPlaneExport::ColorPlaneExport(QWidget *parent) :
23     QDialog(parent)
24 {
25     setupUi(this);
26
27     m_colorTools = new ColorTools();
28
29     tResX->setText("800");
30     tResY->setText("800");
31
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));
39
40     sliderColor->setSliderPosition(128);
41
42     // 0  -> 1
43     // 50 -> 0.5
44     // 80 -> 0.2
45     sliderScaling->setInvertedAppearance(true);
46     sliderScaling->setRange(0, 80);
47     sliderScaling->setSliderPosition(50);
48
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()));
56
57     kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
58
59     slotColormodeChanged();
60     slotValidate();
61 }
62
63 ColorPlaneExport::~ColorPlaneExport()
64 {
65     delete m_colorTools;
66 }
67
68
69
70 ///// Helper functions /////
71
72 void ColorPlaneExport::enableSliderScaling(bool enable)
73 {
74     sliderScaling->setEnabled(enable);
75     lblScaling->setEnabled(enable);
76     lblScaleNr->setEnabled(enable);
77 }
78
79 void ColorPlaneExport::enableSliderColor(bool enable)
80 {
81     sliderColor->setEnabled(enable);
82     lblSliderName->setEnabled(enable);
83     lblColNr->setEnabled(enable);
84 }
85
86 void ColorPlaneExport::enableCbVariant(bool enable)
87 {
88    cbVariant->setEnabled(enable);
89    lblVariant->setEnabled(enable);
90    if (!enable) {
91        while (cbVariant->count() > 0) {
92            cbVariant->removeItem(0);
93        }
94    }
95 }
96
97
98
99 ///// Slots /////
100
101 void ColorPlaneExport::slotUpdateDisplays()
102 {
103     m_scaling = 1 - (float)sliderScaling->value()/100;
104
105     switch(cbColorspace->itemData(cbColorspace->currentIndex()).toInt()){
106     case CPE_RGB_CURVE:
107         lblScaleNr->setText(QChar(0xb1) + QString::number(sliderScaling->value(), 'f', 2));;
108         break;
109     case CPE_HSV_HUESHIFT:
110         lblScaleNr->setText(QString::number(sliderScaling->value()));
111         break;
112     default:
113         lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
114         break;
115     }
116
117     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
118     case CPE_YUV_Y:
119         lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
120         break;
121     default:
122         lblColNr->setText(QString::number(sliderColor->value()));
123         break;
124     }
125
126     lblSize->setText(i18n("%1 px", tResX->text().toInt()*tResY->text().toInt()));
127 }
128
129 void ColorPlaneExport::slotValidate()
130 {
131     bool ok;
132     int nr;
133
134     nr = QVariant(tResX->text()).toInt(&ok);
135     ok = ok && nr > 0;
136     if (ok) {
137         nr = QVariant(tResY->text()).toInt(&ok);
138         ok = ok && nr > 0;
139     }
140     if (ok) {
141         ok = kurlrequester->text().trimmed().length() > 0;
142 #ifdef DEBUG_CPE
143         qDebug() << "File given: " << ok;
144 #endif
145     }
146
147     if (ok) {
148         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
149     } else {
150         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
151     }
152
153     slotUpdateDisplays();
154 }
155
156 void ColorPlaneExport::slotExportPlane()
157 {
158 #ifdef DEBUG_CPE
159     qDebug() << "Exporting plane now to " <<  kurlrequester->text();
160 #endif
161     QString lower = kurlrequester->text().toLower();
162 #ifdef DEBUG_CPE
163     qDebug() << "Lower: " << lower;
164 #endif
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"));
168         }
169     }
170     QImage img;
171     QColor col;
172     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
173     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
174     case CPE_YUV:
175         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
176         break;
177     case CPE_YUV_Y:
178         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
179         break;
180     case CPE_YUV_MOD:
181         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
182         break;
183     case CPE_RGB_CURVE:
184         img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()),
185                                           (double)sliderScaling->value()/255);
186         break;
187     case CPE_YPbPr:
188         img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
189         break;
190     case CPE_HSV_HUESHIFT:
191         img = m_colorTools->hsvHueShiftPlane(size, sliderColor->value(), sliderScaling->value(), -180, 180);
192         break;
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);
196         break;
197     default:
198         Q_ASSERT(false);
199     }
200     img.save(kurlrequester->text());
201 }
202
203 void ColorPlaneExport::slotColormodeChanged()
204 {
205 #ifdef DEBUG_CPE
206     qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
207 #endif
208     lblScaling->setText(i18n("Scaling"));
209     sliderScaling->setInvertedAppearance(true);
210     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
211     case CPE_YUV:
212     case CPE_YUV_MOD:
213     case CPE_YPbPr:
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."));
221         break;
222     case CPE_YUV_Y:
223 #ifdef DEBUG_CPE
224         qDebug() << "Changing slider range.";
225 #endif
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."));
234         break;
235     case CPE_RGB_CURVE:
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));
245         break;
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"));
257         break;
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"));
264         break;
265     default:
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));
273         break;
274     }
275     this->update();
276     slotUpdateDisplays();
277 }
278
279 #include "colorplaneexport.moc"