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