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