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