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