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