]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
Restore zoom & rotation of objects:
[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
34 int settingUp = false;
35
36 const int IMAGEITEM = 7;
37 const int RECTITEM = 3;
38 const int TEXTITEM = 8;
39
40 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
41         QDialog(parent),
42         Ui::TitleWidget_UI(),
43         m_startViewport(NULL),
44         m_endViewport(NULL),
45         m_render(render),
46         m_count(0),
47         m_projectPath(projectPath)
48 {
49     setupUi(this);
50     setFont(KGlobalSettings::toolBarFont());
51     //toolBox->setFont(KGlobalSettings::toolBarFont());
52     frame_properties->setEnabled(false);
53     rect_properties->setFixedHeight(frame_properties->height() + 4);
54     text_properties->setFixedHeight(frame_properties->height() + 4);
55     m_frameWidth = render->renderWidth();
56     m_frameHeight = render->renderHeight();
57     //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
58     //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
59     // kcolorbutton == The color of the background
60     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
61     // horizontalslider == The alpha of the background
62     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
63     //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
64     //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
65
66
67     //ktextedit->setHidden(true);
68     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
69     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
70     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
71     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
72     //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
73
74     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
75     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
76     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
77     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
78     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
79
80     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
84     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
85     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
86
87     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
88     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
89     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
90     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
91     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
92
93     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
94     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
98     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
99     connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
100     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
103
104     // mbd
105     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
106
107     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
108     buttonRealSize->setIcon(KIcon("zoom-original"));
109     buttonBold->setIcon(KIcon("format-text-bold"));
110     buttonItalic->setIcon(KIcon("format-text-italic"));
111     buttonUnder->setIcon(KIcon("format-text-underline"));
112
113     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
114     itemhcenter->setToolTip(i18n("Align item horizontally"));
115     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
116     itemvcenter->setToolTip(i18n("Align item vertically"));
117
118     QHBoxLayout *layout = new QHBoxLayout;
119     frame_toolbar->setLayout(layout);
120     layout->setContentsMargins(2, 2, 2, 2);
121     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
122
123     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
124     m_buttonRect->setCheckable(true);
125     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
126
127     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
128     m_buttonText->setCheckable(true);
129     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
130
131     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
132     m_buttonImage->setCheckable(false);
133     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
134
135     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
136     m_buttonCursor->setCheckable(true);
137     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
138
139     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
140     m_buttonLoad->setCheckable(false);
141     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
142
143     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
144     m_buttonSave->setCheckable(false);
145     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
146
147     layout->addWidget(m_toolbar);
148     text_properties->setHidden(true);
149
150     // initialize graphic scene
151     m_scene = new GraphicsSceneRectMove(this);
152     graphicsView->setScene(m_scene);
153     m_titledocument.setScene(m_scene);
154
155     // a gradient background
156     /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
157     gradient->setSpread(QGradient::ReflectSpread);
158     scene->setBackgroundBrush(*gradient);*/
159
160     m_frameImage = new QGraphicsPixmapItem();
161     QTransform qtrans;
162     qtrans.scale(2.0, 2.0);
163     m_frameImage->setTransform(qtrans);
164     m_frameImage->setZValue(-1200);
165     m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
166     displayBackgroundFrame();
167     graphicsView->scene()->addItem(m_frameImage);
168
169     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
170     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
171     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
172     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
173     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
174     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
175     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
176     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
177
178     QPen framepen(Qt::DotLine);
179     framepen.setColor(Qt::red);
180
181     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
182     m_frameBorder->setPen(framepen);
183     m_frameBorder->setZValue(-1100);
184     m_frameBorder->setBrush(Qt::transparent);
185     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
186     graphicsView->scene()->addItem(m_frameBorder);
187
188     // mbd: load saved settings
189     readChoices();
190
191     initViewports();
192     QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
193     graphicsView->show();
194     //graphicsView->setRenderHint(QPainter::Antialiasing);
195     graphicsView->setInteractive(true);
196     //graphicsView->resize(400, 300);
197     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
198     toolBox->setItemEnabled(2, false);
199     if (!url.isEmpty()) {
200         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
201         slotSelectTool();
202     } else {
203         slotRectTool();
204     }
205 }
206
207 TitleWidget::~TitleWidget()
208 {
209     delete m_buttonRect;
210     delete m_buttonText;
211     delete m_buttonImage;
212     delete m_buttonCursor;
213     delete m_buttonSave;
214     delete m_buttonLoad;
215
216     delete m_frameBorder;
217     delete m_frameImage;
218     delete m_startViewport;
219     delete m_endViewport;
220     delete m_scene;
221 }
222
223 //static
224 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
225 {
226     QStringList result;
227     QString titlePath = projectUrl.path() + "/titles/";
228     KStandardDirs::makeDir(titlePath);
229     QString titleName = "title";
230     int counter = 0;
231     QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
232     while (QFile::exists(path + ".png")) {
233         counter++;
234         path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
235     }
236     result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
237     result.append(path + ".png");
238     return result;
239 }
240
241 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
242 {
243     QStringList result;
244     QString titlePath = projectUrl.path() + "/titles/";
245     KStandardDirs::makeDir(titlePath);
246     return titlePath + titleName + ".png";
247 }
248
249 //virtual
250 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
251 {
252     //slotAdjustZoom();
253 }
254
255 void TitleWidget::slotTextTool()
256 {
257     rect_properties->setHidden(true);
258     text_properties->setHidden(false);
259     m_scene->setTool(TITLE_TEXT);
260     m_buttonRect->setChecked(false);
261     m_buttonCursor->setChecked(false);
262 }
263
264 void TitleWidget::slotRectTool()
265 {
266     text_properties->setHidden(true);
267     rect_properties->setHidden(false);
268     m_scene->setTool(TITLE_RECTANGLE);
269     m_buttonText->setChecked(false);
270     m_buttonCursor->setChecked(false);
271     m_buttonRect->setChecked(true);
272 }
273
274 void TitleWidget::slotSelectTool()
275 {
276     m_scene->setTool(TITLE_SELECT);
277     m_buttonCursor->setChecked(true);
278     m_buttonText->setChecked(false);
279     m_buttonRect->setChecked(false);
280 }
281
282 void TitleWidget::slotImageTool()
283 {
284     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
285     if (!url.isEmpty()) {
286         if (url.path().endsWith(".svg")) {
287             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
288             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
289             svg->setZValue(m_count++);
290             svg->setData(Qt::UserRole, url.path());
291             graphicsView->scene()->addItem(svg);
292         } else {
293             QPixmap pix(url.path());
294             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
295             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
296             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
297             image->setData(Qt::UserRole, url.path());
298             image->setZValue(m_count++);
299             graphicsView->scene()->addItem(image);
300         }
301     }
302     m_scene->setTool(TITLE_SELECT);
303     m_buttonRect->setChecked(false);
304     m_buttonCursor->setChecked(true);
305     m_buttonText->setChecked(false);
306 }
307
308 void TitleWidget::displayBackgroundFrame()
309 {
310     if (!displayBg->isChecked()) {
311         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
312         QPixmap pattern(20, 20);
313         pattern.fill();
314         QPainter p;
315         p.begin(&pattern);
316         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
317         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
318         p.end();
319         QBrush br(pattern);
320
321         p.begin(&bg);
322         p.fillRect(bg.rect(), br);
323         p.end();
324         m_frameImage->setPixmap(bg);
325     } else {
326         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
327     }
328 }
329
330 void TitleWidget::initViewports()
331 {
332     m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
333     m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
334
335     QPen startpen(Qt::DotLine);
336     QPen endpen(Qt::DashDotLine);
337     startpen.setColor(QColor(100, 200, 100, 140));
338     endpen.setColor(QColor(200, 100, 100, 140));
339
340     m_startViewport->setPen(startpen);
341     m_endViewport->setPen(endpen);
342
343     startViewportSize->setValue(40);
344     endViewportSize->setValue(40);
345
346     m_startViewport->setZValue(-1000);
347     m_endViewport->setZValue(-1000);
348
349     m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
350     m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
351
352     graphicsView->scene()->addItem(m_startViewport);
353     graphicsView->scene()->addItem(m_endViewport);
354 }
355
356 void TitleWidget::slotUpdateZoom(int pos)
357 {
358     m_scene->setZoom((double) pos / 100);
359     zoom_label->setText(QString::number(pos) + '%');
360 }
361
362 void TitleWidget::slotZoom(bool up)
363 {
364     int pos = zoom_slider->value();
365     if (up) pos++;
366     else pos--;
367     zoom_slider->setValue(pos);
368 }
369
370 void TitleWidget::slotAdjustZoom()
371 {
372     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
373     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
374     if (scalex > scaley) scalex = scaley;
375     int zoompos = (int)(scalex * 7 + 0.5);*/
376     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
377     int zoompos = graphicsView->matrix().m11() * 100;
378     zoom_slider->setValue(zoompos);
379     graphicsView->centerOn(m_frameBorder);
380 }
381
382 void TitleWidget::slotZoomOneToOne()
383 {
384     zoom_slider->setValue(100);
385     graphicsView->centerOn(m_frameBorder);
386 }
387
388 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
389 {
390     QColor f = rectFColor->color();
391     f.setAlpha(rectFAlpha->value());
392     QPen penf(f);
393     penf.setWidth(rectLineWidth->value());
394     rect->setPen(penf);
395     QColor b = rectBColor->color();
396     b.setAlpha(rectBAlpha->value());
397     rect->setBrush(QBrush(b));
398     rect->setZValue(m_count++);
399     //setCurrentItem(rect);
400     //graphicsView->setFocus();
401 }
402
403 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
404 {
405     QFont font = font_family->currentFont();
406     font.setPixelSize(font_size->value());
407     // mbd: issue 551:
408     font.setBold(buttonBold->isChecked());
409     font.setItalic(buttonItalic->isChecked());
410     font.setUnderline(buttonUnder->isChecked());
411
412     tt->setFont(font);
413     QColor color = fontColorButton->color();
414     color.setAlpha(textAlpha->value());
415     tt->setDefaultTextColor(color);
416     tt->setZValue(m_count++);
417     setCurrentItem(tt);
418 }
419
420 void TitleWidget::setCurrentItem(QGraphicsItem *item)
421 {
422     m_scene->setSelectedItem(item);
423 }
424
425 void TitleWidget::zIndexChanged(int v)
426 {
427     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
428     if (l.size() >= 1) {
429         l[0]->setZValue(v);
430     }
431 }
432
433 void TitleWidget::selectionChanged()
434 {
435     if (m_scene->tool() != TITLE_SELECT) return;
436     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
437     //toolBox->setItemEnabled(2, false);
438     //toolBox->setItemEnabled(3, false);
439     value_x->blockSignals(true);
440     value_y->blockSignals(true);
441     value_w->blockSignals(true);
442     value_h->blockSignals(true);
443     itemzoom->blockSignals(true);
444     itemrotate->blockSignals(true);
445     if (l.size() == 1) {
446         if (l.at(0)->type() == TEXTITEM) {
447             rect_properties->setHidden(true);
448             text_properties->setHidden(false);
449             QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
450             //if (l[0]->hasFocus())
451             //ktextedit->setHtml(i->toHtml());
452             toolBox->setCurrentIndex(0);
453             //toolBox->setItemEnabled(2, true);
454             font_size->blockSignals(true);
455             font_family->blockSignals(true);
456             buttonBold->blockSignals(true);
457             buttonItalic->blockSignals(true);
458             buttonUnder->blockSignals(true);
459             fontColorButton->blockSignals(true);
460             textAlpha->blockSignals(true);
461
462             QFont font = i->font();
463             font_family->setCurrentFont(font);
464             font_size->setValue(font.pixelSize());
465             buttonBold->setChecked(font.bold());
466             buttonItalic->setChecked(font.italic());
467             buttonUnder->setChecked(font.underline());
468
469             QColor color = i->defaultTextColor();
470             fontColorButton->setColor(color);
471             textAlpha->setValue(color.alpha());
472
473             font_size->blockSignals(false);
474             font_family->blockSignals(false);
475             buttonBold->blockSignals(false);
476             buttonItalic->blockSignals(false);
477             buttonUnder->blockSignals(false);
478             fontColorButton->blockSignals(false);
479             textAlpha->blockSignals(false);
480
481             value_x->setValue((int) i->pos().x());
482             value_y->setValue((int) i->pos().y());
483             value_w->setValue((int) i->boundingRect().width());
484             value_h->setValue((int) i->boundingRect().height());
485             frame_properties->setEnabled(true);
486             value_w->setEnabled(false);
487             value_h->setEnabled(false);
488         } else if ((l.at(0))->type() == RECTITEM) {
489             rect_properties->setHidden(false);
490             text_properties->setHidden(true);
491             settingUp = true;
492             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
493             toolBox->setCurrentIndex(0);
494             //toolBox->setItemEnabled(3, true);
495             rectFAlpha->setValue(rec->pen().color().alpha());
496             rectBAlpha->setValue(rec->brush().color().alpha());
497             //kDebug() << rec->brush().color().alpha();
498             QColor fcol = rec->pen().color();
499             QColor bcol = rec->brush().color();
500             //fcol.setAlpha(255);
501             //bcol.setAlpha(255);
502             rectFColor->setColor(fcol);
503             rectBColor->setColor(bcol);
504             settingUp = false;
505             rectLineWidth->setValue(rec->pen().width());
506             value_x->setValue((int) rec->pos().x());
507             value_y->setValue((int) rec->pos().y());
508             value_w->setValue((int) rec->rect().width());
509             value_h->setValue((int) rec->rect().height());
510             frame_properties->setEnabled(true);
511             value_w->setEnabled(true);
512             value_h->setEnabled(true);
513         } else {
514             //toolBox->setCurrentIndex(0);
515             frame_properties->setEnabled(false);
516         }
517         zValue->setValue((int)l.at(0)->zValue());
518         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
519         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
520         value_x->blockSignals(false);
521         value_y->blockSignals(false);
522         value_w->blockSignals(false);
523         value_h->blockSignals(false);
524         itemzoom->blockSignals(false);
525         itemrotate->blockSignals(false);
526     } else frame_properties->setEnabled(false);
527 }
528
529 void TitleWidget::slotAdjustSelectedItem()
530 {
531     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
532     if (l.size() >= 1) {
533         if (l.at(0)->type() == RECTITEM) {
534             //rect item
535             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
536             rec->setPos(value_x->value(), value_y->value());
537             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
538         } else if (l.at(0)->type() == TEXTITEM) {
539             //text item
540             QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(l.at(0));
541             rec->setPos(value_x->value(), value_y->value());
542         }
543     }
544 }
545
546 void TitleWidget::slotChangeBackground()
547 {
548     QColor color = kcolorbutton->color();
549     color.setAlpha(horizontalSlider->value());
550     m_frameBorder->setBrush(QBrush(color));
551 }
552
553 void TitleWidget::textChanged()
554 {
555     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
556     if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
557         //kDebug() << ktextedit->document()->toHtml();
558         //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
559     }
560 }
561
562 void TitleWidget::slotUpdateText()
563 {
564     QFont font = font_family->currentFont();
565     font.setPixelSize(font_size->value());
566     font.setBold(buttonBold->isChecked());
567     font.setItalic(buttonItalic->isChecked());
568     font.setUnderline(buttonUnder->isChecked());
569     QColor color = fontColorButton->color();
570     color.setAlpha(textAlpha->value());
571
572     QGraphicsTextItem* item = NULL;
573     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
574     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
575         item = static_cast <QGraphicsTextItem *>(l.at(0));
576     }
577     if (!item) return;
578     //if (item->textCursor().selection ().isEmpty())
579     {
580         item->setFont(font);
581         item->setDefaultTextColor(color);
582     }
583     /*else {
584     QTextDocumentFragment selec = item->textCursor().selection ();
585     selec.set
586     }*/
587     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
588
589     //ktextedit->setCurrentFont(font);
590     //ktextedit->setTextColor(color);
591     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
592     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
593     QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
594     //item-
595     }*/
596 }
597
598 void TitleWidget::rectChanged()
599 {
600     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
601     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
602         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
603         QColor f = rectFColor->color();
604         f.setAlpha(rectFAlpha->value());
605         QPen penf(f);
606         penf.setWidth(rectLineWidth->value());
607         rec->setPen(penf);
608         QColor b = rectBColor->color();
609         b.setAlpha(rectBAlpha->value());
610         rec->setBrush(QBrush(b));
611     }
612 }
613
614 void TitleWidget::fontBold()
615 {
616     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
617     if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
618         //ktextedit->document()->setTextOption();
619     }
620 }
621
622 void TitleWidget::itemScaled(int val)
623 {
624     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
625     if (l.size() == 1) {
626         Transform x = m_transformations.value(l.at(0));
627         x.scalex = (double)val / 100.0;
628         x.scaley = (double)val / 100.0;
629         QTransform qtrans;
630         qtrans.scale(x.scalex, x.scaley);
631         qtrans.rotate(x.rotate);
632         l[0]->setTransform(qtrans);
633         m_transformations[l.at(0)] = x;
634     }
635 }
636
637 void TitleWidget::itemRotate(int val)
638 {
639     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
640     if (l.size() == 1) {
641         Transform x = m_transformations[l.at(0)];
642         x.rotate = (double)val;
643         QTransform qtrans;
644         qtrans.scale(x.scalex, x.scaley);
645         qtrans.rotate(x.rotate);
646         l[0]->setTransform(qtrans);
647         m_transformations[l.at(0)] = x;
648     }
649 }
650
651 void TitleWidget::itemHCenter()
652 {
653     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
654     if (l.size() == 1) {
655         QGraphicsItem *item = l.at(0);
656         QRectF br;
657         if (item->type() == RECTITEM) {
658             br = ((QGraphicsRectItem*)item)->rect();
659         } else br = item->sceneBoundingRect();
660         int width = (int) br.width();
661         int newPos = (int)((m_frameWidth - width) / 2);
662         item->setPos(newPos, item->pos().y());
663     }
664 }
665
666 void TitleWidget::itemVCenter()
667 {
668     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
669     if (l.size() == 1) {
670         QGraphicsItem *item = l.at(0);
671         QRectF br;
672         if (item->type() == RECTITEM) {
673             br = ((QGraphicsRectItem*)item)->rect();
674         } else br = item->sceneBoundingRect();
675         int height = (int) br.height();
676         int newPos = (int)((m_frameHeight - height) / 2);
677         item->setPos(item->pos().x(), newPos);
678     }
679 }
680
681 void TitleWidget::setupViewports()
682 {
683     double aspect_ratio = 4.0 / 3.0;//read from project
684
685     QRectF sp(0, 0, 0, 0);
686     QRectF ep(0, 0, 0, 0);
687
688     double sv_size = startViewportSize->value();
689     double ev_size = endViewportSize->value();
690     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
691     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
692
693     m_startViewport->setPos(startViewportX->value(), startViewportY->value());
694     m_endViewport->setPos(endViewportX->value(), endViewportY->value());
695
696     m_startViewport->setPolygon(QPolygonF(sp));
697     m_endViewport->setPolygon(QPolygonF(ep));
698
699 }
700
701 void TitleWidget::loadTitle()
702 {
703     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
704     if (!url.isEmpty()) {
705         QList<QGraphicsItem *> items = m_scene->items();
706         for (int i = 0; i < items.size(); i++) {
707             if (items.at(i)->zValue() > -1000) delete items.at(i);
708         }
709         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
710         slotSelectTool();
711     }
712 }
713
714 void TitleWidget::saveTitle(KUrl url)
715 {
716     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
717     if (!url.isEmpty()) {
718         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
719             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
720     }
721 }
722
723 QDomDocument TitleWidget::xml()
724 {
725     return m_titledocument.xml(m_startViewport, m_endViewport);
726 }
727
728 void TitleWidget::setXml(QDomDocument doc)
729 {
730     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
731     m_transformations.clear();
732     QList <QGraphicsItem *> items = graphicsView->scene()->items();
733     const double PI = 4.0 * atan(1.0);
734     for (int i = 0; i < items.count(); i++) {
735         QTransform t = items.at(i)->transform();
736         Transform x;
737         x.scalex = t.m11();
738         x.scaley = t.m22();
739         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
740         m_transformations[items.at(i)] = x;
741     }
742     // mbd: Update the GUI color selectors to match the stuff from the loaded document
743     QColor background_color = m_titledocument.getBackgroundColor();
744     horizontalSlider->blockSignals(true);
745     kcolorbutton->blockSignals(true);
746     horizontalSlider->setValue(background_color.alpha());
747     background_color.setAlpha(255);
748     kcolorbutton->setColor(background_color);
749     horizontalSlider->blockSignals(false);
750     kcolorbutton->blockSignals(false);
751
752     slotSelectTool();
753 }
754
755 QImage TitleWidget::renderedPixmap()
756 {
757     QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
758     pix.fill(Qt::transparent);
759     QPainter painter(&pix);
760     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
761     m_scene->clearTextSelection();
762     QPen framepen = m_frameBorder->pen();
763     m_frameBorder->setPen(Qt::NoPen);
764     m_startViewport->setVisible(false);
765     m_endViewport->setVisible(false);
766     m_frameImage->setVisible(false);
767
768     m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
769     painter.end();
770     m_frameBorder->setPen(framepen);
771     m_startViewport->setVisible(true);
772     m_endViewport->setVisible(true);
773     m_frameImage->setVisible(true);
774     return pix;
775 }
776
777 /** \brief Connected to the accepted signal - calls writeChoices */
778 void TitleWidget::slotAccepted()
779 {
780     writeChoices();
781 }
782
783 /** \brief Store the current choices of font, background and rect values */
784 void TitleWidget::writeChoices()
785 {
786     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
787     KSharedConfigPtr config = KGlobal::config();
788     KConfigGroup titleConfig(config, "TitleWidget");
789     // Write the entries
790     titleConfig.writeEntry("font_family", font_family->currentFont());
791     //titleConfig.writeEntry("font_size", font_size->value());
792     titleConfig.writeEntry("font_pixel_size", font_size->value());
793     titleConfig.writeEntry("font_color", fontColorButton->color());
794     titleConfig.writeEntry("font_alpha", textAlpha->value());
795     titleConfig.writeEntry("font_bold", buttonBold->isChecked());
796     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
797     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
798
799     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
800     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
801     titleConfig.writeEntry("rect_background_color", rectBColor->color());
802     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
803     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
804
805     titleConfig.writeEntry("background_color", kcolorbutton->color());
806     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
807     //! \todo Not sure if I should sync - it is probably safe to do it
808     config->sync();
809
810 }
811
812 /** \brief Read the last stored choices into the dialog */
813 void TitleWidget::readChoices()
814 {
815     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
816     KSharedConfigPtr config = KGlobal::config();
817     KConfigGroup titleConfig(config, "TitleWidget");
818     // read the entries
819     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
820     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
821     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
822     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
823     buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
824     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
825     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
826
827     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
828     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
829     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
830     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
831     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
832
833     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
834     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
835 }
836