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