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