]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
f4cbb5940272387776eb700681ff37771065fac1
[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 }
203
204 void TitleWidget::slotSelectTool() {
205     m_scene->setTool(TITLE_SELECT);
206     m_buttonCursor->setChecked(true);
207     m_buttonText->setChecked(false);
208     m_buttonRect->setChecked(false);
209 }
210
211 void TitleWidget::slotImageTool() {
212     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
213     if (!url.isEmpty()) {
214         if (url.path().endsWith(".svg")) {
215             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
216             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
217             svg->setZValue(m_count++);
218             svg->setData(Qt::UserRole, url.path());
219             graphicsView->scene()->addItem(svg);
220         } else {
221             QPixmap pix(url.path());
222             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
223             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
224             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
225             image->setData(Qt::UserRole, url.path());
226             image->setZValue(m_count++);
227             graphicsView->scene()->addItem(image);
228         }
229     }
230     m_scene->setTool(TITLE_SELECT);
231     m_buttonRect->setChecked(false);
232     m_buttonCursor->setChecked(true);
233     m_buttonText->setChecked(false);
234 }
235
236 void TitleWidget::displayBackgroundFrame() {
237     if (!displayBg->isChecked()) {
238         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
239         QPixmap pattern(20, 20);
240         pattern.fill();
241         QPainter p;
242         p.begin(&pattern);
243         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
244         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
245         p.end();
246         QBrush br(pattern);
247
248         p.begin(&bg);
249         p.fillRect(bg.rect(), br);
250         p.end();
251         m_frameImage->setPixmap(bg);
252     } else {
253         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
254     }
255 }
256
257 void TitleWidget::initViewports() {
258     startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
259     endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
260
261     QPen startpen(Qt::DotLine);
262     QPen endpen(Qt::DashDotLine);
263     startpen.setColor(QColor(100, 200, 100, 140));
264     endpen.setColor(QColor(200, 100, 100, 140));
265
266     startViewport->setPen(startpen);
267     endViewport->setPen(endpen);
268
269     startViewportSize->setValue(40);
270     endViewportSize->setValue(40);
271
272     startViewport->setZValue(-1000);
273     endViewport->setZValue(-1000);
274
275     startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
276     endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
277
278     graphicsView->scene()->addItem(startViewport);
279     graphicsView->scene()->addItem(endViewport);
280 }
281
282 void TitleWidget::slotUpdateZoom(int pos) {
283     m_scene->setZoom((double) pos / 7);
284     zoom_label->setText("x" + QString::number((double) pos / 7, 'g', 2));
285 }
286
287 void TitleWidget::slotZoom(bool up) {
288     int pos = zoom_slider->value();
289     if (up) pos++;
290     else pos--;
291     zoom_slider->setValue(pos);
292 }
293
294 void TitleWidget::slotAdjustZoom() {
295     double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
296     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
297     if (scalex > scaley) scalex = scaley;
298     int zoompos = (int)(scalex * 7 + 0.5);
299     zoom_slider->setValue(zoompos);
300     graphicsView->centerOn(m_frameBorder);
301 }
302
303 void TitleWidget::slotZoomOneToOne() {
304     zoom_slider->setValue(7);
305     graphicsView->centerOn(m_frameBorder);
306 }
307
308 void TitleWidget::slotNewRect(QGraphicsRectItem * rect) {
309     QColor f = rectFColor->color();
310     f.setAlpha(rectFAlpha->value());
311     QPen penf(f);
312     penf.setWidth(rectLineWidth->value());
313     rect->setPen(penf);
314     QColor b = rectBColor->color();
315     b.setAlpha(rectBAlpha->value());
316     rect->setBrush(QBrush(b));
317     rect->setZValue(m_count++);
318     //setCurrentItem(rect);
319     //graphicsView->setFocus();
320 }
321
322 void TitleWidget::slotNewText(QGraphicsTextItem *tt) {
323     QFont font = font_family->currentFont();
324     font.setPointSize(font_size->value());
325     tt->setFont(font);
326     QColor color = fontColorButton->color();
327     color.setAlpha(textAlpha->value());
328     tt->setDefaultTextColor(color);
329     tt->setZValue(m_count++);
330     setCurrentItem(tt);
331 }
332
333 void TitleWidget::setCurrentItem(QGraphicsItem *item) {
334     m_scene->setSelectedItem(item);
335 }
336
337 void TitleWidget::zIndexChanged(int v) {
338     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
339     if (l.size() >= 1) {
340         l[0]->setZValue(v);
341     }
342 }
343
344 void TitleWidget::selectionChanged() {
345     if (m_scene->tool() != TITLE_SELECT) return;
346     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
347     //toolBox->setItemEnabled(2, false);
348     //toolBox->setItemEnabled(3, false);
349     value_x->blockSignals(true);
350     value_y->blockSignals(true);
351     value_w->blockSignals(true);
352     value_h->blockSignals(true);
353     kDebug() << "////////  SELECTION CHANGED; ITEMS: " << l.size();
354     if (l.size() == 1) {
355         if ((l[0])->type() == 8) {
356             rect_properties->setHidden(true);
357             text_properties->setHidden(false);
358             QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
359             //if (l[0]->hasFocus())
360             //ktextedit->setHtml(i->toHtml());
361             toolBox->setCurrentIndex(0);
362             //toolBox->setItemEnabled(2, true);
363             font_size->blockSignals(true);
364             font_family->blockSignals(true);
365             buttonBold->blockSignals(true);
366             buttonItalic->blockSignals(true);
367             buttonUnder->blockSignals(true);
368             fontColorButton->blockSignals(true);
369             textAlpha->blockSignals(true);
370
371             QFont font = i->font();
372             font_family->setCurrentFont(font);
373             font_size->setValue(font.pointSize());
374             buttonBold->setChecked(font.bold());
375             buttonItalic->setChecked(font.italic());
376             buttonUnder->setChecked(font.underline());
377
378             QColor color = i->defaultTextColor();
379             fontColorButton->setColor(color);
380             textAlpha->setValue(color.alpha());
381
382             font_size->blockSignals(false);
383             font_family->blockSignals(false);
384             buttonBold->blockSignals(false);
385             buttonItalic->blockSignals(false);
386             buttonUnder->blockSignals(false);
387             fontColorButton->blockSignals(false);
388             textAlpha->blockSignals(false);
389
390             value_x->setValue((int) i->pos().x());
391             value_y->setValue((int) i->pos().y());
392             value_w->setValue((int) i->boundingRect().width());
393             value_h->setValue((int) i->boundingRect().height());
394             frame_properties->setEnabled(true);
395             value_w->setEnabled(false);
396             value_h->setEnabled(false);
397         } else if ((l[0])->type() == 3) {
398             rect_properties->setHidden(false);
399             text_properties->setHidden(true);
400             settingUp = true;
401             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
402             toolBox->setCurrentIndex(0);
403             //toolBox->setItemEnabled(3, true);
404             rectFAlpha->setValue(rec->pen().color().alpha());
405             rectBAlpha->setValue(rec->brush().color().alpha());
406             kDebug() << rec->brush().color().alpha();
407             QColor fcol = rec->pen().color();
408             QColor bcol = rec->brush().color();
409             //fcol.setAlpha(255);
410             //bcol.setAlpha(255);
411             rectFColor->setColor(fcol);
412             rectBColor->setColor(bcol);
413             settingUp = false;
414             rectLineWidth->setValue(rec->pen().width());
415             value_x->setValue((int) rec->pos().x());
416             value_y->setValue((int) rec->pos().y());
417             value_w->setValue((int) rec->rect().width());
418             value_h->setValue((int) rec->rect().height());
419             frame_properties->setEnabled(true);
420             value_w->setEnabled(true);
421             value_h->setEnabled(true);
422         } else {
423             //toolBox->setCurrentIndex(0);
424             frame_properties->setEnabled(false);
425         }
426         zValue->setValue((int)l[0]->zValue());
427         itemzoom->setValue((int)(transformations[l[0]].scalex * 100));
428         itemrotate->setValue((int)(transformations[l[0]].rotate));
429         value_x->blockSignals(false);
430         value_y->blockSignals(false);
431         value_w->blockSignals(false);
432         value_h->blockSignals(false);
433     } else frame_properties->setEnabled(false);
434 }
435
436 void TitleWidget::slotAdjustSelectedItem() {
437     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
438     if (l.size() >= 1) {
439         if (l[0]->type() == 3) {
440             //rect item
441             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
442             rec->setPos(value_x->value(), value_y->value());
443             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
444         } else if (l[0]->type() == 8) {
445             //text item
446             QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
447             rec->setPos(value_x->value(), value_y->value());
448         }
449     }
450 }
451
452 void TitleWidget::slotChangeBackground() {
453     QColor color = kcolorbutton->color();
454     color.setAlpha(horizontalSlider->value());
455     m_frameBorder->setBrush(QBrush(color));
456 }
457
458 void TitleWidget::textChanged() {
459     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
460     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
461         //kDebug() << ktextedit->document()->toHtml();
462         //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
463     }
464 }
465
466 void TitleWidget::slotUpdateText() {
467     QFont font = font_family->currentFont();
468     font.setPointSize(font_size->value());
469     font.setBold(buttonBold->isChecked());
470     font.setItalic(buttonItalic->isChecked());
471     font.setUnderline(buttonUnder->isChecked());
472     QColor color = fontColorButton->color();
473     color.setAlpha(textAlpha->value());
474
475     QGraphicsTextItem* item = NULL;
476     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
477     if (l.size() == 1 && (l[0])->type() == 8) {
478         item = (QGraphicsTextItem*)l[0];
479     }
480     if (!item) return;
481     //if (item->textCursor().selection ().isEmpty())
482     {
483         item->setFont(font);
484         item->setDefaultTextColor(color);
485     }
486     /*else {
487     QTextDocumentFragment selec = item->textCursor().selection ();
488     selec.set
489     }*/
490     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
491
492     //ktextedit->setCurrentFont(font);
493     //ktextedit->setTextColor(color);
494     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
495     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
496     QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
497     //item-
498     }*/
499 }
500
501 void TitleWidget::rectChanged() {
502     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
503     if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
504         QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
505         QColor f = rectFColor->color();
506         f.setAlpha(rectFAlpha->value());
507         QPen penf(f);
508         penf.setWidth(rectLineWidth->value());
509         rec->setPen(penf);
510         QColor b = rectBColor->color();
511         b.setAlpha(rectBAlpha->value());
512         rec->setBrush(QBrush(b));
513     }
514 }
515
516 void TitleWidget::fontBold() {
517     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
518     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
519         //ktextedit->document()->setTextOption();
520     }
521 }
522
523 void TitleWidget::itemScaled(int val) {
524     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
525     if (l.size() == 1) {
526         Transform x = transformations[l[0]];
527         x.scalex = (double)val / 100.0;
528         x.scaley = (double)val / 100.0;
529         QTransform qtrans;
530         qtrans.scale(x.scalex, x.scaley);
531         qtrans.rotate(x.rotate);
532         l[0]->setTransform(qtrans);
533         transformations[l[0]] = x;
534     }
535 }
536
537 void TitleWidget::itemRotate(int val) {
538     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
539     if (l.size() == 1) {
540         Transform x = transformations[l[0]];
541         x.rotate = (double)val;
542         QTransform qtrans;
543         qtrans.scale(x.scalex, x.scaley);
544         qtrans.rotate(x.rotate);
545         l[0]->setTransform(qtrans);
546         transformations[l[0]] = x;
547     }
548 }
549
550 void TitleWidget::itemHCenter() {
551     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
552     if (l.size() == 1) {
553         QGraphicsItem *item = l[0];
554         QRectF br;
555         if (item->type() == 3) {
556             br = ((QGraphicsRectItem*)item)->rect();
557         } else br = item->boundingRect();
558         int width = (int) br.width();
559         int newPos = (int)((m_frameWidth - width) / 2);
560         item->setPos(newPos, item->pos().y());
561     }
562 }
563
564 void TitleWidget::itemVCenter() {
565     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
566     if (l.size() == 1) {
567         QGraphicsItem *item = l[0];
568         QRectF br;
569         if (item->type() == 3) {
570             br = ((QGraphicsRectItem*)item)->rect();
571         } else br = item->boundingRect();
572         int height = (int) br.height();
573         int newPos = (int)((m_frameHeight - height) / 2);
574         item->setPos(item->pos().x(), newPos);
575     }
576 }
577
578 void TitleWidget::setupViewports() {
579     double aspect_ratio = 4.0 / 3.0;//read from project
580
581     QRectF sp(0, 0, 0, 0);
582     QRectF ep(0, 0, 0, 0);
583
584     double sv_size = startViewportSize->value();
585     double ev_size = endViewportSize->value();
586     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
587     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
588
589     startViewport->setPos(startViewportX->value(), startViewportY->value());
590     endViewport->setPos(endViewportX->value(), endViewportY->value());
591
592     startViewport->setPolygon(QPolygonF(sp));
593     endViewport->setPolygon(QPolygonF(ep));
594
595 }
596
597 void TitleWidget::loadTitle() {
598     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
599     if (!url.isEmpty()) {
600         QList<QGraphicsItem *> items = m_scene->items();
601         for (int i = 0; i < items.size(); i++) {
602             if (items.at(i)->zValue() > -1000) delete items.at(i);
603         }
604         m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
605         slotSelectTool();
606     }
607 }
608
609 void TitleWidget::saveTitle(KUrl url) {
610     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
611     if (!url.isEmpty()) m_titledocument.saveDocument(url, startViewport, endViewport);
612 }
613
614 QPixmap TitleWidget::renderedPixmap() {
615     QPixmap pix(m_frameWidth, m_frameHeight);
616     pix.fill(Qt::transparent);
617     QPainter painter(&pix);
618
619     m_scene->clearSelection();
620     QPen framepen = m_frameBorder->pen();
621     m_frameBorder->setPen(Qt::NoPen);
622     startViewport->setVisible(false);
623     endViewport->setVisible(false);
624     m_frameImage->setVisible(false);
625
626     m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
627     m_frameBorder->setPen(framepen);
628     startViewport->setVisible(true);
629     endViewport->setVisible(true);
630     m_frameImage->setVisible(true);
631     return pix;
632 }
633
634 #include "moc_titlewidget.cpp"
635