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(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
91 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
92 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
93 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
94 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
96 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
97 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
98 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
99 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
100 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
101 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
103 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
104 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
105 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
106 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
107 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
108 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
109 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
110 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
111 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
112 connect(effect_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotAddEffect(int)));
113 connect(blur_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditBlur(int)));
114 connect(shadow_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
115 connect(shadow_x, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
116 connect(shadow_y, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
117 blur_frame->setHidden(true);
118 shadow_frame->setHidden(true);
120 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
121 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
123 m_signalMapper = new QSignalMapper(this);
124 m_signalMapper->setMapping(value_w, ValueWidth);
125 m_signalMapper->setMapping(value_h, ValueHeight);
126 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
127 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
128 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
130 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
131 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
132 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
133 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
134 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
135 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
136 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
137 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
138 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
139 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
140 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
141 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
142 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
143 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
145 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
148 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
150 font_weight_box->blockSignals(true);
151 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
152 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
153 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
154 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
155 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
156 font_weight_box->setToolTip(i18n("Font weight"));
157 font_weight_box->setCurrentIndex(1);
158 font_weight_box->blockSignals(false);
160 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
161 buttonRealSize->setIcon(KIcon("zoom-original"));
162 buttonItalic->setIcon(KIcon("format-text-italic"));
163 buttonUnder->setIcon(KIcon("format-text-underline"));
164 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
165 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
166 buttonAlignRight->setIcon(KIcon("format-justify-right"));
167 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
169 buttonAlignNone->setToolTip(i18n("No alignment"));
170 buttonAlignRight->setToolTip(i18n("Align right"));
171 buttonAlignLeft->setToolTip(i18n("Align left"));
172 buttonAlignCenter->setToolTip(i18n("Align center"));
174 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
175 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
176 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
177 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
178 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
180 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
181 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
182 rectBColor->setToolTip(i18n("Select fill color"));
183 rectFColor->setToolTip(i18n("Select border color"));
184 rectBAlpha->setToolTip(i18n("Fill transparency"));
185 rectFAlpha->setToolTip(i18n("Border transparency"));
186 zoom_slider->setToolTip(i18n("Zoom"));
187 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
188 buttonFitZoom->setToolTip(i18n("Fit zoom"));
189 kcolorbutton->setToolTip(i18n("Select background color"));
190 horizontalSlider->setToolTip(i18n("Background Transparency"));
192 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
193 itemhcenter->setToolTip(i18n("Align item horizontally"));
194 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
195 itemvcenter->setToolTip(i18n("Align item vertically"));
196 itemtop->setIcon(KIcon("kdenlive-align-top"));
197 itemtop->setToolTip(i18n("Align item to top"));
198 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
199 itembottom->setToolTip(i18n("Align item to bottom"));
200 itemright->setIcon(KIcon("kdenlive-align-right"));
201 itemright->setToolTip(i18n("Align item to right"));
202 itemleft->setIcon(KIcon("kdenlive-align-left"));
203 itemleft->setToolTip(i18n("Align item to left"));
206 QHBoxLayout *layout = new QHBoxLayout;
207 frame_toolbar->setLayout(layout);
208 layout->setContentsMargins(2, 2, 2, 2);
209 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
211 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
212 m_buttonCursor->setCheckable(true);
213 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
214 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
215 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
217 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
218 m_buttonText->setCheckable(true);
219 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
220 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
221 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
223 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
224 m_buttonRect->setCheckable(true);
225 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
226 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
227 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
229 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
230 m_buttonImage->setCheckable(false);
231 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
232 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
233 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
235 m_toolbar->addSeparator();
237 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
238 m_buttonLoad->setCheckable(false);
239 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
240 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
242 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
243 m_buttonSave->setCheckable(false);
244 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
245 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
247 layout->addWidget(m_toolbar);
249 // initialize graphic scene
250 m_scene = new GraphicsSceneRectMove(this);
251 graphicsView->setScene(m_scene);
252 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
253 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
255 // a gradient background
256 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
257 gradient->setSpread(QGradient::ReflectSpread);
258 scene->setBackgroundBrush(*gradient);*/
260 m_frameImage = new QGraphicsPixmapItem();
262 qtrans.scale(2.0, 2.0);
263 m_frameImage->setTransform(qtrans);
264 m_frameImage->setZValue(-1200);
265 m_frameImage->setFlags(0);
266 displayBackgroundFrame();
267 graphicsView->scene()->addItem(m_frameImage);
269 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
270 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
271 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
272 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
273 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
274 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
275 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
276 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
278 QPen framepen(Qt::DotLine);
279 framepen.setColor(Qt::red);
281 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
282 m_frameBorder->setPen(framepen);
283 m_frameBorder->setZValue(-1100);
284 m_frameBorder->setBrush(Qt::transparent);
285 m_frameBorder->setFlags(0);
286 graphicsView->scene()->addItem(m_frameBorder);
288 // mbd: load saved settings
291 graphicsView->show();
292 //graphicsView->setRenderHint(QPainter::Antialiasing);
293 graphicsView->setInteractive(true);
294 //graphicsView->resize(400, 300);
295 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
296 //toolBox->setItemEnabled(2, false);
297 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
298 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
299 m_startViewport->setData(0, m_frameWidth);
300 m_startViewport->setData(1, m_frameHeight);
301 m_endViewport->setData(0, m_frameWidth);
302 m_endViewport->setData(1, m_frameHeight);
304 if (!url.isEmpty()) loadTitle(url);
307 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
310 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
311 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
313 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(KdenliveSettings::hastitleproducer());
316 TitleWidget::~TitleWidget()
320 delete m_buttonImage;
321 delete m_buttonCursor;
324 delete m_unicodeAction;
326 delete m_unicodeDialog;
327 delete m_frameBorder;
329 delete m_startViewport;
330 delete m_endViewport;
332 delete m_signalMapper;
336 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
339 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
340 KStandardDirs::makeDir(titlePath);
341 titlePath.append((isClone == false) ? "title" : "clone");
344 while (path.isEmpty() || QFile::exists(path)) {
346 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
348 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
354 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
357 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
358 KStandardDirs::makeDir(titlePath);
359 return titlePath + titleName + ".png";
363 QStringList TitleWidget::extractImageList(QString xml)
366 if (xml.isEmpty()) return result;
369 QDomNodeList images = doc.elementsByTagName("content");
370 for (int i = 0; i < images.count(); i++) {
371 if (images.at(i).toElement().hasAttribute("url"))
372 result.append(images.at(i).toElement().attribute("url"));
379 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
384 void TitleWidget::slotTextTool()
386 m_scene->setTool(TITLE_TEXT);
387 showToolbars(TITLE_TEXT);
388 checkButton(TITLE_TEXT);
391 void TitleWidget::slotRectTool()
393 m_scene->setTool(TITLE_RECTANGLE);
394 showToolbars(TITLE_RECTANGLE);
395 checkButton(TITLE_RECTANGLE);
398 void TitleWidget::slotSelectTool()
400 m_scene->setTool(TITLE_SELECT);
402 // Find out which toolbars need to be shown, depending on selected item
403 TITLETOOL t = TITLE_SELECT;
404 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
406 switch (l.at(0)->type()) {
420 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
421 //graphicsView->centerOn(l.at(0));
427 updateCoordinates(l.at(0));
428 updateDimension(l.at(0));
429 updateRotZoom(l.at(0));
432 checkButton(TITLE_SELECT);
435 void TitleWidget::slotImageTool()
437 // TODO: find a way to get a list of all supported image types...
438 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";
439 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
440 if (!url.isEmpty()) {
441 if (url.path().endsWith(".svg")) {
442 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
443 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
444 svg->setZValue(m_count++);
445 svg->setData(Qt::UserRole, url.path());
446 graphicsView->scene()->addItem(svg);
448 QPixmap pix(url.path());
449 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
450 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
451 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
452 image->setData(Qt::UserRole, url.path());
453 image->setZValue(m_count++);
454 graphicsView->scene()->addItem(image);
457 m_scene->setTool(TITLE_SELECT);
458 showToolbars(TITLE_SELECT);
459 checkButton(TITLE_NONE);
462 void TitleWidget::showToolbars(TITLETOOL toolType)
466 rect_properties->setHidden(true);
467 image_properties->setHidden(true);
468 no_properties->setHidden(true);
469 text_properties->setHidden(false);
471 case TITLE_RECTANGLE:
472 image_properties->setHidden(true);
473 no_properties->setHidden(true);
474 text_properties->setHidden(true);
475 rect_properties->setHidden(false);
478 no_properties->setHidden(true);
479 text_properties->setHidden(true);
480 rect_properties->setHidden(true);
481 image_properties->setHidden(false);
484 text_properties->setHidden(true);
485 rect_properties->setHidden(true);
486 image_properties->setHidden(true);
487 no_properties->setHidden(false);
492 void TitleWidget::enableToolbars(TITLETOOL toolType)
494 // TITLETOOL is defined in graphicsscenerectmove.h
499 bool bValue_w = false;
500 bool bValue_h = false;
509 case TITLE_RECTANGLE:
524 frame_properties->setEnabled(bFrame);
525 text_properties->setEnabled(bText);
526 rect_properties->setEnabled(bRect);
527 image_properties->setEnabled(bImage);
528 value_w->setEnabled(bValue_w);
529 value_h->setEnabled(bValue_h);
532 void TitleWidget::checkButton(TITLETOOL toolType)
534 bool bSelect = false;
546 case TITLE_RECTANGLE:
556 m_buttonCursor->setChecked(bSelect);
557 m_buttonText->setChecked(bText);
558 m_buttonRect->setChecked(bRect);
559 m_buttonImage->setChecked(bImage);
562 void TitleWidget::displayBackgroundFrame()
564 if (!displayBg->isChecked()) {
565 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
566 QPixmap pattern(20, 20);
568 QColor bgcolor(210, 210, 210);
571 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
572 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
577 p.fillRect(bg.rect(), br);
579 m_frameImage->setPixmap(bg);
581 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
585 void TitleWidget::initAnimation()
587 align_box->setEnabled(false);
588 QPen startpen(Qt::DotLine);
589 QPen endpen(Qt::DashDotLine);
590 startpen.setColor(QColor(100, 200, 100, 140));
591 endpen.setColor(QColor(200, 100, 100, 140));
593 m_startViewport->setPen(startpen);
594 m_endViewport->setPen(endpen);
596 m_startViewport->setZValue(-1000);
597 m_endViewport->setZValue(-1000);
599 m_startViewport->setFlags(0);
600 m_endViewport->setFlags(0);
602 graphicsView->scene()->addItem(m_startViewport);
603 graphicsView->scene()->addItem(m_endViewport);
605 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
606 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
607 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
608 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
611 void TitleWidget::slotUpdateZoom(int pos)
613 m_scene->setZoom((double) pos / 100);
614 zoom_label->setText(QString::number(pos) + '%');
617 void TitleWidget::slotZoom(bool up)
619 int pos = zoom_slider->value();
622 zoom_slider->setValue(pos);
625 void TitleWidget::slotAdjustZoom()
627 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
628 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
629 if (scalex > scaley) scalex = scaley;
630 int zoompos = (int)(scalex * 7 + 0.5);*/
631 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
632 int zoompos = graphicsView->matrix().m11() * 100;
633 zoom_slider->setValue(zoompos);
634 graphicsView->centerOn(m_frameBorder);
637 void TitleWidget::slotZoomOneToOne()
639 zoom_slider->setValue(100);
640 graphicsView->centerOn(m_frameBorder);
643 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
645 updateAxisButtons(rect); // back to default
647 QColor f = rectFColor->color();
648 f.setAlpha(rectFAlpha->value());
650 penf.setWidth(rectLineWidth->value());
652 QColor b = rectBColor->color();
653 b.setAlpha(rectBAlpha->value());
654 rect->setBrush(QBrush(b));
655 rect->setZValue(m_count++);
656 //setCurrentItem(rect);
657 //graphicsView->setFocus();
660 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
662 updateAxisButtons(tt); // back to default
664 QFont font = font_family->currentFont();
665 font.setPixelSize(font_size->value());
667 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
668 font.setItalic(buttonItalic->isChecked());
669 font.setUnderline(buttonUnder->isChecked());
672 QColor color = fontColorButton->color();
673 color.setAlpha(textAlpha->value());
674 tt->setDefaultTextColor(color);
675 tt->setZValue(m_count++);
679 void TitleWidget::setFontBoxWeight(int weight)
681 int index = font_weight_box->findData(weight);
683 index = font_weight_box->findData(QFont::Normal);
685 font_weight_box->setCurrentIndex(index);
688 void TitleWidget::setCurrentItem(QGraphicsItem *item)
690 m_scene->setSelectedItem(item);
693 void TitleWidget::zIndexChanged(int v)
695 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
701 void TitleWidget::selectionChanged()
703 if (m_scene->tool() != TITLE_SELECT) return;
704 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
705 //toolBox->setItemEnabled(2, false);
706 //toolBox->setItemEnabled(3, false);
707 value_x->blockSignals(true);
708 value_y->blockSignals(true);
709 value_w->blockSignals(true);
710 value_h->blockSignals(true);
711 itemzoom->blockSignals(true);
712 itemrotate->blockSignals(true);
714 bool blockX = !origin_x_left->signalsBlocked();
715 bool blockY = !origin_y_top->signalsBlocked();
716 if (blockX) origin_x_left->blockSignals(true);
717 if (blockY) origin_y_top->blockSignals(true);
718 origin_x_left->setChecked(false);
719 origin_y_top->setChecked(false);
722 enableToolbars(TITLE_NONE);
723 if (blockX) origin_x_left->blockSignals(false);
724 if (blockY) origin_y_top->blockSignals(false);
725 itemzoom->setEnabled(false);
726 itemrotate->setEnabled(false);
727 } else if (l.size() == 1) {
728 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
729 itemzoom->setEnabled(true);
730 itemrotate->setEnabled(true);
732 itemzoom->setEnabled(false);
733 itemrotate->setEnabled(false);
736 if (l.at(0)->type() == TEXTITEM) {
737 showToolbars(TITLE_TEXT);
738 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
739 //if (l[0]->hasFocus())
740 //toolBox->setCurrentIndex(0);
741 //toolBox->setItemEnabled(2, true);
742 font_size->blockSignals(true);
743 font_family->blockSignals(true);
744 font_weight_box->blockSignals(true);
745 buttonItalic->blockSignals(true);
746 buttonUnder->blockSignals(true);
747 fontColorButton->blockSignals(true);
748 textAlpha->blockSignals(true);
749 buttonAlignLeft->blockSignals(true);
750 buttonAlignRight->blockSignals(true);
751 buttonAlignNone->blockSignals(true);
752 buttonAlignCenter->blockSignals(true);
754 QFont font = i->font();
755 font_family->setCurrentFont(font);
756 font_size->setValue(font.pixelSize());
757 buttonItalic->setChecked(font.italic());
758 buttonUnder->setChecked(font.underline());
759 setFontBoxWeight(font.weight());
761 QColor color = i->defaultTextColor();
762 fontColorButton->setColor(color);
763 textAlpha->setValue(color.alpha());
765 QTextCursor cur = i->textCursor();
766 QTextBlockFormat format = cur.blockFormat();
767 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
768 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
769 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
770 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
772 font_size->blockSignals(false);
773 font_family->blockSignals(false);
774 font_weight_box->blockSignals(false);
775 buttonItalic->blockSignals(false);
776 buttonUnder->blockSignals(false);
777 fontColorButton->blockSignals(false);
778 textAlpha->blockSignals(false);
779 buttonAlignLeft->blockSignals(false);
780 buttonAlignRight->blockSignals(false);
781 buttonAlignNone->blockSignals(false);
782 buttonAlignCenter->blockSignals(false);
784 updateAxisButtons(i);
785 updateCoordinates(i);
787 enableToolbars(TITLE_TEXT);
789 } else if ((l.at(0))->type() == RECTITEM) {
790 showToolbars(TITLE_RECTANGLE);
792 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
793 if (rec == m_startViewport || rec == m_endViewport) {
794 /*toolBox->setCurrentIndex(3);
795 toolBox->widget(0)->setEnabled(false);
796 toolBox->widget(1)->setEnabled(false);*/
797 enableToolbars(TITLE_NONE);
799 /*toolBox->widget(0)->setEnabled(true);
800 toolBox->widget(1)->setEnabled(true);
801 toolBox->setCurrentIndex(0);*/
802 //toolBox->setItemEnabled(3, true);
803 rectFAlpha->setValue(rec->pen().color().alpha());
804 rectBAlpha->setValue(rec->brush().color().alpha());
805 //kDebug() << rec->brush().color().alpha();
806 QColor fcol = rec->pen().color();
807 QColor bcol = rec->brush().color();
808 //fcol.setAlpha(255);
809 //bcol.setAlpha(255);
810 rectFColor->setColor(fcol);
811 rectBColor->setColor(bcol);
813 rectLineWidth->setValue(rec->pen().width());
814 enableToolbars(TITLE_RECTANGLE);
817 updateAxisButtons(l.at(0));
818 updateCoordinates(rec);
819 updateDimension(rec);
821 } else if (l.at(0)->type() == IMAGEITEM) {
822 showToolbars(TITLE_IMAGE);
824 updateCoordinates(l.at(0));
825 updateDimension(l.at(0));
827 enableToolbars(TITLE_IMAGE);
830 //toolBox->setCurrentIndex(0);
831 showToolbars(TITLE_NONE);
832 enableToolbars(TITLE_NONE);
833 /*frame_properties->setEnabled(false);
834 text_properties->setEnabled(false);
835 rect_properties->setEnabled(false);*/
837 zValue->setValue((int)l.at(0)->zValue());
838 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
839 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
840 value_x->blockSignals(false);
841 value_y->blockSignals(false);
842 value_w->blockSignals(false);
843 value_h->blockSignals(false);
844 itemzoom->blockSignals(false);
845 itemrotate->blockSignals(false);
849 void TitleWidget::slotValueChanged(int type)
851 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
852 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
857 val = value_w->value();
860 val = value_h->value();
864 QGraphicsItem *i = l.at(0);
865 Transform t = m_transformations.value(i);
867 // Ratio width:height
868 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
869 double alpha = (double) t.rotate / 180.0 * M_PI;
879 // Add 0.5 because otherwise incrementing by 1 might have no effect
880 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
881 scale = length / i->boundingRect().width();
884 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
885 scale = length / i->boundingRect().height();
892 qtrans.scale(scale, scale);
893 qtrans.rotate(t.rotate);
894 i->setTransform(qtrans);
895 m_transformations[i] = t;
902 /** \brief Updates position/size of the selected item when a value
903 * of an item (coordinates, size) has changed */
904 void TitleWidget::slotAdjustSelectedItem()
906 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
908 if (l.at(0)->type() == RECTITEM) {
910 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
912 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
913 } else if (l.at(0)->type() == TEXTITEM) {
915 updatePosition(l.at(0));
916 } else if (l.at(0)->type() == IMAGEITEM) {
918 updatePosition(l.at(0));
923 /** \brief Updates width/height int the text fields, regarding transformation matrix */
924 void TitleWidget::updateDimension(QGraphicsItem *i)
926 value_w->blockSignals(true);
927 value_h->blockSignals(true);
930 if (i->type() == IMAGEITEM) {
931 // Get multipliers for rotation/scaling
933 /*Transform t = m_transformations.value(i);
934 QRectF r = i->boundingRect();
935 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
936 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
937 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
938 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
940 value_w->setValue(i->sceneBoundingRect().width());
941 value_h->setValue(i->sceneBoundingRect().height());
942 } else if (i->type() == RECTITEM) {
943 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
944 value_w->setValue((int) r->rect().width());
945 value_h->setValue((int) r->rect().height());
946 } else if (i->type() == TEXTITEM) {
947 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
948 value_w->setValue((int) t->boundingRect().width());
949 value_h->setValue((int) t->boundingRect().height());
952 value_w->blockSignals(false);
953 value_h->blockSignals(false);
956 /** \brief Updates the coordinates in the text fields from the item */
957 void TitleWidget::updateCoordinates(QGraphicsItem *i)
959 // Block signals emitted by this method
960 value_x->blockSignals(true);
961 value_y->blockSignals(true);
963 if (i->type() == TEXTITEM) {
965 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
967 // Set the correct x coordinate value
968 if (origin_x_left->isChecked()) {
969 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
970 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
972 // Origin is at 0 (default)
973 value_x->setValue((int) rec->pos().x());
977 if (origin_y_top->isChecked()) {
978 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
980 value_y->setValue((int) rec->pos().y());
983 } else if (i->type() == RECTITEM) {
985 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
987 if (origin_x_left->isChecked()) {
988 // Origin (0 point) is at m_frameWidth
989 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
991 // Origin is at 0 (default)
992 value_x->setValue((int) rec->pos().x());
995 if (origin_y_top->isChecked()) {
996 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
998 value_y->setValue((int) rec->pos().y());
1001 } else if (i->type() == IMAGEITEM) {
1003 if (origin_x_left->isChecked()) {
1004 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
1006 value_x->setValue((int) i->pos().x());
1009 if (origin_y_top->isChecked()) {
1010 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
1012 value_y->setValue((int) i->pos().y());
1017 // Stop blocking signals now
1018 value_x->blockSignals(false);
1019 value_y->blockSignals(false);
1022 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1024 itemzoom->blockSignals(true);
1025 itemrotate->blockSignals(false);
1027 Transform t = m_transformations.value(i);
1028 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1029 itemrotate->setValue((int)(t.rotate));
1031 itemzoom->blockSignals(false);
1032 itemrotate->blockSignals(false);
1035 /** \brief Updates the position of an item by reading coordinates from the text fields */
1036 void TitleWidget::updatePosition(QGraphicsItem *i)
1038 if (i->type() == TEXTITEM) {
1039 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1042 if (origin_x_left->isChecked()) {
1043 /* Origin of the x axis is at m_frameWidth,
1044 * and distance from right border of the item to the right
1045 * border of the frame is taken.
1046 * See comment to slotOriginXClicked().
1048 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1050 posX = value_x->value();
1054 if (origin_y_top->isChecked()) {
1055 /* Same for y axis */
1056 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1058 posY = value_y->value();
1061 rec->setPos(posX, posY);
1063 } else if (i->type() == RECTITEM) {
1065 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1068 if (origin_x_left->isChecked()) {
1069 posX = m_frameWidth - value_x->value() - rec->rect().width();
1071 posX = value_x->value();
1075 if (origin_y_top->isChecked()) {
1076 posY = m_frameHeight - value_y->value() - rec->rect().height();
1078 posY = value_y->value();
1081 rec->setPos(posX, posY);
1083 } else if (i->type() == IMAGEITEM) {
1085 if (origin_x_left->isChecked()) {
1086 // Use the sceneBoundingRect because this also regards transformations like zoom
1087 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1089 posX = value_x->value();
1093 if (origin_y_top->isChecked()) {
1094 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1096 posY = value_y->value();
1099 i->setPos(posX, posY);
1105 void TitleWidget::updateTextOriginX()
1107 if (origin_x_left->isChecked()) {
1108 origin_x_left->setText(i18n("\u2212X"));
1110 origin_x_left->setText(i18n("+X"));
1114 void TitleWidget::slotOriginXClicked()
1116 // Update the text displayed on the button.
1117 updateTextOriginX();
1119 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1120 if (l.size() >= 1) {
1121 updateCoordinates(l.at(0));
1123 // Remember x axis setting
1124 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1125 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1127 graphicsView->setFocus();
1130 void TitleWidget::updateTextOriginY()
1132 if (origin_y_top->isChecked()) {
1133 origin_y_top->setText(i18n("\u2212Y"));
1135 origin_y_top->setText(i18n("+Y"));
1139 void TitleWidget::slotOriginYClicked()
1141 // Update the text displayed on the button.
1142 updateTextOriginY();
1144 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1145 if (l.size() >= 1) {
1146 updateCoordinates(l.at(0));
1148 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1149 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1152 graphicsView->setFocus();
1155 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1157 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1158 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1159 origin_x_left->blockSignals(true);
1160 origin_y_top->blockSignals(true);
1162 if (xAxis == TitleDocument::AxisInverted) {
1163 origin_x_left->setChecked(true);
1165 origin_x_left->setChecked(false);
1167 updateTextOriginX();
1169 if (yAxis == TitleDocument::AxisInverted) {
1170 origin_y_top->setChecked(true);
1172 origin_y_top->setChecked(false);
1174 updateTextOriginY();
1176 origin_x_left->blockSignals(false);
1177 origin_y_top->blockSignals(false);
1180 void TitleWidget::slotChangeBackground()
1182 QColor color = kcolorbutton->color();
1183 color.setAlpha(horizontalSlider->value());
1184 m_frameBorder->setBrush(QBrush(color));
1188 * Something (yeah) has changed in our QGraphicsScene.
1190 void TitleWidget::slotChanged()
1192 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1193 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1194 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1199 * If the user has set origin_x_left (everything also for y),
1200 * we need to look whether a text element has been selected. If yes,
1201 * we need to ensure that the right border of the text field
1202 * remains fixed also when some text has been entered.
1204 * This is also known as right-justified, with the difference that
1205 * it is not valid for text but for its boundingRect. Text may still
1206 * be left-justified.
1208 void TitleWidget::textChanged(QGraphicsTextItem *i)
1213 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1215 if (!i->toPlainText().isEmpty()) {
1219 * Don't do anything if the string is empty. If the position
1220 * would be updated here, a newly created text field would
1221 * be set to the position of the last selected text field.
1227 void TitleWidget::slotInsertUnicode()
1229 m_unicodeDialog->exec();
1232 void TitleWidget::slotInsertUnicodeString(QString text)
1234 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1236 if (l.at(0)->type() == TEXTITEM) {
1237 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1238 t->textCursor().insertText(text);
1243 void TitleWidget::slotUpdateText()
1245 QFont font = font_family->currentFont();
1246 font.setPixelSize(font_size->value());
1247 font.setItalic(buttonItalic->isChecked());
1248 font.setUnderline(buttonUnder->isChecked());
1249 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1250 QColor color = fontColorButton->color();
1251 color.setAlpha(textAlpha->value());
1253 QGraphicsTextItem* item = NULL;
1254 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1255 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1256 item = static_cast <QGraphicsTextItem *>(l.at(0));
1259 //if (item->textCursor().selection ().isEmpty())
1260 QTextCursor cur = item->textCursor();
1261 QTextBlockFormat format = cur.blockFormat();
1262 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1263 item->setTextWidth(item->boundingRect().width());
1264 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1265 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1266 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1268 format.setAlignment(Qt::AlignLeft);
1269 item->setTextWidth(-1);
1273 item->setFont(font);
1274 item->setDefaultTextColor(color);
1275 cur.select(QTextCursor::Document);
1276 cur.setBlockFormat(format);
1277 item->setTextCursor(cur);
1278 cur.clearSelection();
1279 item->setTextCursor(cur);
1284 void TitleWidget::rectChanged()
1286 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1287 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1288 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1289 QColor f = rectFColor->color();
1290 f.setAlpha(rectFAlpha->value());
1292 penf.setWidth(rectLineWidth->value());
1294 QColor b = rectBColor->color();
1295 b.setAlpha(rectBAlpha->value());
1296 rec->setBrush(QBrush(b));
1300 void TitleWidget::itemScaled(int val)
1302 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1303 if (l.size() == 1) {
1304 Transform x = m_transformations.value(l.at(0));
1305 x.scalex = (double)val / 100.0;
1306 x.scaley = (double)val / 100.0;
1308 qtrans.scale(x.scalex, x.scaley);
1309 qtrans.rotate(x.rotate);
1310 l[0]->setTransform(qtrans);
1311 m_transformations[l.at(0)] = x;
1312 updateDimension(l.at(0));
1316 void TitleWidget::itemRotate(int val)
1318 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1319 if (l.size() == 1) {
1320 Transform x = m_transformations[l.at(0)];
1321 x.rotate = (double)val;
1323 qtrans.scale(x.scalex, x.scaley);
1324 qtrans.rotate(x.rotate);
1325 l[0]->setTransform(qtrans);
1326 m_transformations[l.at(0)] = x;
1327 updateDimension(l.at(0));
1331 void TitleWidget::itemHCenter()
1333 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1334 if (l.size() == 1) {
1335 QGraphicsItem *item = l.at(0);
1336 QRectF br = item->sceneBoundingRect();
1337 int width = (int)br.width();
1338 int newPos = (int)((m_frameWidth - width) / 2);
1339 newPos += item->pos().x() - br.left(); // Check item transformation
1340 item->setPos(newPos, item->pos().y());
1341 updateCoordinates(item);
1345 void TitleWidget::itemVCenter()
1347 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1348 if (l.size() == 1) {
1349 QGraphicsItem *item = l.at(0);
1350 QRectF br = item->sceneBoundingRect();
1351 int height = (int)br.height();
1352 int newPos = (int)((m_frameHeight - height) / 2);
1353 newPos += item->pos().y() - br.top(); // Check item transformation
1354 item->setPos(item->pos().x(), newPos);
1355 updateCoordinates(item);
1359 void TitleWidget::itemTop()
1361 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1362 if (l.size() == 1) {
1363 QGraphicsItem *item = l.at(0);
1364 QRectF br = item->sceneBoundingRect();
1366 if (br.top() > 0) diff = -br.top();
1367 else diff = -br.bottom();
1368 item->moveBy(0, diff);
1369 updateCoordinates(item);
1373 void TitleWidget::itemBottom()
1375 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1376 if (l.size() == 1) {
1377 QGraphicsItem *item = l.at(0);
1378 QRectF br = item->sceneBoundingRect();
1380 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1381 else diff = m_frameHeight - br.bottom();
1382 item->moveBy(0, diff);
1383 updateCoordinates(item);
1387 void TitleWidget::itemLeft()
1389 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1390 if (l.size() == 1) {
1391 QGraphicsItem *item = l.at(0);
1392 QRectF br = item->sceneBoundingRect();
1394 if (br.left() > 0) diff = -br.left();
1395 else diff = -br.right();
1396 item->moveBy(diff, 0);
1397 updateCoordinates(item);
1401 void TitleWidget::itemRight()
1403 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1404 if (l.size() == 1) {
1405 QGraphicsItem *item = l.at(0);
1406 QRectF br = item->sceneBoundingRect();
1408 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1409 else diff = m_frameWidth - br.left();
1410 item->moveBy(diff, 0);
1411 updateCoordinates(item);
1415 void TitleWidget::setupViewports()
1417 //double aspect_ratio = 4.0 / 3.0;//read from project
1418 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1419 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1420 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1421 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1422 QPolygonF spoly(sp);
1423 QPolygonF epoly(ep);
1424 spoly.translate(startViewportX->value(), startViewportY->value());
1425 epoly.translate(endViewportX->value(), endViewportY->value());
1426 m_startViewport->setPolygon(spoly);
1427 m_endViewport->setPolygon(epoly);
1428 if (! insertingValues) {
1429 m_startViewport->setData(0, startViewportX->value());
1430 m_startViewport->setData(1, startViewportY->value());
1431 m_startViewport->setData(2, startViewportSize->value());
1433 m_endViewport->setData(0, endViewportX->value());
1434 m_endViewport->setData(1, endViewportY->value());
1435 m_endViewport->setData(2, endViewportSize->value());
1439 void TitleWidget::loadTitle(KUrl url)
1441 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1442 if (!url.isEmpty()) {
1443 QList<QGraphicsItem *> items = m_scene->items();
1444 for (int i = 0; i < items.size(); i++) {
1445 if (items.at(i)->zValue() > -1000) delete items.at(i);
1447 m_scene->clearTextSelection();
1451 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1452 QFile file(tmpfile);
1453 if (file.open(QIODevice::ReadOnly)) {
1454 doc.setContent(&file, false);
1457 KIO::NetAccess::removeTempFile(tmpfile);
1462 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1464 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1465 insertingValues = true;
1466 startViewportX->setValue(m_startViewport->data(0).toInt());
1467 startViewportY->setValue(m_startViewport->data(1).toInt());
1468 startViewportSize->setValue(m_startViewport->data(2).toInt());
1469 endViewportX->setValue(m_endViewport->data(0).toInt());
1470 endViewportY->setValue(m_endViewport->data(1).toInt());
1471 endViewportSize->setValue(m_endViewport->data(2).toInt());
1473 insertingValues = false;
1479 void TitleWidget::saveTitle(KUrl url)
1481 if (anim_start->isChecked()) slotAnimStart(false);
1482 if (anim_end->isChecked()) slotAnimEnd(false);
1483 if (url.isEmpty()) {
1484 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1485 fs->setOperationMode(KFileDialog::Saving);
1486 fs->setMode(KFile::File);
1487 #if KDE_IS_VERSION(4,2,0)
1488 fs->setConfirmOverwrite(true);
1490 fs->setKeepLocation(true);
1492 url = fs->selectedUrl();
1495 if (!url.isEmpty()) {
1496 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1497 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1501 QDomDocument TitleWidget::xml()
1503 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1504 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1508 int TitleWidget::duration() const
1510 return m_tc.getFrameCount(title_duration->text());
1513 void TitleWidget::setXml(QDomDocument doc)
1516 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1518 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1519 /*if (doc.documentElement().hasAttribute("out")) {
1520 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1521 title_duration->setText(m_tc.getTimecode(duration));
1523 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1525 QDomElement e = doc.documentElement();
1526 m_transformations.clear();
1527 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1528 const double PI = 4.0 * atan(1.0);
1529 for (int i = 0; i < items.count(); i++) {
1530 QTransform t = items.at(i)->transform();
1534 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1535 m_transformations[items.at(i)] = x;
1537 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1538 QColor background_color = m_titledocument.getBackgroundColor();
1539 horizontalSlider->blockSignals(true);
1540 kcolorbutton->blockSignals(true);
1541 horizontalSlider->setValue(background_color.alpha());
1542 background_color.setAlpha(255);
1543 kcolorbutton->setColor(background_color);
1544 horizontalSlider->blockSignals(false);
1545 kcolorbutton->blockSignals(false);
1547 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1548 startViewportY->setValue(m_startViewport->data(1).toInt());
1549 startViewportSize->setValue(m_startViewport->data(2).toInt());
1550 endViewportX->setValue(m_endViewport->data(0).toInt());
1551 endViewportY->setValue(m_endViewport->data(1).toInt());
1552 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1554 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1558 /** \brief Connected to the accepted signal - calls writeChoices */
1559 void TitleWidget::slotAccepted()
1561 if (anim_start->isChecked()) slotAnimStart(false);
1562 if (anim_end->isChecked()) slotAnimEnd(false);
1566 /** \brief Store the current choices of font, background and rect values */
1567 void TitleWidget::writeChoices()
1569 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1570 KSharedConfigPtr config = KGlobal::config();
1571 KConfigGroup titleConfig(config, "TitleWidget");
1572 // Write the entries
1573 titleConfig.writeEntry("font_family", font_family->currentFont());
1574 //titleConfig.writeEntry("font_size", font_size->value());
1575 titleConfig.writeEntry("font_pixel_size", font_size->value());
1576 titleConfig.writeEntry("font_color", fontColorButton->color());
1577 titleConfig.writeEntry("font_alpha", textAlpha->value());
1578 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1579 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1580 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1582 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1583 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1584 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1585 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1586 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1588 titleConfig.writeEntry("background_color", kcolorbutton->color());
1589 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1591 //! \todo Not sure if I should sync - it is probably safe to do it
1596 /** \brief Read the last stored choices into the dialog */
1597 void TitleWidget::readChoices()
1599 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1600 KSharedConfigPtr config = KGlobal::config();
1601 KConfigGroup titleConfig(config, "TitleWidget");
1603 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1604 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1605 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1606 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1608 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1609 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1610 setFontBoxWeight(weight);
1611 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1612 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1614 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1615 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1616 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1617 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1618 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1620 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1621 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1624 void TitleWidget::adjustFrameSize()
1626 m_frameWidth = m_titledocument.frameWidth();
1627 m_frameHeight = m_titledocument.frameHeight();
1628 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1629 displayBackgroundFrame();
1632 void TitleWidget::slotAnimStart(bool anim)
1634 if (anim && anim_end->isChecked()) {
1635 anim_end->setChecked(false);
1636 m_endViewport->setZValue(-1000);
1637 m_endViewport->setBrush(QBrush());
1640 QList<QGraphicsItem *> list = m_scene->items();
1641 for (int i = 0; i < list.count(); i++) {
1642 if (list.at(i)->zValue() > -1000) {
1643 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1644 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1647 align_box->setEnabled(anim);
1648 itemzoom->setEnabled(!anim);
1649 itemrotate->setEnabled(!anim);
1650 frame_toolbar->setEnabled(!anim);
1651 rect_properties->setEnabled(!anim);
1653 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1654 m_startViewport->setZValue(1100);
1655 QColor col = m_startViewport->pen().color();
1657 m_startViewport->setBrush(col);
1658 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1659 m_startViewport->setSelected(true);
1662 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1664 m_startViewport->setZValue(-1000);
1665 m_startViewport->setBrush(QBrush());
1666 m_startViewport->setFlags(0);
1667 if (!anim_end->isChecked()) deleteAnimInfoText();
1672 void TitleWidget::slotAnimEnd(bool anim)
1674 if (anim && anim_start->isChecked()) {
1675 anim_start->setChecked(false);
1676 m_startViewport->setZValue(-1000);
1677 m_startViewport->setBrush(QBrush());
1680 QList<QGraphicsItem *> list = m_scene->items();
1681 for (int i = 0; i < list.count(); i++) {
1682 if (list.at(i)->zValue() > -1000) {
1683 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1684 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1687 align_box->setEnabled(anim);
1688 itemzoom->setEnabled(!anim);
1689 itemrotate->setEnabled(!anim);
1690 frame_toolbar->setEnabled(!anim);
1691 rect_properties->setEnabled(!anim);
1693 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1694 m_endViewport->setZValue(1100);
1695 QColor col = m_endViewport->pen().color();
1697 m_endViewport->setBrush(col);
1698 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1699 m_endViewport->setSelected(true);
1702 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1704 m_endViewport->setZValue(-1000);
1705 m_endViewport->setBrush(QBrush());
1706 m_endViewport->setFlags(0);
1707 if (!anim_start->isChecked()) deleteAnimInfoText();
1711 void TitleWidget::addAnimInfoText()
1713 // add text to anim viewport
1714 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1715 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1716 QFont font = t->font();
1717 font.setPixelSize(m_startViewport->rect().width() / 10);
1718 QColor col = m_startViewport->pen().color();
1720 t->setDefaultTextColor(col);
1722 font.setPixelSize(m_endViewport->rect().width() / 10);
1723 col = m_endViewport->pen().color();
1725 t2->setDefaultTextColor(col);
1729 void TitleWidget::updateInfoText()
1731 // update info text font
1732 if (!m_startViewport->childItems().isEmpty()) {
1733 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1735 QFont font = item->font();
1736 font.setPixelSize(m_startViewport->rect().width() / 10);
1737 item->setFont(font);
1740 if (!m_endViewport->childItems().isEmpty()) {
1741 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1743 QFont font = item->font();
1744 font.setPixelSize(m_endViewport->rect().width() / 10);
1745 item->setFont(font);
1750 void TitleWidget::deleteAnimInfoText()
1752 // end animation editing, remove info text
1753 while (!m_startViewport->childItems().isEmpty()) {
1754 QGraphicsItem *item = m_startViewport->childItems().at(0);
1755 m_scene->removeItem(item);
1758 while (!m_endViewport->childItems().isEmpty()) {
1759 QGraphicsItem *item = m_endViewport->childItems().at(0);
1760 m_scene->removeItem(item);
1766 void TitleWidget::slotKeepAspect(bool keep)
1768 if (m_endViewport->zValue() == 1100) {
1769 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1770 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1772 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1773 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1777 void TitleWidget::slotResize50()
1779 if (m_endViewport->zValue() == 1100) {
1780 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1781 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1784 void TitleWidget::slotResize100()
1786 if (m_endViewport->zValue() == 1100) {
1787 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1788 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1791 void TitleWidget::slotResize200()
1793 if (m_endViewport->zValue() == 1100) {
1794 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1795 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1798 void TitleWidget::slotAddEffect(int ix)
1800 #if QT_VERSION < 0x040600
1803 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1804 QGraphicsEffect *eff = NULL;
1807 eff = new QGraphicsBlurEffect();
1808 shadow_frame->setHidden(true);
1809 blur_frame->setHidden(false);
1810 } else if (ix == 2) {
1811 eff = new QGraphicsDropShadowEffect();
1812 blur_frame->setHidden(true);
1813 shadow_frame->setHidden(false);
1815 blur_frame->setHidden(true);
1816 shadow_frame->setHidden(true);
1818 if (l.size() == 1) {
1819 l[0]->setGraphicsEffect(eff);
1824 void TitleWidget::slotEditBlur(int ix)
1826 #if QT_VERSION < 0x040600
1829 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1830 if (l.size() == 1) {
1831 QGraphicsEffect *eff = l[0]->graphicsEffect();
1832 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1833 if (blur) blur->setBlurRadius(ix);
1838 void TitleWidget::slotEditShadow()
1840 #if QT_VERSION < 0x040600
1843 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1844 if (l.size() == 1) {
1845 QGraphicsEffect *eff = l[0]->graphicsEffect();
1846 QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1848 shadow->setBlurRadius(shadow_radius->value());
1849 shadow->setOffset(shadow_x->value(), shadow_y->value());