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