Commit 3dffcfb2 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Fix small selection issues in titler, add grid for easier moves

parent b1bbdca7
......@@ -65,7 +65,7 @@ void DvdScene::drawForeground(QPainter *painter, const QRectF &rect) {
if (gridSize() <= 1)
return;
QPen pen;
QPen pen(QColor(255, 0, 0, 100));
painter->setPen(pen);
qreal left = int(rect.left()) - (int(rect.left()) % m_gridSize);
......
......@@ -41,6 +41,7 @@ MyTextItem::MyTextItem(const QString &txt, QGraphicsItem *parent) :
, m_useShadow(false)
{
setCacheMode(QGraphicsItem::ItemCoordinateCache);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
document()->setDocumentMargin(0);
updateGeometry();
connect(document(), SIGNAL(contentsChange(int, int, int)),
......@@ -266,10 +267,34 @@ QRectF MyTextItem::boundingRect() const
return base;
}
QVariant MyTextItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPoint newPos = value.toPoint();
if (QApplication::mouseButtons() == Qt::LeftButton && qobject_cast<GraphicsSceneRectMove*> (scene())) {
GraphicsSceneRectMove* customScene = qobject_cast<GraphicsSceneRectMove*> (scene());
int gridSize = customScene->gridSize();
int xV = (newPos.x()/gridSize)*gridSize;
int yV = (newPos.y()/gridSize)*gridSize;
newPos = QPoint(xV, yV);
}
return newPos;
} else if (change == QGraphicsItem::ItemSelectedHasChanged) {
if (value.toBool() == false) {
// Make sure to deselect text when item loses focus
QTextCursor cur(document());
cur.clearSelection();
setTextCursor(cur);
}
}
return QGraphicsItem::itemChange(change, value);
}
MyRectItem::MyRectItem(QGraphicsItem *parent) :
QGraphicsRectItem(parent)
{
setCacheMode(QGraphicsItem::ItemCoordinateCache);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
void MyRectItem::setRect(const QRectF & rectangle)
......@@ -282,12 +307,75 @@ void MyRectItem::setRect(const QRectF & rectangle)
}
}
QVariant MyRectItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPoint newPos = value.toPoint();
if (QApplication::mouseButtons() == Qt::LeftButton && qobject_cast<GraphicsSceneRectMove*> (scene())) {
GraphicsSceneRectMove* customScene = qobject_cast<GraphicsSceneRectMove*> (scene());
int gridSize = customScene->gridSize();
int xV = (newPos.x()/gridSize)*gridSize;
int yV = (newPos.y()/gridSize)*gridSize;
newPos = QPoint(xV, yV);
}
return newPos;
}
return QGraphicsItem::itemChange(change, value);
}
MyPixmapItem::MyPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent) :
QGraphicsPixmapItem(pixmap, parent)
{
setCacheMode(QGraphicsItem::ItemCoordinateCache);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
QVariant MyPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPoint newPos = value.toPoint();
if (QApplication::mouseButtons() == Qt::LeftButton && qobject_cast<GraphicsSceneRectMove*> (scene())) {
GraphicsSceneRectMove* customScene = qobject_cast<GraphicsSceneRectMove*> (scene());
int gridSize = customScene->gridSize();
int xV = (newPos.x()/gridSize)*gridSize;
int yV = (newPos.y()/gridSize)*gridSize;
newPos = QPoint(xV, yV);
}
return newPos;
}
return QGraphicsItem::itemChange(change, value);
}
MySvgItem::MySvgItem(const QString &fileName, QGraphicsItem *parent) :
QGraphicsSvgItem(fileName, parent)
{
setCacheMode(QGraphicsItem::ItemCoordinateCache);
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
QVariant MySvgItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene()) {
QPoint newPos = value.toPoint();
if (QApplication::mouseButtons() == Qt::LeftButton && qobject_cast<GraphicsSceneRectMove*> (scene())) {
GraphicsSceneRectMove* customScene = qobject_cast<GraphicsSceneRectMove*> (scene());
int gridSize = customScene->gridSize();
int xV = (newPos.x()/gridSize)*gridSize;
int yV = (newPos.y()/gridSize)*gridSize;
newPos = QPoint(xV, yV);
}
return newPos;
}
return QGraphicsItem::itemChange(change, value);
}
GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent) :
QGraphicsScene(parent),
m_selectedItem(NULL),
m_resizeMode(NoResize),
m_possibleAction(NoResize),
m_tool(TITLE_RECTANGLE)
m_tool(TITLE_RECTANGLE),
m_gridSize(20),
m_createdText(false)
{
//grabMouse();
m_zoom = 1.0;
......@@ -336,8 +424,8 @@ void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
return;
}
}
int diff = 1;
if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10;
int diff = m_gridSize;
if (keyEvent->modifiers() & Qt::ControlModifier) diff = m_gridSize * 5;
switch (keyEvent->key()) {
case Qt::Key_Left:
foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(-diff,0); }
......@@ -400,12 +488,23 @@ void GraphicsSceneRectMove::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
if (m_tool == TITLE_RECTANGLE && m_selectedItem) setSelectedItem(m_selectedItem);
QGraphicsScene::mouseReleaseEvent(e);
if (m_createdText) {
m_selectedItem->setSelected(true);
MyTextItem *newText = static_cast<MyTextItem*>(m_selectedItem);
QTextCursor cur(newText->document());
cur.select(QTextCursor::Document);
newText->setTextCursor(cur);
m_createdText = false;
}
emit actionFinished();
}
void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
{
m_clickPoint = e->scenePos();
int xPos = ((int) e->scenePos().x() / m_gridSize) * m_gridSize;
int yPos = ((int) e->scenePos().y() / m_gridSize) * m_gridSize;
m_clickPoint = QPointF(xPos, yPos);
//m_clickPoint = e->scenePos();
QPointF p = e->scenePos();
p += QPoint(-2, -2);
m_resizeMode = m_possibleAction;
......@@ -456,6 +555,7 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
return;
}
t->setTextInteractionFlags(Qt::NoTextInteraction);
t->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
setCursor(Qt::ClosedHandCursor);
} else if (item->type() == QGraphicsRectItem::Type || item->type() == QGraphicsSvgItem::Type || item->type() == QGraphicsPixmapItem::Type) {
QRectF r1;
......@@ -492,17 +592,19 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
}
}
} else if (m_tool == TITLE_RECTANGLE) {
m_sceneClickPoint = e->scenePos();
m_sceneClickPoint = QPointF(xPos, yPos);
m_selectedItem = NULL;
} else if (m_tool == TITLE_TEXT) {
MyTextItem *textItem = new MyTextItem(QString(), NULL);
MyTextItem *textItem = new MyTextItem(QStringLiteral("Text"), NULL);
yPos = (((int) e->scenePos().y() - (int)(m_fontSize / 2)) / m_gridSize) * m_gridSize;
textItem->setPos(xPos, yPos);
addItem(textItem);
emit newText(textItem);
textItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
textItem->setTextInteractionFlags(Qt::TextEditorInteraction);
textItem->setPos(e->scenePos() - QPointF(0, (int)(m_fontSize / 2)));
emit newText(textItem);
m_selectedItem = textItem;
m_selectedItem->setSelected(true);
m_createdText = true;
}
QGraphicsScene::mousePressEvent(e);
//qDebug() << "////// MOUSE CLICK, RESIZE MODE: " << m_resizeMode;
......@@ -545,7 +647,9 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
else
newrect = m_selectedItem->boundingRect();
QPointF newpoint = e->scenePos();
int xPos = ((int) e->scenePos().x() / m_gridSize) * m_gridSize;
int yPos = ((int) e->scenePos().y() / m_gridSize) * m_gridSize;
QPointF newpoint(xPos, yPos);
switch (m_resizeMode) {
case BottomRight:
case BottomLeft:
......@@ -590,9 +694,8 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
QGraphicsScene::mouseMoveEvent(e);
return;
}
QPointF diff = e->scenePos() - m_sceneClickPoint;
QGraphicsScene::mouseMoveEvent(e);
m_sceneClickPoint = e->scenePos();
foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(diff.x(), diff.y()); }
}
emit itemMoved();
} else if (m_tool == TITLE_SELECT) {
......@@ -690,7 +793,7 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
m_selectedItem->setPos(m_sceneClickPoint);
m_selectedItem->setSelected(true);
emit newRect(rect);
m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
m_resizeMode = BottomRight;
QGraphicsScene::mouseMoveEvent(e);
}
......@@ -746,4 +849,41 @@ void GraphicsSceneRectMove::slotUpdateFontSize(int s)
m_fontSize = s;
}
void GraphicsSceneRectMove::drawForeground(QPainter *painter, const QRectF &rect) {
// draw the grid if needed
if (m_gridSize <= 1)
return;
QPen pen(QColor(255, 0, 0, 100));
painter->setPen(pen);
qreal left = int(rect.left()) - (int(rect.left()) % m_gridSize);
qreal top = int(rect.top()) - (int(rect.top()) % m_gridSize);
QVector<QPointF> points;
for (qreal x = left; x < rect.right(); x += m_gridSize){
for (qreal y = top; y < rect.bottom(); y += m_gridSize){
points.append(QPointF(x,y));
}
}
painter->drawPoints(points.data(), points.size());
}
int GraphicsSceneRectMove::gridSize() const
{
return m_gridSize;
}
void GraphicsSceneRectMove::slotUseGrid(bool enableGrid)
{
m_gridSize = enableGrid ? 20 : 1;
}
void GraphicsSceneRectMove::addNewItem(QGraphicsItem *item)
{
clearSelection();
addItem(item);
item->setSelected(true);
m_selectedItem = item;
}
......@@ -23,6 +23,7 @@
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QGraphicsSvgItem>
enum resizeModes {NoResize = 0, TopLeft, BottomLeft, TopRight, BottomRight, Left, Right, Up, Down};
enum TITLETOOL { TITLE_SELECT = 0, TITLE_RECTANGLE = 1, TITLE_TEXT = 2, TITLE_IMAGE = 3 };
......@@ -43,6 +44,9 @@ public:
void loadShadow(QStringList info);
void paint( QPainter *painter, const QStyleOptionGraphicsItem * option, QWidget* w);
protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
Qt::Alignment m_alignment;
QImage m_shadow;
......@@ -63,10 +67,28 @@ class MyRectItem: public QGraphicsRectItem
public:
MyRectItem(QGraphicsItem *parent = 0);
void setRect(const QRectF & rectangle);
protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
QRectF m_rect;
};
class MyPixmapItem: public QGraphicsPixmapItem
{
public:
MyPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = 0);
protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
};
class MySvgItem: public QGraphicsSvgItem
{
public:
MySvgItem(const QString &fileName, QGraphicsItem *parent = 0);
protected:
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
};
class GraphicsSceneRectMove: public QGraphicsScene
{
Q_OBJECT
......@@ -79,9 +101,12 @@ public:
void setTool(TITLETOOL tool);
TITLETOOL tool() const;
void clearTextSelection();
int gridSize() const;
void addNewItem(QGraphicsItem *item);
public slots:
void slotUpdateFontSize(int s);
void slotUseGrid(bool enableGrid);
protected:
virtual void keyPressEvent(QKeyEvent * keyEvent);
......@@ -91,6 +116,7 @@ protected:
/** @brief Resizes and moves items */
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent*);
virtual void wheelEvent(QGraphicsSceneWheelEvent * wheelEvent);
void drawForeground(QPainter *painter, const QRectF &rect);
private:
void setCursor(QCursor);
......@@ -102,6 +128,8 @@ private:
TITLETOOL m_tool;
QPointF m_clickPoint;
int m_fontSize;
int m_gridSize;
bool m_createdText;
signals:
void itemMoved();
......
......@@ -428,6 +428,7 @@ TitleWidget::TitleWidget(const QUrl &url, const Timecode &tc, const QString &pro
m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
connect(font_size, SIGNAL(valueChanged(int)), m_scene, SLOT(slotUpdateFontSize(int)));
connect(use_grid, SIGNAL(toggled(bool)), m_scene, SLOT(slotUseGrid(bool)));
// Video frame rect
QPen framepen;
......@@ -440,7 +441,7 @@ TitleWidget::TitleWidget(const QUrl &url, const Timecode &tc, const QString &pro
graphicsView->scene()->addItem(m_frameBorder);
// semi transparent safe zones
framepen.setColor(QColor(255, 0, 0, 60));
framepen.setColor(QColor(255, 0, 0, 100));
QGraphicsRectItem *safe1 = new QGraphicsRectItem(QRectF(m_frameWidth * 0.05, m_frameHeight * 0.05, m_frameWidth * 0.9, m_frameHeight * 0.9), m_frameBorder);
safe1->setBrush(Qt::transparent);
safe1->setPen(framepen);
......@@ -742,20 +743,20 @@ void TitleWidget::slotImageTool()
if (url.isValid()) {
KRecentDirs::add(QStringLiteral(":KdenliveImageFolder"), url.adjusted(QUrl::RemoveFilename).path());
if (url.path().endsWith(QLatin1String(".svg"))) {
QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
MySvgItem *svg = new MySvgItem(url.toLocalFile());
svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
svg->setZValue(m_count++);
svg->setData(Qt::UserRole, url.path());
graphicsView->scene()->addItem(svg);
m_scene->addNewItem(svg);
prepareTools(svg);
} else {
QPixmap pix(url.path());
QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
MyPixmapItem *image = new MyPixmapItem(pix);
image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
image->setData(Qt::UserRole, url.path());
image->setZValue(m_count++);
graphicsView->scene()->addItem(image);
m_scene->addNewItem(image);
prepareTools(image);
}
}
......@@ -2015,6 +2016,8 @@ void TitleWidget::writeChoices()
titleConfig.writeEntry("background_color", backgroundColor->color());
titleConfig.writeEntry("background_alpha", backgroundAlpha->value());
titleConfig.writeEntry("use_grid", use_grid->isChecked());
//! \todo Not sure if I should sync - it is probably safe to do it
config->sync();
......@@ -2058,6 +2061,8 @@ void TitleWidget::readChoices()
backgroundColor->setColor(titleConfig.readEntry("background_color", backgroundColor->color()));
backgroundAlpha->setValue(titleConfig.readEntry("background_alpha", backgroundAlpha->value()));
use_grid->setChecked(titleConfig.readEntry("use_grid", false));
m_scene->slotUseGrid(use_grid->isChecked());
}
void TitleWidget::adjustFrameSize()
......@@ -2883,3 +2888,4 @@ void TitleWidget::slotUpdateShadow()
item->updateShadow(shadowBox->isChecked(), blur_radius->value(), shadowX->value(), shadowY->value(), shadowColor->color());
}
}
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1051</width>
<height>1103</height>
<height>1182</height>
</rect>
</property>
<property name="windowTitle">
......@@ -330,29 +330,26 @@
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<layout class="QGridLayout" name="gridLayout_16" rowstretch="0,0,0">
<property name="horizontalSpacing">
<property name="spacing">
<number>6</number>
</property>
<property name="verticalSpacing">
<number>-1</number>
</property>
<item row="2" column="0">
<widget class="QToolButton" name="buttonSelectImages">
<property name="enabled">
<bool>false</bool>
<item row="2" column="3">
<widget class="QToolButton" name="buttonSelectAll">
<property name="whatsThis">
<string>Selects all items on the canvas.</string>
</property>
<property name="text">
<string>I</string>
<string>A</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QToolButton" name="buttonSelectText">
<item row="2" column="4">
<widget class="QToolButton" name="buttonSelectRects">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>T</string>
<string>R</string>
</property>
</widget>
</item>
......@@ -366,46 +363,53 @@
</property>
</widget>
</item>
<item row="2" column="5">
<spacer name="spacerBottomStack">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<item row="0" column="0" colspan="7">
<widget class="QGraphicsView" name="graphicsView">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</spacer>
</widget>
</item>
<item row="2" column="3">
<widget class="QToolButton" name="buttonSelectAll">
<property name="whatsThis">
<string>Selects all items on the canvas.</string>
<item row="2" column="0">
<widget class="QToolButton" name="buttonSelectImages">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>A</string>
<string>I</string>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QToolButton" name="buttonSelectRects">
<item row="2" column="1">
<widget class="QToolButton" name="buttonSelectText">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>R</string>
<string>T</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="6">
<widget class="QGraphicsView" name="graphicsView">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<item row="2" column="6">
<spacer name="spacerBottomStack">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="5">
<widget class="QCheckBox" name="use_grid">
<property name="text">
<string>Use grid</string>
</property>
</widget>
</item>
......@@ -646,14 +650,14 @@
<property name="flat">
<bool>false</bool>
</property>
<property name="color">
<property name="color" stdset="0">
<color>
<red>85</red>
<green>170</green>
<blue>255</blue>
</color>
</property>
<property name="defaultColor">
<property name="defaultColor" stdset="0">
<color>
<red>0</red>
<green>0</green>
......@@ -759,14 +763,14 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="color">
<property name="color" stdset="0">
<color alpha="100">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</property>
<property name="alphaChannelEnabled">
<property name="alphaChannelEnabled" stdset="0">
<bool>true</bool>
</property>
</widget>
......@@ -829,14 +833,14 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="color">
<property name="color" stdset="0">
<color>
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</property>
<property name="defaultColor">
<property name="defaultColor" stdset="0">
<color>
<red>0</red>
<green>0</green>
......@@ -894,14 +898,14 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="color">
<property name="color" stdset="0">
<color>
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</property>
<property name="defaultColor">
<property name="defaultColor" stdset="0">
<color>
<red>0</red>
<green>0</green>
......@@ -999,14 +1003,14 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="color">
<property name="color" stdset="0">
<color>