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