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"
24 #include <KGlobalSettings>
25 #include <KFileDialog>
26 #include <KStandardDirs>
27 #include <KMessageBox>
28 #include <kio/netaccess.h>
29 #include <kdeversion.h>
31 #include <QDomDocument>
32 #include <QGraphicsItem>
33 #include <QGraphicsSvgItem>
37 #include <QSignalMapper>
38 #include <QTextBlockFormat>
39 #include <QTextCursor>
41 #if QT_VERSION >= 0x040600
42 #include <QGraphicsEffect>
43 #include <QGraphicsBlurEffect>
44 #include <QGraphicsDropShadowEffect>
47 int settingUp = false;
49 const int IMAGEITEM = 7;
50 const int RECTITEM = 3;
51 const int TEXTITEM = 8;
53 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
56 m_startViewport(NULL),
60 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
61 m_projectTitlePath(projectTitlePath),
65 setFont(KGlobalSettings::toolBarFont());
66 frame_properties->setEnabled(false);
67 rect_properties->setFixedHeight(frame_properties->height() + 4);
68 no_properties->setFixedHeight(frame_properties->height() + 4);
69 image_properties->setFixedHeight(frame_properties->height() + 4);
70 text_properties->setFixedHeight(frame_properties->height() + 4);
71 frame_properties->setFixedHeight(frame_toolbar->height());
73 itemzoom->setSuffix(i18n("%"));
74 m_frameWidth = render->renderWidth();
75 m_frameHeight = render->renderHeight();
76 showToolbars(TITLE_NONE);
78 //TODO: get default title duration instead of hardcoded one
79 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
81 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
82 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
84 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
85 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
86 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
87 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
88 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
90 connect(font_family, SIGNAL(editTextChanged(const QString &)), this, SLOT(slotFontText(const QString&)));
92 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
93 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
94 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
95 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
96 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
98 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
99 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
100 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
101 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
102 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
103 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
105 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
106 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
107 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
108 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
109 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
110 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
111 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
112 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
113 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
114 connect(effect_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotAddEffect(int)));
115 connect(typewriter_delay, SIGNAL(valueChanged(int)), this, SLOT(slotEditTypewriter(int)));
116 connect(blur_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditBlur(int)));
117 connect(shadow_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
118 connect(shadow_x, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
119 connect(shadow_y, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
120 effect_stack->setHidden(true);
121 effect_list->setEnabled(false);
123 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
124 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
126 m_signalMapper = new QSignalMapper(this);
127 m_signalMapper->setMapping(value_w, ValueWidth);
128 m_signalMapper->setMapping(value_h, ValueHeight);
129 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
130 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
131 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
133 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
134 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
135 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
136 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
137 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
138 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
139 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
140 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
141 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
142 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
143 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
144 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
145 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
146 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
148 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
151 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
153 font_weight_box->blockSignals(true);
154 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
155 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
156 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
157 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
158 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
159 font_weight_box->setToolTip(i18n("Font weight"));
160 font_weight_box->setCurrentIndex(1);
161 font_weight_box->blockSignals(false);
163 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
164 buttonRealSize->setIcon(KIcon("zoom-original"));
165 buttonItalic->setIcon(KIcon("format-text-italic"));
166 buttonUnder->setIcon(KIcon("format-text-underline"));
167 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
168 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
169 buttonAlignRight->setIcon(KIcon("format-justify-right"));
170 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
172 buttonAlignNone->setToolTip(i18n("No alignment"));
173 buttonAlignRight->setToolTip(i18n("Align right"));
174 buttonAlignLeft->setToolTip(i18n("Align left"));
175 buttonAlignCenter->setToolTip(i18n("Align center"));
177 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
178 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
179 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
180 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
181 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
183 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
184 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
185 rectBColor->setToolTip(i18n("Select fill color"));
186 rectFColor->setToolTip(i18n("Select border color"));
187 rectBAlpha->setToolTip(i18n("Fill transparency"));
188 rectFAlpha->setToolTip(i18n("Border transparency"));
189 zoom_slider->setToolTip(i18n("Zoom"));
190 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
191 buttonFitZoom->setToolTip(i18n("Fit zoom"));
192 kcolorbutton->setToolTip(i18n("Select background color"));
193 horizontalSlider->setToolTip(i18n("Background Transparency"));
195 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
196 itemhcenter->setToolTip(i18n("Align item horizontally"));
197 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
198 itemvcenter->setToolTip(i18n("Align item vertically"));
199 itemtop->setIcon(KIcon("kdenlive-align-top"));
200 itemtop->setToolTip(i18n("Align item to top"));
201 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
202 itembottom->setToolTip(i18n("Align item to bottom"));
203 itemright->setIcon(KIcon("kdenlive-align-right"));
204 itemright->setToolTip(i18n("Align item to right"));
205 itemleft->setIcon(KIcon("kdenlive-align-left"));
206 itemleft->setToolTip(i18n("Align item to left"));
209 QHBoxLayout *layout = new QHBoxLayout;
210 frame_toolbar->setLayout(layout);
211 layout->setContentsMargins(2, 2, 2, 2);
212 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
214 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
215 m_buttonCursor->setCheckable(true);
216 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
217 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
218 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
220 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
221 m_buttonText->setCheckable(true);
222 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
223 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
224 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
226 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
227 m_buttonRect->setCheckable(true);
228 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
229 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
230 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
232 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
233 m_buttonImage->setCheckable(false);
234 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
235 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
236 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
238 m_toolbar->addSeparator();
240 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
241 m_buttonLoad->setCheckable(false);
242 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
243 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
245 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
246 m_buttonSave->setCheckable(false);
247 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
248 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
250 layout->addWidget(m_toolbar);
252 // initialize graphic scene
253 m_scene = new GraphicsSceneRectMove(this);
254 graphicsView->setScene(m_scene);
255 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
256 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
258 // a gradient background
259 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
260 gradient->setSpread(QGradient::ReflectSpread);
261 scene->setBackgroundBrush(*gradient);*/
263 m_frameImage = new QGraphicsPixmapItem();
265 qtrans.scale(2.0, 2.0);
266 m_frameImage->setTransform(qtrans);
267 m_frameImage->setZValue(-1200);
268 m_frameImage->setFlags(0);
269 displayBackgroundFrame();
270 graphicsView->scene()->addItem(m_frameImage);
272 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
273 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
274 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
275 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
276 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
277 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
278 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
279 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
281 QPen framepen(Qt::DotLine);
282 framepen.setColor(Qt::red);
284 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
285 m_frameBorder->setPen(framepen);
286 m_frameBorder->setZValue(-1100);
287 m_frameBorder->setBrush(Qt::transparent);
288 m_frameBorder->setFlags(0);
289 graphicsView->scene()->addItem(m_frameBorder);
291 // mbd: load saved settings
294 graphicsView->show();
295 //graphicsView->setRenderHint(QPainter::Antialiasing);
296 graphicsView->setInteractive(true);
297 //graphicsView->resize(400, 300);
298 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
299 //toolBox->setItemEnabled(2, false);
300 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
301 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
302 m_startViewport->setData(0, m_frameWidth);
303 m_startViewport->setData(1, m_frameHeight);
304 m_endViewport->setData(0, m_frameWidth);
305 m_endViewport->setData(1, m_frameHeight);
307 if (!url.isEmpty()) loadTitle(url);
310 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
313 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
314 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
316 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(KdenliveSettings::hastitleproducer());
319 TitleWidget::~TitleWidget()
323 delete m_buttonImage;
324 delete m_buttonCursor;
327 delete m_unicodeAction;
329 delete m_unicodeDialog;
330 delete m_frameBorder;
332 delete m_startViewport;
333 delete m_endViewport;
335 delete m_signalMapper;
339 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
342 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
343 KStandardDirs::makeDir(titlePath);
344 titlePath.append((isClone == false) ? "title" : "clone");
347 while (path.isEmpty() || QFile::exists(path)) {
349 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
351 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
357 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
360 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
361 KStandardDirs::makeDir(titlePath);
362 return titlePath + titleName + ".png";
366 QStringList TitleWidget::extractImageList(QString xml)
369 if (xml.isEmpty()) return result;
372 QDomNodeList images = doc.elementsByTagName("content");
373 for (int i = 0; i < images.count(); i++) {
374 if (images.at(i).toElement().hasAttribute("url"))
375 result.append(images.at(i).toElement().attribute("url"));
382 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
387 void TitleWidget::slotTextTool()
389 m_scene->setTool(TITLE_TEXT);
390 showToolbars(TITLE_TEXT);
391 checkButton(TITLE_TEXT);
394 void TitleWidget::slotRectTool()
396 m_scene->setTool(TITLE_RECTANGLE);
397 showToolbars(TITLE_RECTANGLE);
398 checkButton(TITLE_RECTANGLE);
401 void TitleWidget::slotSelectTool()
403 m_scene->setTool(TITLE_SELECT);
405 // Find out which toolbars need to be shown, depending on selected item
406 TITLETOOL t = TITLE_SELECT;
407 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
409 switch (l.at(0)->type()) {
423 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
424 //graphicsView->centerOn(l.at(0));
430 updateCoordinates(l.at(0));
431 updateDimension(l.at(0));
432 updateRotZoom(l.at(0));
435 checkButton(TITLE_SELECT);
438 void TitleWidget::slotImageTool()
440 // TODO: find a way to get a list of all supported image types...
441 QString allExtensions = "image/gif image/jpeg image/png image/x-tga image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr";
442 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
443 if (!url.isEmpty()) {
444 if (url.path().endsWith(".svg")) {
445 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
446 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
447 svg->setZValue(m_count++);
448 svg->setData(Qt::UserRole, url.path());
449 graphicsView->scene()->addItem(svg);
451 QPixmap pix(url.path());
452 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
453 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
454 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
455 image->setData(Qt::UserRole, url.path());
456 image->setZValue(m_count++);
457 graphicsView->scene()->addItem(image);
460 m_scene->setTool(TITLE_SELECT);
461 showToolbars(TITLE_SELECT);
462 checkButton(TITLE_NONE);
465 void TitleWidget::showToolbars(TITLETOOL toolType)
469 rect_properties->setHidden(true);
470 image_properties->setHidden(true);
471 no_properties->setHidden(true);
472 text_properties->setHidden(false);
474 case TITLE_RECTANGLE:
475 image_properties->setHidden(true);
476 no_properties->setHidden(true);
477 text_properties->setHidden(true);
478 rect_properties->setHidden(false);
481 no_properties->setHidden(true);
482 text_properties->setHidden(true);
483 rect_properties->setHidden(true);
484 image_properties->setHidden(false);
487 text_properties->setHidden(true);
488 rect_properties->setHidden(true);
489 image_properties->setHidden(true);
490 no_properties->setHidden(false);
495 void TitleWidget::enableToolbars(TITLETOOL toolType)
497 // TITLETOOL is defined in graphicsscenerectmove.h
502 bool bValue_w = false;
503 bool bValue_h = false;
512 case TITLE_RECTANGLE:
527 frame_properties->setEnabled(bFrame);
528 text_properties->setEnabled(bText);
529 rect_properties->setEnabled(bRect);
530 image_properties->setEnabled(bImage);
531 value_w->setEnabled(bValue_w);
532 value_h->setEnabled(bValue_h);
535 void TitleWidget::checkButton(TITLETOOL toolType)
537 bool bSelect = false;
549 case TITLE_RECTANGLE:
559 m_buttonCursor->setChecked(bSelect);
560 m_buttonText->setChecked(bText);
561 m_buttonRect->setChecked(bRect);
562 m_buttonImage->setChecked(bImage);
565 void TitleWidget::displayBackgroundFrame()
567 if (!displayBg->isChecked()) {
568 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
569 QPixmap pattern(20, 20);
571 QColor bgcolor(210, 210, 210);
574 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
575 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
580 p.fillRect(bg.rect(), br);
582 m_frameImage->setPixmap(bg);
584 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
588 void TitleWidget::initAnimation()
590 align_box->setEnabled(false);
591 QPen startpen(Qt::DotLine);
592 QPen endpen(Qt::DashDotLine);
593 startpen.setColor(QColor(100, 200, 100, 140));
594 endpen.setColor(QColor(200, 100, 100, 140));
596 m_startViewport->setPen(startpen);
597 m_endViewport->setPen(endpen);
599 m_startViewport->setZValue(-1000);
600 m_endViewport->setZValue(-1000);
602 m_startViewport->setFlags(0);
603 m_endViewport->setFlags(0);
605 graphicsView->scene()->addItem(m_startViewport);
606 graphicsView->scene()->addItem(m_endViewport);
608 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
609 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
610 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
611 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
614 void TitleWidget::slotUpdateZoom(int pos)
616 m_scene->setZoom((double) pos / 100);
617 zoom_label->setText(QString::number(pos) + '%');
620 void TitleWidget::slotZoom(bool up)
622 int pos = zoom_slider->value();
625 zoom_slider->setValue(pos);
628 void TitleWidget::slotAdjustZoom()
630 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
631 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
632 if (scalex > scaley) scalex = scaley;
633 int zoompos = (int)(scalex * 7 + 0.5);*/
634 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
635 int zoompos = graphicsView->matrix().m11() * 100;
636 zoom_slider->setValue(zoompos);
637 graphicsView->centerOn(m_frameBorder);
640 void TitleWidget::slotZoomOneToOne()
642 zoom_slider->setValue(100);
643 graphicsView->centerOn(m_frameBorder);
646 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
648 updateAxisButtons(rect); // back to default
650 QColor f = rectFColor->color();
651 f.setAlpha(rectFAlpha->value());
653 penf.setWidth(rectLineWidth->value());
655 QColor b = rectBColor->color();
656 b.setAlpha(rectBAlpha->value());
657 rect->setBrush(QBrush(b));
658 rect->setZValue(m_count++);
659 //setCurrentItem(rect);
660 //graphicsView->setFocus();
663 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
665 updateAxisButtons(tt); // back to default
667 QFont font = font_family->currentFont();
668 font.setPixelSize(font_size->value());
670 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
671 font.setItalic(buttonItalic->isChecked());
672 font.setUnderline(buttonUnder->isChecked());
675 QColor color = fontColorButton->color();
676 color.setAlpha(textAlpha->value());
677 tt->setDefaultTextColor(color);
678 tt->setZValue(m_count++);
682 void TitleWidget::setFontBoxWeight(int weight)
684 int index = font_weight_box->findData(weight);
686 index = font_weight_box->findData(QFont::Normal);
688 font_weight_box->setCurrentIndex(index);
691 void TitleWidget::setCurrentItem(QGraphicsItem *item)
693 m_scene->setSelectedItem(item);
696 void TitleWidget::zIndexChanged(int v)
698 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
704 void TitleWidget::selectionChanged()
706 if (m_scene->tool() != TITLE_SELECT) return;
707 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
708 //toolBox->setItemEnabled(2, false);
709 //toolBox->setItemEnabled(3, false);
710 value_x->blockSignals(true);
711 value_y->blockSignals(true);
712 value_w->blockSignals(true);
713 value_h->blockSignals(true);
714 itemzoom->blockSignals(true);
715 itemrotate->blockSignals(true);
717 effect_stack->setHidden(true);
718 effect_list->setEnabled(false);
719 effect_list->setCurrentIndex(0);
720 bool blockX = !origin_x_left->signalsBlocked();
721 bool blockY = !origin_y_top->signalsBlocked();
722 if (blockX) origin_x_left->blockSignals(true);
723 if (blockY) origin_y_top->blockSignals(true);
724 origin_x_left->setChecked(false);
725 origin_y_top->setChecked(false);
728 enableToolbars(TITLE_NONE);
729 if (blockX) origin_x_left->blockSignals(false);
730 if (blockY) origin_y_top->blockSignals(false);
731 itemzoom->setEnabled(false);
732 itemrotate->setEnabled(false);
733 } else if (l.size() == 1) {
734 effect_list->setEnabled(true);
735 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
736 itemzoom->setEnabled(true);
737 itemrotate->setEnabled(true);
739 itemzoom->setEnabled(false);
740 itemrotate->setEnabled(false);
743 if (l.at(0)->type() == TEXTITEM) {
744 showToolbars(TITLE_TEXT);
745 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
746 if (!i->data(100).isNull()) {
747 // Item has an effect
748 QStringList effdata = i->data(100).toStringList();
749 if (effdata.at(0) == "typewriter") {
750 typewriter_delay->setValue(effdata.at(1).toInt());
751 effect_list->setCurrentIndex(3);
752 effect_stack->setHidden(false);
755 else effect_stack->setHidden(true);
756 //if (l[0]->hasFocus())
757 //toolBox->setCurrentIndex(0);
758 //toolBox->setItemEnabled(2, true);
759 font_size->blockSignals(true);
760 font_family->blockSignals(true);
761 font_weight_box->blockSignals(true);
762 buttonItalic->blockSignals(true);
763 buttonUnder->blockSignals(true);
764 fontColorButton->blockSignals(true);
765 textAlpha->blockSignals(true);
766 buttonAlignLeft->blockSignals(true);
767 buttonAlignRight->blockSignals(true);
768 buttonAlignNone->blockSignals(true);
769 buttonAlignCenter->blockSignals(true);
771 QFont font = i->font();
772 font_family->setCurrentFont(font);
773 font_size->setValue(font.pixelSize());
774 buttonItalic->setChecked(font.italic());
775 buttonUnder->setChecked(font.underline());
776 setFontBoxWeight(font.weight());
778 QColor color = i->defaultTextColor();
779 fontColorButton->setColor(color);
780 textAlpha->setValue(color.alpha());
782 QTextCursor cur = i->textCursor();
783 QTextBlockFormat format = cur.blockFormat();
784 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
785 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
786 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
787 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
789 font_size->blockSignals(false);
790 font_family->blockSignals(false);
791 font_weight_box->blockSignals(false);
792 buttonItalic->blockSignals(false);
793 buttonUnder->blockSignals(false);
794 fontColorButton->blockSignals(false);
795 textAlpha->blockSignals(false);
796 buttonAlignLeft->blockSignals(false);
797 buttonAlignRight->blockSignals(false);
798 buttonAlignNone->blockSignals(false);
799 buttonAlignCenter->blockSignals(false);
801 updateAxisButtons(i);
802 updateCoordinates(i);
804 enableToolbars(TITLE_TEXT);
806 } else if ((l.at(0))->type() == RECTITEM) {
807 showToolbars(TITLE_RECTANGLE);
809 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
810 if (rec == m_startViewport || rec == m_endViewport) {
811 /*toolBox->setCurrentIndex(3);
812 toolBox->widget(0)->setEnabled(false);
813 toolBox->widget(1)->setEnabled(false);*/
814 enableToolbars(TITLE_NONE);
816 /*toolBox->widget(0)->setEnabled(true);
817 toolBox->widget(1)->setEnabled(true);
818 toolBox->setCurrentIndex(0);*/
819 //toolBox->setItemEnabled(3, true);
820 rectFAlpha->setValue(rec->pen().color().alpha());
821 rectBAlpha->setValue(rec->brush().color().alpha());
822 //kDebug() << rec->brush().color().alpha();
823 QColor fcol = rec->pen().color();
824 QColor bcol = rec->brush().color();
825 //fcol.setAlpha(255);
826 //bcol.setAlpha(255);
827 rectFColor->setColor(fcol);
828 rectBColor->setColor(bcol);
830 rectLineWidth->setValue(rec->pen().width());
831 enableToolbars(TITLE_RECTANGLE);
834 updateAxisButtons(l.at(0));
835 updateCoordinates(rec);
836 updateDimension(rec);
838 } else if (l.at(0)->type() == IMAGEITEM) {
839 showToolbars(TITLE_IMAGE);
841 updateCoordinates(l.at(0));
842 updateDimension(l.at(0));
844 enableToolbars(TITLE_IMAGE);
847 //toolBox->setCurrentIndex(0);
848 showToolbars(TITLE_NONE);
849 enableToolbars(TITLE_NONE);
850 /*frame_properties->setEnabled(false);
851 text_properties->setEnabled(false);
852 rect_properties->setEnabled(false);*/
854 zValue->setValue((int)l.at(0)->zValue());
855 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
856 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
857 value_x->blockSignals(false);
858 value_y->blockSignals(false);
859 value_w->blockSignals(false);
860 value_h->blockSignals(false);
861 itemzoom->blockSignals(false);
862 itemrotate->blockSignals(false);
866 void TitleWidget::slotValueChanged(int type)
868 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
869 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
874 val = value_w->value();
877 val = value_h->value();
881 QGraphicsItem *i = l.at(0);
882 Transform t = m_transformations.value(i);
884 // Ratio width:height
885 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
886 double alpha = (double) t.rotate / 180.0 * M_PI;
896 // Add 0.5 because otherwise incrementing by 1 might have no effect
897 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
898 scale = length / i->boundingRect().width();
901 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
902 scale = length / i->boundingRect().height();
909 qtrans.scale(scale, scale);
910 qtrans.rotate(t.rotate);
911 i->setTransform(qtrans);
912 m_transformations[i] = t;
919 /** \brief Updates position/size of the selected item when a value
920 * of an item (coordinates, size) has changed */
921 void TitleWidget::slotAdjustSelectedItem()
923 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
925 if (l.at(0)->type() == RECTITEM) {
927 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
929 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
930 } else if (l.at(0)->type() == TEXTITEM) {
932 updatePosition(l.at(0));
933 } else if (l.at(0)->type() == IMAGEITEM) {
935 updatePosition(l.at(0));
940 /** \brief Updates width/height int the text fields, regarding transformation matrix */
941 void TitleWidget::updateDimension(QGraphicsItem *i)
943 value_w->blockSignals(true);
944 value_h->blockSignals(true);
947 if (i->type() == IMAGEITEM) {
948 // Get multipliers for rotation/scaling
950 /*Transform t = m_transformations.value(i);
951 QRectF r = i->boundingRect();
952 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
953 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
954 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
955 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
957 value_w->setValue(i->sceneBoundingRect().width());
958 value_h->setValue(i->sceneBoundingRect().height());
959 } else if (i->type() == RECTITEM) {
960 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
961 value_w->setValue((int) r->rect().width());
962 value_h->setValue((int) r->rect().height());
963 } else if (i->type() == TEXTITEM) {
964 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
965 value_w->setValue((int) t->boundingRect().width());
966 value_h->setValue((int) t->boundingRect().height());
969 value_w->blockSignals(false);
970 value_h->blockSignals(false);
973 /** \brief Updates the coordinates in the text fields from the item */
974 void TitleWidget::updateCoordinates(QGraphicsItem *i)
976 // Block signals emitted by this method
977 value_x->blockSignals(true);
978 value_y->blockSignals(true);
980 if (i->type() == TEXTITEM) {
982 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
984 // Set the correct x coordinate value
985 if (origin_x_left->isChecked()) {
986 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
987 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
989 // Origin is at 0 (default)
990 value_x->setValue((int) rec->pos().x());
994 if (origin_y_top->isChecked()) {
995 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
997 value_y->setValue((int) rec->pos().y());
1000 } else if (i->type() == RECTITEM) {
1002 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1004 if (origin_x_left->isChecked()) {
1005 // Origin (0 point) is at m_frameWidth
1006 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
1008 // Origin is at 0 (default)
1009 value_x->setValue((int) rec->pos().x());
1012 if (origin_y_top->isChecked()) {
1013 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
1015 value_y->setValue((int) rec->pos().y());
1018 } else if (i->type() == IMAGEITEM) {
1020 if (origin_x_left->isChecked()) {
1021 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
1023 value_x->setValue((int) i->pos().x());
1026 if (origin_y_top->isChecked()) {
1027 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
1029 value_y->setValue((int) i->pos().y());
1034 // Stop blocking signals now
1035 value_x->blockSignals(false);
1036 value_y->blockSignals(false);
1039 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1041 itemzoom->blockSignals(true);
1042 itemrotate->blockSignals(false);
1044 Transform t = m_transformations.value(i);
1045 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1046 itemrotate->setValue((int)(t.rotate));
1048 itemzoom->blockSignals(false);
1049 itemrotate->blockSignals(false);
1052 /** \brief Updates the position of an item by reading coordinates from the text fields */
1053 void TitleWidget::updatePosition(QGraphicsItem *i)
1055 if (i->type() == TEXTITEM) {
1056 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1059 if (origin_x_left->isChecked()) {
1060 /* Origin of the x axis is at m_frameWidth,
1061 * and distance from right border of the item to the right
1062 * border of the frame is taken.
1063 * See comment to slotOriginXClicked().
1065 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1067 posX = value_x->value();
1071 if (origin_y_top->isChecked()) {
1072 /* Same for y axis */
1073 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1075 posY = value_y->value();
1078 rec->setPos(posX, posY);
1080 } else if (i->type() == RECTITEM) {
1082 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1085 if (origin_x_left->isChecked()) {
1086 posX = m_frameWidth - value_x->value() - rec->rect().width();
1088 posX = value_x->value();
1092 if (origin_y_top->isChecked()) {
1093 posY = m_frameHeight - value_y->value() - rec->rect().height();
1095 posY = value_y->value();
1098 rec->setPos(posX, posY);
1100 } else if (i->type() == IMAGEITEM) {
1102 if (origin_x_left->isChecked()) {
1103 // Use the sceneBoundingRect because this also regards transformations like zoom
1104 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1106 posX = value_x->value();
1110 if (origin_y_top->isChecked()) {
1111 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1113 posY = value_y->value();
1116 i->setPos(posX, posY);
1122 void TitleWidget::updateTextOriginX()
1124 if (origin_x_left->isChecked()) {
1125 origin_x_left->setText(i18n("\u2212X"));
1127 origin_x_left->setText(i18n("+X"));
1131 void TitleWidget::slotOriginXClicked()
1133 // Update the text displayed on the button.
1134 updateTextOriginX();
1136 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1137 if (l.size() >= 1) {
1138 updateCoordinates(l.at(0));
1140 // Remember x axis setting
1141 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1142 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1144 graphicsView->setFocus();
1147 void TitleWidget::updateTextOriginY()
1149 if (origin_y_top->isChecked()) {
1150 origin_y_top->setText(i18n("\u2212Y"));
1152 origin_y_top->setText(i18n("+Y"));
1156 void TitleWidget::slotOriginYClicked()
1158 // Update the text displayed on the button.
1159 updateTextOriginY();
1161 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1162 if (l.size() >= 1) {
1163 updateCoordinates(l.at(0));
1165 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1166 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1169 graphicsView->setFocus();
1172 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1174 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1175 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1176 origin_x_left->blockSignals(true);
1177 origin_y_top->blockSignals(true);
1179 if (xAxis == TitleDocument::AxisInverted) {
1180 origin_x_left->setChecked(true);
1182 origin_x_left->setChecked(false);
1184 updateTextOriginX();
1186 if (yAxis == TitleDocument::AxisInverted) {
1187 origin_y_top->setChecked(true);
1189 origin_y_top->setChecked(false);
1191 updateTextOriginY();
1193 origin_x_left->blockSignals(false);
1194 origin_y_top->blockSignals(false);
1197 void TitleWidget::slotChangeBackground()
1199 QColor color = kcolorbutton->color();
1200 color.setAlpha(horizontalSlider->value());
1201 m_frameBorder->setBrush(QBrush(color));
1205 * Something (yeah) has changed in our QGraphicsScene.
1207 void TitleWidget::slotChanged()
1209 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1210 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1211 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1216 * If the user has set origin_x_left (everything also for y),
1217 * we need to look whether a text element has been selected. If yes,
1218 * we need to ensure that the right border of the text field
1219 * remains fixed also when some text has been entered.
1221 * This is also known as right-justified, with the difference that
1222 * it is not valid for text but for its boundingRect. Text may still
1223 * be left-justified.
1225 void TitleWidget::textChanged(QGraphicsTextItem *i)
1230 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1232 if (!i->toPlainText().isEmpty()) {
1236 * Don't do anything if the string is empty. If the position
1237 * would be updated here, a newly created text field would
1238 * be set to the position of the last selected text field.
1244 void TitleWidget::slotInsertUnicode()
1246 m_unicodeDialog->exec();
1249 void TitleWidget::slotInsertUnicodeString(QString text)
1251 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1253 if (l.at(0)->type() == TEXTITEM) {
1254 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1255 t->textCursor().insertText(text);
1260 void TitleWidget::slotUpdateText()
1262 QFont font = font_family->currentFont();
1263 font.setPixelSize(font_size->value());
1264 font.setItalic(buttonItalic->isChecked());
1265 font.setUnderline(buttonUnder->isChecked());
1266 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1267 QColor color = fontColorButton->color();
1268 color.setAlpha(textAlpha->value());
1270 QGraphicsTextItem* item = NULL;
1271 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1272 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1273 item = static_cast <QGraphicsTextItem *>(l.at(0));
1276 //if (item->textCursor().selection ().isEmpty())
1277 QTextCursor cur = item->textCursor();
1278 QTextBlockFormat format = cur.blockFormat();
1279 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1280 item->setTextWidth(item->boundingRect().width());
1281 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1282 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1283 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1285 format.setAlignment(Qt::AlignLeft);
1286 item->setTextWidth(-1);
1290 item->setFont(font);
1291 item->setDefaultTextColor(color);
1292 cur.select(QTextCursor::Document);
1293 cur.setBlockFormat(format);
1294 item->setTextCursor(cur);
1295 cur.clearSelection();
1296 item->setTextCursor(cur);
1301 void TitleWidget::rectChanged()
1303 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1304 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1305 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1306 QColor f = rectFColor->color();
1307 f.setAlpha(rectFAlpha->value());
1309 penf.setWidth(rectLineWidth->value());
1311 QColor b = rectBColor->color();
1312 b.setAlpha(rectBAlpha->value());
1313 rec->setBrush(QBrush(b));
1317 void TitleWidget::itemScaled(int val)
1319 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1320 if (l.size() == 1) {
1321 Transform x = m_transformations.value(l.at(0));
1322 x.scalex = (double)val / 100.0;
1323 x.scaley = (double)val / 100.0;
1325 qtrans.scale(x.scalex, x.scaley);
1326 qtrans.rotate(x.rotate);
1327 l[0]->setTransform(qtrans);
1328 m_transformations[l.at(0)] = x;
1329 updateDimension(l.at(0));
1333 void TitleWidget::itemRotate(int val)
1335 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1336 if (l.size() == 1) {
1337 Transform x = m_transformations[l.at(0)];
1338 x.rotate = (double)val;
1340 qtrans.scale(x.scalex, x.scaley);
1341 qtrans.rotate(x.rotate);
1342 l[0]->setTransform(qtrans);
1343 m_transformations[l.at(0)] = x;
1344 updateDimension(l.at(0));
1348 void TitleWidget::itemHCenter()
1350 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1351 if (l.size() == 1) {
1352 QGraphicsItem *item = l.at(0);
1353 QRectF br = item->sceneBoundingRect();
1354 int width = (int)br.width();
1355 int newPos = (int)((m_frameWidth - width) / 2);
1356 newPos += item->pos().x() - br.left(); // Check item transformation
1357 item->setPos(newPos, item->pos().y());
1358 updateCoordinates(item);
1362 void TitleWidget::itemVCenter()
1364 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1365 if (l.size() == 1) {
1366 QGraphicsItem *item = l.at(0);
1367 QRectF br = item->sceneBoundingRect();
1368 int height = (int)br.height();
1369 int newPos = (int)((m_frameHeight - height) / 2);
1370 newPos += item->pos().y() - br.top(); // Check item transformation
1371 item->setPos(item->pos().x(), newPos);
1372 updateCoordinates(item);
1376 void TitleWidget::itemTop()
1378 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1379 if (l.size() == 1) {
1380 QGraphicsItem *item = l.at(0);
1381 QRectF br = item->sceneBoundingRect();
1383 if (br.top() > 0) diff = -br.top();
1384 else diff = -br.bottom();
1385 item->moveBy(0, diff);
1386 updateCoordinates(item);
1390 void TitleWidget::itemBottom()
1392 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1393 if (l.size() == 1) {
1394 QGraphicsItem *item = l.at(0);
1395 QRectF br = item->sceneBoundingRect();
1397 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1398 else diff = m_frameHeight - br.bottom();
1399 item->moveBy(0, diff);
1400 updateCoordinates(item);
1404 void TitleWidget::itemLeft()
1406 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1407 if (l.size() == 1) {
1408 QGraphicsItem *item = l.at(0);
1409 QRectF br = item->sceneBoundingRect();
1411 if (br.left() > 0) diff = -br.left();
1412 else diff = -br.right();
1413 item->moveBy(diff, 0);
1414 updateCoordinates(item);
1418 void TitleWidget::itemRight()
1420 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1421 if (l.size() == 1) {
1422 QGraphicsItem *item = l.at(0);
1423 QRectF br = item->sceneBoundingRect();
1425 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1426 else diff = m_frameWidth - br.left();
1427 item->moveBy(diff, 0);
1428 updateCoordinates(item);
1432 void TitleWidget::setupViewports()
1434 //double aspect_ratio = 4.0 / 3.0;//read from project
1435 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1436 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1437 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1438 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1439 QPolygonF spoly(sp);
1440 QPolygonF epoly(ep);
1441 spoly.translate(startViewportX->value(), startViewportY->value());
1442 epoly.translate(endViewportX->value(), endViewportY->value());
1443 m_startViewport->setPolygon(spoly);
1444 m_endViewport->setPolygon(epoly);
1445 if (! insertingValues) {
1446 m_startViewport->setData(0, startViewportX->value());
1447 m_startViewport->setData(1, startViewportY->value());
1448 m_startViewport->setData(2, startViewportSize->value());
1450 m_endViewport->setData(0, endViewportX->value());
1451 m_endViewport->setData(1, endViewportY->value());
1452 m_endViewport->setData(2, endViewportSize->value());
1456 void TitleWidget::loadTitle(KUrl url)
1458 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1459 if (!url.isEmpty()) {
1460 QList<QGraphicsItem *> items = m_scene->items();
1461 for (int i = 0; i < items.size(); i++) {
1462 if (items.at(i)->zValue() > -1000) delete items.at(i);
1464 m_scene->clearTextSelection();
1468 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1469 QFile file(tmpfile);
1470 if (file.open(QIODevice::ReadOnly)) {
1471 doc.setContent(&file, false);
1474 KIO::NetAccess::removeTempFile(tmpfile);
1479 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1481 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1482 insertingValues = true;
1483 startViewportX->setValue(m_startViewport->data(0).toInt());
1484 startViewportY->setValue(m_startViewport->data(1).toInt());
1485 startViewportSize->setValue(m_startViewport->data(2).toInt());
1486 endViewportX->setValue(m_endViewport->data(0).toInt());
1487 endViewportY->setValue(m_endViewport->data(1).toInt());
1488 endViewportSize->setValue(m_endViewport->data(2).toInt());
1490 insertingValues = false;
1496 void TitleWidget::saveTitle(KUrl url)
1498 if (anim_start->isChecked()) slotAnimStart(false);
1499 if (anim_end->isChecked()) slotAnimEnd(false);
1500 if (url.isEmpty()) {
1501 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1502 fs->setOperationMode(KFileDialog::Saving);
1503 fs->setMode(KFile::File);
1504 #if KDE_IS_VERSION(4,2,0)
1505 fs->setConfirmOverwrite(true);
1507 fs->setKeepLocation(true);
1509 url = fs->selectedUrl();
1512 if (!url.isEmpty()) {
1513 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1514 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1518 QDomDocument TitleWidget::xml()
1520 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1521 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1525 int TitleWidget::duration() const
1527 return m_tc.getFrameCount(title_duration->text());
1530 void TitleWidget::setXml(QDomDocument doc)
1533 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1535 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1536 /*if (doc.documentElement().hasAttribute("out")) {
1537 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1538 title_duration->setText(m_tc.getTimecode(duration));
1540 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1542 QDomElement e = doc.documentElement();
1543 m_transformations.clear();
1544 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1545 const double PI = 4.0 * atan(1.0);
1546 for (int i = 0; i < items.count(); i++) {
1547 QTransform t = items.at(i)->transform();
1551 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1552 m_transformations[items.at(i)] = x;
1554 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1555 QColor background_color = m_titledocument.getBackgroundColor();
1556 horizontalSlider->blockSignals(true);
1557 kcolorbutton->blockSignals(true);
1558 horizontalSlider->setValue(background_color.alpha());
1559 background_color.setAlpha(255);
1560 kcolorbutton->setColor(background_color);
1561 horizontalSlider->blockSignals(false);
1562 kcolorbutton->blockSignals(false);
1564 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1565 startViewportY->setValue(m_startViewport->data(1).toInt());
1566 startViewportSize->setValue(m_startViewport->data(2).toInt());
1567 endViewportX->setValue(m_endViewport->data(0).toInt());
1568 endViewportY->setValue(m_endViewport->data(1).toInt());
1569 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1571 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1575 /** \brief Connected to the accepted signal - calls writeChoices */
1576 void TitleWidget::slotAccepted()
1578 if (anim_start->isChecked()) slotAnimStart(false);
1579 if (anim_end->isChecked()) slotAnimEnd(false);
1583 /** \brief Store the current choices of font, background and rect values */
1584 void TitleWidget::writeChoices()
1586 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1587 KSharedConfigPtr config = KGlobal::config();
1588 KConfigGroup titleConfig(config, "TitleWidget");
1589 // Write the entries
1590 titleConfig.writeEntry("font_family", font_family->currentFont());
1591 //titleConfig.writeEntry("font_size", font_size->value());
1592 titleConfig.writeEntry("font_pixel_size", font_size->value());
1593 titleConfig.writeEntry("font_color", fontColorButton->color());
1594 titleConfig.writeEntry("font_alpha", textAlpha->value());
1595 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1596 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1597 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1599 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1600 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1601 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1602 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1603 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1605 titleConfig.writeEntry("background_color", kcolorbutton->color());
1606 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1608 //! \todo Not sure if I should sync - it is probably safe to do it
1613 /** \brief Read the last stored choices into the dialog */
1614 void TitleWidget::readChoices()
1616 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1617 KSharedConfigPtr config = KGlobal::config();
1618 KConfigGroup titleConfig(config, "TitleWidget");
1620 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1621 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1622 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1623 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1625 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1626 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1627 setFontBoxWeight(weight);
1628 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1629 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1631 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1632 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1633 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1634 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1635 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1637 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1638 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1641 void TitleWidget::adjustFrameSize()
1643 m_frameWidth = m_titledocument.frameWidth();
1644 m_frameHeight = m_titledocument.frameHeight();
1645 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1646 displayBackgroundFrame();
1649 void TitleWidget::slotAnimStart(bool anim)
1651 if (anim && anim_end->isChecked()) {
1652 anim_end->setChecked(false);
1653 m_endViewport->setZValue(-1000);
1654 m_endViewport->setBrush(QBrush());
1657 QList<QGraphicsItem *> list = m_scene->items();
1658 for (int i = 0; i < list.count(); i++) {
1659 if (list.at(i)->zValue() > -1000) {
1660 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1661 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1664 align_box->setEnabled(anim);
1665 itemzoom->setEnabled(!anim);
1666 itemrotate->setEnabled(!anim);
1667 frame_toolbar->setEnabled(!anim);
1668 rect_properties->setEnabled(!anim);
1670 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1671 m_startViewport->setZValue(1100);
1672 QColor col = m_startViewport->pen().color();
1674 m_startViewport->setBrush(col);
1675 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1676 m_startViewport->setSelected(true);
1679 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1681 m_startViewport->setZValue(-1000);
1682 m_startViewport->setBrush(QBrush());
1683 m_startViewport->setFlags(0);
1684 if (!anim_end->isChecked()) deleteAnimInfoText();
1689 void TitleWidget::slotAnimEnd(bool anim)
1691 if (anim && anim_start->isChecked()) {
1692 anim_start->setChecked(false);
1693 m_startViewport->setZValue(-1000);
1694 m_startViewport->setBrush(QBrush());
1697 QList<QGraphicsItem *> list = m_scene->items();
1698 for (int i = 0; i < list.count(); i++) {
1699 if (list.at(i)->zValue() > -1000) {
1700 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1701 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1704 align_box->setEnabled(anim);
1705 itemzoom->setEnabled(!anim);
1706 itemrotate->setEnabled(!anim);
1707 frame_toolbar->setEnabled(!anim);
1708 rect_properties->setEnabled(!anim);
1710 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1711 m_endViewport->setZValue(1100);
1712 QColor col = m_endViewport->pen().color();
1714 m_endViewport->setBrush(col);
1715 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1716 m_endViewport->setSelected(true);
1719 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1721 m_endViewport->setZValue(-1000);
1722 m_endViewport->setBrush(QBrush());
1723 m_endViewport->setFlags(0);
1724 if (!anim_start->isChecked()) deleteAnimInfoText();
1728 void TitleWidget::addAnimInfoText()
1730 // add text to anim viewport
1731 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1732 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1733 QFont font = t->font();
1734 font.setPixelSize(m_startViewport->rect().width() / 10);
1735 QColor col = m_startViewport->pen().color();
1737 t->setDefaultTextColor(col);
1739 font.setPixelSize(m_endViewport->rect().width() / 10);
1740 col = m_endViewport->pen().color();
1742 t2->setDefaultTextColor(col);
1746 void TitleWidget::updateInfoText()
1748 // update info text font
1749 if (!m_startViewport->childItems().isEmpty()) {
1750 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1752 QFont font = item->font();
1753 font.setPixelSize(m_startViewport->rect().width() / 10);
1754 item->setFont(font);
1757 if (!m_endViewport->childItems().isEmpty()) {
1758 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1760 QFont font = item->font();
1761 font.setPixelSize(m_endViewport->rect().width() / 10);
1762 item->setFont(font);
1767 void TitleWidget::deleteAnimInfoText()
1769 // end animation editing, remove info text
1770 while (!m_startViewport->childItems().isEmpty()) {
1771 QGraphicsItem *item = m_startViewport->childItems().at(0);
1772 m_scene->removeItem(item);
1775 while (!m_endViewport->childItems().isEmpty()) {
1776 QGraphicsItem *item = m_endViewport->childItems().at(0);
1777 m_scene->removeItem(item);
1783 void TitleWidget::slotKeepAspect(bool keep)
1785 if (m_endViewport->zValue() == 1100) {
1786 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1787 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1789 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1790 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1794 void TitleWidget::slotResize50()
1796 if (m_endViewport->zValue() == 1100) {
1797 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1798 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1801 void TitleWidget::slotResize100()
1803 if (m_endViewport->zValue() == 1100) {
1804 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1805 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1808 void TitleWidget::slotResize200()
1810 if (m_endViewport->zValue() == 1100) {
1811 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1812 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1815 void TitleWidget::slotAddEffect(int ix)
1818 effect_stack->setHidden(true);
1821 effect_stack->setCurrentIndex(ix -1);
1822 effect_stack->setHidden(false);
1823 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1825 if (l.size() == 1) {
1826 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value());
1827 l[0]->setData(100, effdata);
1830 #if QT_VERSION < 0x040600
1835 if (l.size() == 1) {
1836 QGraphicsEffect *eff = new QGraphicsBlurEffect();
1837 l[0]->setGraphicsEffect(eff);
1839 } else if (ix == 2) {
1840 if (l.size() == 1) {
1841 QGraphicsEffect *eff = new QGraphicsDropShadowEffect();
1842 l[0]->setGraphicsEffect(eff);
1850 void TitleWidget::slotFontText(const QString& s)
1853 if (f.exactMatch()) {
1854 // Font really exists (could also just be a «d» if the user
1855 // starts typing «dejavu» for example).
1856 font_family->setCurrentFont(f);
1858 // TODO: typing dejavu serif does not recognize the font (takes sans).
1859 // upper/lowercase problem?
1862 void TitleWidget::slotEditTypewriter(int ix)
1864 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1865 if (l.size() == 1) {
1866 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value());
1867 l[0]->setData(100, effdata);
1871 void TitleWidget::slotEditBlur(int ix)
1873 #if QT_VERSION < 0x040600
1876 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1877 if (l.size() == 1) {
1878 QGraphicsEffect *eff = l[0]->graphicsEffect();
1879 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1880 if (blur) blur->setBlurRadius(ix);
1885 void TitleWidget::slotEditShadow()
1887 #if QT_VERSION < 0x040600
1890 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1891 if (l.size() == 1) {
1892 QGraphicsEffect *eff = l[0]->graphicsEffect();
1893 QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1895 shadow->setBlurRadius(shadow_radius->value());
1896 shadow->setOffset(shadow_x->value(), shadow_y->value());