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