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 projectPath, Render *render, QWidget *parent) :
45 m_startViewport(NULL),
49 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
50 m_projectPath(projectPath)
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(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
106 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
109 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
111 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
112 buttonRealSize->setIcon(KIcon("zoom-original"));
113 buttonBold->setIcon(KIcon("format-text-bold"));
114 buttonItalic->setIcon(KIcon("format-text-italic"));
115 buttonUnder->setIcon(KIcon("format-text-underline"));
116 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
117 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
118 buttonAlignRight->setIcon(KIcon("format-justify-right"));
119 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
120 buttonInsertUnicode->setIcon(KIcon("kdenlive-insert-unicode"));
122 buttonAlignNone->setToolTip(i18n("No alignment"));
123 buttonAlignRight->setToolTip(i18n("Align right"));
124 buttonAlignLeft->setToolTip(i18n("Align left"));
125 buttonAlignCenter->setToolTip(i18n("Align center"));
126 buttonInsertUnicode->setToolTip(i18n("Insert Unicode character (Shift+Ctrl+U)"));
127 buttonInsertUnicode->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
128 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
129 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
130 rectBColor->setToolTip(i18n("Select fill color"));
131 rectFColor->setToolTip(i18n("Select border color"));
132 rectBAlpha->setToolTip(i18n("Fill transparency"));
133 rectFAlpha->setToolTip(i18n("Border transparency"));
134 zoom_slider->setToolTip(i18n("Zoom"));
135 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
136 buttonFitZoom->setToolTip(i18n("Fit zoom"));
137 kcolorbutton->setToolTip(i18n("Select background color"));
138 horizontalSlider->setToolTip(i18n("Background Transparency"));
140 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
141 itemhcenter->setToolTip(i18n("Align item horizontally"));
142 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
143 itemvcenter->setToolTip(i18n("Align item vertically"));
145 QHBoxLayout *layout = new QHBoxLayout;
146 frame_toolbar->setLayout(layout);
147 layout->setContentsMargins(2, 2, 2, 2);
148 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
150 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle (Alt+R)"));
151 m_buttonRect->setCheckable(true);
152 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
153 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
155 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text (Alt+T)"));
156 m_buttonText->setCheckable(true);
157 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
158 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
160 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image (Alt+I)"));
161 m_buttonImage->setCheckable(false);
162 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
163 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
165 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool (Alt+S)"));
166 m_buttonCursor->setCheckable(true);
167 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
168 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
170 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
171 m_buttonLoad->setCheckable(false);
172 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
173 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
175 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
176 m_buttonSave->setCheckable(false);
177 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
178 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
180 layout->addWidget(m_toolbar);
181 text_properties->setHidden(true);
183 // initialize graphic scene
184 m_scene = new GraphicsSceneRectMove(this);
185 graphicsView->setScene(m_scene);
186 m_titledocument.setScene(m_scene);
187 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
189 // a gradient background
190 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
191 gradient->setSpread(QGradient::ReflectSpread);
192 scene->setBackgroundBrush(*gradient);*/
194 m_frameImage = new QGraphicsPixmapItem();
196 qtrans.scale(2.0, 2.0);
197 m_frameImage->setTransform(qtrans);
198 m_frameImage->setZValue(-1200);
199 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
200 displayBackgroundFrame();
201 graphicsView->scene()->addItem(m_frameImage);
203 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
204 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
205 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
206 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
207 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
208 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
209 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
210 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
212 QPen framepen(Qt::DotLine);
213 framepen.setColor(Qt::red);
215 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
216 m_frameBorder->setPen(framepen);
217 m_frameBorder->setZValue(-1100);
218 m_frameBorder->setBrush(Qt::transparent);
219 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
220 graphicsView->scene()->addItem(m_frameBorder);
222 // mbd: load saved settings
226 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
227 graphicsView->show();
228 //graphicsView->setRenderHint(QPainter::Antialiasing);
229 graphicsView->setInteractive(true);
230 //graphicsView->resize(400, 300);
231 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
232 toolBox->setItemEnabled(2, false);
233 if (!url.isEmpty()) {
234 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
241 TitleWidget::~TitleWidget()
245 delete m_buttonImage;
246 delete m_buttonCursor;
250 delete m_unicodeDialog;
251 delete m_frameBorder;
253 delete m_startViewport;
254 delete m_endViewport;
259 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
262 QString titlePath = projectUrl.path() + "/titles/";
263 KStandardDirs::makeDir(titlePath);
264 QString titleName = "title";
266 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
267 while (QFile::exists(path + ".png")) {
269 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
271 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
272 result.append(path + ".png");
276 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
279 QString titlePath = projectUrl.path() + "/titles/";
280 KStandardDirs::makeDir(titlePath);
281 return titlePath + titleName + ".png";
285 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
290 void TitleWidget::slotTextTool()
292 rect_properties->setHidden(true);
293 text_properties->setHidden(false);
294 m_scene->setTool(TITLE_TEXT);
295 m_buttonRect->setChecked(false);
296 m_buttonCursor->setChecked(false);
299 void TitleWidget::slotRectTool()
301 text_properties->setHidden(true);
302 rect_properties->setHidden(false);
303 m_scene->setTool(TITLE_RECTANGLE);
304 m_buttonText->setChecked(false);
305 m_buttonCursor->setChecked(false);
306 m_buttonRect->setChecked(true);
309 void TitleWidget::slotSelectTool()
311 m_scene->setTool(TITLE_SELECT);
312 m_buttonCursor->setChecked(true);
313 m_buttonText->setChecked(false);
314 m_buttonRect->setChecked(false);
317 void TitleWidget::slotImageTool()
319 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
320 if (!url.isEmpty()) {
321 if (url.path().endsWith(".svg")) {
322 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
323 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
324 svg->setZValue(m_count++);
325 svg->setData(Qt::UserRole, url.path());
326 graphicsView->scene()->addItem(svg);
328 QPixmap pix(url.path());
329 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
330 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
331 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
332 image->setData(Qt::UserRole, url.path());
333 image->setZValue(m_count++);
334 graphicsView->scene()->addItem(image);
337 m_scene->setTool(TITLE_SELECT);
338 m_buttonRect->setChecked(false);
339 m_buttonCursor->setChecked(true);
340 m_buttonText->setChecked(false);
343 void TitleWidget::displayBackgroundFrame()
345 if (!displayBg->isChecked()) {
346 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
347 QPixmap pattern(20, 20);
351 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
352 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
357 p.fillRect(bg.rect(), br);
359 m_frameImage->setPixmap(bg);
361 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
365 void TitleWidget::initViewports()
367 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
368 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
370 QPen startpen(Qt::DotLine);
371 QPen endpen(Qt::DashDotLine);
372 startpen.setColor(QColor(100, 200, 100, 140));
373 endpen.setColor(QColor(200, 100, 100, 140));
375 m_startViewport->setPen(startpen);
376 m_endViewport->setPen(endpen);
378 startViewportSize->setValue(40);
379 endViewportSize->setValue(40);
381 m_startViewport->setZValue(-1000);
382 m_endViewport->setZValue(-1000);
384 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
385 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
387 graphicsView->scene()->addItem(m_startViewport);
388 graphicsView->scene()->addItem(m_endViewport);
391 void TitleWidget::slotUpdateZoom(int pos)
393 m_scene->setZoom((double) pos / 100);
394 zoom_label->setText(QString::number(pos) + '%');
397 void TitleWidget::slotZoom(bool up)
399 int pos = zoom_slider->value();
402 zoom_slider->setValue(pos);
405 void TitleWidget::slotAdjustZoom()
407 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
408 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
409 if (scalex > scaley) scalex = scaley;
410 int zoompos = (int)(scalex * 7 + 0.5);*/
411 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
412 int zoompos = graphicsView->matrix().m11() * 100;
413 zoom_slider->setValue(zoompos);
414 graphicsView->centerOn(m_frameBorder);
417 void TitleWidget::slotZoomOneToOne()
419 zoom_slider->setValue(100);
420 graphicsView->centerOn(m_frameBorder);
423 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
425 QColor f = rectFColor->color();
426 f.setAlpha(rectFAlpha->value());
428 penf.setWidth(rectLineWidth->value());
430 QColor b = rectBColor->color();
431 b.setAlpha(rectBAlpha->value());
432 rect->setBrush(QBrush(b));
433 rect->setZValue(m_count++);
434 //setCurrentItem(rect);
435 //graphicsView->setFocus();
438 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
440 QFont font = font_family->currentFont();
441 font.setPixelSize(font_size->value());
443 font.setBold(buttonBold->isChecked());
444 font.setItalic(buttonItalic->isChecked());
445 font.setUnderline(buttonUnder->isChecked());
448 QColor color = fontColorButton->color();
449 color.setAlpha(textAlpha->value());
450 tt->setDefaultTextColor(color);
451 tt->setZValue(m_count++);
455 void TitleWidget::setCurrentItem(QGraphicsItem *item)
457 m_scene->setSelectedItem(item);
460 void TitleWidget::zIndexChanged(int v)
462 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
468 void TitleWidget::selectionChanged()
470 if (m_scene->tool() != TITLE_SELECT) return;
471 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
472 //toolBox->setItemEnabled(2, false);
473 //toolBox->setItemEnabled(3, false);
474 value_x->blockSignals(true);
475 value_y->blockSignals(true);
476 value_w->blockSignals(true);
477 value_h->blockSignals(true);
478 itemzoom->blockSignals(true);
479 itemrotate->blockSignals(true);
481 bool blockX = !origin_x_left->signalsBlocked();
482 bool blockY = !origin_y_top->signalsBlocked();
483 if (blockX) origin_x_left->blockSignals(true);
484 if (blockY) origin_y_top->blockSignals(true);
485 origin_x_left->setChecked(false);
486 origin_y_top->setChecked(false);
489 frame_properties->setEnabled(false);
490 text_properties->setEnabled(false);
491 rect_properties->setEnabled(false);
492 if (blockX) origin_x_left->blockSignals(false);
493 if (blockY) origin_y_top->blockSignals(false);
494 } else if (l.size() == 1) {
495 if (l.at(0)->type() == TEXTITEM) {
496 rect_properties->setHidden(true);
497 text_properties->setHidden(false);
498 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
499 //if (l[0]->hasFocus())
500 toolBox->setCurrentIndex(0);
501 //toolBox->setItemEnabled(2, true);
502 font_size->blockSignals(true);
503 font_family->blockSignals(true);
504 buttonBold->blockSignals(true);
505 buttonItalic->blockSignals(true);
506 buttonUnder->blockSignals(true);
507 fontColorButton->blockSignals(true);
508 textAlpha->blockSignals(true);
509 buttonAlignLeft->blockSignals(true);
510 buttonAlignRight->blockSignals(true);
511 buttonAlignNone->blockSignals(true);
512 buttonAlignCenter->blockSignals(true);
514 QFont font = i->font();
515 font_family->setCurrentFont(font);
516 font_size->setValue(font.pixelSize());
517 buttonBold->setChecked(font.bold());
518 buttonItalic->setChecked(font.italic());
519 buttonUnder->setChecked(font.underline());
521 QColor color = i->defaultTextColor();
522 fontColorButton->setColor(color);
523 textAlpha->setValue(color.alpha());
525 QTextCursor cur = i->textCursor();
526 QTextBlockFormat format = cur.blockFormat();
527 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
528 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
529 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
530 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
532 font_size->blockSignals(false);
533 font_family->blockSignals(false);
534 buttonBold->blockSignals(false);
535 buttonItalic->blockSignals(false);
536 buttonUnder->blockSignals(false);
537 fontColorButton->blockSignals(false);
538 textAlpha->blockSignals(false);
539 buttonAlignLeft->blockSignals(false);
540 buttonAlignRight->blockSignals(false);
541 buttonAlignNone->blockSignals(false);
542 buttonAlignCenter->blockSignals(false);
544 updateAxisButtons(i);
545 updateCoordinates(i);
547 //value_w->setValue((int) i->boundingRect().width());
548 //value_h->setValue((int) i->boundingRect().height());
549 frame_properties->setEnabled(true);
550 text_properties->setEnabled(true);
551 rect_properties->setEnabled(false);
552 value_w->setEnabled(false);
553 value_h->setEnabled(false);
555 } else if ((l.at(0))->type() == RECTITEM) {
556 rect_properties->setHidden(false);
557 text_properties->setHidden(true);
559 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
560 toolBox->setCurrentIndex(0);
561 //toolBox->setItemEnabled(3, true);
562 rectFAlpha->setValue(rec->pen().color().alpha());
563 rectBAlpha->setValue(rec->brush().color().alpha());
564 //kDebug() << rec->brush().color().alpha();
565 QColor fcol = rec->pen().color();
566 QColor bcol = rec->brush().color();
567 //fcol.setAlpha(255);
568 //bcol.setAlpha(255);
569 rectFColor->setColor(fcol);
570 rectBColor->setColor(bcol);
572 rectLineWidth->setValue(rec->pen().width());
574 updateAxisButtons(l.at(0));
575 updateCoordinates(rec);
576 updateDimension(rec);
577 //value_w->setValue((int) rec->rect().width());
578 //value_h->setValue((int) rec->rect().height());
579 frame_properties->setEnabled(true);
580 text_properties->setEnabled(false);
581 rect_properties->setEnabled(true);
582 value_w->setEnabled(true);
583 value_h->setEnabled(true);
585 } else if (l.at(0)->type() == IMAGEITEM) {
586 updateCoordinates(l.at(0));
587 updateDimension(l.at(0));
589 frame_properties->setEnabled(true);
590 text_properties->setEnabled(false);
591 rect_properties->setEnabled(false);
592 value_x->setEnabled(true);
593 value_w->setEnabled(false);
594 value_h->setEnabled(false);
597 //toolBox->setCurrentIndex(0);
598 frame_properties->setEnabled(false);
599 text_properties->setEnabled(false);
600 rect_properties->setEnabled(false);
602 zValue->setValue((int)l.at(0)->zValue());
603 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
604 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
605 value_x->blockSignals(false);
606 value_y->blockSignals(false);
607 value_w->blockSignals(false);
608 value_h->blockSignals(false);
609 itemzoom->blockSignals(false);
610 itemrotate->blockSignals(false);
614 /** \brief Updates position/size of the selected item when a value
615 * of an item (coordinates, size) has changed */
616 void TitleWidget::slotAdjustSelectedItem()
618 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
620 if (l.at(0)->type() == RECTITEM) {
622 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
624 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
625 } else if (l.at(0)->type() == TEXTITEM) {
627 updatePosition(l.at(0));
628 } else if (l.at(0)->type() == IMAGEITEM) {
630 updatePosition(l.at(0));
635 /** \brief Updates width/height int the text fields, regarding transformation matrix */
636 void TitleWidget::updateDimension(QGraphicsItem *i)
638 bool blockW = !value_w->signalsBlocked();
639 bool blockH = !value_h->signalsBlocked();
641 if (blockW) value_w->blockSignals(true);
642 if (blockH) value_h->blockSignals(true);
645 if (i->type() == IMAGEITEM) {
646 // Get multipliers for rotation/scaling
648 /*Transform t = m_transformations.value(i);
649 QRectF r = i->boundingRect();
650 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
651 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
652 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
653 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
655 value_w->setValue(i->sceneBoundingRect().width());
656 value_h->setValue(i->sceneBoundingRect().height());
657 } else if (i->type() == RECTITEM) {
658 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *> (i);
659 value_w->setValue((int) r->rect().width());
660 value_h->setValue((int) r->rect().height());
661 } else if (i->type() == TEXTITEM) {
662 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *> (i);
663 value_w->setValue((int) t->boundingRect().width());
664 value_h->setValue((int) t->boundingRect().height());
667 if (blockW) value_w->blockSignals(false);
668 if (blockH) value_h->blockSignals(false);
671 /** \brief Updates the coordinates in the text fields from the item */
672 void TitleWidget::updateCoordinates(QGraphicsItem *i)
675 bool blockX = !value_x->signalsBlocked();
676 bool blockY = !value_y->signalsBlocked();
678 // Block signals emitted by this method
679 if (blockX) value_x->blockSignals(true);
680 if (blockY) value_y->blockSignals(true);
682 if (i->type() == TEXTITEM) {
684 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *> (i);
686 // Set the correct x coordinate value
687 if (origin_x_left->isChecked()) {
688 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
689 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
691 // Origin is at 0 (default)
692 value_x->setValue((int) rec->pos().x());
696 if (origin_y_top->isChecked()) {
697 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
699 value_y->setValue((int) rec->pos().y());
702 } else if (i->type() == RECTITEM) {
704 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
706 if (origin_x_left->isChecked()) {
707 // Origin (0 point) is at m_frameWidth
708 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width()));
710 // Origin is at 0 (default)
711 value_x->setValue((int) rec->pos().x());
714 if (origin_y_top->isChecked()) {
715 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->rect().height()));
717 value_y->setValue((int) rec->pos().y());
720 } else if (i->type() == IMAGEITEM) {
722 if (origin_x_left->isChecked()) {
723 value_x->setValue((int) (m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
725 value_x->setValue((int) i->pos().x());
728 if (origin_y_top->isChecked()) {
729 value_y->setValue((int) (m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
731 value_y->setValue((int) i->pos().y());
736 // Stop blocking signals now
737 if (!blockX) value_x->blockSignals(false);
738 if (!blockY) value_y->blockSignals(false);
741 /** \brief Updates the position of an item by reading coordinates from the text fields */
742 void TitleWidget::updatePosition(QGraphicsItem *i) {
744 if (i->type() == TEXTITEM) {
745 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
748 if (origin_x_left->isChecked()) {
749 /* Origin of the x axis is at m_frameWidth,
750 * and distance from right border of the item to the right
751 * border of the frame is taken.
752 * See comment to slotOriginXClicked().
754 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
756 posX = value_x->value();
760 if (origin_y_top->isChecked()) {
761 /* Same for y axis */
762 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
764 posY = value_y->value();
767 rec->setPos(posX, posY);
769 } else if (i->type() == RECTITEM) {
771 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
774 if (origin_x_left->isChecked()) {
775 posX = m_frameWidth - value_x->value() - rec->rect().width();
777 posX = value_x->value();
781 if (origin_y_top->isChecked()) {
782 posY = m_frameHeight - value_y->value() - rec->rect().height();
784 posY = value_y->value();
787 rec->setPos(posX, posY);
789 } else if (i->type() == IMAGEITEM) {
792 if (origin_x_left->isChecked()) {
793 // Use the sceneBoundingRect because this also regards transformations like zoom
794 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
796 posX = value_x->value();
800 if (origin_y_top->isChecked()) {
801 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
803 posY = value_y->value();
806 i->setPos(posX, posY);
812 void TitleWidget::updateTextOriginX()
814 if (origin_x_left->isChecked()) {
815 origin_x_left->setText(i18n("\u2212X"));
817 origin_x_left->setText(i18n("+X"));
821 void TitleWidget::slotOriginXClicked()
823 // Update the text displayed on the button.
826 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
828 updateCoordinates(l.at(0));
830 // Remember x axis setting
831 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()?
832 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
836 void TitleWidget::updateTextOriginY()
838 if (origin_y_top->isChecked()) {
839 origin_y_top->setText(i18n("\u2212Y"));
841 origin_y_top->setText(i18n("+Y"));
845 void TitleWidget::slotOriginYClicked()
847 // Update the text displayed on the button.
850 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
852 updateCoordinates(l.at(0));
854 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked()?
855 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
860 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
862 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
863 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
864 origin_x_left->blockSignals(true);
865 origin_y_top->blockSignals(true);
867 if (xAxis == TitleDocument::AxisInverted) {
868 origin_x_left->setChecked(true);
870 origin_x_left->setChecked(false);
874 if (yAxis == TitleDocument::AxisInverted) {
875 origin_y_top->setChecked(true);
877 origin_y_top->setChecked(false);
881 origin_x_left->blockSignals(false);
882 origin_y_top->blockSignals(false);
885 void TitleWidget::slotChangeBackground()
887 QColor color = kcolorbutton->color();
888 color.setAlpha(horizontalSlider->value());
889 m_frameBorder->setBrush(QBrush(color));
893 * Something (yeah) has changed in our QGraphicsScene.
895 void TitleWidget::slotChanged() {
896 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
897 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
898 textChanged(static_cast <QGraphicsTextItem *> (l.at(0)));
903 * If the user has set origin_x_left (everything also for y),
904 * we need to look whether a text element has been selected. If yes,
905 * we need to ensure that the right border of the text field
906 * remains fixed also when some text has been entered.
908 * This is also known as right-justified, with the difference that
909 * it is not valid for text but for its boundingRect. Text may still
912 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->showLastUnicode();
933 m_unicodeDialog->exec();
936 void TitleWidget::slotInsertUnicodeString(QString text)
938 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
940 if (l.at(0)->type() == TEXTITEM) {
941 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *> (l.at(0));
942 t->textCursor().insertText(text);
947 void TitleWidget::slotUpdateText()
949 QFont font = font_family->currentFont();
950 font.setPixelSize(font_size->value());
951 font.setBold(buttonBold->isChecked());
952 font.setItalic(buttonItalic->isChecked());
953 font.setUnderline(buttonUnder->isChecked());
954 QColor color = fontColorButton->color();
955 color.setAlpha(textAlpha->value());
957 QGraphicsTextItem* item = NULL;
958 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
959 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
960 item = static_cast <QGraphicsTextItem *>(l.at(0));
963 //if (item->textCursor().selection ().isEmpty())
964 QTextCursor cur = item->textCursor();
965 QTextBlockFormat format = cur.blockFormat();
966 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
967 item->setTextWidth(item->boundingRect().width());
968 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
969 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
970 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
972 format.setAlignment(Qt::AlignLeft);
973 item->setTextWidth(-1);
978 item->setDefaultTextColor(color);
979 cur.select(QTextCursor::Document);
980 cur.setBlockFormat(format);
981 item->setTextCursor(cur);
982 cur.clearSelection();
983 item->setTextCursor(cur);
988 void TitleWidget::rectChanged()
990 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
991 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
992 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
993 QColor f = rectFColor->color();
994 f.setAlpha(rectFAlpha->value());
996 penf.setWidth(rectLineWidth->value());
998 QColor b = rectBColor->color();
999 b.setAlpha(rectBAlpha->value());
1000 rec->setBrush(QBrush(b));
1004 void TitleWidget::itemScaled(int val)
1006 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1007 if (l.size() == 1) {
1008 Transform x = m_transformations.value(l.at(0));
1009 x.scalex = (double)val / 100.0;
1010 x.scaley = (double)val / 100.0;
1012 qtrans.scale(x.scalex, x.scaley);
1013 qtrans.rotate(x.rotate);
1014 l[0]->setTransform(qtrans);
1015 m_transformations[l.at(0)] = x;
1016 updateDimension(l.at(0));
1020 void TitleWidget::itemRotate(int val)
1022 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1023 if (l.size() == 1) {
1024 Transform x = m_transformations[l.at(0)];
1025 x.rotate = (double)val;
1027 qtrans.scale(x.scalex, x.scaley);
1028 qtrans.rotate(x.rotate);
1029 l[0]->setTransform(qtrans);
1030 m_transformations[l.at(0)] = x;
1031 updateDimension(l.at(0));
1035 void TitleWidget::itemHCenter()
1037 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1038 if (l.size() == 1) {
1039 QGraphicsItem *item = l.at(0);
1041 br = item->sceneBoundingRect();
1042 int width = (int)br.width();
1043 int newPos = (int)((m_frameWidth - width) / 2);
1044 newPos += item->pos().x() - br.left(); // Check item transformation
1045 item->setPos(newPos, item->pos().y());
1046 updateCoordinates(item);
1050 void TitleWidget::itemVCenter()
1052 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1053 if (l.size() == 1) {
1054 QGraphicsItem *item = l.at(0);
1056 br = item->sceneBoundingRect();
1057 int height = (int)br.height();
1058 int newPos = (int)((m_frameHeight - height) / 2);
1059 newPos += item->pos().y() - br.top(); // Check item transformation
1060 item->setPos(item->pos().x(), newPos);
1061 updateCoordinates(item);
1065 void TitleWidget::setupViewports()
1067 double aspect_ratio = 4.0 / 3.0;//read from project
1069 QRectF sp(0, 0, 0, 0);
1070 QRectF ep(0, 0, 0, 0);
1072 double sv_size = startViewportSize->value();
1073 double ev_size = endViewportSize->value();
1074 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
1075 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
1077 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
1078 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
1080 m_startViewport->setPolygon(QPolygonF(sp));
1081 m_endViewport->setPolygon(QPolygonF(ep));
1085 void TitleWidget::loadTitle()
1087 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
1088 if (!url.isEmpty()) {
1089 QList<QGraphicsItem *> items = m_scene->items();
1090 for (int i = 0; i < items.size(); i++) {
1091 if (items.at(i)->zValue() > -1000) delete items.at(i);
1093 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
1098 void TitleWidget::saveTitle(KUrl url)
1100 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
1101 if (!url.isEmpty()) {
1102 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
1103 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1107 QDomDocument TitleWidget::xml()
1109 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1110 if (cropImage->isChecked()) {
1111 doc.documentElement().setAttribute("crop", 1);
1116 void TitleWidget::setXml(QDomDocument doc)
1118 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
1119 QDomElement e = doc.documentElement();
1120 cropImage->setChecked(e.hasAttribute("crop"));
1121 m_transformations.clear();
1122 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1123 const double PI = 4.0 * atan(1.0);
1124 for (int i = 0; i < items.count(); i++) {
1125 QTransform t = items.at(i)->transform();
1129 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1130 m_transformations[items.at(i)] = x;
1132 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1133 QColor background_color = m_titledocument.getBackgroundColor();
1134 horizontalSlider->blockSignals(true);
1135 kcolorbutton->blockSignals(true);
1136 horizontalSlider->setValue(background_color.alpha());
1137 background_color.setAlpha(255);
1138 kcolorbutton->setColor(background_color);
1139 horizontalSlider->blockSignals(false);
1140 kcolorbutton->blockSignals(false);
1145 QImage TitleWidget::renderedPixmap()
1149 int maxX = m_frameWidth;
1150 int maxY = m_frameHeight;
1151 if (!cropImage->isChecked()) {
1152 m_scene->removeItem(m_startViewport);
1153 m_scene->removeItem(m_endViewport);
1154 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1155 if (boundingRect.left() < 0) minX = boundingRect.left();
1156 if (boundingRect.top() < 0) minY = boundingRect.top();
1157 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1158 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1167 QImage pix(maxX, maxY, QImage::Format_ARGB32);
1168 pix.fill(Qt::transparent);
1169 QPainter painter(&pix);
1170 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1171 m_scene->clearTextSelection();
1172 QPen framepen = m_frameBorder->pen();
1173 m_frameBorder->setPen(Qt::NoPen);
1174 m_startViewport->setVisible(false);
1175 m_endViewport->setVisible(false);
1176 m_frameImage->setVisible(false);
1178 m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
1180 m_frameBorder->setPen(framepen);
1181 m_startViewport->setVisible(true);
1182 m_endViewport->setVisible(true);
1183 m_frameImage->setVisible(true);
1187 /** \brief Connected to the accepted signal - calls writeChoices */
1188 void TitleWidget::slotAccepted()
1193 /** \brief Store the current choices of font, background and rect values */
1194 void TitleWidget::writeChoices()
1196 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1197 KSharedConfigPtr config = KGlobal::config();
1198 KConfigGroup titleConfig(config, "TitleWidget");
1199 // Write the entries
1200 titleConfig.writeEntry("font_family", font_family->currentFont());
1201 //titleConfig.writeEntry("font_size", font_size->value());
1202 titleConfig.writeEntry("font_pixel_size", font_size->value());
1203 titleConfig.writeEntry("font_color", fontColorButton->color());
1204 titleConfig.writeEntry("font_alpha", textAlpha->value());
1205 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1206 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1207 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1209 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1210 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1211 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1212 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1213 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1215 titleConfig.writeEntry("background_color", kcolorbutton->color());
1216 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1218 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1219 //! \todo Not sure if I should sync - it is probably safe to do it
1224 /** \brief Read the last stored choices into the dialog */
1225 void TitleWidget::readChoices()
1227 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1228 KSharedConfigPtr config = KGlobal::config();
1229 KConfigGroup titleConfig(config, "TitleWidget");
1231 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1232 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1233 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1234 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1235 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1236 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1237 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1239 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1240 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1241 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1242 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1243 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1245 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1246 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1248 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));