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