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