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