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