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