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

Ebauche d'implémentation d'une bibliothèque de structures, ajout d'un dossier...

Ebauche d'implémentation d'une bibliothèque de structures, ajout d'un dossier patterns/ contenant quelques structures d'exemple
parent 202e1d3c
Pipeline #78250 passed with stages
in 17 seconds
...@@ -222,6 +222,22 @@ pattern recorded :</string> ...@@ -222,6 +222,22 @@ pattern recorded :</string>
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Automaton</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QVBoxLayout" name="settingsLayout"> <layout class="QVBoxLayout" name="settingsLayout">
<item> <item>
...@@ -515,6 +531,27 @@ pattern recorded :</string> ...@@ -515,6 +531,27 @@ pattern recorded :</string>
</layout> </layout>
</item> </item>
</layout> </layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Structures</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="StructureLibraryView" name="struct_library">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</item> </item>
</layout> </layout>
</widget> </widget>
...@@ -524,7 +561,7 @@ pattern recorded :</string> ...@@ -524,7 +561,7 @@ pattern recorded :</string>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1089</width> <width>1089</width>
<height>26</height> <height>20</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFichier"> <widget class="QMenu" name="menuFichier">
...@@ -564,6 +601,12 @@ pattern recorded :</string> ...@@ -564,6 +601,12 @@ pattern recorded :</string>
<header>gridview.hpp</header> <header>gridview.hpp</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>StructureLibraryView</class>
<extends>QFrame</extends>
<header>structurelibraryview.hpp</header>
<container>1</container>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>StructureLibraryView</class>
<widget class="QFrame" name="StructureLibraryView">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Frame</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTreeWidget" name="tree">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>5</verstretch>
</sizepolicy>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>3</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Title : </string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="struct_title">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_struct_author">
<property name="text">
<string>Author : </string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Date : </string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="struct_date">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Description : </string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QPlainTextEdit" name="struct_desc">
<property name="enabled">
<bool>true</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="struct_author">
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_copy">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Copy Structure</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
...@@ -54,6 +54,8 @@ public: ...@@ -54,6 +54,8 @@ public:
void set_current_pen(unsigned state); void set_current_pen(unsigned state);
unsigned current_pen() const; unsigned current_pen() const;
void set_clipboard(const Structure& s);
void copy_grid(const Grid& grid); void copy_grid(const Grid& grid);
Grid get_grid() const; Grid get_grid() const;
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "property.hpp" #include "property.hpp"
class Structure;
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } namespace Ui { class MainWindow; }
QT_END_NAMESPACE QT_END_NAMESPACE
...@@ -56,6 +58,9 @@ private slots: ...@@ -56,6 +58,9 @@ private slots:
void on_nbrStatesComboBox_currentTextChanged(const QString &arg1); void on_nbrStatesComboBox_currentTextChanged(const QString &arg1);
//! \brief Copie la structure choisie dans la bibliothèque dans le presse-papier du GridView
void copy_structure_clicked(const Structure& s);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
}; };
......
/*
* <32021 by Stellaris. Copying Art is an act of love. Love is not subject to law.
*/
#ifndef STRUCTURELIBRARYVIEW_HPP
#define STRUCTURELIBRARYVIEW_HPP
#include <QFrame>
#include <QDir>
#include <QTreeWidgetItem>
#include "structure.hpp"
namespace Ui {
class StructureLibraryView;
}
class StructureLibraryView : public QFrame
{
Q_OBJECT
public:
explicit StructureLibraryView(QWidget *parent = nullptr);
~StructureLibraryView();
signals:
void structure_copied(const Structure& s);
private:
void load_structures();
QTreeWidgetItem *add_directory_contents(const QDir& dir);
bool load_structure(const QString& filename, Structure& s);
private slots:
void update_info(QTreeWidgetItem* item, int column);
void copy_button_clicked();
private:
Ui::StructureLibraryView *ui;
};
#endif // STRUCTURELIBRARYVIEW_HPP
#C This is a glider.
x = 3, y = 3
bo$2bo$3o!
\ No newline at end of file
{
"author": "Bob",
"cells": [
{
"state": 3,
"x": 0,
"y": 0
},
{
"state": 1,
"x": 1,
"y": 2
},
{
"state": 5,
"x": 5,
"y": 8
}
],
"date": "28/07/2001",
"desc": "Lorem ipsum",
"title": "Foo"
}
\ No newline at end of file
...@@ -164,6 +164,11 @@ unsigned GridView::current_pen() const ...@@ -164,6 +164,11 @@ unsigned GridView::current_pen() const
return m_pen; return m_pen;
} }
void GridView::set_clipboard(const Structure &s)
{
m_copy_paste_buffer = s;
}
void GridView::copy_grid(const Grid &grid) void GridView::copy_grid(const Grid &grid)
{ {
m_scene->clear(); m_scene->clear();
......
...@@ -12,6 +12,7 @@ MainWindow::MainWindow(QWidget *parent) ...@@ -12,6 +12,7 @@ MainWindow::MainWindow(QWidget *parent)
ui->setupUi(this); ui->setupUi(this);
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);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
...@@ -213,3 +214,9 @@ void MainWindow::on_nbrStatesComboBox_currentTextChanged(const QString &arg1) ...@@ -213,3 +214,9 @@ void MainWindow::on_nbrStatesComboBox_currentTextChanged(const QString &arg1)
} }
} }
void MainWindow::copy_structure_clicked(const Structure &s)
{
ui->grid_view->set_clipboard(s);
statusBar()->showMessage("Structure copied.", 5000);
}
...@@ -25,7 +25,8 @@ SOURCES += \ ...@@ -25,7 +25,8 @@ SOURCES += \
history.cpp \ history.cpp \
vonNeumannNeighborhoodRule.cpp \ vonNeumannNeighborhoodRule.cpp \
structurewriter.cpp \ structurewriter.cpp \
structuresavingdialog.cpp structuresavingdialog.cpp \
structurelibraryview.cpp
HEADERS += \ HEADERS += \
../include/coord.hpp \ ../include/coord.hpp \
...@@ -46,14 +47,26 @@ HEADERS += \ ...@@ -46,14 +47,26 @@ HEADERS += \
../include/stateColor.hpp \ ../include/stateColor.hpp \
../include/state.hpp \ ../include/state.hpp \
../include/history.h \ ../include/history.h \
../include/structuresavingdialog.hpp ../include/structuresavingdialog.hpp \
../include/structurelibraryview.hpp
FORMS += \ FORMS += \
../forms/interface.ui \ ../forms/interface.ui \
../forms/structuresavingdialog.ui ../forms/structuresavingdialog.ui \
../forms/structurelibraryview.ui
# Default rules for deployment. # Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target !isEmpty(target.path): INSTALLS += target
CONFIG(debug, debug|release) {
DEBUG_OR_RELEASE = debug
} else {
DEBUG_OR_RELEASE = release
}
# On copie les fichiers de pattern vers le dossier d'exécution
message($$system($$QMAKE_COPY_DIR $$shell_quote($$PWD/../patterns) $$shell_quote($$OUT_PWD/$$DEBUG_OR_RELEASE/patterns)))
message($$system($$QMAKE_COPY_DIR $$shell_quote($$PWD/../patterns) $$shell_quote($$OUT_PWD/patterns)))
/*
* <32021 by Stellaris. Copying Art is an act of love. Love is not subject to law.
*/
#include "structurelibraryview.hpp"
#include "ui_structurelibraryview.h"
#include "structurereader.hpp"
#include <QFile>
#include <QTextStream>
#include <QDebug>
StructureLibraryView::StructureLibraryView(QWidget *parent) :
QFrame(parent),
ui(new Ui::StructureLibraryView)
{
ui->setupUi(this);
load_structures();
connect(ui->tree, &QTreeWidget::itemClicked, this, &StructureLibraryView::update_info);
connect(ui->button_copy, &QPushButton::pressed, this, &StructureLibraryView::copy_button_clicked);
}
StructureLibraryView::~StructureLibraryView()
{
delete ui;
}
void StructureLibraryView::load_structures()
{
QTreeWidgetItem* pattern_list = add_directory_contents(QDir("patterns/"));
pattern_list->setText(0, "Patterns");
ui->tree->addTopLevelItem(pattern_list);
pattern_list->setExpanded(true);
}
QTreeWidgetItem *StructureLibraryView::add_directory_contents(const QDir &dir)
{
QTreeWidgetItem* root = new QTreeWidgetItem;
QFileInfoList dirs = dir.entryInfoList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
Q_FOREACH (QFileInfo dir, dirs)
{
QTreeWidgetItem *child = add_directory_contents(QDir(dir.absoluteFilePath()));
// empty directory
if (child->childCount() == 0)
continue;
child->setText(0, dir.fileName());
root->addChild(child);
}
QFileInfoList files = dir.entryInfoList(QStringList() << "*.rle" << "*.json", QDir::Files);
Q_FOREACH (QFileInfo file, files)
{
Structure s;
bool valid = load_structure(file.absoluteFilePath(), s);
if (!valid)
continue;
QTreeWidgetItem *child = new QTreeWidgetItem();
child->setText(0, file.fileName());
// data is the complete filepath of the structure, store it so it can be easily accessed when an item is selected
child->setData(0, Qt::UserRole, file.absoluteFilePath());
root->addChild(child);
}
return root;
}
bool StructureLibraryView::load_structure(const QString &filename, Structure &s)
{
QFile f(filename);
if (!f.open(QFile::ReadOnly | QFile::Text))
return false;
QTextStream in(&f);
std::string data = in.readAll().toStdString();
if (QFileInfo(filename).suffix() == "json")
{
JSONStructureReader json(data);
try
{
s = json.read_structure();
return true;
}
catch (const StructureReaderException& e)
{
qDebug() << "StructureReaderException : " << e.what() << "\n";
return false;
}
}
else if (QFileInfo(filename).suffix() == "rle")
{
RLEStructureReader rle(data);
try
{
s = rle.read_structure();
return true;
}
catch (const StructureReaderException& e)
{
qDebug() << "StructureReaderException : " << e.what() << "\n";
return false;
}
}
else
return false;
}
void StructureLibraryView::update_info(QTreeWidgetItem *item, int column)
{
(void)column;
Structure s;
if (!load_structure(item->data(0, Qt::UserRole).toString(), s))
{
ui->button_copy->setEnabled(false);
return;
}
ui->struct_author->setText(QString::fromStdString(s.author));
ui->struct_date->setText(QString::fromStdString(s.date));
ui->struct_title->setText(QString::fromStdString(s.title));
ui->struct_desc->setPlainText(QString::fromStdString(s.desc));
ui->button_copy->setEnabled(true);
}
void StructureLibraryView::copy_button_clicked()
{
if (ui->tree->selectedItems().size() == 0)
return;
QTreeWidgetItem* item = ui->tree->selectedItems()[0];
Structure s;
if (!load_structure(item->data(0, Qt::UserRole).toString(), s))
return;
emit structure_copied(s);
}
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