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