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