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