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