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