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 @@ ...@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1089</width> <width>1101</width>
<height>653</height> <height>706</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
...@@ -98,6 +98,9 @@ ...@@ -98,6 +98,9 @@
<property name="text"> <property name="text">
<string>Simulation speed :</string> <string>Simulation speed :</string>
</property> </property>
<property name="buddy">
<cstring>simSpeedSlider</cstring>
</property>
</widget> </widget>
</item> </item>
<item> <item>
...@@ -183,6 +186,9 @@ ...@@ -183,6 +186,9 @@
<string>Number of previous <string>Number of previous
pattern recorded :</string> pattern recorded :</string>
</property> </property>
<property name="buddy">
<cstring>recordLineEdit</cstring>
</property>
</widget> </widget>
</item> </item>
<item> <item>
...@@ -272,6 +278,9 @@ pattern recorded :</string> ...@@ -272,6 +278,9 @@ pattern recorded :</string>
<property name="text"> <property name="text">
<string>Width :</string> <string>Width :</string>
</property> </property>
<property name="buddy">
<cstring>widthSpinBox</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="1" column="0">
...@@ -279,6 +288,9 @@ pattern recorded :</string> ...@@ -279,6 +288,9 @@ pattern recorded :</string>
<property name="text"> <property name="text">
<string>Height :</string> <string>Height :</string>
</property> </property>
<property name="buddy">
<cstring>heightSpinBox</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="2" column="1">
...@@ -398,6 +410,9 @@ pattern recorded :</string> ...@@ -398,6 +410,9 @@ pattern recorded :</string>
<property name="text"> <property name="text">
<string>Neighborhood :</string> <string>Neighborhood :</string>
</property> </property>
<property name="buddy">
<cstring>neighborhoodComboBox</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
...@@ -483,6 +498,9 @@ pattern recorded :</string> ...@@ -483,6 +498,9 @@ pattern recorded :</string>
<property name="text"> <property name="text">
<string>Number of states :</string> <string>Number of states :</string>
</property> </property>
<property name="buddy">
<cstring>nbrStatesComboBox</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="2" column="1">
...@@ -490,6 +508,9 @@ pattern recorded :</string> ...@@ -490,6 +508,9 @@ pattern recorded :</string>
<property name="text"> <property name="text">
<string>Radius r :</string> <string>Radius r :</string>
</property> </property>
<property name="buddy">
<cstring>radiusSpinBox</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="2" column="2"> <item row="2" column="2">
...@@ -506,6 +527,9 @@ pattern recorded :</string> ...@@ -506,6 +527,9 @@ pattern recorded :</string>
<property name="text"> <property name="text">
<string>Transition function :</string> <string>Transition function :</string>
</property> </property>
<property name="buddy">
<cstring>transitionFunctionTextEdit</cstring>
</property>
</widget> </widget>
</item> </item>
<item> <item>
...@@ -560,8 +584,8 @@ pattern recorded :</string> ...@@ -560,8 +584,8 @@ pattern recorded :</string>
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1089</width> <width>1101</width>
<height>20</height> <height>25</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFichier"> <widget class="QMenu" name="menuFichier">
......
...@@ -7,13 +7,20 @@ ...@@ -7,13 +7,20 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>400</width>
<height>370</height> <height>393</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Frame</string> <string>Frame</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <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> <item>
<widget class="QTreeWidget" name="tree"> <widget class="QTreeWidget" name="tree">
<property name="sizePolicy"> <property name="sizePolicy">
...@@ -87,6 +94,9 @@ ...@@ -87,6 +94,9 @@
<property name="text"> <property name="text">
<string>Description : </string> <string>Description : </string>
</property> </property>
<property name="buddy">
<cstring>struct_desc</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
...@@ -145,13 +155,6 @@ ...@@ -145,13 +155,6 @@
</property> </property>
</widget> </widget>
</item> </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> <item>
<widget class="QPushButton" name="button_copy"> <widget class="QPushButton" name="button_copy">
<property name="enabled"> <property name="enabled">
......
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
<property name="text"> <property name="text">
<string>Nom : </string> <string>Nom : </string>
</property> </property>
<property name="buddy">
<cstring>nom</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
...@@ -35,6 +38,9 @@ ...@@ -35,6 +38,9 @@
<property name="text"> <property name="text">
<string>Auteur : </string> <string>Auteur : </string>
</property> </property>
<property name="buddy">
<cstring>auteur</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
...@@ -45,6 +51,9 @@ ...@@ -45,6 +51,9 @@
<property name="text"> <property name="text">
<string>Description : </string> <string>Description : </string>
</property> </property>
<property name="buddy">
<cstring>desc</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="3" column="1">
...@@ -59,6 +68,9 @@ ...@@ -59,6 +68,9 @@
<property name="text"> <property name="text">
<string>Date de création : </string> <string>Date de création : </string>
</property> </property>
<property name="buddy">
<cstring>date</cstring>
</property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="2" column="1">
......
...@@ -26,6 +26,30 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av ...@@ -26,6 +26,30 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include "grid.h" #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 GridItem;
class GridView; class GridView;
class GridGraphicsView : public QGraphicsView class GridGraphicsView : public QGraphicsView
...@@ -61,6 +85,9 @@ public: ...@@ -61,6 +85,9 @@ public:
void set_clipboard(const Structure& s); 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); void copy_grid(const Grid& grid);
Grid get_grid() const; Grid get_grid() const;
...@@ -68,6 +95,11 @@ public: ...@@ -68,6 +95,11 @@ public:
void fill_selection(unsigned state); void fill_selection(unsigned state);
Structure selected_cells() const; 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: private:
GridItem* item_at(Coord pos); GridItem* item_at(Coord pos);
const GridItem* item_at(Coord pos) const; const GridItem* item_at(Coord pos) const;
...@@ -84,6 +116,7 @@ protected: ...@@ -84,6 +116,7 @@ protected:
private: private:
GridGraphicsView* m_view; GridGraphicsView* m_view;
QGraphicsScene* m_scene; QGraphicsScene* m_scene;
detail::Graphics_view_zoom* m_zoom;
unsigned m_width; unsigned m_width;
unsigned m_height; unsigned m_height;
unsigned m_pen; unsigned m_pen;
...@@ -91,27 +124,4 @@ private: ...@@ -91,27 +124,4 @@ private:
Alphabet m_alph; 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 #endif // GRIDVIEW_HPP
...@@ -16,6 +16,8 @@ namespace Ui { ...@@ -16,6 +16,8 @@ namespace Ui {
class StructureLibraryView; class StructureLibraryView;
} }
// TODO : Documenter
class StructureLibraryView : public QFrame class StructureLibraryView : public QFrame
{ {
Q_OBJECT Q_OBJECT
...@@ -24,12 +26,13 @@ public: ...@@ -24,12 +26,13 @@ public:
explicit StructureLibraryView(QWidget *parent = nullptr); explicit StructureLibraryView(QWidget *parent = nullptr);
~StructureLibraryView(); ~StructureLibraryView();
protected:
void mouseMoveEvent(QMouseEvent* event);
signals: signals:
void structure_copied(const Structure& s); 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: private:
void load_structures(); void load_structures();
QTreeWidgetItem *add_directory_contents(const QDir& dir); QTreeWidgetItem *add_directory_contents(const QDir& dir);
...@@ -45,6 +48,7 @@ private slots: ...@@ -45,6 +48,7 @@ private slots:
private: private:
Ui::StructureLibraryView *ui; Ui::StructureLibraryView *ui;
QFileSystemWatcher m_watcher; QFileSystemWatcher m_watcher;
unsigned m_cell_pixel_size;
}; };
#endif // STRUCTURELIBRARYVIEW_HPP #endif // STRUCTURELIBRARYVIEW_HPP
...@@ -28,9 +28,9 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av ...@@ -28,9 +28,9 @@ Cette classe représente le widget utilisé pour l'affichage et l'interaction av
#include "structurereader.hpp" #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: public:
GridItem(GridView& in_gridview, QColor color, const QString& state_name, QGraphicsItem *parent = nullptr) GridItem(GridView& in_gridview, QColor color, const QString& state_name, QGraphicsItem *parent = nullptr)
...@@ -191,12 +191,12 @@ GridView::GridView(QWidget *parent) ...@@ -191,12 +191,12 @@ GridView::GridView(QWidget *parent)
m_view->setBackgroundBrush(QBrush(Qt::gray)); m_view->setBackgroundBrush(QBrush(Qt::gray));
m_view->setRubberBandSelectionMode(Qt::IntersectsItemBoundingRect); // provides better performance m_view->setRubberBandSelectionMode(Qt::IntersectsItemBoundingRect); // provides better performance
detail::Graphics_view_zoom* z = new detail::Graphics_view_zoom(m_view); m_zoom = new detail::Graphics_view_zoom(m_view);
(void)z;
QGridLayout *layout = new QGridLayout; QGridLayout *layout = new QGridLayout;
layout->addWidget(m_view); 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); setLayout(layout);
set_current_pen(0); set_current_pen(0);
...@@ -211,6 +211,11 @@ GridView::GridView(QWidget *parent) ...@@ -211,6 +211,11 @@ GridView::GridView(QWidget *parent)
Grid default_grid(m_height, m_width); Grid default_grid(m_height, m_width);
copy_grid(default_grid); 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) void GridView::set_alphabet(const Alphabet &alph)
...@@ -238,6 +243,17 @@ void GridView::set_clipboard(const Structure &s) ...@@ -238,6 +243,17 @@ void GridView::set_clipboard(const Structure &s)
m_copy_paste_buffer = 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) void GridView::copy_grid(const Grid &grid)
{ {
m_scene->clear(); m_scene->clear();
...@@ -406,6 +422,14 @@ void GridView::keyPressEvent(QKeyEvent *event) ...@@ -406,6 +422,14 @@ void GridView::keyPressEvent(QKeyEvent *event)
{ {
paste_clipboard(); 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) ...@@ -13,6 +13,9 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui->action_save_struct, &QAction::triggered, this, &MainWindow::afficher_interface_sauvegarde_structure); 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->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() MainWindow::~MainWindow()
......
...@@ -37,9 +37,9 @@ StructureLibraryView::~StructureLibraryView() ...@@ -37,9 +37,9 @@ StructureLibraryView::~StructureLibraryView()
delete ui; 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() void StructureLibraryView::load_structures()
...@@ -189,11 +189,23 @@ void StructureLibraryView::create_drag(QTreeWidgetItem *item, int column) ...@@ -189,11 +189,23 @@ void StructureLibraryView::create_drag(QTreeWidgetItem *item, int column)
QPixmap pix; QPixmap pix;
QImage img = create_preview_image(s, Qt::transparent); QImage img = create_preview_image(s, Qt::transparent);
pix.convertFromImage(img); 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); mimeData->setText(filename);
drag->setMimeData(mimeData); drag->setMimeData(mimeData);
drag->setPixmap(pix); drag->setPixmap(pix);
drag->setHotSpot(QPoint(m_cell_pixel_size/2, m_cell_pixel_size/2));
drag->exec(Qt::CopyAction); 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