]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
Fix display of font / outline color in buttons
[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             textAlpha->setValue(color.alpha());
848             color.setAlpha(255);
849             fontColorButton->setColor(color);
850
851             if (!i->data(101).isNull()) {
852                 textOutline->blockSignals(true);
853                 textOutline->setValue(i->data(101).toDouble()*10);
854                 textOutline->blockSignals(false);
855             }
856             if (!i->data(102).isNull()) {
857                 textOutlineColor->blockSignals(true);
858                 textOutlineAlpha->blockSignals(true);
859                 color = QColor(i->data(102).toString());
860                 textOutlineAlpha->setValue(color.alpha());
861                 color.setAlpha(255);
862                 textOutlineColor->setColor(color);
863                 textOutlineColor->blockSignals(false);
864                 textOutlineAlpha->blockSignals(false);
865             }
866             QTextCursor cur = i->textCursor();
867             QTextBlockFormat format = cur.blockFormat();
868             if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
869             else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
870             else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
871             else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
872
873             font_size->blockSignals(false);
874             font_family->blockSignals(false);
875             font_weight_box->blockSignals(false);
876             buttonItalic->blockSignals(false);
877             buttonUnder->blockSignals(false);
878             fontColorButton->blockSignals(false);
879             textAlpha->blockSignals(false);
880             buttonAlignLeft->blockSignals(false);
881             buttonAlignRight->blockSignals(false);
882             buttonAlignNone->blockSignals(false);
883             buttonAlignCenter->blockSignals(false);
884
885             updateAxisButtons(i);
886             updateCoordinates(i);
887             updateDimension(i);
888             enableToolbars(TITLE_TEXT);
889
890         } else if ((l.at(0))->type() == RECTITEM) {
891             showToolbars(TITLE_RECTANGLE);
892             settingUp = true;
893             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
894             if (rec == m_startViewport || rec == m_endViewport) {
895                 /*toolBox->setCurrentIndex(3);
896                 toolBox->widget(0)->setEnabled(false);
897                 toolBox->widget(1)->setEnabled(false);*/
898                 enableToolbars(TITLE_SELECT);
899             } else {
900                 /*toolBox->widget(0)->setEnabled(true);
901                 toolBox->widget(1)->setEnabled(true);
902                 toolBox->setCurrentIndex(0);*/
903                 //toolBox->setItemEnabled(3, true);
904                 rectFAlpha->setValue(rec->pen().color().alpha());
905                 rectBAlpha->setValue(rec->brush().color().alpha());
906                 //kDebug() << rec->brush().color().alpha();
907                 QColor fcol = rec->pen().color();
908                 QColor bcol = rec->brush().color();
909                 //fcol.setAlpha(255);
910                 //bcol.setAlpha(255);
911                 rectFColor->setColor(fcol);
912                 rectBColor->setColor(bcol);
913                 settingUp = false;
914                 rectLineWidth->setValue(rec->pen().width());
915                 enableToolbars(TITLE_RECTANGLE);
916             }
917
918             updateAxisButtons(l.at(0));
919             updateCoordinates(rec);
920             updateDimension(rec);
921
922         } else if (l.at(0)->type() == IMAGEITEM) {
923             showToolbars(TITLE_IMAGE);
924
925             updateCoordinates(l.at(0));
926             updateDimension(l.at(0));
927
928             enableToolbars(TITLE_IMAGE);
929
930         } else {
931             //toolBox->setCurrentIndex(0);
932             showToolbars(TITLE_SELECT);
933             enableToolbars(TITLE_SELECT);
934             frame_properties->setEnabled(false);
935         }
936         zValue->setValue((int)l.at(0)->zValue());
937         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
938         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
939         value_x->blockSignals(false);
940         value_y->blockSignals(false);
941         value_w->blockSignals(false);
942         value_h->blockSignals(false);
943         itemzoom->blockSignals(false);
944         itemrotate->blockSignals(false);
945     }
946 }
947
948 void TitleWidget::slotValueChanged(int type)
949 {
950     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
951     if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
952
953         int val = 0;
954         switch (type) {
955         case ValueWidth:
956             val = value_w->value();
957             break;
958         case ValueHeight:
959             val = value_h->value();
960             break;
961         }
962
963         QGraphicsItem *i = l.at(0);
964         Transform t = m_transformations.value(i);
965
966         // Ratio width:height
967         double phi = (double) i->boundingRect().width() / i->boundingRect().height();
968         double alpha = (double) t.rotate / 180.0 * M_PI;
969
970         // New length
971         double length = val;
972
973         // Scaling factor
974         double scale = 1;
975
976         switch (type) {
977         case ValueWidth:
978             // Add 0.5 because otherwise incrementing by 1 might have no effect
979             length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
980             scale = length / i->boundingRect().width();
981             break;
982         case ValueHeight:
983             length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
984             scale = length / i->boundingRect().height();
985             break;
986         }
987
988         t.scalex = scale;
989         t.scaley = scale;
990         QTransform qtrans;
991         qtrans.scale(scale, scale);
992         qtrans.rotate(t.rotate);
993         i->setTransform(qtrans);
994         m_transformations[i] = t;
995
996         updateDimension(i);
997         updateRotZoom(i);
998     }
999 }
1000
1001 /** \brief Updates position/size of the selected item when a value
1002  * of an item (coordinates, size) has changed */
1003 void TitleWidget::slotAdjustSelectedItem()
1004 {
1005     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1006     if (l.size() >= 1) {
1007         if (l.at(0)->type() == RECTITEM) {
1008             //rect item
1009             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
1010             updatePosition(rec);
1011             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
1012         } else if (l.at(0)->type() == TEXTITEM) {
1013             //text item
1014             updatePosition(l.at(0));
1015         } else if (l.at(0)->type() == IMAGEITEM) {
1016             //image item
1017             updatePosition(l.at(0));
1018         }
1019     }
1020 }
1021
1022 /** \brief Updates width/height int the text fields, regarding transformation matrix */
1023 void TitleWidget::updateDimension(QGraphicsItem *i)
1024 {
1025     value_w->blockSignals(true);
1026     value_h->blockSignals(true);
1027     zValue->blockSignals(true);
1028
1029     zValue->setValue((int) i->zValue());
1030     if (i->type() == IMAGEITEM) {
1031         // Get multipliers for rotation/scaling
1032
1033         /*Transform t = m_transformations.value(i);
1034         QRectF r = i->boundingRect();
1035         int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
1036                     + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
1037         int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
1038                     + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
1039
1040         value_w->setValue(i->sceneBoundingRect().width());
1041         value_h->setValue(i->sceneBoundingRect().height());
1042     } else if (i->type() == RECTITEM) {
1043         QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
1044         value_w->setValue((int) r->rect().width());
1045         value_h->setValue((int) r->rect().height());
1046     } else if (i->type() == TEXTITEM) {
1047         QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
1048         value_w->setValue((int) t->boundingRect().width());
1049         value_h->setValue((int) t->boundingRect().height());
1050     }
1051
1052     zValue->blockSignals(false);
1053     value_w->blockSignals(false);
1054     value_h->blockSignals(false);
1055 }
1056
1057 /** \brief Updates the coordinates in the text fields from the item */
1058 void TitleWidget::updateCoordinates(QGraphicsItem *i)
1059 {
1060     // Block signals emitted by this method
1061     value_x->blockSignals(true);
1062     value_y->blockSignals(true);
1063
1064     if (i->type() == TEXTITEM) {
1065
1066         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1067
1068         // Set the correct x coordinate value
1069         if (origin_x_left->isChecked()) {
1070             // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
1071             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
1072         } else {
1073             // Origin is at 0 (default)
1074             value_x->setValue((int) rec->pos().x());
1075         }
1076
1077         // Same for y
1078         if (origin_y_top->isChecked()) {
1079             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
1080         } else {
1081             value_y->setValue((int) rec->pos().y());
1082         }
1083
1084     } else if (i->type() == RECTITEM) {
1085
1086         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1087
1088         if (origin_x_left->isChecked()) {
1089             // Origin (0 point) is at m_frameWidth
1090             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
1091         } else {
1092             // Origin is at 0 (default)
1093             value_x->setValue((int) rec->pos().x());
1094         }
1095
1096         if (origin_y_top->isChecked()) {
1097             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
1098         } else {
1099             value_y->setValue((int) rec->pos().y());
1100         }
1101
1102     } else if (i->type() == IMAGEITEM) {
1103
1104         if (origin_x_left->isChecked()) {
1105             value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
1106         } else {
1107             value_x->setValue((int) i->pos().x());
1108         }
1109
1110         if (origin_y_top->isChecked()) {
1111             value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
1112         } else {
1113             value_y->setValue((int) i->pos().y());
1114         }
1115
1116     }
1117
1118     // Stop blocking signals now
1119     value_x->blockSignals(false);
1120     value_y->blockSignals(false);
1121 }
1122
1123 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1124 {
1125     itemzoom->blockSignals(true);
1126     itemrotate->blockSignals(false);
1127
1128     Transform t = m_transformations.value(i);
1129     itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1130     itemrotate->setValue((int)(t.rotate));
1131
1132     itemzoom->blockSignals(false);
1133     itemrotate->blockSignals(false);
1134 }
1135
1136 /** \brief Updates the position of an item by reading coordinates from the text fields */
1137 void TitleWidget::updatePosition(QGraphicsItem *i)
1138 {
1139     if (i->type() == TEXTITEM) {
1140         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1141
1142         int posX;
1143         if (origin_x_left->isChecked()) {
1144             /* Origin of the x axis is at m_frameWidth,
1145              * and distance from right border of the item to the right
1146              * border of the frame is taken.
1147              * See comment to slotOriginXClicked().
1148              */
1149             posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1150         } else {
1151             posX = value_x->value();
1152         }
1153
1154         int posY;
1155         if (origin_y_top->isChecked()) {
1156             /* Same for y axis */
1157             posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1158         } else {
1159             posY = value_y->value();
1160         }
1161
1162         rec->setPos(posX, posY);
1163
1164     } else if (i->type() == RECTITEM) {
1165
1166         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1167
1168         int posX;
1169         if (origin_x_left->isChecked()) {
1170             posX = m_frameWidth - value_x->value() - rec->rect().width();
1171         } else {
1172             posX = value_x->value();
1173         }
1174
1175         int posY;
1176         if (origin_y_top->isChecked()) {
1177             posY = m_frameHeight - value_y->value() - rec->rect().height();
1178         } else {
1179             posY = value_y->value();
1180         }
1181
1182         rec->setPos(posX, posY);
1183
1184     } else if (i->type() == IMAGEITEM) {
1185         int posX;
1186         if (origin_x_left->isChecked()) {
1187             // Use the sceneBoundingRect because this also regards transformations like zoom
1188             posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1189         } else {
1190             posX = value_x->value();
1191         }
1192
1193         int posY;
1194         if (origin_y_top->isChecked()) {
1195             posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1196         } else {
1197             posY = value_y->value();
1198         }
1199
1200         i->setPos(posX, posY);
1201
1202     }
1203
1204 }
1205
1206 void TitleWidget::updateTextOriginX()
1207 {
1208     if (origin_x_left->isChecked()) {
1209         origin_x_left->setText(i18n("\u2212X"));
1210     } else {
1211         origin_x_left->setText(i18n("+X"));
1212     }
1213 }
1214
1215 void TitleWidget::slotOriginXClicked()
1216 {
1217     // Update the text displayed on the button.
1218     updateTextOriginX();
1219
1220     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1221     if (l.size() >= 1) {
1222         updateCoordinates(l.at(0));
1223
1224         // Remember x axis setting
1225         l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1226                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1227     }
1228     graphicsView->setFocus();
1229 }
1230
1231 void TitleWidget::updateTextOriginY()
1232 {
1233     if (origin_y_top->isChecked()) {
1234         origin_y_top->setText(i18n("\u2212Y"));
1235     } else {
1236         origin_y_top->setText(i18n("+Y"));
1237     }
1238 }
1239
1240 void TitleWidget::slotOriginYClicked()
1241 {
1242     // Update the text displayed on the button.
1243     updateTextOriginY();
1244
1245     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1246     if (l.size() >= 1) {
1247         updateCoordinates(l.at(0));
1248
1249         l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1250                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1251
1252     }
1253     graphicsView->setFocus();
1254 }
1255
1256 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1257 {
1258     int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1259     int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1260     origin_x_left->blockSignals(true);
1261     origin_y_top->blockSignals(true);
1262
1263     if (xAxis == TitleDocument::AxisInverted) {
1264         origin_x_left->setChecked(true);
1265     } else {
1266         origin_x_left->setChecked(false);
1267     }
1268     updateTextOriginX();
1269
1270     if (yAxis == TitleDocument::AxisInverted) {
1271         origin_y_top->setChecked(true);
1272     } else {
1273         origin_y_top->setChecked(false);
1274     }
1275     updateTextOriginY();
1276
1277     origin_x_left->blockSignals(false);
1278     origin_y_top->blockSignals(false);
1279 }
1280
1281 void TitleWidget::slotChangeBackground()
1282 {
1283     QColor color = kcolorbutton->color();
1284     color.setAlpha(horizontalSlider->value());
1285     m_frameBorder->setBrush(QBrush(color));
1286 }
1287
1288 /**
1289  * Something (yeah) has changed in our QGraphicsScene.
1290  */
1291 void TitleWidget::slotChanged()
1292 {
1293     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1294     if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1295         textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1296     }
1297 }
1298
1299 /**
1300  * If the user has set origin_x_left (everything also for y),
1301  * we need to look whether a text element has been selected. If yes,
1302  * we need to ensure that the right border of the text field
1303  * remains fixed also when some text has been entered.
1304  *
1305  * This is also known as right-justified, with the difference that
1306  * it is not valid for text but for its boundingRect. Text may still
1307  * be left-justified.
1308  */
1309 void TitleWidget::textChanged(QGraphicsTextItem *i)
1310 {
1311
1312     updateDimension(i);
1313
1314     if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1315
1316         if (!i->toPlainText().isEmpty()) {
1317             updatePosition(i);
1318         } else {
1319             /*
1320              * Don't do anything if the string is empty. If the position
1321              * would be updated here, a newly created text field would
1322              * be set to the position of the last selected text field.
1323              */
1324         }
1325     }
1326 }
1327
1328 void TitleWidget::slotInsertUnicode()
1329 {
1330     m_unicodeDialog->exec();
1331 }
1332
1333 void TitleWidget::slotInsertUnicodeString(QString text)
1334 {
1335     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1336     if (l.size() > 0) {
1337         if (l.at(0)->type() == TEXTITEM) {
1338             QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1339             t->textCursor().insertText(text);
1340         }
1341     }
1342 }
1343
1344 void TitleWidget::slotUpdateText()
1345 {
1346     QFont font = font_family->currentFont();
1347     font.setPixelSize(font_size->value());
1348     font.setItalic(buttonItalic->isChecked());
1349     font.setUnderline(buttonUnder->isChecked());
1350     font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1351     QColor color = fontColorButton->color();
1352     color.setAlpha(textAlpha->value());
1353
1354     QColor outlineColor = textOutlineColor->color();
1355     outlineColor.setAlpha(textOutlineAlpha->value());
1356     double outlineWidth = textOutline->value() / 10.0;
1357     QGraphicsTextItem* item = NULL;
1358     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1359     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1360         item = static_cast <QGraphicsTextItem *>(l.at(0));
1361     }
1362     if (!item) return;
1363     //if (item->textCursor().selection ().isEmpty())
1364     QTextCursor cur(item->document());
1365     cur.select(QTextCursor::Document);
1366     QTextBlockFormat format = cur.blockFormat();
1367     if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1368         item->setTextWidth(item->boundingRect().width());
1369         if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1370         else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1371         else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1372     } else {
1373         format.setAlignment(Qt::AlignLeft);
1374         item->setTextWidth(-1);
1375     }
1376
1377     item->setFont(font);
1378     QTextCharFormat cformat = cur.charFormat();
1379
1380     item->setData(101, outlineWidth);
1381     item->setData(102, outlineColor);
1382     if (outlineWidth > 0.0) cformat.setTextOutline(QPen(outlineColor, outlineWidth));
1383
1384     cformat.setForeground(QBrush(color));
1385     cur.setCharFormat(cformat);
1386     cur.setBlockFormat(format);
1387     item->setTextCursor(cur);
1388     cur.clearSelection();
1389     item->setTextCursor(cur);
1390 }
1391
1392 void TitleWidget::rectChanged()
1393 {
1394     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1395     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1396         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1397         QColor f = rectFColor->color();
1398         f.setAlpha(rectFAlpha->value());
1399         QPen penf(f);
1400         penf.setWidth(rectLineWidth->value());
1401         rec->setPen(penf);
1402         QColor b = rectBColor->color();
1403         b.setAlpha(rectBAlpha->value());
1404         rec->setBrush(QBrush(b));
1405     }
1406 }
1407
1408 void TitleWidget::itemScaled(int val)
1409 {
1410     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1411     if (l.size() == 1) {
1412         Transform x = m_transformations.value(l.at(0));
1413         x.scalex = (double)val / 100.0;
1414         x.scaley = (double)val / 100.0;
1415         QTransform qtrans;
1416         qtrans.scale(x.scalex, x.scaley);
1417         qtrans.rotate(x.rotate);
1418         l[0]->setTransform(qtrans);
1419         m_transformations[l.at(0)] = x;
1420         updateDimension(l.at(0));
1421     }
1422 }
1423
1424 void TitleWidget::itemRotate(int val)
1425 {
1426     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1427     if (l.size() == 1) {
1428         Transform x = m_transformations[l.at(0)];
1429         x.rotate = (double)val;
1430         QTransform qtrans;
1431         qtrans.scale(x.scalex, x.scaley);
1432         qtrans.rotate(x.rotate);
1433         l[0]->setTransform(qtrans);
1434         m_transformations[l.at(0)] = x;
1435         updateDimension(l.at(0));
1436     }
1437 }
1438
1439 void TitleWidget::itemHCenter()
1440 {
1441     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1442     if (l.size() == 1) {
1443         QGraphicsItem *item = l.at(0);
1444         QRectF br = item->sceneBoundingRect();
1445         int width = (int)br.width();
1446         int newPos = (int)((m_frameWidth - width) / 2);
1447         newPos += item->pos().x() - br.left(); // Check item transformation
1448         item->setPos(newPos, item->pos().y());
1449         updateCoordinates(item);
1450     }
1451 }
1452
1453 void TitleWidget::itemVCenter()
1454 {
1455     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1456     if (l.size() == 1) {
1457         QGraphicsItem *item = l.at(0);
1458         QRectF br = item->sceneBoundingRect();
1459         int height = (int)br.height();
1460         int newPos = (int)((m_frameHeight - height) / 2);
1461         newPos += item->pos().y() - br.top(); // Check item transformation
1462         item->setPos(item->pos().x(), newPos);
1463         updateCoordinates(item);
1464     }
1465 }
1466
1467 void TitleWidget::itemTop()
1468 {
1469     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1470     if (l.size() == 1) {
1471         QGraphicsItem *item = l.at(0);
1472         QRectF br = item->sceneBoundingRect();
1473         double diff;
1474         if (br.top() > 0) diff = -br.top();
1475         else diff = -br.bottom();
1476         item->moveBy(0, diff);
1477         updateCoordinates(item);
1478     }
1479 }
1480
1481 void TitleWidget::itemBottom()
1482 {
1483     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1484     if (l.size() == 1) {
1485         QGraphicsItem *item = l.at(0);
1486         QRectF br = item->sceneBoundingRect();
1487         double diff;
1488         if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1489         else diff = m_frameHeight - br.bottom();
1490         item->moveBy(0, diff);
1491         updateCoordinates(item);
1492     }
1493 }
1494
1495 void TitleWidget::itemLeft()
1496 {
1497     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1498     if (l.size() == 1) {
1499         QGraphicsItem *item = l.at(0);
1500         QRectF br = item->sceneBoundingRect();
1501         double diff;
1502         if (br.left() > 0) diff = -br.left();
1503         else diff = -br.right();
1504         item->moveBy(diff, 0);
1505         updateCoordinates(item);
1506     }
1507 }
1508
1509 void TitleWidget::itemRight()
1510 {
1511     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1512     if (l.size() == 1) {
1513         QGraphicsItem *item = l.at(0);
1514         QRectF br = item->sceneBoundingRect();
1515         double diff;
1516         if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1517         else diff = m_frameWidth - br.left();
1518         item->moveBy(diff, 0);
1519         updateCoordinates(item);
1520     }
1521 }
1522
1523 void TitleWidget::setupViewports()
1524 {
1525     //double aspect_ratio = 4.0 / 3.0;//read from project
1526     //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1527     /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1528     QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1529     // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1530     QPolygonF spoly(sp);
1531     QPolygonF epoly(ep);
1532     spoly.translate(startViewportX->value(), startViewportY->value());
1533     epoly.translate(endViewportX->value(), endViewportY->value());
1534     m_startViewport->setPolygon(spoly);
1535     m_endViewport->setPolygon(epoly);
1536     if (! insertingValues) {
1537         m_startViewport->setData(0, startViewportX->value());
1538         m_startViewport->setData(1, startViewportY->value());
1539         m_startViewport->setData(2, startViewportSize->value());
1540
1541         m_endViewport->setData(0, endViewportX->value());
1542         m_endViewport->setData(1, endViewportY->value());
1543         m_endViewport->setData(2, endViewportSize->value());
1544     }*/
1545 }
1546
1547 void TitleWidget::loadTitle(KUrl url)
1548 {
1549     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1550     if (!url.isEmpty()) {
1551         QList<QGraphicsItem *> items = m_scene->items();
1552         for (int i = 0; i < items.size(); i++) {
1553             if (items.at(i)->zValue() > -1000) delete items.at(i);
1554         }
1555         m_scene->clearTextSelection();
1556         QDomDocument doc;
1557         QString tmpfile;
1558
1559         if (KIO::NetAccess::download(url, tmpfile, 0)) {
1560             QFile file(tmpfile);
1561             if (file.open(QIODevice::ReadOnly)) {
1562                 doc.setContent(&file, false);
1563                 file.close();
1564             } else return;
1565             KIO::NetAccess::removeTempFile(tmpfile);
1566         }
1567         setXml(doc);
1568
1569         /*int out;
1570         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1571         adjustFrameSize();
1572         title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1573         insertingValues = true;
1574         startViewportX->setValue(m_startViewport->data(0).toInt());
1575         startViewportY->setValue(m_startViewport->data(1).toInt());
1576         startViewportSize->setValue(m_startViewport->data(2).toInt());
1577         endViewportX->setValue(m_endViewport->data(0).toInt());
1578         endViewportY->setValue(m_endViewport->data(1).toInt());
1579         endViewportSize->setValue(m_endViewport->data(2).toInt());
1580
1581         insertingValues = false;
1582         slotSelectTool();
1583         slotAdjustZoom();*/
1584     }
1585 }
1586
1587 void TitleWidget::saveTitle(KUrl url)
1588 {
1589     if (anim_start->isChecked()) slotAnimStart(false);
1590     if (anim_end->isChecked()) slotAnimEnd(false);
1591     if (url.isEmpty()) {
1592         KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1593         fs->setOperationMode(KFileDialog::Saving);
1594         fs->setMode(KFile::File);
1595 #if KDE_IS_VERSION(4,2,0)
1596         fs->setConfirmOverwrite(true);
1597 #endif
1598         fs->setKeepLocation(true);
1599         fs->exec();
1600         url = fs->selectedUrl();
1601         delete fs;
1602     }
1603     if (!url.isEmpty()) {
1604         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1605             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1606     }
1607 }
1608
1609 QDomDocument TitleWidget::xml()
1610 {
1611     QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1612     doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1613     return doc;
1614 }
1615
1616 int TitleWidget::duration() const
1617 {
1618     return m_tc.getFrameCount(title_duration->text());
1619 }
1620
1621 void TitleWidget::setXml(QDomDocument doc)
1622 {
1623     int out;
1624     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1625     adjustFrameSize();
1626     title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1627     /*if (doc.documentElement().hasAttribute("out")) {
1628     GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1629     title_duration->setText(m_tc.getTimecode(duration));
1630     }
1631     else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1632
1633     QDomElement e = doc.documentElement();
1634     m_transformations.clear();
1635     QList <QGraphicsItem *> items = graphicsView->scene()->items();
1636     const double PI = 4.0 * atan(1.0);
1637     for (int i = 0; i < items.count(); i++) {
1638         QTransform t = items.at(i)->transform();
1639         Transform x;
1640         x.scalex = t.m11();
1641         x.scaley = t.m22();
1642         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1643         m_transformations[items.at(i)] = x;
1644     }
1645     // mbd: Update the GUI color selectors to match the stuff from the loaded document
1646     QColor background_color = m_titledocument.getBackgroundColor();
1647     horizontalSlider->blockSignals(true);
1648     kcolorbutton->blockSignals(true);
1649     horizontalSlider->setValue(background_color.alpha());
1650     background_color.setAlpha(255);
1651     kcolorbutton->setColor(background_color);
1652     horizontalSlider->blockSignals(false);
1653     kcolorbutton->blockSignals(false);
1654
1655     /*startViewportX->setValue(m_startViewport->data(0).toInt());
1656     startViewportY->setValue(m_startViewport->data(1).toInt());
1657     startViewportSize->setValue(m_startViewport->data(2).toInt());
1658     endViewportX->setValue(m_endViewport->data(0).toInt());
1659     endViewportY->setValue(m_endViewport->data(1).toInt());
1660     endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1661
1662     QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1663     slotSelectTool();
1664 }
1665
1666 /** \brief Connected to the accepted signal - calls writeChoices */
1667 void TitleWidget::slotAccepted()
1668 {
1669     if (anim_start->isChecked()) slotAnimStart(false);
1670     if (anim_end->isChecked()) slotAnimEnd(false);
1671     writeChoices();
1672 }
1673
1674 /** \brief Store the current choices of font, background and rect values */
1675 void TitleWidget::writeChoices()
1676 {
1677     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1678     KSharedConfigPtr config = KGlobal::config();
1679     KConfigGroup titleConfig(config, "TitleWidget");
1680     // Write the entries
1681     titleConfig.writeEntry("font_family", font_family->currentFont());
1682     //titleConfig.writeEntry("font_size", font_size->value());
1683     titleConfig.writeEntry("font_pixel_size", font_size->value());
1684     titleConfig.writeEntry("font_color", fontColorButton->color());
1685     titleConfig.writeEntry("font_alpha", textAlpha->value());
1686     titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1687     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1688     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1689
1690     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1691     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1692     titleConfig.writeEntry("rect_background_color", rectBColor->color());
1693     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1694     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1695
1696     titleConfig.writeEntry("background_color", kcolorbutton->color());
1697     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1698
1699     //! \todo Not sure if I should sync - it is probably safe to do it
1700     config->sync();
1701
1702 }
1703
1704 /** \brief Read the last stored choices into the dialog */
1705 void TitleWidget::readChoices()
1706 {
1707     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1708     KSharedConfigPtr config = KGlobal::config();
1709     KConfigGroup titleConfig(config, "TitleWidget");
1710     // read the entries
1711     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1712     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1713     m_scene->slotUpdateFontSize(font_size->value());
1714     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1715     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1716     int weight;
1717     if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1718     else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1719     setFontBoxWeight(weight);
1720     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1721     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1722
1723     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1724     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1725     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1726     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1727     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1728
1729     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1730     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1731 }
1732
1733 void TitleWidget::adjustFrameSize()
1734 {
1735     m_frameWidth = m_titledocument.frameWidth();
1736     m_frameHeight = m_titledocument.frameHeight();
1737     m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1738     displayBackgroundFrame();
1739 }
1740
1741 void TitleWidget::slotAnimStart(bool anim)
1742 {
1743     if (anim && anim_end->isChecked()) {
1744         anim_end->setChecked(false);
1745         m_endViewport->setZValue(-1000);
1746         m_endViewport->setBrush(QBrush());
1747     }
1748     slotSelectTool();
1749     QList<QGraphicsItem *> list = m_scene->items();
1750     for (int i = 0; i < list.count(); i++) {
1751         if (list.at(i)->zValue() > -1000) {
1752             list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1753             list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1754         }
1755     }
1756     align_box->setEnabled(anim);
1757     itemzoom->setEnabled(!anim);
1758     itemrotate->setEnabled(!anim);
1759     frame_toolbar->setEnabled(!anim);
1760     toolbar_stack->setEnabled(!anim);
1761     if (anim) {
1762         keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1763         m_startViewport->setZValue(1100);
1764         QColor col = m_startViewport->pen().color();
1765         col.setAlpha(100);
1766         m_startViewport->setBrush(col);
1767         m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1768         m_startViewport->setSelected(true);
1769         selectionChanged();
1770         slotSelectTool();
1771         if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1772     } else {
1773         m_startViewport->setZValue(-1000);
1774         m_startViewport->setBrush(QBrush());
1775         m_startViewport->setFlags(0);
1776         if (!anim_end->isChecked()) deleteAnimInfoText();
1777     }
1778
1779 }
1780
1781 void TitleWidget::slotAnimEnd(bool anim)
1782 {
1783     if (anim && anim_start->isChecked()) {
1784         anim_start->setChecked(false);
1785         m_startViewport->setZValue(-1000);
1786         m_startViewport->setBrush(QBrush());
1787     }
1788     slotSelectTool();
1789     QList<QGraphicsItem *> list = m_scene->items();
1790     for (int i = 0; i < list.count(); i++) {
1791         if (list.at(i)->zValue() > -1000) {
1792             list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1793             list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1794         }
1795     }
1796     align_box->setEnabled(anim);
1797     itemzoom->setEnabled(!anim);
1798     itemrotate->setEnabled(!anim);
1799     frame_toolbar->setEnabled(!anim);
1800     toolbar_stack->setEnabled(!anim);
1801     if (anim) {
1802         keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1803         m_endViewport->setZValue(1100);
1804         QColor col = m_endViewport->pen().color();
1805         col.setAlpha(100);
1806         m_endViewport->setBrush(col);
1807         m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1808         m_endViewport->setSelected(true);
1809         selectionChanged();
1810         slotSelectTool();
1811         if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1812     } else {
1813         m_endViewport->setZValue(-1000);
1814         m_endViewport->setBrush(QBrush());
1815         m_endViewport->setFlags(0);
1816         if (!anim_start->isChecked()) deleteAnimInfoText();
1817     }
1818 }
1819
1820 void TitleWidget::addAnimInfoText()
1821 {
1822     // add text to anim viewport
1823     QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1824     QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1825     QFont font = t->font();
1826     font.setPixelSize(m_startViewport->rect().width() / 10);
1827     QColor col = m_startViewport->pen().color();
1828     col.setAlpha(255);
1829     t->setDefaultTextColor(col);
1830     t->setFont(font);
1831     font.setPixelSize(m_endViewport->rect().width() / 10);
1832     col = m_endViewport->pen().color();
1833     col.setAlpha(255);
1834     t2->setDefaultTextColor(col);
1835     t2->setFont(font);
1836 }
1837
1838 void TitleWidget::updateInfoText()
1839 {
1840     // update info text font
1841     if (!m_startViewport->childItems().isEmpty()) {
1842         QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1843         if (item) {
1844             QFont font = item->font();
1845             font.setPixelSize(m_startViewport->rect().width() / 10);
1846             item->setFont(font);
1847         }
1848     }
1849     if (!m_endViewport->childItems().isEmpty()) {
1850         QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1851         if (item) {
1852             QFont font = item->font();
1853             font.setPixelSize(m_endViewport->rect().width() / 10);
1854             item->setFont(font);
1855         }
1856     }
1857 }
1858
1859 void TitleWidget::deleteAnimInfoText()
1860 {
1861     // end animation editing, remove info text
1862     while (!m_startViewport->childItems().isEmpty()) {
1863         QGraphicsItem *item = m_startViewport->childItems().at(0);
1864         m_scene->removeItem(item);
1865         delete item;
1866     }
1867     while (!m_endViewport->childItems().isEmpty()) {
1868         QGraphicsItem *item = m_endViewport->childItems().at(0);
1869         m_scene->removeItem(item);
1870         delete item;
1871     }
1872 }
1873
1874
1875 void TitleWidget::slotKeepAspect(bool keep)
1876 {
1877     if (m_endViewport->zValue() == 1100) {
1878         m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1879         m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1880     } else {
1881         m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1882         m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1883     }
1884 }
1885
1886 void TitleWidget::slotResize50()
1887 {
1888     if (m_endViewport->zValue() == 1100) {
1889         m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1890     } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1891 }
1892
1893 void TitleWidget::slotResize100()
1894 {
1895     if (m_endViewport->zValue() == 1100) {
1896         m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1897     } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1898 }
1899
1900 void TitleWidget::slotResize200()
1901 {
1902     if (m_endViewport->zValue() == 1100) {
1903         m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1904     } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1905 }
1906
1907 void TitleWidget::slotAddEffect(int ix)
1908 {
1909     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1910     int effect = effect_list->itemData(ix).toInt();
1911     if (effect == NOEFFECT) {
1912         if (l.size() == 1) l[0]->setData(100, QVariant());
1913         effect_stack->setHidden(true);
1914         return;
1915     }
1916     effect_stack->setCurrentIndex(effect - 1);
1917     effect_stack->setHidden(false);
1918     if (effect == TYPEWRITEREFFECT) {
1919         if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1920             QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value()) + ";" + QString::number(typewriter_start->value());
1921             l[0]->setData(100, effdata);
1922         }
1923     }
1924 #if QT_VERSION < 0x040600
1925     return;
1926 #else
1927     if (effect == BLUREFFECT) {
1928         // Blur effect
1929         if (l.size() == 1) {
1930             QGraphicsEffect *eff = new QGraphicsBlurEffect();
1931             l[0]->setGraphicsEffect(eff);
1932         }
1933     } else if (effect == SHADOWEFFECT) {
1934         if (l.size() == 1) {
1935             QGraphicsEffect *eff = new QGraphicsDropShadowEffect();
1936             l[0]->setGraphicsEffect(eff);
1937         }
1938     }
1939
1940 #endif
1941 }
1942
1943
1944 void TitleWidget::slotFontText(const QString& s)
1945 {
1946     const QFont f(s);
1947     if (f.exactMatch()) {
1948         // Font really exists (could also just be a «d» if the user
1949         // starts typing «dejavu» for example).
1950         font_family->setCurrentFont(f);
1951     }
1952     // Note: Typing dejavu serif does not recognize the font (takes sans)
1953     // in older Qt versions.
1954 }
1955
1956 void TitleWidget::slotEditTypewriter(int ix)
1957 {
1958     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1959     if (l.size() == 1) {
1960         QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value()) + ";" + QString::number(typewriter_start->value());
1961         l[0]->setData(100, effdata);
1962     }
1963 }
1964
1965 void TitleWidget::slotEditBlur(int ix)
1966 {
1967 #if QT_VERSION < 0x040600
1968     return;
1969 #else
1970     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1971     if (l.size() == 1) {
1972         QGraphicsEffect *eff = l[0]->graphicsEffect();
1973         QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1974         if (blur) blur->setBlurRadius(ix);
1975     }
1976 #endif
1977 }
1978
1979 void TitleWidget::slotEditShadow()
1980 {
1981 #if QT_VERSION < 0x040600
1982     return;
1983 #else
1984     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1985     if (l.size() == 1) {
1986         QGraphicsEffect *eff = l[0]->graphicsEffect();
1987         QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1988         if (shadow) {
1989             shadow->setBlurRadius(shadow_radius->value());
1990             shadow->setOffset(shadow_x->value(), shadow_y->value());
1991         }
1992     }
1993 #endif
1994 }
1995
1996 qreal TitleWidget::zIndexBounds(bool maxBound)
1997 {
1998     qreal bound = maxBound ? -99 : 99;
1999     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
2000     if (l.size() > 0) {
2001         QList<QGraphicsItem*> lItems = graphicsView->scene()->items(l[0]->sceneBoundingRect(), Qt::IntersectsItemShape);
2002         if (lItems.size() > 0) {
2003             int n = lItems.size();
2004             qreal z;
2005             if (maxBound) {
2006                 for (int i = 0; i < n; i++) {
2007                     z = lItems[i]->zValue();
2008                     if (z > bound && !lItems[i]->isSelected()) {
2009                         bound = z;
2010                     }
2011                 }
2012             } else {
2013                 // Get minimum z index.
2014                 for (int i = 0; i < n; i++) {
2015                     z = lItems[i]->zValue();
2016                     if (z < bound && !lItems[i]->isSelected() && z > -999) {
2017                         // There are items at the very bottom (background e.g.) with z-index < -1000.
2018                         bound = z;
2019                     }
2020                 }
2021             }
2022         }
2023     }
2024     return bound;
2025 }
2026
2027 void TitleWidget::slotZIndexUp()
2028 {
2029     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
2030     if (l.size() >= 1) {
2031         qreal currentZ = l[0]->zValue();
2032         qreal max = zIndexBounds(true);
2033         if (currentZ <= max) {
2034             l[0]->setZValue(currentZ + 1);
2035             updateDimension(l[0]);
2036         }
2037     }
2038 }
2039
2040 void TitleWidget::slotZIndexTop()
2041 {
2042     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
2043     if (l.size() >= 1) {
2044         qreal currentZ = l[0]->zValue();
2045         qreal max = zIndexBounds(true);
2046         if (currentZ <= max) {
2047             l[0]->setZValue(max + 1);
2048             updateDimension(l[0]);
2049         }
2050     }
2051 }
2052
2053 void TitleWidget::slotZIndexDown()
2054 {
2055     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
2056     if (l.size() >= 1) {
2057         qreal currentZ = l[0]->zValue();
2058         qreal min = zIndexBounds(false);
2059         if (currentZ >= min) {
2060             l[0]->setZValue(currentZ - 1);
2061             updateDimension(l[0]);
2062         }
2063     }
2064 }
2065
2066 void TitleWidget::slotZIndexBottom()
2067 {
2068     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
2069     if (l.size() >= 1) {
2070         qreal currentZ = l[0]->zValue();
2071         qreal min = zIndexBounds(false);
2072         if (currentZ >= min) {
2073             l[0]->setZValue(min - 1);
2074             updateDimension(l[0]);
2075         }
2076     }
2077 }