Permalink
Browse files

Started modifications to facilitate drag/drop behaviour

  • Loading branch information...
aelliott committed Feb 16, 2012
1 parent 7676a1e commit 5fc2f09017cc3644878785a738bf9e0b5af562d8
@@ -82,7 +82,9 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
break;
case T_ALTERNATION:
{
- AlternationGraphicsItem *alternation = new AlternationGraphicsItem(currentLayout, *_iter, _pos);
+ // This is a bit nasty, but it works
+ int start = static_cast<RegexGraphicsItem *>(currentLayout->itemAt(0))->tokenPos();
+ AlternationGraphicsItem *alternation = new AlternationGraphicsItem(currentLayout, *_iter, start);
currentLayout = new QGraphicsLinearLayout(Qt::Horizontal);
currentLayout->setSpacing(_itemSpacing);
currentLayout->addItem(alternation);
@@ -217,7 +219,7 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
case T_REPEAT_ONE_OR_MORE:
case T_REPEAT_SPECIFIED:
currentLayout->removeItem(_lastItem);
- repeat = new RepeatGraphicsItem(*_iter, _pos-1, _lastItem);
+ repeat = new RepeatGraphicsItem(*_iter, _lastItem->tokenPos(), _lastItem);
repeat->setEndPos(_pos);
currentLayout->addItem(repeat);
_lastItem = repeat;
@@ -63,7 +63,7 @@ public slots:
QString _expression;
RegexFactory *_factory;
QGraphicsLinearLayout *_root;
- QGraphicsWidget *_lastItem;
+ RegexGraphicsItem *_lastItem;
std::vector<Token *> _tokens;
std::vector<Token *>::iterator _iter;
int _pos;
@@ -20,6 +20,8 @@
* along with Expression editor. If not, see <http://www.gnu.org/licenses/>.
*/
#include "literalgraphicsitem.hpp"
+#include <QApplication>
+#include <QFontMetrics>
LiteralGraphicsItem::LiteralGraphicsItem(Token *token, int tokenPos, QGraphicsItem *parent)
: RegexGraphicsItem(token, tokenPos, parent)
@@ -33,7 +35,7 @@ LiteralGraphicsItem::LiteralGraphicsItem(Token *token, int tokenPos, QGraphicsIt
text.remove(i, 1);
}
- _text = new QGraphicsTextItem(text, this);
+ _text = text;
}
QRectF LiteralGraphicsItem::boundingRect() const
@@ -42,9 +44,7 @@ QRectF LiteralGraphicsItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/Literal/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Literal/VerticalPadding", 5.0).toDouble();
- QRectF textRect = _text->boundingRect();
-
- return QRectF(0, 0, textRect.width() + 2*horizontalPadding, textRect.height() + 2*verticalPadding);
+ return QRectF(0, 0, _metrics.width(_text) + 2*horizontalPadding, _metrics.height() + 2*verticalPadding);
}
void LiteralGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -58,6 +58,7 @@ void LiteralGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsIte
double cornerRadius = settings.value("Visualisation/Literal/CornerRadius", 5.0).toDouble();
QColor bgColor = settings.value("Visualisation/Literal/Color", QColor(220,255,220)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -75,7 +76,12 @@ void LiteralGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsIte
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- _text->setPos(horizontalPadding, verticalPadding);
+ QRectF textRect = QRectF(drawRect.x() + horizontalPadding,
+ drawRect.y() + verticalPadding,
+ drawRect.width() - 2*horizontalPadding,
+ drawRect.height() - 2*verticalPadding);
+
+ painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignHCenter, _text);
}
QSizeF LiteralGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
@@ -37,7 +37,7 @@ class LiteralGraphicsItem : public RegexGraphicsItem
private:
QString _literal;
- QGraphicsTextItem *_text;
+ QString _text;
};
#endif // LITERALGRAPHICSITEM_HPP
@@ -24,13 +24,19 @@
#include <QGraphicsScene>
#include <QDrag>
#include <QGraphicsSceneMouseEvent>
+#include <QFont>
RegexGraphicsItem::RegexGraphicsItem(Token *token, int tokenPos, QGraphicsItem *parent)
: QGraphicsWidget(parent)
, _token(token)
, _tokenPos(tokenPos)
, _endPos(tokenPos)
+ , _metrics(QFont())
{
+ QSettings settings;
+ _font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
+ _metrics = QFontMetrics(_font);
+
setCursor(Qt::OpenHandCursor);
setAcceptedMouseButtons(Qt::LeftButton);
}
@@ -25,6 +25,7 @@
#include <QGraphicsWidget>
#include <QPainter>
#include <QSettings>
+#include <QFontMetrics>
#include "RegexModules/token.hpp"
@@ -48,6 +49,8 @@ class RegexGraphicsItem : public QGraphicsWidget
Token *_token;
int _tokenPos;
int _endPos;
+ QFont _font;
+ QFontMetrics _metrics;
};
#endif // REGEXGRAPHICSITEM_HPP
@@ -21,7 +21,81 @@
*/
#include "expressiongraphicsscene.hpp"
-ExpressionGraphicsScene::ExpressionGraphicsScene(QObject *parent) :
- QGraphicsScene(parent)
+#include <QGraphicsSceneDragDropEvent>
+#include <QMimeData>
+#include <QDebug>
+
+ExpressionGraphicsScene::ExpressionGraphicsScene(QObject *parent)
+ : QGraphicsScene(parent)
+{
+}
+
+void ExpressionGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
+ while(_dropZoneLabels.size() > 0)
+ {
+ removeItem(_dropZoneLabels.at(0));
+ _dropZoneLabels.removeAt(0);
+ }
+
+ QList<QGraphicsItem *> itemList = items();
+ for(int i = 0; i < itemList.size()-1; ++i)
+ {
+ QGraphicsItem *item = itemList.at(i);
+ QPointF itemPos = item->scenePos();
+ QRectF bounds = item->boundingRect();
+
+ qDebug() << itemPos;
+ qDebug() << bounds;
+
+ QPointF topLeft = itemPos;
+ topLeft.setY(itemPos.y()-8.0);
+
+ QPointF bottomRight = itemPos;
+ bottomRight.setX(itemPos.x()+bounds.width());
+
+ QGraphicsRectItem *dropZoneLabel = new QGraphicsRectItem(0, 0, bounds.width(), 8.0);
+ dropZoneLabel->setPos(topLeft);
+ addItem(dropZoneLabel);
+ _dropZoneLabels.append(dropZoneLabel);
+
+ qDebug() << dropZoneLabel->boundingRect();
+ }
+
+ event->accept();
+}
+
+void ExpressionGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
+{
+ event->accept();
+}
+
+void ExpressionGraphicsScene::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
+{
+ while(_dropZoneLabels.size() > 0)
+ {
+ removeItem(_dropZoneLabels.at(0));
+ _dropZoneLabels.removeAt(0);
+ }
+
+ event->accept();
+}
+
+void ExpressionGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
+{
+ while(_dropZoneLabels.size() > 0)
+ {
+ removeItem(_dropZoneLabels.at(0));
+ _dropZoneLabels.removeAt(0);
+ }
+
+ event->accept();
+
+ const QMimeData *mime = event->mimeData();
+ int startPos = QVariant(mime->data("text/x-regexp-start-pos")).toInt();
+ int endPos = QVariant(mime->data("text/x-regexp-end-pos")).toInt();
+
+ //qDebug() << "Drop Accepted from: " << startPos << " to " << endPos;
+
+ // Must now decide how to handle this drop
}
@@ -23,17 +23,24 @@
#define EXPRESSIONGRAPHICSSCENE_HPP
#include <QGraphicsScene>
+#include <QList>
+#include <QRectF>
+#include <QGraphicsRectItem>
class ExpressionGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit ExpressionGraphicsScene(QObject *parent = 0);
-signals:
-
-public slots:
+protected:
+ void dragEnterEvent(QGraphicsSceneDragDropEvent *event);
+ void dragMoveEvent(QGraphicsSceneDragDropEvent *event);
+ void dragLeaveEvent(QGraphicsSceneDragDropEvent *event);
+ void dropEvent(QGraphicsSceneDragDropEvent *event);
+private:
+ QList<QGraphicsRectItem *> _dropZoneLabels;
};
#endif // EXPRESSIONGRAPHICSSCENE_HPP
View
@@ -66,10 +66,10 @@ MainWindow::MainWindow(QWidget *parent)
_visualisation = new ExpressionGraphicsItem("", RegexFactory::Qt);
- _ui->expressionView->setScene(new QGraphicsScene());
+ ExpressionGraphicsScene *scene = new ExpressionGraphicsScene();
+ _ui->expressionView->setScene(scene);
_ui->expressionView->setRenderHint(QPainter::Antialiasing, true);
_ui->expressionView->setRenderHint(QPainter::SmoothPixmapTransform, true);
- QGraphicsScene *scene = _ui->expressionView->scene();
scene->addItem(_visualisation);
scene->setSceneRect(scene->itemsBoundingRect());
_ui->expressionView->setSceneRect(scene->itemsBoundingRect());
View
@@ -33,6 +33,7 @@
#include "regexfactory.hpp"
#include "GraphicsItems/expressiongraphicsitem.hpp"
+#include "expressiongraphicsscene.hpp"
#include "settingsdialog.hpp"
#include "expressionedit.hpp"
View
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
- <width>415</width>
- <height>550</height>
+ <width>454</width>
+ <height>613</height>
</rect>
</property>
<property name="windowTitle">
@@ -146,13 +146,27 @@
</attribute>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
+ <widget class="QLabel" name="fontLabel">
+ <property name="text">
+ <string>VIsualisation Font:</string>
+ </property>
+ </widget>
+ </item>
+ <item row="0" column="1">
+ <widget class="QPushButton" name="selectFontButton">
+ <property name="text">
+ <string>Select Font</string>
+ </property>
+ </widget>
+ </item>
+ <item row="1" column="0">
<widget class="QLabel" name="horizontalSpacingLabel">
<property name="text">
<string>Horizontal Spacing:</string>
</property>
</widget>
</item>
- <item row="0" column="1">
+ <item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalSpacingLayout">
<item>
<widget class="QSpinBox" name="horizontalSpacingSpinBox">
@@ -170,6 +184,31 @@
</item>
</layout>
</item>
+ <item row="2" column="0">
+ <widget class="QLabel" name="verticalSpacingLabel">
+ <property name="text">
+ <string>Vertical Spacing</string>
+ </property>
+ </widget>
+ </item>
+ <item row="2" column="1">
+ <layout class="QHBoxLayout" name="verticalSpacingLayout">
+ <item>
+ <widget class="QSpinBox" name="verticalSpacingSpinBox">
+ <property name="value">
+ <number>12</number>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QLabel" name="verticalSpacingUnitsLabel">
+ <property name="text">
+ <string>pixels</string>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
<item row="3" column="0">
<widget class="QLabel" name="displayOptionsLabel">
<property name="text">
@@ -204,31 +243,6 @@
</property>
</widget>
</item>
- <item row="1" column="0">
- <widget class="QLabel" name="verticalSpacingLabel">
- <property name="text">
- <string>Vertical Spacing</string>
- </property>
- </widget>
- </item>
- <item row="1" column="1">
- <layout class="QHBoxLayout" name="verticalSpacingLayout">
- <item>
- <widget class="QSpinBox" name="verticalSpacingSpinBox">
- <property name="value">
- <number>12</number>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QLabel" name="verticalSpacingUnitsLabel">
- <property name="text">
- <string>pixels</string>
- </property>
- </widget>
- </item>
- </layout>
- </item>
</layout>
</widget>
<widget class="QWidget" name="visualisationLiteralPage">

0 comments on commit 5fc2f09

Please sign in to comment.