]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
More work on title widget:
[kdenlive] / src / titlewidget.cpp
1 /***************************************************************************
2                           titlewidget.h  -  description
3                              -------------------
4     begin                : Feb 28 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
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
20 #include <QGraphicsView>
21 #include <QDomDocument>
22 #include <QGraphicsItem>
23 #include <QGraphicsSvgItem>
24 #include <QTextDocumentFragment>
25 #include <QTimer>
26
27 #include <KDebug>
28 #include <KGlobalSettings>
29 #include <KFileDialog>
30
31 #include "titlewidget.h"
32 #include "kdenlivesettings.h"
33
34 int settingUp = false;
35
36 TitleWidget::TitleWidget(Render *render, QWidget *parent): QDialog(parent) {
37     setupUi(this);
38     frame_properties->setFont(KGlobalSettings::toolBarFont());
39     toolBox->setFont(KGlobalSettings::toolBarFont());
40     frame_properties->setEnabled(false);
41     connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
42     connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
43     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
44     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
45     connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
46     //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
47     connect(loadButton, SIGNAL(clicked()), this, SLOT(loadTitle())) ;
48     connect(saveButton, SIGNAL(clicked()), this, SLOT(saveTitle())) ;
49
50     ktextedit->setHidden(true);
51     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
52     connect(font_family, SIGNAL(currentFontChanged (const QFont &)), this, SLOT(slotUpdateText())) ;
53     connect(font_size, SIGNAL(valueChanged (int)), this, SLOT(slotUpdateText())) ;
54     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
55     //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
56
57     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
58     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
59     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
60     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
61     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
62
63     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
64     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
65     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
66     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
67     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
68     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
69
70     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
71     connect(svgfilename, SIGNAL(urlSelected(const KUrl&)), this, SLOT(svgSelected(const KUrl &)));
72     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
73     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
74
75     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
76     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
77     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
78     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
79     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
80     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
81
82     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
83     buttonRealSize->setIcon(KIcon("zoom-original"));
84     m_scene = new GraphicsSceneRectMove(this);
85
86     // a gradient background
87     QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
88     gradient->setSpread(QGradient::ReflectSpread);
89     //scene->setBackgroundBrush(*gradient);
90
91
92     graphicsView->setScene(m_scene);
93     m_titledocument.setScene(m_scene);
94     connect(graphicsView->scene(), SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
95     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
96     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
97     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
98
99
100     QPen framepen(Qt::DotLine);
101     framepen.setColor(Qt::red);
102     m_frameWidth = render->renderWidth();
103     m_frameHeight = render->renderHeight();
104     QPixmap bg = render->extractFrame((int) render->seekPosition().frames(render->fps()), m_frameWidth, m_frameHeight);
105
106     QGraphicsPixmapItem *fb = new QGraphicsPixmapItem(bg);
107     fb->setZValue(-1000);
108     fb->setFlags(QGraphicsItem::ItemClipsToShape);
109     graphicsView->scene()->addItem(fb);
110
111     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
112     m_frameBorder->setPen(framepen);
113     m_frameBorder->setZValue(-1000);
114     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
115     graphicsView->scene()->addItem(m_frameBorder);
116
117     initViewports();
118
119
120
121     graphicsView->show();
122     graphicsView->setRenderHint(QPainter::Antialiasing);
123     graphicsView->setInteractive(true);
124     QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
125     //graphicsView->resize(400, 300);
126     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
127     toolBox->setItemEnabled(2, false);
128     toolBox->setItemEnabled(3, false);
129 }
130
131
132 //virtual
133 void TitleWidget::resizeEvent(QResizeEvent * event) {
134     //slotAdjustZoom();
135 }
136
137 void TitleWidget::initViewports() {
138     startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
139     endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
140
141     QPen startpen(Qt::DotLine);
142     QPen endpen(Qt::DashDotLine);
143     startpen.setColor(QColor(100, 200, 100, 140));
144     endpen.setColor(QColor(200, 100, 100, 140));
145
146     startViewport->setPen(startpen);
147     endViewport->setPen(endpen);
148
149     startViewportSize->setValue(40);
150     endViewportSize->setValue(40);
151
152     startViewport->setZValue(-1000);
153     endViewport->setZValue(-1000);
154
155     startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
156     endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
157
158     graphicsView->scene()->addItem(startViewport);
159     graphicsView->scene()->addItem(endViewport);
160 }
161
162 void TitleWidget::slotUpdateZoom(int pos) {
163     m_scene->setZoom((double) pos / 7);
164     zoom_label->setText("x" + QString::number((double) pos / 7, 'g', 2));
165 }
166
167 void TitleWidget::slotZoom(bool up) {
168     int pos = zoom_slider->value();
169     if (up) pos++;
170     else pos--;
171     zoom_slider->setValue(pos);
172 }
173
174 void TitleWidget::slotAdjustZoom() {
175     double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
176     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
177     if (scalex > scaley) scalex = scaley;
178     int zoompos = (int)(scalex * 7 + 0.5);
179     zoom_slider->setValue(zoompos);
180     graphicsView->centerOn(m_frameBorder);
181 }
182
183 void TitleWidget::slotZoomOneToOne() {
184     zoom_slider->setValue(7);
185     graphicsView->centerOn(m_frameBorder);
186 }
187
188 void TitleWidget::slotNewRect() {
189     QGraphicsRectItem * ri = graphicsView->scene()->addRect(0, 0, 100, 100);
190     ri->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
191     ri->setPos(m_frameWidth / 2, m_frameHeight / 2);
192     QColor f = rectFColor->color();
193     f.setAlpha(rectFAlpha->value());
194     QPen penf(f);
195     penf.setWidth(rectLineWidth->value());
196     ri->setPen(penf);
197     QColor b = rectBColor->color();
198     b.setAlpha(rectBAlpha->value());
199     ri->setBrush(QBrush(b));
200 }
201
202 void TitleWidget::slotNewText() {
203     QGraphicsTextItem *tt = graphicsView->scene()->addText("Text here");
204     tt->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
205     tt->setTextInteractionFlags(Qt::NoTextInteraction);
206     tt->setPos(m_frameWidth / 2, m_frameHeight / 2);
207     QFont font = font_family->currentFont();
208     font.setPointSize (font_size->value());
209     tt->setFont(font);
210     connect(tt->document(), SIGNAL(contentsChanged()), this, SLOT(selectionChanged()));
211     kDebug() << tt->metaObject()->className();
212     /*QGraphicsRectItem * ri=graphicsView->scene()->addRect(-50,-50,100,100);
213     ri->setFlags(QGraphicsItem::ItemIsMovable|QGraphicsItem::ItemIsSelectable);*/
214
215 }
216
217 void TitleWidget::zIndexChanged(int v) {
218     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
219     if (l.size() >= 1) {
220         l[0]->setZValue(v);
221     }
222 }
223
224 void TitleWidget::selectionChanged() {
225     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
226     toolBox->setItemEnabled(2, false);
227     toolBox->setItemEnabled(3, false);
228     value_x->blockSignals(true);
229     value_y->blockSignals(true);
230     value_w->blockSignals(true);
231     value_h->blockSignals(true);
232     if (l.size() == 1) {
233         if ((l[0])->type() == 8) {
234             QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
235             if (l[0]->hasFocus())
236                 ktextedit->setHtml(i->toHtml());
237             toolBox->setCurrentIndex(2);
238             toolBox->setItemEnabled(2, true);
239             value_x->setValue((int) i->pos().x());
240             value_y->setValue((int) i->pos().y());
241             value_w->setValue((int) i->boundingRect().width());
242             value_h->setValue((int) i->boundingRect().height());
243             frame_properties->setEnabled(true);
244             value_w->setEnabled(false);
245             value_h->setEnabled(false);
246         } else
247             if ((l[0])->type() == 3) {
248                 settingUp = true;
249                 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
250                 toolBox->setCurrentIndex(3);
251                 toolBox->setItemEnabled(3, true);
252                 rectFAlpha->setValue(rec->pen().color().alpha());
253                 rectBAlpha->setValue(rec->brush().color().alpha());
254                 kDebug() << rec->brush().color().alpha();
255                 QColor fcol = rec->pen().color();
256                 QColor bcol = rec->brush().color();
257                 //fcol.setAlpha(255);
258                 //bcol.setAlpha(255);
259                 rectFColor->setColor(fcol);
260                 rectBColor->setColor(bcol);
261                 settingUp = false;
262                 rectLineWidth->setValue(rec->pen().width());
263                 value_x->setValue((int) rec->pos().x());
264                 value_y->setValue((int) rec->pos().y());
265                 value_w->setValue((int) rec->rect().width());
266                 value_h->setValue((int) rec->rect().height());
267                 frame_properties->setEnabled(true);
268                 value_w->setEnabled(true);
269                 value_h->setEnabled(true);
270             } else {
271                 //toolBox->setCurrentIndex(0);
272                 frame_properties->setEnabled(false);
273             }
274         zValue->setValue((int)l[0]->zValue());
275         itemzoom->setValue((int)transformations[l[0]].scalex*100);
276         itemrotate->setValue((int)transformations[l[0]].rotate);
277         value_x->blockSignals(false);
278         value_y->blockSignals(false);
279         value_w->blockSignals(false);
280         value_h->blockSignals(false);
281     }
282 }
283
284 void TitleWidget::slotAdjustSelectedItem() {
285     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
286     if (l.size() >= 1) {
287         if (l[0]->type() == 3) {
288             //rect item
289             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
290             rec->setPos(value_x->value(), value_y->value());
291             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
292         } else if (l[0]->type() == 8) {
293             //text item
294             QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
295             rec->setPos(value_x->value(), value_y->value());
296         }
297     }
298 }
299
300 void TitleWidget::slotChangeBackground() {
301     QColor color = kcolorbutton->color();
302     color.setAlpha(horizontalSlider->value());
303     m_frameBorder->setBrush(QBrush(color));
304 }
305
306 void TitleWidget::textChanged() {
307     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
308     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
309         kDebug() << ktextedit->document()->toHtml();
310         ((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
311     }
312 }
313
314 void TitleWidget::slotUpdateText() {
315     QFont font = font_family->currentFont();
316     font.setPointSize (font_size->value());
317     QColor color = fontColorButton->color();
318     color.setAlpha(textAlpha->value());
319
320     QGraphicsTextItem* item = NULL;
321     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
322     if (l.size() == 1 && (l[0])->type() == 8) {
323         item = (QGraphicsTextItem*)l[0];
324     }
325     if (!item) return;
326     if (item->textCursor().selection ().isEmpty()) 
327     {
328         item->setFont(font);
329         item->setDefaultTextColor(color);
330     }
331     /*else {
332         QTextDocumentFragment selec = item->textCursor().selection ();
333         selec.set
334     }*/
335     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
336
337     //ktextedit->setCurrentFont(font);
338     //ktextedit->setTextColor(color);
339     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
340     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
341         QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
342         //item-
343     }*/
344 }
345
346 void TitleWidget::rectChanged() {
347     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
348     if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
349         QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
350         QColor f = rectFColor->color();
351         f.setAlpha(rectFAlpha->value());
352         QPen penf(f);
353         penf.setWidth(rectLineWidth->value());
354         rec->setPen(penf);
355         QColor b = rectBColor->color();
356         b.setAlpha(rectBAlpha->value());
357         rec->setBrush(QBrush(b));
358     }
359 }
360
361 void TitleWidget::fontBold() {
362     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
363     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
364         //ktextedit->document()->setTextOption();
365     }
366 }
367
368 void TitleWidget::svgSelected(const KUrl& u) {
369     QGraphicsSvgItem *svg = new QGraphicsSvgItem(u.toLocalFile());
370     svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
371     graphicsView->scene()->addItem(svg);
372 }
373
374 void TitleWidget::itemScaled(int val) {
375     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
376     if (l.size() == 1) {
377         Transform x = transformations[l[0]];
378         x.scalex = (double)val / 100.0;
379         x.scaley = (double)val / 100.0;
380         QTransform qtrans;
381         qtrans.scale(x.scalex, x.scaley);
382         qtrans.rotate(x.rotate);
383         l[0]->setTransform(qtrans);
384         transformations[l[0]] = x;
385     }
386 }
387
388 void TitleWidget::itemRotate(int val) {
389     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
390     if (l.size() == 1) {
391         Transform x = transformations[l[0]];
392         x.rotate = (double)val;
393         QTransform qtrans;
394         qtrans.scale(x.scalex, x.scaley);
395         qtrans.rotate(x.rotate);
396         l[0]->setTransform(qtrans);
397         transformations[l[0]] = x;
398     }
399 }
400
401
402 void TitleWidget::setupViewports() {
403     double aspect_ratio = 4.0 / 3.0;//read from project
404
405     QRectF sp(0, 0, 0, 0);
406     QRectF ep(0, 0, 0, 0);
407
408     double sv_size = startViewportSize->value();
409     double ev_size = endViewportSize->value();
410     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
411     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
412
413     startViewport->setPos(startViewportX->value(), startViewportY->value());
414     endViewport->setPos(endViewportX->value(), endViewportY->value());
415
416     startViewport->setPolygon(QPolygonF(sp));
417     endViewport->setPolygon(QPolygonF(ep));
418
419 }
420
421 void TitleWidget::loadTitle() {
422     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.kdenlivetitle", this, tr("Save Title"));
423     m_titledocument.loadDocument(url, startViewport, endViewport);
424 }
425
426 void TitleWidget::saveTitle() {
427     KUrl url = KFileDialog::getSaveUrl(KUrl(), "*.kdenlivetitle", this, tr("Save Title"));
428     m_titledocument.saveDocument(url, startViewport, endViewport);
429 }
430
431 #include "moc_titlewidget.cpp"
432