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