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