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