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>
33 #include <QTextBlockFormat>
34 #include <QTextCursor>
36 int settingUp = false;
38 const int IMAGEITEM = 7;
39 const int RECTITEM = 3;
40 const int TEXTITEM = 8;
42 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
45 m_startViewport(NULL),
49 m_projectPath(projectPath)
52 setFont(KGlobalSettings::toolBarFont());
53 //toolBox->setFont(KGlobalSettings::toolBarFont());
54 frame_properties->setEnabled(false);
55 rect_properties->setFixedHeight(frame_properties->height() + 4);
56 text_properties->setFixedHeight(frame_properties->height() + 4);
57 m_frameWidth = render->renderWidth();
58 m_frameHeight = render->renderHeight();
59 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
60 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
61 // kcolorbutton == The color of the background
62 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
63 // horizontalslider == The alpha of the background
64 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
65 //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
66 //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
69 //ktextedit->setHidden(true);
70 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
71 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
72 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
73 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
74 //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
76 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
77 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
78 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
79 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
80 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
82 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
84 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
85 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
86 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
87 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
89 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
90 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
91 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
92 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
93 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
95 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
98 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
99 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
100 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
101 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
105 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
106 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
107 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
108 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
111 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
113 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
114 buttonRealSize->setIcon(KIcon("zoom-original"));
115 buttonBold->setIcon(KIcon("format-text-bold"));
116 buttonItalic->setIcon(KIcon("format-text-italic"));
117 buttonUnder->setIcon(KIcon("format-text-underline"));
118 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
119 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
120 buttonAlignRight->setIcon(KIcon("format-justify-right"));
121 buttonAlignNone->setToolTip(i18n("No alignment"));
122 buttonAlignRight->setToolTip(i18n("Align right"));
123 buttonAlignLeft->setToolTip(i18n("Align left"));
124 buttonAlignCenter->setToolTip(i18n("Align center"));
126 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
127 itemhcenter->setToolTip(i18n("Align item horizontally"));
128 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
129 itemvcenter->setToolTip(i18n("Align item vertically"));
131 QHBoxLayout *layout = new QHBoxLayout;
132 frame_toolbar->setLayout(layout);
133 layout->setContentsMargins(2, 2, 2, 2);
134 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
136 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
137 m_buttonRect->setCheckable(true);
138 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
140 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
141 m_buttonText->setCheckable(true);
142 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
144 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
145 m_buttonImage->setCheckable(false);
146 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
148 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
149 m_buttonCursor->setCheckable(true);
150 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
152 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
153 m_buttonLoad->setCheckable(false);
154 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
156 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
157 m_buttonSave->setCheckable(false);
158 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
160 layout->addWidget(m_toolbar);
161 text_properties->setHidden(true);
163 // initialize graphic scene
164 m_scene = new GraphicsSceneRectMove(this);
165 graphicsView->setScene(m_scene);
166 m_titledocument.setScene(m_scene);
168 // a gradient background
169 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
170 gradient->setSpread(QGradient::ReflectSpread);
171 scene->setBackgroundBrush(*gradient);*/
173 m_frameImage = new QGraphicsPixmapItem();
175 qtrans.scale(2.0, 2.0);
176 m_frameImage->setTransform(qtrans);
177 m_frameImage->setZValue(-1200);
178 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
179 displayBackgroundFrame();
180 graphicsView->scene()->addItem(m_frameImage);
182 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
183 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
184 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
185 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
186 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
187 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
188 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
189 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
191 QPen framepen(Qt::DotLine);
192 framepen.setColor(Qt::red);
194 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
195 m_frameBorder->setPen(framepen);
196 m_frameBorder->setZValue(-1100);
197 m_frameBorder->setBrush(Qt::transparent);
198 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
199 graphicsView->scene()->addItem(m_frameBorder);
201 // mbd: load saved settings
205 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
206 graphicsView->show();
207 //graphicsView->setRenderHint(QPainter::Antialiasing);
208 graphicsView->setInteractive(true);
209 //graphicsView->resize(400, 300);
210 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
211 toolBox->setItemEnabled(2, false);
212 if (!url.isEmpty()) {
213 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
220 TitleWidget::~TitleWidget()
224 delete m_buttonImage;
225 delete m_buttonCursor;
229 delete m_frameBorder;
231 delete m_startViewport;
232 delete m_endViewport;
237 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
240 QString titlePath = projectUrl.path() + "/titles/";
241 KStandardDirs::makeDir(titlePath);
242 QString titleName = "title";
244 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
245 while (QFile::exists(path + ".png")) {
247 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
249 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
250 result.append(path + ".png");
254 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
257 QString titlePath = projectUrl.path() + "/titles/";
258 KStandardDirs::makeDir(titlePath);
259 return titlePath + titleName + ".png";
263 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
268 void TitleWidget::slotTextTool()
270 rect_properties->setHidden(true);
271 text_properties->setHidden(false);
272 m_scene->setTool(TITLE_TEXT);
273 m_buttonRect->setChecked(false);
274 m_buttonCursor->setChecked(false);
277 void TitleWidget::slotRectTool()
279 text_properties->setHidden(true);
280 rect_properties->setHidden(false);
281 m_scene->setTool(TITLE_RECTANGLE);
282 m_buttonText->setChecked(false);
283 m_buttonCursor->setChecked(false);
284 m_buttonRect->setChecked(true);
287 void TitleWidget::slotSelectTool()
289 m_scene->setTool(TITLE_SELECT);
290 m_buttonCursor->setChecked(true);
291 m_buttonText->setChecked(false);
292 m_buttonRect->setChecked(false);
295 void TitleWidget::slotImageTool()
297 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
298 if (!url.isEmpty()) {
299 if (url.path().endsWith(".svg")) {
300 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
301 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
302 svg->setZValue(m_count++);
303 svg->setData(Qt::UserRole, url.path());
304 graphicsView->scene()->addItem(svg);
306 QPixmap pix(url.path());
307 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
308 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
309 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
310 image->setData(Qt::UserRole, url.path());
311 image->setZValue(m_count++);
312 graphicsView->scene()->addItem(image);
315 m_scene->setTool(TITLE_SELECT);
316 m_buttonRect->setChecked(false);
317 m_buttonCursor->setChecked(true);
318 m_buttonText->setChecked(false);
321 void TitleWidget::displayBackgroundFrame()
323 if (!displayBg->isChecked()) {
324 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
325 QPixmap pattern(20, 20);
329 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
330 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
335 p.fillRect(bg.rect(), br);
337 m_frameImage->setPixmap(bg);
339 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
343 void TitleWidget::initViewports()
345 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
346 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
348 QPen startpen(Qt::DotLine);
349 QPen endpen(Qt::DashDotLine);
350 startpen.setColor(QColor(100, 200, 100, 140));
351 endpen.setColor(QColor(200, 100, 100, 140));
353 m_startViewport->setPen(startpen);
354 m_endViewport->setPen(endpen);
356 startViewportSize->setValue(40);
357 endViewportSize->setValue(40);
359 m_startViewport->setZValue(-1000);
360 m_endViewport->setZValue(-1000);
362 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
363 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
365 graphicsView->scene()->addItem(m_startViewport);
366 graphicsView->scene()->addItem(m_endViewport);
369 void TitleWidget::slotUpdateZoom(int pos)
371 m_scene->setZoom((double) pos / 100);
372 zoom_label->setText(QString::number(pos) + '%');
375 void TitleWidget::slotZoom(bool up)
377 int pos = zoom_slider->value();
380 zoom_slider->setValue(pos);
383 void TitleWidget::slotAdjustZoom()
385 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
386 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
387 if (scalex > scaley) scalex = scaley;
388 int zoompos = (int)(scalex * 7 + 0.5);*/
389 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
390 int zoompos = graphicsView->matrix().m11() * 100;
391 zoom_slider->setValue(zoompos);
392 graphicsView->centerOn(m_frameBorder);
395 void TitleWidget::slotZoomOneToOne()
397 zoom_slider->setValue(100);
398 graphicsView->centerOn(m_frameBorder);
401 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
403 QColor f = rectFColor->color();
404 f.setAlpha(rectFAlpha->value());
406 penf.setWidth(rectLineWidth->value());
408 QColor b = rectBColor->color();
409 b.setAlpha(rectBAlpha->value());
410 rect->setBrush(QBrush(b));
411 rect->setZValue(m_count++);
412 //setCurrentItem(rect);
413 //graphicsView->setFocus();
416 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
418 QFont font = font_family->currentFont();
419 font.setPixelSize(font_size->value());
421 font.setBold(buttonBold->isChecked());
422 font.setItalic(buttonItalic->isChecked());
423 font.setUnderline(buttonUnder->isChecked());
426 QColor color = fontColorButton->color();
427 color.setAlpha(textAlpha->value());
428 tt->setDefaultTextColor(color);
429 tt->setZValue(m_count++);
433 void TitleWidget::setCurrentItem(QGraphicsItem *item)
435 m_scene->setSelectedItem(item);
438 void TitleWidget::zIndexChanged(int v)
440 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
446 void TitleWidget::selectionChanged()
448 if (m_scene->tool() != TITLE_SELECT) return;
449 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
450 //toolBox->setItemEnabled(2, false);
451 //toolBox->setItemEnabled(3, false);
452 value_x->blockSignals(true);
453 value_y->blockSignals(true);
454 value_w->blockSignals(true);
455 value_h->blockSignals(true);
456 itemzoom->blockSignals(true);
457 itemrotate->blockSignals(true);
459 if (l.at(0)->type() == TEXTITEM) {
460 rect_properties->setHidden(true);
461 text_properties->setHidden(false);
462 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
463 //if (l[0]->hasFocus())
464 //ktextedit->setHtml(i->toHtml());
465 toolBox->setCurrentIndex(0);
466 //toolBox->setItemEnabled(2, true);
467 font_size->blockSignals(true);
468 font_family->blockSignals(true);
469 buttonBold->blockSignals(true);
470 buttonItalic->blockSignals(true);
471 buttonUnder->blockSignals(true);
472 fontColorButton->blockSignals(true);
473 textAlpha->blockSignals(true);
474 buttonAlignLeft->blockSignals(true);
475 buttonAlignRight->blockSignals(true);
476 buttonAlignNone->blockSignals(true);
477 buttonAlignCenter->blockSignals(true);
479 QFont font = i->font();
480 font_family->setCurrentFont(font);
481 font_size->setValue(font.pixelSize());
482 buttonBold->setChecked(font.bold());
483 buttonItalic->setChecked(font.italic());
484 buttonUnder->setChecked(font.underline());
486 QColor color = i->defaultTextColor();
487 fontColorButton->setColor(color);
488 textAlpha->setValue(color.alpha());
490 QTextCursor cur = i->textCursor();
491 QTextBlockFormat format = cur.blockFormat();
492 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
493 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
494 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
495 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
497 font_size->blockSignals(false);
498 font_family->blockSignals(false);
499 buttonBold->blockSignals(false);
500 buttonItalic->blockSignals(false);
501 buttonUnder->blockSignals(false);
502 fontColorButton->blockSignals(false);
503 textAlpha->blockSignals(false);
504 buttonAlignLeft->blockSignals(false);
505 buttonAlignRight->blockSignals(false);
506 buttonAlignNone->blockSignals(false);
507 buttonAlignCenter->blockSignals(false);
509 value_x->setValue((int) i->pos().x());
510 value_y->setValue((int) i->pos().y());
511 value_w->setValue((int) i->boundingRect().width());
512 value_h->setValue((int) i->boundingRect().height());
513 frame_properties->setEnabled(true);
514 value_w->setEnabled(false);
515 value_h->setEnabled(false);
516 } else if ((l.at(0))->type() == RECTITEM) {
517 rect_properties->setHidden(false);
518 text_properties->setHidden(true);
520 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
521 toolBox->setCurrentIndex(0);
522 //toolBox->setItemEnabled(3, true);
523 rectFAlpha->setValue(rec->pen().color().alpha());
524 rectBAlpha->setValue(rec->brush().color().alpha());
525 //kDebug() << rec->brush().color().alpha();
526 QColor fcol = rec->pen().color();
527 QColor bcol = rec->brush().color();
528 //fcol.setAlpha(255);
529 //bcol.setAlpha(255);
530 rectFColor->setColor(fcol);
531 rectBColor->setColor(bcol);
533 rectLineWidth->setValue(rec->pen().width());
534 value_x->setValue((int) rec->pos().x());
535 value_y->setValue((int) rec->pos().y());
536 value_w->setValue((int) rec->rect().width());
537 value_h->setValue((int) rec->rect().height());
538 frame_properties->setEnabled(true);
539 value_w->setEnabled(true);
540 value_h->setEnabled(true);
542 //toolBox->setCurrentIndex(0);
543 frame_properties->setEnabled(false);
545 zValue->setValue((int)l.at(0)->zValue());
546 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
547 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
548 value_x->blockSignals(false);
549 value_y->blockSignals(false);
550 value_w->blockSignals(false);
551 value_h->blockSignals(false);
552 itemzoom->blockSignals(false);
553 itemrotate->blockSignals(false);
554 } else frame_properties->setEnabled(false);
557 void TitleWidget::slotAdjustSelectedItem()
559 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
561 if (l.at(0)->type() == RECTITEM) {
563 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
564 rec->setPos(value_x->value(), value_y->value());
565 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
566 } else if (l.at(0)->type() == TEXTITEM) {
568 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(l.at(0));
569 rec->setPos(value_x->value(), value_y->value());
574 void TitleWidget::slotChangeBackground()
576 QColor color = kcolorbutton->color();
577 color.setAlpha(horizontalSlider->value());
578 m_frameBorder->setBrush(QBrush(color));
581 void TitleWidget::textChanged()
583 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
584 if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
585 //kDebug() << ktextedit->document()->toHtml();
586 //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
590 void TitleWidget::slotUpdateText()
592 QFont font = font_family->currentFont();
593 font.setPixelSize(font_size->value());
594 font.setBold(buttonBold->isChecked());
595 font.setItalic(buttonItalic->isChecked());
596 font.setUnderline(buttonUnder->isChecked());
597 QColor color = fontColorButton->color();
598 color.setAlpha(textAlpha->value());
600 QGraphicsTextItem* item = NULL;
601 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
602 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
603 item = static_cast <QGraphicsTextItem *>(l.at(0));
606 //if (item->textCursor().selection ().isEmpty())
607 QTextCursor cur = item->textCursor();
608 QTextBlockFormat format = cur.blockFormat();
609 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
610 item->setTextWidth(item->boundingRect().width());
611 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
612 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
613 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
615 format.setAlignment(Qt::AlignLeft);
616 item->setTextWidth(-1);
621 item->setDefaultTextColor(color);
622 cur.select(QTextCursor::Document);
623 cur.setBlockFormat(format);
624 item->setTextCursor(cur);
625 cur.clearSelection();
626 item->setTextCursor(cur);
630 QTextDocumentFragment selec = item->textCursor().selection ();
633 //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
635 //ktextedit->setCurrentFont(font);
636 //ktextedit->setTextColor(color);
637 /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
638 if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
639 QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
644 void TitleWidget::rectChanged()
646 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
647 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
648 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
649 QColor f = rectFColor->color();
650 f.setAlpha(rectFAlpha->value());
652 penf.setWidth(rectLineWidth->value());
654 QColor b = rectBColor->color();
655 b.setAlpha(rectBAlpha->value());
656 rec->setBrush(QBrush(b));
660 void TitleWidget::fontBold()
662 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
663 if (l.size() == 1 && l.at(0)->type() == TEXTITEM && !l.at(0)->hasFocus()) {
664 //ktextedit->document()->setTextOption();
668 void TitleWidget::itemScaled(int val)
670 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
672 Transform x = m_transformations.value(l.at(0));
673 x.scalex = (double)val / 100.0;
674 x.scaley = (double)val / 100.0;
676 qtrans.scale(x.scalex, x.scaley);
677 qtrans.rotate(x.rotate);
678 l[0]->setTransform(qtrans);
679 m_transformations[l.at(0)] = x;
683 void TitleWidget::itemRotate(int val)
685 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
687 Transform x = m_transformations[l.at(0)];
688 x.rotate = (double)val;
690 qtrans.scale(x.scalex, x.scaley);
691 qtrans.rotate(x.rotate);
692 l[0]->setTransform(qtrans);
693 m_transformations[l.at(0)] = x;
697 void TitleWidget::itemHCenter()
699 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
701 QGraphicsItem *item = l.at(0);
703 if (item->type() == RECTITEM) {
704 br = ((QGraphicsRectItem*)item)->rect();
705 } else br = item->sceneBoundingRect();
706 int width = (int) br.width();
707 int newPos = (int)((m_frameWidth - width) / 2);
708 item->setPos(newPos, item->pos().y());
712 void TitleWidget::itemVCenter()
714 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
716 QGraphicsItem *item = l.at(0);
718 if (item->type() == RECTITEM) {
719 br = ((QGraphicsRectItem*)item)->rect();
720 } else br = item->sceneBoundingRect();
721 int height = (int) br.height();
722 int newPos = (int)((m_frameHeight - height) / 2);
723 item->setPos(item->pos().x(), newPos);
727 void TitleWidget::setupViewports()
729 double aspect_ratio = 4.0 / 3.0;//read from project
731 QRectF sp(0, 0, 0, 0);
732 QRectF ep(0, 0, 0, 0);
734 double sv_size = startViewportSize->value();
735 double ev_size = endViewportSize->value();
736 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
737 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
739 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
740 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
742 m_startViewport->setPolygon(QPolygonF(sp));
743 m_endViewport->setPolygon(QPolygonF(ep));
747 void TitleWidget::loadTitle()
749 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
750 if (!url.isEmpty()) {
751 QList<QGraphicsItem *> items = m_scene->items();
752 for (int i = 0; i < items.size(); i++) {
753 if (items.at(i)->zValue() > -1000) delete items.at(i);
755 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
760 void TitleWidget::saveTitle(KUrl url)
762 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
763 if (!url.isEmpty()) {
764 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
765 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
769 QDomDocument TitleWidget::xml()
771 return m_titledocument.xml(m_startViewport, m_endViewport);
774 void TitleWidget::setXml(QDomDocument doc)
776 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
777 m_transformations.clear();
778 QList <QGraphicsItem *> items = graphicsView->scene()->items();
779 const double PI = 4.0 * atan(1.0);
780 for (int i = 0; i < items.count(); i++) {
781 QTransform t = items.at(i)->transform();
785 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
786 m_transformations[items.at(i)] = x;
788 // mbd: Update the GUI color selectors to match the stuff from the loaded document
789 QColor background_color = m_titledocument.getBackgroundColor();
790 horizontalSlider->blockSignals(true);
791 kcolorbutton->blockSignals(true);
792 horizontalSlider->setValue(background_color.alpha());
793 background_color.setAlpha(255);
794 kcolorbutton->setColor(background_color);
795 horizontalSlider->blockSignals(false);
796 kcolorbutton->blockSignals(false);
801 QImage TitleWidget::renderedPixmap()
803 QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
804 pix.fill(Qt::transparent);
805 QPainter painter(&pix);
806 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
807 m_scene->clearTextSelection();
808 QPen framepen = m_frameBorder->pen();
809 m_frameBorder->setPen(Qt::NoPen);
810 m_startViewport->setVisible(false);
811 m_endViewport->setVisible(false);
812 m_frameImage->setVisible(false);
814 m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
816 m_frameBorder->setPen(framepen);
817 m_startViewport->setVisible(true);
818 m_endViewport->setVisible(true);
819 m_frameImage->setVisible(true);
823 /** \brief Connected to the accepted signal - calls writeChoices */
824 void TitleWidget::slotAccepted()
829 /** \brief Store the current choices of font, background and rect values */
830 void TitleWidget::writeChoices()
832 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
833 KSharedConfigPtr config = KGlobal::config();
834 KConfigGroup titleConfig(config, "TitleWidget");
836 titleConfig.writeEntry("font_family", font_family->currentFont());
837 //titleConfig.writeEntry("font_size", font_size->value());
838 titleConfig.writeEntry("font_pixel_size", font_size->value());
839 titleConfig.writeEntry("font_color", fontColorButton->color());
840 titleConfig.writeEntry("font_alpha", textAlpha->value());
841 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
842 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
843 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
845 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
846 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
847 titleConfig.writeEntry("rect_background_color", rectBColor->color());
848 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
849 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
851 titleConfig.writeEntry("background_color", kcolorbutton->color());
852 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
853 //! \todo Not sure if I should sync - it is probably safe to do it
858 /** \brief Read the last stored choices into the dialog */
859 void TitleWidget::readChoices()
861 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
862 KSharedConfigPtr config = KGlobal::config();
863 KConfigGroup titleConfig(config, "TitleWidget");
865 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
866 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
867 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
868 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
869 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
870 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
871 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
873 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
874 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
875 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
876 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
877 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
879 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
880 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));