]> git.sesse.net Git - kdenlive/blob - src/colorplaneexport.cpp
Improve the export audio automatic setting:
[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     lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
100
101     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
102     case CPE_YUV_Y:
103         lblColNr->setText(i18n("%1°", QString::number(sliderColor->value())));
104         break;
105     default:
106         lblColNr->setText(QString::number(sliderColor->value()));
107         break;
108     }
109
110     lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
111 }
112
113 void ColorPlaneExport::slotValidate()
114 {
115     bool ok;
116     int nr;
117
118     nr = QVariant(tResX->text()).toInt(&ok);
119     ok = ok && nr > 0;
120     if (ok) {
121         nr = QVariant(tResY->text()).toInt(&ok);
122         ok = ok && nr > 0;
123     }
124     if (ok) {
125         ok = kurlrequester->text().trimmed().length() > 0;
126         qDebug() << "File given: " << ok;
127     }
128
129     if (ok) {
130         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
131     } else {
132         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
133     }
134
135     slotUpdateDisplays();
136 }
137
138 void ColorPlaneExport::slotExportPlane()
139 {
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"));
146         }
147     }
148     QImage img;
149     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
150     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
151     case CPE_YUV:
152         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
153         break;
154     case CPE_YUV_Y:
155         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
156         break;
157     case CPE_YUV_MOD:
158         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
159         break;
160     case CPE_RGB_CURVE:
161         img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()));
162         break;
163     case CPE_YPbPr:
164         img = m_colorTools->yPbPrColorWheel(size, sliderColor->value(), m_scaling, false);
165         break;
166     }
167     img.save(kurlrequester->text());
168 }
169
170 void ColorPlaneExport::slotColormodeChanged()
171 {
172     qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
173     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
174     case CPE_YUV:
175     case CPE_YUV_MOD:
176     case CPE_YPbPr:
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."));
184         break;
185     case CPE_YUV_Y:
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."));
195         break;
196     case CPE_RGB_CURVE:
197         // deliberately fall through
198     default:
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));
205         break;
206     }
207     this->update();
208     slotUpdateDisplays();
209 }