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(0);
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(0);
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));
285 m_startViewport->setData(0, m_frameWidth);
286 m_startViewport->setData(1, m_frameHeight);
287 m_endViewport->setData(0, m_frameWidth);
288 m_endViewport->setData(1, m_frameHeight);
290 if (!url.isEmpty()) loadTitle(url);
293 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
296 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
297 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
300 TitleWidget::~TitleWidget()
304 delete m_buttonImage;
305 delete m_buttonCursor;
308 delete m_unicodeAction;
310 delete m_unicodeDialog;
311 delete m_frameBorder;
313 delete m_startViewport;
314 delete m_endViewport;
316 delete m_signalMapper;
320 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
323 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
324 KStandardDirs::makeDir(titlePath);
325 titlePath.append((isClone == false) ? "title" : "clone");
328 while (path.isEmpty() || QFile::exists(path)) {
330 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
332 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
337 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
340 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
341 KStandardDirs::makeDir(titlePath);
342 return titlePath + titleName + ".png";
346 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
351 void TitleWidget::slotTextTool()
353 m_scene->setTool(TITLE_TEXT);
354 showToolbars(TITLE_TEXT);
355 checkButton(TITLE_TEXT);
358 void TitleWidget::slotRectTool()
360 m_scene->setTool(TITLE_RECTANGLE);
361 showToolbars(TITLE_RECTANGLE);
362 checkButton(TITLE_RECTANGLE);
365 void TitleWidget::slotSelectTool()
367 m_scene->setTool(TITLE_SELECT);
369 // Find out which toolbars need to be shown, depending on selected item
370 TITLETOOL t = TITLE_SELECT;
371 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
373 switch (l.at(0)->type()) {
387 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
388 //graphicsView->centerOn(l.at(0));
394 updateCoordinates(l.at(0));
395 updateDimension(l.at(0));
396 updateRotZoom(l.at(0));
399 checkButton(TITLE_SELECT);
402 void TitleWidget::slotImageTool()
404 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
405 if (!url.isEmpty()) {
406 if (url.path().endsWith(".svg")) {
407 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
408 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
409 svg->setZValue(m_count++);
410 svg->setData(Qt::UserRole, url.path());
411 graphicsView->scene()->addItem(svg);
413 QPixmap pix(url.path());
414 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
415 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
416 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
417 image->setData(Qt::UserRole, url.path());
418 image->setZValue(m_count++);
419 graphicsView->scene()->addItem(image);
422 m_scene->setTool(TITLE_SELECT);
423 showToolbars(TITLE_SELECT);
424 checkButton(TITLE_NONE);
427 void TitleWidget::showToolbars(TITLETOOL toolType)
431 rect_properties->setHidden(true);
432 image_properties->setHidden(true);
433 no_properties->setHidden(true);
434 text_properties->setHidden(false);
436 case TITLE_RECTANGLE:
437 image_properties->setHidden(true);
438 no_properties->setHidden(true);
439 text_properties->setHidden(true);
440 rect_properties->setHidden(false);
443 no_properties->setHidden(true);
444 text_properties->setHidden(true);
445 rect_properties->setHidden(true);
446 image_properties->setHidden(false);
449 text_properties->setHidden(true);
450 rect_properties->setHidden(true);
451 image_properties->setHidden(true);
452 no_properties->setHidden(false);
457 void TitleWidget::enableToolbars(TITLETOOL toolType)
459 // TITLETOOL is defined in graphicsscenerectmove.h
464 bool bValue_w = false;
465 bool bValue_h = false;
474 case TITLE_RECTANGLE:
489 frame_properties->setEnabled(bFrame);
490 text_properties->setEnabled(bText);
491 rect_properties->setEnabled(bRect);
492 image_properties->setEnabled(bImage);
493 value_w->setEnabled(bValue_w);
494 value_h->setEnabled(bValue_h);
497 void TitleWidget::checkButton(TITLETOOL toolType)
499 bool bSelect = false;
511 case TITLE_RECTANGLE:
521 m_buttonCursor->setChecked(bSelect);
522 m_buttonText->setChecked(bText);
523 m_buttonRect->setChecked(bRect);
524 m_buttonImage->setChecked(bImage);
527 void TitleWidget::displayBackgroundFrame()
529 if (!displayBg->isChecked()) {
530 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
531 QPixmap pattern(20, 20);
533 QColor bgcolor(210, 210, 210);
536 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
537 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
542 p.fillRect(bg.rect(), br);
544 m_frameImage->setPixmap(bg);
546 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
550 void TitleWidget::initAnimation()
552 align_box->setEnabled(false);
553 QPen startpen(Qt::DotLine);
554 QPen endpen(Qt::DashDotLine);
555 startpen.setColor(QColor(100, 200, 100, 140));
556 endpen.setColor(QColor(200, 100, 100, 140));
558 m_startViewport->setPen(startpen);
559 m_endViewport->setPen(endpen);
561 m_startViewport->setZValue(-1000);
562 m_endViewport->setZValue(-1000);
564 m_startViewport->setFlags(0);
565 m_endViewport->setFlags(0);
567 graphicsView->scene()->addItem(m_startViewport);
568 graphicsView->scene()->addItem(m_endViewport);
570 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
571 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
572 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
573 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
576 void TitleWidget::slotUpdateZoom(int pos)
578 m_scene->setZoom((double) pos / 100);
579 zoom_label->setText(QString::number(pos) + '%');
582 void TitleWidget::slotZoom(bool up)
584 int pos = zoom_slider->value();
587 zoom_slider->setValue(pos);
590 void TitleWidget::slotAdjustZoom()
592 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
593 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
594 if (scalex > scaley) scalex = scaley;
595 int zoompos = (int)(scalex * 7 + 0.5);*/
596 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
597 int zoompos = graphicsView->matrix().m11() * 100;
598 zoom_slider->setValue(zoompos);
599 graphicsView->centerOn(m_frameBorder);
602 void TitleWidget::slotZoomOneToOne()
604 zoom_slider->setValue(100);
605 graphicsView->centerOn(m_frameBorder);
608 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
610 updateAxisButtons(rect); // back to default
612 QColor f = rectFColor->color();
613 f.setAlpha(rectFAlpha->value());
615 penf.setWidth(rectLineWidth->value());
617 QColor b = rectBColor->color();
618 b.setAlpha(rectBAlpha->value());
619 rect->setBrush(QBrush(b));
620 rect->setZValue(m_count++);
621 //setCurrentItem(rect);
622 //graphicsView->setFocus();
625 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
627 updateAxisButtons(tt); // back to default
629 QFont font = font_family->currentFont();
630 font.setPixelSize(font_size->value());
632 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
633 font.setItalic(buttonItalic->isChecked());
634 font.setUnderline(buttonUnder->isChecked());
637 QColor color = fontColorButton->color();
638 color.setAlpha(textAlpha->value());
639 tt->setDefaultTextColor(color);
640 tt->setZValue(m_count++);
644 void TitleWidget::setFontBoxWeight(int weight)
646 int index = font_weight_box->findData(weight);
648 index = font_weight_box->findData(QFont::Normal);
650 font_weight_box->setCurrentIndex(index);
653 void TitleWidget::setCurrentItem(QGraphicsItem *item)
655 m_scene->setSelectedItem(item);
658 void TitleWidget::zIndexChanged(int v)
660 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
666 void TitleWidget::selectionChanged()
668 if (m_scene->tool() != TITLE_SELECT) return;
669 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
670 //toolBox->setItemEnabled(2, false);
671 //toolBox->setItemEnabled(3, false);
672 value_x->blockSignals(true);
673 value_y->blockSignals(true);
674 value_w->blockSignals(true);
675 value_h->blockSignals(true);
676 itemzoom->blockSignals(true);
677 itemrotate->blockSignals(true);
679 bool blockX = !origin_x_left->signalsBlocked();
680 bool blockY = !origin_y_top->signalsBlocked();
681 if (blockX) origin_x_left->blockSignals(true);
682 if (blockY) origin_y_top->blockSignals(true);
683 origin_x_left->setChecked(false);
684 origin_y_top->setChecked(false);
687 enableToolbars(TITLE_NONE);
688 if (blockX) origin_x_left->blockSignals(false);
689 if (blockY) origin_y_top->blockSignals(false);
690 itemzoom->setEnabled(false);
691 itemrotate->setEnabled(false);
692 } else if (l.size() == 1) {
693 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
694 itemzoom->setEnabled(true);
695 itemrotate->setEnabled(true);
697 itemzoom->setEnabled(false);
698 itemrotate->setEnabled(false);
700 if (l.at(0)->type() == TEXTITEM) {
701 showToolbars(TITLE_TEXT);
702 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
703 //if (l[0]->hasFocus())
704 //toolBox->setCurrentIndex(0);
705 //toolBox->setItemEnabled(2, true);
706 font_size->blockSignals(true);
707 font_family->blockSignals(true);
708 font_weight_box->blockSignals(true);
709 buttonItalic->blockSignals(true);
710 buttonUnder->blockSignals(true);
711 fontColorButton->blockSignals(true);
712 textAlpha->blockSignals(true);
713 buttonAlignLeft->blockSignals(true);
714 buttonAlignRight->blockSignals(true);
715 buttonAlignNone->blockSignals(true);
716 buttonAlignCenter->blockSignals(true);
718 QFont font = i->font();
719 font_family->setCurrentFont(font);
720 font_size->setValue(font.pixelSize());
721 buttonItalic->setChecked(font.italic());
722 buttonUnder->setChecked(font.underline());
723 setFontBoxWeight(font.weight());
725 QColor color = i->defaultTextColor();
726 fontColorButton->setColor(color);
727 textAlpha->setValue(color.alpha());
729 QTextCursor cur = i->textCursor();
730 QTextBlockFormat format = cur.blockFormat();
731 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
732 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
733 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
734 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
736 font_size->blockSignals(false);
737 font_family->blockSignals(false);
738 font_weight_box->blockSignals(false);
739 buttonItalic->blockSignals(false);
740 buttonUnder->blockSignals(false);
741 fontColorButton->blockSignals(false);
742 textAlpha->blockSignals(false);
743 buttonAlignLeft->blockSignals(false);
744 buttonAlignRight->blockSignals(false);
745 buttonAlignNone->blockSignals(false);
746 buttonAlignCenter->blockSignals(false);
748 updateAxisButtons(i);
749 updateCoordinates(i);
751 enableToolbars(TITLE_TEXT);
753 } else if ((l.at(0))->type() == RECTITEM) {
754 showToolbars(TITLE_RECTANGLE);
756 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
757 if (rec == m_startViewport || rec == m_endViewport) {
758 /*toolBox->setCurrentIndex(3);
759 toolBox->widget(0)->setEnabled(false);
760 toolBox->widget(1)->setEnabled(false);*/
761 enableToolbars(TITLE_NONE);
763 /*toolBox->widget(0)->setEnabled(true);
764 toolBox->widget(1)->setEnabled(true);
765 toolBox->setCurrentIndex(0);*/
766 //toolBox->setItemEnabled(3, true);
767 rectFAlpha->setValue(rec->pen().color().alpha());
768 rectBAlpha->setValue(rec->brush().color().alpha());
769 //kDebug() << rec->brush().color().alpha();
770 QColor fcol = rec->pen().color();
771 QColor bcol = rec->brush().color();
772 //fcol.setAlpha(255);
773 //bcol.setAlpha(255);
774 rectFColor->setColor(fcol);
775 rectBColor->setColor(bcol);
777 rectLineWidth->setValue(rec->pen().width());
778 enableToolbars(TITLE_RECTANGLE);
781 updateAxisButtons(l.at(0));
782 updateCoordinates(rec);
783 updateDimension(rec);
785 } else if (l.at(0)->type() == IMAGEITEM) {
786 showToolbars(TITLE_IMAGE);
788 updateCoordinates(l.at(0));
789 updateDimension(l.at(0));
791 enableToolbars(TITLE_IMAGE);
794 //toolBox->setCurrentIndex(0);
795 showToolbars(TITLE_NONE);
796 enableToolbars(TITLE_NONE);
797 /*frame_properties->setEnabled(false);
798 text_properties->setEnabled(false);
799 rect_properties->setEnabled(false);*/
801 zValue->setValue((int)l.at(0)->zValue());
802 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
803 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
804 value_x->blockSignals(false);
805 value_y->blockSignals(false);
806 value_w->blockSignals(false);
807 value_h->blockSignals(false);
808 itemzoom->blockSignals(false);
809 itemrotate->blockSignals(false);
813 void TitleWidget::slotValueChanged(int type)
815 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
816 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
821 val = value_w->value();
824 val = value_h->value();
828 QGraphicsItem *i = l.at(0);
829 Transform t = m_transformations.value(i);
831 // Ratio width:height
832 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
833 double alpha = (double) t.rotate / 180.0 * M_PI;
843 // Add 0.5 because otherwise incrementing by 1 might have no effect
844 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
845 scale = length / i->boundingRect().width();
848 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
849 scale = length / i->boundingRect().height();
856 qtrans.scale(scale, scale);
857 qtrans.rotate(t.rotate);
858 i->setTransform(qtrans);
859 m_transformations[i] = t;
866 /** \brief Updates position/size of the selected item when a value
867 * of an item (coordinates, size) has changed */
868 void TitleWidget::slotAdjustSelectedItem()
870 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
872 if (l.at(0)->type() == RECTITEM) {
874 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
876 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
877 } else if (l.at(0)->type() == TEXTITEM) {
879 updatePosition(l.at(0));
880 } else if (l.at(0)->type() == IMAGEITEM) {
882 updatePosition(l.at(0));
887 /** \brief Updates width/height int the text fields, regarding transformation matrix */
888 void TitleWidget::updateDimension(QGraphicsItem *i)
890 value_w->blockSignals(true);
891 value_h->blockSignals(true);
894 if (i->type() == IMAGEITEM) {
895 // Get multipliers for rotation/scaling
897 /*Transform t = m_transformations.value(i);
898 QRectF r = i->boundingRect();
899 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
900 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
901 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
902 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
904 value_w->setValue(i->sceneBoundingRect().width());
905 value_h->setValue(i->sceneBoundingRect().height());
906 } else if (i->type() == RECTITEM) {
907 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
908 value_w->setValue((int) r->rect().width());
909 value_h->setValue((int) r->rect().height());
910 } else if (i->type() == TEXTITEM) {
911 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
912 value_w->setValue((int) t->boundingRect().width());
913 value_h->setValue((int) t->boundingRect().height());
916 value_w->blockSignals(false);
917 value_h->blockSignals(false);
920 /** \brief Updates the coordinates in the text fields from the item */
921 void TitleWidget::updateCoordinates(QGraphicsItem *i)
923 // Block signals emitted by this method
924 value_x->blockSignals(true);
925 value_y->blockSignals(true);
927 if (i->type() == TEXTITEM) {
929 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
931 // Set the correct x coordinate value
932 if (origin_x_left->isChecked()) {
933 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
934 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
936 // Origin is at 0 (default)
937 value_x->setValue((int) rec->pos().x());
941 if (origin_y_top->isChecked()) {
942 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
944 value_y->setValue((int) rec->pos().y());
947 } else if (i->type() == RECTITEM) {
949 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
951 if (origin_x_left->isChecked()) {
952 // Origin (0 point) is at m_frameWidth
953 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
955 // Origin is at 0 (default)
956 value_x->setValue((int) rec->pos().x());
959 if (origin_y_top->isChecked()) {
960 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
962 value_y->setValue((int) rec->pos().y());
965 } else if (i->type() == IMAGEITEM) {
967 if (origin_x_left->isChecked()) {
968 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
970 value_x->setValue((int) i->pos().x());
973 if (origin_y_top->isChecked()) {
974 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
976 value_y->setValue((int) i->pos().y());
981 // Stop blocking signals now
982 value_x->blockSignals(false);
983 value_y->blockSignals(false);
986 void TitleWidget::updateRotZoom(QGraphicsItem *i)
988 itemzoom->blockSignals(true);
989 itemrotate->blockSignals(false);
991 Transform t = m_transformations.value(i);
992 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
993 itemrotate->setValue((int)(t.rotate));
995 itemzoom->blockSignals(false);
996 itemrotate->blockSignals(false);
999 /** \brief Updates the position of an item by reading coordinates from the text fields */
1000 void TitleWidget::updatePosition(QGraphicsItem *i)
1002 if (i->type() == TEXTITEM) {
1003 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1006 if (origin_x_left->isChecked()) {
1007 /* Origin of the x axis is at m_frameWidth,
1008 * and distance from right border of the item to the right
1009 * border of the frame is taken.
1010 * See comment to slotOriginXClicked().
1012 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1014 posX = value_x->value();
1018 if (origin_y_top->isChecked()) {
1019 /* Same for y axis */
1020 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1022 posY = value_y->value();
1025 rec->setPos(posX, posY);
1027 } else if (i->type() == RECTITEM) {
1029 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1032 if (origin_x_left->isChecked()) {
1033 posX = m_frameWidth - value_x->value() - rec->rect().width();
1035 posX = value_x->value();
1039 if (origin_y_top->isChecked()) {
1040 posY = m_frameHeight - value_y->value() - rec->rect().height();
1042 posY = value_y->value();
1045 rec->setPos(posX, posY);
1047 } else if (i->type() == IMAGEITEM) {
1049 if (origin_x_left->isChecked()) {
1050 // Use the sceneBoundingRect because this also regards transformations like zoom
1051 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1053 posX = value_x->value();
1057 if (origin_y_top->isChecked()) {
1058 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1060 posY = value_y->value();
1063 i->setPos(posX, posY);
1069 void TitleWidget::updateTextOriginX()
1071 if (origin_x_left->isChecked()) {
1072 origin_x_left->setText(i18n("\u2212X"));
1074 origin_x_left->setText(i18n("+X"));
1078 void TitleWidget::slotOriginXClicked()
1080 // Update the text displayed on the button.
1081 updateTextOriginX();
1083 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1084 if (l.size() >= 1) {
1085 updateCoordinates(l.at(0));
1087 // Remember x axis setting
1088 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1089 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1091 graphicsView->setFocus();
1094 void TitleWidget::updateTextOriginY()
1096 if (origin_y_top->isChecked()) {
1097 origin_y_top->setText(i18n("\u2212Y"));
1099 origin_y_top->setText(i18n("+Y"));
1103 void TitleWidget::slotOriginYClicked()
1105 // Update the text displayed on the button.
1106 updateTextOriginY();
1108 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1109 if (l.size() >= 1) {
1110 updateCoordinates(l.at(0));
1112 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1113 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1116 graphicsView->setFocus();
1119 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1121 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1122 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1123 origin_x_left->blockSignals(true);
1124 origin_y_top->blockSignals(true);
1126 if (xAxis == TitleDocument::AxisInverted) {
1127 origin_x_left->setChecked(true);
1129 origin_x_left->setChecked(false);
1131 updateTextOriginX();
1133 if (yAxis == TitleDocument::AxisInverted) {
1134 origin_y_top->setChecked(true);
1136 origin_y_top->setChecked(false);
1138 updateTextOriginY();
1140 origin_x_left->blockSignals(false);
1141 origin_y_top->blockSignals(false);
1144 void TitleWidget::slotChangeBackground()
1146 QColor color = kcolorbutton->color();
1147 color.setAlpha(horizontalSlider->value());
1148 m_frameBorder->setBrush(QBrush(color));
1152 * Something (yeah) has changed in our QGraphicsScene.
1154 void TitleWidget::slotChanged()
1156 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1157 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1158 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1163 * If the user has set origin_x_left (everything also for y),
1164 * we need to look whether a text element has been selected. If yes,
1165 * we need to ensure that the right border of the text field
1166 * remains fixed also when some text has been entered.
1168 * This is also known as right-justified, with the difference that
1169 * it is not valid for text but for its boundingRect. Text may still
1170 * be left-justified.
1172 void TitleWidget::textChanged(QGraphicsTextItem *i)
1177 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1179 if (!i->toPlainText().isEmpty()) {
1183 * Don't do anything if the string is empty. If the position
1184 * would be updated here, a newly created text field would
1185 * be set to the position of the last selected text field.
1191 void TitleWidget::slotInsertUnicode()
1193 m_unicodeDialog->exec();
1196 void TitleWidget::slotInsertUnicodeString(QString text)
1198 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1200 if (l.at(0)->type() == TEXTITEM) {
1201 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1202 t->textCursor().insertText(text);
1207 void TitleWidget::slotUpdateText()
1209 QFont font = font_family->currentFont();
1210 font.setPixelSize(font_size->value());
1211 font.setItalic(buttonItalic->isChecked());
1212 font.setUnderline(buttonUnder->isChecked());
1213 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1214 QColor color = fontColorButton->color();
1215 color.setAlpha(textAlpha->value());
1217 QGraphicsTextItem* item = NULL;
1218 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1219 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1220 item = static_cast <QGraphicsTextItem *>(l.at(0));
1223 //if (item->textCursor().selection ().isEmpty())
1224 QTextCursor cur = item->textCursor();
1225 QTextBlockFormat format = cur.blockFormat();
1226 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1227 item->setTextWidth(item->boundingRect().width());
1228 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1229 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1230 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1232 format.setAlignment(Qt::AlignLeft);
1233 item->setTextWidth(-1);
1237 item->setFont(font);
1238 item->setDefaultTextColor(color);
1239 cur.select(QTextCursor::Document);
1240 cur.setBlockFormat(format);
1241 item->setTextCursor(cur);
1242 cur.clearSelection();
1243 item->setTextCursor(cur);
1248 void TitleWidget::rectChanged()
1250 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1251 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1252 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1253 QColor f = rectFColor->color();
1254 f.setAlpha(rectFAlpha->value());
1256 penf.setWidth(rectLineWidth->value());
1258 QColor b = rectBColor->color();
1259 b.setAlpha(rectBAlpha->value());
1260 rec->setBrush(QBrush(b));
1264 void TitleWidget::itemScaled(int val)
1266 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1267 if (l.size() == 1) {
1268 Transform x = m_transformations.value(l.at(0));
1269 x.scalex = (double)val / 100.0;
1270 x.scaley = (double)val / 100.0;
1272 qtrans.scale(x.scalex, x.scaley);
1273 qtrans.rotate(x.rotate);
1274 l[0]->setTransform(qtrans);
1275 m_transformations[l.at(0)] = x;
1276 updateDimension(l.at(0));
1280 void TitleWidget::itemRotate(int val)
1282 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1283 if (l.size() == 1) {
1284 Transform x = m_transformations[l.at(0)];
1285 x.rotate = (double)val;
1287 qtrans.scale(x.scalex, x.scaley);
1288 qtrans.rotate(x.rotate);
1289 l[0]->setTransform(qtrans);
1290 m_transformations[l.at(0)] = x;
1291 updateDimension(l.at(0));
1295 void TitleWidget::itemHCenter()
1297 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1298 if (l.size() == 1) {
1299 QGraphicsItem *item = l.at(0);
1300 QRectF br = item->sceneBoundingRect();
1301 int width = (int)br.width();
1302 int newPos = (int)((m_frameWidth - width) / 2);
1303 newPos += item->pos().x() - br.left(); // Check item transformation
1304 item->setPos(newPos, item->pos().y());
1305 updateCoordinates(item);
1309 void TitleWidget::itemVCenter()
1311 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1312 if (l.size() == 1) {
1313 QGraphicsItem *item = l.at(0);
1314 QRectF br = item->sceneBoundingRect();
1315 int height = (int)br.height();
1316 int newPos = (int)((m_frameHeight - height) / 2);
1317 newPos += item->pos().y() - br.top(); // Check item transformation
1318 item->setPos(item->pos().x(), newPos);
1319 updateCoordinates(item);
1323 void TitleWidget::itemTop()
1325 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1326 if (l.size() == 1) {
1327 QGraphicsItem *item = l.at(0);
1328 QRectF br = item->sceneBoundingRect();
1330 if (br.top() > 0) diff = -br.top();
1331 else diff = -br.bottom();
1332 item->moveBy(0, diff);
1333 updateCoordinates(item);
1337 void TitleWidget::itemBottom()
1339 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1340 if (l.size() == 1) {
1341 QGraphicsItem *item = l.at(0);
1342 QRectF br = item->sceneBoundingRect();
1344 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1345 else diff = m_frameHeight - br.bottom();
1346 item->moveBy(0, diff);
1347 updateCoordinates(item);
1351 void TitleWidget::itemLeft()
1353 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1354 if (l.size() == 1) {
1355 QGraphicsItem *item = l.at(0);
1356 QRectF br = item->sceneBoundingRect();
1358 if (br.left() > 0) diff = -br.left();
1359 else diff = -br.right();
1360 item->moveBy(diff, 0);
1361 updateCoordinates(item);
1365 void TitleWidget::itemRight()
1367 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1368 if (l.size() == 1) {
1369 QGraphicsItem *item = l.at(0);
1370 QRectF br = item->sceneBoundingRect();
1372 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1373 else diff = m_frameWidth - br.left();
1374 item->moveBy(diff, 0);
1375 updateCoordinates(item);
1379 void TitleWidget::setupViewports()
1381 //double aspect_ratio = 4.0 / 3.0;//read from project
1382 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1383 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1384 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1385 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1386 QPolygonF spoly(sp);
1387 QPolygonF epoly(ep);
1388 spoly.translate(startViewportX->value(), startViewportY->value());
1389 epoly.translate(endViewportX->value(), endViewportY->value());
1390 m_startViewport->setPolygon(spoly);
1391 m_endViewport->setPolygon(epoly);
1392 if (! insertingValues) {
1393 m_startViewport->setData(0, startViewportX->value());
1394 m_startViewport->setData(1, startViewportY->value());
1395 m_startViewport->setData(2, startViewportSize->value());
1397 m_endViewport->setData(0, endViewportX->value());
1398 m_endViewport->setData(1, endViewportY->value());
1399 m_endViewport->setData(2, endViewportSize->value());
1403 void TitleWidget::loadTitle(KUrl url)
1405 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1406 if (!url.isEmpty()) {
1407 QList<QGraphicsItem *> items = m_scene->items();
1408 for (int i = 0; i < items.size(); i++) {
1409 if (items.at(i)->zValue() > -1000) delete items.at(i);
1411 m_scene->clearTextSelection();
1415 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1416 QFile file(tmpfile);
1417 if (file.open(QIODevice::ReadOnly)) {
1418 doc.setContent(&file, false);
1421 KIO::NetAccess::removeTempFile(tmpfile);
1426 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1428 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1429 insertingValues = true;
1430 startViewportX->setValue(m_startViewport->data(0).toInt());
1431 startViewportY->setValue(m_startViewport->data(1).toInt());
1432 startViewportSize->setValue(m_startViewport->data(2).toInt());
1433 endViewportX->setValue(m_endViewport->data(0).toInt());
1434 endViewportY->setValue(m_endViewport->data(1).toInt());
1435 endViewportSize->setValue(m_endViewport->data(2).toInt());
1437 insertingValues = false;
1443 void TitleWidget::saveTitle(KUrl url)
1445 if (anim_start->isChecked()) slotAnimStart(false);
1446 if (anim_end->isChecked()) slotAnimEnd(false);
1447 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Save Title"));
1448 if (!url.isEmpty()) {
1449 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1450 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1454 QDomDocument TitleWidget::xml()
1456 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1457 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1458 if (cropImage->isChecked()) {
1459 doc.documentElement().setAttribute("crop", 1);
1464 int TitleWidget::duration() const
1466 return m_tc.getFrameCount(title_duration->text());
1469 void TitleWidget::setXml(QDomDocument doc)
1472 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1474 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1475 /*if (doc.documentElement().hasAttribute("out")) {
1476 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1477 title_duration->setText(m_tc.getTimecode(duration));
1479 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1481 QDomElement e = doc.documentElement();
1482 cropImage->setChecked(e.hasAttribute("crop"));
1483 m_transformations.clear();
1484 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1485 const double PI = 4.0 * atan(1.0);
1486 for (int i = 0; i < items.count(); i++) {
1487 QTransform t = items.at(i)->transform();
1491 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1492 m_transformations[items.at(i)] = x;
1494 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1495 QColor background_color = m_titledocument.getBackgroundColor();
1496 horizontalSlider->blockSignals(true);
1497 kcolorbutton->blockSignals(true);
1498 horizontalSlider->setValue(background_color.alpha());
1499 background_color.setAlpha(255);
1500 kcolorbutton->setColor(background_color);
1501 horizontalSlider->blockSignals(false);
1502 kcolorbutton->blockSignals(false);
1504 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1505 startViewportY->setValue(m_startViewport->data(1).toInt());
1506 startViewportSize->setValue(m_startViewport->data(2).toInt());
1507 endViewportX->setValue(m_endViewport->data(0).toInt());
1508 endViewportY->setValue(m_endViewport->data(1).toInt());
1509 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1511 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1515 const QRect TitleWidget::renderedRect()
1519 int maxX = m_frameWidth;
1520 int maxY = m_frameHeight;
1521 if (!cropImage->isChecked()) {
1522 m_scene->removeItem(m_startViewport);
1523 m_scene->removeItem(m_endViewport);
1524 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1525 if (boundingRect.left() < 0) minX = boundingRect.left();
1526 if (boundingRect.top() < 0) minY = boundingRect.top();
1527 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1528 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1536 QRect rect(minX, minY, maxX, maxY);
1540 QImage TitleWidget::renderedPixmap()
1542 QRect rect = renderedRect();
1543 QImage pix(rect.width(), rect.height(), QImage::Format_ARGB32);
1544 pix.fill(Qt::transparent);
1545 QPainter painter(&pix);
1546 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1547 m_scene->clearTextSelection();
1548 QPen framepen = m_frameBorder->pen();
1549 m_frameBorder->setPen(Qt::NoPen);
1550 m_startViewport->setVisible(false);
1551 m_endViewport->setVisible(false);
1552 m_frameImage->setVisible(false);
1554 m_scene->render(&painter, QRectF(), rect); //QRectF(minX, minY, maxX - minX, maxY - minY));
1556 m_frameBorder->setPen(framepen);
1557 m_startViewport->setVisible(true);
1558 m_endViewport->setVisible(true);
1559 m_frameImage->setVisible(true);
1563 /** \brief Connected to the accepted signal - calls writeChoices */
1564 void TitleWidget::slotAccepted()
1566 if (anim_start->isChecked()) slotAnimStart(false);
1567 if (anim_end->isChecked()) slotAnimEnd(false);
1571 /** \brief Store the current choices of font, background and rect values */
1572 void TitleWidget::writeChoices()
1574 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1575 KSharedConfigPtr config = KGlobal::config();
1576 KConfigGroup titleConfig(config, "TitleWidget");
1577 // Write the entries
1578 titleConfig.writeEntry("font_family", font_family->currentFont());
1579 //titleConfig.writeEntry("font_size", font_size->value());
1580 titleConfig.writeEntry("font_pixel_size", font_size->value());
1581 titleConfig.writeEntry("font_color", fontColorButton->color());
1582 titleConfig.writeEntry("font_alpha", textAlpha->value());
1583 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1584 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1585 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1587 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1588 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1589 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1590 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1591 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1593 titleConfig.writeEntry("background_color", kcolorbutton->color());
1594 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1596 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1598 //! \todo Not sure if I should sync - it is probably safe to do it
1603 /** \brief Read the last stored choices into the dialog */
1604 void TitleWidget::readChoices()
1606 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1607 KSharedConfigPtr config = KGlobal::config();
1608 KConfigGroup titleConfig(config, "TitleWidget");
1610 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1611 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1612 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1613 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1615 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1616 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1617 setFontBoxWeight(weight);
1618 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1619 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1621 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1622 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1623 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1624 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1625 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1627 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1628 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1630 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));
1633 void TitleWidget::adjustFrameSize()
1635 m_frameWidth = m_titledocument.frameWidth();
1636 m_frameHeight = m_titledocument.frameHeight();
1637 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1638 displayBackgroundFrame();
1641 void TitleWidget::slotAnimStart(bool anim)
1643 if (anim && anim_end->isChecked()) {
1644 anim_end->setChecked(false);
1645 m_endViewport->setZValue(-1000);
1646 m_endViewport->setBrush(QBrush());
1649 QList<QGraphicsItem *> list = m_scene->items();
1650 for (int i = 0; i < list.count(); i++) {
1651 if (list.at(i)->zValue() > -1000) {
1652 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1653 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1656 align_box->setEnabled(anim);
1657 itemzoom->setEnabled(!anim);
1658 itemrotate->setEnabled(!anim);
1659 frame_toolbar->setEnabled(!anim);
1660 rect_properties->setEnabled(!anim);
1662 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1663 m_startViewport->setZValue(1100);
1664 QColor col = m_startViewport->pen().color();
1666 m_startViewport->setBrush(col);
1667 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1668 m_startViewport->setSelected(true);
1672 m_startViewport->setZValue(-1000);
1673 m_startViewport->setBrush(QBrush());
1674 m_startViewport->setFlags(0);
1679 void TitleWidget::slotAnimEnd(bool anim)
1681 if (anim && anim_start->isChecked()) {
1682 anim_start->setChecked(false);
1683 m_startViewport->setZValue(-1000);
1684 m_startViewport->setBrush(QBrush());
1687 QList<QGraphicsItem *> list = m_scene->items();
1688 for (int i = 0; i < list.count(); i++) {
1689 if (list.at(i)->zValue() > -1000) {
1690 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1691 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1694 align_box->setEnabled(anim);
1695 itemzoom->setEnabled(!anim);
1696 itemrotate->setEnabled(!anim);
1697 frame_toolbar->setEnabled(!anim);
1698 rect_properties->setEnabled(!anim);
1700 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1701 m_endViewport->setZValue(1100);
1702 QColor col = m_endViewport->pen().color();
1704 m_endViewport->setBrush(col);
1705 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1706 m_endViewport->setSelected(true);
1710 m_endViewport->setZValue(-1000);
1711 m_endViewport->setBrush(QBrush());
1712 m_endViewport->setFlags(0);
1716 void TitleWidget::slotKeepAspect(bool keep)
1718 if (m_endViewport->zValue() == 1100) {
1719 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1720 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1722 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1723 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1727 void TitleWidget::slotResize50()
1729 if (m_endViewport->zValue() == 1100) {
1730 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1731 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1734 void TitleWidget::slotResize100()
1736 if (m_endViewport->zValue() == 1100) {
1737 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1738 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1741 void TitleWidget::slotResize200()
1743 if (m_endViewport->zValue() == 1100) {
1744 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1745 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);