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())) ;
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()));
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()));
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()));
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()));
91 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
92 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
94 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
98 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
99 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
100 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
105 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
106 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
107 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
110 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
112 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
113 buttonRealSize->setIcon(KIcon("zoom-original"));
114 buttonBold->setIcon(KIcon("format-text-bold"));
115 buttonItalic->setIcon(KIcon("format-text-italic"));
116 buttonUnder->setIcon(KIcon("format-text-underline"));
117 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
118 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
119 buttonAlignRight->setIcon(KIcon("format-justify-right"));
120 buttonAlignNone->setToolTip(i18n("No alignment"));
121 buttonAlignRight->setToolTip(i18n("Align right"));
122 buttonAlignLeft->setToolTip(i18n("Align left"));
123 buttonAlignCenter->setToolTip(i18n("Align center"));
125 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
126 itemhcenter->setToolTip(i18n("Align item horizontally"));
127 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
128 itemvcenter->setToolTip(i18n("Align item vertically"));
130 QHBoxLayout *layout = new QHBoxLayout;
131 frame_toolbar->setLayout(layout);
132 layout->setContentsMargins(2, 2, 2, 2);
133 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
135 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
136 m_buttonRect->setCheckable(true);
137 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
139 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
140 m_buttonText->setCheckable(true);
141 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
143 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
144 m_buttonImage->setCheckable(false);
145 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
147 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
148 m_buttonCursor->setCheckable(true);
149 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
151 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
152 m_buttonLoad->setCheckable(false);
153 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
155 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
156 m_buttonSave->setCheckable(false);
157 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
159 layout->addWidget(m_toolbar);
160 text_properties->setHidden(true);
162 // initialize graphic scene
163 m_scene = new GraphicsSceneRectMove(this);
164 graphicsView->setScene(m_scene);
165 m_titledocument.setScene(m_scene);
166 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
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 bool blockX = !origin_x_left->signalsBlocked();
460 bool blockY = !origin_y_top->signalsBlocked();
461 if (blockX) origin_x_left->blockSignals(true);
462 if (blockY) origin_y_top->blockSignals(true);
463 origin_x_left->setChecked(false);
464 origin_y_top->setChecked(false);
467 if (blockX) origin_x_left->blockSignals(false);
468 if (blockY) origin_y_top->blockSignals(false);
469 } else if (l.size() == 1) {
470 if (l.at(0)->type() == TEXTITEM) {
471 rect_properties->setHidden(true);
472 text_properties->setHidden(false);
473 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
474 //if (l[0]->hasFocus())
475 toolBox->setCurrentIndex(0);
476 //toolBox->setItemEnabled(2, true);
477 font_size->blockSignals(true);
478 font_family->blockSignals(true);
479 buttonBold->blockSignals(true);
480 buttonItalic->blockSignals(true);
481 buttonUnder->blockSignals(true);
482 fontColorButton->blockSignals(true);
483 textAlpha->blockSignals(true);
484 buttonAlignLeft->blockSignals(true);
485 buttonAlignRight->blockSignals(true);
486 buttonAlignNone->blockSignals(true);
487 buttonAlignCenter->blockSignals(true);
489 QFont font = i->font();
490 font_family->setCurrentFont(font);
491 font_size->setValue(font.pixelSize());
492 buttonBold->setChecked(font.bold());
493 buttonItalic->setChecked(font.italic());
494 buttonUnder->setChecked(font.underline());
496 QColor color = i->defaultTextColor();
497 fontColorButton->setColor(color);
498 textAlpha->setValue(color.alpha());
500 QTextCursor cur = i->textCursor();
501 QTextBlockFormat format = cur.blockFormat();
502 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
503 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
504 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
505 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
507 font_size->blockSignals(false);
508 font_family->blockSignals(false);
509 buttonBold->blockSignals(false);
510 buttonItalic->blockSignals(false);
511 buttonUnder->blockSignals(false);
512 fontColorButton->blockSignals(false);
513 textAlpha->blockSignals(false);
514 buttonAlignLeft->blockSignals(false);
515 buttonAlignRight->blockSignals(false);
516 buttonAlignNone->blockSignals(false);
517 buttonAlignCenter->blockSignals(false);
519 updateAxisButtons(i);
520 updateCoordinates(i);
521 value_w->setValue((int) i->boundingRect().width());
522 value_h->setValue((int) i->boundingRect().height());
523 frame_properties->setEnabled(true);
524 value_w->setEnabled(false);
525 value_h->setEnabled(false);
527 } else if ((l.at(0))->type() == RECTITEM) {
528 rect_properties->setHidden(false);
529 text_properties->setHidden(true);
531 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
532 toolBox->setCurrentIndex(0);
533 //toolBox->setItemEnabled(3, true);
534 rectFAlpha->setValue(rec->pen().color().alpha());
535 rectBAlpha->setValue(rec->brush().color().alpha());
536 //kDebug() << rec->brush().color().alpha();
537 QColor fcol = rec->pen().color();
538 QColor bcol = rec->brush().color();
539 //fcol.setAlpha(255);
540 //bcol.setAlpha(255);
541 rectFColor->setColor(fcol);
542 rectBColor->setColor(bcol);
544 rectLineWidth->setValue(rec->pen().width());
546 updateAxisButtons(l.at(0));
547 updateCoordinates(rec);
548 value_w->setValue((int) rec->rect().width());
549 value_h->setValue((int) rec->rect().height());
550 frame_properties->setEnabled(true);
551 value_w->setEnabled(true);
552 value_h->setEnabled(true);
554 //toolBox->setCurrentIndex(0);
555 frame_properties->setEnabled(false);
557 zValue->setValue((int)l.at(0)->zValue());
558 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
559 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
560 value_x->blockSignals(false);
561 value_y->blockSignals(false);
562 value_w->blockSignals(false);
563 value_h->blockSignals(false);
564 itemzoom->blockSignals(false);
565 itemrotate->blockSignals(false);
566 } else frame_properties->setEnabled(false);
569 /** \brief Updates position/size of the selected item when a value
570 * of an item (coordinates, size) has changed */
571 void TitleWidget::slotAdjustSelectedItem()
573 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
575 if (l.at(0)->type() == RECTITEM) {
577 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
579 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
580 } else if (l.at(0)->type() == TEXTITEM) {
582 updatePosition(l.at(0));
587 /** \brief Updates the coordinates in the text fields from the item */
588 void TitleWidget::updateCoordinates(QGraphicsItem *i)
591 bool blockX = !value_x->signalsBlocked();
592 bool blockY = !value_y->signalsBlocked();
594 // Block signals emitted by this method
595 if (blockX) value_x->blockSignals(true);
596 if (blockY) value_y->blockSignals(true);
598 if (i->type() == TEXTITEM) {
600 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *> (i);
602 // Set the correct x coordinate value
603 if (origin_x_left->isChecked()) {
604 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
605 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
607 // Origin is at 0 (default)
608 value_x->setValue((int) rec->pos().x());
612 if (origin_y_top->isChecked()) {
613 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
615 value_y->setValue((int) rec->pos().y());
618 } else if (i->type() == RECTITEM) {
620 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
622 if (origin_x_left->isChecked()) {
623 // Origin (0 point) is at m_frameWidth
624 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width()));
626 // Origin is at 0 (default)
627 value_x->setValue((int) rec->pos().x());
630 if (origin_y_top->isChecked()) {
631 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->rect().height()));
633 value_y->setValue((int) rec->pos().y());
639 // Stop blocking signals now
640 if (!blockX) value_x->blockSignals(false);
641 if (!blockY) value_y->blockSignals(false);
644 /** \brief Updates the position of an item by reading coordinates from the text fields */
645 void TitleWidget::updatePosition(QGraphicsItem *i) {
647 if (i->type() == TEXTITEM) {
648 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
651 if (origin_x_left->isChecked()) {
652 /* Origin of the x axis is at m_frameWidth,
653 * and distance from right border of the item to the right
654 * border of the frame is taken.
655 * See comment to slotOriginXClicked().
657 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
659 posX = value_x->value();
663 if (origin_y_top->isChecked()) {
664 /* Same for y axis */
665 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
667 posY = value_y->value();
670 rec->setPos(posX, posY);
671 } else if (i->type() == RECTITEM) {
673 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
676 if (origin_x_left->isChecked()) {
677 posX = m_frameWidth - value_x->value() - rec->rect().width();
679 posX = value_x->value();
683 if (origin_y_top->isChecked()) {
684 posY = m_frameHeight - value_y->value() - rec->rect().height();
686 posY = value_y->value();
689 rec->setPos(posX, posY);
694 void TitleWidget::updateTextOriginX()
696 if (origin_x_left->isChecked()) {
697 origin_x_left->setText(i18n("\u2212X"));
699 origin_x_left->setText(i18n("+X"));
703 void TitleWidget::slotOriginXClicked()
705 // Update the text displayed on the button.
708 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
710 updateCoordinates(l.at(0));
712 // Remember x axis setting
713 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()?
714 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
718 void TitleWidget::updateTextOriginY()
720 if (origin_y_top->isChecked()) {
721 origin_y_top->setText(i18n("\u2212Y"));
723 origin_y_top->setText(i18n("+Y"));
727 void TitleWidget::slotOriginYClicked()
729 // Update the text displayed on the button.
732 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
734 updateCoordinates(l.at(0));
736 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked()?
737 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
742 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
744 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
745 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
746 origin_x_left->blockSignals(true);
747 origin_y_top->blockSignals(true);
749 if (xAxis == TitleDocument::AxisInverted) {
750 origin_x_left->setChecked(true);
752 origin_x_left->setChecked(false);
756 if (yAxis == TitleDocument::AxisInverted) {
757 origin_y_top->setChecked(true);
759 origin_y_top->setChecked(false);
763 origin_x_left->blockSignals(false);
764 origin_y_top->blockSignals(false);
767 void TitleWidget::slotChangeBackground()
769 QColor color = kcolorbutton->color();
770 color.setAlpha(horizontalSlider->value());
771 m_frameBorder->setBrush(QBrush(color));
775 * Something (yeah) has changed in our QGraphicsScene.
777 void TitleWidget::slotChanged() {
778 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
779 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
780 textChanged(static_cast <QGraphicsTextItem *> (l.at(0)));
785 * If the user has set origin_x_left (everything also for y),
786 * we need to look whether a text element has been selected. If yes,
787 * we need to ensure that the right border of the text field
788 * remains fixed also when some text has been entered.
790 * This is also known as right-justified, with the difference that
791 * it is not valid for text but for its boundingRect. Text may still
794 void TitleWidget::textChanged(QGraphicsTextItem *i) {
795 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
797 if (!i->toPlainText().isEmpty()) {
801 * Don't do anything if the string is empty. If the position
802 * would be updated here, a newly created text field would
803 * be set to the position of the last selected text field.
809 void TitleWidget::slotUpdateText()
811 QFont font = font_family->currentFont();
812 font.setPixelSize(font_size->value());
813 font.setBold(buttonBold->isChecked());
814 font.setItalic(buttonItalic->isChecked());
815 font.setUnderline(buttonUnder->isChecked());
816 QColor color = fontColorButton->color();
817 color.setAlpha(textAlpha->value());
819 QGraphicsTextItem* item = NULL;
820 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
821 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
822 item = static_cast <QGraphicsTextItem *>(l.at(0));
825 //if (item->textCursor().selection ().isEmpty())
826 QTextCursor cur = item->textCursor();
827 QTextBlockFormat format = cur.blockFormat();
828 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
829 item->setTextWidth(item->boundingRect().width());
830 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
831 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
832 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
834 format.setAlignment(Qt::AlignLeft);
835 item->setTextWidth(-1);
840 item->setDefaultTextColor(color);
841 cur.select(QTextCursor::Document);
842 cur.setBlockFormat(format);
843 item->setTextCursor(cur);
844 cur.clearSelection();
845 item->setTextCursor(cur);
850 void TitleWidget::rectChanged()
852 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
853 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
854 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
855 QColor f = rectFColor->color();
856 f.setAlpha(rectFAlpha->value());
858 penf.setWidth(rectLineWidth->value());
860 QColor b = rectBColor->color();
861 b.setAlpha(rectBAlpha->value());
862 rec->setBrush(QBrush(b));
866 void TitleWidget::itemScaled(int val)
868 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
870 Transform x = m_transformations.value(l.at(0));
871 x.scalex = (double)val / 100.0;
872 x.scaley = (double)val / 100.0;
874 qtrans.scale(x.scalex, x.scaley);
875 qtrans.rotate(x.rotate);
876 l[0]->setTransform(qtrans);
877 m_transformations[l.at(0)] = x;
881 void TitleWidget::itemRotate(int val)
883 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
885 Transform x = m_transformations[l.at(0)];
886 x.rotate = (double)val;
888 qtrans.scale(x.scalex, x.scaley);
889 qtrans.rotate(x.rotate);
890 l[0]->setTransform(qtrans);
891 m_transformations[l.at(0)] = x;
895 void TitleWidget::itemHCenter()
897 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
899 QGraphicsItem *item = l.at(0);
901 if (item->type() == RECTITEM) {
902 br = ((QGraphicsRectItem*)item)->rect();
903 } else br = item->sceneBoundingRect();
904 int width = (int) br.width();
905 int newPos = (int)((m_frameWidth - width) / 2);
906 item->setPos(newPos, item->pos().y());
910 void TitleWidget::itemVCenter()
912 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
914 QGraphicsItem *item = l.at(0);
916 if (item->type() == RECTITEM) {
917 br = ((QGraphicsRectItem*)item)->rect();
918 } else br = item->sceneBoundingRect();
919 int height = (int) br.height();
920 int newPos = (int)((m_frameHeight - height) / 2);
921 item->setPos(item->pos().x(), newPos);
925 void TitleWidget::setupViewports()
927 double aspect_ratio = 4.0 / 3.0;//read from project
929 QRectF sp(0, 0, 0, 0);
930 QRectF ep(0, 0, 0, 0);
932 double sv_size = startViewportSize->value();
933 double ev_size = endViewportSize->value();
934 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
935 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
937 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
938 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
940 m_startViewport->setPolygon(QPolygonF(sp));
941 m_endViewport->setPolygon(QPolygonF(ep));
945 void TitleWidget::loadTitle()
947 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
948 if (!url.isEmpty()) {
949 QList<QGraphicsItem *> items = m_scene->items();
950 for (int i = 0; i < items.size(); i++) {
951 if (items.at(i)->zValue() > -1000) delete items.at(i);
953 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
958 void TitleWidget::saveTitle(KUrl url)
960 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
961 if (!url.isEmpty()) {
962 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
963 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
967 QDomDocument TitleWidget::xml()
969 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
970 if (cropImage->isChecked()) {
971 doc.documentElement().setAttribute("crop", 1);
976 void TitleWidget::setXml(QDomDocument doc)
978 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
979 QDomElement e = doc.documentElement();
980 cropImage->setChecked(e.hasAttribute("crop"));
981 m_transformations.clear();
982 QList <QGraphicsItem *> items = graphicsView->scene()->items();
983 const double PI = 4.0 * atan(1.0);
984 for (int i = 0; i < items.count(); i++) {
985 QTransform t = items.at(i)->transform();
989 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
990 m_transformations[items.at(i)] = x;
992 // mbd: Update the GUI color selectors to match the stuff from the loaded document
993 QColor background_color = m_titledocument.getBackgroundColor();
994 horizontalSlider->blockSignals(true);
995 kcolorbutton->blockSignals(true);
996 horizontalSlider->setValue(background_color.alpha());
997 background_color.setAlpha(255);
998 kcolorbutton->setColor(background_color);
999 horizontalSlider->blockSignals(false);
1000 kcolorbutton->blockSignals(false);
1005 QImage TitleWidget::renderedPixmap()
1009 int maxX = m_frameWidth;
1010 int maxY = m_frameHeight;
1011 if (!cropImage->isChecked()) {
1012 m_scene->removeItem(m_startViewport);
1013 m_scene->removeItem(m_endViewport);
1014 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1015 if (boundingRect.left() < 0) minX = boundingRect.left();
1016 if (boundingRect.top() < 0) minY = boundingRect.top();
1017 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1018 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1027 QImage pix(maxX, maxY, QImage::Format_ARGB32);
1028 pix.fill(Qt::transparent);
1029 QPainter painter(&pix);
1030 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1031 m_scene->clearTextSelection();
1032 QPen framepen = m_frameBorder->pen();
1033 m_frameBorder->setPen(Qt::NoPen);
1034 m_startViewport->setVisible(false);
1035 m_endViewport->setVisible(false);
1036 m_frameImage->setVisible(false);
1038 m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
1040 m_frameBorder->setPen(framepen);
1041 m_startViewport->setVisible(true);
1042 m_endViewport->setVisible(true);
1043 m_frameImage->setVisible(true);
1047 /** \brief Connected to the accepted signal - calls writeChoices */
1048 void TitleWidget::slotAccepted()
1053 /** \brief Store the current choices of font, background and rect values */
1054 void TitleWidget::writeChoices()
1056 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1057 KSharedConfigPtr config = KGlobal::config();
1058 KConfigGroup titleConfig(config, "TitleWidget");
1059 // Write the entries
1060 titleConfig.writeEntry("font_family", font_family->currentFont());
1061 //titleConfig.writeEntry("font_size", font_size->value());
1062 titleConfig.writeEntry("font_pixel_size", font_size->value());
1063 titleConfig.writeEntry("font_color", fontColorButton->color());
1064 titleConfig.writeEntry("font_alpha", textAlpha->value());
1065 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1066 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1067 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1069 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1070 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1071 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1072 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1073 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1075 titleConfig.writeEntry("background_color", kcolorbutton->color());
1076 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1078 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1079 //! \todo Not sure if I should sync - it is probably safe to do it
1084 /** \brief Read the last stored choices into the dialog */
1085 void TitleWidget::readChoices()
1087 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1088 KSharedConfigPtr config = KGlobal::config();
1089 KConfigGroup titleConfig(config, "TitleWidget");
1091 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1092 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1093 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1094 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1095 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1096 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1097 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1099 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1100 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1101 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1102 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1103 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1105 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1106 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1108 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));