From e2f17afda0f0376ba8976bee7927ba5e52e06800 Mon Sep 17 00:00:00 2001
From: yboucher <yann.boucher@etu.utc.fr>
Date: Mon, 7 Jun 2021 21:16:34 +0200
Subject: [PATCH] Support de la couleur pour les preview

---
 include/structurelibraryview.hpp |  8 +++++++-
 src/interface.cpp                |  1 +
 src/structurelibraryview.cpp     | 17 ++++++++++++-----
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/include/structurelibraryview.hpp b/include/structurelibraryview.hpp
index 1b1523e..b8040f9 100644
--- a/include/structurelibraryview.hpp
+++ b/include/structurelibraryview.hpp
@@ -16,6 +16,7 @@ Widget de la bibliothèque de structures.
 #include <QFileSystemWatcher>
 
 #include "structure.hpp"
+#include "alphabet.hpp"
 
 namespace Ui {
 class StructureLibraryView;
@@ -32,6 +33,10 @@ public:
     explicit StructureLibraryView(QWidget *parent = nullptr);
     ~StructureLibraryView();
 
+    //! \brief Spécifie l'alphabet à utiliser pour les couleurs de la preview
+    void set_alphabet(const Alphabet& alph)
+    { m_alph = alph; }
+
 signals:
     //! \brief Signal émis lorsqu'une structure de la bibliothèque est copiée.
     //! \param s La structure copiée.
@@ -47,7 +52,7 @@ private:
     QTreeWidgetItem *add_directory_contents(const QDir& dir);
     bool try_load_structure(const QString& filename, Structure& s);
     void update_preview(const Structure& s);
-    QImage create_preview_image(const Structure &s, QColor background);
+    QImage create_preview_image(const Structure &s, bool transparent);
     void create_drag(QTreeWidgetItem* item, int column);
 
 private slots:
@@ -58,6 +63,7 @@ private:
     Ui::StructureLibraryView *ui;
     QFileSystemWatcher m_watcher;
     unsigned m_cell_pixel_size;
+    Alphabet m_alph;
 };
 
 #endif // STRUCTURELIBRARYVIEW_HPP
diff --git a/src/interface.cpp b/src/interface.cpp
index 06791c8..2c8c170 100644
--- a/src/interface.cpp
+++ b/src/interface.cpp
@@ -501,6 +501,7 @@ void MainWindow::load_from_image()
 void MainWindow::ui_update_alphabet(const Alphabet &alpha)
 {
     simulation.setAlphabet(alpha);
+    ui->struct_library->set_alphabet(alpha);
     ui->grid_view->set_alphabet(alpha);
     ui->nbrStateComboBox->clear();
     for (unsigned i = 0; i < alpha.taille(); ++i)
diff --git a/src/structurelibraryview.cpp b/src/structurelibraryview.cpp
index 7bd4b16..7314f11 100644
--- a/src/structurelibraryview.cpp
+++ b/src/structurelibraryview.cpp
@@ -117,19 +117,26 @@ void StructureLibraryView::update_preview(const Structure &s)
     QPixmap pix;
 
     /** to check wether load ok */
-    if(pix.convertFromImage(create_preview_image(s, Qt::white))){
+    if(pix.convertFromImage(create_preview_image(s, false))){
         /** scale pixmap to fit in label'size and keep ratio of pixmap */
         pix = pix.scaled(ui->preview->size() - 2*QSize(ui->preview->lineWidth(), ui->preview->lineWidth()),Qt::KeepAspectRatio);
         ui->preview->setPixmap(pix);
     }
 }
 
-QImage StructureLibraryView::create_preview_image(const Structure &s, QColor background)
+QImage StructureLibraryView::create_preview_image(const Structure &s, bool transparent)
 {
+    stateColor bg = m_alph.getState(0).getColor();
     QImage img(s.width(), s.height(), QImage::Format_ARGB32);
-    img.fill(background);
+    if (transparent)
+        img.fill(Qt::transparent);
+    else
+        img.fill(Qt::white);
     for (const auto& cell : s)
-        img.setPixelColor(cell.first.x, cell.first.y, QColor(Qt::black));
+    {
+        stateColor s_c = m_alph.getState(cell.second % m_alph.taille()).getColor();
+        img.setPixelColor(cell.first.x, cell.first.y, QColor(s_c.getRed(), s_c.getGreen(), s_c.getBlue()));
+    }
 
     return img;
 }
@@ -193,7 +200,7 @@ void StructureLibraryView::create_drag(QTreeWidgetItem *item, int column)
 
     QString filename = QFileInfo(item->data(0, Qt::UserRole).toString()).absoluteFilePath();
     QPixmap pix;
-    QImage img = create_preview_image(s, Qt::transparent);
+    QImage img = create_preview_image(s, true);
     pix.convertFromImage(img);
 
 
-- 
GitLab