]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
New features: Titles can now be larger than the frame size. Then, you can add a compo...
[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     //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
66     //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
67
68
69     //ktextedit->setHidden(true);
70     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
71     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
72     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
73     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
74     //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
75
76     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
77     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
78     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
79     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
80     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
81
82     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
84     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
85     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
86     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
87     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
88
89     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
90     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
91     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
92     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
93     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
94
95     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
98     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
99     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
100     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
101     connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104     connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
105     connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
106     connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
107     connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
108     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
109
110     // mbd
111     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
112
113     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
114     buttonRealSize->setIcon(KIcon("zoom-original"));
115     buttonBold->setIcon(KIcon("format-text-bold"));
116     buttonItalic->setIcon(KIcon("format-text-italic"));
117     buttonUnder->setIcon(KIcon("format-text-underline"));
118     buttonAlignCenter->setIcon(KIcon("format-justify-center"));
119     buttonAlignLeft->setIcon(KIcon("format-justify-left"));
120     buttonAlignRight->setIcon(KIcon("format-justify-right"));
121     buttonAlignNone->setToolTip(i18n("No alignment"));
122     buttonAlignRight->setToolTip(i18n("Align right"));
123     buttonAlignLeft->setToolTip(i18n("Align left"));
124     buttonAlignCenter->setToolTip(i18n("Align center"));
125
126     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
127     itemhcenter->setToolTip(i18n("Align item horizontally"));
128     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
129     itemvcenter->setToolTip(i18n("Align item vertically"));
130
131     QHBoxLayout *layout = new QHBoxLayout;
132     frame_toolbar->setLayout(layout);
133     layout->setContentsMargins(2, 2, 2, 2);
134     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
135
136     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
137     m_buttonRect->setCheckable(true);
138     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
139
140     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
141     m_buttonText->setCheckable(true);
142     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
143
144     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
145     m_buttonImage->setCheckable(false);
146     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
147
148     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
149     m_buttonCursor->setCheckable(true);
150     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
151
152     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
153     m_buttonLoad->setCheckable(false);
154     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
155
156     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
157     m_buttonSave->setCheckable(false);
158     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
159
160     layout->addWidget(m_toolbar);
161     text_properties->setHidden(true);
162
163     // initialize graphic scene
164     m_scene = new GraphicsSceneRectMove(this);
165     graphicsView->setScene(m_scene);
166     m_titledocument.setScene(m_scene);
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() == 1) {
459         if (l.at(0)->type() == TEXTITEM) {
460             rect_properties->setHidden(true);
461             text_properties->setHidden(false);
462             QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
463             //if (l[0]->hasFocus())
464             //ktextedit->setHtml(i->toHtml());
465             toolBox->setCurrentIndex(0);
466             //toolBox->setItemEnabled(2, true);
467             font_size->blockSignals(true);
468             font_family->blockSignals(true);
469             buttonBold->blockSignals(true);
470             buttonItalic->blockSignals(true);
471             buttonUnder->blockSignals(true);
472             fontColorButton->blockSignals(true);
473             textAlpha->blockSignals(true);
474             buttonAlignLeft->blockSignals(true);
475             buttonAlignRight->blockSignals(true);
476             buttonAlignNone->blockSignals(true);
477             buttonAlignCenter->blockSignals(true);
478
479             QFont font = i->font();
480             font_family->setCurrentFont(font);
481             font_size->setValue(font.pixelSize());
482             buttonBold->setChecked(font.bold());
483             buttonItalic->setChecked(font.italic());
484             buttonUnder->setChecked(font.underline());
485
486             QColor color = i->defaultTextColor();
487             fontColorButton->setColor(color);
488             textAlpha->setValue(color.alpha());
489
490             QTextCursor cur = i->textCursor();
491             QTextBlockFormat format = cur.blockFormat();
492             if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
493             else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
494             else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
495             else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
496
497             font_size->blockSignals(false);
498             font_family->blockSignals(false);
499             buttonBold->blockSignals(false);
500             buttonItalic->blockSignals(false);
501             buttonUnder->blockSignals(false);
502             fontColorButton->blockSignals(false);
503             textAlpha->blockSignals(false);
504             buttonAlignLeft->blockSignals(false);
505             buttonAlignRight->blockSignals(false);
506             buttonAlignNone->blockSignals(false);
507             buttonAlignCenter->blockSignals(false);
508
509             value_x->setValue((int) i->pos().x());
510             value_y->setValue((int) i->pos().y());
511             value_w->setValue((int) i->boundingRect().width());
512             value_h->setValue((int) i->boundingRect().height());
513             frame_properties->setEnabled(true);
514             value_w->setEnabled(false);
515             value_h->setEnabled(false);
516         } else if ((l.at(0))->type() == RECTITEM) {
517             rect_properties->setHidden(false);
518             text_properties->setHidden(true);
519             settingUp = true;
520             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
521             toolBox->setCurrentIndex(0);
522             //toolBox->setItemEnabled(3, true);
523             rectFAlpha->setValue(rec->pen().color().alpha());
524             rectBAlpha->setValue(rec->brush().color().alpha());
525             //kDebug() << rec->brush().color().alpha();
526             QColor fcol = rec->pen().color();
527             QColor bcol = rec->brush().color();
528             //fcol.setAlpha(255);
529             //bcol.setAlpha(255);
530             rectFColor->setColor(fcol);
531             rectBColor->setColor(bcol);
532             settingUp = false;
533             rectLineWidth->setValue(rec->pen().width());
534             value_x->setValue((int) rec->pos().x());
535             value_y->setValue((int) rec->pos().y());
536             value_w->setValue((int) rec->rect().width());
537             value_h->setValue((int) rec->rect().height());
538             frame_properties->setEnabled(true);
539             value_w->setEnabled(true);
540             value_h->setEnabled(true);
541         } else {
542             //toolBox->setCurrentIndex(0);
543             frame_properties->setEnabled(false);
544         }
545         zValue->setValue((int)l.at(0)->zValue());
546         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
547         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
548         value_x->blockSignals(false);
549         value_y->blockSignals(false);
550         value_w->blockSignals(false);
551         value_h->blockSignals(false);
552         itemzoom->blockSignals(false);
553         itemrotate->blockSignals(false);
554     } else frame_properties->setEnabled(false);
555 }
556
557 void TitleWidget::slotAdjustSelectedItem()
558 {
559     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
560     if (l.size() >= 1) {
561         if (l.at(0)->type() == RECTITEM) {
562             //rect item
563             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
564             rec->setPos(value_x->value(), value_y->value());
565             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
566         } else if (l.at(0)->type() == TEXTITEM) {
567             //text item
568             QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(l.at(0));
569             rec->setPos(value_x->value(), value_y->value());
570         }
571     }
572 }
573
574 void TitleWidget::slotChangeBackground()
575 {
576     QColor color = kcolorbutton->color();
577     color.setAlpha(horizontalSlider->value());
578     m_frameBorder->setBrush(QBrush(color));
579 }
580
581 void TitleWidget::textChanged()
582 {
583     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
584     if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
585         //kDebug() << ktextedit->document()->toHtml();
586         //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
587     }
588 }
589
590 void TitleWidget::slotUpdateText()
591 {
592     QFont font = font_family->currentFont();
593     font.setPixelSize(font_size->value());
594     font.setBold(buttonBold->isChecked());
595     font.setItalic(buttonItalic->isChecked());
596     font.setUnderline(buttonUnder->isChecked());
597     QColor color = fontColorButton->color();
598     color.setAlpha(textAlpha->value());
599
600     QGraphicsTextItem* item = NULL;
601     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
602     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
603         item = static_cast <QGraphicsTextItem *>(l.at(0));
604     }
605     if (!item) return;
606     //if (item->textCursor().selection ().isEmpty())
607     QTextCursor cur = item->textCursor();
608     QTextBlockFormat format = cur.blockFormat();
609     if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
610         item->setTextWidth(item->boundingRect().width());
611         if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
612         else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
613         else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
614     } else {
615         format.setAlignment(Qt::AlignLeft);
616         item->setTextWidth(-1);
617     }
618
619     {
620         item->setFont(font);
621         item->setDefaultTextColor(color);
622         cur.select(QTextCursor::Document);
623         cur.setBlockFormat(format);
624         item->setTextCursor(cur);
625         cur.clearSelection();
626         item->setTextCursor(cur);
627
628     }
629     /*else {
630     QTextDocumentFragment selec = item->textCursor().selection ();
631     selec.set
632     }*/
633     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
634
635     //ktextedit->setCurrentFont(font);
636     //ktextedit->setTextColor(color);
637     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
638     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
639     QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
640     //item-
641     }*/
642 }
643
644 void TitleWidget::rectChanged()
645 {
646     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
647     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
648         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
649         QColor f = rectFColor->color();
650         f.setAlpha(rectFAlpha->value());
651         QPen penf(f);
652         penf.setWidth(rectLineWidth->value());
653         rec->setPen(penf);
654         QColor b = rectBColor->color();
655         b.setAlpha(rectBAlpha->value());
656         rec->setBrush(QBrush(b));
657     }
658 }
659
660 void TitleWidget::fontBold()
661 {
662     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
663     if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
664         //ktextedit->document()->setTextOption();
665     }
666 }
667
668 void TitleWidget::itemScaled(int val)
669 {
670     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
671     if (l.size() == 1) {
672         Transform x = m_transformations.value(l.at(0));
673         x.scalex = (double)val / 100.0;
674         x.scaley = (double)val / 100.0;
675         QTransform qtrans;
676         qtrans.scale(x.scalex, x.scaley);
677         qtrans.rotate(x.rotate);
678         l[0]->setTransform(qtrans);
679         m_transformations[l.at(0)] = x;
680     }
681 }
682
683 void TitleWidget::itemRotate(int val)
684 {
685     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
686     if (l.size() == 1) {
687         Transform x = m_transformations[l.at(0)];
688         x.rotate = (double)val;
689         QTransform qtrans;
690         qtrans.scale(x.scalex, x.scaley);
691         qtrans.rotate(x.rotate);
692         l[0]->setTransform(qtrans);
693         m_transformations[l.at(0)] = x;
694     }
695 }
696
697 void TitleWidget::itemHCenter()
698 {
699     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
700     if (l.size() == 1) {
701         QGraphicsItem *item = l.at(0);
702         QRectF br;
703         if (item->type() == RECTITEM) {
704             br = ((QGraphicsRectItem*)item)->rect();
705         } else br = item->sceneBoundingRect();
706         int width = (int) br.width();
707         int newPos = (int)((m_frameWidth - width) / 2);
708         item->setPos(newPos, item->pos().y());
709     }
710 }
711
712 void TitleWidget::itemVCenter()
713 {
714     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
715     if (l.size() == 1) {
716         QGraphicsItem *item = l.at(0);
717         QRectF br;
718         if (item->type() == RECTITEM) {
719             br = ((QGraphicsRectItem*)item)->rect();
720         } else br = item->sceneBoundingRect();
721         int height = (int) br.height();
722         int newPos = (int)((m_frameHeight - height) / 2);
723         item->setPos(item->pos().x(), newPos);
724     }
725 }
726
727 void TitleWidget::setupViewports()
728 {
729     double aspect_ratio = 4.0 / 3.0;//read from project
730
731     QRectF sp(0, 0, 0, 0);
732     QRectF ep(0, 0, 0, 0);
733
734     double sv_size = startViewportSize->value();
735     double ev_size = endViewportSize->value();
736     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
737     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
738
739     m_startViewport->setPos(startViewportX->value(), startViewportY->value());
740     m_endViewport->setPos(endViewportX->value(), endViewportY->value());
741
742     m_startViewport->setPolygon(QPolygonF(sp));
743     m_endViewport->setPolygon(QPolygonF(ep));
744
745 }
746
747 void TitleWidget::loadTitle()
748 {
749     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
750     if (!url.isEmpty()) {
751         QList<QGraphicsItem *> items = m_scene->items();
752         for (int i = 0; i < items.size(); i++) {
753             if (items.at(i)->zValue() > -1000) delete items.at(i);
754         }
755         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
756         slotSelectTool();
757     }
758 }
759
760 void TitleWidget::saveTitle(KUrl url)
761 {
762     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
763     if (!url.isEmpty()) {
764         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
765             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
766     }
767 }
768
769 QDomDocument TitleWidget::xml()
770 {
771     QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
772     if (cropImage->isChecked()) {
773         doc.documentElement().setAttribute("crop", 1);
774     }
775     return doc;
776 }
777
778 void TitleWidget::setXml(QDomDocument doc)
779 {
780     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
781     QDomElement e = doc.documentElement();
782     cropImage->setChecked(e.hasAttribute("crop"));
783     m_transformations.clear();
784     QList <QGraphicsItem *> items = graphicsView->scene()->items();
785     const double PI = 4.0 * atan(1.0);
786     for (int i = 0; i < items.count(); i++) {
787         QTransform t = items.at(i)->transform();
788         Transform x;
789         x.scalex = t.m11();
790         x.scaley = t.m22();
791         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
792         m_transformations[items.at(i)] = x;
793     }
794     // mbd: Update the GUI color selectors to match the stuff from the loaded document
795     QColor background_color = m_titledocument.getBackgroundColor();
796     horizontalSlider->blockSignals(true);
797     kcolorbutton->blockSignals(true);
798     horizontalSlider->setValue(background_color.alpha());
799     background_color.setAlpha(255);
800     kcolorbutton->setColor(background_color);
801     horizontalSlider->blockSignals(false);
802     kcolorbutton->blockSignals(false);
803
804     slotSelectTool();
805 }
806
807 QImage TitleWidget::renderedPixmap()
808 {
809     int minX = 0;
810     int minY = 0;
811     int maxX = m_frameWidth;
812     int maxY = m_frameHeight;
813     if (!cropImage->isChecked()) {
814         m_scene->removeItem(m_startViewport);
815         m_scene->removeItem(m_endViewport);
816         QRect boundingRect = m_scene->itemsBoundingRect().toRect();
817         if (boundingRect.left() < 0) minX = boundingRect.left();
818         if (boundingRect.top() < 0) minY = boundingRect.top();
819         if (boundingRect.right() > maxX) maxX = boundingRect.right();
820         if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
821         if (minX < 0) {
822             maxX = maxX - minX;
823         }
824         if (minY < 0) {
825             maxY = maxY - minY;
826         }
827     }
828
829     QImage pix(maxX, maxY, QImage::Format_ARGB32);
830     pix.fill(Qt::transparent);
831     QPainter painter(&pix);
832     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
833     m_scene->clearTextSelection();
834     QPen framepen = m_frameBorder->pen();
835     m_frameBorder->setPen(Qt::NoPen);
836     m_startViewport->setVisible(false);
837     m_endViewport->setVisible(false);
838     m_frameImage->setVisible(false);
839
840     m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
841     painter.end();
842     m_frameBorder->setPen(framepen);
843     m_startViewport->setVisible(true);
844     m_endViewport->setVisible(true);
845     m_frameImage->setVisible(true);
846     return pix;
847 }
848
849 /** \brief Connected to the accepted signal - calls writeChoices */
850 void TitleWidget::slotAccepted()
851 {
852     writeChoices();
853 }
854
855 /** \brief Store the current choices of font, background and rect values */
856 void TitleWidget::writeChoices()
857 {
858     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
859     KSharedConfigPtr config = KGlobal::config();
860     KConfigGroup titleConfig(config, "TitleWidget");
861     // Write the entries
862     titleConfig.writeEntry("font_family", font_family->currentFont());
863     //titleConfig.writeEntry("font_size", font_size->value());
864     titleConfig.writeEntry("font_pixel_size", font_size->value());
865     titleConfig.writeEntry("font_color", fontColorButton->color());
866     titleConfig.writeEntry("font_alpha", textAlpha->value());
867     titleConfig.writeEntry("font_bold", buttonBold->isChecked());
868     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
869     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
870
871     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
872     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
873     titleConfig.writeEntry("rect_background_color", rectBColor->color());
874     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
875     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
876
877     titleConfig.writeEntry("background_color", kcolorbutton->color());
878     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
879
880     titleConfig.writeEntry("crop_image", cropImage->isChecked());
881     //! \todo Not sure if I should sync - it is probably safe to do it
882     config->sync();
883
884 }
885
886 /** \brief Read the last stored choices into the dialog */
887 void TitleWidget::readChoices()
888 {
889     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
890     KSharedConfigPtr config = KGlobal::config();
891     KConfigGroup titleConfig(config, "TitleWidget");
892     // read the entries
893     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
894     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
895     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
896     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
897     buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
898     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
899     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
900
901     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
902     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
903     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
904     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
905     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
906
907     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
908     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
909
910     cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));
911 }
912