1 /***************************************************************************
2 titlewidget.h - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "titlewidget.h"
19 #include "kdenlivesettings.h"
22 #include <KGlobalSettings>
23 #include <KFileDialog>
24 #include <KStandardDirs>
25 #include <KMessageBox>
27 #include <QDomDocument>
28 #include <QGraphicsItem>
29 #include <QGraphicsSvgItem>
34 int settingUp = false;
36 const int IMAGEITEM = 7;
37 const int RECTITEM = 3;
38 const int TEXTITEM = 8;
40 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
43 m_startViewport(NULL),
47 m_projectPath(projectPath)
50 setFont(KGlobalSettings::toolBarFont());
51 //toolBox->setFont(KGlobalSettings::toolBarFont());
52 frame_properties->setEnabled(false);
53 rect_properties->setFixedHeight(frame_properties->height() + 4);
54 text_properties->setFixedHeight(frame_properties->height() + 4);
55 m_frameWidth = render->renderWidth();
56 m_frameHeight = render->renderHeight();
57 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
58 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
59 // kcolorbutton == The color of the background
60 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
61 // horizontalslider == The alpha of the background
62 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
63 //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
64 //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
67 //ktextedit->setHidden(true);
68 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
69 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
70 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
71 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
72 //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
74 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
75 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
76 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
77 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
78 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
80 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
84 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
85 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
87 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
88 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
89 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
90 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
91 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
93 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
94 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
98 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
99 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
100 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
105 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
107 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
108 buttonRealSize->setIcon(KIcon("zoom-original"));
109 buttonBold->setIcon(KIcon("format-text-bold"));
110 buttonItalic->setIcon(KIcon("format-text-italic"));
111 buttonUnder->setIcon(KIcon("format-text-underline"));
113 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
114 itemhcenter->setToolTip(i18n("Align item horizontally"));
115 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
116 itemvcenter->setToolTip(i18n("Align item vertically"));
118 QHBoxLayout *layout = new QHBoxLayout;
119 frame_toolbar->setLayout(layout);
120 layout->setContentsMargins(2, 2, 2, 2);
121 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
123 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
124 m_buttonRect->setCheckable(true);
125 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
127 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
128 m_buttonText->setCheckable(true);
129 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
131 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
132 m_buttonImage->setCheckable(false);
133 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
135 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
136 m_buttonCursor->setCheckable(true);
137 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
139 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
140 m_buttonLoad->setCheckable(false);
141 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
143 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
144 m_buttonSave->setCheckable(false);
145 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
147 layout->addWidget(m_toolbar);
148 text_properties->setHidden(true);
150 // initialize graphic scene
151 m_scene = new GraphicsSceneRectMove(this);
152 graphicsView->setScene(m_scene);
153 m_titledocument.setScene(m_scene);
155 // a gradient background
156 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
157 gradient->setSpread(QGradient::ReflectSpread);
158 scene->setBackgroundBrush(*gradient);*/
160 m_frameImage = new QGraphicsPixmapItem();
162 qtrans.scale(2.0, 2.0);
163 m_frameImage->setTransform(qtrans);
164 m_frameImage->setZValue(-1200);
165 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
166 displayBackgroundFrame();
167 graphicsView->scene()->addItem(m_frameImage);
169 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
170 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
171 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
172 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
173 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
174 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
175 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
176 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
178 QPen framepen(Qt::DotLine);
179 framepen.setColor(Qt::red);
181 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
182 m_frameBorder->setPen(framepen);
183 m_frameBorder->setZValue(-1100);
184 m_frameBorder->setBrush(Qt::transparent);
185 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
186 graphicsView->scene()->addItem(m_frameBorder);
188 // mbd: load saved settings
192 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
193 graphicsView->show();
194 //graphicsView->setRenderHint(QPainter::Antialiasing);
195 graphicsView->setInteractive(true);
196 //graphicsView->resize(400, 300);
197 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
198 toolBox->setItemEnabled(2, false);
199 if (!url.isEmpty()) {
200 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
207 TitleWidget::~TitleWidget()
211 delete m_buttonImage;
212 delete m_buttonCursor;
216 delete m_frameBorder;
218 delete m_startViewport;
219 delete m_endViewport;
224 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
227 QString titlePath = projectUrl.path() + "/titles/";
228 KStandardDirs::makeDir(titlePath);
229 QString titleName = "title";
231 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
232 while (QFile::exists(path + ".png")) {
234 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
236 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
237 result.append(path + ".png");
241 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
244 QString titlePath = projectUrl.path() + "/titles/";
245 KStandardDirs::makeDir(titlePath);
246 return titlePath + titleName + ".png";
250 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
255 void TitleWidget::slotTextTool()
257 rect_properties->setHidden(true);
258 text_properties->setHidden(false);
259 m_scene->setTool(TITLE_TEXT);
260 m_buttonRect->setChecked(false);
261 m_buttonCursor->setChecked(false);
264 void TitleWidget::slotRectTool()
266 text_properties->setHidden(true);
267 rect_properties->setHidden(false);
268 m_scene->setTool(TITLE_RECTANGLE);
269 m_buttonText->setChecked(false);
270 m_buttonCursor->setChecked(false);
271 m_buttonRect->setChecked(true);
274 void TitleWidget::slotSelectTool()
276 m_scene->setTool(TITLE_SELECT);
277 m_buttonCursor->setChecked(true);
278 m_buttonText->setChecked(false);
279 m_buttonRect->setChecked(false);
282 void TitleWidget::slotImageTool()
284 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
285 if (!url.isEmpty()) {
286 if (url.path().endsWith(".svg")) {
287 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
288 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
289 svg->setZValue(m_count++);
290 svg->setData(Qt::UserRole, url.path());
291 graphicsView->scene()->addItem(svg);
293 QPixmap pix(url.path());
294 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
295 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
296 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
297 image->setData(Qt::UserRole, url.path());
298 image->setZValue(m_count++);
299 graphicsView->scene()->addItem(image);
302 m_scene->setTool(TITLE_SELECT);
303 m_buttonRect->setChecked(false);
304 m_buttonCursor->setChecked(true);
305 m_buttonText->setChecked(false);
308 void TitleWidget::displayBackgroundFrame()
310 if (!displayBg->isChecked()) {
311 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
312 QPixmap pattern(20, 20);
316 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
317 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
322 p.fillRect(bg.rect(), br);
324 m_frameImage->setPixmap(bg);
326 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
330 void TitleWidget::initViewports()
332 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
333 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
335 QPen startpen(Qt::DotLine);
336 QPen endpen(Qt::DashDotLine);
337 startpen.setColor(QColor(100, 200, 100, 140));
338 endpen.setColor(QColor(200, 100, 100, 140));
340 m_startViewport->setPen(startpen);
341 m_endViewport->setPen(endpen);
343 startViewportSize->setValue(40);
344 endViewportSize->setValue(40);
346 m_startViewport->setZValue(-1000);
347 m_endViewport->setZValue(-1000);
349 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
350 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
352 graphicsView->scene()->addItem(m_startViewport);
353 graphicsView->scene()->addItem(m_endViewport);
356 void TitleWidget::slotUpdateZoom(int pos)
358 m_scene->setZoom((double) pos / 100);
359 zoom_label->setText(QString::number(pos) + '%');
362 void TitleWidget::slotZoom(bool up)
364 int pos = zoom_slider->value();
367 zoom_slider->setValue(pos);
370 void TitleWidget::slotAdjustZoom()
372 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
373 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
374 if (scalex > scaley) scalex = scaley;
375 int zoompos = (int)(scalex * 7 + 0.5);*/
376 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
377 int zoompos = graphicsView->matrix().m11() * 100;
378 zoom_slider->setValue(zoompos);
379 graphicsView->centerOn(m_frameBorder);
382 void TitleWidget::slotZoomOneToOne()
384 zoom_slider->setValue(100);
385 graphicsView->centerOn(m_frameBorder);
388 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
390 QColor f = rectFColor->color();
391 f.setAlpha(rectFAlpha->value());
393 penf.setWidth(rectLineWidth->value());
395 QColor b = rectBColor->color();
396 b.setAlpha(rectBAlpha->value());
397 rect->setBrush(QBrush(b));
398 rect->setZValue(m_count++);
399 //setCurrentItem(rect);
400 //graphicsView->setFocus();
403 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
405 QFont font = font_family->currentFont();
406 font.setPixelSize(font_size->value());
408 font.setBold(buttonBold->isChecked());
409 font.setItalic(buttonItalic->isChecked());
410 font.setUnderline(buttonUnder->isChecked());
413 QColor color = fontColorButton->color();
414 color.setAlpha(textAlpha->value());
415 tt->setDefaultTextColor(color);
416 tt->setZValue(m_count++);
420 void TitleWidget::setCurrentItem(QGraphicsItem *item)
422 m_scene->setSelectedItem(item);
425 void TitleWidget::zIndexChanged(int v)
427 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
433 void TitleWidget::selectionChanged()
435 if (m_scene->tool() != TITLE_SELECT) return;
436 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
437 //toolBox->setItemEnabled(2, false);
438 //toolBox->setItemEnabled(3, false);
439 value_x->blockSignals(true);
440 value_y->blockSignals(true);
441 value_w->blockSignals(true);
442 value_h->blockSignals(true);
443 itemzoom->blockSignals(true);
444 itemrotate->blockSignals(true);
446 if (l.at(0)->type() == TEXTITEM) {
447 rect_properties->setHidden(true);
448 text_properties->setHidden(false);
449 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
450 //if (l[0]->hasFocus())
451 //ktextedit->setHtml(i->toHtml());
452 toolBox->setCurrentIndex(0);
453 //toolBox->setItemEnabled(2, true);
454 font_size->blockSignals(true);
455 font_family->blockSignals(true);
456 buttonBold->blockSignals(true);
457 buttonItalic->blockSignals(true);
458 buttonUnder->blockSignals(true);
459 fontColorButton->blockSignals(true);
460 textAlpha->blockSignals(true);
462 QFont font = i->font();
463 font_family->setCurrentFont(font);
464 font_size->setValue(font.pixelSize());
465 buttonBold->setChecked(font.bold());
466 buttonItalic->setChecked(font.italic());
467 buttonUnder->setChecked(font.underline());
469 QColor color = i->defaultTextColor();
470 fontColorButton->setColor(color);
471 textAlpha->setValue(color.alpha());
473 font_size->blockSignals(false);
474 font_family->blockSignals(false);
475 buttonBold->blockSignals(false);
476 buttonItalic->blockSignals(false);
477 buttonUnder->blockSignals(false);
478 fontColorButton->blockSignals(false);
479 textAlpha->blockSignals(false);
481 value_x->setValue((int) i->pos().x());
482 value_y->setValue((int) i->pos().y());
483 value_w->setValue((int) i->boundingRect().width());
484 value_h->setValue((int) i->boundingRect().height());
485 frame_properties->setEnabled(true);
486 value_w->setEnabled(false);
487 value_h->setEnabled(false);
488 } else if ((l.at(0))->type() == RECTITEM) {
489 rect_properties->setHidden(false);
490 text_properties->setHidden(true);
492 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
493 toolBox->setCurrentIndex(0);
494 //toolBox->setItemEnabled(3, true);
495 rectFAlpha->setValue(rec->pen().color().alpha());
496 rectBAlpha->setValue(rec->brush().color().alpha());
497 //kDebug() << rec->brush().color().alpha();
498 QColor fcol = rec->pen().color();
499 QColor bcol = rec->brush().color();
500 //fcol.setAlpha(255);
501 //bcol.setAlpha(255);
502 rectFColor->setColor(fcol);
503 rectBColor->setColor(bcol);
505 rectLineWidth->setValue(rec->pen().width());
506 value_x->setValue((int) rec->pos().x());
507 value_y->setValue((int) rec->pos().y());
508 value_w->setValue((int) rec->rect().width());
509 value_h->setValue((int) rec->rect().height());
510 frame_properties->setEnabled(true);
511 value_w->setEnabled(true);
512 value_h->setEnabled(true);
514 //toolBox->setCurrentIndex(0);
515 frame_properties->setEnabled(false);
517 zValue->setValue((int)l.at(0)->zValue());
518 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
519 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
520 value_x->blockSignals(false);
521 value_y->blockSignals(false);
522 value_w->blockSignals(false);
523 value_h->blockSignals(false);
524 itemzoom->blockSignals(false);
525 itemrotate->blockSignals(false);
526 } else frame_properties->setEnabled(false);
529 void TitleWidget::slotAdjustSelectedItem()
531 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
533 if (l.at(0)->type() == RECTITEM) {
535 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
536 rec->setPos(value_x->value(), value_y->value());
537 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
538 } else if (l.at(0)->type() == TEXTITEM) {
540 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(l.at(0));
541 rec->setPos(value_x->value(), value_y->value());
546 void TitleWidget::slotChangeBackground()
548 QColor color = kcolorbutton->color();
549 color.setAlpha(horizontalSlider->value());
550 m_frameBorder->setBrush(QBrush(color));
553 void TitleWidget::textChanged()
555 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
556 if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
557 //kDebug() << ktextedit->document()->toHtml();
558 //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
562 void TitleWidget::slotUpdateText()
564 QFont font = font_family->currentFont();
565 font.setPixelSize(font_size->value());
566 font.setBold(buttonBold->isChecked());
567 font.setItalic(buttonItalic->isChecked());
568 font.setUnderline(buttonUnder->isChecked());
569 QColor color = fontColorButton->color();
570 color.setAlpha(textAlpha->value());
572 QGraphicsTextItem* item = NULL;
573 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
574 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
575 item = static_cast <QGraphicsTextItem *>(l.at(0));
578 //if (item->textCursor().selection ().isEmpty())
581 item->setDefaultTextColor(color);
584 QTextDocumentFragment selec = item->textCursor().selection ();
587 //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
589 //ktextedit->setCurrentFont(font);
590 //ktextedit->setTextColor(color);
591 /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
592 if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
593 QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
598 void TitleWidget::rectChanged()
600 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
601 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
602 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
603 QColor f = rectFColor->color();
604 f.setAlpha(rectFAlpha->value());
606 penf.setWidth(rectLineWidth->value());
608 QColor b = rectBColor->color();
609 b.setAlpha(rectBAlpha->value());
610 rec->setBrush(QBrush(b));
614 void TitleWidget::fontBold()
616 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
617 if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
618 //ktextedit->document()->setTextOption();
622 void TitleWidget::itemScaled(int val)
624 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
626 Transform x = m_transformations.value(l.at(0));
627 x.scalex = (double)val / 100.0;
628 x.scaley = (double)val / 100.0;
630 qtrans.scale(x.scalex, x.scaley);
631 qtrans.rotate(x.rotate);
632 l[0]->setTransform(qtrans);
633 m_transformations[l.at(0)] = x;
637 void TitleWidget::itemRotate(int val)
639 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
641 Transform x = m_transformations[l.at(0)];
642 x.rotate = (double)val;
644 qtrans.scale(x.scalex, x.scaley);
645 qtrans.rotate(x.rotate);
646 l[0]->setTransform(qtrans);
647 m_transformations[l.at(0)] = x;
651 void TitleWidget::itemHCenter()
653 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
655 QGraphicsItem *item = l.at(0);
657 if (item->type() == RECTITEM) {
658 br = ((QGraphicsRectItem*)item)->rect();
659 } else br = item->sceneBoundingRect();
660 int width = (int) br.width();
661 int newPos = (int)((m_frameWidth - width) / 2);
662 item->setPos(newPos, item->pos().y());
666 void TitleWidget::itemVCenter()
668 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
670 QGraphicsItem *item = l.at(0);
672 if (item->type() == RECTITEM) {
673 br = ((QGraphicsRectItem*)item)->rect();
674 } else br = item->sceneBoundingRect();
675 int height = (int) br.height();
676 int newPos = (int)((m_frameHeight - height) / 2);
677 item->setPos(item->pos().x(), newPos);
681 void TitleWidget::setupViewports()
683 double aspect_ratio = 4.0 / 3.0;//read from project
685 QRectF sp(0, 0, 0, 0);
686 QRectF ep(0, 0, 0, 0);
688 double sv_size = startViewportSize->value();
689 double ev_size = endViewportSize->value();
690 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
691 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
693 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
694 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
696 m_startViewport->setPolygon(QPolygonF(sp));
697 m_endViewport->setPolygon(QPolygonF(ep));
701 void TitleWidget::loadTitle()
703 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
704 if (!url.isEmpty()) {
705 QList<QGraphicsItem *> items = m_scene->items();
706 for (int i = 0; i < items.size(); i++) {
707 if (items.at(i)->zValue() > -1000) delete items.at(i);
709 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
714 void TitleWidget::saveTitle(KUrl url)
716 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
717 if (!url.isEmpty()) {
718 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
719 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
723 QDomDocument TitleWidget::xml()
725 return m_titledocument.xml(m_startViewport, m_endViewport);
728 void TitleWidget::setXml(QDomDocument doc)
730 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
731 m_transformations.clear();
732 QList <QGraphicsItem *> items = graphicsView->scene()->items();
733 const double PI = 4.0 * atan(1.0);
734 for (int i = 0; i < items.count(); i++) {
735 QTransform t = items.at(i)->transform();
739 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
740 m_transformations[items.at(i)] = x;
742 // mbd: Update the GUI color selectors to match the stuff from the loaded document
743 QColor background_color = m_titledocument.getBackgroundColor();
744 horizontalSlider->blockSignals(true);
745 kcolorbutton->blockSignals(true);
746 horizontalSlider->setValue(background_color.alpha());
747 background_color.setAlpha(255);
748 kcolorbutton->setColor(background_color);
749 horizontalSlider->blockSignals(false);
750 kcolorbutton->blockSignals(false);
755 QImage TitleWidget::renderedPixmap()
757 QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
758 pix.fill(Qt::transparent);
759 QPainter painter(&pix);
760 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
761 m_scene->clearTextSelection();
762 QPen framepen = m_frameBorder->pen();
763 m_frameBorder->setPen(Qt::NoPen);
764 m_startViewport->setVisible(false);
765 m_endViewport->setVisible(false);
766 m_frameImage->setVisible(false);
768 m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
770 m_frameBorder->setPen(framepen);
771 m_startViewport->setVisible(true);
772 m_endViewport->setVisible(true);
773 m_frameImage->setVisible(true);
777 /** \brief Connected to the accepted signal - calls writeChoices */
778 void TitleWidget::slotAccepted()
783 /** \brief Store the current choices of font, background and rect values */
784 void TitleWidget::writeChoices()
786 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
787 KSharedConfigPtr config = KGlobal::config();
788 KConfigGroup titleConfig(config, "TitleWidget");
790 titleConfig.writeEntry("font_family", font_family->currentFont());
791 //titleConfig.writeEntry("font_size", font_size->value());
792 titleConfig.writeEntry("font_pixel_size", font_size->value());
793 titleConfig.writeEntry("font_color", fontColorButton->color());
794 titleConfig.writeEntry("font_alpha", textAlpha->value());
795 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
796 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
797 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
799 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
800 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
801 titleConfig.writeEntry("rect_background_color", rectBColor->color());
802 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
803 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
805 titleConfig.writeEntry("background_color", kcolorbutton->color());
806 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
807 //! \todo Not sure if I should sync - it is probably safe to do it
812 /** \brief Read the last stored choices into the dialog */
813 void TitleWidget::readChoices()
815 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
816 KSharedConfigPtr config = KGlobal::config();
817 KConfigGroup titleConfig(config, "TitleWidget");
819 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
820 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
821 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
822 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
823 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
824 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
825 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
827 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
828 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
829 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
830 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
831 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
833 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
834 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));