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