Commit 81ef0fa2 authored by Yann Boucher's avatar Yann Boucher
Browse files

Better drag&drop icon, added support for zooming via CTRL+/CTRL-

parent af362766
Pipeline #78343 passed with stages
in 17 seconds
......@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1089</width>
<height>653</height>
<width>1101</width>
<height>706</height>
</rect>
</property>
<property name="windowTitle">
......@@ -98,6 +98,9 @@
<property name="text">
<string>Simulation speed :</string>
</property>
<property name="buddy">
<cstring>simSpeedSlider</cstring>
</property>
</widget>
</item>
<item>
......@@ -183,6 +186,9 @@
<string>Number of previous
pattern recorded :</string>
</property>
<property name="buddy">
<cstring>recordLineEdit</cstring>
</property>
</widget>
</item>
<item>
......@@ -272,6 +278,9 @@ pattern recorded :</string>
<property name="text">
<string>Width :</string>
</property>
<property name="buddy">
<cstring>widthSpinBox</cstring>
</property>
</widget>
</item>
<item row="1" column="0">
......@@ -279,6 +288,9 @@ pattern recorded :</string>
<property name="text">
<string>Height :</string>
</property>
<property name="buddy">
<cstring>heightSpinBox</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
......@@ -398,6 +410,9 @@ pattern recorded :</string>
<property name="text">
<string>Neighborhood :</string>
</property>
<property name="buddy">
<cstring>neighborhoodComboBox</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
......@@ -483,6 +498,9 @@ pattern recorded :</string>
<property name="text">
<string>Number of states :</string>
</property>
<property name="buddy">
<cstring>nbrStatesComboBox</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
......@@ -490,6 +508,9 @@ pattern recorded :</string>
<property name="text">
<string>Radius r :</string>
</property>
<property name="buddy">
<cstring>radiusSpinBox</cstring>
</property>
</widget>
</item>
<item row="2" column="2">
......@@ -506,6 +527,9 @@ pattern recorded :</string>
<property name="text">
<string>Transition function :</string>
</property>
<property name="buddy">
<cstring>transitionFunctionTextEdit</cstring>
</property>
</widget>
</item>
<item>
......@@ -560,8 +584,8 @@ pattern recorded :</string>
<rect>
<x>0</x>
<y>0</y>
<width>1089</width>
<height>20</height>
<width>1101</width>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menuFichier">
......
......@@ -7,13 +7,20 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>370</height>
<height>393</height>
</rect>
</property>
<property name="windowTitle">
<string>Frame</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Double clic pour faire un glisser-déposer de la structure.</string>
</property>
</widget>
</item>
<item>
<widget class="QTreeWidget" name="tree">
<property name="sizePolicy">
......@@ -87,6 +94,9 @@
<property name="text">
<string>Description : </string>
</property>
<property name="buddy">
<cstring>struct_desc</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
......@@ -145,13 +155,6 @@
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Double clic pour faire un glisser-déposer de la structure.</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_copy">
<property name="enabled">
......
......@@ -25,6 +25,9 @@
<property name="text">
<string>Nom : </string>
</property>
<property name="buddy">
<cstring>nom</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
......@@ -35,6 +38,9 @@
<property name="text">
<string>Auteur : </string>
</property>
<property name="buddy">
<cstring>auteur</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
......@@ -45,6 +51,9 @@
<property name="text">
<string>Description : </string>
</property>
<property name="buddy">
<cstring>desc</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
......@@ -59,6 +68,9 @@
<property name="text">
<string>Date de création : </string>
</property>
<property name="buddy">
<cstring>date</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
......
......@@ -26,6 +26,30 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include "grid.h"
namespace detail
{
// based on https://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
class Graphics_view_zoom : public QObject {
Q_OBJECT
public:
Graphics_view_zoom(QGraphicsView* view);
void gentle_zoom(double factor);
void set_zoom_factor_base(double value);
private:
QGraphicsView* _view;
double _zoom_factor_base;
QPointF target_scene_pos, target_viewport_pos;
bool eventFilter(QObject* object, QEvent* event);
signals:
void zoomed();
};
}
class GridItem;
class GridView;
class GridGraphicsView : public QGraphicsView
......@@ -61,6 +85,9 @@ public:
void set_clipboard(const Structure& s);
//! Retourne la taille du côté en pixels d'une cellule au niveau de zoom actuel
unsigned cell_pixel_size() const;
void copy_grid(const Grid& grid);
Grid get_grid() const;
......@@ -68,6 +95,11 @@ public:
void fill_selection(unsigned state);
Structure selected_cells() const;
signals:
//! Emis quand le zoom change
//! \param cell_size la nouvelle taille à l'écran en pixels d'une cellule
void zoom_changed(unsigned cell_size);
private:
GridItem* item_at(Coord pos);
const GridItem* item_at(Coord pos) const;
......@@ -84,6 +116,7 @@ protected:
private:
GridGraphicsView* m_view;
QGraphicsScene* m_scene;
detail::Graphics_view_zoom* m_zoom;
unsigned m_width;
unsigned m_height;
unsigned m_pen;
......@@ -91,27 +124,4 @@ private:
Alphabet m_alph;
};
namespace detail
{
// based on https://stackoverflow.com/questions/19113532/qgraphicsview-zooming-in-and-out-under-mouse-position-using-mouse-wheel
class Graphics_view_zoom : public QObject {
Q_OBJECT
public:
Graphics_view_zoom(QGraphicsView* view);
void gentle_zoom(double factor);
void set_zoom_factor_base(double value);
private:
QGraphicsView* _view;
double _zoom_factor_base;
QPointF target_scene_pos, target_viewport_pos;
bool eventFilter(QObject* object, QEvent* event);
signals:
void zoomed();
};
}
#endif // GRIDVIEW_HPP
......@@ -16,6 +16,8 @@ namespace Ui {
class StructureLibraryView;
}
// TODO : Documenter
class StructureLibraryView : public QFrame
{
Q_OBJECT
......@@ -24,12 +26,13 @@ public:
explicit StructureLibraryView(QWidget *parent = nullptr);
~StructureLibraryView();
protected:
void mouseMoveEvent(QMouseEvent* event);
signals:
void structure_copied(const Structure& s);
public slots:
//! Permet de mettre à jour la taille en pixels d'une cellule, pour que l'affichage d'un drag&drop soit correct
void update_cell_pixel_size(unsigned size);
private:
void load_structures();
QTreeWidgetItem *add_directory_contents(const QDir& dir);
......@@ -45,6 +48,7 @@ private slots:
private:
Ui::StructureLibraryView *ui;
QFileSystemWatcher m_watcher;
unsigned m_cell_pixel_size;
};
#endif // STRUCTURELIBRARYVIEW_HPP
......@@ -28,9 +28,9 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include "structurereader.hpp"
// FIXME : faire en sorte que le tooltip soit correctement mis à jour à chaque fois
// FIXME : faire en sorte que le tooltip soit correctement mis à jour à chaque fois
class GridItem : public QGraphicsRectItem
class GridItem : public QGraphicsRectItem
{
public:
GridItem(GridView& in_gridview, QColor color, const QString& state_name, QGraphicsItem *parent = nullptr)
......@@ -191,12 +191,12 @@ GridView::GridView(QWidget *parent)
m_view->setBackgroundBrush(QBrush(Qt::gray));
m_view->setRubberBandSelectionMode(Qt::IntersectsItemBoundingRect); // provides better performance
detail::Graphics_view_zoom* z = new detail::Graphics_view_zoom(m_view);
(void)z;
m_zoom = new detail::Graphics_view_zoom(m_view);
QGridLayout *layout = new QGridLayout;
layout->addWidget(m_view);
layout->addWidget(new QLabel("Maintenir SHIFT pour déplacer la grille", this));
layout->addWidget(new QLabel("Clic gauche : éditer; Clic droit : sélectionner", this));
layout->addWidget(new QLabel("Maintenir SHIFT pour déplacer la grille; Molette ou CTRL+/CTRL- pour zoomer", this));
setLayout(layout);
set_current_pen(0);
......@@ -211,6 +211,11 @@ GridView::GridView(QWidget *parent)
Grid default_grid(m_height, m_width);
copy_grid(default_grid);
connect(m_zoom, &detail::Graphics_view_zoom::zoomed, this, [this]
{
emit zoom_changed(cell_pixel_size());
});
}
void GridView::set_alphabet(const Alphabet &alph)
......@@ -238,6 +243,17 @@ void GridView::set_clipboard(const Structure &s)
m_copy_paste_buffer = s;
}
unsigned GridView::cell_pixel_size() const
{
if (m_view->items().empty())
return 10; // a default size if there are no cells in the grid yet
const GridItem* item = item_at({0, 0});
assert(item);
return m_view->transform().mapRect(item->sceneBoundingRect()).size().toSize().width();
}
void GridView::copy_grid(const Grid &grid)
{
m_scene->clear();
......@@ -406,6 +422,14 @@ void GridView::keyPressEvent(QKeyEvent *event)
{
paste_clipboard();
}
else if (event->key() == Qt::Key_Plus)
{
m_zoom->gentle_zoom(1.25);
}
else if (event->key() == Qt::Key_Minus)
{
m_zoom->gentle_zoom(0.75);
}
}
......
......@@ -13,6 +13,9 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui->action_save_struct, &QAction::triggered, this, &MainWindow::afficher_interface_sauvegarde_structure);
connect(ui->struct_library, &StructureLibraryView::structure_copied, this, &MainWindow::copy_structure_clicked);
connect(ui->grid_view, &GridView::zoom_changed, ui->struct_library, &StructureLibraryView::update_cell_pixel_size);
ui->struct_library->update_cell_pixel_size(ui->grid_view->cell_pixel_size());
}
MainWindow::~MainWindow()
......
......@@ -37,9 +37,9 @@ StructureLibraryView::~StructureLibraryView()
delete ui;
}
void StructureLibraryView::mouseMoveEvent(QMouseEvent *event)
void StructureLibraryView::update_cell_pixel_size(unsigned size)
{
qDebug() << event->pos() << "\n";
m_cell_pixel_size = size;
}
void StructureLibraryView::load_structures()
......@@ -189,11 +189,23 @@ void StructureLibraryView::create_drag(QTreeWidgetItem *item, int column)
QPixmap pix;
QImage img = create_preview_image(s, Qt::transparent);
pix.convertFromImage(img);
pix = pix.scaled(QSize(100, 100), Qt::KeepAspectRatio);
QSize target_size;
// set target size so that a single cell has the same size as the grid's cells
target_size = QSize(s.width(), s.height()) * m_cell_pixel_size;
// Have a maximum size to avoid drawing huge megastructures all over the screen
const int max_size = 600;
if (target_size.width() >= max_size)
target_size.setWidth(max_size);
if (target_size.height() >= max_size)
target_size.setHeight(max_size);
pix = pix.scaled(target_size, Qt::KeepAspectRatio);
mimeData->setText(filename);
drag->setMimeData(mimeData);
drag->setPixmap(pix);
drag->setHotSpot(QPoint(m_cell_pixel_size/2, m_cell_pixel_size/2));
drag->exec(Qt::CopyAction);
}
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