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