Permalink
Browse files

Added a simple placeholder for when there is no expression

  • Loading branch information...
aelliott committed Feb 17, 2012
1 parent ea6fcaa commit cc7b90e001c00d53ef29dfc4d6ca983730638f37
View
@@ -94,6 +94,7 @@ SET(expressioneditor_CPP_SRCS
GraphicsItems/expressiongraphicsitem.cpp
GraphicsItems/groupinggraphicsitem.cpp
GraphicsItems/literalgraphicsitem.cpp
+ GraphicsItems/placeholdergraphicsitem.cpp
GraphicsItems/regexgraphicsitem.cpp
GraphicsItems/repeatgraphicsitem.cpp
GraphicsItems/specialchargraphicsitem.cpp
@@ -0,0 +1,82 @@
+/*!
+ * \file
+ *
+ * Copyright (c) 2012 Alex Elliott <alex@alex-elliott.co.uk>
+ *
+ * \section LICENSE
+ * This file is part of Expression editor
+ *
+ * Expression editor is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Expression editor is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Expression editor. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "placeholdergraphicsitem.hpp"
+
+#include <QSettings>
+#include <QObject>
+
+PlaceholderGraphicsItem::PlaceholderGraphicsItem(QGraphicsItem *parent)
+ : QGraphicsItem(parent)
+ , _metrics(QFont())
+{
+ _msg = QObject::tr("Drop an Item From the Left\nor Begin Typing Below");
+
+ QSettings settings;
+ QFont font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
+ font.setPointSize(font.pointSize()+1);
+ _metrics = QFontMetrics(font);
+}
+
+QRectF PlaceholderGraphicsItem::boundingRect() const
+{
+ double horizontalPadding = 12.0;
+ double verticalPadding = 10.0;
+
+ int lines = _msg.count("\n")+1;
+ double textWidth = 0.0;
+ QStringList words = _msg.split("\n");
+ for(int i = 0; i < words.size(); ++i)
+ if(_metrics.width(words.at(i)) > textWidth)
+ textWidth = _metrics.width(words.at(i));
+ // To prevent a rounding error causing a cut-off later, just add an extra pixel now
+ textWidth++;
+
+ return QRectF(0.5, 0.5, textWidth + 2*horizontalPadding, lines*_metrics.height() + 2*verticalPadding);
+}
+
+void PlaceholderGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+ Q_UNUSED(option)
+ Q_UNUSED(widget)
+
+ QSettings settings;
+ QFont font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
+ font.setPointSize(font.pointSize()+1);
+ double horizontalPadding = 6.0;
+ double verticalPadding = 5.0;
+ double cornerRadius = 5.0;
+
+ painter->setFont(font);
+ painter->setBrush(Qt::white);
+ painter->setPen(Qt::black);
+
+ QRectF drawRect = boundingRect();
+ painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
+
+ painter->drawText(QRectF(
+ horizontalPadding,
+ verticalPadding,
+ drawRect.width() - 2*horizontalPadding,
+ drawRect.height() - 2*verticalPadding),
+ Qt::AlignCenter,
+ _msg);
+}
@@ -0,0 +1,42 @@
+/*!
+ * \file
+ *
+ * Copyright (c) 2012 Alex Elliott <alex@alex-elliott.co.uk>
+ *
+ * \section LICENSE
+ * This file is part of Expression editor
+ *
+ * Expression editor is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Expression editor is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Expression editor. If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef PLACEHOLDERGRAPHICSITEM_HPP
+#define PLACEHOLDERGRAPHICSITEM_HPP
+
+#include <QGraphicsItem>
+#include <QPainter>
+#include <QFontMetrics>
+
+class PlaceholderGraphicsItem : public QGraphicsItem
+{
+public:
+ explicit PlaceholderGraphicsItem(QGraphicsItem *parent = 0);
+
+ QRectF boundingRect() const;
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
+
+private:
+ QString _msg;
+ QFontMetrics _metrics;
+};
+
+#endif // PLACEHOLDERGRAPHICSITEM_HPP
View
@@ -52,7 +52,8 @@ SOURCES += main.cpp\
RegexModules/cppgrepparser.cpp \
GraphicsItems/specialchargraphicsitem.cpp \
expressiongraphicsscene.cpp \
- GraphicsItems/regexgraphicsitem.cpp
+ GraphicsItems/regexgraphicsitem.cpp \
+ GraphicsItems/placeholdergraphicsitem.cpp
HEADERS += mainwindow.hpp \
RegexModules/regexbase.hpp \
@@ -96,7 +97,8 @@ HEADERS += mainwindow.hpp \
RegexModules/cppgrepparser.hpp \
GraphicsItems/specialchargraphicsitem.hpp \
expressiongraphicsscene.hpp \
- GraphicsItems/regexgraphicsitem.hpp
+ GraphicsItems/regexgraphicsitem.hpp \
+ GraphicsItems/placeholdergraphicsitem.hpp
FORMS += mainwindow.ui \
settingsdialog.ui \
@@ -1,7 +1,7 @@
/*!
* \file
*
- * Copyright (c) 2009,2010,2011 Alex Elliott <alex@alex-elliott.co.uk>
+ * Copyright (c) 2009-2012 Alex Elliott <alex@alex-elliott.co.uk>
*
* \section LICENSE
* This file is part of Expression editor
View
@@ -34,6 +34,7 @@ MainWindow::MainWindow(QWidget *parent)
, _expression("")
, _edited(false)
, _visualisation(0)
+ , _placeholder(0)
{
QSettings settings;
_ui->setupUi(this);
@@ -65,10 +66,13 @@ MainWindow::MainWindow(QWidget *parent)
_factory = new RegexFactory(RegexFactory::Qt);
+ _placeholder = new PlaceholderGraphicsItem();
+
ExpressionGraphicsScene *scene = new ExpressionGraphicsScene();
_ui->expressionView->setScene(scene);
_ui->expressionView->setRenderHint(QPainter::Antialiasing, true);
_ui->expressionView->setRenderHint(QPainter::SmoothPixmapTransform, true);
+ scene->addItem(_placeholder);
scene->setSceneRect(scene->itemsBoundingRect());
_ui->expressionView->setSceneRect(scene->itemsBoundingRect());
@@ -254,9 +258,26 @@ void MainWindow::updateExpression(QString expression)
{
scene->removeItem(_visualisation);
delete _visualisation;
+ _visualisation = 0;
+ }
+ if(_placeholder != 0)
+ {
+ scene->removeItem(_placeholder);
+ delete _placeholder;
+ _placeholder = 0;
+ }
+
+ if(!_expression.isEmpty())
+ {
+ _visualisation = new ExpressionGraphicsItem(_expression, _factory->format());
+ scene->addItem(_visualisation);
}
- _visualisation = new ExpressionGraphicsItem(_expression, _factory->format());
- scene->addItem(_visualisation);
+ else
+ {
+ _placeholder = new PlaceholderGraphicsItem();
+ scene->addItem(_placeholder);
+ }
+
scene->setSceneRect(scene->itemsBoundingRect());
_ui->expressionView->setSceneRect(scene->itemsBoundingRect());
View
@@ -33,6 +33,7 @@
#include "regexfactory.hpp"
#include "GraphicsItems/expressiongraphicsitem.hpp"
+#include "GraphicsItems/placeholdergraphicsitem.hpp"
#include "expressiongraphicsscene.hpp"
#include "settingsdialog.hpp"
@@ -88,6 +89,7 @@ public slots:
bool _edited;
RegexFactory *_factory;
ExpressionGraphicsItem *_visualisation;
+ PlaceholderGraphicsItem *_placeholder;
};
#endif // MAINWINDOW_HPP

0 comments on commit cc7b90e

Please sign in to comment.