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