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>
30 #include <QDomDocument>
31 #include <QGraphicsItem>
32 #include <QGraphicsSvgItem>
36 #include <QSignalMapper>
37 #include <QTextBlockFormat>
38 #include <QTextCursor>
40 int settingUp = false;
42 const int IMAGEITEM = 7;
43 const int RECTITEM = 3;
44 const int TEXTITEM = 8;
45 static bool insertingValues = false;
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 //toolBox->setFont(KGlobalSettings::toolBarFont());
61 frame_properties->setEnabled(false);
62 rect_properties->setFixedHeight(frame_properties->height() + 4);
63 no_properties->setFixedHeight(frame_properties->height() + 4);
64 image_properties->setFixedHeight(frame_properties->height() + 4);
65 text_properties->setFixedHeight(frame_properties->height() + 4);
66 frame_properties->setFixedHeight(frame_toolbar->height());
67 m_frameWidth = render->renderWidth();
68 m_frameHeight = render->renderHeight();
69 showToolbars(TITLE_NONE);
71 //TODO: get default title duration instead of hardcoded one
72 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
74 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
75 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
77 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
78 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
79 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
80 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
81 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
83 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
84 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
85 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
86 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
87 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
89 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
90 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
91 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
92 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
93 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
94 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
96 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
97 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
98 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
99 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
100 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
101 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
102 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
103 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
104 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
106 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
107 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
109 m_signalMapper = new QSignalMapper(this);
110 m_signalMapper->setMapping(value_w, ValueWidth);
111 m_signalMapper->setMapping(value_h, ValueHeight);
112 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
113 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
114 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
116 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
117 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
118 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
119 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
120 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
121 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
122 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
123 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
124 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
125 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
126 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
127 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
128 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
129 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
131 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
134 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
136 font_weight_box->blockSignals(true);
137 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
138 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
139 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
140 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
141 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
142 font_weight_box->setToolTip(i18n("Font weight"));
143 font_weight_box->setCurrentIndex(1);
144 font_weight_box->blockSignals(false);
146 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
147 buttonRealSize->setIcon(KIcon("zoom-original"));
148 buttonItalic->setIcon(KIcon("format-text-italic"));
149 buttonUnder->setIcon(KIcon("format-text-underline"));
150 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
151 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
152 buttonAlignRight->setIcon(KIcon("format-justify-right"));
153 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
155 buttonAlignNone->setToolTip(i18n("No alignment"));
156 buttonAlignRight->setToolTip(i18n("Align right"));
157 buttonAlignLeft->setToolTip(i18n("Align left"));
158 buttonAlignCenter->setToolTip(i18n("Align center"));
160 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
161 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
162 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
163 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
164 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
166 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
167 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
168 rectBColor->setToolTip(i18n("Select fill color"));
169 rectFColor->setToolTip(i18n("Select border color"));
170 rectBAlpha->setToolTip(i18n("Fill transparency"));
171 rectFAlpha->setToolTip(i18n("Border transparency"));
172 zoom_slider->setToolTip(i18n("Zoom"));
173 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
174 buttonFitZoom->setToolTip(i18n("Fit zoom"));
175 kcolorbutton->setToolTip(i18n("Select background color"));
176 horizontalSlider->setToolTip(i18n("Background Transparency"));
178 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
179 itemhcenter->setToolTip(i18n("Align item horizontally"));
180 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
181 itemvcenter->setToolTip(i18n("Align item vertically"));
182 itemtop->setIcon(KIcon("kdenlive-align-top"));
183 itemtop->setToolTip(i18n("Align item to top"));
184 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
185 itembottom->setToolTip(i18n("Align item to bottom"));
186 itemright->setIcon(KIcon("kdenlive-align-right"));
187 itemright->setToolTip(i18n("Align item to right"));
188 itemleft->setIcon(KIcon("kdenlive-align-left"));
189 itemleft->setToolTip(i18n("Align item to left"));
192 QHBoxLayout *layout = new QHBoxLayout;
193 frame_toolbar->setLayout(layout);
194 layout->setContentsMargins(2, 2, 2, 2);
195 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
197 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
198 m_buttonCursor->setCheckable(true);
199 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
200 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
201 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
203 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
204 m_buttonText->setCheckable(true);
205 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
206 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
207 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
209 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
210 m_buttonRect->setCheckable(true);
211 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
212 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
213 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
215 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
216 m_buttonImage->setCheckable(false);
217 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
218 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
219 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
221 m_toolbar->addSeparator();
223 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
224 m_buttonLoad->setCheckable(false);
225 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
226 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
228 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
229 m_buttonSave->setCheckable(false);
230 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
231 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
233 layout->addWidget(m_toolbar);
235 // initialize graphic scene
236 m_scene = new GraphicsSceneRectMove(this);
237 graphicsView->setScene(m_scene);
238 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
239 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
241 // a gradient background
242 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
243 gradient->setSpread(QGradient::ReflectSpread);
244 scene->setBackgroundBrush(*gradient);*/
246 m_frameImage = new QGraphicsPixmapItem();
248 qtrans.scale(2.0, 2.0);
249 m_frameImage->setTransform(qtrans);
250 m_frameImage->setZValue(-1200);
251 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
252 displayBackgroundFrame();
253 graphicsView->scene()->addItem(m_frameImage);
255 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
256 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
257 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
258 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
259 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
260 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
261 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
262 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
264 QPen framepen(Qt::DotLine);
265 framepen.setColor(Qt::red);
267 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
268 m_frameBorder->setPen(framepen);
269 m_frameBorder->setZValue(-1100);
270 m_frameBorder->setBrush(Qt::transparent);
271 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
272 graphicsView->scene()->addItem(m_frameBorder);
274 // mbd: load saved settings
277 graphicsView->show();
278 //graphicsView->setRenderHint(QPainter::Antialiasing);
279 graphicsView->setInteractive(true);
280 //graphicsView->resize(400, 300);
281 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
282 //toolBox->setItemEnabled(2, false);
283 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
284 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)));
301 TitleWidget::~TitleWidget()
305 delete m_buttonImage;
306 delete m_buttonCursor;
309 delete m_unicodeAction;
311 delete m_unicodeDialog;
312 delete m_frameBorder;
314 delete m_startViewport;
315 delete m_endViewport;
317 delete m_signalMapper;
321 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
324 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
325 KStandardDirs::makeDir(titlePath);
326 titlePath.append((isClone == false) ? "title" : "clone");
329 while (path.isEmpty() || QFile::exists(path)) {
331 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
333 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
338 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
341 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
342 KStandardDirs::makeDir(titlePath);
343 return titlePath + titleName + ".png";
347 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
352 void TitleWidget::slotTextTool()
354 m_scene->setTool(TITLE_TEXT);
355 showToolbars(TITLE_TEXT);
356 checkButton(TITLE_TEXT);
359 void TitleWidget::slotRectTool()
361 m_scene->setTool(TITLE_RECTANGLE);
362 showToolbars(TITLE_RECTANGLE);
363 checkButton(TITLE_RECTANGLE);
366 void TitleWidget::slotSelectTool()
368 m_scene->setTool(TITLE_SELECT);
370 // Find out which toolbars need to be shown, depending on selected item
371 TITLETOOL t = TITLE_SELECT;
372 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
374 switch (l.at(0)->type()) {
388 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
389 //graphicsView->centerOn(l.at(0));
395 updateCoordinates(l.at(0));
396 updateDimension(l.at(0));
397 updateRotZoom(l.at(0));
400 checkButton(TITLE_SELECT);
403 void TitleWidget::slotImageTool()
405 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
406 if (!url.isEmpty()) {
407 if (url.path().endsWith(".svg")) {
408 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
409 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
410 svg->setZValue(m_count++);
411 svg->setData(Qt::UserRole, url.path());
412 graphicsView->scene()->addItem(svg);
414 QPixmap pix(url.path());
415 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
416 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
417 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
418 image->setData(Qt::UserRole, url.path());
419 image->setZValue(m_count++);
420 graphicsView->scene()->addItem(image);
423 m_scene->setTool(TITLE_SELECT);
424 showToolbars(TITLE_SELECT);
425 checkButton(TITLE_NONE);
428 void TitleWidget::showToolbars(TITLETOOL toolType)
432 rect_properties->setHidden(true);
433 image_properties->setHidden(true);
434 no_properties->setHidden(true);
435 text_properties->setHidden(false);
437 case TITLE_RECTANGLE:
438 image_properties->setHidden(true);
439 no_properties->setHidden(true);
440 text_properties->setHidden(true);
441 rect_properties->setHidden(false);
444 no_properties->setHidden(true);
445 text_properties->setHidden(true);
446 rect_properties->setHidden(true);
447 image_properties->setHidden(false);
450 text_properties->setHidden(true);
451 rect_properties->setHidden(true);
452 image_properties->setHidden(true);
453 no_properties->setHidden(false);
458 void TitleWidget::enableToolbars(TITLETOOL toolType)
460 // TITLETOOL is defined in graphicsscenerectmove.h
465 bool bValue_w = false;
466 bool bValue_h = false;
475 case TITLE_RECTANGLE:
490 frame_properties->setEnabled(bFrame);
491 text_properties->setEnabled(bText);
492 rect_properties->setEnabled(bRect);
493 image_properties->setEnabled(bImage);
494 value_w->setEnabled(bValue_w);
495 value_h->setEnabled(bValue_h);
498 void TitleWidget::checkButton(TITLETOOL toolType)
500 bool bSelect = false;
512 case TITLE_RECTANGLE:
522 m_buttonCursor->setChecked(bSelect);
523 m_buttonText->setChecked(bText);
524 m_buttonRect->setChecked(bRect);
525 m_buttonImage->setChecked(bImage);
528 void TitleWidget::displayBackgroundFrame()
530 if (!displayBg->isChecked()) {
531 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
532 QPixmap pattern(20, 20);
534 QColor bgcolor(210, 210, 210);
537 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
538 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
543 p.fillRect(bg.rect(), br);
545 m_frameImage->setPixmap(bg);
547 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
551 void TitleWidget::initAnimation()
553 align_box->setEnabled(false);
554 QPen startpen(Qt::DotLine);
555 QPen endpen(Qt::DashDotLine);
556 startpen.setColor(QColor(100, 200, 100, 140));
557 endpen.setColor(QColor(200, 100, 100, 140));
559 m_startViewport->setPen(startpen);
560 m_endViewport->setPen(endpen);
562 m_startViewport->setZValue(-1000);
563 m_endViewport->setZValue(-1000);
565 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
566 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
568 graphicsView->scene()->addItem(m_startViewport);
569 graphicsView->scene()->addItem(m_endViewport);
571 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
572 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
573 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
574 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
577 void TitleWidget::slotUpdateZoom(int pos)
579 m_scene->setZoom((double) pos / 100);
580 zoom_label->setText(QString::number(pos) + '%');
583 void TitleWidget::slotZoom(bool up)
585 int pos = zoom_slider->value();
588 zoom_slider->setValue(pos);
591 void TitleWidget::slotAdjustZoom()
593 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
594 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
595 if (scalex > scaley) scalex = scaley;
596 int zoompos = (int)(scalex * 7 + 0.5);*/
597 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
598 int zoompos = graphicsView->matrix().m11() * 100;
599 zoom_slider->setValue(zoompos);
600 graphicsView->centerOn(m_frameBorder);
603 void TitleWidget::slotZoomOneToOne()
605 zoom_slider->setValue(100);
606 graphicsView->centerOn(m_frameBorder);
609 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
611 updateAxisButtons(rect); // back to default
613 QColor f = rectFColor->color();
614 f.setAlpha(rectFAlpha->value());
616 penf.setWidth(rectLineWidth->value());
618 QColor b = rectBColor->color();
619 b.setAlpha(rectBAlpha->value());
620 rect->setBrush(QBrush(b));
621 rect->setZValue(m_count++);
622 //setCurrentItem(rect);
623 //graphicsView->setFocus();
626 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
628 updateAxisButtons(tt); // back to default
630 QFont font = font_family->currentFont();
631 font.setPixelSize(font_size->value());
633 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
634 font.setItalic(buttonItalic->isChecked());
635 font.setUnderline(buttonUnder->isChecked());
638 QColor color = fontColorButton->color();
639 color.setAlpha(textAlpha->value());
640 tt->setDefaultTextColor(color);
641 tt->setZValue(m_count++);
645 void TitleWidget::setFontBoxWeight(int weight)
647 int index = font_weight_box->findData(weight);
649 index = font_weight_box->findData(QFont::Normal);
651 font_weight_box->setCurrentIndex(index);
654 void TitleWidget::setCurrentItem(QGraphicsItem *item)
656 m_scene->setSelectedItem(item);
659 void TitleWidget::zIndexChanged(int v)
661 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
667 void TitleWidget::selectionChanged()
669 if (m_scene->tool() != TITLE_SELECT) return;
670 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
671 //toolBox->setItemEnabled(2, false);
672 //toolBox->setItemEnabled(3, false);
673 value_x->blockSignals(true);
674 value_y->blockSignals(true);
675 value_w->blockSignals(true);
676 value_h->blockSignals(true);
677 itemzoom->blockSignals(true);
678 itemrotate->blockSignals(true);
680 bool blockX = !origin_x_left->signalsBlocked();
681 bool blockY = !origin_y_top->signalsBlocked();
682 if (blockX) origin_x_left->blockSignals(true);
683 if (blockY) origin_y_top->blockSignals(true);
684 origin_x_left->setChecked(false);
685 origin_y_top->setChecked(false);
688 enableToolbars(TITLE_NONE);
689 if (blockX) origin_x_left->blockSignals(false);
690 if (blockY) origin_y_top->blockSignals(false);
691 } else if (l.size() == 1) {
692 if (l.at(0)->type() == TEXTITEM) {
693 showToolbars(TITLE_TEXT);
694 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
695 //if (l[0]->hasFocus())
696 //toolBox->setCurrentIndex(0);
697 //toolBox->setItemEnabled(2, true);
698 font_size->blockSignals(true);
699 font_family->blockSignals(true);
700 font_weight_box->blockSignals(true);
701 buttonItalic->blockSignals(true);
702 buttonUnder->blockSignals(true);
703 fontColorButton->blockSignals(true);
704 textAlpha->blockSignals(true);
705 buttonAlignLeft->blockSignals(true);
706 buttonAlignRight->blockSignals(true);
707 buttonAlignNone->blockSignals(true);
708 buttonAlignCenter->blockSignals(true);
710 QFont font = i->font();
711 font_family->setCurrentFont(font);
712 font_size->setValue(font.pixelSize());
713 buttonItalic->setChecked(font.italic());
714 buttonUnder->setChecked(font.underline());
715 setFontBoxWeight(font.weight());
717 QColor color = i->defaultTextColor();
718 fontColorButton->setColor(color);
719 textAlpha->setValue(color.alpha());
721 QTextCursor cur = i->textCursor();
722 QTextBlockFormat format = cur.blockFormat();
723 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
724 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
725 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
726 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
728 font_size->blockSignals(false);
729 font_family->blockSignals(false);
730 font_weight_box->blockSignals(false);
731 buttonItalic->blockSignals(false);
732 buttonUnder->blockSignals(false);
733 fontColorButton->blockSignals(false);
734 textAlpha->blockSignals(false);
735 buttonAlignLeft->blockSignals(false);
736 buttonAlignRight->blockSignals(false);
737 buttonAlignNone->blockSignals(false);
738 buttonAlignCenter->blockSignals(false);
740 updateAxisButtons(i);
741 updateCoordinates(i);
743 enableToolbars(TITLE_TEXT);
745 } else if ((l.at(0))->type() == RECTITEM) {
746 showToolbars(TITLE_RECTANGLE);
748 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
749 if (rec == m_startViewport || rec == m_endViewport) {
750 /*toolBox->setCurrentIndex(3);
751 toolBox->widget(0)->setEnabled(false);
752 toolBox->widget(1)->setEnabled(false);*/
753 enableToolbars(TITLE_NONE);
756 /*toolBox->widget(0)->setEnabled(true);
757 toolBox->widget(1)->setEnabled(true);
758 toolBox->setCurrentIndex(0);*/
759 //toolBox->setItemEnabled(3, true);
760 rectFAlpha->setValue(rec->pen().color().alpha());
761 rectBAlpha->setValue(rec->brush().color().alpha());
762 //kDebug() << rec->brush().color().alpha();
763 QColor fcol = rec->pen().color();
764 QColor bcol = rec->brush().color();
765 //fcol.setAlpha(255);
766 //bcol.setAlpha(255);
767 rectFColor->setColor(fcol);
768 rectBColor->setColor(bcol);
770 rectLineWidth->setValue(rec->pen().width());
771 enableToolbars(TITLE_RECTANGLE);
774 updateAxisButtons(l.at(0));
775 updateCoordinates(rec);
776 updateDimension(rec);
778 } else if (l.at(0)->type() == IMAGEITEM) {
779 showToolbars(TITLE_IMAGE);
781 updateCoordinates(l.at(0));
782 updateDimension(l.at(0));
784 enableToolbars(TITLE_IMAGE);
787 //toolBox->setCurrentIndex(0);
788 showToolbars(TITLE_NONE);
789 enableToolbars(TITLE_NONE);
790 /*frame_properties->setEnabled(false);
791 text_properties->setEnabled(false);
792 rect_properties->setEnabled(false);*/
794 zValue->setValue((int)l.at(0)->zValue());
795 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
796 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
797 value_x->blockSignals(false);
798 value_y->blockSignals(false);
799 value_w->blockSignals(false);
800 value_h->blockSignals(false);
801 itemzoom->blockSignals(false);
802 itemrotate->blockSignals(false);
806 void TitleWidget::slotValueChanged(int type)
808 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
809 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
814 val = value_w->value();
817 val = value_h->value();
821 QGraphicsItem *i = l.at(0);
822 Transform t = m_transformations.value(i);
824 // Ratio width:height
825 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
826 double alpha = (double) t.rotate / 180.0 * M_PI;
836 // Add 0.5 because otherwise incrementing by 1 might have no effect
837 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
838 scale = length / i->boundingRect().width();
841 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
842 scale = length / i->boundingRect().height();
849 qtrans.scale(scale, scale);
850 qtrans.rotate(t.rotate);
851 i->setTransform(qtrans);
852 m_transformations[i] = t;
859 /** \brief Updates position/size of the selected item when a value
860 * of an item (coordinates, size) has changed */
861 void TitleWidget::slotAdjustSelectedItem()
863 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
865 if (l.at(0)->type() == RECTITEM) {
867 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
869 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
870 } else if (l.at(0)->type() == TEXTITEM) {
872 updatePosition(l.at(0));
873 } else if (l.at(0)->type() == IMAGEITEM) {
875 updatePosition(l.at(0));
880 /** \brief Updates width/height int the text fields, regarding transformation matrix */
881 void TitleWidget::updateDimension(QGraphicsItem *i)
883 value_w->blockSignals(true);
884 value_h->blockSignals(true);
887 if (i->type() == IMAGEITEM) {
888 // Get multipliers for rotation/scaling
890 /*Transform t = m_transformations.value(i);
891 QRectF r = i->boundingRect();
892 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
893 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
894 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
895 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
897 value_w->setValue(i->sceneBoundingRect().width());
898 value_h->setValue(i->sceneBoundingRect().height());
899 } else if (i->type() == RECTITEM) {
900 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
901 value_w->setValue((int) r->rect().width());
902 value_h->setValue((int) r->rect().height());
903 } else if (i->type() == TEXTITEM) {
904 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
905 value_w->setValue((int) t->boundingRect().width());
906 value_h->setValue((int) t->boundingRect().height());
909 value_w->blockSignals(false);
910 value_h->blockSignals(false);
913 /** \brief Updates the coordinates in the text fields from the item */
914 void TitleWidget::updateCoordinates(QGraphicsItem *i)
916 // Block signals emitted by this method
917 value_x->blockSignals(true);
918 value_y->blockSignals(true);
920 if (i->type() == TEXTITEM) {
922 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
924 // Set the correct x coordinate value
925 if (origin_x_left->isChecked()) {
926 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
927 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
929 // Origin is at 0 (default)
930 value_x->setValue((int) rec->pos().x());
934 if (origin_y_top->isChecked()) {
935 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
937 value_y->setValue((int) rec->pos().y());
940 } else if (i->type() == RECTITEM) {
942 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
944 if (origin_x_left->isChecked()) {
945 // Origin (0 point) is at m_frameWidth
946 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
948 // Origin is at 0 (default)
949 value_x->setValue((int) rec->pos().x());
952 if (origin_y_top->isChecked()) {
953 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
955 value_y->setValue((int) rec->pos().y());
958 } else if (i->type() == IMAGEITEM) {
960 if (origin_x_left->isChecked()) {
961 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
963 value_x->setValue((int) i->pos().x());
966 if (origin_y_top->isChecked()) {
967 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
969 value_y->setValue((int) i->pos().y());
974 // Stop blocking signals now
975 value_x->blockSignals(false);
976 value_y->blockSignals(false);
979 void TitleWidget::updateRotZoom(QGraphicsItem *i)
981 itemzoom->blockSignals(true);
982 itemrotate->blockSignals(false);
984 Transform t = m_transformations.value(i);
985 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
986 itemrotate->setValue((int)(t.rotate));
988 itemzoom->blockSignals(false);
989 itemrotate->blockSignals(false);
992 /** \brief Updates the position of an item by reading coordinates from the text fields */
993 void TitleWidget::updatePosition(QGraphicsItem *i)
995 if (i->type() == TEXTITEM) {
996 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
999 if (origin_x_left->isChecked()) {
1000 /* Origin of the x axis is at m_frameWidth,
1001 * and distance from right border of the item to the right
1002 * border of the frame is taken.
1003 * See comment to slotOriginXClicked().
1005 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1007 posX = value_x->value();
1011 if (origin_y_top->isChecked()) {
1012 /* Same for y axis */
1013 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1015 posY = value_y->value();
1018 rec->setPos(posX, posY);
1020 } else if (i->type() == RECTITEM) {
1022 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1025 if (origin_x_left->isChecked()) {
1026 posX = m_frameWidth - value_x->value() - rec->rect().width();
1028 posX = value_x->value();
1032 if (origin_y_top->isChecked()) {
1033 posY = m_frameHeight - value_y->value() - rec->rect().height();
1035 posY = value_y->value();
1038 rec->setPos(posX, posY);
1040 } else if (i->type() == IMAGEITEM) {
1042 if (origin_x_left->isChecked()) {
1043 // Use the sceneBoundingRect because this also regards transformations like zoom
1044 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1046 posX = value_x->value();
1050 if (origin_y_top->isChecked()) {
1051 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1053 posY = value_y->value();
1056 i->setPos(posX, posY);
1062 void TitleWidget::updateTextOriginX()
1064 if (origin_x_left->isChecked()) {
1065 origin_x_left->setText(i18n("\u2212X"));
1067 origin_x_left->setText(i18n("+X"));
1071 void TitleWidget::slotOriginXClicked()
1073 // Update the text displayed on the button.
1074 updateTextOriginX();
1076 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1077 if (l.size() >= 1) {
1078 updateCoordinates(l.at(0));
1080 // Remember x axis setting
1081 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1082 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1084 graphicsView->setFocus();
1087 void TitleWidget::updateTextOriginY()
1089 if (origin_y_top->isChecked()) {
1090 origin_y_top->setText(i18n("\u2212Y"));
1092 origin_y_top->setText(i18n("+Y"));
1096 void TitleWidget::slotOriginYClicked()
1098 // Update the text displayed on the button.
1099 updateTextOriginY();
1101 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1102 if (l.size() >= 1) {
1103 updateCoordinates(l.at(0));
1105 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1106 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1109 graphicsView->setFocus();
1112 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1114 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1115 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1116 origin_x_left->blockSignals(true);
1117 origin_y_top->blockSignals(true);
1119 if (xAxis == TitleDocument::AxisInverted) {
1120 origin_x_left->setChecked(true);
1122 origin_x_left->setChecked(false);
1124 updateTextOriginX();
1126 if (yAxis == TitleDocument::AxisInverted) {
1127 origin_y_top->setChecked(true);
1129 origin_y_top->setChecked(false);
1131 updateTextOriginY();
1133 origin_x_left->blockSignals(false);
1134 origin_y_top->blockSignals(false);
1137 void TitleWidget::slotChangeBackground()
1139 QColor color = kcolorbutton->color();
1140 color.setAlpha(horizontalSlider->value());
1141 m_frameBorder->setBrush(QBrush(color));
1145 * Something (yeah) has changed in our QGraphicsScene.
1147 void TitleWidget::slotChanged()
1149 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1150 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1151 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1156 * If the user has set origin_x_left (everything also for y),
1157 * we need to look whether a text element has been selected. If yes,
1158 * we need to ensure that the right border of the text field
1159 * remains fixed also when some text has been entered.
1161 * This is also known as right-justified, with the difference that
1162 * it is not valid for text but for its boundingRect. Text may still
1163 * be left-justified.
1165 void TitleWidget::textChanged(QGraphicsTextItem *i)
1170 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1172 if (!i->toPlainText().isEmpty()) {
1176 * Don't do anything if the string is empty. If the position
1177 * would be updated here, a newly created text field would
1178 * be set to the position of the last selected text field.
1184 void TitleWidget::slotInsertUnicode()
1186 m_unicodeDialog->exec();
1189 void TitleWidget::slotInsertUnicodeString(QString text)
1191 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1193 if (l.at(0)->type() == TEXTITEM) {
1194 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1195 t->textCursor().insertText(text);
1200 void TitleWidget::slotUpdateText()
1202 QFont font = font_family->currentFont();
1203 font.setPixelSize(font_size->value());
1204 font.setItalic(buttonItalic->isChecked());
1205 font.setUnderline(buttonUnder->isChecked());
1206 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1207 QColor color = fontColorButton->color();
1208 color.setAlpha(textAlpha->value());
1210 QGraphicsTextItem* item = NULL;
1211 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1212 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1213 item = static_cast <QGraphicsTextItem *>(l.at(0));
1216 //if (item->textCursor().selection ().isEmpty())
1217 QTextCursor cur = item->textCursor();
1218 QTextBlockFormat format = cur.blockFormat();
1219 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1220 item->setTextWidth(item->boundingRect().width());
1221 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1222 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1223 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1225 format.setAlignment(Qt::AlignLeft);
1226 item->setTextWidth(-1);
1230 item->setFont(font);
1231 item->setDefaultTextColor(color);
1232 cur.select(QTextCursor::Document);
1233 cur.setBlockFormat(format);
1234 item->setTextCursor(cur);
1235 cur.clearSelection();
1236 item->setTextCursor(cur);
1241 void TitleWidget::rectChanged()
1243 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1244 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1245 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1246 QColor f = rectFColor->color();
1247 f.setAlpha(rectFAlpha->value());
1249 penf.setWidth(rectLineWidth->value());
1251 QColor b = rectBColor->color();
1252 b.setAlpha(rectBAlpha->value());
1253 rec->setBrush(QBrush(b));
1257 void TitleWidget::itemScaled(int val)
1259 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1260 if (l.size() == 1) {
1261 Transform x = m_transformations.value(l.at(0));
1262 x.scalex = (double)val / 100.0;
1263 x.scaley = (double)val / 100.0;
1265 qtrans.scale(x.scalex, x.scaley);
1266 qtrans.rotate(x.rotate);
1267 l[0]->setTransform(qtrans);
1268 m_transformations[l.at(0)] = x;
1269 updateDimension(l.at(0));
1273 void TitleWidget::itemRotate(int val)
1275 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1276 if (l.size() == 1) {
1277 Transform x = m_transformations[l.at(0)];
1278 x.rotate = (double)val;
1280 qtrans.scale(x.scalex, x.scaley);
1281 qtrans.rotate(x.rotate);
1282 l[0]->setTransform(qtrans);
1283 m_transformations[l.at(0)] = x;
1284 updateDimension(l.at(0));
1288 void TitleWidget::itemHCenter()
1290 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1291 if (l.size() == 1) {
1292 QGraphicsItem *item = l.at(0);
1293 QRectF br = item->sceneBoundingRect();
1294 int width = (int)br.width();
1295 int newPos = (int)((m_frameWidth - width) / 2);
1296 newPos += item->pos().x() - br.left(); // Check item transformation
1297 item->setPos(newPos, item->pos().y());
1298 updateCoordinates(item);
1302 void TitleWidget::itemVCenter()
1304 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1305 if (l.size() == 1) {
1306 QGraphicsItem *item = l.at(0);
1307 QRectF br = item->sceneBoundingRect();
1308 int height = (int)br.height();
1309 int newPos = (int)((m_frameHeight - height) / 2);
1310 newPos += item->pos().y() - br.top(); // Check item transformation
1311 item->setPos(item->pos().x(), newPos);
1312 updateCoordinates(item);
1316 void TitleWidget::itemTop()
1318 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1319 if (l.size() == 1) {
1320 QGraphicsItem *item = l.at(0);
1321 QRectF br = item->sceneBoundingRect();
1323 if (br.top() > 0) diff = -br.top();
1324 else diff = -br.bottom();
1325 item->moveBy(0, diff);
1326 updateCoordinates(item);
1330 void TitleWidget::itemBottom()
1332 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1333 if (l.size() == 1) {
1334 QGraphicsItem *item = l.at(0);
1335 QRectF br = item->sceneBoundingRect();
1337 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1338 else diff = m_frameHeight - br.bottom();
1339 item->moveBy(0, diff);
1340 updateCoordinates(item);
1344 void TitleWidget::itemLeft()
1346 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1347 if (l.size() == 1) {
1348 QGraphicsItem *item = l.at(0);
1349 QRectF br = item->sceneBoundingRect();
1351 if (br.left() > 0) diff = -br.left();
1352 else diff = -br.right();
1353 item->moveBy(diff, 0);
1354 updateCoordinates(item);
1358 void TitleWidget::itemRight()
1360 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1361 if (l.size() == 1) {
1362 QGraphicsItem *item = l.at(0);
1363 QRectF br = item->sceneBoundingRect();
1365 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1366 else diff = m_frameWidth - br.left();
1367 item->moveBy(diff, 0);
1368 updateCoordinates(item);
1372 void TitleWidget::setupViewports()
1374 //double aspect_ratio = 4.0 / 3.0;//read from project
1375 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1376 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1377 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1378 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1379 QPolygonF spoly(sp);
1380 QPolygonF epoly(ep);
1381 spoly.translate(startViewportX->value(), startViewportY->value());
1382 epoly.translate(endViewportX->value(), endViewportY->value());
1383 m_startViewport->setPolygon(spoly);
1384 m_endViewport->setPolygon(epoly);
1385 if (! insertingValues) {
1386 m_startViewport->setData(0, startViewportX->value());
1387 m_startViewport->setData(1, startViewportY->value());
1388 m_startViewport->setData(2, startViewportSize->value());
1390 m_endViewport->setData(0, endViewportX->value());
1391 m_endViewport->setData(1, endViewportY->value());
1392 m_endViewport->setData(2, endViewportSize->value());
1396 void TitleWidget::loadTitle(KUrl url)
1398 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1399 if (!url.isEmpty()) {
1400 QList<QGraphicsItem *> items = m_scene->items();
1401 for (int i = 0; i < items.size(); i++) {
1402 if (items.at(i)->zValue() > -1000) delete items.at(i);
1404 m_scene->clearTextSelection();
1408 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1409 QFile file(tmpfile);
1410 if (file.open(QIODevice::ReadOnly)) {
1411 doc.setContent(&file, false);
1414 KIO::NetAccess::removeTempFile(tmpfile);
1419 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1421 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1422 insertingValues = true;
1423 startViewportX->setValue(m_startViewport->data(0).toInt());
1424 startViewportY->setValue(m_startViewport->data(1).toInt());
1425 startViewportSize->setValue(m_startViewport->data(2).toInt());
1426 endViewportX->setValue(m_endViewport->data(0).toInt());
1427 endViewportY->setValue(m_endViewport->data(1).toInt());
1428 endViewportSize->setValue(m_endViewport->data(2).toInt());
1430 insertingValues = false;
1436 void TitleWidget::saveTitle(KUrl url)
1438 if (anim_start->isChecked()) slotAnimStart(false);
1439 if (anim_end->isChecked()) slotAnimEnd(false);
1440 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Save Title"));
1441 if (!url.isEmpty()) {
1442 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1443 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1447 QDomDocument TitleWidget::xml()
1449 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1450 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1451 if (cropImage->isChecked()) {
1452 doc.documentElement().setAttribute("crop", 1);
1457 int TitleWidget::duration() const
1459 return m_tc.getFrameCount(title_duration->text());
1462 void TitleWidget::setXml(QDomDocument doc)
1465 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1467 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1468 /*if (doc.documentElement().hasAttribute("out")) {
1469 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1470 title_duration->setText(m_tc.getTimecode(duration));
1472 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1474 QDomElement e = doc.documentElement();
1475 cropImage->setChecked(e.hasAttribute("crop"));
1476 m_transformations.clear();
1477 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1478 const double PI = 4.0 * atan(1.0);
1479 for (int i = 0; i < items.count(); i++) {
1480 QTransform t = items.at(i)->transform();
1484 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1485 m_transformations[items.at(i)] = x;
1487 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1488 QColor background_color = m_titledocument.getBackgroundColor();
1489 horizontalSlider->blockSignals(true);
1490 kcolorbutton->blockSignals(true);
1491 horizontalSlider->setValue(background_color.alpha());
1492 background_color.setAlpha(255);
1493 kcolorbutton->setColor(background_color);
1494 horizontalSlider->blockSignals(false);
1495 kcolorbutton->blockSignals(false);
1497 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1498 startViewportY->setValue(m_startViewport->data(1).toInt());
1499 startViewportSize->setValue(m_startViewport->data(2).toInt());
1500 endViewportX->setValue(m_endViewport->data(0).toInt());
1501 endViewportY->setValue(m_endViewport->data(1).toInt());
1502 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1504 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1508 const QRect TitleWidget::renderedRect()
1512 int maxX = m_frameWidth;
1513 int maxY = m_frameHeight;
1514 if (!cropImage->isChecked()) {
1515 m_scene->removeItem(m_startViewport);
1516 m_scene->removeItem(m_endViewport);
1517 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1518 if (boundingRect.left() < 0) minX = boundingRect.left();
1519 if (boundingRect.top() < 0) minY = boundingRect.top();
1520 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1521 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1529 QRect rect(minX, minY, maxX, maxY);
1533 QImage TitleWidget::renderedPixmap()
1535 QRect rect = renderedRect();
1536 QImage pix(rect.width(), rect.height(), QImage::Format_ARGB32);
1537 pix.fill(Qt::transparent);
1538 QPainter painter(&pix);
1539 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1540 m_scene->clearTextSelection();
1541 QPen framepen = m_frameBorder->pen();
1542 m_frameBorder->setPen(Qt::NoPen);
1543 m_startViewport->setVisible(false);
1544 m_endViewport->setVisible(false);
1545 m_frameImage->setVisible(false);
1547 m_scene->render(&painter, QRectF(), rect); //QRectF(minX, minY, maxX - minX, maxY - minY));
1549 m_frameBorder->setPen(framepen);
1550 m_startViewport->setVisible(true);
1551 m_endViewport->setVisible(true);
1552 m_frameImage->setVisible(true);
1556 /** \brief Connected to the accepted signal - calls writeChoices */
1557 void TitleWidget::slotAccepted()
1559 if (anim_start->isChecked()) slotAnimStart(false);
1560 if (anim_end->isChecked()) slotAnimEnd(false);
1564 /** \brief Store the current choices of font, background and rect values */
1565 void TitleWidget::writeChoices()
1567 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1568 KSharedConfigPtr config = KGlobal::config();
1569 KConfigGroup titleConfig(config, "TitleWidget");
1570 // Write the entries
1571 titleConfig.writeEntry("font_family", font_family->currentFont());
1572 //titleConfig.writeEntry("font_size", font_size->value());
1573 titleConfig.writeEntry("font_pixel_size", font_size->value());
1574 titleConfig.writeEntry("font_color", fontColorButton->color());
1575 titleConfig.writeEntry("font_alpha", textAlpha->value());
1576 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1577 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1578 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1580 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1581 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1582 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1583 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1584 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1586 titleConfig.writeEntry("background_color", kcolorbutton->color());
1587 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1589 titleConfig.writeEntry("crop_image", cropImage->isChecked());
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()));
1623 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));
1626 void TitleWidget::adjustFrameSize()
1628 m_frameWidth = m_titledocument.frameWidth();
1629 m_frameHeight = m_titledocument.frameHeight();
1630 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1631 displayBackgroundFrame();
1634 void TitleWidget::slotAnimStart(bool anim)
1636 if (anim && anim_end->isChecked()) {
1637 anim_end->setChecked(false);
1638 m_endViewport->setZValue(-1000);
1639 m_endViewport->setBrush(QBrush());
1642 QList<QGraphicsItem *> list = m_scene->items();
1643 for (int i = 0; i < list.count(); i++) {
1644 if (list.at(i)->zValue() > -1000) {
1645 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1646 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1649 align_box->setEnabled(anim);
1650 itemzoom->setEnabled(!anim);
1651 itemrotate->setEnabled(!anim);
1652 frame_toolbar->setEnabled(!anim);
1653 rect_properties->setEnabled(!anim);
1655 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1656 m_startViewport->setZValue(1100);
1657 QColor col = m_startViewport->pen().color();
1659 m_startViewport->setBrush(col);
1660 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1661 m_startViewport->setSelected(true);
1666 m_startViewport->setZValue(-1000);
1667 m_startViewport->setBrush(QBrush());
1668 m_startViewport->setFlags(0);
1673 void TitleWidget::slotAnimEnd(bool anim)
1675 if (anim && anim_start->isChecked()) {
1676 anim_start->setChecked(false);
1677 m_startViewport->setZValue(-1000);
1678 m_startViewport->setBrush(QBrush());
1681 QList<QGraphicsItem *> list = m_scene->items();
1682 for (int i = 0; i < list.count(); i++) {
1683 if (list.at(i)->zValue() > -1000) {
1684 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1685 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1688 align_box->setEnabled(anim);
1689 itemzoom->setEnabled(!anim);
1690 itemrotate->setEnabled(!anim);
1691 frame_toolbar->setEnabled(!anim);
1692 rect_properties->setEnabled(!anim);
1694 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1695 m_endViewport->setZValue(1100);
1696 QColor col = m_endViewport->pen().color();
1698 m_endViewport->setBrush(col);
1699 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1700 m_endViewport->setSelected(true);
1705 m_endViewport->setZValue(-1000);
1706 m_endViewport->setBrush(QBrush());
1707 m_endViewport->setFlags(0);
1711 void TitleWidget::slotKeepAspect(bool keep)
1713 if (m_endViewport->zValue() == 1100) {
1714 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1715 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1718 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1719 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1723 void TitleWidget::slotResize50()
1725 if (m_endViewport->zValue() == 1100) {
1726 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1728 else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1731 void TitleWidget::slotResize100()
1733 if (m_endViewport->zValue() == 1100) {
1734 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1736 else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1739 void TitleWidget::slotResize200()
1741 if (m_endViewport->zValue() == 1100) {
1742 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1744 else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);