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->setToolTip(i18n("No alignment"));
120 buttonAlignRight->setToolTip(i18n("Align right"));
121 buttonAlignLeft->setToolTip(i18n("Align left"));
122 buttonAlignCenter->setToolTip(i18n("Align center"));
123 buttonInsertUnicode->setToolTip(i18n("Insert Unicode character (Shift+Ctrl+U)"));
124 buttonInsertUnicode->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
125 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
126 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
127 rectBColor->setToolTip(i18n("Select fill color"));
128 rectFColor->setToolTip(i18n("Select border color"));
129 rectBAlpha->setToolTip(i18n("Fill transparency"));
130 rectFAlpha->setToolTip(i18n("Border transparency"));
131 zoom_slider->setToolTip(i18n("Zoom"));
132 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
133 buttonFitZoom->setToolTip(i18n("Fit zoom"));
134 kcolorbutton->setToolTip(i18n("Select background color"));
135 horizontalSlider->setToolTip(i18n("Background Transparency"));
137 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
138 itemhcenter->setToolTip(i18n("Align item horizontally"));
139 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
140 itemvcenter->setToolTip(i18n("Align item vertically"));
142 QHBoxLayout *layout = new QHBoxLayout;
143 frame_toolbar->setLayout(layout);
144 layout->setContentsMargins(2, 2, 2, 2);
145 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
147 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle (Alt+R)"));
148 m_buttonRect->setCheckable(true);
149 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
150 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
152 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text (Alt+T)"));
153 m_buttonText->setCheckable(true);
154 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
155 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
157 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image (Alt+I)"));
158 m_buttonImage->setCheckable(false);
159 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
160 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
162 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool (Alt+S)"));
163 m_buttonCursor->setCheckable(true);
164 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
165 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
167 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
168 m_buttonLoad->setCheckable(false);
169 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
170 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
172 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
173 m_buttonSave->setCheckable(false);
174 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
175 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
177 layout->addWidget(m_toolbar);
178 text_properties->setHidden(true);
180 // initialize graphic scene
181 m_scene = new GraphicsSceneRectMove(this);
182 graphicsView->setScene(m_scene);
183 m_titledocument.setScene(m_scene);
184 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
186 // a gradient background
187 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
188 gradient->setSpread(QGradient::ReflectSpread);
189 scene->setBackgroundBrush(*gradient);*/
191 m_frameImage = new QGraphicsPixmapItem();
193 qtrans.scale(2.0, 2.0);
194 m_frameImage->setTransform(qtrans);
195 m_frameImage->setZValue(-1200);
196 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
197 displayBackgroundFrame();
198 graphicsView->scene()->addItem(m_frameImage);
200 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
201 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
202 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
203 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
204 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
205 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
206 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
207 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
209 QPen framepen(Qt::DotLine);
210 framepen.setColor(Qt::red);
212 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
213 m_frameBorder->setPen(framepen);
214 m_frameBorder->setZValue(-1100);
215 m_frameBorder->setBrush(Qt::transparent);
216 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
217 graphicsView->scene()->addItem(m_frameBorder);
219 // mbd: load saved settings
223 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
224 graphicsView->show();
225 //graphicsView->setRenderHint(QPainter::Antialiasing);
226 graphicsView->setInteractive(true);
227 //graphicsView->resize(400, 300);
228 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
229 toolBox->setItemEnabled(2, false);
230 if (!url.isEmpty()) {
231 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
238 TitleWidget::~TitleWidget()
242 delete m_buttonImage;
243 delete m_buttonCursor;
247 delete m_unicodeDialog;
248 delete m_frameBorder;
250 delete m_startViewport;
251 delete m_endViewport;
256 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
259 QString titlePath = projectUrl.path() + "/titles/";
260 KStandardDirs::makeDir(titlePath);
261 QString titleName = "title";
263 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
264 while (QFile::exists(path + ".png")) {
266 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
268 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
269 result.append(path + ".png");
273 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
276 QString titlePath = projectUrl.path() + "/titles/";
277 KStandardDirs::makeDir(titlePath);
278 return titlePath + titleName + ".png";
282 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
287 void TitleWidget::slotTextTool()
289 rect_properties->setHidden(true);
290 text_properties->setHidden(false);
291 m_scene->setTool(TITLE_TEXT);
292 m_buttonRect->setChecked(false);
293 m_buttonCursor->setChecked(false);
296 void TitleWidget::slotRectTool()
298 text_properties->setHidden(true);
299 rect_properties->setHidden(false);
300 m_scene->setTool(TITLE_RECTANGLE);
301 m_buttonText->setChecked(false);
302 m_buttonCursor->setChecked(false);
303 m_buttonRect->setChecked(true);
306 void TitleWidget::slotSelectTool()
308 m_scene->setTool(TITLE_SELECT);
309 m_buttonCursor->setChecked(true);
310 m_buttonText->setChecked(false);
311 m_buttonRect->setChecked(false);
314 void TitleWidget::slotImageTool()
316 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
317 if (!url.isEmpty()) {
318 if (url.path().endsWith(".svg")) {
319 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
320 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
321 svg->setZValue(m_count++);
322 svg->setData(Qt::UserRole, url.path());
323 graphicsView->scene()->addItem(svg);
325 QPixmap pix(url.path());
326 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
327 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
328 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
329 image->setData(Qt::UserRole, url.path());
330 image->setZValue(m_count++);
331 graphicsView->scene()->addItem(image);
334 m_scene->setTool(TITLE_SELECT);
335 m_buttonRect->setChecked(false);
336 m_buttonCursor->setChecked(true);
337 m_buttonText->setChecked(false);
340 void TitleWidget::displayBackgroundFrame()
342 if (!displayBg->isChecked()) {
343 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
344 QPixmap pattern(20, 20);
348 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
349 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
354 p.fillRect(bg.rect(), br);
356 m_frameImage->setPixmap(bg);
358 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
362 void TitleWidget::initViewports()
364 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
365 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
367 QPen startpen(Qt::DotLine);
368 QPen endpen(Qt::DashDotLine);
369 startpen.setColor(QColor(100, 200, 100, 140));
370 endpen.setColor(QColor(200, 100, 100, 140));
372 m_startViewport->setPen(startpen);
373 m_endViewport->setPen(endpen);
375 startViewportSize->setValue(40);
376 endViewportSize->setValue(40);
378 m_startViewport->setZValue(-1000);
379 m_endViewport->setZValue(-1000);
381 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
382 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
384 graphicsView->scene()->addItem(m_startViewport);
385 graphicsView->scene()->addItem(m_endViewport);
388 void TitleWidget::slotUpdateZoom(int pos)
390 m_scene->setZoom((double) pos / 100);
391 zoom_label->setText(QString::number(pos) + '%');
394 void TitleWidget::slotZoom(bool up)
396 int pos = zoom_slider->value();
399 zoom_slider->setValue(pos);
402 void TitleWidget::slotAdjustZoom()
404 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
405 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
406 if (scalex > scaley) scalex = scaley;
407 int zoompos = (int)(scalex * 7 + 0.5);*/
408 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
409 int zoompos = graphicsView->matrix().m11() * 100;
410 zoom_slider->setValue(zoompos);
411 graphicsView->centerOn(m_frameBorder);
414 void TitleWidget::slotZoomOneToOne()
416 zoom_slider->setValue(100);
417 graphicsView->centerOn(m_frameBorder);
420 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
422 QColor f = rectFColor->color();
423 f.setAlpha(rectFAlpha->value());
425 penf.setWidth(rectLineWidth->value());
427 QColor b = rectBColor->color();
428 b.setAlpha(rectBAlpha->value());
429 rect->setBrush(QBrush(b));
430 rect->setZValue(m_count++);
431 //setCurrentItem(rect);
432 //graphicsView->setFocus();
435 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
437 QFont font = font_family->currentFont();
438 font.setPixelSize(font_size->value());
440 font.setBold(buttonBold->isChecked());
441 font.setItalic(buttonItalic->isChecked());
442 font.setUnderline(buttonUnder->isChecked());
445 QColor color = fontColorButton->color();
446 color.setAlpha(textAlpha->value());
447 tt->setDefaultTextColor(color);
448 tt->setZValue(m_count++);
452 void TitleWidget::setCurrentItem(QGraphicsItem *item)
454 m_scene->setSelectedItem(item);
457 void TitleWidget::zIndexChanged(int v)
459 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
465 void TitleWidget::selectionChanged()
467 if (m_scene->tool() != TITLE_SELECT) return;
468 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
469 //toolBox->setItemEnabled(2, false);
470 //toolBox->setItemEnabled(3, false);
471 value_x->blockSignals(true);
472 value_y->blockSignals(true);
473 value_w->blockSignals(true);
474 value_h->blockSignals(true);
475 itemzoom->blockSignals(true);
476 itemrotate->blockSignals(true);
478 bool blockX = !origin_x_left->signalsBlocked();
479 bool blockY = !origin_y_top->signalsBlocked();
480 if (blockX) origin_x_left->blockSignals(true);
481 if (blockY) origin_y_top->blockSignals(true);
482 origin_x_left->setChecked(false);
483 origin_y_top->setChecked(false);
486 frame_properties->setEnabled(false);
487 text_properties->setEnabled(false);
488 rect_properties->setEnabled(false);
489 if (blockX) origin_x_left->blockSignals(false);
490 if (blockY) origin_y_top->blockSignals(false);
491 } else if (l.size() == 1) {
492 if (l.at(0)->type() == TEXTITEM) {
493 rect_properties->setHidden(true);
494 text_properties->setHidden(false);
495 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
496 //if (l[0]->hasFocus())
497 toolBox->setCurrentIndex(0);
498 //toolBox->setItemEnabled(2, true);
499 font_size->blockSignals(true);
500 font_family->blockSignals(true);
501 buttonBold->blockSignals(true);
502 buttonItalic->blockSignals(true);
503 buttonUnder->blockSignals(true);
504 fontColorButton->blockSignals(true);
505 textAlpha->blockSignals(true);
506 buttonAlignLeft->blockSignals(true);
507 buttonAlignRight->blockSignals(true);
508 buttonAlignNone->blockSignals(true);
509 buttonAlignCenter->blockSignals(true);
511 QFont font = i->font();
512 font_family->setCurrentFont(font);
513 font_size->setValue(font.pixelSize());
514 buttonBold->setChecked(font.bold());
515 buttonItalic->setChecked(font.italic());
516 buttonUnder->setChecked(font.underline());
518 QColor color = i->defaultTextColor();
519 fontColorButton->setColor(color);
520 textAlpha->setValue(color.alpha());
522 QTextCursor cur = i->textCursor();
523 QTextBlockFormat format = cur.blockFormat();
524 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
525 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
526 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
527 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
529 font_size->blockSignals(false);
530 font_family->blockSignals(false);
531 buttonBold->blockSignals(false);
532 buttonItalic->blockSignals(false);
533 buttonUnder->blockSignals(false);
534 fontColorButton->blockSignals(false);
535 textAlpha->blockSignals(false);
536 buttonAlignLeft->blockSignals(false);
537 buttonAlignRight->blockSignals(false);
538 buttonAlignNone->blockSignals(false);
539 buttonAlignCenter->blockSignals(false);
541 updateAxisButtons(i);
542 updateCoordinates(i);
544 //value_w->setValue((int) i->boundingRect().width());
545 //value_h->setValue((int) i->boundingRect().height());
546 frame_properties->setEnabled(true);
547 text_properties->setEnabled(true);
548 rect_properties->setEnabled(false);
549 value_w->setEnabled(false);
550 value_h->setEnabled(false);
552 } else if ((l.at(0))->type() == RECTITEM) {
553 rect_properties->setHidden(false);
554 text_properties->setHidden(true);
556 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
557 toolBox->setCurrentIndex(0);
558 //toolBox->setItemEnabled(3, true);
559 rectFAlpha->setValue(rec->pen().color().alpha());
560 rectBAlpha->setValue(rec->brush().color().alpha());
561 //kDebug() << rec->brush().color().alpha();
562 QColor fcol = rec->pen().color();
563 QColor bcol = rec->brush().color();
564 //fcol.setAlpha(255);
565 //bcol.setAlpha(255);
566 rectFColor->setColor(fcol);
567 rectBColor->setColor(bcol);
569 rectLineWidth->setValue(rec->pen().width());
571 updateAxisButtons(l.at(0));
572 updateCoordinates(rec);
573 updateDimension(rec);
574 //value_w->setValue((int) rec->rect().width());
575 //value_h->setValue((int) rec->rect().height());
576 frame_properties->setEnabled(true);
577 text_properties->setEnabled(false);
578 rect_properties->setEnabled(true);
579 value_w->setEnabled(true);
580 value_h->setEnabled(true);
582 } else if (l.at(0)->type() == IMAGEITEM) {
583 updateCoordinates(l.at(0));
584 updateDimension(l.at(0));
586 frame_properties->setEnabled(true);
587 text_properties->setEnabled(false);
588 rect_properties->setEnabled(false);
589 value_x->setEnabled(true);
590 value_w->setEnabled(false);
591 value_h->setEnabled(false);
594 //toolBox->setCurrentIndex(0);
595 frame_properties->setEnabled(false);
596 text_properties->setEnabled(false);
597 rect_properties->setEnabled(false);
599 zValue->setValue((int)l.at(0)->zValue());
600 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
601 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
602 value_x->blockSignals(false);
603 value_y->blockSignals(false);
604 value_w->blockSignals(false);
605 value_h->blockSignals(false);
606 itemzoom->blockSignals(false);
607 itemrotate->blockSignals(false);
611 /** \brief Updates position/size of the selected item when a value
612 * of an item (coordinates, size) has changed */
613 void TitleWidget::slotAdjustSelectedItem()
615 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
617 if (l.at(0)->type() == RECTITEM) {
619 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
621 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
622 } else if (l.at(0)->type() == TEXTITEM) {
624 updatePosition(l.at(0));
625 } else if (l.at(0)->type() == IMAGEITEM) {
627 updatePosition(l.at(0));
632 /** \brief Updates width/height int the text fields, regarding transformation matrix */
633 void TitleWidget::updateDimension(QGraphicsItem *i)
635 bool blockW = !value_w->signalsBlocked();
636 bool blockH = !value_h->signalsBlocked();
638 if (blockW) value_w->blockSignals(true);
639 if (blockH) value_h->blockSignals(true);
642 if (i->type() == IMAGEITEM) {
643 // Get multipliers for rotation/scaling
645 /*Transform t = m_transformations.value(i);
646 QRectF r = i->boundingRect();
647 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
648 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
649 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
650 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
652 value_w->setValue(i->sceneBoundingRect().width());
653 value_h->setValue(i->sceneBoundingRect().height());
654 } else if (i->type() == RECTITEM) {
655 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *> (i);
656 value_w->setValue((int) r->rect().width());
657 value_h->setValue((int) r->rect().height());
658 } else if (i->type() == TEXTITEM) {
659 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *> (i);
660 value_w->setValue((int) t->boundingRect().width());
661 value_h->setValue((int) t->boundingRect().height());
664 if (blockW) value_w->blockSignals(false);
665 if (blockH) value_h->blockSignals(false);
668 /** \brief Updates the coordinates in the text fields from the item */
669 void TitleWidget::updateCoordinates(QGraphicsItem *i)
672 bool blockX = !value_x->signalsBlocked();
673 bool blockY = !value_y->signalsBlocked();
675 // Block signals emitted by this method
676 if (blockX) value_x->blockSignals(true);
677 if (blockY) 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 if (!blockX) value_x->blockSignals(false);
735 if (!blockY) 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) {
789 if (origin_x_left->isChecked()) {
790 // Use the sceneBoundingRect because this also regards transformations like zoom
791 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
793 posX = value_x->value();
797 if (origin_y_top->isChecked()) {
798 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
800 posY = value_y->value();
803 i->setPos(posX, posY);
809 void TitleWidget::updateTextOriginX()
811 if (origin_x_left->isChecked()) {
812 origin_x_left->setText(i18n("\u2212X"));
814 origin_x_left->setText(i18n("+X"));
818 void TitleWidget::slotOriginXClicked()
820 // Update the text displayed on the button.
823 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
825 updateCoordinates(l.at(0));
827 // Remember x axis setting
828 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()?
829 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
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);
857 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
859 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
860 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
861 origin_x_left->blockSignals(true);
862 origin_y_top->blockSignals(true);
864 if (xAxis == TitleDocument::AxisInverted) {
865 origin_x_left->setChecked(true);
867 origin_x_left->setChecked(false);
871 if (yAxis == TitleDocument::AxisInverted) {
872 origin_y_top->setChecked(true);
874 origin_y_top->setChecked(false);
878 origin_x_left->blockSignals(false);
879 origin_y_top->blockSignals(false);
882 void TitleWidget::slotChangeBackground()
884 QColor color = kcolorbutton->color();
885 color.setAlpha(horizontalSlider->value());
886 m_frameBorder->setBrush(QBrush(color));
890 * Something (yeah) has changed in our QGraphicsScene.
892 void TitleWidget::slotChanged() {
893 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
894 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
895 textChanged(static_cast <QGraphicsTextItem *> (l.at(0)));
900 * If the user has set origin_x_left (everything also for y),
901 * we need to look whether a text element has been selected. If yes,
902 * we need to ensure that the right border of the text field
903 * remains fixed also when some text has been entered.
905 * This is also known as right-justified, with the difference that
906 * it is not valid for text but for its boundingRect. Text may still
909 void TitleWidget::textChanged(QGraphicsTextItem *i) {
913 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
915 if (!i->toPlainText().isEmpty()) {
919 * Don't do anything if the string is empty. If the position
920 * would be updated here, a newly created text field would
921 * be set to the position of the last selected text field.
927 void TitleWidget::slotInsertUnicode()
929 m_unicodeDialog->exec();
932 void TitleWidget::slotInsertUnicodeString(QString text)
934 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
936 if (l.at(0)->type() == TEXTITEM) {
937 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *> (l.at(0));
938 t->textCursor().insertText(text);
943 void TitleWidget::slotUpdateText()
945 QFont font = font_family->currentFont();
946 font.setPixelSize(font_size->value());
947 font.setBold(buttonBold->isChecked());
948 font.setItalic(buttonItalic->isChecked());
949 font.setUnderline(buttonUnder->isChecked());
950 QColor color = fontColorButton->color();
951 color.setAlpha(textAlpha->value());
953 QGraphicsTextItem* item = NULL;
954 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
955 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
956 item = static_cast <QGraphicsTextItem *>(l.at(0));
959 //if (item->textCursor().selection ().isEmpty())
960 QTextCursor cur = item->textCursor();
961 QTextBlockFormat format = cur.blockFormat();
962 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
963 item->setTextWidth(item->boundingRect().width());
964 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
965 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
966 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
968 format.setAlignment(Qt::AlignLeft);
969 item->setTextWidth(-1);
974 item->setDefaultTextColor(color);
975 cur.select(QTextCursor::Document);
976 cur.setBlockFormat(format);
977 item->setTextCursor(cur);
978 cur.clearSelection();
979 item->setTextCursor(cur);
984 void TitleWidget::rectChanged()
986 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
987 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
988 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
989 QColor f = rectFColor->color();
990 f.setAlpha(rectFAlpha->value());
992 penf.setWidth(rectLineWidth->value());
994 QColor b = rectBColor->color();
995 b.setAlpha(rectBAlpha->value());
996 rec->setBrush(QBrush(b));
1000 void TitleWidget::itemScaled(int val)
1002 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1003 if (l.size() == 1) {
1004 Transform x = m_transformations.value(l.at(0));
1005 x.scalex = (double)val / 100.0;
1006 x.scaley = (double)val / 100.0;
1008 qtrans.scale(x.scalex, x.scaley);
1009 qtrans.rotate(x.rotate);
1010 l[0]->setTransform(qtrans);
1011 m_transformations[l.at(0)] = x;
1012 updateDimension(l.at(0));
1016 void TitleWidget::itemRotate(int val)
1018 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1019 if (l.size() == 1) {
1020 Transform x = m_transformations[l.at(0)];
1021 x.rotate = (double)val;
1023 qtrans.scale(x.scalex, x.scaley);
1024 qtrans.rotate(x.rotate);
1025 l[0]->setTransform(qtrans);
1026 m_transformations[l.at(0)] = x;
1027 updateDimension(l.at(0));
1031 void TitleWidget::itemHCenter()
1033 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1034 if (l.size() == 1) {
1035 QGraphicsItem *item = l.at(0);
1037 if (item->type() == RECTITEM) {
1038 br = ((QGraphicsRectItem*)item)->rect();
1039 } else br = item->sceneBoundingRect();
1040 int width = (int) br.width();
1041 int newPos = (int)((m_frameWidth - width) / 2);
1042 item->setPos(newPos, item->pos().y());
1043 updateCoordinates(item);
1047 void TitleWidget::itemVCenter()
1049 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1050 if (l.size() == 1) {
1051 QGraphicsItem *item = l.at(0);
1053 if (item->type() == RECTITEM) {
1054 br = ((QGraphicsRectItem*)item)->rect();
1055 } else br = item->sceneBoundingRect();
1056 int height = (int) br.height();
1057 int newPos = (int)((m_frameHeight - height) / 2);
1058 item->setPos(item->pos().x(), newPos);
1059 updateCoordinates(item);
1063 void TitleWidget::setupViewports()
1065 double aspect_ratio = 4.0 / 3.0;//read from project
1067 QRectF sp(0, 0, 0, 0);
1068 QRectF ep(0, 0, 0, 0);
1070 double sv_size = startViewportSize->value();
1071 double ev_size = endViewportSize->value();
1072 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
1073 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
1075 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
1076 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
1078 m_startViewport->setPolygon(QPolygonF(sp));
1079 m_endViewport->setPolygon(QPolygonF(ep));
1083 void TitleWidget::loadTitle()
1085 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
1086 if (!url.isEmpty()) {
1087 QList<QGraphicsItem *> items = m_scene->items();
1088 for (int i = 0; i < items.size(); i++) {
1089 if (items.at(i)->zValue() > -1000) delete items.at(i);
1091 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
1096 void TitleWidget::saveTitle(KUrl url)
1098 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
1099 if (!url.isEmpty()) {
1100 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
1101 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1105 QDomDocument TitleWidget::xml()
1107 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1108 if (cropImage->isChecked()) {
1109 doc.documentElement().setAttribute("crop", 1);
1114 void TitleWidget::setXml(QDomDocument doc)
1116 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
1117 QDomElement e = doc.documentElement();
1118 cropImage->setChecked(e.hasAttribute("crop"));
1119 m_transformations.clear();
1120 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1121 const double PI = 4.0 * atan(1.0);
1122 for (int i = 0; i < items.count(); i++) {
1123 QTransform t = items.at(i)->transform();
1127 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1128 m_transformations[items.at(i)] = x;
1130 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1131 QColor background_color = m_titledocument.getBackgroundColor();
1132 horizontalSlider->blockSignals(true);
1133 kcolorbutton->blockSignals(true);
1134 horizontalSlider->setValue(background_color.alpha());
1135 background_color.setAlpha(255);
1136 kcolorbutton->setColor(background_color);
1137 horizontalSlider->blockSignals(false);
1138 kcolorbutton->blockSignals(false);
1143 QImage TitleWidget::renderedPixmap()
1147 int maxX = m_frameWidth;
1148 int maxY = m_frameHeight;
1149 if (!cropImage->isChecked()) {
1150 m_scene->removeItem(m_startViewport);
1151 m_scene->removeItem(m_endViewport);
1152 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1153 if (boundingRect.left() < 0) minX = boundingRect.left();
1154 if (boundingRect.top() < 0) minY = boundingRect.top();
1155 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1156 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1165 QImage pix(maxX, maxY, QImage::Format_ARGB32);
1166 pix.fill(Qt::transparent);
1167 QPainter painter(&pix);
1168 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1169 m_scene->clearTextSelection();
1170 QPen framepen = m_frameBorder->pen();
1171 m_frameBorder->setPen(Qt::NoPen);
1172 m_startViewport->setVisible(false);
1173 m_endViewport->setVisible(false);
1174 m_frameImage->setVisible(false);
1176 m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
1178 m_frameBorder->setPen(framepen);
1179 m_startViewport->setVisible(true);
1180 m_endViewport->setVisible(true);
1181 m_frameImage->setVisible(true);
1185 /** \brief Connected to the accepted signal - calls writeChoices */
1186 void TitleWidget::slotAccepted()
1191 /** \brief Store the current choices of font, background and rect values */
1192 void TitleWidget::writeChoices()
1194 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1195 KSharedConfigPtr config = KGlobal::config();
1196 KConfigGroup titleConfig(config, "TitleWidget");
1197 // Write the entries
1198 titleConfig.writeEntry("font_family", font_family->currentFont());
1199 //titleConfig.writeEntry("font_size", font_size->value());
1200 titleConfig.writeEntry("font_pixel_size", font_size->value());
1201 titleConfig.writeEntry("font_color", fontColorButton->color());
1202 titleConfig.writeEntry("font_alpha", textAlpha->value());
1203 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1204 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1205 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1207 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1208 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1209 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1210 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1211 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1213 titleConfig.writeEntry("background_color", kcolorbutton->color());
1214 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1216 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1217 //! \todo Not sure if I should sync - it is probably safe to do it
1222 /** \brief Read the last stored choices into the dialog */
1223 void TitleWidget::readChoices()
1225 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1226 KSharedConfigPtr config = KGlobal::config();
1227 KConfigGroup titleConfig(config, "TitleWidget");
1229 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1230 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1231 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1232 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1233 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1234 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1235 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1237 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1238 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1239 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1240 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1241 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1243 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1244 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1246 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));