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