]> git.sesse.net Git - kdenlive/blob - src/colorplaneexport.cpp
Colorplane export changes:
[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(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));
32
33     sliderColor->setSliderPosition(128);
34
35     // 0  -> 1
36     // 50 -> 0.5
37     // 80 -> 0.2
38     sliderScaling->setInvertedAppearance(true);
39     sliderScaling->setRange(0, 80);
40     sliderScaling->setSliderPosition(50);
41
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()));
49
50     kurlrequester->setText("/tmp/yuv-plane.png");
51
52     slotColormodeChanged();
53     slotValidate();
54 }
55
56 ColorPlaneExport::~ColorPlaneExport()
57 {
58     delete m_colorTools;
59 }
60
61
62
63 ///// Helper functions /////
64
65 void ColorPlaneExport::enableSliderScaling(const bool &enable)
66 {
67     sliderScaling->setEnabled(enable);
68     lblScaling->setEnabled(enable);
69     lblScaleNr->setEnabled(enable);
70 }
71
72 void ColorPlaneExport::enableSliderColor(const bool &enable)
73 {
74     sliderColor->setEnabled(enable);
75     lblSliderName->setEnabled(enable);
76     lblColNr->setEnabled(enable);
77 }
78
79 void ColorPlaneExport::enableCbVariant(const bool &enable)
80 {
81    cbVariant->setEnabled(enable);
82    lblVariant->setEnabled(enable);
83    if (!enable) {
84        while (cbVariant->count() > 0) {
85            cbVariant->removeItem(0);
86        }
87    }
88 }
89
90
91
92 ///// Slots /////
93
94 void ColorPlaneExport::slotUpdateDisplays()
95 {
96     m_scaling = 1 - (float)sliderScaling->value()/100;
97
98     lblScaleNr->setText("0..." + QString::number(m_scaling, 'f', 2));
99
100     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
101     case CPE_YUV_Y:
102         lblColNr->setText(i18n("%1 °", QString::number(sliderColor->value())));
103         break;
104     default:
105         lblColNr->setText(QString::number(sliderColor->value()));
106         break;
107     }
108
109     lblSize->setText(i18n("%1 px", QVariant(tResX->text()).toInt()*QVariant(tResY->text()).toInt()));
110 }
111
112 void ColorPlaneExport::slotValidate()
113 {
114     bool ok;
115     int nr;
116
117     nr = QVariant(tResX->text()).toInt(&ok);
118     ok = ok && nr > 0;
119     if (ok) {
120         nr = QVariant(tResY->text()).toInt(&ok);
121         ok = ok && nr > 0;
122     }
123     if (ok) {
124         ok = kurlrequester->text().trimmed().length() > 0;
125         qDebug() << "File given: " << ok;
126     }
127
128     if (ok) {
129         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
130     } else {
131         buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
132     }
133
134     slotUpdateDisplays();
135 }
136
137 void ColorPlaneExport::slotExportPlane()
138 {
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->setText(kurlrequester->text() + ".png");
145         }
146     }
147     QImage img;
148     QSize size(QVariant(tResX->text()).toInt(), QVariant(tResY->text()).toInt());
149     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
150     case CPE_YUV:
151         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, false, false);
152         break;
153     case CPE_YUV_Y:
154         img = m_colorTools->yuvVerticalPlane(size, sliderColor->value(), m_scaling);
155         break;
156     case CPE_YUV_MOD:
157         img = m_colorTools->yuvColorWheel(size, sliderColor->value(), m_scaling, true, false);
158         break;
159     case CPE_RGB_CURVE:
160         img = m_colorTools->rgbCurvePlane(size, (ColorTools::ColorsRGB) (cbVariant->itemData(cbVariant->currentIndex()).toInt()));
161         break;
162     }
163     img.save(kurlrequester->text());
164 }
165
166 void ColorPlaneExport::slotColormodeChanged()
167 {
168     qDebug() << "Color mode changed to " << cbColorspace->itemData(cbColorspace->currentIndex()).toInt();
169     switch (cbColorspace->itemData(cbColorspace->currentIndex()).toInt()) {
170     case CPE_YUV:
171     case CPE_YUV_MOD:
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."));
179         break;
180     case CPE_YUV_Y:
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."));
190         break;
191     case CPE_RGB_CURVE:
192         // deliberately fall through
193     default:
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));
200         break;
201     }
202     this->update();
203     slotUpdateDisplays();
204 }