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