]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
correctly load / save text clips
[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 <QTimer>
25
26 #include <QToolBar>
27 #include <QMenu>
28
29 #include <KDebug>
30 #include <KGlobalSettings>
31 #include <KFileDialog>
32
33 #include "titlewidget.h"
34 #include "kdenlivesettings.h"
35
36 int settingUp = false;
37
38 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent): QDialog(parent), m_render(render), m_count(0), m_projectPath(projectPath) {
39     setupUi(this);
40     //frame_properties->
41     setFont(KGlobalSettings::toolBarFont());
42     //toolBox->setFont(KGlobalSettings::toolBarFont());
43     frame_properties->setEnabled(false);
44     m_frameWidth = render->renderWidth();
45     m_frameHeight = render->renderHeight();
46     //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
47     //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
48     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
49     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
50     //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
51     //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
52
53
54     //ktextedit->setHidden(true);
55     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
56     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
57     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
58     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
59     //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
60
61     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
62     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
63     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
64     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
65     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
66
67     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
68     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
69     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
70     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
71     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
72     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
73
74     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
75     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
76     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
77     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
78     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
79
80     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
81     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
82     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
83     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
84     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
85     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
86     connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
87     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
88     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
89     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
90
91     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
92     buttonRealSize->setIcon(KIcon("zoom-original"));
93     buttonBold->setIcon(KIcon("format-text-bold"));
94     buttonItalic->setIcon(KIcon("format-text-italic"));
95     buttonUnder->setIcon(KIcon("format-text-underline"));
96
97     QHBoxLayout *layout = new QHBoxLayout;
98     frame_toolbar->setLayout(layout);
99
100     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
101
102     m_buttonRect = m_toolbar->addAction(KIcon("insert-rect"), i18n("Add Rectangle"));
103     m_buttonRect->setCheckable(true);
104     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
105
106     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
107     m_buttonText->setCheckable(true);
108     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
109
110     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
111     m_buttonImage->setCheckable(false);
112     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
113
114     m_buttonCursor = m_toolbar->addAction(KIcon("select-rectangular"), i18n("Selection Tool"));
115     m_buttonCursor->setCheckable(true);
116     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
117
118     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
119     m_buttonLoad->setCheckable(false);
120     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
121
122     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
123     m_buttonSave->setCheckable(false);
124     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
125
126     layout->addWidget(m_toolbar);
127     text_properties->setHidden(true);
128
129     // initialize graphic scene
130     m_scene = new GraphicsSceneRectMove(this);
131     graphicsView->setScene(m_scene);
132     m_titledocument.setScene(m_scene);
133
134     // a gradient background
135     QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
136     gradient->setSpread(QGradient::ReflectSpread);
137     //scene->setBackgroundBrush(*gradient);
138
139     m_frameImage = new QGraphicsPixmapItem();
140     QTransform qtrans;
141     qtrans.scale(2.0, 2.0);
142     m_frameImage->setTransform(qtrans);
143     m_frameImage->setZValue(-1200);
144     m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
145     displayBackgroundFrame();
146     graphicsView->scene()->addItem(m_frameImage);
147
148     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
149     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
150     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
151     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
152     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
153     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
154     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
155     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
156
157     QPen framepen(Qt::DotLine);
158     framepen.setColor(Qt::red);
159
160     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
161     m_frameBorder->setPen(framepen);
162     m_frameBorder->setZValue(-1100);
163     m_frameBorder->setBrush(QColor(0, 0, 0, 0));
164     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
165     graphicsView->scene()->addItem(m_frameBorder);
166
167     initViewports();
168
169     graphicsView->show();
170     graphicsView->setRenderHint(QPainter::Antialiasing);
171     graphicsView->setInteractive(true);
172     QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
173     //graphicsView->resize(400, 300);
174     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
175     toolBox->setItemEnabled(2, false);
176     if (!url.isEmpty()) {
177         m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
178         slotSelectTool();
179     } else slotRectTool();
180 }
181
182
183 //virtual
184 void TitleWidget::resizeEvent(QResizeEvent * event) {
185     //slotAdjustZoom();
186 }
187
188 void TitleWidget::slotTextTool() {
189     rect_properties->setHidden(true);
190     text_properties->setHidden(false);
191     m_scene->setTool(TITLE_TEXT);
192     m_buttonRect->setChecked(false);
193     m_buttonCursor->setChecked(false);
194 }
195
196 void TitleWidget::slotRectTool() {
197     text_properties->setHidden(true);
198     rect_properties->setHidden(false);
199     m_scene->setTool(TITLE_RECTANGLE);
200     m_buttonText->setChecked(false);
201     m_buttonCursor->setChecked(false);
202     m_buttonRect->setChecked(true);
203 }
204
205 void TitleWidget::slotSelectTool() {
206     m_scene->setTool(TITLE_SELECT);
207     m_buttonCursor->setChecked(true);
208     m_buttonText->setChecked(false);
209     m_buttonRect->setChecked(false);
210 }
211
212 void TitleWidget::slotImageTool() {
213     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
214     if (!url.isEmpty()) {
215         if (url.path().endsWith(".svg")) {
216             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
217             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
218             svg->setZValue(m_count++);
219             svg->setData(Qt::UserRole, url.path());
220             graphicsView->scene()->addItem(svg);
221         } else {
222             QPixmap pix(url.path());
223             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
224             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
225             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
226             image->setData(Qt::UserRole, url.path());
227             image->setZValue(m_count++);
228             graphicsView->scene()->addItem(image);
229         }
230     }
231     m_scene->setTool(TITLE_SELECT);
232     m_buttonRect->setChecked(false);
233     m_buttonCursor->setChecked(true);
234     m_buttonText->setChecked(false);
235 }
236
237 void TitleWidget::displayBackgroundFrame() {
238     if (!displayBg->isChecked()) {
239         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
240         QPixmap pattern(20, 20);
241         pattern.fill();
242         QPainter p;
243         p.begin(&pattern);
244         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
245         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
246         p.end();
247         QBrush br(pattern);
248
249         p.begin(&bg);
250         p.fillRect(bg.rect(), br);
251         p.end();
252         m_frameImage->setPixmap(bg);
253     } else {
254         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
255     }
256 }
257
258 void TitleWidget::initViewports() {
259     startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
260     endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
261
262     QPen startpen(Qt::DotLine);
263     QPen endpen(Qt::DashDotLine);
264     startpen.setColor(QColor(100, 200, 100, 140));
265     endpen.setColor(QColor(200, 100, 100, 140));
266
267     startViewport->setPen(startpen);
268     endViewport->setPen(endpen);
269
270     startViewportSize->setValue(40);
271     endViewportSize->setValue(40);
272
273     startViewport->setZValue(-1000);
274     endViewport->setZValue(-1000);
275
276     startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
277     endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
278
279     graphicsView->scene()->addItem(startViewport);
280     graphicsView->scene()->addItem(endViewport);
281 }
282
283 void TitleWidget::slotUpdateZoom(int pos) {
284     m_scene->setZoom((double) pos / 7);
285     zoom_label->setText("x" + QString::number((double) pos / 7, 'g', 2));
286 }
287
288 void TitleWidget::slotZoom(bool up) {
289     int pos = zoom_slider->value();
290     if (up) pos++;
291     else pos--;
292     zoom_slider->setValue(pos);
293 }
294
295 void TitleWidget::slotAdjustZoom() {
296     double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
297     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
298     if (scalex > scaley) scalex = scaley;
299     int zoompos = (int)(scalex * 7 + 0.5);
300     zoom_slider->setValue(zoompos);
301     graphicsView->centerOn(m_frameBorder);
302 }
303
304 void TitleWidget::slotZoomOneToOne() {
305     zoom_slider->setValue(7);
306     graphicsView->centerOn(m_frameBorder);
307 }
308
309 void TitleWidget::slotNewRect(QGraphicsRectItem * rect) {
310     QColor f = rectFColor->color();
311     f.setAlpha(rectFAlpha->value());
312     QPen penf(f);
313     penf.setWidth(rectLineWidth->value());
314     rect->setPen(penf);
315     QColor b = rectBColor->color();
316     b.setAlpha(rectBAlpha->value());
317     rect->setBrush(QBrush(b));
318     rect->setZValue(m_count++);
319     //setCurrentItem(rect);
320     //graphicsView->setFocus();
321 }
322
323 void TitleWidget::slotNewText(QGraphicsTextItem *tt) {
324     QFont font = font_family->currentFont();
325     font.setPointSize(font_size->value());
326     tt->setFont(font);
327     QColor color = fontColorButton->color();
328     color.setAlpha(textAlpha->value());
329     tt->setDefaultTextColor(color);
330     tt->setZValue(m_count++);
331     setCurrentItem(tt);
332 }
333
334 void TitleWidget::setCurrentItem(QGraphicsItem *item) {
335     m_scene->setSelectedItem(item);
336 }
337
338 void TitleWidget::zIndexChanged(int v) {
339     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
340     if (l.size() >= 1) {
341         l[0]->setZValue(v);
342     }
343 }
344
345 void TitleWidget::selectionChanged() {
346     if (m_scene->tool() != TITLE_SELECT) return;
347     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
348     //toolBox->setItemEnabled(2, false);
349     //toolBox->setItemEnabled(3, false);
350     value_x->blockSignals(true);
351     value_y->blockSignals(true);
352     value_w->blockSignals(true);
353     value_h->blockSignals(true);
354     kDebug() << "////////  SELECTION CHANGED; ITEMS: " << l.size();
355     if (l.size() == 1) {
356         if ((l[0])->type() == 8) {
357             rect_properties->setHidden(true);
358             text_properties->setHidden(false);
359             QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
360             //if (l[0]->hasFocus())
361             //ktextedit->setHtml(i->toHtml());
362             toolBox->setCurrentIndex(0);
363             //toolBox->setItemEnabled(2, true);
364             font_size->blockSignals(true);
365             font_family->blockSignals(true);
366             buttonBold->blockSignals(true);
367             buttonItalic->blockSignals(true);
368             buttonUnder->blockSignals(true);
369             fontColorButton->blockSignals(true);
370             textAlpha->blockSignals(true);
371
372             QFont font = i->font();
373             font_family->setCurrentFont(font);
374             font_size->setValue(font.pointSize());
375             buttonBold->setChecked(font.bold());
376             buttonItalic->setChecked(font.italic());
377             buttonUnder->setChecked(font.underline());
378
379             QColor color = i->defaultTextColor();
380             fontColorButton->setColor(color);
381             textAlpha->setValue(color.alpha());
382
383             font_size->blockSignals(false);
384             font_family->blockSignals(false);
385             buttonBold->blockSignals(false);
386             buttonItalic->blockSignals(false);
387             buttonUnder->blockSignals(false);
388             fontColorButton->blockSignals(false);
389             textAlpha->blockSignals(false);
390
391             value_x->setValue((int) i->pos().x());
392             value_y->setValue((int) i->pos().y());
393             value_w->setValue((int) i->boundingRect().width());
394             value_h->setValue((int) i->boundingRect().height());
395             frame_properties->setEnabled(true);
396             value_w->setEnabled(false);
397             value_h->setEnabled(false);
398         } else if ((l[0])->type() == 3) {
399             rect_properties->setHidden(false);
400             text_properties->setHidden(true);
401             settingUp = true;
402             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
403             toolBox->setCurrentIndex(0);
404             //toolBox->setItemEnabled(3, true);
405             rectFAlpha->setValue(rec->pen().color().alpha());
406             rectBAlpha->setValue(rec->brush().color().alpha());
407             kDebug() << rec->brush().color().alpha();
408             QColor fcol = rec->pen().color();
409             QColor bcol = rec->brush().color();
410             //fcol.setAlpha(255);
411             //bcol.setAlpha(255);
412             rectFColor->setColor(fcol);
413             rectBColor->setColor(bcol);
414             settingUp = false;
415             rectLineWidth->setValue(rec->pen().width());
416             value_x->setValue((int) rec->pos().x());
417             value_y->setValue((int) rec->pos().y());
418             value_w->setValue((int) rec->rect().width());
419             value_h->setValue((int) rec->rect().height());
420             frame_properties->setEnabled(true);
421             value_w->setEnabled(true);
422             value_h->setEnabled(true);
423         } else {
424             //toolBox->setCurrentIndex(0);
425             frame_properties->setEnabled(false);
426         }
427         zValue->setValue((int)l[0]->zValue());
428         itemzoom->setValue((int)(transformations[l[0]].scalex * 100));
429         itemrotate->setValue((int)(transformations[l[0]].rotate));
430         value_x->blockSignals(false);
431         value_y->blockSignals(false);
432         value_w->blockSignals(false);
433         value_h->blockSignals(false);
434     } else frame_properties->setEnabled(false);
435 }
436
437 void TitleWidget::slotAdjustSelectedItem() {
438     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
439     if (l.size() >= 1) {
440         if (l[0]->type() == 3) {
441             //rect item
442             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
443             rec->setPos(value_x->value(), value_y->value());
444             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
445         } else if (l[0]->type() == 8) {
446             //text item
447             QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
448             rec->setPos(value_x->value(), value_y->value());
449         }
450     }
451 }
452
453 void TitleWidget::slotChangeBackground() {
454     QColor color = kcolorbutton->color();
455     color.setAlpha(horizontalSlider->value());
456     m_frameBorder->setBrush(QBrush(color));
457 }
458
459 void TitleWidget::textChanged() {
460     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
461     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
462         //kDebug() << ktextedit->document()->toHtml();
463         //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
464     }
465 }
466
467 void TitleWidget::slotUpdateText() {
468     QFont font = font_family->currentFont();
469     font.setPointSize(font_size->value());
470     font.setBold(buttonBold->isChecked());
471     font.setItalic(buttonItalic->isChecked());
472     font.setUnderline(buttonUnder->isChecked());
473     QColor color = fontColorButton->color();
474     color.setAlpha(textAlpha->value());
475
476     QGraphicsTextItem* item = NULL;
477     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
478     if (l.size() == 1 && (l[0])->type() == 8) {
479         item = (QGraphicsTextItem*)l[0];
480     }
481     if (!item) return;
482     //if (item->textCursor().selection ().isEmpty())
483     {
484         item->setFont(font);
485         item->setDefaultTextColor(color);
486     }
487     /*else {
488     QTextDocumentFragment selec = item->textCursor().selection ();
489     selec.set
490     }*/
491     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
492
493     //ktextedit->setCurrentFont(font);
494     //ktextedit->setTextColor(color);
495     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
496     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
497     QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
498     //item-
499     }*/
500 }
501
502 void TitleWidget::rectChanged() {
503     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
504     if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
505         QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
506         QColor f = rectFColor->color();
507         f.setAlpha(rectFAlpha->value());
508         QPen penf(f);
509         penf.setWidth(rectLineWidth->value());
510         rec->setPen(penf);
511         QColor b = rectBColor->color();
512         b.setAlpha(rectBAlpha->value());
513         rec->setBrush(QBrush(b));
514     }
515 }
516
517 void TitleWidget::fontBold() {
518     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
519     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
520         //ktextedit->document()->setTextOption();
521     }
522 }
523
524 void TitleWidget::itemScaled(int val) {
525     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
526     if (l.size() == 1) {
527         Transform x = transformations[l[0]];
528         x.scalex = (double)val / 100.0;
529         x.scaley = (double)val / 100.0;
530         QTransform qtrans;
531         qtrans.scale(x.scalex, x.scaley);
532         qtrans.rotate(x.rotate);
533         l[0]->setTransform(qtrans);
534         transformations[l[0]] = x;
535     }
536 }
537
538 void TitleWidget::itemRotate(int val) {
539     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
540     if (l.size() == 1) {
541         Transform x = transformations[l[0]];
542         x.rotate = (double)val;
543         QTransform qtrans;
544         qtrans.scale(x.scalex, x.scaley);
545         qtrans.rotate(x.rotate);
546         l[0]->setTransform(qtrans);
547         transformations[l[0]] = x;
548     }
549 }
550
551 void TitleWidget::itemHCenter() {
552     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
553     if (l.size() == 1) {
554         QGraphicsItem *item = l[0];
555         QRectF br;
556         if (item->type() == 3) {
557             br = ((QGraphicsRectItem*)item)->rect();
558         } else br = item->boundingRect();
559         int width = (int) br.width();
560         int newPos = (int)((m_frameWidth - width) / 2);
561         item->setPos(newPos, item->pos().y());
562     }
563 }
564
565 void TitleWidget::itemVCenter() {
566     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
567     if (l.size() == 1) {
568         QGraphicsItem *item = l[0];
569         QRectF br;
570         if (item->type() == 3) {
571             br = ((QGraphicsRectItem*)item)->rect();
572         } else br = item->boundingRect();
573         int height = (int) br.height();
574         int newPos = (int)((m_frameHeight - height) / 2);
575         item->setPos(item->pos().x(), newPos);
576     }
577 }
578
579 void TitleWidget::setupViewports() {
580     double aspect_ratio = 4.0 / 3.0;//read from project
581
582     QRectF sp(0, 0, 0, 0);
583     QRectF ep(0, 0, 0, 0);
584
585     double sv_size = startViewportSize->value();
586     double ev_size = endViewportSize->value();
587     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
588     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
589
590     startViewport->setPos(startViewportX->value(), startViewportY->value());
591     endViewport->setPos(endViewportX->value(), endViewportY->value());
592
593     startViewport->setPolygon(QPolygonF(sp));
594     endViewport->setPolygon(QPolygonF(ep));
595
596 }
597
598 void TitleWidget::loadTitle() {
599     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
600     if (!url.isEmpty()) {
601         QList<QGraphicsItem *> items = m_scene->items();
602         for (int i = 0; i < items.size(); i++) {
603             if (items.at(i)->zValue() > -1000) delete items.at(i);
604         }
605         m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
606         slotSelectTool();
607     }
608 }
609
610 void TitleWidget::saveTitle(KUrl url) {
611     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
612     if (!url.isEmpty()) m_titledocument.saveDocument(url, startViewport, endViewport);
613 }
614
615 QDomDocument TitleWidget::xml() {
616     return m_titledocument.xml(startViewport, endViewport);
617 }
618
619 void TitleWidget::setXml(QDomDocument doc) {
620     m_titledocument.loadFromXml(doc, startViewport, endViewport);
621     slotSelectTool();
622 }
623
624 QPixmap TitleWidget::renderedPixmap() {
625     QPixmap pix(m_frameWidth, m_frameHeight);
626     pix.fill(Qt::transparent);
627     QPainter painter(&pix);
628
629     m_scene->clearSelection();
630     QPen framepen = m_frameBorder->pen();
631     m_frameBorder->setPen(Qt::NoPen);
632     startViewport->setVisible(false);
633     endViewport->setVisible(false);
634     m_frameImage->setVisible(false);
635
636     m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
637     m_frameBorder->setPen(framepen);
638     startViewport->setVisible(true);
639     endViewport->setVisible(true);
640     m_frameImage->setVisible(true);
641     return pix;
642 }
643
644 #include "moc_titlewidget.cpp"
645