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 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
39 m_startViewport(NULL),
43 m_projectPath(projectPath)
46 setFont(KGlobalSettings::toolBarFont());
47 //toolBox->setFont(KGlobalSettings::toolBarFont());
48 frame_properties->setEnabled(false);
49 rect_properties->setFixedHeight(frame_properties->height() + 4);
50 text_properties->setFixedHeight(frame_properties->height() + 4);
51 m_frameWidth = render->renderWidth();
52 m_frameHeight = render->renderHeight();
53 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
54 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
55 // kcolorbutton == The color of the background
56 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
57 // horizontalslider == The alpha of the background
58 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
59 //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
60 //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
63 //ktextedit->setHidden(true);
64 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
65 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
66 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
67 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
68 //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
70 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
71 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
72 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
73 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
74 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
76 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
77 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
78 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
84 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
85 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
86 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
87 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
89 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
90 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
91 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
92 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
93 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
94 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
95 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
96 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
97 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
98 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
101 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
103 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
104 buttonRealSize->setIcon(KIcon("zoom-original"));
105 buttonBold->setIcon(KIcon("format-text-bold"));
106 buttonItalic->setIcon(KIcon("format-text-italic"));
107 buttonUnder->setIcon(KIcon("format-text-underline"));
109 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
110 itemhcenter->setToolTip(i18n("Align item horizontally"));
111 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
112 itemvcenter->setToolTip(i18n("Align item vertically"));
114 QHBoxLayout *layout = new QHBoxLayout;
115 frame_toolbar->setLayout(layout);
116 layout->setContentsMargins(2, 2, 2, 2);
117 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
119 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
120 m_buttonRect->setCheckable(true);
121 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
123 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
124 m_buttonText->setCheckable(true);
125 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
127 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
128 m_buttonImage->setCheckable(false);
129 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
131 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
132 m_buttonCursor->setCheckable(true);
133 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
135 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
136 m_buttonLoad->setCheckable(false);
137 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
139 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
140 m_buttonSave->setCheckable(false);
141 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
143 layout->addWidget(m_toolbar);
144 text_properties->setHidden(true);
146 // initialize graphic scene
147 m_scene = new GraphicsSceneRectMove(this);
148 graphicsView->setScene(m_scene);
149 m_titledocument.setScene(m_scene);
151 // a gradient background
152 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
153 gradient->setSpread(QGradient::ReflectSpread);
154 scene->setBackgroundBrush(*gradient);*/
156 m_frameImage = new QGraphicsPixmapItem();
158 qtrans.scale(2.0, 2.0);
159 m_frameImage->setTransform(qtrans);
160 m_frameImage->setZValue(-1200);
161 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
162 displayBackgroundFrame();
163 graphicsView->scene()->addItem(m_frameImage);
165 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
166 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
167 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
168 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
169 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
170 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
171 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
172 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
174 QPen framepen(Qt::DotLine);
175 framepen.setColor(Qt::red);
177 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
178 m_frameBorder->setPen(framepen);
179 m_frameBorder->setZValue(-1100);
180 m_frameBorder->setBrush(Qt::transparent);
181 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
182 graphicsView->scene()->addItem(m_frameBorder);
184 // mbd: load saved settings
188 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
189 graphicsView->show();
190 //graphicsView->setRenderHint(QPainter::Antialiasing);
191 graphicsView->setInteractive(true);
192 //graphicsView->resize(400, 300);
193 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
194 toolBox->setItemEnabled(2, false);
195 if (!url.isEmpty()) {
196 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
203 TitleWidget::~TitleWidget()
207 delete m_buttonImage;
208 delete m_buttonCursor;
212 delete m_frameBorder;
214 delete m_startViewport;
215 delete m_endViewport;
220 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
223 QString titlePath = projectUrl.path() + "/titles/";
224 KStandardDirs::makeDir(titlePath);
225 QString titleName = "title";
227 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
228 while (QFile::exists(path + ".png")) {
230 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
232 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
233 result.append(path + ".png");
237 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
240 QString titlePath = projectUrl.path() + "/titles/";
241 KStandardDirs::makeDir(titlePath);
242 return titlePath + titleName + ".png";
246 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
251 void TitleWidget::slotTextTool()
253 rect_properties->setHidden(true);
254 text_properties->setHidden(false);
255 m_scene->setTool(TITLE_TEXT);
256 m_buttonRect->setChecked(false);
257 m_buttonCursor->setChecked(false);
260 void TitleWidget::slotRectTool()
262 text_properties->setHidden(true);
263 rect_properties->setHidden(false);
264 m_scene->setTool(TITLE_RECTANGLE);
265 m_buttonText->setChecked(false);
266 m_buttonCursor->setChecked(false);
267 m_buttonRect->setChecked(true);
270 void TitleWidget::slotSelectTool()
272 m_scene->setTool(TITLE_SELECT);
273 m_buttonCursor->setChecked(true);
274 m_buttonText->setChecked(false);
275 m_buttonRect->setChecked(false);
278 void TitleWidget::slotImageTool()
280 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
281 if (!url.isEmpty()) {
282 if (url.path().endsWith(".svg")) {
283 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
284 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
285 svg->setZValue(m_count++);
286 svg->setData(Qt::UserRole, url.path());
287 graphicsView->scene()->addItem(svg);
289 QPixmap pix(url.path());
290 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
291 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
292 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
293 image->setData(Qt::UserRole, url.path());
294 image->setZValue(m_count++);
295 graphicsView->scene()->addItem(image);
298 m_scene->setTool(TITLE_SELECT);
299 m_buttonRect->setChecked(false);
300 m_buttonCursor->setChecked(true);
301 m_buttonText->setChecked(false);
304 void TitleWidget::displayBackgroundFrame()
306 if (!displayBg->isChecked()) {
307 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
308 QPixmap pattern(20, 20);
312 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
313 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
318 p.fillRect(bg.rect(), br);
320 m_frameImage->setPixmap(bg);
322 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
326 void TitleWidget::initViewports()
328 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
329 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
331 QPen startpen(Qt::DotLine);
332 QPen endpen(Qt::DashDotLine);
333 startpen.setColor(QColor(100, 200, 100, 140));
334 endpen.setColor(QColor(200, 100, 100, 140));
336 m_startViewport->setPen(startpen);
337 m_endViewport->setPen(endpen);
339 startViewportSize->setValue(40);
340 endViewportSize->setValue(40);
342 m_startViewport->setZValue(-1000);
343 m_endViewport->setZValue(-1000);
345 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
346 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
348 graphicsView->scene()->addItem(m_startViewport);
349 graphicsView->scene()->addItem(m_endViewport);
352 void TitleWidget::slotUpdateZoom(int pos)
354 m_scene->setZoom((double) pos / 100);
355 zoom_label->setText(QString::number(pos) + '%');
358 void TitleWidget::slotZoom(bool up)
360 int pos = zoom_slider->value();
363 zoom_slider->setValue(pos);
366 void TitleWidget::slotAdjustZoom()
368 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
369 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
370 if (scalex > scaley) scalex = scaley;
371 int zoompos = (int)(scalex * 7 + 0.5);*/
372 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
373 int zoompos = graphicsView->matrix().m11() * 100;
374 zoom_slider->setValue(zoompos);
375 graphicsView->centerOn(m_frameBorder);
378 void TitleWidget::slotZoomOneToOne()
380 zoom_slider->setValue(100);
381 graphicsView->centerOn(m_frameBorder);
384 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
386 QColor f = rectFColor->color();
387 f.setAlpha(rectFAlpha->value());
389 penf.setWidth(rectLineWidth->value());
391 QColor b = rectBColor->color();
392 b.setAlpha(rectBAlpha->value());
393 rect->setBrush(QBrush(b));
394 rect->setZValue(m_count++);
395 //setCurrentItem(rect);
396 //graphicsView->setFocus();
399 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
401 QFont font = font_family->currentFont();
402 font.setPixelSize(font_size->value());
404 font.setBold(buttonBold->isChecked());
405 font.setItalic(buttonItalic->isChecked());
406 font.setUnderline(buttonUnder->isChecked());
409 QColor color = fontColorButton->color();
410 color.setAlpha(textAlpha->value());
411 tt->setDefaultTextColor(color);
412 tt->setZValue(m_count++);
416 void TitleWidget::setCurrentItem(QGraphicsItem *item)
418 m_scene->setSelectedItem(item);
421 void TitleWidget::zIndexChanged(int v)
423 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
429 void TitleWidget::selectionChanged()
431 if (m_scene->tool() != TITLE_SELECT) return;
432 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
433 //toolBox->setItemEnabled(2, false);
434 //toolBox->setItemEnabled(3, false);
435 value_x->blockSignals(true);
436 value_y->blockSignals(true);
437 value_w->blockSignals(true);
438 value_h->blockSignals(true);
439 //kDebug() << "//////// SELECTION CHANGED; ITEMS: " << l.size();
441 if ((l[0])->type() == 8) {
442 rect_properties->setHidden(true);
443 text_properties->setHidden(false);
444 QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
445 //if (l[0]->hasFocus())
446 //ktextedit->setHtml(i->toHtml());
447 toolBox->setCurrentIndex(0);
448 //toolBox->setItemEnabled(2, true);
449 font_size->blockSignals(true);
450 font_family->blockSignals(true);
451 buttonBold->blockSignals(true);
452 buttonItalic->blockSignals(true);
453 buttonUnder->blockSignals(true);
454 fontColorButton->blockSignals(true);
455 textAlpha->blockSignals(true);
457 QFont font = i->font();
458 font_family->setCurrentFont(font);
459 font_size->setValue(font.pixelSize());
460 buttonBold->setChecked(font.bold());
461 buttonItalic->setChecked(font.italic());
462 buttonUnder->setChecked(font.underline());
464 QColor color = i->defaultTextColor();
465 fontColorButton->setColor(color);
466 textAlpha->setValue(color.alpha());
468 font_size->blockSignals(false);
469 font_family->blockSignals(false);
470 buttonBold->blockSignals(false);
471 buttonItalic->blockSignals(false);
472 buttonUnder->blockSignals(false);
473 fontColorButton->blockSignals(false);
474 textAlpha->blockSignals(false);
476 value_x->setValue((int) i->pos().x());
477 value_y->setValue((int) i->pos().y());
478 value_w->setValue((int) i->boundingRect().width());
479 value_h->setValue((int) i->boundingRect().height());
480 frame_properties->setEnabled(true);
481 value_w->setEnabled(false);
482 value_h->setEnabled(false);
483 } else if ((l[0])->type() == 3) {
484 rect_properties->setHidden(false);
485 text_properties->setHidden(true);
487 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
488 toolBox->setCurrentIndex(0);
489 //toolBox->setItemEnabled(3, true);
490 rectFAlpha->setValue(rec->pen().color().alpha());
491 rectBAlpha->setValue(rec->brush().color().alpha());
492 //kDebug() << rec->brush().color().alpha();
493 QColor fcol = rec->pen().color();
494 QColor bcol = rec->brush().color();
495 //fcol.setAlpha(255);
496 //bcol.setAlpha(255);
497 rectFColor->setColor(fcol);
498 rectBColor->setColor(bcol);
500 rectLineWidth->setValue(rec->pen().width());
501 value_x->setValue((int) rec->pos().x());
502 value_y->setValue((int) rec->pos().y());
503 value_w->setValue((int) rec->rect().width());
504 value_h->setValue((int) rec->rect().height());
505 frame_properties->setEnabled(true);
506 value_w->setEnabled(true);
507 value_h->setEnabled(true);
509 //toolBox->setCurrentIndex(0);
510 frame_properties->setEnabled(false);
512 zValue->setValue((int)l[0]->zValue());
513 itemzoom->setValue((int)(m_transformations[l[0]].scalex * 100));
514 itemrotate->setValue((int)(m_transformations[l[0]].rotate));
515 value_x->blockSignals(false);
516 value_y->blockSignals(false);
517 value_w->blockSignals(false);
518 value_h->blockSignals(false);
519 } else frame_properties->setEnabled(false);
522 void TitleWidget::slotAdjustSelectedItem()
524 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
526 if (l[0]->type() == 3) {
528 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
529 rec->setPos(value_x->value(), value_y->value());
530 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
531 } else if (l[0]->type() == 8) {
533 QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
534 rec->setPos(value_x->value(), value_y->value());
539 void TitleWidget::slotChangeBackground()
541 QColor color = kcolorbutton->color();
542 color.setAlpha(horizontalSlider->value());
543 m_frameBorder->setBrush(QBrush(color));
546 void TitleWidget::textChanged()
548 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
549 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
550 //kDebug() << ktextedit->document()->toHtml();
551 //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
555 void TitleWidget::slotUpdateText()
557 QFont font = font_family->currentFont();
558 font.setPixelSize(font_size->value());
559 font.setBold(buttonBold->isChecked());
560 font.setItalic(buttonItalic->isChecked());
561 font.setUnderline(buttonUnder->isChecked());
562 QColor color = fontColorButton->color();
563 color.setAlpha(textAlpha->value());
565 QGraphicsTextItem* item = NULL;
566 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
567 if (l.size() == 1 && (l[0])->type() == 8) {
568 item = (QGraphicsTextItem*)l[0];
571 //if (item->textCursor().selection ().isEmpty())
574 item->setDefaultTextColor(color);
577 QTextDocumentFragment selec = item->textCursor().selection ();
580 //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
582 //ktextedit->setCurrentFont(font);
583 //ktextedit->setTextColor(color);
584 /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
585 if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
586 QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
591 void TitleWidget::rectChanged()
593 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
594 if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
595 QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
596 QColor f = rectFColor->color();
597 f.setAlpha(rectFAlpha->value());
599 penf.setWidth(rectLineWidth->value());
601 QColor b = rectBColor->color();
602 b.setAlpha(rectBAlpha->value());
603 rec->setBrush(QBrush(b));
607 void TitleWidget::fontBold()
609 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
610 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
611 //ktextedit->document()->setTextOption();
615 void TitleWidget::itemScaled(int val)
617 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
619 Transform x = m_transformations[l[0]];
620 x.scalex = (double)val / 100.0;
621 x.scaley = (double)val / 100.0;
623 qtrans.scale(x.scalex, x.scaley);
624 qtrans.rotate(x.rotate);
625 l[0]->setTransform(qtrans);
626 m_transformations[l[0]] = x;
630 void TitleWidget::itemRotate(int val)
632 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
634 Transform x = m_transformations[l[0]];
635 x.rotate = (double)val;
637 qtrans.scale(x.scalex, x.scaley);
638 qtrans.rotate(x.rotate);
639 l[0]->setTransform(qtrans);
640 m_transformations[l[0]] = x;
644 void TitleWidget::itemHCenter()
646 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
648 QGraphicsItem *item = l[0];
650 if (item->type() == 3) {
651 br = ((QGraphicsRectItem*)item)->rect();
652 } else br = item->boundingRect();
653 int width = (int) br.width();
654 int newPos = (int)((m_frameWidth - width) / 2);
655 item->setPos(newPos, item->pos().y());
659 void TitleWidget::itemVCenter()
661 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
663 QGraphicsItem *item = l[0];
665 if (item->type() == 3) {
666 br = ((QGraphicsRectItem*)item)->rect();
667 } else br = item->boundingRect();
668 int height = (int) br.height();
669 int newPos = (int)((m_frameHeight - height) / 2);
670 item->setPos(item->pos().x(), newPos);
674 void TitleWidget::setupViewports()
676 double aspect_ratio = 4.0 / 3.0;//read from project
678 QRectF sp(0, 0, 0, 0);
679 QRectF ep(0, 0, 0, 0);
681 double sv_size = startViewportSize->value();
682 double ev_size = endViewportSize->value();
683 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
684 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
686 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
687 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
689 m_startViewport->setPolygon(QPolygonF(sp));
690 m_endViewport->setPolygon(QPolygonF(ep));
694 void TitleWidget::loadTitle()
696 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
697 if (!url.isEmpty()) {
698 QList<QGraphicsItem *> items = m_scene->items();
699 for (int i = 0; i < items.size(); i++) {
700 if (items.at(i)->zValue() > -1000) delete items.at(i);
702 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
707 void TitleWidget::saveTitle(KUrl url)
709 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
710 if (!url.isEmpty()) {
711 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
712 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
716 QDomDocument TitleWidget::xml()
718 return m_titledocument.xml(m_startViewport, m_endViewport);
721 void TitleWidget::setXml(QDomDocument doc)
723 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
724 // mbd: Update the GUI color selectors to match the stuff from the loaded document
725 QColor background_color = m_titledocument.getBackgroundColor();
726 horizontalSlider->blockSignals(true);
727 kcolorbutton->blockSignals(true);
728 horizontalSlider->setValue(background_color.alpha());
729 background_color.setAlpha(255);
730 kcolorbutton->setColor(background_color);
731 horizontalSlider->blockSignals(false);
732 kcolorbutton->blockSignals(false);
737 QImage TitleWidget::renderedPixmap()
739 QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
740 pix.fill(Qt::transparent);
741 QPainter painter(&pix);
742 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
743 m_scene->clearTextSelection();
744 QPen framepen = m_frameBorder->pen();
745 m_frameBorder->setPen(Qt::NoPen);
746 m_startViewport->setVisible(false);
747 m_endViewport->setVisible(false);
748 m_frameImage->setVisible(false);
750 m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
752 m_frameBorder->setPen(framepen);
753 m_startViewport->setVisible(true);
754 m_endViewport->setVisible(true);
755 m_frameImage->setVisible(true);
759 /** \brief Connected to the accepted signal - calls writeChoices */
760 void TitleWidget::slotAccepted()
765 /** \brief Store the current choices of font, background and rect values */
766 void TitleWidget::writeChoices()
768 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
769 KSharedConfigPtr config = KGlobal::config();
770 KConfigGroup titleConfig(config, "TitleWidget");
772 titleConfig.writeEntry("font_family", font_family->currentFont());
773 //titleConfig.writeEntry("font_size", font_size->value());
774 titleConfig.writeEntry("font_pixel_size", font_size->value());
775 titleConfig.writeEntry("font_color", fontColorButton->color());
776 titleConfig.writeEntry("font_alpha", textAlpha->value());
777 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
778 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
779 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
781 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
782 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
783 titleConfig.writeEntry("rect_background_color", rectBColor->color());
784 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
785 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
787 titleConfig.writeEntry("background_color", kcolorbutton->color());
788 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
789 //! \todo Not sure if I should sync - it is probably safe to do it
794 /** \brief Read the last stored choices into the dialog */
795 void TitleWidget::readChoices()
797 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
798 KSharedConfigPtr config = KGlobal::config();
799 KConfigGroup titleConfig(config, "TitleWidget");
801 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
802 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
803 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
804 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
805 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
806 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
807 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
809 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
810 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
811 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
812 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
813 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
815 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
816 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));