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