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