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