]> git.sesse.net Git - kdenlive/blob - src/colorplaneexport.cpp
Improve profile autodetection
[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 <QDebug>
13 #include <KMessageBox>
14
15 const QString EXTENSION_PNG = ".png";
16 const int SCALE_RANGE = 80;
17
18 ColorPlaneExport::ColorPlaneExport(QWidget *parent) :
19     QDialog(parent)
20 {
21     setupUi(this);
22
23     m_colorTools = new ColorTools();
24
25     tResX->setText("800");
26     tResY->setText("800");
27
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));
33
34     sliderColor->setSliderPosition(128);
35
36     // 0  -> 1
37     // 50 -> 0.5
38     // 80 -> 0.2
39     sliderScaling->setInvertedAppearance(true);
40     sliderScaling->setRange(0, 80);
41     sliderScaling->setSliderPosition(50);
42
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()));
50
51     kurlrequester->setUrl(KUrl("/tmp/yuv-plane.png"));
52
53     slotColormodeChanged();
54     slotValidate();
55 }
56
57 ColorPlaneExport::~ColorPlaneExport()
58 {
59     delete m_colorTools;
60 }
61
62
63
64 ///// Helper functions /////
65
66 void ColorPlaneExport::enableSliderScaling(const bool &enable)
67 {
68     sliderScaling->setEnabled(enable);
69     lblScaling->setEnabled(enable);
70     lblScaleNr->setEnabled(enable);
71 }
72
73 void ColorPlaneExport::enableSliderColor(const bool &enable)
74 {
75     sliderColor->setEnabled(enable);
76     lblSliderName->setEnabled(enable);
77     lblColNr->setEnabled(enable);
78 }
79
80 void ColorPlaneExport::enableCbVariant(const bool &enable)
81 {
82    cbVariant->setEnabled(enable);
83    lblVariant->setEnabled(enable);
84    if (!enable) {
85        while (cbVariant->count() > 0) {
86            cbVariant->removeItem(0);
87        }
88    }
89 }
90
91
92
93 ///// Slots /////
94
95 void ColorPlaneExport::slotUpdateDisplays()
96 {
97     m_scaling = 1 - (float)sliderScaling->value()/100;
98
99     switch(cbColorspace->itemData(cbColorspace->currentIndex()).toInt()){
100     case CPE_RGB_CURVE:
101         lblScaleNr->setText(QChar(0xb1) + QString::number(sliderScaling->value(), 'f', 2));;
102         break;
103     default:
104         lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
105         break;
106     }
107
108     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
109     case CPE_YUV_Y:
110         lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
111         break;
112     default:
113         lblColNr->setText(QString::number(sliderColor->value()));
114         break;
115     }
116
117     lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
118 }
119
120 void ColorPlaneExport::slotValidate()
121 {
122     bool ok;
123     int nr;
124
125     nr = QVariant(tResX->text()).toInt(&ok);
126     ok = ok && nr > 0;
127     if (ok) {
128         nr = QVariant(tResY->text()).toInt(&ok);
129         ok = ok && nr > 0;
130     }
131     if (ok) {
132         ok = kurlrequester->text().trimmed().length() > 0;
133         qDebug() << "File given: " << ok;
134     }
135
136     if (ok) {
137         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
138     } else {
139         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
140     }
141
142     slotUpdateDisplays();
143 }
144
145 void ColorPlaneExport::slotExportPlane()
146 {
147     qDebug() << "Exporting plane now to " <<  kurlrequester->text();
148     QString lower = kurlrequester->text().toLower();
149     qDebug() << "Lower: " << lower;
150     if (!lower.endsWith(".png") && !lower.endsWith(".jpg") && !lower.endsWith(".tif") && !lower.endsWith(".tiff")) {
151         if (KMessageBox::questionYesNo(this, i18n("File has no extension. Add extension (%1)?", EXTENSION_PNG)) == KMessageBox::Yes) {
152             kurlrequester->setUrl(KUrl(kurlrequester->text() + ".png"));
153         }
154     }
155     QImage img;
156     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
157     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
158     case CPE_YUV:
159         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
160         break;
161     case CPE_YUV_Y:
162         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
163         break;
164     case CPE_YUV_MOD:
165         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
166         break;
167     case CPE_RGB_CURVE:
168         img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()),
169                                           (double)sliderScaling->value()/255);
170         break;
171     case CPE_YPbPr:
172         img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
173         break;
174     }
175     img.save(kurlrequester->text());
176 }
177
178 void ColorPlaneExport::slotColormodeChanged()
179 {
180     qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
181     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
182     case CPE_YUV:
183     case CPE_YUV_MOD:
184     case CPE_YPbPr:
185         enableSliderScaling(true);
186         enableSliderColor(true);
187         enableCbVariant(false);
188         sliderColor->setRange(0,255);
189         sliderColor->setPageStep(128);
190         lblSliderName->setText(i18n("Y value"));
191         lblSliderName->setToolTip(i18n("The Y value describes the brightness of the colors."));
192         break;
193     case CPE_YUV_Y:
194         qDebug() << "Changing slider range.";
195         enableSliderScaling(true);
196         enableSliderColor(true);
197         enableCbVariant(false);
198         sliderColor->setMaximum(321);
199         sliderColor->setRange(0,179);
200         sliderColor->setPageStep(90);
201         lblSliderName->setText(i18n("UV angle"));
202         lblSliderName->setToolTip(i18n("Angle through the UV plane, with all possible Y values."));
203         break;
204     case CPE_RGB_CURVE:
205         enableSliderScaling(true);
206         enableSliderColor(false);
207         enableCbVariant(true);
208         sliderScaling->setRange(1,255);
209         sliderScaling->setValue(255);
210         cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
211         cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
212         cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
213         cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
214         break;
215     default:
216         enableSliderScaling(false);
217         enableSliderColor(false);
218         enableCbVariant(true);
219         cbVariant->addItem(i18n("Red"), QVariant(ColorTools::COL_R));
220         cbVariant->addItem(i18n("Green"), QVariant(ColorTools::COL_G));
221         cbVariant->addItem(i18n("Blue"), QVariant(ColorTools::COL_B));
222         cbVariant->addItem(i18n("Luma"), QVariant(ColorTools::COL_Luma));
223         break;
224     }
225     this->update();
226     slotUpdateDisplays();
227 }