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 int settingUp = false;
43 const int IMAGEITEM = 7;
44 const int RECTITEM = 3;
45 const int TEXTITEM = 8;
47 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
50 m_startViewport(NULL),
54 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
55 m_projectTitlePath(projectTitlePath),
59 setFont(KGlobalSettings::toolBarFont());
60 frame_properties->setEnabled(false);
61 rect_properties->setFixedHeight(frame_properties->height() + 4);
62 no_properties->setFixedHeight(frame_properties->height() + 4);
63 image_properties->setFixedHeight(frame_properties->height() + 4);
64 text_properties->setFixedHeight(frame_properties->height() + 4);
65 frame_properties->setFixedHeight(frame_toolbar->height());
67 itemzoom->setSuffix(i18n("%"));
68 m_frameWidth = render->renderWidth();
69 m_frameHeight = render->renderHeight();
70 showToolbars(TITLE_NONE);
72 //TODO: get default title duration instead of hardcoded one
73 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
75 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
76 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
78 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
79 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
80 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
81 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
82 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
84 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
85 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
86 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
87 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
88 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
90 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
91 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
92 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
93 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
94 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
95 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
97 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
98 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
99 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
100 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
101 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
102 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
103 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
104 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
105 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
107 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
108 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
110 m_signalMapper = new QSignalMapper(this);
111 m_signalMapper->setMapping(value_w, ValueWidth);
112 m_signalMapper->setMapping(value_h, ValueHeight);
113 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
114 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
115 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
117 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
118 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
119 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
120 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
121 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
122 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
123 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
124 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
125 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
126 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
127 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
128 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
129 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
130 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
132 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
135 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
137 font_weight_box->blockSignals(true);
138 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
139 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
140 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
141 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
142 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
143 font_weight_box->setToolTip(i18n("Font weight"));
144 font_weight_box->setCurrentIndex(1);
145 font_weight_box->blockSignals(false);
147 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
148 buttonRealSize->setIcon(KIcon("zoom-original"));
149 buttonItalic->setIcon(KIcon("format-text-italic"));
150 buttonUnder->setIcon(KIcon("format-text-underline"));
151 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
152 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
153 buttonAlignRight->setIcon(KIcon("format-justify-right"));
154 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
156 buttonAlignNone->setToolTip(i18n("No alignment"));
157 buttonAlignRight->setToolTip(i18n("Align right"));
158 buttonAlignLeft->setToolTip(i18n("Align left"));
159 buttonAlignCenter->setToolTip(i18n("Align center"));
161 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
162 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
163 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
164 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
165 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
167 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
168 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
169 rectBColor->setToolTip(i18n("Select fill color"));
170 rectFColor->setToolTip(i18n("Select border color"));
171 rectBAlpha->setToolTip(i18n("Fill transparency"));
172 rectFAlpha->setToolTip(i18n("Border transparency"));
173 zoom_slider->setToolTip(i18n("Zoom"));
174 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
175 buttonFitZoom->setToolTip(i18n("Fit zoom"));
176 kcolorbutton->setToolTip(i18n("Select background color"));
177 horizontalSlider->setToolTip(i18n("Background Transparency"));
179 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
180 itemhcenter->setToolTip(i18n("Align item horizontally"));
181 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
182 itemvcenter->setToolTip(i18n("Align item vertically"));
183 itemtop->setIcon(KIcon("kdenlive-align-top"));
184 itemtop->setToolTip(i18n("Align item to top"));
185 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
186 itembottom->setToolTip(i18n("Align item to bottom"));
187 itemright->setIcon(KIcon("kdenlive-align-right"));
188 itemright->setToolTip(i18n("Align item to right"));
189 itemleft->setIcon(KIcon("kdenlive-align-left"));
190 itemleft->setToolTip(i18n("Align item to left"));
193 QHBoxLayout *layout = new QHBoxLayout;
194 frame_toolbar->setLayout(layout);
195 layout->setContentsMargins(2, 2, 2, 2);
196 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
198 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
199 m_buttonCursor->setCheckable(true);
200 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
201 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
202 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
204 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
205 m_buttonText->setCheckable(true);
206 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
207 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
208 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
210 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
211 m_buttonRect->setCheckable(true);
212 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
213 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
214 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
216 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
217 m_buttonImage->setCheckable(false);
218 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
219 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
220 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
222 m_toolbar->addSeparator();
224 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
225 m_buttonLoad->setCheckable(false);
226 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
227 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
229 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
230 m_buttonSave->setCheckable(false);
231 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
232 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
234 layout->addWidget(m_toolbar);
236 // initialize graphic scene
237 m_scene = new GraphicsSceneRectMove(this);
238 graphicsView->setScene(m_scene);
239 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
240 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
242 // a gradient background
243 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
244 gradient->setSpread(QGradient::ReflectSpread);
245 scene->setBackgroundBrush(*gradient);*/
247 m_frameImage = new QGraphicsPixmapItem();
249 qtrans.scale(2.0, 2.0);
250 m_frameImage->setTransform(qtrans);
251 m_frameImage->setZValue(-1200);
252 m_frameImage->setFlags(0);
253 displayBackgroundFrame();
254 graphicsView->scene()->addItem(m_frameImage);
256 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
257 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
258 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
259 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
260 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
261 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
262 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
263 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
265 QPen framepen(Qt::DotLine);
266 framepen.setColor(Qt::red);
268 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
269 m_frameBorder->setPen(framepen);
270 m_frameBorder->setZValue(-1100);
271 m_frameBorder->setBrush(Qt::transparent);
272 m_frameBorder->setFlags(0);
273 graphicsView->scene()->addItem(m_frameBorder);
275 // mbd: load saved settings
278 graphicsView->show();
279 //graphicsView->setRenderHint(QPainter::Antialiasing);
280 graphicsView->setInteractive(true);
281 //graphicsView->resize(400, 300);
282 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
283 //toolBox->setItemEnabled(2, false);
284 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
285 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
286 m_startViewport->setData(0, m_frameWidth);
287 m_startViewport->setData(1, m_frameHeight);
288 m_endViewport->setData(0, m_frameWidth);
289 m_endViewport->setData(1, m_frameHeight);
291 if (!url.isEmpty()) loadTitle(url);
294 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
297 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
298 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
300 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(KdenliveSettings::hastitleproducer());
303 TitleWidget::~TitleWidget()
307 delete m_buttonImage;
308 delete m_buttonCursor;
311 delete m_unicodeAction;
313 delete m_unicodeDialog;
314 delete m_frameBorder;
316 delete m_startViewport;
317 delete m_endViewport;
319 delete m_signalMapper;
323 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
326 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
327 KStandardDirs::makeDir(titlePath);
328 titlePath.append((isClone == false) ? "title" : "clone");
331 while (path.isEmpty() || QFile::exists(path)) {
333 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
335 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
341 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
344 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
345 KStandardDirs::makeDir(titlePath);
346 return titlePath + titleName + ".png";
350 QStringList TitleWidget::extractImageList(QString xml)
353 if (xml.isEmpty()) return result;
356 QDomNodeList images = doc.elementsByTagName("content");
357 for (int i = 0; i < images.count(); i++) {
358 if (images.at(i).toElement().hasAttribute("url"))
359 result.append(images.at(i).toElement().attribute("url"));
366 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
371 void TitleWidget::slotTextTool()
373 m_scene->setTool(TITLE_TEXT);
374 showToolbars(TITLE_TEXT);
375 checkButton(TITLE_TEXT);
378 void TitleWidget::slotRectTool()
380 m_scene->setTool(TITLE_RECTANGLE);
381 showToolbars(TITLE_RECTANGLE);
382 checkButton(TITLE_RECTANGLE);
385 void TitleWidget::slotSelectTool()
387 m_scene->setTool(TITLE_SELECT);
389 // Find out which toolbars need to be shown, depending on selected item
390 TITLETOOL t = TITLE_SELECT;
391 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
393 switch (l.at(0)->type()) {
407 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
408 //graphicsView->centerOn(l.at(0));
414 updateCoordinates(l.at(0));
415 updateDimension(l.at(0));
416 updateRotZoom(l.at(0));
419 checkButton(TITLE_SELECT);
422 void TitleWidget::slotImageTool()
424 // TODO: find a way to get a list of all supported image types...
425 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";
426 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
427 if (!url.isEmpty()) {
428 if (url.path().endsWith(".svg")) {
429 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
430 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
431 svg->setZValue(m_count++);
432 svg->setData(Qt::UserRole, url.path());
433 graphicsView->scene()->addItem(svg);
435 QPixmap pix(url.path());
436 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
437 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
438 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
439 image->setData(Qt::UserRole, url.path());
440 image->setZValue(m_count++);
441 graphicsView->scene()->addItem(image);
444 m_scene->setTool(TITLE_SELECT);
445 showToolbars(TITLE_SELECT);
446 checkButton(TITLE_NONE);
449 void TitleWidget::showToolbars(TITLETOOL toolType)
453 rect_properties->setHidden(true);
454 image_properties->setHidden(true);
455 no_properties->setHidden(true);
456 text_properties->setHidden(false);
458 case TITLE_RECTANGLE:
459 image_properties->setHidden(true);
460 no_properties->setHidden(true);
461 text_properties->setHidden(true);
462 rect_properties->setHidden(false);
465 no_properties->setHidden(true);
466 text_properties->setHidden(true);
467 rect_properties->setHidden(true);
468 image_properties->setHidden(false);
471 text_properties->setHidden(true);
472 rect_properties->setHidden(true);
473 image_properties->setHidden(true);
474 no_properties->setHidden(false);
479 void TitleWidget::enableToolbars(TITLETOOL toolType)
481 // TITLETOOL is defined in graphicsscenerectmove.h
486 bool bValue_w = false;
487 bool bValue_h = false;
496 case TITLE_RECTANGLE:
511 frame_properties->setEnabled(bFrame);
512 text_properties->setEnabled(bText);
513 rect_properties->setEnabled(bRect);
514 image_properties->setEnabled(bImage);
515 value_w->setEnabled(bValue_w);
516 value_h->setEnabled(bValue_h);
519 void TitleWidget::checkButton(TITLETOOL toolType)
521 bool bSelect = false;
533 case TITLE_RECTANGLE:
543 m_buttonCursor->setChecked(bSelect);
544 m_buttonText->setChecked(bText);
545 m_buttonRect->setChecked(bRect);
546 m_buttonImage->setChecked(bImage);
549 void TitleWidget::displayBackgroundFrame()
551 if (!displayBg->isChecked()) {
552 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
553 QPixmap pattern(20, 20);
555 QColor bgcolor(210, 210, 210);
558 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
559 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
564 p.fillRect(bg.rect(), br);
566 m_frameImage->setPixmap(bg);
568 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
572 void TitleWidget::initAnimation()
574 align_box->setEnabled(false);
575 QPen startpen(Qt::DotLine);
576 QPen endpen(Qt::DashDotLine);
577 startpen.setColor(QColor(100, 200, 100, 140));
578 endpen.setColor(QColor(200, 100, 100, 140));
580 m_startViewport->setPen(startpen);
581 m_endViewport->setPen(endpen);
583 m_startViewport->setZValue(-1000);
584 m_endViewport->setZValue(-1000);
586 m_startViewport->setFlags(0);
587 m_endViewport->setFlags(0);
589 graphicsView->scene()->addItem(m_startViewport);
590 graphicsView->scene()->addItem(m_endViewport);
592 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
593 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
594 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
595 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
598 void TitleWidget::slotUpdateZoom(int pos)
600 m_scene->setZoom((double) pos / 100);
601 zoom_label->setText(QString::number(pos) + '%');
604 void TitleWidget::slotZoom(bool up)
606 int pos = zoom_slider->value();
609 zoom_slider->setValue(pos);
612 void TitleWidget::slotAdjustZoom()
614 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
615 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
616 if (scalex > scaley) scalex = scaley;
617 int zoompos = (int)(scalex * 7 + 0.5);*/
618 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
619 int zoompos = graphicsView->matrix().m11() * 100;
620 zoom_slider->setValue(zoompos);
621 graphicsView->centerOn(m_frameBorder);
624 void TitleWidget::slotZoomOneToOne()
626 zoom_slider->setValue(100);
627 graphicsView->centerOn(m_frameBorder);
630 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
632 updateAxisButtons(rect); // back to default
634 QColor f = rectFColor->color();
635 f.setAlpha(rectFAlpha->value());
637 penf.setWidth(rectLineWidth->value());
639 QColor b = rectBColor->color();
640 b.setAlpha(rectBAlpha->value());
641 rect->setBrush(QBrush(b));
642 rect->setZValue(m_count++);
643 //setCurrentItem(rect);
644 //graphicsView->setFocus();
647 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
649 updateAxisButtons(tt); // back to default
651 QFont font = font_family->currentFont();
652 font.setPixelSize(font_size->value());
654 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
655 font.setItalic(buttonItalic->isChecked());
656 font.setUnderline(buttonUnder->isChecked());
659 QColor color = fontColorButton->color();
660 color.setAlpha(textAlpha->value());
661 tt->setDefaultTextColor(color);
662 tt->setZValue(m_count++);
666 void TitleWidget::setFontBoxWeight(int weight)
668 int index = font_weight_box->findData(weight);
670 index = font_weight_box->findData(QFont::Normal);
672 font_weight_box->setCurrentIndex(index);
675 void TitleWidget::setCurrentItem(QGraphicsItem *item)
677 m_scene->setSelectedItem(item);
680 void TitleWidget::zIndexChanged(int v)
682 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
688 void TitleWidget::selectionChanged()
690 if (m_scene->tool() != TITLE_SELECT) return;
691 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
692 //toolBox->setItemEnabled(2, false);
693 //toolBox->setItemEnabled(3, false);
694 value_x->blockSignals(true);
695 value_y->blockSignals(true);
696 value_w->blockSignals(true);
697 value_h->blockSignals(true);
698 itemzoom->blockSignals(true);
699 itemrotate->blockSignals(true);
701 bool blockX = !origin_x_left->signalsBlocked();
702 bool blockY = !origin_y_top->signalsBlocked();
703 if (blockX) origin_x_left->blockSignals(true);
704 if (blockY) origin_y_top->blockSignals(true);
705 origin_x_left->setChecked(false);
706 origin_y_top->setChecked(false);
709 enableToolbars(TITLE_NONE);
710 if (blockX) origin_x_left->blockSignals(false);
711 if (blockY) origin_y_top->blockSignals(false);
712 itemzoom->setEnabled(false);
713 itemrotate->setEnabled(false);
714 } else if (l.size() == 1) {
715 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
716 itemzoom->setEnabled(true);
717 itemrotate->setEnabled(true);
719 itemzoom->setEnabled(false);
720 itemrotate->setEnabled(false);
723 if (l.at(0)->type() == TEXTITEM) {
724 showToolbars(TITLE_TEXT);
725 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
726 //if (l[0]->hasFocus())
727 //toolBox->setCurrentIndex(0);
728 //toolBox->setItemEnabled(2, true);
729 font_size->blockSignals(true);
730 font_family->blockSignals(true);
731 font_weight_box->blockSignals(true);
732 buttonItalic->blockSignals(true);
733 buttonUnder->blockSignals(true);
734 fontColorButton->blockSignals(true);
735 textAlpha->blockSignals(true);
736 buttonAlignLeft->blockSignals(true);
737 buttonAlignRight->blockSignals(true);
738 buttonAlignNone->blockSignals(true);
739 buttonAlignCenter->blockSignals(true);
741 QFont font = i->font();
742 font_family->setCurrentFont(font);
743 font_size->setValue(font.pixelSize());
744 buttonItalic->setChecked(font.italic());
745 buttonUnder->setChecked(font.underline());
746 setFontBoxWeight(font.weight());
748 QColor color = i->defaultTextColor();
749 fontColorButton->setColor(color);
750 textAlpha->setValue(color.alpha());
752 QTextCursor cur = i->textCursor();
753 QTextBlockFormat format = cur.blockFormat();
754 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
755 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
756 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
757 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
759 font_size->blockSignals(false);
760 font_family->blockSignals(false);
761 font_weight_box->blockSignals(false);
762 buttonItalic->blockSignals(false);
763 buttonUnder->blockSignals(false);
764 fontColorButton->blockSignals(false);
765 textAlpha->blockSignals(false);
766 buttonAlignLeft->blockSignals(false);
767 buttonAlignRight->blockSignals(false);
768 buttonAlignNone->blockSignals(false);
769 buttonAlignCenter->blockSignals(false);
771 updateAxisButtons(i);
772 updateCoordinates(i);
774 enableToolbars(TITLE_TEXT);
776 } else if ((l.at(0))->type() == RECTITEM) {
777 showToolbars(TITLE_RECTANGLE);
779 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
780 if (rec == m_startViewport || rec == m_endViewport) {
781 /*toolBox->setCurrentIndex(3);
782 toolBox->widget(0)->setEnabled(false);
783 toolBox->widget(1)->setEnabled(false);*/
784 enableToolbars(TITLE_NONE);
786 /*toolBox->widget(0)->setEnabled(true);
787 toolBox->widget(1)->setEnabled(true);
788 toolBox->setCurrentIndex(0);*/
789 //toolBox->setItemEnabled(3, true);
790 rectFAlpha->setValue(rec->pen().color().alpha());
791 rectBAlpha->setValue(rec->brush().color().alpha());
792 //kDebug() << rec->brush().color().alpha();
793 QColor fcol = rec->pen().color();
794 QColor bcol = rec->brush().color();
795 //fcol.setAlpha(255);
796 //bcol.setAlpha(255);
797 rectFColor->setColor(fcol);
798 rectBColor->setColor(bcol);
800 rectLineWidth->setValue(rec->pen().width());
801 enableToolbars(TITLE_RECTANGLE);
804 updateAxisButtons(l.at(0));
805 updateCoordinates(rec);
806 updateDimension(rec);
808 } else if (l.at(0)->type() == IMAGEITEM) {
809 showToolbars(TITLE_IMAGE);
811 updateCoordinates(l.at(0));
812 updateDimension(l.at(0));
814 enableToolbars(TITLE_IMAGE);
817 //toolBox->setCurrentIndex(0);
818 showToolbars(TITLE_NONE);
819 enableToolbars(TITLE_NONE);
820 /*frame_properties->setEnabled(false);
821 text_properties->setEnabled(false);
822 rect_properties->setEnabled(false);*/
824 zValue->setValue((int)l.at(0)->zValue());
825 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
826 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
827 value_x->blockSignals(false);
828 value_y->blockSignals(false);
829 value_w->blockSignals(false);
830 value_h->blockSignals(false);
831 itemzoom->blockSignals(false);
832 itemrotate->blockSignals(false);
836 void TitleWidget::slotValueChanged(int type)
838 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
839 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
844 val = value_w->value();
847 val = value_h->value();
851 QGraphicsItem *i = l.at(0);
852 Transform t = m_transformations.value(i);
854 // Ratio width:height
855 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
856 double alpha = (double) t.rotate / 180.0 * M_PI;
866 // Add 0.5 because otherwise incrementing by 1 might have no effect
867 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
868 scale = length / i->boundingRect().width();
871 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
872 scale = length / i->boundingRect().height();
879 qtrans.scale(scale, scale);
880 qtrans.rotate(t.rotate);
881 i->setTransform(qtrans);
882 m_transformations[i] = t;
889 /** \brief Updates position/size of the selected item when a value
890 * of an item (coordinates, size) has changed */
891 void TitleWidget::slotAdjustSelectedItem()
893 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
895 if (l.at(0)->type() == RECTITEM) {
897 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
899 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
900 } else if (l.at(0)->type() == TEXTITEM) {
902 updatePosition(l.at(0));
903 } else if (l.at(0)->type() == IMAGEITEM) {
905 updatePosition(l.at(0));
910 /** \brief Updates width/height int the text fields, regarding transformation matrix */
911 void TitleWidget::updateDimension(QGraphicsItem *i)
913 value_w->blockSignals(true);
914 value_h->blockSignals(true);
917 if (i->type() == IMAGEITEM) {
918 // Get multipliers for rotation/scaling
920 /*Transform t = m_transformations.value(i);
921 QRectF r = i->boundingRect();
922 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
923 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
924 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
925 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
927 value_w->setValue(i->sceneBoundingRect().width());
928 value_h->setValue(i->sceneBoundingRect().height());
929 } else if (i->type() == RECTITEM) {
930 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
931 value_w->setValue((int) r->rect().width());
932 value_h->setValue((int) r->rect().height());
933 } else if (i->type() == TEXTITEM) {
934 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
935 value_w->setValue((int) t->boundingRect().width());
936 value_h->setValue((int) t->boundingRect().height());
939 value_w->blockSignals(false);
940 value_h->blockSignals(false);
943 /** \brief Updates the coordinates in the text fields from the item */
944 void TitleWidget::updateCoordinates(QGraphicsItem *i)
946 // Block signals emitted by this method
947 value_x->blockSignals(true);
948 value_y->blockSignals(true);
950 if (i->type() == TEXTITEM) {
952 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
954 // Set the correct x coordinate value
955 if (origin_x_left->isChecked()) {
956 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
957 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
959 // Origin is at 0 (default)
960 value_x->setValue((int) rec->pos().x());
964 if (origin_y_top->isChecked()) {
965 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
967 value_y->setValue((int) rec->pos().y());
970 } else if (i->type() == RECTITEM) {
972 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
974 if (origin_x_left->isChecked()) {
975 // Origin (0 point) is at m_frameWidth
976 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
978 // Origin is at 0 (default)
979 value_x->setValue((int) rec->pos().x());
982 if (origin_y_top->isChecked()) {
983 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
985 value_y->setValue((int) rec->pos().y());
988 } else if (i->type() == IMAGEITEM) {
990 if (origin_x_left->isChecked()) {
991 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
993 value_x->setValue((int) i->pos().x());
996 if (origin_y_top->isChecked()) {
997 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
999 value_y->setValue((int) i->pos().y());
1004 // Stop blocking signals now
1005 value_x->blockSignals(false);
1006 value_y->blockSignals(false);
1009 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1011 itemzoom->blockSignals(true);
1012 itemrotate->blockSignals(false);
1014 Transform t = m_transformations.value(i);
1015 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1016 itemrotate->setValue((int)(t.rotate));
1018 itemzoom->blockSignals(false);
1019 itemrotate->blockSignals(false);
1022 /** \brief Updates the position of an item by reading coordinates from the text fields */
1023 void TitleWidget::updatePosition(QGraphicsItem *i)
1025 if (i->type() == TEXTITEM) {
1026 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1029 if (origin_x_left->isChecked()) {
1030 /* Origin of the x axis is at m_frameWidth,
1031 * and distance from right border of the item to the right
1032 * border of the frame is taken.
1033 * See comment to slotOriginXClicked().
1035 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1037 posX = value_x->value();
1041 if (origin_y_top->isChecked()) {
1042 /* Same for y axis */
1043 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1045 posY = value_y->value();
1048 rec->setPos(posX, posY);
1050 } else if (i->type() == RECTITEM) {
1052 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1055 if (origin_x_left->isChecked()) {
1056 posX = m_frameWidth - value_x->value() - rec->rect().width();
1058 posX = value_x->value();
1062 if (origin_y_top->isChecked()) {
1063 posY = m_frameHeight - value_y->value() - rec->rect().height();
1065 posY = value_y->value();
1068 rec->setPos(posX, posY);
1070 } else if (i->type() == IMAGEITEM) {
1072 if (origin_x_left->isChecked()) {
1073 // Use the sceneBoundingRect because this also regards transformations like zoom
1074 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1076 posX = value_x->value();
1080 if (origin_y_top->isChecked()) {
1081 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1083 posY = value_y->value();
1086 i->setPos(posX, posY);
1092 void TitleWidget::updateTextOriginX()
1094 if (origin_x_left->isChecked()) {
1095 origin_x_left->setText(i18n("\u2212X"));
1097 origin_x_left->setText(i18n("+X"));
1101 void TitleWidget::slotOriginXClicked()
1103 // Update the text displayed on the button.
1104 updateTextOriginX();
1106 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1107 if (l.size() >= 1) {
1108 updateCoordinates(l.at(0));
1110 // Remember x axis setting
1111 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1112 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1114 graphicsView->setFocus();
1117 void TitleWidget::updateTextOriginY()
1119 if (origin_y_top->isChecked()) {
1120 origin_y_top->setText(i18n("\u2212Y"));
1122 origin_y_top->setText(i18n("+Y"));
1126 void TitleWidget::slotOriginYClicked()
1128 // Update the text displayed on the button.
1129 updateTextOriginY();
1131 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1132 if (l.size() >= 1) {
1133 updateCoordinates(l.at(0));
1135 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1136 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1139 graphicsView->setFocus();
1142 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1144 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1145 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1146 origin_x_left->blockSignals(true);
1147 origin_y_top->blockSignals(true);
1149 if (xAxis == TitleDocument::AxisInverted) {
1150 origin_x_left->setChecked(true);
1152 origin_x_left->setChecked(false);
1154 updateTextOriginX();
1156 if (yAxis == TitleDocument::AxisInverted) {
1157 origin_y_top->setChecked(true);
1159 origin_y_top->setChecked(false);
1161 updateTextOriginY();
1163 origin_x_left->blockSignals(false);
1164 origin_y_top->blockSignals(false);
1167 void TitleWidget::slotChangeBackground()
1169 QColor color = kcolorbutton->color();
1170 color.setAlpha(horizontalSlider->value());
1171 m_frameBorder->setBrush(QBrush(color));
1175 * Something (yeah) has changed in our QGraphicsScene.
1177 void TitleWidget::slotChanged()
1179 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1180 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1181 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1186 * If the user has set origin_x_left (everything also for y),
1187 * we need to look whether a text element has been selected. If yes,
1188 * we need to ensure that the right border of the text field
1189 * remains fixed also when some text has been entered.
1191 * This is also known as right-justified, with the difference that
1192 * it is not valid for text but for its boundingRect. Text may still
1193 * be left-justified.
1195 void TitleWidget::textChanged(QGraphicsTextItem *i)
1200 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1202 if (!i->toPlainText().isEmpty()) {
1206 * Don't do anything if the string is empty. If the position
1207 * would be updated here, a newly created text field would
1208 * be set to the position of the last selected text field.
1214 void TitleWidget::slotInsertUnicode()
1216 m_unicodeDialog->exec();
1219 void TitleWidget::slotInsertUnicodeString(QString text)
1221 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1223 if (l.at(0)->type() == TEXTITEM) {
1224 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1225 t->textCursor().insertText(text);
1230 void TitleWidget::slotUpdateText()
1232 QFont font = font_family->currentFont();
1233 font.setPixelSize(font_size->value());
1234 font.setItalic(buttonItalic->isChecked());
1235 font.setUnderline(buttonUnder->isChecked());
1236 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1237 QColor color = fontColorButton->color();
1238 color.setAlpha(textAlpha->value());
1240 QGraphicsTextItem* item = NULL;
1241 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1242 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1243 item = static_cast <QGraphicsTextItem *>(l.at(0));
1246 //if (item->textCursor().selection ().isEmpty())
1247 QTextCursor cur = item->textCursor();
1248 QTextBlockFormat format = cur.blockFormat();
1249 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1250 item->setTextWidth(item->boundingRect().width());
1251 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1252 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1253 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1255 format.setAlignment(Qt::AlignLeft);
1256 item->setTextWidth(-1);
1260 item->setFont(font);
1261 item->setDefaultTextColor(color);
1262 cur.select(QTextCursor::Document);
1263 cur.setBlockFormat(format);
1264 item->setTextCursor(cur);
1265 cur.clearSelection();
1266 item->setTextCursor(cur);
1271 void TitleWidget::rectChanged()
1273 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1274 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1275 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1276 QColor f = rectFColor->color();
1277 f.setAlpha(rectFAlpha->value());
1279 penf.setWidth(rectLineWidth->value());
1281 QColor b = rectBColor->color();
1282 b.setAlpha(rectBAlpha->value());
1283 rec->setBrush(QBrush(b));
1287 void TitleWidget::itemScaled(int val)
1289 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1290 if (l.size() == 1) {
1291 Transform x = m_transformations.value(l.at(0));
1292 x.scalex = (double)val / 100.0;
1293 x.scaley = (double)val / 100.0;
1295 qtrans.scale(x.scalex, x.scaley);
1296 qtrans.rotate(x.rotate);
1297 l[0]->setTransform(qtrans);
1298 m_transformations[l.at(0)] = x;
1299 updateDimension(l.at(0));
1303 void TitleWidget::itemRotate(int val)
1305 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1306 if (l.size() == 1) {
1307 Transform x = m_transformations[l.at(0)];
1308 x.rotate = (double)val;
1310 qtrans.scale(x.scalex, x.scaley);
1311 qtrans.rotate(x.rotate);
1312 l[0]->setTransform(qtrans);
1313 m_transformations[l.at(0)] = x;
1314 updateDimension(l.at(0));
1318 void TitleWidget::itemHCenter()
1320 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1321 if (l.size() == 1) {
1322 QGraphicsItem *item = l.at(0);
1323 QRectF br = item->sceneBoundingRect();
1324 int width = (int)br.width();
1325 int newPos = (int)((m_frameWidth - width) / 2);
1326 newPos += item->pos().x() - br.left(); // Check item transformation
1327 item->setPos(newPos, item->pos().y());
1328 updateCoordinates(item);
1332 void TitleWidget::itemVCenter()
1334 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1335 if (l.size() == 1) {
1336 QGraphicsItem *item = l.at(0);
1337 QRectF br = item->sceneBoundingRect();
1338 int height = (int)br.height();
1339 int newPos = (int)((m_frameHeight - height) / 2);
1340 newPos += item->pos().y() - br.top(); // Check item transformation
1341 item->setPos(item->pos().x(), newPos);
1342 updateCoordinates(item);
1346 void TitleWidget::itemTop()
1348 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1349 if (l.size() == 1) {
1350 QGraphicsItem *item = l.at(0);
1351 QRectF br = item->sceneBoundingRect();
1353 if (br.top() > 0) diff = -br.top();
1354 else diff = -br.bottom();
1355 item->moveBy(0, diff);
1356 updateCoordinates(item);
1360 void TitleWidget::itemBottom()
1362 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1363 if (l.size() == 1) {
1364 QGraphicsItem *item = l.at(0);
1365 QRectF br = item->sceneBoundingRect();
1367 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1368 else diff = m_frameHeight - br.bottom();
1369 item->moveBy(0, diff);
1370 updateCoordinates(item);
1374 void TitleWidget::itemLeft()
1376 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1377 if (l.size() == 1) {
1378 QGraphicsItem *item = l.at(0);
1379 QRectF br = item->sceneBoundingRect();
1381 if (br.left() > 0) diff = -br.left();
1382 else diff = -br.right();
1383 item->moveBy(diff, 0);
1384 updateCoordinates(item);
1388 void TitleWidget::itemRight()
1390 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1391 if (l.size() == 1) {
1392 QGraphicsItem *item = l.at(0);
1393 QRectF br = item->sceneBoundingRect();
1395 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1396 else diff = m_frameWidth - br.left();
1397 item->moveBy(diff, 0);
1398 updateCoordinates(item);
1402 void TitleWidget::setupViewports()
1404 //double aspect_ratio = 4.0 / 3.0;//read from project
1405 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1406 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1407 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1408 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1409 QPolygonF spoly(sp);
1410 QPolygonF epoly(ep);
1411 spoly.translate(startViewportX->value(), startViewportY->value());
1412 epoly.translate(endViewportX->value(), endViewportY->value());
1413 m_startViewport->setPolygon(spoly);
1414 m_endViewport->setPolygon(epoly);
1415 if (! insertingValues) {
1416 m_startViewport->setData(0, startViewportX->value());
1417 m_startViewport->setData(1, startViewportY->value());
1418 m_startViewport->setData(2, startViewportSize->value());
1420 m_endViewport->setData(0, endViewportX->value());
1421 m_endViewport->setData(1, endViewportY->value());
1422 m_endViewport->setData(2, endViewportSize->value());
1426 void TitleWidget::loadTitle(KUrl url)
1428 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1429 if (!url.isEmpty()) {
1430 QList<QGraphicsItem *> items = m_scene->items();
1431 for (int i = 0; i < items.size(); i++) {
1432 if (items.at(i)->zValue() > -1000) delete items.at(i);
1434 m_scene->clearTextSelection();
1438 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1439 QFile file(tmpfile);
1440 if (file.open(QIODevice::ReadOnly)) {
1441 doc.setContent(&file, false);
1444 KIO::NetAccess::removeTempFile(tmpfile);
1449 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1451 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1452 insertingValues = true;
1453 startViewportX->setValue(m_startViewport->data(0).toInt());
1454 startViewportY->setValue(m_startViewport->data(1).toInt());
1455 startViewportSize->setValue(m_startViewport->data(2).toInt());
1456 endViewportX->setValue(m_endViewport->data(0).toInt());
1457 endViewportY->setValue(m_endViewport->data(1).toInt());
1458 endViewportSize->setValue(m_endViewport->data(2).toInt());
1460 insertingValues = false;
1466 void TitleWidget::saveTitle(KUrl url)
1468 if (anim_start->isChecked()) slotAnimStart(false);
1469 if (anim_end->isChecked()) slotAnimEnd(false);
1470 if (url.isEmpty()) {
1471 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1472 fs->setOperationMode(KFileDialog::Saving);
1473 fs->setMode(KFile::File);
1474 #if KDE_IS_VERSION(4,2,0)
1475 fs->setConfirmOverwrite(true);
1477 fs->setKeepLocation(true);
1479 url = fs->selectedUrl();
1482 if (!url.isEmpty()) {
1483 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1484 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1488 QDomDocument TitleWidget::xml()
1490 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1491 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1495 int TitleWidget::duration() const
1497 return m_tc.getFrameCount(title_duration->text());
1500 void TitleWidget::setXml(QDomDocument doc)
1503 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1505 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1506 /*if (doc.documentElement().hasAttribute("out")) {
1507 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1508 title_duration->setText(m_tc.getTimecode(duration));
1510 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1512 QDomElement e = doc.documentElement();
1513 m_transformations.clear();
1514 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1515 const double PI = 4.0 * atan(1.0);
1516 for (int i = 0; i < items.count(); i++) {
1517 QTransform t = items.at(i)->transform();
1521 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1522 m_transformations[items.at(i)] = x;
1524 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1525 QColor background_color = m_titledocument.getBackgroundColor();
1526 horizontalSlider->blockSignals(true);
1527 kcolorbutton->blockSignals(true);
1528 horizontalSlider->setValue(background_color.alpha());
1529 background_color.setAlpha(255);
1530 kcolorbutton->setColor(background_color);
1531 horizontalSlider->blockSignals(false);
1532 kcolorbutton->blockSignals(false);
1534 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1535 startViewportY->setValue(m_startViewport->data(1).toInt());
1536 startViewportSize->setValue(m_startViewport->data(2).toInt());
1537 endViewportX->setValue(m_endViewport->data(0).toInt());
1538 endViewportY->setValue(m_endViewport->data(1).toInt());
1539 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1541 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1545 /** \brief Connected to the accepted signal - calls writeChoices */
1546 void TitleWidget::slotAccepted()
1548 if (anim_start->isChecked()) slotAnimStart(false);
1549 if (anim_end->isChecked()) slotAnimEnd(false);
1553 /** \brief Store the current choices of font, background and rect values */
1554 void TitleWidget::writeChoices()
1556 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1557 KSharedConfigPtr config = KGlobal::config();
1558 KConfigGroup titleConfig(config, "TitleWidget");
1559 // Write the entries
1560 titleConfig.writeEntry("font_family", font_family->currentFont());
1561 //titleConfig.writeEntry("font_size", font_size->value());
1562 titleConfig.writeEntry("font_pixel_size", font_size->value());
1563 titleConfig.writeEntry("font_color", fontColorButton->color());
1564 titleConfig.writeEntry("font_alpha", textAlpha->value());
1565 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1566 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1567 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1569 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1570 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1571 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1572 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1573 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1575 titleConfig.writeEntry("background_color", kcolorbutton->color());
1576 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1578 //! \todo Not sure if I should sync - it is probably safe to do it
1583 /** \brief Read the last stored choices into the dialog */
1584 void TitleWidget::readChoices()
1586 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1587 KSharedConfigPtr config = KGlobal::config();
1588 KConfigGroup titleConfig(config, "TitleWidget");
1590 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1591 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1592 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1593 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1595 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1596 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1597 setFontBoxWeight(weight);
1598 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1599 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1601 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1602 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1603 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1604 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1605 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1607 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1608 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1611 void TitleWidget::adjustFrameSize()
1613 m_frameWidth = m_titledocument.frameWidth();
1614 m_frameHeight = m_titledocument.frameHeight();
1615 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1616 displayBackgroundFrame();
1619 void TitleWidget::slotAnimStart(bool anim)
1621 if (anim && anim_end->isChecked()) {
1622 anim_end->setChecked(false);
1623 m_endViewport->setZValue(-1000);
1624 m_endViewport->setBrush(QBrush());
1627 QList<QGraphicsItem *> list = m_scene->items();
1628 for (int i = 0; i < list.count(); i++) {
1629 if (list.at(i)->zValue() > -1000) {
1630 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1631 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1634 align_box->setEnabled(anim);
1635 itemzoom->setEnabled(!anim);
1636 itemrotate->setEnabled(!anim);
1637 frame_toolbar->setEnabled(!anim);
1638 rect_properties->setEnabled(!anim);
1640 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1641 m_startViewport->setZValue(1100);
1642 QColor col = m_startViewport->pen().color();
1644 m_startViewport->setBrush(col);
1645 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1646 m_startViewport->setSelected(true);
1649 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1651 m_startViewport->setZValue(-1000);
1652 m_startViewport->setBrush(QBrush());
1653 m_startViewport->setFlags(0);
1654 if (!anim_end->isChecked()) deleteAnimInfoText();
1659 void TitleWidget::slotAnimEnd(bool anim)
1661 if (anim && anim_start->isChecked()) {
1662 anim_start->setChecked(false);
1663 m_startViewport->setZValue(-1000);
1664 m_startViewport->setBrush(QBrush());
1667 QList<QGraphicsItem *> list = m_scene->items();
1668 for (int i = 0; i < list.count(); i++) {
1669 if (list.at(i)->zValue() > -1000) {
1670 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1671 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1674 align_box->setEnabled(anim);
1675 itemzoom->setEnabled(!anim);
1676 itemrotate->setEnabled(!anim);
1677 frame_toolbar->setEnabled(!anim);
1678 rect_properties->setEnabled(!anim);
1680 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1681 m_endViewport->setZValue(1100);
1682 QColor col = m_endViewport->pen().color();
1684 m_endViewport->setBrush(col);
1685 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1686 m_endViewport->setSelected(true);
1689 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1691 m_endViewport->setZValue(-1000);
1692 m_endViewport->setBrush(QBrush());
1693 m_endViewport->setFlags(0);
1694 if (!anim_start->isChecked()) deleteAnimInfoText();
1698 void TitleWidget::addAnimInfoText()
1700 // add text to anim viewport
1701 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1702 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1703 QFont font = t->font();
1704 font.setPixelSize(m_startViewport->rect().width() / 10);
1705 QColor col = m_startViewport->pen().color();
1707 t->setDefaultTextColor(col);
1709 font.setPixelSize(m_endViewport->rect().width() / 10);
1710 col = m_endViewport->pen().color();
1712 t2->setDefaultTextColor(col);
1716 void TitleWidget::updateInfoText()
1718 // update info text font
1719 if (!m_startViewport->childItems().isEmpty()) {
1720 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1722 QFont font = item->font();
1723 font.setPixelSize(m_startViewport->rect().width() / 10);
1724 item->setFont(font);
1727 if (!m_endViewport->childItems().isEmpty()) {
1728 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1730 QFont font = item->font();
1731 font.setPixelSize(m_endViewport->rect().width() / 10);
1732 item->setFont(font);
1737 void TitleWidget::deleteAnimInfoText()
1739 // end animation editing, remove info text
1740 while (!m_startViewport->childItems().isEmpty()) {
1741 QGraphicsItem *item = m_startViewport->childItems().at(0);
1742 m_scene->removeItem(item);
1745 while (!m_endViewport->childItems().isEmpty()) {
1746 QGraphicsItem *item = m_endViewport->childItems().at(0);
1747 m_scene->removeItem(item);
1753 void TitleWidget::slotKeepAspect(bool keep)
1755 if (m_endViewport->zValue() == 1100) {
1756 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1757 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1759 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1760 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1764 void TitleWidget::slotResize50()
1766 if (m_endViewport->zValue() == 1100) {
1767 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1768 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1771 void TitleWidget::slotResize100()
1773 if (m_endViewport->zValue() == 1100) {
1774 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1775 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1778 void TitleWidget::slotResize200()
1780 if (m_endViewport->zValue() == 1100) {
1781 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1782 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);