]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
Typewriter effect now working (MLT patch pending)
[kdenlive] / src / titlewidget.cpp
1 /***************************************************************************
2                           titlewidget.cpp  -  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 <cmath>
22
23 #include <KDebug>
24 #include <KGlobalSettings>
25 #include <KFileDialog>
26 #include <KStandardDirs>
27 #include <KMessageBox>
28 #include <kio/netaccess.h>
29 #include <kdeversion.h>
30
31 #include <QDomDocument>
32 #include <QGraphicsItem>
33 #include <QGraphicsSvgItem>
34 #include <QTimer>
35 #include <QToolBar>
36 #include <QMenu>
37 #include <QSignalMapper>
38 #include <QTextBlockFormat>
39 #include <QTextCursor>
40
41 #if QT_VERSION >= 0x040600
42 #include <QGraphicsEffect>
43 #include <QGraphicsBlurEffect>
44 #include <QGraphicsDropShadowEffect>
45 #endif
46
47 int settingUp = false;
48
49 const int IMAGEITEM = 7;
50 const int RECTITEM = 3;
51 const int TEXTITEM = 8;
52
53 const int NOEFFECT = 0;
54 const int BLUREFFECT = 1;
55 const int SHADOWEFFECT = 2;
56 const int TYPEWRITEREFFECT = 3;
57
58 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
59         QDialog(parent),
60         Ui::TitleWidget_UI(),
61         m_startViewport(NULL),
62         m_endViewport(NULL),
63         m_render(render),
64         m_count(0),
65         m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
66         m_projectTitlePath(projectTitlePath),
67         m_tc(tc)
68 {
69     setupUi(this);
70     setFont(KGlobalSettings::toolBarFont());
71     frame_properties->setEnabled(false);
72     frame_properties->setFixedHeight(frame_toolbar->height());
73
74     itemzoom->setSuffix(i18n("%"));
75     m_frameWidth = render->renderWidth();
76     m_frameHeight = render->renderHeight();
77     showToolbars(TITLE_SELECT);
78
79     //TODO: get default title duration instead of hardcoded one
80     title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
81
82     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
83     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
84
85     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
86     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
87     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
88     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
89     connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
90
91     connect(font_family, SIGNAL(editTextChanged(const QString &)), this, SLOT(slotFontText(const QString&)));
92
93     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
94     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
95     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
96     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
97     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
98
99     /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
100     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
101     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
102     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
103     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
104     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
105
106     // Fill effects
107     effect_list->addItem(i18n("None"), NOEFFECT);
108     effect_list->addItem(i18n("Typewriter"), TYPEWRITEREFFECT);
109
110     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
111     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
112     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
113     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
114     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
115     connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
116     connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
117     connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
118     connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
119     connect(effect_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotAddEffect(int)));
120     connect(typewriter_delay, SIGNAL(valueChanged(int)), this, SLOT(slotEditTypewriter(int)));
121     connect(blur_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditBlur(int)));
122     connect(shadow_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
123     connect(shadow_x, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
124     connect(shadow_y, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
125     effect_stack->setHidden(true);
126     effect_frame->setEnabled(false);
127
128     connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
129     connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
130
131     m_signalMapper = new QSignalMapper(this);
132     m_signalMapper->setMapping(value_w, ValueWidth);
133     m_signalMapper->setMapping(value_h, ValueHeight);
134     connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
135     connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
136     connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
137
138     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
139     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
140     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
141     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
142     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
143     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
144     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
145     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
146     connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
147     connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
148     connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
149     connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
150     //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
151     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
152
153     connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
154
155     // mbd
156     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
157
158     font_weight_box->blockSignals(true);
159     font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
160     font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
161     font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
162     font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
163     font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
164     font_weight_box->setToolTip(i18n("Font weight"));
165     font_weight_box->setCurrentIndex(1);
166     font_weight_box->blockSignals(false);
167
168     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
169     buttonRealSize->setIcon(KIcon("zoom-original"));
170     buttonItalic->setIcon(KIcon("format-text-italic"));
171     buttonUnder->setIcon(KIcon("format-text-underline"));
172     buttonAlignCenter->setIcon(KIcon("format-justify-center"));
173     buttonAlignLeft->setIcon(KIcon("format-justify-left"));
174     buttonAlignRight->setIcon(KIcon("format-justify-right"));
175     buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
176
177     buttonAlignNone->setToolTip(i18n("No alignment"));
178     buttonAlignRight->setToolTip(i18n("Align right"));
179     buttonAlignLeft->setToolTip(i18n("Align left"));
180     buttonAlignCenter->setToolTip(i18n("Align center"));
181
182     m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
183     m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
184     m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
185     connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
186     buttonInsertUnicode->setDefaultAction(m_unicodeAction);
187
188     origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
189     origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
190     rectBColor->setToolTip(i18n("Select fill color"));
191     rectFColor->setToolTip(i18n("Select border color"));
192     rectBAlpha->setToolTip(i18n("Fill transparency"));
193     rectFAlpha->setToolTip(i18n("Border transparency"));
194     zoom_slider->setToolTip(i18n("Zoom"));
195     buttonRealSize->setToolTip(i18n("Original size (1:1)"));
196     buttonFitZoom->setToolTip(i18n("Fit zoom"));
197     kcolorbutton->setToolTip(i18n("Select background color"));
198     horizontalSlider->setToolTip(i18n("Background Transparency"));
199
200     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
201     itemhcenter->setToolTip(i18n("Align item horizontally"));
202     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
203     itemvcenter->setToolTip(i18n("Align item vertically"));
204     itemtop->setIcon(KIcon("kdenlive-align-top"));
205     itemtop->setToolTip(i18n("Align item to top"));
206     itembottom->setIcon(KIcon("kdenlive-align-bottom"));
207     itembottom->setToolTip(i18n("Align item to bottom"));
208     itemright->setIcon(KIcon("kdenlive-align-right"));
209     itemright->setToolTip(i18n("Align item to right"));
210     itemleft->setIcon(KIcon("kdenlive-align-left"));
211     itemleft->setToolTip(i18n("Align item to left"));
212
213
214     QHBoxLayout *layout = new QHBoxLayout;
215     frame_toolbar->setLayout(layout);
216     layout->setContentsMargins(0, 0, 0, 0);
217     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
218     int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
219     m_toolbar->setIconSize(QSize(s, s));
220
221     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
222     m_buttonCursor->setCheckable(true);
223     m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
224     m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
225     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
226
227     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
228     m_buttonText->setCheckable(true);
229     m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
230     m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
231     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
232
233     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
234     m_buttonRect->setCheckable(true);
235     m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
236     m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
237     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
238
239     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
240     m_buttonImage->setCheckable(false);
241     m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
242     m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
243     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
244
245     m_toolbar->addSeparator();
246
247     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
248     m_buttonLoad->setCheckable(false);
249     m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
250     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
251
252     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
253     m_buttonSave->setCheckable(false);
254     m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
255     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
256
257     layout->addWidget(m_toolbar);
258
259     // initialize graphic scene
260     m_scene = new GraphicsSceneRectMove(this);
261     graphicsView->setScene(m_scene);
262     m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
263     connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
264
265     // a gradient background
266     /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
267     gradient->setSpread(QGradient::ReflectSpread);
268     scene->setBackgroundBrush(*gradient);*/
269
270     m_frameImage = new QGraphicsPixmapItem();
271     QTransform qtrans;
272     qtrans.scale(2.0, 2.0);
273     m_frameImage->setTransform(qtrans);
274     m_frameImage->setZValue(-1200);
275     m_frameImage->setFlags(0);
276     displayBackgroundFrame();
277     graphicsView->scene()->addItem(m_frameImage);
278
279     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
280     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
281     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
282     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
283     //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
284     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
285     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
286     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
287
288     QPen framepen(Qt::DotLine);
289     framepen.setColor(Qt::red);
290
291     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
292     m_frameBorder->setPen(framepen);
293     m_frameBorder->setZValue(-1100);
294     m_frameBorder->setBrush(Qt::transparent);
295     m_frameBorder->setFlags(0);
296     graphicsView->scene()->addItem(m_frameBorder);
297
298     // mbd: load saved settings
299     readChoices();
300
301     graphicsView->show();
302     //graphicsView->setRenderHint(QPainter::Antialiasing);
303     graphicsView->setInteractive(true);
304     //graphicsView->resize(400, 300);
305     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
306     //toolBox->setItemEnabled(2, false);
307     m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
308     m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
309     m_startViewport->setData(0, m_frameWidth);
310     m_startViewport->setData(1, m_frameHeight);
311     m_endViewport->setData(0, m_frameWidth);
312     m_endViewport->setData(1, m_frameHeight);
313
314     if (!url.isEmpty()) loadTitle(url);
315     else {
316         slotTextTool();
317         QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
318     }
319     initAnimation();
320     connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
321     connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
322
323     buttonBox->button(QDialogButtonBox::Ok)->setEnabled(KdenliveSettings::hastitleproducer());
324 }
325
326 TitleWidget::~TitleWidget()
327 {
328     delete m_buttonRect;
329     delete m_buttonText;
330     delete m_buttonImage;
331     delete m_buttonCursor;
332     delete m_buttonSave;
333     delete m_buttonLoad;
334     delete m_unicodeAction;
335
336     delete m_unicodeDialog;
337     delete m_frameBorder;
338     delete m_frameImage;
339     delete m_startViewport;
340     delete m_endViewport;
341     delete m_scene;
342     delete m_signalMapper;
343 }
344
345 //static
346 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
347 {
348     QStringList result;
349     QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
350     KStandardDirs::makeDir(titlePath);
351     titlePath.append((isClone == false) ? "title" : "clone");
352     int counter = 0;
353     QString path;
354     while (path.isEmpty() || QFile::exists(path)) {
355         counter++;
356         path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
357     }
358     result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
359     result.append(path);
360     return result;
361 }
362
363 // static
364 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
365 {
366     QStringList result;
367     QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
368     KStandardDirs::makeDir(titlePath);
369     return titlePath + titleName + ".png";
370 }
371
372 // static
373 QStringList TitleWidget::extractImageList(QString xml)
374 {
375     QStringList result;
376     if (xml.isEmpty()) return result;
377     QDomDocument doc;
378     doc.setContent(xml);
379     QDomNodeList images = doc.elementsByTagName("content");
380     for (int i = 0; i < images.count(); i++) {
381         if (images.at(i).toElement().hasAttribute("url"))
382             result.append(images.at(i).toElement().attribute("url"));
383     }
384     return result;
385 }
386
387
388 //virtual
389 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
390 {
391     //slotAdjustZoom();
392 }
393
394 void TitleWidget::slotTextTool()
395 {
396     m_scene->setTool(TITLE_TEXT);
397     showToolbars(TITLE_TEXT);
398     checkButton(TITLE_TEXT);
399 }
400
401 void TitleWidget::slotRectTool()
402 {
403     m_scene->setTool(TITLE_RECTANGLE);
404     showToolbars(TITLE_RECTANGLE);
405     checkButton(TITLE_RECTANGLE);
406 }
407
408 void TitleWidget::slotSelectTool()
409 {
410     m_scene->setTool(TITLE_SELECT);
411
412     // Find out which toolbars need to be shown, depending on selected item
413     TITLETOOL t = TITLE_SELECT;
414     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
415     if (l.size() > 0) {
416         switch (l.at(0)->type()) {
417         case TEXTITEM:
418             t = TITLE_TEXT;
419             break;
420         case RECTITEM:
421             t = TITLE_RECTANGLE;
422             break;
423         case IMAGEITEM:
424             t = TITLE_IMAGE;
425             break;
426         }
427     }
428
429     enableToolbars(t);
430     if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
431         //graphicsView->centerOn(l.at(0));
432         t = TITLE_SELECT;
433     }
434     showToolbars(t);
435
436     if (l.size() > 0) {
437         updateCoordinates(l.at(0));
438         updateDimension(l.at(0));
439         updateRotZoom(l.at(0));
440     }
441
442     checkButton(TITLE_SELECT);
443 }
444
445 void TitleWidget::slotImageTool()
446 {
447     // TODO: find a way to get a list of all supported image types...
448     QString allExtensions = "image/gif image/jpeg image/png image/x-tga image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr";
449     KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
450     if (!url.isEmpty()) {
451         if (url.path().endsWith(".svg")) {
452             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
453             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
454             svg->setZValue(m_count++);
455             svg->setData(Qt::UserRole, url.path());
456             graphicsView->scene()->addItem(svg);
457         } else {
458             QPixmap pix(url.path());
459             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
460             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
461             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
462             image->setData(Qt::UserRole, url.path());
463             image->setZValue(m_count++);
464             graphicsView->scene()->addItem(image);
465         }
466     }
467     m_scene->setTool(TITLE_SELECT);
468     showToolbars(TITLE_SELECT);
469     checkButton(TITLE_SELECT);
470 }
471
472 void TitleWidget::showToolbars(TITLETOOL toolType)
473 {
474     toolbar_stack->setCurrentIndex((int) toolType);
475 }
476
477 void TitleWidget::enableToolbars(TITLETOOL toolType)
478 {
479     // TITLETOOL is defined in graphicsscenerectmove.h
480     bool enable = false;
481     if (toolType == TITLE_RECTANGLE || toolType == TITLE_IMAGE) enable = true;
482     value_w->setEnabled(enable);
483     value_h->setEnabled(enable);
484 }
485
486 void TitleWidget::checkButton(TITLETOOL toolType)
487 {
488     bool bSelect = false;
489     bool bText = false;
490     bool bRect = false;
491     bool bImage = false;
492
493     switch (toolType) {
494     case TITLE_SELECT:
495         bSelect = true;
496         break;
497     case TITLE_TEXT:
498         bText = true;
499         break;
500     case TITLE_RECTANGLE:
501         bRect = true;
502         break;
503     case TITLE_IMAGE:
504         bImage = true;
505         break;
506     default:
507         break;
508     }
509
510     m_buttonCursor->setChecked(bSelect);
511     m_buttonText->setChecked(bText);
512     m_buttonRect->setChecked(bRect);
513     m_buttonImage->setChecked(bImage);
514 }
515
516 void TitleWidget::displayBackgroundFrame()
517 {
518     if (!displayBg->isChecked()) {
519         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
520         QPixmap pattern(20, 20);
521         pattern.fill();
522         QColor bgcolor(210, 210, 210);
523         QPainter p;
524         p.begin(&pattern);
525         p.fillRect(QRect(0, 0, 10, 10), bgcolor);
526         p.fillRect(QRect(10, 10, 20, 20), bgcolor);
527         p.end();
528         QBrush br(pattern);
529
530         p.begin(&bg);
531         p.fillRect(bg.rect(), br);
532         p.end();
533         m_frameImage->setPixmap(bg);
534     } else {
535         m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
536     }
537 }
538
539 void TitleWidget::initAnimation()
540 {
541     align_box->setEnabled(false);
542     QPen startpen(Qt::DotLine);
543     QPen endpen(Qt::DashDotLine);
544     startpen.setColor(QColor(100, 200, 100, 140));
545     endpen.setColor(QColor(200, 100, 100, 140));
546
547     m_startViewport->setPen(startpen);
548     m_endViewport->setPen(endpen);
549
550     m_startViewport->setZValue(-1000);
551     m_endViewport->setZValue(-1000);
552
553     m_startViewport->setFlags(0);
554     m_endViewport->setFlags(0);
555
556     graphicsView->scene()->addItem(m_startViewport);
557     graphicsView->scene()->addItem(m_endViewport);
558
559     connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
560     connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
561     connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
562     connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
563 }
564
565 void TitleWidget::slotUpdateZoom(int pos)
566 {
567     m_scene->setZoom((double) pos / 100);
568     zoom_label->setText(QString::number(pos) + '%');
569 }
570
571 void TitleWidget::slotZoom(bool up)
572 {
573     int pos = zoom_slider->value();
574     if (up) pos++;
575     else pos--;
576     zoom_slider->setValue(pos);
577 }
578
579 void TitleWidget::slotAdjustZoom()
580 {
581     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
582     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
583     if (scalex > scaley) scalex = scaley;
584     int zoompos = (int)(scalex * 7 + 0.5);*/
585     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
586     int zoompos = graphicsView->matrix().m11() * 100;
587     zoom_slider->setValue(zoompos);
588     graphicsView->centerOn(m_frameBorder);
589 }
590
591 void TitleWidget::slotZoomOneToOne()
592 {
593     zoom_slider->setValue(100);
594     graphicsView->centerOn(m_frameBorder);
595 }
596
597 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
598 {
599     updateAxisButtons(rect); // back to default
600
601     QColor f = rectFColor->color();
602     f.setAlpha(rectFAlpha->value());
603     QPen penf(f);
604     penf.setWidth(rectLineWidth->value());
605     rect->setPen(penf);
606     QColor b = rectBColor->color();
607     b.setAlpha(rectBAlpha->value());
608     rect->setBrush(QBrush(b));
609     rect->setZValue(m_count++);
610     //setCurrentItem(rect);
611     //graphicsView->setFocus();
612 }
613
614 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
615 {
616     updateAxisButtons(tt); // back to default
617
618     QFont font = font_family->currentFont();
619     font.setPixelSize(font_size->value());
620     // mbd: issue 551:
621     font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
622     font.setItalic(buttonItalic->isChecked());
623     font.setUnderline(buttonUnder->isChecked());
624
625     tt->setFont(font);
626     QColor color = fontColorButton->color();
627     color.setAlpha(textAlpha->value());
628     tt->setDefaultTextColor(color);
629     tt->setZValue(m_count++);
630     setCurrentItem(tt);
631 }
632
633 void TitleWidget::setFontBoxWeight(int weight)
634 {
635     int index = font_weight_box->findData(weight);
636     if (index < 0) {
637         index = font_weight_box->findData(QFont::Normal);
638     }
639     font_weight_box->setCurrentIndex(index);
640 }
641
642 void TitleWidget::setCurrentItem(QGraphicsItem *item)
643 {
644     m_scene->setSelectedItem(item);
645 }
646
647 void TitleWidget::zIndexChanged(int v)
648 {
649     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
650     if (l.size() >= 1) {
651         l[0]->setZValue(v);
652     }
653 }
654
655 void TitleWidget::selectionChanged()
656 {
657     if (m_scene->tool() != TITLE_SELECT) return;
658     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
659     //toolBox->setItemEnabled(2, false);
660     //toolBox->setItemEnabled(3, false);
661     value_x->blockSignals(true);
662     value_y->blockSignals(true);
663     value_w->blockSignals(true);
664     value_h->blockSignals(true);
665     itemzoom->blockSignals(true);
666     itemrotate->blockSignals(true);
667     if (l.size() == 0) {
668         effect_stack->setHidden(true);
669         effect_frame->setEnabled(false);
670         effect_list->setCurrentIndex(0);
671         bool blockX = !origin_x_left->signalsBlocked();
672         bool blockY = !origin_y_top->signalsBlocked();
673         if (blockX) origin_x_left->blockSignals(true);
674         if (blockY) origin_y_top->blockSignals(true);
675         origin_x_left->setChecked(false);
676         origin_y_top->setChecked(false);
677         updateTextOriginX();
678         updateTextOriginY();
679         enableToolbars(TITLE_SELECT);
680         if (blockX) origin_x_left->blockSignals(false);
681         if (blockY) origin_y_top->blockSignals(false);
682         itemzoom->setEnabled(false);
683         itemrotate->setEnabled(false);
684         frame_properties->setEnabled(false);
685     } else if (l.size() == 1) {
686         effect_frame->setEnabled(true);
687         frame_properties->setEnabled(true);
688         if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
689             itemzoom->setEnabled(true);
690             itemrotate->setEnabled(true);
691         } else {
692             itemzoom->setEnabled(false);
693             itemrotate->setEnabled(false);
694             updateInfoText();
695         }
696         if (l.at(0)->type() == TEXTITEM) {
697             showToolbars(TITLE_TEXT);
698             QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
699             if (!i->data(100).isNull()) {
700                 // Item has an effect
701                 QStringList effdata = i->data(100).toStringList();
702                 if (effdata.at(0) == "typewriter") {
703                     typewriter_delay->setValue(effdata.at(1).toInt());
704                     effect_list->setCurrentIndex(effect_list->findData((int) TYPEWRITEREFFECT));
705                     effect_stack->setHidden(false);
706                 }
707             } else {
708                 if (i->graphicsEffect()) {
709                     QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(i->graphicsEffect());
710                     if (blur) {
711                         effect_list->setCurrentIndex(effect_list->findData((int) BLUREFFECT));
712                         int rad = (int) blur->blurRadius();
713                         blur_radius->setValue(rad);
714                         effect_stack->setHidden(false);
715                     } else {
716                         QGraphicsDropShadowEffect *shad = static_cast <QGraphicsDropShadowEffect *>(i->graphicsEffect());
717                         if (shad) {
718                             effect_list->setCurrentIndex(effect_list->findData((int) SHADOWEFFECT));
719                             shadow_radius->setValue(shad->blurRadius());
720                             shadow_x->setValue(shad->xOffset());
721                             shadow_y->setValue(shad->yOffset());
722                             effect_stack->setHidden(false);
723                         }
724                     }
725                 } else effect_stack->setHidden(true);
726             }
727             //if (l[0]->hasFocus())
728             //toolBox->setCurrentIndex(0);
729             //toolBox->setItemEnabled(2, true);
730             font_size->blockSignals(true);
731             font_family->blockSignals(true);
732             font_weight_box->blockSignals(true);
733             buttonItalic->blockSignals(true);
734             buttonUnder->blockSignals(true);
735             fontColorButton->blockSignals(true);
736             textAlpha->blockSignals(true);
737             buttonAlignLeft->blockSignals(true);
738             buttonAlignRight->blockSignals(true);
739             buttonAlignNone->blockSignals(true);
740             buttonAlignCenter->blockSignals(true);
741
742             QFont font = i->font();
743             font_family->setCurrentFont(font);
744             font_size->setValue(font.pixelSize());
745             buttonItalic->setChecked(font.italic());
746             buttonUnder->setChecked(font.underline());
747             setFontBoxWeight(font.weight());
748
749             QColor color = i->defaultTextColor();
750             fontColorButton->setColor(color);
751             textAlpha->setValue(color.alpha());
752
753             QTextCursor cur = i->textCursor();
754             QTextBlockFormat format = cur.blockFormat();
755             if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
756             else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
757             else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
758             else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
759
760             font_size->blockSignals(false);
761             font_family->blockSignals(false);
762             font_weight_box->blockSignals(false);
763             buttonItalic->blockSignals(false);
764             buttonUnder->blockSignals(false);
765             fontColorButton->blockSignals(false);
766             textAlpha->blockSignals(false);
767             buttonAlignLeft->blockSignals(false);
768             buttonAlignRight->blockSignals(false);
769             buttonAlignNone->blockSignals(false);
770             buttonAlignCenter->blockSignals(false);
771
772             updateAxisButtons(i);
773             updateCoordinates(i);
774             updateDimension(i);
775             enableToolbars(TITLE_TEXT);
776
777         } else if ((l.at(0))->type() == RECTITEM) {
778             showToolbars(TITLE_RECTANGLE);
779             settingUp = true;
780             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
781             if (rec == m_startViewport || rec == m_endViewport) {
782                 /*toolBox->setCurrentIndex(3);
783                 toolBox->widget(0)->setEnabled(false);
784                 toolBox->widget(1)->setEnabled(false);*/
785                 enableToolbars(TITLE_SELECT);
786             } else {
787                 /*toolBox->widget(0)->setEnabled(true);
788                 toolBox->widget(1)->setEnabled(true);
789                 toolBox->setCurrentIndex(0);*/
790                 //toolBox->setItemEnabled(3, true);
791                 rectFAlpha->setValue(rec->pen().color().alpha());
792                 rectBAlpha->setValue(rec->brush().color().alpha());
793                 //kDebug() << rec->brush().color().alpha();
794                 QColor fcol = rec->pen().color();
795                 QColor bcol = rec->brush().color();
796                 //fcol.setAlpha(255);
797                 //bcol.setAlpha(255);
798                 rectFColor->setColor(fcol);
799                 rectBColor->setColor(bcol);
800                 settingUp = false;
801                 rectLineWidth->setValue(rec->pen().width());
802                 enableToolbars(TITLE_RECTANGLE);
803             }
804
805             updateAxisButtons(l.at(0));
806             updateCoordinates(rec);
807             updateDimension(rec);
808
809         } else if (l.at(0)->type() == IMAGEITEM) {
810             showToolbars(TITLE_IMAGE);
811
812             updateCoordinates(l.at(0));
813             updateDimension(l.at(0));
814
815             enableToolbars(TITLE_IMAGE);
816
817         } else {
818             //toolBox->setCurrentIndex(0);
819             showToolbars(TITLE_SELECT);
820             enableToolbars(TITLE_SELECT);
821             frame_properties->setEnabled(false);
822         }
823         zValue->setValue((int)l.at(0)->zValue());
824         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
825         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
826         value_x->blockSignals(false);
827         value_y->blockSignals(false);
828         value_w->blockSignals(false);
829         value_h->blockSignals(false);
830         itemzoom->blockSignals(false);
831         itemrotate->blockSignals(false);
832     }
833 }
834
835 void TitleWidget::slotValueChanged(int type)
836 {
837     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
838     if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
839
840         int val = 0;
841         switch (type) {
842         case ValueWidth:
843             val = value_w->value();
844             break;
845         case ValueHeight:
846             val = value_h->value();
847             break;
848         }
849
850         QGraphicsItem *i = l.at(0);
851         Transform t = m_transformations.value(i);
852
853         // Ratio width:height
854         double phi = (double) i->boundingRect().width() / i->boundingRect().height();
855         double alpha = (double) t.rotate / 180.0 * M_PI;
856
857         // New length
858         double length = val;
859
860         // Scaling factor
861         double scale = 1;
862
863         switch (type) {
864         case ValueWidth:
865             // Add 0.5 because otherwise incrementing by 1 might have no effect
866             length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
867             scale = length / i->boundingRect().width();
868             break;
869         case ValueHeight:
870             length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
871             scale = length / i->boundingRect().height();
872             break;
873         }
874
875         t.scalex = scale;
876         t.scaley = scale;
877         QTransform qtrans;
878         qtrans.scale(scale, scale);
879         qtrans.rotate(t.rotate);
880         i->setTransform(qtrans);
881         m_transformations[i] = t;
882
883         updateDimension(i);
884         updateRotZoom(i);
885     }
886 }
887
888 /** \brief Updates position/size of the selected item when a value
889  * of an item (coordinates, size) has changed */
890 void TitleWidget::slotAdjustSelectedItem()
891 {
892     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
893     if (l.size() >= 1) {
894         if (l.at(0)->type() == RECTITEM) {
895             //rect item
896             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
897             updatePosition(rec);
898             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
899         } else if (l.at(0)->type() == TEXTITEM) {
900             //text item
901             updatePosition(l.at(0));
902         } else if (l.at(0)->type() == IMAGEITEM) {
903             //image item
904             updatePosition(l.at(0));
905         }
906     }
907 }
908
909 /** \brief Updates width/height int the text fields, regarding transformation matrix */
910 void TitleWidget::updateDimension(QGraphicsItem *i)
911 {
912     value_w->blockSignals(true);
913     value_h->blockSignals(true);
914
915
916     if (i->type() == IMAGEITEM) {
917         // Get multipliers for rotation/scaling
918
919         /*Transform t = m_transformations.value(i);
920         QRectF r = i->boundingRect();
921         int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
922                     + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
923         int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
924                     + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
925
926         value_w->setValue(i->sceneBoundingRect().width());
927         value_h->setValue(i->sceneBoundingRect().height());
928     } else if (i->type() == RECTITEM) {
929         QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
930         value_w->setValue((int) r->rect().width());
931         value_h->setValue((int) r->rect().height());
932     } else if (i->type() == TEXTITEM) {
933         QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
934         value_w->setValue((int) t->boundingRect().width());
935         value_h->setValue((int) t->boundingRect().height());
936     }
937
938     value_w->blockSignals(false);
939     value_h->blockSignals(false);
940 }
941
942 /** \brief Updates the coordinates in the text fields from the item */
943 void TitleWidget::updateCoordinates(QGraphicsItem *i)
944 {
945     // Block signals emitted by this method
946     value_x->blockSignals(true);
947     value_y->blockSignals(true);
948
949     if (i->type() == TEXTITEM) {
950
951         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
952
953         // Set the correct x coordinate value
954         if (origin_x_left->isChecked()) {
955             // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
956             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
957         } else {
958             // Origin is at 0 (default)
959             value_x->setValue((int) rec->pos().x());
960         }
961
962         // Same for y
963         if (origin_y_top->isChecked()) {
964             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
965         } else {
966             value_y->setValue((int) rec->pos().y());
967         }
968
969     } else if (i->type() == RECTITEM) {
970
971         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
972
973         if (origin_x_left->isChecked()) {
974             // Origin (0 point) is at m_frameWidth
975             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
976         } else {
977             // Origin is at 0 (default)
978             value_x->setValue((int) rec->pos().x());
979         }
980
981         if (origin_y_top->isChecked()) {
982             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
983         } else {
984             value_y->setValue((int) rec->pos().y());
985         }
986
987     } else if (i->type() == IMAGEITEM) {
988
989         if (origin_x_left->isChecked()) {
990             value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
991         } else {
992             value_x->setValue((int) i->pos().x());
993         }
994
995         if (origin_y_top->isChecked()) {
996             value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
997         } else {
998             value_y->setValue((int) i->pos().y());
999         }
1000
1001     }
1002
1003     // Stop blocking signals now
1004     value_x->blockSignals(false);
1005     value_y->blockSignals(false);
1006 }
1007
1008 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1009 {
1010     itemzoom->blockSignals(true);
1011     itemrotate->blockSignals(false);
1012
1013     Transform t = m_transformations.value(i);
1014     itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1015     itemrotate->setValue((int)(t.rotate));
1016
1017     itemzoom->blockSignals(false);
1018     itemrotate->blockSignals(false);
1019 }
1020
1021 /** \brief Updates the position of an item by reading coordinates from the text fields */
1022 void TitleWidget::updatePosition(QGraphicsItem *i)
1023 {
1024     if (i->type() == TEXTITEM) {
1025         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1026
1027         int posX;
1028         if (origin_x_left->isChecked()) {
1029             /* Origin of the x axis is at m_frameWidth,
1030              * and distance from right border of the item to the right
1031              * border of the frame is taken.
1032              * See comment to slotOriginXClicked().
1033              */
1034             posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1035         } else {
1036             posX = value_x->value();
1037         }
1038
1039         int posY;
1040         if (origin_y_top->isChecked()) {
1041             /* Same for y axis */
1042             posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1043         } else {
1044             posY = value_y->value();
1045         }
1046
1047         rec->setPos(posX, posY);
1048
1049     } else if (i->type() == RECTITEM) {
1050
1051         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1052
1053         int posX;
1054         if (origin_x_left->isChecked()) {
1055             posX = m_frameWidth - value_x->value() - rec->rect().width();
1056         } else {
1057             posX = value_x->value();
1058         }
1059
1060         int posY;
1061         if (origin_y_top->isChecked()) {
1062             posY = m_frameHeight - value_y->value() - rec->rect().height();
1063         } else {
1064             posY = value_y->value();
1065         }
1066
1067         rec->setPos(posX, posY);
1068
1069     } else if (i->type() == IMAGEITEM) {
1070         int posX;
1071         if (origin_x_left->isChecked()) {
1072             // Use the sceneBoundingRect because this also regards transformations like zoom
1073             posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1074         } else {
1075             posX = value_x->value();
1076         }
1077
1078         int posY;
1079         if (origin_y_top->isChecked()) {
1080             posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1081         } else {
1082             posY = value_y->value();
1083         }
1084
1085         i->setPos(posX, posY);
1086
1087     }
1088
1089 }
1090
1091 void TitleWidget::updateTextOriginX()
1092 {
1093     if (origin_x_left->isChecked()) {
1094         origin_x_left->setText(i18n("\u2212X"));
1095     } else {
1096         origin_x_left->setText(i18n("+X"));
1097     }
1098 }
1099
1100 void TitleWidget::slotOriginXClicked()
1101 {
1102     // Update the text displayed on the button.
1103     updateTextOriginX();
1104
1105     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1106     if (l.size() >= 1) {
1107         updateCoordinates(l.at(0));
1108
1109         // Remember x axis setting
1110         l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1111                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1112     }
1113     graphicsView->setFocus();
1114 }
1115
1116 void TitleWidget::updateTextOriginY()
1117 {
1118     if (origin_y_top->isChecked()) {
1119         origin_y_top->setText(i18n("\u2212Y"));
1120     } else {
1121         origin_y_top->setText(i18n("+Y"));
1122     }
1123 }
1124
1125 void TitleWidget::slotOriginYClicked()
1126 {
1127     // Update the text displayed on the button.
1128     updateTextOriginY();
1129
1130     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1131     if (l.size() >= 1) {
1132         updateCoordinates(l.at(0));
1133
1134         l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1135                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1136
1137     }
1138     graphicsView->setFocus();
1139 }
1140
1141 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1142 {
1143     int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1144     int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1145     origin_x_left->blockSignals(true);
1146     origin_y_top->blockSignals(true);
1147
1148     if (xAxis == TitleDocument::AxisInverted) {
1149         origin_x_left->setChecked(true);
1150     } else {
1151         origin_x_left->setChecked(false);
1152     }
1153     updateTextOriginX();
1154
1155     if (yAxis == TitleDocument::AxisInverted) {
1156         origin_y_top->setChecked(true);
1157     } else {
1158         origin_y_top->setChecked(false);
1159     }
1160     updateTextOriginY();
1161
1162     origin_x_left->blockSignals(false);
1163     origin_y_top->blockSignals(false);
1164 }
1165
1166 void TitleWidget::slotChangeBackground()
1167 {
1168     QColor color = kcolorbutton->color();
1169     color.setAlpha(horizontalSlider->value());
1170     m_frameBorder->setBrush(QBrush(color));
1171 }
1172
1173 /**
1174  * Something (yeah) has changed in our QGraphicsScene.
1175  */
1176 void TitleWidget::slotChanged()
1177 {
1178     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1179     if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1180         textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1181     }
1182 }
1183
1184 /**
1185  * If the user has set origin_x_left (everything also for y),
1186  * we need to look whether a text element has been selected. If yes,
1187  * we need to ensure that the right border of the text field
1188  * remains fixed also when some text has been entered.
1189  *
1190  * This is also known as right-justified, with the difference that
1191  * it is not valid for text but for its boundingRect. Text may still
1192  * be left-justified.
1193  */
1194 void TitleWidget::textChanged(QGraphicsTextItem *i)
1195 {
1196
1197     updateDimension(i);
1198
1199     if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1200
1201         if (!i->toPlainText().isEmpty()) {
1202             updatePosition(i);
1203         } else {
1204             /*
1205              * Don't do anything if the string is empty. If the position
1206              * would be updated here, a newly created text field would
1207              * be set to the position of the last selected text field.
1208              */
1209         }
1210     }
1211 }
1212
1213 void TitleWidget::slotInsertUnicode()
1214 {
1215     m_unicodeDialog->exec();
1216 }
1217
1218 void TitleWidget::slotInsertUnicodeString(QString text)
1219 {
1220     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1221     if (l.size() > 0) {
1222         if (l.at(0)->type() == TEXTITEM) {
1223             QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1224             t->textCursor().insertText(text);
1225         }
1226     }
1227 }
1228
1229 void TitleWidget::slotUpdateText()
1230 {
1231     QFont font = font_family->currentFont();
1232     font.setPixelSize(font_size->value());
1233     font.setItalic(buttonItalic->isChecked());
1234     font.setUnderline(buttonUnder->isChecked());
1235     font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1236     QColor color = fontColorButton->color();
1237     color.setAlpha(textAlpha->value());
1238
1239     QGraphicsTextItem* item = NULL;
1240     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1241     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1242         item = static_cast <QGraphicsTextItem *>(l.at(0));
1243     }
1244     if (!item) return;
1245     //if (item->textCursor().selection ().isEmpty())
1246     QTextCursor cur = item->textCursor();
1247     QTextBlockFormat format = cur.blockFormat();
1248     if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1249         item->setTextWidth(item->boundingRect().width());
1250         if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1251         else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1252         else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1253     } else {
1254         format.setAlignment(Qt::AlignLeft);
1255         item->setTextWidth(-1);
1256     }
1257
1258     {
1259         item->setFont(font);
1260         item->setDefaultTextColor(color);
1261         cur.select(QTextCursor::Document);
1262         cur.setBlockFormat(format);
1263         item->setTextCursor(cur);
1264         cur.clearSelection();
1265         item->setTextCursor(cur);
1266
1267     }
1268 }
1269
1270 void TitleWidget::rectChanged()
1271 {
1272     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1273     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1274         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1275         QColor f = rectFColor->color();
1276         f.setAlpha(rectFAlpha->value());
1277         QPen penf(f);
1278         penf.setWidth(rectLineWidth->value());
1279         rec->setPen(penf);
1280         QColor b = rectBColor->color();
1281         b.setAlpha(rectBAlpha->value());
1282         rec->setBrush(QBrush(b));
1283     }
1284 }
1285
1286 void TitleWidget::itemScaled(int val)
1287 {
1288     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1289     if (l.size() == 1) {
1290         Transform x = m_transformations.value(l.at(0));
1291         x.scalex = (double)val / 100.0;
1292         x.scaley = (double)val / 100.0;
1293         QTransform qtrans;
1294         qtrans.scale(x.scalex, x.scaley);
1295         qtrans.rotate(x.rotate);
1296         l[0]->setTransform(qtrans);
1297         m_transformations[l.at(0)] = x;
1298         updateDimension(l.at(0));
1299     }
1300 }
1301
1302 void TitleWidget::itemRotate(int val)
1303 {
1304     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1305     if (l.size() == 1) {
1306         Transform x = m_transformations[l.at(0)];
1307         x.rotate = (double)val;
1308         QTransform qtrans;
1309         qtrans.scale(x.scalex, x.scaley);
1310         qtrans.rotate(x.rotate);
1311         l[0]->setTransform(qtrans);
1312         m_transformations[l.at(0)] = x;
1313         updateDimension(l.at(0));
1314     }
1315 }
1316
1317 void TitleWidget::itemHCenter()
1318 {
1319     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1320     if (l.size() == 1) {
1321         QGraphicsItem *item = l.at(0);
1322         QRectF br = item->sceneBoundingRect();
1323         int width = (int)br.width();
1324         int newPos = (int)((m_frameWidth - width) / 2);
1325         newPos += item->pos().x() - br.left(); // Check item transformation
1326         item->setPos(newPos, item->pos().y());
1327         updateCoordinates(item);
1328     }
1329 }
1330
1331 void TitleWidget::itemVCenter()
1332 {
1333     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1334     if (l.size() == 1) {
1335         QGraphicsItem *item = l.at(0);
1336         QRectF br = item->sceneBoundingRect();
1337         int height = (int)br.height();
1338         int newPos = (int)((m_frameHeight - height) / 2);
1339         newPos += item->pos().y() - br.top(); // Check item transformation
1340         item->setPos(item->pos().x(), newPos);
1341         updateCoordinates(item);
1342     }
1343 }
1344
1345 void TitleWidget::itemTop()
1346 {
1347     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1348     if (l.size() == 1) {
1349         QGraphicsItem *item = l.at(0);
1350         QRectF br = item->sceneBoundingRect();
1351         double diff;
1352         if (br.top() > 0) diff = -br.top();
1353         else diff = -br.bottom();
1354         item->moveBy(0, diff);
1355         updateCoordinates(item);
1356     }
1357 }
1358
1359 void TitleWidget::itemBottom()
1360 {
1361     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1362     if (l.size() == 1) {
1363         QGraphicsItem *item = l.at(0);
1364         QRectF br = item->sceneBoundingRect();
1365         double diff;
1366         if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1367         else diff = m_frameHeight - br.bottom();
1368         item->moveBy(0, diff);
1369         updateCoordinates(item);
1370     }
1371 }
1372
1373 void TitleWidget::itemLeft()
1374 {
1375     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1376     if (l.size() == 1) {
1377         QGraphicsItem *item = l.at(0);
1378         QRectF br = item->sceneBoundingRect();
1379         double diff;
1380         if (br.left() > 0) diff = -br.left();
1381         else diff = -br.right();
1382         item->moveBy(diff, 0);
1383         updateCoordinates(item);
1384     }
1385 }
1386
1387 void TitleWidget::itemRight()
1388 {
1389     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1390     if (l.size() == 1) {
1391         QGraphicsItem *item = l.at(0);
1392         QRectF br = item->sceneBoundingRect();
1393         double diff;
1394         if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1395         else diff = m_frameWidth - br.left();
1396         item->moveBy(diff, 0);
1397         updateCoordinates(item);
1398     }
1399 }
1400
1401 void TitleWidget::setupViewports()
1402 {
1403     //double aspect_ratio = 4.0 / 3.0;//read from project
1404     //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1405     /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1406     QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1407     // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1408     QPolygonF spoly(sp);
1409     QPolygonF epoly(ep);
1410     spoly.translate(startViewportX->value(), startViewportY->value());
1411     epoly.translate(endViewportX->value(), endViewportY->value());
1412     m_startViewport->setPolygon(spoly);
1413     m_endViewport->setPolygon(epoly);
1414     if (! insertingValues) {
1415         m_startViewport->setData(0, startViewportX->value());
1416         m_startViewport->setData(1, startViewportY->value());
1417         m_startViewport->setData(2, startViewportSize->value());
1418
1419         m_endViewport->setData(0, endViewportX->value());
1420         m_endViewport->setData(1, endViewportY->value());
1421         m_endViewport->setData(2, endViewportSize->value());
1422     }*/
1423 }
1424
1425 void TitleWidget::loadTitle(KUrl url)
1426 {
1427     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1428     if (!url.isEmpty()) {
1429         QList<QGraphicsItem *> items = m_scene->items();
1430         for (int i = 0; i < items.size(); i++) {
1431             if (items.at(i)->zValue() > -1000) delete items.at(i);
1432         }
1433         m_scene->clearTextSelection();
1434         QDomDocument doc;
1435         QString tmpfile;
1436
1437         if (KIO::NetAccess::download(url, tmpfile, 0)) {
1438             QFile file(tmpfile);
1439             if (file.open(QIODevice::ReadOnly)) {
1440                 doc.setContent(&file, false);
1441                 file.close();
1442             } else return;
1443             KIO::NetAccess::removeTempFile(tmpfile);
1444         }
1445         setXml(doc);
1446
1447         /*int out;
1448         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1449         adjustFrameSize();
1450         title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1451         insertingValues = true;
1452         startViewportX->setValue(m_startViewport->data(0).toInt());
1453         startViewportY->setValue(m_startViewport->data(1).toInt());
1454         startViewportSize->setValue(m_startViewport->data(2).toInt());
1455         endViewportX->setValue(m_endViewport->data(0).toInt());
1456         endViewportY->setValue(m_endViewport->data(1).toInt());
1457         endViewportSize->setValue(m_endViewport->data(2).toInt());
1458
1459         insertingValues = false;
1460         slotSelectTool();
1461         slotAdjustZoom();*/
1462     }
1463 }
1464
1465 void TitleWidget::saveTitle(KUrl url)
1466 {
1467     if (anim_start->isChecked()) slotAnimStart(false);
1468     if (anim_end->isChecked()) slotAnimEnd(false);
1469     if (url.isEmpty()) {
1470         KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1471         fs->setOperationMode(KFileDialog::Saving);
1472         fs->setMode(KFile::File);
1473 #if KDE_IS_VERSION(4,2,0)
1474         fs->setConfirmOverwrite(true);
1475 #endif
1476         fs->setKeepLocation(true);
1477         fs->exec();
1478         url = fs->selectedUrl();
1479         delete fs;
1480     }
1481     if (!url.isEmpty()) {
1482         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1483             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1484     }
1485 }
1486
1487 QDomDocument TitleWidget::xml()
1488 {
1489     QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1490     doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1491     return doc;
1492 }
1493
1494 int TitleWidget::duration() const
1495 {
1496     return m_tc.getFrameCount(title_duration->text());
1497 }
1498
1499 void TitleWidget::setXml(QDomDocument doc)
1500 {
1501     int out;
1502     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1503     adjustFrameSize();
1504     title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1505     /*if (doc.documentElement().hasAttribute("out")) {
1506     GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1507     title_duration->setText(m_tc.getTimecode(duration));
1508     }
1509     else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1510
1511     QDomElement e = doc.documentElement();
1512     m_transformations.clear();
1513     QList <QGraphicsItem *> items = graphicsView->scene()->items();
1514     const double PI = 4.0 * atan(1.0);
1515     for (int i = 0; i < items.count(); i++) {
1516         QTransform t = items.at(i)->transform();
1517         Transform x;
1518         x.scalex = t.m11();
1519         x.scaley = t.m22();
1520         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1521         m_transformations[items.at(i)] = x;
1522     }
1523     // mbd: Update the GUI color selectors to match the stuff from the loaded document
1524     QColor background_color = m_titledocument.getBackgroundColor();
1525     horizontalSlider->blockSignals(true);
1526     kcolorbutton->blockSignals(true);
1527     horizontalSlider->setValue(background_color.alpha());
1528     background_color.setAlpha(255);
1529     kcolorbutton->setColor(background_color);
1530     horizontalSlider->blockSignals(false);
1531     kcolorbutton->blockSignals(false);
1532
1533     /*startViewportX->setValue(m_startViewport->data(0).toInt());
1534     startViewportY->setValue(m_startViewport->data(1).toInt());
1535     startViewportSize->setValue(m_startViewport->data(2).toInt());
1536     endViewportX->setValue(m_endViewport->data(0).toInt());
1537     endViewportY->setValue(m_endViewport->data(1).toInt());
1538     endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1539
1540     QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1541     slotSelectTool();
1542 }
1543
1544 /** \brief Connected to the accepted signal - calls writeChoices */
1545 void TitleWidget::slotAccepted()
1546 {
1547     if (anim_start->isChecked()) slotAnimStart(false);
1548     if (anim_end->isChecked()) slotAnimEnd(false);
1549     writeChoices();
1550 }
1551
1552 /** \brief Store the current choices of font, background and rect values */
1553 void TitleWidget::writeChoices()
1554 {
1555     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1556     KSharedConfigPtr config = KGlobal::config();
1557     KConfigGroup titleConfig(config, "TitleWidget");
1558     // Write the entries
1559     titleConfig.writeEntry("font_family", font_family->currentFont());
1560     //titleConfig.writeEntry("font_size", font_size->value());
1561     titleConfig.writeEntry("font_pixel_size", font_size->value());
1562     titleConfig.writeEntry("font_color", fontColorButton->color());
1563     titleConfig.writeEntry("font_alpha", textAlpha->value());
1564     titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1565     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1566     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1567
1568     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1569     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1570     titleConfig.writeEntry("rect_background_color", rectBColor->color());
1571     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1572     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1573
1574     titleConfig.writeEntry("background_color", kcolorbutton->color());
1575     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1576
1577     //! \todo Not sure if I should sync - it is probably safe to do it
1578     config->sync();
1579
1580 }
1581
1582 /** \brief Read the last stored choices into the dialog */
1583 void TitleWidget::readChoices()
1584 {
1585     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1586     KSharedConfigPtr config = KGlobal::config();
1587     KConfigGroup titleConfig(config, "TitleWidget");
1588     // read the entries
1589     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1590     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1591     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1592     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1593     int weight;
1594     if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1595     else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1596     setFontBoxWeight(weight);
1597     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1598     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1599
1600     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1601     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1602     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1603     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1604     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1605
1606     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1607     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1608 }
1609
1610 void TitleWidget::adjustFrameSize()
1611 {
1612     m_frameWidth = m_titledocument.frameWidth();
1613     m_frameHeight = m_titledocument.frameHeight();
1614     m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1615     displayBackgroundFrame();
1616 }
1617
1618 void TitleWidget::slotAnimStart(bool anim)
1619 {
1620     if (anim && anim_end->isChecked()) {
1621         anim_end->setChecked(false);
1622         m_endViewport->setZValue(-1000);
1623         m_endViewport->setBrush(QBrush());
1624     }
1625     slotSelectTool();
1626     QList<QGraphicsItem *> list = m_scene->items();
1627     for (int i = 0; i < list.count(); i++) {
1628         if (list.at(i)->zValue() > -1000) {
1629             list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1630             list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1631         }
1632     }
1633     align_box->setEnabled(anim);
1634     itemzoom->setEnabled(!anim);
1635     itemrotate->setEnabled(!anim);
1636     frame_toolbar->setEnabled(!anim);
1637     toolbar_stack->setEnabled(!anim);
1638     if (anim) {
1639         keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1640         m_startViewport->setZValue(1100);
1641         QColor col = m_startViewport->pen().color();
1642         col.setAlpha(100);
1643         m_startViewport->setBrush(col);
1644         m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1645         m_startViewport->setSelected(true);
1646         selectionChanged();
1647         slotSelectTool();
1648         if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1649     } else {
1650         m_startViewport->setZValue(-1000);
1651         m_startViewport->setBrush(QBrush());
1652         m_startViewport->setFlags(0);
1653         if (!anim_end->isChecked()) deleteAnimInfoText();
1654     }
1655
1656 }
1657
1658 void TitleWidget::slotAnimEnd(bool anim)
1659 {
1660     if (anim && anim_start->isChecked()) {
1661         anim_start->setChecked(false);
1662         m_startViewport->setZValue(-1000);
1663         m_startViewport->setBrush(QBrush());
1664     }
1665     slotSelectTool();
1666     QList<QGraphicsItem *> list = m_scene->items();
1667     for (int i = 0; i < list.count(); i++) {
1668         if (list.at(i)->zValue() > -1000) {
1669             list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1670             list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1671         }
1672     }
1673     align_box->setEnabled(anim);
1674     itemzoom->setEnabled(!anim);
1675     itemrotate->setEnabled(!anim);
1676     frame_toolbar->setEnabled(!anim);
1677     toolbar_stack->setEnabled(!anim);
1678     if (anim) {
1679         keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1680         m_endViewport->setZValue(1100);
1681         QColor col = m_endViewport->pen().color();
1682         col.setAlpha(100);
1683         m_endViewport->setBrush(col);
1684         m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1685         m_endViewport->setSelected(true);
1686         selectionChanged();
1687         slotSelectTool();
1688         if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1689     } else {
1690         m_endViewport->setZValue(-1000);
1691         m_endViewport->setBrush(QBrush());
1692         m_endViewport->setFlags(0);
1693         if (!anim_start->isChecked()) deleteAnimInfoText();
1694     }
1695 }
1696
1697 void TitleWidget::addAnimInfoText()
1698 {
1699     // add text to anim viewport
1700     QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1701     QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1702     QFont font = t->font();
1703     font.setPixelSize(m_startViewport->rect().width() / 10);
1704     QColor col = m_startViewport->pen().color();
1705     col.setAlpha(255);
1706     t->setDefaultTextColor(col);
1707     t->setFont(font);
1708     font.setPixelSize(m_endViewport->rect().width() / 10);
1709     col = m_endViewport->pen().color();
1710     col.setAlpha(255);
1711     t2->setDefaultTextColor(col);
1712     t2->setFont(font);
1713 }
1714
1715 void TitleWidget::updateInfoText()
1716 {
1717     // update info text font
1718     if (!m_startViewport->childItems().isEmpty()) {
1719         QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1720         if (item) {
1721             QFont font = item->font();
1722             font.setPixelSize(m_startViewport->rect().width() / 10);
1723             item->setFont(font);
1724         }
1725     }
1726     if (!m_endViewport->childItems().isEmpty()) {
1727         QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1728         if (item) {
1729             QFont font = item->font();
1730             font.setPixelSize(m_endViewport->rect().width() / 10);
1731             item->setFont(font);
1732         }
1733     }
1734 }
1735
1736 void TitleWidget::deleteAnimInfoText()
1737 {
1738     // end animation editing, remove info text
1739     while (!m_startViewport->childItems().isEmpty()) {
1740         QGraphicsItem *item = m_startViewport->childItems().at(0);
1741         m_scene->removeItem(item);
1742         delete item;
1743     }
1744     while (!m_endViewport->childItems().isEmpty()) {
1745         QGraphicsItem *item = m_endViewport->childItems().at(0);
1746         m_scene->removeItem(item);
1747         delete item;
1748     }
1749 }
1750
1751
1752 void TitleWidget::slotKeepAspect(bool keep)
1753 {
1754     if (m_endViewport->zValue() == 1100) {
1755         m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1756         m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1757     } else {
1758         m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1759         m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1760     }
1761 }
1762
1763 void TitleWidget::slotResize50()
1764 {
1765     if (m_endViewport->zValue() == 1100) {
1766         m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1767     } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1768 }
1769
1770 void TitleWidget::slotResize100()
1771 {
1772     if (m_endViewport->zValue() == 1100) {
1773         m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1774     } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1775 }
1776
1777 void TitleWidget::slotResize200()
1778 {
1779     if (m_endViewport->zValue() == 1100) {
1780         m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1781     } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1782 }
1783
1784 void TitleWidget::slotAddEffect(int ix)
1785 {
1786     int effect = effect_list->itemData(ix).toInt();
1787     if (effect == 0) {
1788         effect_stack->setHidden(true);
1789         return;
1790     }
1791     effect_stack->setCurrentIndex(effect - 1);
1792     effect_stack->setHidden(false);
1793     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1794     if (effect == TYPEWRITEREFFECT) {
1795         if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1796             QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value());
1797             l[0]->setData(100, effdata);
1798         }
1799     }
1800 #if QT_VERSION < 0x040600
1801     return;
1802 #else
1803     if (effect == BLUREFFECT) {
1804         // Blur effect
1805         if (l.size() == 1) {
1806             QGraphicsEffect *eff = new QGraphicsBlurEffect();
1807             l[0]->setGraphicsEffect(eff);
1808         }
1809     } else if (effect == SHADOWEFFECT) {
1810         if (l.size() == 1) {
1811             QGraphicsEffect *eff = new QGraphicsDropShadowEffect();
1812             l[0]->setGraphicsEffect(eff);
1813         }
1814     }
1815
1816 #endif
1817 }
1818
1819
1820 void TitleWidget::slotFontText(const QString& s)
1821 {
1822     const QFont f(s);
1823     if (f.exactMatch()) {
1824         // Font really exists (could also just be a «d» if the user
1825         // starts typing «dejavu» for example).
1826         font_family->setCurrentFont(f);
1827     }
1828     // TODO: typing dejavu serif does not recognize the font (takes sans).
1829     // upper/lowercase problem?
1830 }
1831
1832 void TitleWidget::slotEditTypewriter(int ix)
1833 {
1834     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1835     if (l.size() == 1) {
1836         QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value());
1837         l[0]->setData(100, effdata);
1838     }
1839 }
1840
1841 void TitleWidget::slotEditBlur(int ix)
1842 {
1843 #if QT_VERSION < 0x040600
1844     return;
1845 #else
1846     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1847     if (l.size() == 1) {
1848         QGraphicsEffect *eff = l[0]->graphicsEffect();
1849         QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1850         if (blur) blur->setBlurRadius(ix);
1851     }
1852 #endif
1853 }
1854
1855 void TitleWidget::slotEditShadow()
1856 {
1857 #if QT_VERSION < 0x040600
1858     return;
1859 #else
1860     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1861     if (l.size() == 1) {
1862         QGraphicsEffect *eff = l[0]->graphicsEffect();
1863         QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1864         if (shadow) {
1865             shadow->setBlurRadius(shadow_radius->value());
1866             shadow->setOffset(shadow_x->value(), shadow_y->value());
1867         }
1868     }
1869 #endif
1870 }