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