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