Commit b30834ee authored by Yann Boucher's avatar Yann Boucher
Browse files

Few cosmetic improvements to GridView

parent 88a33280
......@@ -41,6 +41,7 @@ public:
private:
GridItem* item_at(Coord pos);
const GridItem* item_at(Coord pos) const;
void copy_selection();
void paste_clipboard();
......@@ -54,6 +55,8 @@ protected:
private:
QGraphicsView* m_view;
QGraphicsScene* m_scene;
unsigned m_width;
unsigned m_height;
Structure m_copy_paste_buffer;
};
......@@ -67,12 +70,10 @@ class Graphics_view_zoom : public QObject {
public:
Graphics_view_zoom(QGraphicsView* view);
void gentle_zoom(double factor);
void set_modifiers(Qt::KeyboardModifiers modifiers);
void set_zoom_factor_base(double value);
private:
QGraphicsView* _view;
Qt::KeyboardModifiers _modifiers;
double _zoom_factor_base;
QPointF target_scene_pos, target_viewport_pos;
bool eventFilter(QObject* object, QEvent* event);
......
......@@ -25,7 +25,7 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include <qmath.h>
class GridItem : public QGraphicsRectItem
class GridItem : public QGraphicsRectItem
{
public:
GridItem(QColor color, const QString& state_name, QGraphicsItem *parent = nullptr)
......@@ -44,12 +44,18 @@ public:
{ return 20; }
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
assert(scene()->views().size() != 0);
QGraphicsView* view = scene()->views().first();
QSizeF screen_size = view->transform().mapRect(sceneBoundingRect()).size();
QRectF rect = boundingRect();
QPen pen(Qt::black, 0); // cosmetic 1-pixel pen
painter->setPen(pen);
if (screen_size.width() < 7.5)
painter->setPen(Qt::NoPen); // if the cell is too small, don't draw its outline
else
painter->setPen(QPen(Qt::black, 0)); // cosmetic 1-pixel pen
if (isSelected())
{
......@@ -58,7 +64,6 @@ protected:
}
else
{
painter->setBrush(brush());
}
......@@ -75,8 +80,10 @@ private:
};
GridView::GridView(QWidget *parent)
: QFrame(parent)
: QFrame(parent), m_width(0), m_height(0)
{
setMouseTracking(true);
setFrameStyle(Sunken | StyledPanel);
m_scene = new QGraphicsScene(this);
......@@ -94,17 +101,19 @@ GridView::GridView(QWidget *parent)
m_view->viewport()->setCursor(Qt::ArrowCursor);
detail::Graphics_view_zoom* z = new detail::Graphics_view_zoom(m_view);
z->set_modifiers(Qt::NoModifier);
(void)z;
QGridLayout *layout = new QGridLayout;
layout->addWidget(m_view);
layout->addWidget(new QLabel("Maintenir SHIFT pour déplacer la grille", this));
setLayout(layout);
m_width = m_height = 100;
// Populate scene;
for (int i = 0; i < 100; ++i)
for (unsigned i = 0; i < m_width; ++i)
{
for (int j = 0; j < 100; ++j)
for (unsigned j = 0; j < m_height; ++j)
{
QColor color(Qt::black);
......@@ -135,12 +144,27 @@ void GridView::copy_grid(const Grid &grid)
m_scene->addItem(item);
}
}
m_width = grid.get_col();
m_height = grid.get_rows();
}
Grid GridView::get_grid() const
{
// TODO
return Grid(0, 0);
Grid grid = Grid(m_height, m_width);
for (size_t i = 0; i < grid.get_col(); ++i)
{
for (size_t j = 0; j < grid.get_rows(); ++j)
{
const GridItem* item = item_at(Coord{(int)i, (int)j});
assert(item);
grid.set_cell(i, j, item->cell_state);
}
}
return grid;
}
void GridView::clear_selection()
......@@ -170,6 +194,11 @@ GridItem *GridView::item_at(Coord pos)
QGraphicsItem* gitem_ptr = m_scene->itemAt(QPointF(pos.x*GridItem::width(), pos.y*GridItem::height()), QTransform());
return dynamic_cast<GridItem*>(gitem_ptr);
}
const GridItem *GridView::item_at(Coord pos) const
{
QGraphicsItem* gitem_ptr = m_scene->itemAt(QPointF(pos.x*GridItem::width(), pos.y*GridItem::height()), QTransform());
return dynamic_cast<GridItem*>(gitem_ptr);
}
void GridView::copy_selection()
{
......@@ -284,7 +313,6 @@ detail::Graphics_view_zoom::Graphics_view_zoom(QGraphicsView* view)
{
_view->viewport()->installEventFilter(this);
_view->setMouseTracking(true);
_modifiers = Qt::ControlModifier;
_zoom_factor_base = 1.0015;
}
......@@ -298,11 +326,6 @@ void detail::Graphics_view_zoom::gentle_zoom(double factor) {
emit zoomed();
}
void detail::Graphics_view_zoom::set_modifiers(Qt::KeyboardModifiers modifiers) {
_modifiers = modifiers;
}
void detail::Graphics_view_zoom::set_zoom_factor_base(double value) {
_zoom_factor_base = value;
}
......@@ -317,14 +340,12 @@ bool detail::Graphics_view_zoom::eventFilter(QObject *object, QEvent *event) {
}
} else if (event->type() == QEvent::Wheel) {
QWheelEvent* wheel_event = static_cast<QWheelEvent*>(event);
if (QApplication::keyboardModifiers() == _modifiers) {
if (wheel_event->angleDelta().y() != 0) {
double angle = wheel_event->angleDelta().y();
double factor = qPow(_zoom_factor_base, angle);
gentle_zoom(factor);
return true;
}
}
}
Q_UNUSED(object)
return false;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment