]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
Fix regression introduced by document validation changes (document profile was not...
[kdenlive] / src / titlewidget.cpp
1 /***************************************************************************
2                           titlewidget.h  -  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 <KDebug>
22 #include <KGlobalSettings>
23 #include <KFileDialog>
24 #include <KStandardDirs>
25 #include <KMessageBox>
26
27 #include <QDomDocument>
28 #include <QGraphicsItem>
29 #include <QGraphicsSvgItem>
30 #include <QTimer>
31 #include <QToolBar>
32 #include <QMenu>
33 #include <QTextBlockFormat>
34 #include <QTextCursor>
35
36 int settingUp = false;
37
38 const int IMAGEITEM = 7;
39 const int RECTITEM = 3;
40 const int TEXTITEM = 8;
41
42 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
43         QDialog(parent),
44         Ui::TitleWidget_UI(),
45         m_startViewport(NULL),
46         m_endViewport(NULL),
47         m_render(render),
48         m_count(0),
49         m_projectPath(projectPath)
50 {
51     setupUi(this);
52     setFont(KGlobalSettings::toolBarFont());
53     //toolBox->setFont(KGlobalSettings::toolBarFont());
54     frame_properties->setEnabled(false);
55     rect_properties->setFixedHeight(frame_properties->height() + 4);
56     text_properties->setFixedHeight(frame_properties->height() + 4);
57     m_frameWidth = render->renderWidth();
58     m_frameHeight = render->renderHeight();
59     //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
60     //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
61     // kcolorbutton == The color of the background
62     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
63     // horizontalslider == The alpha of the background
64     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
65         
66
67     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
68     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
69     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
70     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
71
72     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
73     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
74     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
75     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
76     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
77
78     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
84
85     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
86     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
87     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
88     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
89     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
90
91         connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
92         connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
93
94     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
98     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
99     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
100     connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103     connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104     connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
105     connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
106     connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
107     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
108
109     // mbd
110     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
111
112     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
113     buttonRealSize->setIcon(KIcon("zoom-original"));
114     buttonBold->setIcon(KIcon("format-text-bold"));
115     buttonItalic->setIcon(KIcon("format-text-italic"));
116     buttonUnder->setIcon(KIcon("format-text-underline"));
117     buttonAlignCenter->setIcon(KIcon("format-justify-center"));
118     buttonAlignLeft->setIcon(KIcon("format-justify-left"));
119     buttonAlignRight->setIcon(KIcon("format-justify-right"));
120     buttonAlignNone->setToolTip(i18n("No alignment"));
121     buttonAlignRight->setToolTip(i18n("Align right"));
122     buttonAlignLeft->setToolTip(i18n("Align left"));
123     buttonAlignCenter->setToolTip(i18n("Align center"));
124
125     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
126     itemhcenter->setToolTip(i18n("Align item horizontally"));
127     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
128     itemvcenter->setToolTip(i18n("Align item vertically"));
129
130     QHBoxLayout *layout = new QHBoxLayout;
131     frame_toolbar->setLayout(layout);
132     layout->setContentsMargins(2, 2, 2, 2);
133     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
134
135     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
136     m_buttonRect->setCheckable(true);
137     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
138
139     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
140     m_buttonText->setCheckable(true);
141     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
142
143     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
144     m_buttonImage->setCheckable(false);
145     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
146
147     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
148     m_buttonCursor->setCheckable(true);
149     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
150
151     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
152     m_buttonLoad->setCheckable(false);
153     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
154
155     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
156     m_buttonSave->setCheckable(false);
157     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
158
159     layout->addWidget(m_toolbar);
160     text_properties->setHidden(true);
161
162     // initialize graphic scene
163     m_scene = new GraphicsSceneRectMove(this);
164     graphicsView->setScene(m_scene);
165     m_titledocument.setScene(m_scene);
166         connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
167
168     // a gradient background
169     /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
170     gradient->setSpread(QGradient::ReflectSpread);
171     scene->setBackgroundBrush(*gradient);*/
172
173     m_frameImage = new QGraphicsPixmapItem();
174     QTransform qtrans;
175     qtrans.scale(2.0, 2.0);
176     m_frameImage->setTransform(qtrans);
177     m_frameImage->setZValue(-1200);
178     m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
179     displayBackgroundFrame();
180     graphicsView->scene()->addItem(m_frameImage);
181
182     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
183     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
184     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
185     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
186     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
187     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
188     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
189     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
190
191     QPen framepen(Qt::DotLine);
192     framepen.setColor(Qt::red);
193
194     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
195     m_frameBorder->setPen(framepen);
196     m_frameBorder->setZValue(-1100);
197     m_frameBorder->setBrush(Qt::transparent);
198     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
199     graphicsView->scene()->addItem(m_frameBorder);
200
201     // mbd: load saved settings
202     readChoices();
203
204     initViewports();
205     QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
206     graphicsView->show();
207     //graphicsView->setRenderHint(QPainter::Antialiasing);
208     graphicsView->setInteractive(true);
209     //graphicsView->resize(400, 300);
210     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
211     toolBox->setItemEnabled(2, false);
212     if (!url.isEmpty()) {
213         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
214         slotSelectTool();
215     } else {
216         slotRectTool();
217     }
218 }
219
220 TitleWidget::~TitleWidget()
221 {
222     delete m_buttonRect;
223     delete m_buttonText;
224     delete m_buttonImage;
225     delete m_buttonCursor;
226     delete m_buttonSave;
227     delete m_buttonLoad;
228
229     delete m_frameBorder;
230     delete m_frameImage;
231     delete m_startViewport;
232     delete m_endViewport;
233     delete m_scene;
234 }
235
236 //static
237 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
238 {
239     QStringList result;
240     QString titlePath = projectUrl.path() + "/titles/";
241     KStandardDirs::makeDir(titlePath);
242     QString titleName = "title";
243     int counter = 0;
244     QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
245     while (QFile::exists(path + ".png")) {
246         counter++;
247         path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
248     }
249     result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
250     result.append(path + ".png");
251     return result;
252 }
253
254 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
255 {
256     QStringList result;
257     QString titlePath = projectUrl.path() + "/titles/";
258     KStandardDirs::makeDir(titlePath);
259     return titlePath + titleName + ".png";
260 }
261
262 //virtual
263 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
264 {
265     //slotAdjustZoom();
266 }
267
268 void TitleWidget::slotTextTool()
269 {
270     rect_properties->setHidden(true);
271     text_properties->setHidden(false);
272     m_scene->setTool(TITLE_TEXT);
273     m_buttonRect->setChecked(false);
274     m_buttonCursor->setChecked(false);
275 }
276
277 void TitleWidget::slotRectTool()
278 {
279     text_properties->setHidden(true);
280     rect_properties->setHidden(false);
281     m_scene->setTool(TITLE_RECTANGLE);
282     m_buttonText->setChecked(false);
283     m_buttonCursor->setChecked(false);
284     m_buttonRect->setChecked(true);
285 }
286
287 void TitleWidget::slotSelectTool()
288 {
289     m_scene->setTool(TITLE_SELECT);
290     m_buttonCursor->setChecked(true);
291     m_buttonText->setChecked(false);
292     m_buttonRect->setChecked(false);
293 }
294
295 void TitleWidget::slotImageTool()
296 {
297     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
298     if (!url.isEmpty()) {
299         if (url.path().endsWith(".svg")) {
300             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
301             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
302             svg->setZValue(m_count++);
303             svg->setData(Qt::UserRole, url.path());
304             graphicsView->scene()->addItem(svg);
305         } else {
306             QPixmap pix(url.path());
307             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
308             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
309             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
310             image->setData(Qt::UserRole, url.path());
311             image->setZValue(m_count++);
312             graphicsView->scene()->addItem(image);
313         }
314     }
315     m_scene->setTool(TITLE_SELECT);
316     m_buttonRect->setChecked(false);
317     m_buttonCursor->setChecked(true);
318     m_buttonText->setChecked(false);
319 }
320
321 void TitleWidget::displayBackgroundFrame()
322 {
323     if (!displayBg->isChecked()) {
324         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
325         QPixmap pattern(20, 20);
326         pattern.fill();
327         QPainter p;
328         p.begin(&pattern);
329         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
330         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
331         p.end();
332         QBrush br(pattern);
333
334         p.begin(&bg);
335         p.fillRect(bg.rect(), br);
336         p.end();
337         m_frameImage->setPixmap(bg);
338     } else {
339         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
340     }
341 }
342
343 void TitleWidget::initViewports()
344 {
345     m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
346     m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
347
348     QPen startpen(Qt::DotLine);
349     QPen endpen(Qt::DashDotLine);
350     startpen.setColor(QColor(100, 200, 100, 140));
351     endpen.setColor(QColor(200, 100, 100, 140));
352
353     m_startViewport->setPen(startpen);
354     m_endViewport->setPen(endpen);
355
356     startViewportSize->setValue(40);
357     endViewportSize->setValue(40);
358
359     m_startViewport->setZValue(-1000);
360     m_endViewport->setZValue(-1000);
361
362     m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
363     m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
364
365     graphicsView->scene()->addItem(m_startViewport);
366     graphicsView->scene()->addItem(m_endViewport);
367 }
368
369 void TitleWidget::slotUpdateZoom(int pos)
370 {
371     m_scene->setZoom((double) pos / 100);
372     zoom_label->setText(QString::number(pos) + '%');
373 }
374
375 void TitleWidget::slotZoom(bool up)
376 {
377     int pos = zoom_slider->value();
378     if (up) pos++;
379     else pos--;
380     zoom_slider->setValue(pos);
381 }
382
383 void TitleWidget::slotAdjustZoom()
384 {
385     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
386     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
387     if (scalex > scaley) scalex = scaley;
388     int zoompos = (int)(scalex * 7 + 0.5);*/
389     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
390     int zoompos = graphicsView->matrix().m11() * 100;
391     zoom_slider->setValue(zoompos);
392     graphicsView->centerOn(m_frameBorder);
393 }
394
395 void TitleWidget::slotZoomOneToOne()
396 {
397     zoom_slider->setValue(100);
398     graphicsView->centerOn(m_frameBorder);
399 }
400
401 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
402 {
403     QColor f = rectFColor->color();
404     f.setAlpha(rectFAlpha->value());
405     QPen penf(f);
406     penf.setWidth(rectLineWidth->value());
407     rect->setPen(penf);
408     QColor b = rectBColor->color();
409     b.setAlpha(rectBAlpha->value());
410     rect->setBrush(QBrush(b));
411     rect->setZValue(m_count++);
412     //setCurrentItem(rect);
413     //graphicsView->setFocus();
414 }
415
416 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
417 {
418     QFont font = font_family->currentFont();
419     font.setPixelSize(font_size->value());
420     // mbd: issue 551:
421     font.setBold(buttonBold->isChecked());
422     font.setItalic(buttonItalic->isChecked());
423     font.setUnderline(buttonUnder->isChecked());
424
425     tt->setFont(font);
426     QColor color = fontColorButton->color();
427     color.setAlpha(textAlpha->value());
428     tt->setDefaultTextColor(color);
429     tt->setZValue(m_count++);
430     setCurrentItem(tt);
431 }
432
433 void TitleWidget::setCurrentItem(QGraphicsItem *item)
434 {
435     m_scene->setSelectedItem(item);
436 }
437
438 void TitleWidget::zIndexChanged(int v)
439 {
440     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
441     if (l.size() >= 1) {
442         l[0]->setZValue(v);
443     }
444 }
445
446 void TitleWidget::selectionChanged()
447 {
448     if (m_scene->tool() != TITLE_SELECT) return;
449     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
450     //toolBox->setItemEnabled(2, false);
451     //toolBox->setItemEnabled(3, false);
452     value_x->blockSignals(true);
453     value_y->blockSignals(true);
454     value_w->blockSignals(true);
455     value_h->blockSignals(true);
456     itemzoom->blockSignals(true);
457     itemrotate->blockSignals(true);
458     if (l.size() == 0) {
459                 bool blockX = !origin_x_left->signalsBlocked();
460                 bool blockY = !origin_y_top->signalsBlocked();
461                 if (blockX) origin_x_left->blockSignals(true);
462                 if (blockY) origin_y_top->blockSignals(true);
463                 origin_x_left->setChecked(false);
464                 origin_y_top->setChecked(false);
465                 updateTextOriginX();
466                 updateTextOriginY();
467                 if (blockX) origin_x_left->blockSignals(false);
468                 if (blockY) origin_y_top->blockSignals(false);
469         } else if (l.size() == 1) {
470         if (l.at(0)->type() == TEXTITEM) {
471             rect_properties->setHidden(true);
472             text_properties->setHidden(false);
473             QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
474             //if (l[0]->hasFocus())
475             toolBox->setCurrentIndex(0);
476             //toolBox->setItemEnabled(2, true);
477             font_size->blockSignals(true);
478             font_family->blockSignals(true);
479             buttonBold->blockSignals(true);
480             buttonItalic->blockSignals(true);
481             buttonUnder->blockSignals(true);
482             fontColorButton->blockSignals(true);
483             textAlpha->blockSignals(true);
484             buttonAlignLeft->blockSignals(true);
485             buttonAlignRight->blockSignals(true);
486             buttonAlignNone->blockSignals(true);
487             buttonAlignCenter->blockSignals(true);
488
489             QFont font = i->font();
490             font_family->setCurrentFont(font);
491             font_size->setValue(font.pixelSize());
492             buttonBold->setChecked(font.bold());
493             buttonItalic->setChecked(font.italic());
494             buttonUnder->setChecked(font.underline());
495
496             QColor color = i->defaultTextColor();
497             fontColorButton->setColor(color);
498             textAlpha->setValue(color.alpha());
499
500             QTextCursor cur = i->textCursor();
501             QTextBlockFormat format = cur.blockFormat();
502             if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
503             else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
504             else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
505             else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
506
507             font_size->blockSignals(false);
508             font_family->blockSignals(false);
509             buttonBold->blockSignals(false);
510             buttonItalic->blockSignals(false);
511             buttonUnder->blockSignals(false);
512             fontColorButton->blockSignals(false);
513             textAlpha->blockSignals(false);
514             buttonAlignLeft->blockSignals(false);
515             buttonAlignRight->blockSignals(false);
516             buttonAlignNone->blockSignals(false);
517             buttonAlignCenter->blockSignals(false);
518
519                         updateAxisButtons(i);
520                         updateCoordinates(i);
521             value_w->setValue((int) i->boundingRect().width());
522             value_h->setValue((int) i->boundingRect().height());
523             frame_properties->setEnabled(true);
524             value_w->setEnabled(false);
525             value_h->setEnabled(false);
526                         
527         } else if ((l.at(0))->type() == RECTITEM) {
528             rect_properties->setHidden(false);
529             text_properties->setHidden(true);
530             settingUp = true;
531             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
532             toolBox->setCurrentIndex(0);
533             //toolBox->setItemEnabled(3, true);
534             rectFAlpha->setValue(rec->pen().color().alpha());
535             rectBAlpha->setValue(rec->brush().color().alpha());
536             //kDebug() << rec->brush().color().alpha();
537             QColor fcol = rec->pen().color();
538             QColor bcol = rec->brush().color();
539             //fcol.setAlpha(255);
540             //bcol.setAlpha(255);
541             rectFColor->setColor(fcol);
542             rectBColor->setColor(bcol);
543             settingUp = false;
544             rectLineWidth->setValue(rec->pen().width());
545                         
546                         updateAxisButtons(l.at(0));
547                         updateCoordinates(rec);
548             value_w->setValue((int) rec->rect().width());
549             value_h->setValue((int) rec->rect().height());
550             frame_properties->setEnabled(true);
551             value_w->setEnabled(true);
552             value_h->setEnabled(true);
553         } else {
554             //toolBox->setCurrentIndex(0);
555             frame_properties->setEnabled(false);
556         }
557         zValue->setValue((int)l.at(0)->zValue());
558         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
559         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
560         value_x->blockSignals(false);
561         value_y->blockSignals(false);
562         value_w->blockSignals(false);
563         value_h->blockSignals(false);
564         itemzoom->blockSignals(false);
565         itemrotate->blockSignals(false);
566     } else frame_properties->setEnabled(false);
567 }
568
569 /** \brief Updates position/size of the selected item when a value
570  * of an item (coordinates, size) has changed */
571 void TitleWidget::slotAdjustSelectedItem()
572 {
573     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
574     if (l.size() >= 1) {
575         if (l.at(0)->type() == RECTITEM) {
576             //rect item
577             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
578                         updatePosition(rec);
579             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
580         } else if (l.at(0)->type() == TEXTITEM) {
581             //text item
582             updatePosition(l.at(0));
583         }
584     }
585 }
586
587 /** \brief Updates the coordinates in the text fields from the item */
588 void TitleWidget::updateCoordinates(QGraphicsItem *i) 
589 {
590         
591         bool blockX = !value_x->signalsBlocked();
592         bool blockY = !value_y->signalsBlocked();
593         
594         // Block signals emitted by this method
595         if (blockX) value_x->blockSignals(true);
596         if (blockY) value_y->blockSignals(true);
597         
598         if (i->type() == TEXTITEM) {
599                 
600                 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *> (i);
601                 
602                 // Set the correct x coordinate value
603                 if (origin_x_left->isChecked()) {
604                         // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
605                         value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width())); 
606                 } else {
607                         // Origin is at 0 (default)
608                         value_x->setValue((int) rec->pos().x());
609                 }
610                 
611                 // Same for y
612                 if (origin_y_top->isChecked()) {
613                         value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
614                 } else {
615                         value_y->setValue((int) rec->pos().y());
616                 }
617                 
618         } else if (i->type() == RECTITEM) {
619                 
620                 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
621                 
622                 if (origin_x_left->isChecked()) {
623                         // Origin (0 point) is at m_frameWidth
624                         value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width())); 
625                 } else {
626                         // Origin is at 0 (default)
627                         value_x->setValue((int) rec->pos().x());
628                 }
629                 
630                 if (origin_y_top->isChecked()) {
631                         value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->rect().height()));
632                 } else {
633                         value_y->setValue((int) rec->pos().y());
634                 }
635                 
636                 
637         }
638         
639         // Stop blocking signals now
640         if (!blockX) value_x->blockSignals(false);
641         if (!blockY) value_y->blockSignals(false);
642 }
643
644 /** \brief Updates the position of an item by reading coordinates from the text fields */
645 void TitleWidget::updatePosition(QGraphicsItem *i) {
646         
647         if (i->type() == TEXTITEM) {
648                 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
649                 
650                 int posX;
651                 if (origin_x_left->isChecked()) {
652                         /* Origin of the x axis is at m_frameWidth,
653                          * and distance from right border of the item to the right
654                          * border of the frame is taken.
655                          * See comment to slotOriginXClicked().
656                          */
657                         posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
658                 } else {
659                         posX = value_x->value();
660                 }
661         
662                 int posY;
663                 if (origin_y_top->isChecked()) {
664                         /* Same for y axis */
665                         posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
666                 } else {
667                         posY = value_y->value();
668                 }
669                 
670                 rec->setPos(posX, posY);
671         } else if (i->type() == RECTITEM) {
672                 
673                 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
674                 
675                 int posX;
676                 if (origin_x_left->isChecked()) {
677                         posX = m_frameWidth - value_x->value() - rec->rect().width();
678                 } else {
679                         posX = value_x->value();
680                 }
681                 
682                 int posY;
683                 if (origin_y_top->isChecked()) {
684                         posY = m_frameHeight - value_y->value() - rec->rect().height();
685                 } else {
686                         posY = value_y->value();
687                 }
688                 
689                 rec->setPos(posX, posY);
690         }
691         
692 }
693
694 void TitleWidget::updateTextOriginX() 
695 {
696         if (origin_x_left->isChecked()) {
697                 origin_x_left->setText(i18n("\u2212X"));
698         } else {
699                 origin_x_left->setText(i18n("+X"));
700         }
701 }
702
703 void TitleWidget::slotOriginXClicked()
704 {
705         // Update the text displayed on the button.
706         updateTextOriginX();
707         
708         QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
709         if (l.size() >= 1) {
710                 updateCoordinates(l.at(0));
711                 
712                 // Remember x axis setting
713                 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()? 
714                         TitleDocument::AxisInverted : TitleDocument::AxisDefault);
715         }
716 }
717
718 void TitleWidget::updateTextOriginY()
719 {
720         if (origin_y_top->isChecked()) {
721                 origin_y_top->setText(i18n("\u2212Y"));
722         } else {
723                 origin_y_top->setText(i18n("+Y"));
724         }
725 }
726
727 void TitleWidget::slotOriginYClicked()
728 {
729         // Update the text displayed on the button.
730         updateTextOriginY();
731         
732         QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
733         if (l.size() >= 1) {
734                 updateCoordinates(l.at(0));
735                 
736                 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked()? 
737                         TitleDocument::AxisInverted : TitleDocument::AxisDefault);
738                         
739         }
740 }
741
742 void TitleWidget::updateAxisButtons(QGraphicsItem *i) 
743 {
744         int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
745         int yAxis = i->data(TitleDocument::OriginYTop).toInt();
746         origin_x_left->blockSignals(true);
747         origin_y_top->blockSignals(true);
748         
749         if (xAxis == TitleDocument::AxisInverted) {
750                 origin_x_left->setChecked(true);
751         } else {
752                 origin_x_left->setChecked(false);
753         }
754         updateTextOriginX();
755         
756         if (yAxis == TitleDocument::AxisInverted) {
757                 origin_y_top->setChecked(true);
758         } else {
759                 origin_y_top->setChecked(false);
760         }
761         updateTextOriginY();
762         
763         origin_x_left->blockSignals(false);
764         origin_y_top->blockSignals(false);
765 }
766
767 void TitleWidget::slotChangeBackground()
768 {
769     QColor color = kcolorbutton->color();
770     color.setAlpha(horizontalSlider->value());
771     m_frameBorder->setBrush(QBrush(color));
772 }
773
774 /**
775  * Something (yeah) has changed in our QGraphicsScene.
776  */
777 void TitleWidget::slotChanged() {
778         QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
779         if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
780                 textChanged(static_cast <QGraphicsTextItem *> (l.at(0)));
781         }
782 }
783
784 /**
785  * If the user has set origin_x_left (everything also for y),
786  * we need to look whether a text element has been selected. If yes, 
787  * we need to ensure that the right border of the text field 
788  * remains fixed also when some text has been entered.
789  * 
790  * This is also known as right-justified, with the difference that
791  * it is not valid for text but for its boundingRect. Text may still 
792  * be left-justified.
793  */
794 void TitleWidget::textChanged(QGraphicsTextItem *i) {
795         if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
796
797                 if (!i->toPlainText().isEmpty()) {
798                         updatePosition(i);
799                 } else {
800                         /*
801                          * Don't do anything if the string is empty. If the position
802                          * would be updated here, a newly created text field would 
803                          * be set to the position of the last selected text field.
804                          */
805                 }
806         }
807 }
808
809 void TitleWidget::slotUpdateText()
810 {
811     QFont font = font_family->currentFont();
812     font.setPixelSize(font_size->value());
813     font.setBold(buttonBold->isChecked());
814     font.setItalic(buttonItalic->isChecked());
815     font.setUnderline(buttonUnder->isChecked());
816     QColor color = fontColorButton->color();
817     color.setAlpha(textAlpha->value());
818
819     QGraphicsTextItem* item = NULL;
820     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
821     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
822         item = static_cast <QGraphicsTextItem *>(l.at(0));
823     }
824     if (!item) return;
825     //if (item->textCursor().selection ().isEmpty())
826     QTextCursor cur = item->textCursor();
827     QTextBlockFormat format = cur.blockFormat();
828     if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
829         item->setTextWidth(item->boundingRect().width());
830         if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
831         else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
832         else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
833     } else {
834         format.setAlignment(Qt::AlignLeft);
835         item->setTextWidth(-1);
836     }
837
838     {
839         item->setFont(font);
840         item->setDefaultTextColor(color);
841         cur.select(QTextCursor::Document);
842         cur.setBlockFormat(format);
843         item->setTextCursor(cur);
844         cur.clearSelection();
845         item->setTextCursor(cur);
846
847     }
848 }
849
850 void TitleWidget::rectChanged()
851 {
852     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
853     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
854         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
855         QColor f = rectFColor->color();
856         f.setAlpha(rectFAlpha->value());
857         QPen penf(f);
858         penf.setWidth(rectLineWidth->value());
859         rec->setPen(penf);
860         QColor b = rectBColor->color();
861         b.setAlpha(rectBAlpha->value());
862         rec->setBrush(QBrush(b));
863     }
864 }
865
866 void TitleWidget::itemScaled(int val)
867 {
868     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
869     if (l.size() == 1) {
870         Transform x = m_transformations.value(l.at(0));
871         x.scalex = (double)val / 100.0;
872         x.scaley = (double)val / 100.0;
873         QTransform qtrans;
874         qtrans.scale(x.scalex, x.scaley);
875         qtrans.rotate(x.rotate);
876         l[0]->setTransform(qtrans);
877         m_transformations[l.at(0)] = x;
878     }
879 }
880
881 void TitleWidget::itemRotate(int val)
882 {
883     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
884     if (l.size() == 1) {
885         Transform x = m_transformations[l.at(0)];
886         x.rotate = (double)val;
887         QTransform qtrans;
888         qtrans.scale(x.scalex, x.scaley);
889         qtrans.rotate(x.rotate);
890         l[0]->setTransform(qtrans);
891         m_transformations[l.at(0)] = x;
892     }
893 }
894
895 void TitleWidget::itemHCenter()
896 {
897     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
898     if (l.size() == 1) {
899         QGraphicsItem *item = l.at(0);
900         QRectF br;
901         if (item->type() == RECTITEM) {
902             br = ((QGraphicsRectItem*)item)->rect();
903         } else br = item->sceneBoundingRect();
904         int width = (int) br.width();
905         int newPos = (int)((m_frameWidth - width) / 2);
906         item->setPos(newPos, item->pos().y());
907     }
908 }
909
910 void TitleWidget::itemVCenter()
911 {
912     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
913     if (l.size() == 1) {
914         QGraphicsItem *item = l.at(0);
915         QRectF br;
916         if (item->type() == RECTITEM) {
917             br = ((QGraphicsRectItem*)item)->rect();
918         } else br = item->sceneBoundingRect();
919         int height = (int) br.height();
920         int newPos = (int)((m_frameHeight - height) / 2);
921         item->setPos(item->pos().x(), newPos);
922     }
923 }
924
925 void TitleWidget::setupViewports()
926 {
927     double aspect_ratio = 4.0 / 3.0;//read from project
928
929     QRectF sp(0, 0, 0, 0);
930     QRectF ep(0, 0, 0, 0);
931
932     double sv_size = startViewportSize->value();
933     double ev_size = endViewportSize->value();
934     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
935     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
936
937     m_startViewport->setPos(startViewportX->value(), startViewportY->value());
938     m_endViewport->setPos(endViewportX->value(), endViewportY->value());
939
940     m_startViewport->setPolygon(QPolygonF(sp));
941     m_endViewport->setPolygon(QPolygonF(ep));
942
943 }
944
945 void TitleWidget::loadTitle()
946 {
947     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
948     if (!url.isEmpty()) {
949         QList<QGraphicsItem *> items = m_scene->items();
950         for (int i = 0; i < items.size(); i++) {
951             if (items.at(i)->zValue() > -1000) delete items.at(i);
952         }
953         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
954         slotSelectTool();
955     }
956 }
957
958 void TitleWidget::saveTitle(KUrl url)
959 {
960     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
961     if (!url.isEmpty()) {
962         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
963             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
964     }
965 }
966
967 QDomDocument TitleWidget::xml()
968 {
969     QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
970     if (cropImage->isChecked()) {
971         doc.documentElement().setAttribute("crop", 1);
972     }
973     return doc;
974 }
975
976 void TitleWidget::setXml(QDomDocument doc)
977 {
978     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
979     QDomElement e = doc.documentElement();
980     cropImage->setChecked(e.hasAttribute("crop"));
981     m_transformations.clear();
982     QList <QGraphicsItem *> items = graphicsView->scene()->items();
983     const double PI = 4.0 * atan(1.0);
984     for (int i = 0; i < items.count(); i++) {
985         QTransform t = items.at(i)->transform();
986         Transform x;
987         x.scalex = t.m11();
988         x.scaley = t.m22();
989         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
990         m_transformations[items.at(i)] = x;
991     }
992     // mbd: Update the GUI color selectors to match the stuff from the loaded document
993     QColor background_color = m_titledocument.getBackgroundColor();
994     horizontalSlider->blockSignals(true);
995     kcolorbutton->blockSignals(true);
996     horizontalSlider->setValue(background_color.alpha());
997     background_color.setAlpha(255);
998     kcolorbutton->setColor(background_color);
999     horizontalSlider->blockSignals(false);
1000     kcolorbutton->blockSignals(false);
1001
1002     slotSelectTool();
1003 }
1004
1005 QImage TitleWidget::renderedPixmap()
1006 {
1007     int minX = 0;
1008     int minY = 0;
1009     int maxX = m_frameWidth;
1010     int maxY = m_frameHeight;
1011     if (!cropImage->isChecked()) {
1012         m_scene->removeItem(m_startViewport);
1013         m_scene->removeItem(m_endViewport);
1014         QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1015         if (boundingRect.left() < 0) minX = boundingRect.left();
1016         if (boundingRect.top() < 0) minY = boundingRect.top();
1017         if (boundingRect.right() > maxX) maxX = boundingRect.right();
1018         if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1019         if (minX < 0) {
1020             maxX = maxX - minX;
1021         }
1022         if (minY < 0) {
1023             maxY = maxY - minY;
1024         }
1025     }
1026
1027     QImage pix(maxX, maxY, QImage::Format_ARGB32);
1028     pix.fill(Qt::transparent);
1029     QPainter painter(&pix);
1030     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1031     m_scene->clearTextSelection();
1032     QPen framepen = m_frameBorder->pen();
1033     m_frameBorder->setPen(Qt::NoPen);
1034     m_startViewport->setVisible(false);
1035     m_endViewport->setVisible(false);
1036     m_frameImage->setVisible(false);
1037
1038     m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
1039     painter.end();
1040     m_frameBorder->setPen(framepen);
1041     m_startViewport->setVisible(true);
1042     m_endViewport->setVisible(true);
1043     m_frameImage->setVisible(true);
1044     return pix;
1045 }
1046
1047 /** \brief Connected to the accepted signal - calls writeChoices */
1048 void TitleWidget::slotAccepted()
1049 {
1050     writeChoices();
1051 }
1052
1053 /** \brief Store the current choices of font, background and rect values */
1054 void TitleWidget::writeChoices()
1055 {
1056     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1057     KSharedConfigPtr config = KGlobal::config();
1058     KConfigGroup titleConfig(config, "TitleWidget");
1059     // Write the entries
1060     titleConfig.writeEntry("font_family", font_family->currentFont());
1061     //titleConfig.writeEntry("font_size", font_size->value());
1062     titleConfig.writeEntry("font_pixel_size", font_size->value());
1063     titleConfig.writeEntry("font_color", fontColorButton->color());
1064     titleConfig.writeEntry("font_alpha", textAlpha->value());
1065     titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1066     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1067     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1068
1069     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1070     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1071     titleConfig.writeEntry("rect_background_color", rectBColor->color());
1072     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1073     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1074
1075     titleConfig.writeEntry("background_color", kcolorbutton->color());
1076     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1077
1078     titleConfig.writeEntry("crop_image", cropImage->isChecked());
1079     //! \todo Not sure if I should sync - it is probably safe to do it
1080     config->sync();
1081
1082 }
1083
1084 /** \brief Read the last stored choices into the dialog */
1085 void TitleWidget::readChoices()
1086 {
1087     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1088     KSharedConfigPtr config = KGlobal::config();
1089     KConfigGroup titleConfig(config, "TitleWidget");
1090     // read the entries
1091     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1092     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1093     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1094     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1095     buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1096     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1097     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1098
1099     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1100     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1101     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1102     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1103     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1104
1105     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1106     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1107
1108     cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));
1109 }
1110