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