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