]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
f0e43c3d827c4cfcff8b69f4aa7020d06709afaa
[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), startViewport(NULL), endViewport(NULL) {
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 TitleWidget::~TitleWidget() {
199     delete m_buttonRect;
200     delete m_buttonText;
201     delete m_buttonImage;
202     delete m_buttonCursor;
203     delete m_buttonSave;
204     delete m_buttonLoad;
205
206     delete m_frameBorder;
207     delete m_frameImage;
208     if (startViewport) delete startViewport;
209     if (endViewport) delete endViewport;
210     delete m_scene;
211 }
212
213 //static
214 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl) {
215     QStringList result;
216     QString titlePath = projectUrl.path() + "/titles/";
217     KStandardDirs::makeDir(titlePath);
218     QString titleName = "title";
219     int counter = 0;
220     QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
221     while (QFile::exists(path + ".png")) {
222         counter++;
223         path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
224     }
225     result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
226     result.append(path + ".png");
227     return result;
228 }
229
230 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName) {
231     QStringList result;
232     QString titlePath = projectUrl.path() + "/titles/";
233     KStandardDirs::makeDir(titlePath);
234     return titlePath + titleName + ".png";
235 }
236
237 //virtual
238 void TitleWidget::resizeEvent(QResizeEvent * event) {
239     //slotAdjustZoom();
240 }
241
242 void TitleWidget::slotTextTool() {
243     rect_properties->setHidden(true);
244     text_properties->setHidden(false);
245     m_scene->setTool(TITLE_TEXT);
246     m_buttonRect->setChecked(false);
247     m_buttonCursor->setChecked(false);
248 }
249
250 void TitleWidget::slotRectTool() {
251     text_properties->setHidden(true);
252     rect_properties->setHidden(false);
253     m_scene->setTool(TITLE_RECTANGLE);
254     m_buttonText->setChecked(false);
255     m_buttonCursor->setChecked(false);
256     m_buttonRect->setChecked(true);
257 }
258
259 void TitleWidget::slotSelectTool() {
260     m_scene->setTool(TITLE_SELECT);
261     m_buttonCursor->setChecked(true);
262     m_buttonText->setChecked(false);
263     m_buttonRect->setChecked(false);
264 }
265
266 void TitleWidget::slotImageTool() {
267     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
268     if (!url.isEmpty()) {
269         if (url.path().endsWith(".svg")) {
270             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
271             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
272             svg->setZValue(m_count++);
273             svg->setData(Qt::UserRole, url.path());
274             graphicsView->scene()->addItem(svg);
275         } else {
276             QPixmap pix(url.path());
277             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
278             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
279             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
280             image->setData(Qt::UserRole, url.path());
281             image->setZValue(m_count++);
282             graphicsView->scene()->addItem(image);
283         }
284     }
285     m_scene->setTool(TITLE_SELECT);
286     m_buttonRect->setChecked(false);
287     m_buttonCursor->setChecked(true);
288     m_buttonText->setChecked(false);
289 }
290
291 void TitleWidget::displayBackgroundFrame() {
292     if (!displayBg->isChecked()) {
293         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
294         QPixmap pattern(20, 20);
295         pattern.fill();
296         QPainter p;
297         p.begin(&pattern);
298         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
299         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
300         p.end();
301         QBrush br(pattern);
302
303         p.begin(&bg);
304         p.fillRect(bg.rect(), br);
305         p.end();
306         m_frameImage->setPixmap(bg);
307     } else {
308         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
309     }
310 }
311
312 void TitleWidget::initViewports() {
313     startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
314     endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
315
316     QPen startpen(Qt::DotLine);
317     QPen endpen(Qt::DashDotLine);
318     startpen.setColor(QColor(100, 200, 100, 140));
319     endpen.setColor(QColor(200, 100, 100, 140));
320
321     startViewport->setPen(startpen);
322     endViewport->setPen(endpen);
323
324     startViewportSize->setValue(40);
325     endViewportSize->setValue(40);
326
327     startViewport->setZValue(-1000);
328     endViewport->setZValue(-1000);
329
330     startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
331     endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
332
333     graphicsView->scene()->addItem(startViewport);
334     graphicsView->scene()->addItem(endViewport);
335 }
336
337 void TitleWidget::slotUpdateZoom(int pos) {
338     m_scene->setZoom((double) pos / 100);
339     zoom_label->setText(QString::number(pos) + '%');
340 }
341
342 void TitleWidget::slotZoom(bool up) {
343     int pos = zoom_slider->value();
344     if (up) pos++;
345     else pos--;
346     zoom_slider->setValue(pos);
347 }
348
349 void TitleWidget::slotAdjustZoom() {
350     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
351     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
352     if (scalex > scaley) scalex = scaley;
353     int zoompos = (int)(scalex * 7 + 0.5);*/
354     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
355     int zoompos = graphicsView->matrix().m11() * 100;
356     zoom_slider->setValue(zoompos);
357     graphicsView->centerOn(m_frameBorder);
358 }
359
360 void TitleWidget::slotZoomOneToOne() {
361     zoom_slider->setValue(100);
362     graphicsView->centerOn(m_frameBorder);
363 }
364
365 void TitleWidget::slotNewRect(QGraphicsRectItem * rect) {
366     QColor f = rectFColor->color();
367     f.setAlpha(rectFAlpha->value());
368     QPen penf(f);
369     penf.setWidth(rectLineWidth->value());
370     rect->setPen(penf);
371     QColor b = rectBColor->color();
372     b.setAlpha(rectBAlpha->value());
373     rect->setBrush(QBrush(b));
374     rect->setZValue(m_count++);
375     //setCurrentItem(rect);
376     //graphicsView->setFocus();
377 }
378
379 void TitleWidget::slotNewText(QGraphicsTextItem *tt) {
380     QFont font = font_family->currentFont();
381     font.setPointSize(font_size->value());
382     // mbd: issue 551:
383     font.setBold(buttonBold->isChecked());
384     font.setItalic(buttonItalic->isChecked());
385     font.setUnderline(buttonUnder->isChecked());
386
387     tt->setFont(font);
388     QColor color = fontColorButton->color();
389     color.setAlpha(textAlpha->value());
390     tt->setDefaultTextColor(color);
391     tt->setZValue(m_count++);
392     setCurrentItem(tt);
393 }
394
395 void TitleWidget::setCurrentItem(QGraphicsItem *item) {
396     m_scene->setSelectedItem(item);
397 }
398
399 void TitleWidget::zIndexChanged(int v) {
400     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
401     if (l.size() >= 1) {
402         l[0]->setZValue(v);
403     }
404 }
405
406 void TitleWidget::selectionChanged() {
407     if (m_scene->tool() != TITLE_SELECT) return;
408     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
409     //toolBox->setItemEnabled(2, false);
410     //toolBox->setItemEnabled(3, false);
411     value_x->blockSignals(true);
412     value_y->blockSignals(true);
413     value_w->blockSignals(true);
414     value_h->blockSignals(true);
415     //kDebug() << "////////  SELECTION CHANGED; ITEMS: " << l.size();
416     if (l.size() == 1) {
417         if ((l[0])->type() == 8) {
418             rect_properties->setHidden(true);
419             text_properties->setHidden(false);
420             QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
421             //if (l[0]->hasFocus())
422             //ktextedit->setHtml(i->toHtml());
423             toolBox->setCurrentIndex(0);
424             //toolBox->setItemEnabled(2, true);
425             font_size->blockSignals(true);
426             font_family->blockSignals(true);
427             buttonBold->blockSignals(true);
428             buttonItalic->blockSignals(true);
429             buttonUnder->blockSignals(true);
430             fontColorButton->blockSignals(true);
431             textAlpha->blockSignals(true);
432
433             QFont font = i->font();
434             font_family->setCurrentFont(font);
435             font_size->setValue(font.pointSize());
436             buttonBold->setChecked(font.bold());
437             buttonItalic->setChecked(font.italic());
438             buttonUnder->setChecked(font.underline());
439
440             QColor color = i->defaultTextColor();
441             fontColorButton->setColor(color);
442             textAlpha->setValue(color.alpha());
443
444             font_size->blockSignals(false);
445             font_family->blockSignals(false);
446             buttonBold->blockSignals(false);
447             buttonItalic->blockSignals(false);
448             buttonUnder->blockSignals(false);
449             fontColorButton->blockSignals(false);
450             textAlpha->blockSignals(false);
451
452             value_x->setValue((int) i->pos().x());
453             value_y->setValue((int) i->pos().y());
454             value_w->setValue((int) i->boundingRect().width());
455             value_h->setValue((int) i->boundingRect().height());
456             frame_properties->setEnabled(true);
457             value_w->setEnabled(false);
458             value_h->setEnabled(false);
459         } else if ((l[0])->type() == 3) {
460             rect_properties->setHidden(false);
461             text_properties->setHidden(true);
462             settingUp = true;
463             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
464             toolBox->setCurrentIndex(0);
465             //toolBox->setItemEnabled(3, true);
466             rectFAlpha->setValue(rec->pen().color().alpha());
467             rectBAlpha->setValue(rec->brush().color().alpha());
468             //kDebug() << rec->brush().color().alpha();
469             QColor fcol = rec->pen().color();
470             QColor bcol = rec->brush().color();
471             //fcol.setAlpha(255);
472             //bcol.setAlpha(255);
473             rectFColor->setColor(fcol);
474             rectBColor->setColor(bcol);
475             settingUp = false;
476             rectLineWidth->setValue(rec->pen().width());
477             value_x->setValue((int) rec->pos().x());
478             value_y->setValue((int) rec->pos().y());
479             value_w->setValue((int) rec->rect().width());
480             value_h->setValue((int) rec->rect().height());
481             frame_properties->setEnabled(true);
482             value_w->setEnabled(true);
483             value_h->setEnabled(true);
484         } else {
485             //toolBox->setCurrentIndex(0);
486             frame_properties->setEnabled(false);
487         }
488         zValue->setValue((int)l[0]->zValue());
489         itemzoom->setValue((int)(transformations[l[0]].scalex * 100));
490         itemrotate->setValue((int)(transformations[l[0]].rotate));
491         value_x->blockSignals(false);
492         value_y->blockSignals(false);
493         value_w->blockSignals(false);
494         value_h->blockSignals(false);
495     } else frame_properties->setEnabled(false);
496 }
497
498 void TitleWidget::slotAdjustSelectedItem() {
499     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
500     if (l.size() >= 1) {
501         if (l[0]->type() == 3) {
502             //rect item
503             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
504             rec->setPos(value_x->value(), value_y->value());
505             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
506         } else if (l[0]->type() == 8) {
507             //text item
508             QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
509             rec->setPos(value_x->value(), value_y->value());
510         }
511     }
512 }
513
514 void TitleWidget::slotChangeBackground() {
515     QColor color = kcolorbutton->color();
516     color.setAlpha(horizontalSlider->value());
517     m_frameBorder->setBrush(QBrush(color));
518 }
519
520 void TitleWidget::textChanged() {
521     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
522     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
523         //kDebug() << ktextedit->document()->toHtml();
524         //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
525     }
526 }
527
528 void TitleWidget::slotUpdateText() {
529     QFont font = font_family->currentFont();
530     font.setPointSize(font_size->value());
531     font.setBold(buttonBold->isChecked());
532     font.setItalic(buttonItalic->isChecked());
533     font.setUnderline(buttonUnder->isChecked());
534     QColor color = fontColorButton->color();
535     color.setAlpha(textAlpha->value());
536
537     QGraphicsTextItem* item = NULL;
538     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
539     if (l.size() == 1 && (l[0])->type() == 8) {
540         item = (QGraphicsTextItem*)l[0];
541     }
542     if (!item) return;
543     //if (item->textCursor().selection ().isEmpty())
544     {
545         item->setFont(font);
546         item->setDefaultTextColor(color);
547     }
548     /*else {
549     QTextDocumentFragment selec = item->textCursor().selection ();
550     selec.set
551     }*/
552     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
553
554     //ktextedit->setCurrentFont(font);
555     //ktextedit->setTextColor(color);
556     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
557     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
558     QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
559     //item-
560     }*/
561 }
562
563 void TitleWidget::rectChanged() {
564     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
565     if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
566         QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
567         QColor f = rectFColor->color();
568         f.setAlpha(rectFAlpha->value());
569         QPen penf(f);
570         penf.setWidth(rectLineWidth->value());
571         rec->setPen(penf);
572         QColor b = rectBColor->color();
573         b.setAlpha(rectBAlpha->value());
574         rec->setBrush(QBrush(b));
575     }
576 }
577
578 void TitleWidget::fontBold() {
579     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
580     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
581         //ktextedit->document()->setTextOption();
582     }
583 }
584
585 void TitleWidget::itemScaled(int val) {
586     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
587     if (l.size() == 1) {
588         Transform x = transformations[l[0]];
589         x.scalex = (double)val / 100.0;
590         x.scaley = (double)val / 100.0;
591         QTransform qtrans;
592         qtrans.scale(x.scalex, x.scaley);
593         qtrans.rotate(x.rotate);
594         l[0]->setTransform(qtrans);
595         transformations[l[0]] = x;
596     }
597 }
598
599 void TitleWidget::itemRotate(int val) {
600     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
601     if (l.size() == 1) {
602         Transform x = transformations[l[0]];
603         x.rotate = (double)val;
604         QTransform qtrans;
605         qtrans.scale(x.scalex, x.scaley);
606         qtrans.rotate(x.rotate);
607         l[0]->setTransform(qtrans);
608         transformations[l[0]] = x;
609     }
610 }
611
612 void TitleWidget::itemHCenter() {
613     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
614     if (l.size() == 1) {
615         QGraphicsItem *item = l[0];
616         QRectF br;
617         if (item->type() == 3) {
618             br = ((QGraphicsRectItem*)item)->rect();
619         } else br = item->boundingRect();
620         int width = (int) br.width();
621         int newPos = (int)((m_frameWidth - width) / 2);
622         item->setPos(newPos, item->pos().y());
623     }
624 }
625
626 void TitleWidget::itemVCenter() {
627     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
628     if (l.size() == 1) {
629         QGraphicsItem *item = l[0];
630         QRectF br;
631         if (item->type() == 3) {
632             br = ((QGraphicsRectItem*)item)->rect();
633         } else br = item->boundingRect();
634         int height = (int) br.height();
635         int newPos = (int)((m_frameHeight - height) / 2);
636         item->setPos(item->pos().x(), newPos);
637     }
638 }
639
640 void TitleWidget::setupViewports() {
641     double aspect_ratio = 4.0 / 3.0;//read from project
642
643     QRectF sp(0, 0, 0, 0);
644     QRectF ep(0, 0, 0, 0);
645
646     double sv_size = startViewportSize->value();
647     double ev_size = endViewportSize->value();
648     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
649     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
650
651     startViewport->setPos(startViewportX->value(), startViewportY->value());
652     endViewport->setPos(endViewportX->value(), endViewportY->value());
653
654     startViewport->setPolygon(QPolygonF(sp));
655     endViewport->setPolygon(QPolygonF(ep));
656
657 }
658
659 void TitleWidget::loadTitle() {
660     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
661     if (!url.isEmpty()) {
662         QList<QGraphicsItem *> items = m_scene->items();
663         for (int i = 0; i < items.size(); i++) {
664             if (items.at(i)->zValue() > -1000) delete items.at(i);
665         }
666         m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
667         slotSelectTool();
668     }
669 }
670
671 void TitleWidget::saveTitle(KUrl url) {
672     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
673     if (!url.isEmpty()) m_titledocument.saveDocument(url, startViewport, endViewport);
674 }
675
676 QDomDocument TitleWidget::xml() {
677     return m_titledocument.xml(startViewport, endViewport);
678 }
679
680 void TitleWidget::setXml(QDomDocument doc) {
681     m_count = m_titledocument.loadFromXml(doc, startViewport, endViewport);
682     // mbd: Update the GUI color selectors to match the stuff from the loaded document
683     QColor background_color = m_titledocument.getBackgroundColor();
684     horizontalSlider->blockSignals(true);
685     kcolorbutton->blockSignals(true);
686     horizontalSlider->setValue(background_color.alpha());
687     background_color.setAlpha(255);
688     kcolorbutton->setColor(background_color);
689     horizontalSlider->blockSignals(false);
690     kcolorbutton->blockSignals(false);
691
692     slotSelectTool();
693 }
694
695 QImage TitleWidget::renderedPixmap() {
696     QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
697     pix.fill(Qt::transparent);
698     QPainter painter(&pix);
699     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
700     m_scene->clearTextSelection();
701     QPen framepen = m_frameBorder->pen();
702     m_frameBorder->setPen(Qt::NoPen);
703     startViewport->setVisible(false);
704     endViewport->setVisible(false);
705     m_frameImage->setVisible(false);
706
707     m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
708     painter.end();
709     m_frameBorder->setPen(framepen);
710     startViewport->setVisible(true);
711     endViewport->setVisible(true);
712     m_frameImage->setVisible(true);
713     return pix;
714 }
715
716 /** \brief Connected to the accepted signal - calls writeChoices */
717 void TitleWidget::slotAccepted() {
718     writeChoices();
719 }
720
721 /** \brief Store the current choices of font, background and rect values */
722 void TitleWidget::writeChoices() {
723     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
724     KSharedConfigPtr config = KGlobal::config();
725     KConfigGroup titleConfig(config, "TitleWidget");
726     // Write the entries
727     titleConfig.writeEntry("font_family", font_family->currentFont());
728     titleConfig.writeEntry("font_size", font_size->value());
729     titleConfig.writeEntry("font_color", fontColorButton->color());
730     titleConfig.writeEntry("font_alpha", textAlpha->value());
731     titleConfig.writeEntry("font_bold", buttonBold->isChecked());
732     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
733     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
734
735     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
736     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
737     titleConfig.writeEntry("rect_background_color", rectBColor->color());
738     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
739     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
740
741     titleConfig.writeEntry("background_color", kcolorbutton->color());
742     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
743     //! \todo Not sure if I should sync - it is probably safe to do it
744     config->sync();
745
746 }
747
748 /** \brief Read the last stored choices into the dialog */
749 void TitleWidget::readChoices() {
750     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
751     KSharedConfigPtr config = KGlobal::config();
752     KConfigGroup titleConfig(config, "TitleWidget");
753     // read the entries
754     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
755     font_size->setValue(titleConfig.readEntry("font_size", font_size->value()));
756     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
757     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
758     buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
759     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
760     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
761
762     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
763     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
764     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
765     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
766     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
767
768     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
769     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
770 }
771