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