Commit fec40e59 authored by Floréal Cabanettes's avatar Floréal Cabanettes

Add option to customize units, Fixes #13

parent c9dbe9dc
......@@ -114,6 +114,7 @@ public:
static bool validate_quantity(QString qte);
static QStringList get_wp_tags(QVariantList tags);
static QString get_img_wp_tag(QString img);
static void loadUnits();
};
#endif // FUNCTIONS_H
......@@ -38,6 +38,7 @@
#include "coupdecoeur.h"
#include "opendistant.h"
#include "searchupdates.h"
#include "units.h"
#include <QMainWindow>
......@@ -232,6 +233,8 @@ private slots:
void received_recipe_wp(HttpRequestWorker *worker);
void on_actionG_rer_les_unit_s_triggered();
public slots:
void init();
void config();
......@@ -271,6 +274,7 @@ private:
void toggleEditPict();
void resetIngrFields();
void resetMatFields();
void refreshUnits();
private:
void closeEvent (QCloseEvent *event);
......
#ifndef UNITS_H
#define UNITS_H
#include <QDialog>
#include <QRegExp>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
namespace Ui {
class Units;
}
class Units : public QDialog
{
Q_OBJECT
public:
explicit Units(QWidget *parent = 0);
~Units();
void init();
private slots:
void on_buttonBox_accepted();
private:
Ui::Units *ui;
void saveUnits();
};
#endif // UNITS_H
......@@ -39,7 +39,8 @@ SOURCES += src/main.cpp\
src/sendwordpress.cpp \
src/sendpywebcooking.cpp \
src/filenotfoundexception.cpp \
src/globalexception.cpp
src/globalexception.cpp \
src/units.cpp
HEADERS += headers/qrecipewriter.h \
headers/options.h \
......@@ -64,7 +65,8 @@ HEADERS += headers/qrecipewriter.h \
headers/sendwordpress.h \
headers/sendpywebcooking.h \
headers/filenotfoundexception.h \
headers/globalexception.h
headers/globalexception.h \
headers/units.h
FORMS += ui/qrecipewriter.ui \
ui/options.ui \
......@@ -76,7 +78,8 @@ FORMS += ui/qrecipewriter.ui \
ui/qinputmultiline.ui \
ui/coupdecoeur.ui \
ui/opendistant.ui \
ui/login.ui
ui/login.ui \
ui/units.ui
RESOURCES += \
Ressources.qrc
......
......@@ -48,6 +48,7 @@ extern bool checkF7beforeSend;
extern bool autoCheckUpdt;
extern QMap<QString, QString> liens;
extern QStringList units;
/**
* @brief Functions::Functions
......@@ -1718,3 +1719,15 @@ QStringList Functions::get_wp_tags(QVariantList tags) {
}
return tags_list;
}
void Functions::loadUnits() {
QFile file(confDir + "/.units");
if (file.exists()) {
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "Error reading units", file.errorString());
}
QTextStream in(&file);
QString my_units = in.readAll().replace("\n", "");
units = my_units.split(QRegExp("\\s*,\\s*"));
}
}
......@@ -135,7 +135,7 @@ int main(int argc, char *argv[])
#endif
//Default units:
units << "" << "ml" << "cl" << "dl" << "L" << "g" << "kg" << "cs" << "cc" << QObject::tr("tasse") << QObject::tr("verre") << QObject::tr("bol") << QObject::tr("sachet") << QObject::tr("pincée");
units << "ml" << "cl" << "dl" << "L" << "g" << "kg" << "cs" << "cc" << QObject::tr("tasse") << QObject::tr("verre") << QObject::tr("bol") << QObject::tr("sachet") << QObject::tr("pincée");
//Define confFile:
confFile = confDir +".config";
......
......@@ -463,6 +463,13 @@ void QRecipeWriter::init()
updtUrl = "https://qrecipewriter.coolcooking.fr/files/LATEST-" + systExp;
//Set units:
Functions::loadUnits();
this->refreshUnits();
}
void QRecipeWriter::refreshUnits() {
ui->unit_ingr->clear();
ui->unit_ingr->addItem("");
ui->unit_ingr->addItems(units);
}
......@@ -726,6 +733,18 @@ void QRecipeWriter::on_actionGerer_les_categories_triggered()
resetCats();
}
/**
* @brief QRecipeWriter::on_actionG_rer_les_unit_s_triggered
* Start to choose units used by the program
*/
void QRecipeWriter::on_actionG_rer_les_unit_s_triggered()
{
Units *units = new Units(this);
units->init();
units->exec();
this->refreshUnits();
}
/**
* @brief QRecipeWriter::on_mainPicture_clicked
* Change main picture of the recipe
......
#include "headers/units.h"
#include "ui_units.h"
extern QString confDir;
extern QStringList units;
Units::Units(QWidget *parent) :
QDialog(parent),
ui(new Ui::Units)
{
ui->setupUi(this);
}
Units::~Units()
{
delete ui;
}
void Units::init() {
ui->units_input->setText(units.join(","));
}
void Units::on_buttonBox_accepted()
{
units = ui->units_input->text().split(QRegExp("\\s*,\\s*"));
this->saveUnits();
}
void Units::saveUnits() {
QString filename = confDir + "/.units";
QFile file(filename);
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << ui->units_input->text() << endl;
}
else {
QMessageBox::critical((QWidget*)this->parent(), tr("Sauvegarde impossible"), tr("Impossible de sauvegarder les unités."));
}
}
......@@ -167,9 +167,9 @@ left: 10px; padding: 0 3px;}</string>
<widget class="QWidget" name="scrollAreaWidgetContents_2">
<property name="geometry">
<rect>
<x>-11</x>
<x>0</x>
<y>0</y>
<width>355</width>
<width>348</width>
<height>460</height>
</rect>
</property>
......@@ -270,7 +270,7 @@ left: 10px; padding: 0 3px;}</string>
<rect>
<x>0</x>
<y>0</y>
<width>346</width>
<width>340</width>
<height>92</height>
</rect>
</property>
......@@ -1524,8 +1524,8 @@ left: 10px; padding: 0 3px;}</string>
<rect>
<x>0</x>
<y>0</y>
<width>673</width>
<height>645</height>
<width>675</width>
<height>643</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
......@@ -2586,7 +2586,7 @@ left: 10px; padding: 0 3px;}</string>
<x>0</x>
<y>0</y>
<width>1149</width>
<height>19</height>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuOutils">
......@@ -2598,6 +2598,7 @@ left: 10px; padding: 0 3px;}</string>
<addaction name="actionCorrection_orthographique"/>
<addaction name="actionOptions"/>
<addaction name="actionGerer_les_categories"/>
<addaction name="actionG_rer_les_unit_s"/>
<addaction name="actionRechercher_une_mise_jour"/>
<addaction name="actionAide"/>
<addaction name="action_propos"/>
......@@ -2732,6 +2733,14 @@ left: 10px; padding: 0 3px;}</string>
<string>Ctrl+Shift+O</string>
</property>
</action>
<action name="actionG_rer_les_unit_s">
<property name="text">
<string>Gérer les unités</string>
</property>
<property name="shortcut">
<string>F9</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Units</class>
<widget class="QDialog" name="Units">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>135</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>135</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>135</height>
</size>
</property>
<property name="windowTitle">
<string>Unités disponibles pour les ingrédients</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Liste des unités (séparées par une virgule) :</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="units_input"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Units</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Units</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
Markdown is supported
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