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