1 /***************************************************************************
2                           kdeapp.cpp  -  description
3                              -------------------
4     begin                : Fri Jul 21 22:28:19 CEST 2006
5     copyright            : (C) 2002 by Christophe Teyssier
6     email                : chris@teyssier.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 
19 #include "videocapturedlg.h"
20 #include "celestia/celestiacore.h"
21 
22 #include <klocale.h>
23 #include <qcombobox.h>
24 #include <qcheckbox.h>
25 #include <qspinbox.h>
26 #include <qwidget.h>
27 #include <qlayout.h>
28 #include <qlabel.h>
29 #include <kmainwindow.h>
30 #include <kactioncollection.h>
31 #include <kurlrequester.h>
32 #include <kfiledialog.h>
33 #include <qurl.h>
34 #include <kmessagebox.h>
35 #include <krun.h>
36 #include <kstatusbar.h>
37 #include <kapp.h>
38 #include "kdeapp.h"
39 
VideoCaptureDlg(QWidget * p,const QString & dir)40 VideoCaptureDlg::VideoCaptureDlg(QWidget* p, const QString &dir):
41     accepted(false)
42 {
43     fileUrl->fileDialog()->setURL(dir);
44     KGlobal::config()->setGroup("Preferences");
45     if (KGlobal::config()->hasKey("CaptureVideoFrameRate")) {
46         frameRate->setValue(KGlobal::config()->readNumEntry("CaptureVideoFrameRate"));
47     }
48     if (KGlobal::config()->hasKey("CaptureVideoQuality")) {
49         videoQuality->setValue(KGlobal::config()->readNumEntry("CaptureVideoQuality"));
50     }
51     parent = static_cast<KdeApp*>(p);
52     statusBar = parent->statusBar();
53     statusBar->show();
54     kapp->processEvents();
55 
56     currentWidth = parent->getGlWidth();
57     currentHeight = parent->getGlHeight();
58     newAspectRatioSlot(0);
59 
60     statusBar->insertItem("", 4); // filename
61     char dim[30];
62     snprintf(dim, 30, "%d x %d", currentWidth, currentHeight);
63     statusBar->insertItem(dim, 1);
64     statusBar->insertItem(i18n("Duration: %1").arg("0:00"), 2, 1);
65     statusBar->insertItem(i18n("Size: %1 MB").arg("0"), 3, 1);
66     statusBar->insertItem(i18n("  Paused"), 0);
67 
68     connect(parent, SIGNAL(resized(int, int)), this, SLOT(newMainWindowSizeSlot(int, int)));
69 }
70 
~VideoCaptureDlg()71 VideoCaptureDlg::~VideoCaptureDlg()
72 {
73     statusBar->removeItem(0);
74     statusBar->removeItem(1);
75     statusBar->removeItem(2);
76     statusBar->removeItem(3);
77     statusBar->removeItem(4);
78     statusBar->hide();
79     if (result() == QDialog::Accepted) {
80         parent->layout()->setResizeMode(QLayout::Auto);
81         parent->setMaximumSize(32767, 32767);
82         static_cast<KMainWindow*>(parent)->actionCollection()->action("captureVideo")->setEnabled(true);
83         parent->resize(mainWindowInitialWidth, mainWindowInitialHeight);
84         if (playVideo->isChecked() && getFrameCount() > 0) KRun::runURL(fileUrl->url(), "application/ogg");
85     }
86 }
87 
newAspectRatioSlot(int idx)88 void VideoCaptureDlg::newAspectRatioSlot(int idx) {
89     imageSize->clear();
90     widths.clear();
91     heights.clear();
92     int d;
93     switch(idx) {
94     case 0: // currect window aspect ratio
95         imageSize->insertItem(i18n("Current size: %1 x %2").arg(currentWidth).arg(currentHeight));
96         widths.push_back(currentWidth);
97         heights.push_back(currentHeight);
98         break;
99     case 1: // 11:9
100         d = currentWidth * 9 / 11;
101         imageSize->insertItem(i18n("Current width: %1 x %2").arg(currentWidth).arg(d));
102         widths.push_back(currentWidth);
103         heights.push_back(d);
104 
105         d = currentHeight * 11 / 9;
106         imageSize->insertItem(i18n("Current height: %1 x %2").arg(d).arg(currentHeight));
107         widths.push_back(d);
108         heights.push_back(currentHeight);
109 
110         imageSize->insertItem("QCIF: 176 x 144");
111         widths.push_back(176);
112         heights.push_back(144);
113 
114         imageSize->insertItem("CIF: 352 x 288");
115         widths.push_back(352);
116         heights.push_back(288);
117 
118         imageSize->insertItem("4CIF: 704 x 576");
119         widths.push_back(704);
120         heights.push_back(576);
121 
122         imageSize->insertItem("9CIF: 1056 x 864");
123         widths.push_back(1056);
124         heights.push_back(864);
125 
126         imageSize->insertItem("16CIF: 1408 x 1152");
127         widths.push_back(1408);
128         heights.push_back(1152);
129 
130         break;
131     case 2: // 4:3
132         d = currentWidth * 3 / 4;
133         imageSize->insertItem(i18n("Current width: %1 x %2").arg(currentWidth).arg(d));
134         widths.push_back(currentWidth);
135         heights.push_back(d);
136 
137         d = currentHeight * 4 / 3;
138         imageSize->insertItem(i18n("Current height: %1 x %2").arg(d).arg(currentHeight));
139         widths.push_back(d);
140         heights.push_back(currentHeight);
141 
142         imageSize->insertItem("SQCIF: 128 x 96");
143         widths.push_back(128);
144         heights.push_back(96);
145 
146         imageSize->insertItem("QVGA: 320 x 240");
147         widths.push_back(320);
148         heights.push_back(240);
149 
150         imageSize->insertItem("VGA/NTSC: 640 x 480");
151         widths.push_back(640);
152         heights.push_back(480);
153 
154         imageSize->insertItem("PAL: 768 x 576");
155         widths.push_back(768);
156         heights.push_back(576);
157 
158         imageSize->insertItem("SVGA: 800 x 600");
159         widths.push_back(800);
160         heights.push_back(600);
161 
162         imageSize->insertItem("XGA: 1024 x 768");
163         widths.push_back(1024);
164         heights.push_back(768);
165 
166         imageSize->insertItem("1280 x 960");
167         widths.push_back(1280);
168         heights.push_back(960);
169 
170         imageSize->insertItem("SXGA+: 1400 x 1050");
171         widths.push_back(1400);
172         heights.push_back(1050);
173 
174         imageSize->insertItem("UXGA: 1600 x 1200");
175         widths.push_back(1600);
176         heights.push_back(1200);
177 
178         imageSize->insertItem("QXGA: 2048 x 1536");
179         widths.push_back(2048);
180         heights.push_back(1536);
181 
182         break;
183     case 3: // 16:9
184         d = currentWidth * 9 / 16;
185         imageSize->insertItem(i18n("Current width: %1 x %2").arg(currentWidth).arg(d));
186         widths.push_back(currentWidth);
187         heights.push_back(d);
188 
189         d = currentHeight * 16 / 9;
190         imageSize->insertItem(i18n("Current height: %1 x %2").arg(d).arg(currentHeight));
191         widths.push_back(d);
192         heights.push_back(currentHeight);
193 
194         imageSize->insertItem("WVGA/NTSC: 854 x 480");
195         widths.push_back(854);
196         heights.push_back(480);
197 
198         imageSize->insertItem("PAL: 1024 x 576");
199         widths.push_back(1024);
200         heights.push_back(576);
201 
202         imageSize->insertItem("HD-720: 1280 x 720");
203         widths.push_back(1280);
204         heights.push_back(720);
205 
206         imageSize->insertItem("HD-1080: 1920 x 1080");
207         widths.push_back(1920);
208         heights.push_back(1080);
209 
210         break;
211     default:
212         break;
213     }
214 }
215 
getWidth() const216 int VideoCaptureDlg::getWidth() const {
217     return widths[imageSize->currentItem()];
218 }
219 
getHeight() const220 int VideoCaptureDlg::getHeight() const {
221     return heights[imageSize->currentItem()];
222 }
223 
getDir() const224 QString VideoCaptureDlg::getDir() const {
225     QUrl file(fileUrl->url());
226     return file.dirPath();
227 }
228 
okSlot()229 void VideoCaptureDlg::okSlot() {
230     accepted = true;
231 
232     if (fileUrl->url() == "") {
233         KMessageBox::queuedMessageBox(this, KMessageBox::Error, i18n("You must specify a file name."));
234         return;
235     }
236     mainWindowInitialWidth = parent->width();
237     mainWindowInitialHeight = parent->height();
238 
239     parent->layout()->setResizeMode(QLayout::FreeResize);
240     parent->setFixedSize(getWidth() + parent->width() - parent->getGlWidth(), getHeight() + parent->height() - parent->getGlHeight());
241     kapp->processEvents();
242 
243     parent->setFixedSize(getWidth() + parent->width() - parent->getGlWidth(), getHeight() + parent->height() - parent->getGlHeight());
244     kapp->processEvents();
245 
246     KGlobal::config()->setGroup("Preferences");
247     KGlobal::config()->writeEntry("CaptureVideoFrameRate", frameRate->value());
248     KGlobal::config()->writeEntry("CaptureVideoQuality", videoQuality->value());
249 
250     setAspectRatio(1, 1);
251     setQuality(videoQuality->value());
252     bool success = start(fileUrl->url().latin1(), getWidth(), getHeight(), frameRate->value());
253     char dim[30];
254     snprintf(dim, 30, "%d x %d", getWidth(), getHeight());
255     statusBar->changeItem(dim, 1);
256 
257     if (success) {
258         accept();
259     } else {
260         KMessageBox::queuedMessageBox(parent, KMessageBox::Error, i18n("Error initializing movie capture."));
261         reject();
262     }
263 }
264 
cancelSlot()265 void VideoCaptureDlg::cancelSlot() {
266     reject();
267 }
268 
frameCaptured()269 void VideoCaptureDlg::frameCaptured() {
270     char duration[30];
271     if (getFrameCount() % int(getFrameRate()) == 0) {
272         float sec = getFrameCount() / getFrameRate();
273         int min = (int) (sec / 60);
274         sec -= min * 60.0f;
275         snprintf(duration, 30, "%3d:%02d", min, (int)sec);
276         statusBar->changeItem(i18n("Duration: %1").arg(duration), 2);
277         float mb_out = getBytesOut() / 1024 / 1024;
278         statusBar->changeItem(i18n("Size: %1 MB").arg(mb_out), 3);
279     }
280 }
281 
recordingStatus(bool started)282 void VideoCaptureDlg::recordingStatus(bool started) {
283     statusBar->changeItem(started?i18n("  Recording"):i18n("  Paused"), 0);
284 }
285 
filenameSlot(const QString & name)286 void VideoCaptureDlg::filenameSlot(const QString& name) {
287     statusBar->changeItem(name, 4);
288 }
289 
newMainWindowSizeSlot(int,int)290 void VideoCaptureDlg::newMainWindowSizeSlot(int, int) {
291     if (accepted) return;
292 
293     currentWidth = parent->getGlWidth();
294     currentHeight = parent->getGlHeight();
295     char dim[30];
296     snprintf(dim, 30, "%d x %d", currentWidth, currentHeight);
297     statusBar->changeItem(dim, 1);
298 
299     newAspectRatioSlot(imageSize->currentItem());
300 }
301