1 /***************************************************************************
2 titlewidget.cpp - 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 projectTitlePath, Render *render, QWidget *parent) :
45 m_startViewport(NULL),
49 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
50 m_projectTitlePath(projectTitlePath)
53 setFont(KGlobalSettings::toolBarFont());
54 //toolBox->setFont(KGlobalSettings::toolBarFont());
55 frame_properties->setEnabled(false);
56 rect_properties->setFixedHeight(frame_properties->height() + 4);
57 text_properties->setFixedHeight(frame_properties->height() + 4);
58 m_frameWidth = render->renderWidth();
59 m_frameHeight = render->renderHeight();
60 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
61 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
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()));
69 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
70 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
71 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
72 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
73 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
75 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
76 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
77 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
78 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
83 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
84 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
85 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
86 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
88 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
89 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
91 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
92 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
93 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
94 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
96 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
97 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
98 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
99 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
100 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104 connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
105 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
107 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
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->setIcon(KIcon("kdenlive-align-none"));
121 buttonInsertUnicode->setIcon(KIcon("kdenlive-insert-unicode"));
123 buttonAlignNone->setToolTip(i18n("No alignment"));
124 buttonAlignRight->setToolTip(i18n("Align right"));
125 buttonAlignLeft->setToolTip(i18n("Align left"));
126 buttonAlignCenter->setToolTip(i18n("Align center"));
127 buttonInsertUnicode->setToolTip(i18n("Insert Unicode character (Shift+Ctrl+U)"));
128 buttonInsertUnicode->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
129 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
130 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
131 rectBColor->setToolTip(i18n("Select fill color"));
132 rectFColor->setToolTip(i18n("Select border color"));
133 rectBAlpha->setToolTip(i18n("Fill transparency"));
134 rectFAlpha->setToolTip(i18n("Border transparency"));
135 zoom_slider->setToolTip(i18n("Zoom"));
136 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
137 buttonFitZoom->setToolTip(i18n("Fit zoom"));
138 kcolorbutton->setToolTip(i18n("Select background color"));
139 horizontalSlider->setToolTip(i18n("Background Transparency"));
141 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
142 itemhcenter->setToolTip(i18n("Align item horizontally"));
143 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
144 itemvcenter->setToolTip(i18n("Align item vertically"));
146 QHBoxLayout *layout = new QHBoxLayout;
147 frame_toolbar->setLayout(layout);
148 layout->setContentsMargins(2, 2, 2, 2);
149 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
151 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle (Alt+R)"));
152 m_buttonRect->setCheckable(true);
153 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
154 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
156 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text (Alt+T)"));
157 m_buttonText->setCheckable(true);
158 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
159 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
161 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image (Alt+I)"));
162 m_buttonImage->setCheckable(false);
163 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
164 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
166 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool (Alt+S)"));
167 m_buttonCursor->setCheckable(true);
168 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
169 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
171 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
172 m_buttonLoad->setCheckable(false);
173 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
174 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
176 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
177 m_buttonSave->setCheckable(false);
178 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
179 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
181 layout->addWidget(m_toolbar);
182 text_properties->setHidden(true);
184 // initialize graphic scene
185 m_scene = new GraphicsSceneRectMove(this);
186 graphicsView->setScene(m_scene);
187 m_titledocument.setScene(m_scene);
188 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
190 // a gradient background
191 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
192 gradient->setSpread(QGradient::ReflectSpread);
193 scene->setBackgroundBrush(*gradient);*/
195 m_frameImage = new QGraphicsPixmapItem();
197 qtrans.scale(2.0, 2.0);
198 m_frameImage->setTransform(qtrans);
199 m_frameImage->setZValue(-1200);
200 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
201 displayBackgroundFrame();
202 graphicsView->scene()->addItem(m_frameImage);
204 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
205 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
206 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
207 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
208 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
209 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
210 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
211 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
213 QPen framepen(Qt::DotLine);
214 framepen.setColor(Qt::red);
216 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
217 m_frameBorder->setPen(framepen);
218 m_frameBorder->setZValue(-1100);
219 m_frameBorder->setBrush(Qt::transparent);
220 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
221 graphicsView->scene()->addItem(m_frameBorder);
223 // mbd: load saved settings
227 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
228 graphicsView->show();
229 //graphicsView->setRenderHint(QPainter::Antialiasing);
230 graphicsView->setInteractive(true);
231 //graphicsView->resize(400, 300);
232 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
233 toolBox->setItemEnabled(2, false);
234 if (!url.isEmpty()) {
235 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
242 TitleWidget::~TitleWidget()
246 delete m_buttonImage;
247 delete m_buttonCursor;
251 delete m_unicodeDialog;
252 delete m_frameBorder;
254 delete m_startViewport;
255 delete m_endViewport;
260 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
263 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
264 KStandardDirs::makeDir(titlePath);
265 titlePath.append((isClone == false) ? "title" : "clone");
268 while (path.isEmpty() || QFile::exists(path)) {
270 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
272 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
277 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
280 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
281 KStandardDirs::makeDir(titlePath);
282 return titlePath + titleName + ".png";
286 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
291 void TitleWidget::slotTextTool()
293 rect_properties->setHidden(true);
294 text_properties->setHidden(false);
295 m_scene->setTool(TITLE_TEXT);
296 m_buttonRect->setChecked(false);
297 m_buttonCursor->setChecked(false);
300 void TitleWidget::slotRectTool()
302 text_properties->setHidden(true);
303 rect_properties->setHidden(false);
304 m_scene->setTool(TITLE_RECTANGLE);
305 m_buttonText->setChecked(false);
306 m_buttonCursor->setChecked(false);
307 m_buttonRect->setChecked(true);
310 void TitleWidget::slotSelectTool()
312 m_scene->setTool(TITLE_SELECT);
313 m_buttonCursor->setChecked(true);
314 m_buttonText->setChecked(false);
315 m_buttonRect->setChecked(false);
318 void TitleWidget::slotImageTool()
320 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
321 if (!url.isEmpty()) {
322 if (url.path().endsWith(".svg")) {
323 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
324 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
325 svg->setZValue(m_count++);
326 svg->setData(Qt::UserRole, url.path());
327 graphicsView->scene()->addItem(svg);
329 QPixmap pix(url.path());
330 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
331 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
332 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
333 image->setData(Qt::UserRole, url.path());
334 image->setZValue(m_count++);
335 graphicsView->scene()->addItem(image);
338 m_scene->setTool(TITLE_SELECT);
339 m_buttonRect->setChecked(false);
340 m_buttonCursor->setChecked(true);
341 m_buttonText->setChecked(false);
344 void TitleWidget::displayBackgroundFrame()
346 if (!displayBg->isChecked()) {
347 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
348 QPixmap pattern(20, 20);
352 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
353 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
358 p.fillRect(bg.rect(), br);
360 m_frameImage->setPixmap(bg);
362 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
366 void TitleWidget::initViewports()
368 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
369 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
371 QPen startpen(Qt::DotLine);
372 QPen endpen(Qt::DashDotLine);
373 startpen.setColor(QColor(100, 200, 100, 140));
374 endpen.setColor(QColor(200, 100, 100, 140));
376 m_startViewport->setPen(startpen);
377 m_endViewport->setPen(endpen);
379 startViewportSize->setValue(40);
380 endViewportSize->setValue(40);
382 m_startViewport->setZValue(-1000);
383 m_endViewport->setZValue(-1000);
385 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
386 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
388 graphicsView->scene()->addItem(m_startViewport);
389 graphicsView->scene()->addItem(m_endViewport);
392 void TitleWidget::slotUpdateZoom(int pos)
394 m_scene->setZoom((double) pos / 100);
395 zoom_label->setText(QString::number(pos) + '%');
398 void TitleWidget::slotZoom(bool up)
400 int pos = zoom_slider->value();
403 zoom_slider->setValue(pos);
406 void TitleWidget::slotAdjustZoom()
408 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
409 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
410 if (scalex > scaley) scalex = scaley;
411 int zoompos = (int)(scalex * 7 + 0.5);*/
412 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
413 int zoompos = graphicsView->matrix().m11() * 100;
414 zoom_slider->setValue(zoompos);
415 graphicsView->centerOn(m_frameBorder);
418 void TitleWidget::slotZoomOneToOne()
420 zoom_slider->setValue(100);
421 graphicsView->centerOn(m_frameBorder);
424 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
426 QColor f = rectFColor->color();
427 f.setAlpha(rectFAlpha->value());
429 penf.setWidth(rectLineWidth->value());
431 QColor b = rectBColor->color();
432 b.setAlpha(rectBAlpha->value());
433 rect->setBrush(QBrush(b));
434 rect->setZValue(m_count++);
435 //setCurrentItem(rect);
436 //graphicsView->setFocus();
439 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
441 QFont font = font_family->currentFont();
442 font.setPixelSize(font_size->value());
444 font.setBold(buttonBold->isChecked());
445 font.setItalic(buttonItalic->isChecked());
446 font.setUnderline(buttonUnder->isChecked());
449 QColor color = fontColorButton->color();
450 color.setAlpha(textAlpha->value());
451 tt->setDefaultTextColor(color);
452 tt->setZValue(m_count++);
456 void TitleWidget::setCurrentItem(QGraphicsItem *item)
458 m_scene->setSelectedItem(item);
461 void TitleWidget::zIndexChanged(int v)
463 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
469 void TitleWidget::selectionChanged()
471 if (m_scene->tool() != TITLE_SELECT) return;
472 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
473 //toolBox->setItemEnabled(2, false);
474 //toolBox->setItemEnabled(3, false);
475 value_x->blockSignals(true);
476 value_y->blockSignals(true);
477 value_w->blockSignals(true);
478 value_h->blockSignals(true);
479 itemzoom->blockSignals(true);
480 itemrotate->blockSignals(true);
482 bool blockX = !origin_x_left->signalsBlocked();
483 bool blockY = !origin_y_top->signalsBlocked();
484 if (blockX) origin_x_left->blockSignals(true);
485 if (blockY) origin_y_top->blockSignals(true);
486 origin_x_left->setChecked(false);
487 origin_y_top->setChecked(false);
490 frame_properties->setEnabled(false);
491 text_properties->setEnabled(false);
492 rect_properties->setEnabled(false);
493 if (blockX) origin_x_left->blockSignals(false);
494 if (blockY) origin_y_top->blockSignals(false);
495 } else if (l.size() == 1) {
496 if (l.at(0)->type() == TEXTITEM) {
497 rect_properties->setHidden(true);
498 text_properties->setHidden(false);
499 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
500 //if (l[0]->hasFocus())
501 toolBox->setCurrentIndex(0);
502 //toolBox->setItemEnabled(2, true);
503 font_size->blockSignals(true);
504 font_family->blockSignals(true);
505 buttonBold->blockSignals(true);
506 buttonItalic->blockSignals(true);
507 buttonUnder->blockSignals(true);
508 fontColorButton->blockSignals(true);
509 textAlpha->blockSignals(true);
510 buttonAlignLeft->blockSignals(true);
511 buttonAlignRight->blockSignals(true);
512 buttonAlignNone->blockSignals(true);
513 buttonAlignCenter->blockSignals(true);
515 QFont font = i->font();
516 font_family->setCurrentFont(font);
517 font_size->setValue(font.pixelSize());
518 buttonBold->setChecked(font.bold());
519 buttonItalic->setChecked(font.italic());
520 buttonUnder->setChecked(font.underline());
522 QColor color = i->defaultTextColor();
523 fontColorButton->setColor(color);
524 textAlpha->setValue(color.alpha());
526 QTextCursor cur = i->textCursor();
527 QTextBlockFormat format = cur.blockFormat();
528 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
529 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
530 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
531 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
533 font_size->blockSignals(false);
534 font_family->blockSignals(false);
535 buttonBold->blockSignals(false);
536 buttonItalic->blockSignals(false);
537 buttonUnder->blockSignals(false);
538 fontColorButton->blockSignals(false);
539 textAlpha->blockSignals(false);
540 buttonAlignLeft->blockSignals(false);
541 buttonAlignRight->blockSignals(false);
542 buttonAlignNone->blockSignals(false);
543 buttonAlignCenter->blockSignals(false);
545 updateAxisButtons(i);
546 updateCoordinates(i);
548 //value_w->setValue((int) i->boundingRect().width());
549 //value_h->setValue((int) i->boundingRect().height());
550 frame_properties->setEnabled(true);
551 text_properties->setEnabled(true);
552 rect_properties->setEnabled(false);
553 value_w->setEnabled(false);
554 value_h->setEnabled(false);
556 } else if ((l.at(0))->type() == RECTITEM) {
557 rect_properties->setHidden(false);
558 text_properties->setHidden(true);
560 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
561 toolBox->setCurrentIndex(0);
562 //toolBox->setItemEnabled(3, true);
563 rectFAlpha->setValue(rec->pen().color().alpha());
564 rectBAlpha->setValue(rec->brush().color().alpha());
565 //kDebug() << rec->brush().color().alpha();
566 QColor fcol = rec->pen().color();
567 QColor bcol = rec->brush().color();
568 //fcol.setAlpha(255);
569 //bcol.setAlpha(255);
570 rectFColor->setColor(fcol);
571 rectBColor->setColor(bcol);
573 rectLineWidth->setValue(rec->pen().width());
575 updateAxisButtons(l.at(0));
576 updateCoordinates(rec);
577 updateDimension(rec);
578 //value_w->setValue((int) rec->rect().width());
579 //value_h->setValue((int) rec->rect().height());
580 frame_properties->setEnabled(true);
581 text_properties->setEnabled(false);
582 rect_properties->setEnabled(true);
583 value_w->setEnabled(true);
584 value_h->setEnabled(true);
586 } else if (l.at(0)->type() == IMAGEITEM) {
587 updateCoordinates(l.at(0));
588 updateDimension(l.at(0));
590 frame_properties->setEnabled(true);
591 text_properties->setEnabled(false);
592 rect_properties->setEnabled(false);
593 value_x->setEnabled(true);
594 value_w->setEnabled(false);
595 value_h->setEnabled(false);
598 //toolBox->setCurrentIndex(0);
599 frame_properties->setEnabled(false);
600 text_properties->setEnabled(false);
601 rect_properties->setEnabled(false);
603 zValue->setValue((int)l.at(0)->zValue());
604 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
605 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
606 value_x->blockSignals(false);
607 value_y->blockSignals(false);
608 value_w->blockSignals(false);
609 value_h->blockSignals(false);
610 itemzoom->blockSignals(false);
611 itemrotate->blockSignals(false);
615 /** \brief Updates position/size of the selected item when a value
616 * of an item (coordinates, size) has changed */
617 void TitleWidget::slotAdjustSelectedItem()
619 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
621 if (l.at(0)->type() == RECTITEM) {
623 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
625 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
626 } else if (l.at(0)->type() == TEXTITEM) {
628 updatePosition(l.at(0));
629 } else if (l.at(0)->type() == IMAGEITEM) {
631 updatePosition(l.at(0));
636 /** \brief Updates width/height int the text fields, regarding transformation matrix */
637 void TitleWidget::updateDimension(QGraphicsItem *i)
639 bool blockW = !value_w->signalsBlocked();
640 bool blockH = !value_h->signalsBlocked();
642 if (blockW) value_w->blockSignals(true);
643 if (blockH) value_h->blockSignals(true);
646 if (i->type() == IMAGEITEM) {
647 // Get multipliers for rotation/scaling
649 /*Transform t = m_transformations.value(i);
650 QRectF r = i->boundingRect();
651 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
652 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
653 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
654 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
656 value_w->setValue(i->sceneBoundingRect().width());
657 value_h->setValue(i->sceneBoundingRect().height());
658 } else if (i->type() == RECTITEM) {
659 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
660 value_w->setValue((int) r->rect().width());
661 value_h->setValue((int) r->rect().height());
662 } else if (i->type() == TEXTITEM) {
663 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
664 value_w->setValue((int) t->boundingRect().width());
665 value_h->setValue((int) t->boundingRect().height());
668 if (blockW) value_w->blockSignals(false);
669 if (blockH) value_h->blockSignals(false);
672 /** \brief Updates the coordinates in the text fields from the item */
673 void TitleWidget::updateCoordinates(QGraphicsItem *i)
675 // Block signals emitted by this method
676 value_x->blockSignals(true);
677 value_y->blockSignals(true);
679 if (i->type() == TEXTITEM) {
681 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
683 // Set the correct x coordinate value
684 if (origin_x_left->isChecked()) {
685 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
686 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
688 // Origin is at 0 (default)
689 value_x->setValue((int) rec->pos().x());
693 if (origin_y_top->isChecked()) {
694 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
696 value_y->setValue((int) rec->pos().y());
699 } else if (i->type() == RECTITEM) {
701 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
703 if (origin_x_left->isChecked()) {
704 // Origin (0 point) is at m_frameWidth
705 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
707 // Origin is at 0 (default)
708 value_x->setValue((int) rec->pos().x());
711 if (origin_y_top->isChecked()) {
712 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
714 value_y->setValue((int) rec->pos().y());
717 } else if (i->type() == IMAGEITEM) {
719 if (origin_x_left->isChecked()) {
720 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
722 value_x->setValue((int) i->pos().x());
725 if (origin_y_top->isChecked()) {
726 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
728 value_y->setValue((int) i->pos().y());
733 // Stop blocking signals now
734 value_x->blockSignals(false);
735 value_y->blockSignals(false);
738 /** \brief Updates the position of an item by reading coordinates from the text fields */
739 void TitleWidget::updatePosition(QGraphicsItem *i)
741 if (i->type() == TEXTITEM) {
742 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
745 if (origin_x_left->isChecked()) {
746 /* Origin of the x axis is at m_frameWidth,
747 * and distance from right border of the item to the right
748 * border of the frame is taken.
749 * See comment to slotOriginXClicked().
751 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
753 posX = value_x->value();
757 if (origin_y_top->isChecked()) {
758 /* Same for y axis */
759 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
761 posY = value_y->value();
764 rec->setPos(posX, posY);
766 } else if (i->type() == RECTITEM) {
768 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
771 if (origin_x_left->isChecked()) {
772 posX = m_frameWidth - value_x->value() - rec->rect().width();
774 posX = value_x->value();
778 if (origin_y_top->isChecked()) {
779 posY = m_frameHeight - value_y->value() - rec->rect().height();
781 posY = value_y->value();
784 rec->setPos(posX, posY);
786 } else if (i->type() == IMAGEITEM) {
788 if (origin_x_left->isChecked()) {
789 // Use the sceneBoundingRect because this also regards transformations like zoom
790 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
792 posX = value_x->value();
796 if (origin_y_top->isChecked()) {
797 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
799 posY = value_y->value();
802 i->setPos(posX, posY);
808 void TitleWidget::updateTextOriginX()
810 if (origin_x_left->isChecked()) {
811 origin_x_left->setText(i18n("\u2212X"));
813 origin_x_left->setText(i18n("+X"));
817 void TitleWidget::slotOriginXClicked()
819 // Update the text displayed on the button.
822 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
824 updateCoordinates(l.at(0));
826 // Remember x axis setting
827 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
828 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
830 graphicsView->setFocus();
833 void TitleWidget::updateTextOriginY()
835 if (origin_y_top->isChecked()) {
836 origin_y_top->setText(i18n("\u2212Y"));
838 origin_y_top->setText(i18n("+Y"));
842 void TitleWidget::slotOriginYClicked()
844 // Update the text displayed on the button.
847 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
849 updateCoordinates(l.at(0));
851 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
852 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
855 graphicsView->setFocus();
858 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
860 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
861 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
862 origin_x_left->blockSignals(true);
863 origin_y_top->blockSignals(true);
865 if (xAxis == TitleDocument::AxisInverted) {
866 origin_x_left->setChecked(true);
868 origin_x_left->setChecked(false);
872 if (yAxis == TitleDocument::AxisInverted) {
873 origin_y_top->setChecked(true);
875 origin_y_top->setChecked(false);
879 origin_x_left->blockSignals(false);
880 origin_y_top->blockSignals(false);
883 void TitleWidget::slotChangeBackground()
885 QColor color = kcolorbutton->color();
886 color.setAlpha(horizontalSlider->value());
887 m_frameBorder->setBrush(QBrush(color));
891 * Something (yeah) has changed in our QGraphicsScene.
893 void TitleWidget::slotChanged()
895 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
896 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
897 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
902 * If the user has set origin_x_left (everything also for y),
903 * we need to look whether a text element has been selected. If yes,
904 * we need to ensure that the right border of the text field
905 * remains fixed also when some text has been entered.
907 * This is also known as right-justified, with the difference that
908 * it is not valid for text but for its boundingRect. Text may still
911 void TitleWidget::textChanged(QGraphicsTextItem *i)
916 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
918 if (!i->toPlainText().isEmpty()) {
922 * Don't do anything if the string is empty. If the position
923 * would be updated here, a newly created text field would
924 * be set to the position of the last selected text field.
930 void TitleWidget::slotInsertUnicode()
932 m_unicodeDialog->exec();
935 void TitleWidget::slotInsertUnicodeString(QString text)
937 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
939 if (l.at(0)->type() == TEXTITEM) {
940 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
941 t->textCursor().insertText(text);
946 void TitleWidget::slotUpdateText()
948 QFont font = font_family->currentFont();
949 font.setPixelSize(font_size->value());
950 font.setBold(buttonBold->isChecked());
951 font.setItalic(buttonItalic->isChecked());
952 font.setUnderline(buttonUnder->isChecked());
953 QColor color = fontColorButton->color();
954 color.setAlpha(textAlpha->value());
956 QGraphicsTextItem* item = NULL;
957 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
958 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
959 item = static_cast <QGraphicsTextItem *>(l.at(0));
962 //if (item->textCursor().selection ().isEmpty())
963 QTextCursor cur = item->textCursor();
964 QTextBlockFormat format = cur.blockFormat();
965 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
966 item->setTextWidth(item->boundingRect().width());
967 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
968 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
969 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
971 format.setAlignment(Qt::AlignLeft);
972 item->setTextWidth(-1);
977 item->setDefaultTextColor(color);
978 cur.select(QTextCursor::Document);
979 cur.setBlockFormat(format);
980 item->setTextCursor(cur);
981 cur.clearSelection();
982 item->setTextCursor(cur);
987 void TitleWidget::rectChanged()
989 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
990 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
991 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
992 QColor f = rectFColor->color();
993 f.setAlpha(rectFAlpha->value());
995 penf.setWidth(rectLineWidth->value());
997 QColor b = rectBColor->color();
998 b.setAlpha(rectBAlpha->value());
999 rec->setBrush(QBrush(b));
1003 void TitleWidget::itemScaled(int val)
1005 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1006 if (l.size() == 1) {
1007 Transform x = m_transformations.value(l.at(0));
1008 x.scalex = (double)val / 100.0;
1009 x.scaley = (double)val / 100.0;
1011 qtrans.scale(x.scalex, x.scaley);
1012 qtrans.rotate(x.rotate);
1013 l[0]->setTransform(qtrans);
1014 m_transformations[l.at(0)] = x;
1015 updateDimension(l.at(0));
1019 void TitleWidget::itemRotate(int val)
1021 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1022 if (l.size() == 1) {
1023 Transform x = m_transformations[l.at(0)];
1024 x.rotate = (double)val;
1026 qtrans.scale(x.scalex, x.scaley);
1027 qtrans.rotate(x.rotate);
1028 l[0]->setTransform(qtrans);
1029 m_transformations[l.at(0)] = x;
1030 updateDimension(l.at(0));
1034 void TitleWidget::itemHCenter()
1036 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1037 if (l.size() == 1) {
1038 QGraphicsItem *item = l.at(0);
1040 br = item->sceneBoundingRect();
1041 int width = (int)br.width();
1042 int newPos = (int)((m_frameWidth - width) / 2);
1043 newPos += item->pos().x() - br.left(); // Check item transformation
1044 item->setPos(newPos, item->pos().y());
1045 updateCoordinates(item);
1049 void TitleWidget::itemVCenter()
1051 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1052 if (l.size() == 1) {
1053 QGraphicsItem *item = l.at(0);
1055 br = item->sceneBoundingRect();
1056 int height = (int)br.height();
1057 int newPos = (int)((m_frameHeight - height) / 2);
1058 newPos += item->pos().y() - br.top(); // Check item transformation
1059 item->setPos(item->pos().x(), newPos);
1060 updateCoordinates(item);
1064 void TitleWidget::setupViewports()
1066 double aspect_ratio = 4.0 / 3.0;//read from project
1068 QRectF sp(0, 0, 0, 0);
1069 QRectF ep(0, 0, 0, 0);
1071 double sv_size = startViewportSize->value();
1072 double ev_size = endViewportSize->value();
1073 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
1074 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
1076 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
1077 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
1079 m_startViewport->setPolygon(QPolygonF(sp));
1080 m_endViewport->setPolygon(QPolygonF(ep));
1084 void TitleWidget::loadTitle()
1086 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "*.kdenlivetitle", this, i18n("Load Title"));
1087 if (!url.isEmpty()) {
1088 QList<QGraphicsItem *> items = m_scene->items();
1089 for (int i = 0; i < items.size(); i++) {
1090 if (items.at(i)->zValue() > -1000) delete items.at(i);
1092 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
1097 void TitleWidget::saveTitle(KUrl url)
1099 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectTitlePath), "*.kdenlivetitle", this, i18n("Save Title"));
1100 if (!url.isEmpty()) {
1101 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
1102 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1106 QDomDocument TitleWidget::xml()
1108 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1109 if (cropImage->isChecked()) {
1110 doc.documentElement().setAttribute("crop", 1);
1115 void TitleWidget::setXml(QDomDocument doc)
1117 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
1118 QDomElement e = doc.documentElement();
1119 cropImage->setChecked(e.hasAttribute("crop"));
1120 m_transformations.clear();
1121 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1122 const double PI = 4.0 * atan(1.0);
1123 for (int i = 0; i < items.count(); i++) {
1124 QTransform t = items.at(i)->transform();
1128 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1129 m_transformations[items.at(i)] = x;
1131 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1132 QColor background_color = m_titledocument.getBackgroundColor();
1133 horizontalSlider->blockSignals(true);
1134 kcolorbutton->blockSignals(true);
1135 horizontalSlider->setValue(background_color.alpha());
1136 background_color.setAlpha(255);
1137 kcolorbutton->setColor(background_color);
1138 horizontalSlider->blockSignals(false);
1139 kcolorbutton->blockSignals(false);
1144 const QRect TitleWidget::renderedRect()
1148 int maxX = m_frameWidth;
1149 int maxY = m_frameHeight;
1150 if (!cropImage->isChecked()) {
1151 m_scene->removeItem(m_startViewport);
1152 m_scene->removeItem(m_endViewport);
1153 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1154 if (boundingRect.left() < 0) minX = boundingRect.left();
1155 if (boundingRect.top() < 0) minY = boundingRect.top();
1156 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1157 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1165 QRect rect(minX, minY, maxX, maxY);
1169 QImage TitleWidget::renderedPixmap()
1171 QRect rect = renderedRect();
1172 QImage pix(rect.width(), rect.height(), QImage::Format_ARGB32);
1173 pix.fill(Qt::transparent);
1174 QPainter painter(&pix);
1175 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1176 m_scene->clearTextSelection();
1177 QPen framepen = m_frameBorder->pen();
1178 m_frameBorder->setPen(Qt::NoPen);
1179 m_startViewport->setVisible(false);
1180 m_endViewport->setVisible(false);
1181 m_frameImage->setVisible(false);
1183 m_scene->render(&painter, QRectF(), rect); //QRectF(minX, minY, maxX - minX, maxY - minY));
1185 m_frameBorder->setPen(framepen);
1186 m_startViewport->setVisible(true);
1187 m_endViewport->setVisible(true);
1188 m_frameImage->setVisible(true);
1192 /** \brief Connected to the accepted signal - calls writeChoices */
1193 void TitleWidget::slotAccepted()
1198 /** \brief Store the current choices of font, background and rect values */
1199 void TitleWidget::writeChoices()
1201 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1202 KSharedConfigPtr config = KGlobal::config();
1203 KConfigGroup titleConfig(config, "TitleWidget");
1204 // Write the entries
1205 titleConfig.writeEntry("font_family", font_family->currentFont());
1206 //titleConfig.writeEntry("font_size", font_size->value());
1207 titleConfig.writeEntry("font_pixel_size", font_size->value());
1208 titleConfig.writeEntry("font_color", fontColorButton->color());
1209 titleConfig.writeEntry("font_alpha", textAlpha->value());
1210 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1211 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1212 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1214 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1215 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1216 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1217 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1218 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1220 titleConfig.writeEntry("background_color", kcolorbutton->color());
1221 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1223 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1225 //! \todo Not sure if I should sync - it is probably safe to do it
1230 /** \brief Read the last stored choices into the dialog */
1231 void TitleWidget::readChoices()
1233 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1234 KSharedConfigPtr config = KGlobal::config();
1235 KConfigGroup titleConfig(config, "TitleWidget");
1237 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1238 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1239 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1240 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1241 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1242 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1243 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1245 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1246 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1247 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1248 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1249 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1251 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1252 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1254 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));