Skip to content

Commit

Permalink
Add a cli
Browse files Browse the repository at this point in the history
  • Loading branch information
MickaelG committed May 26, 2018
1 parent a1b94ed commit 54cfdc9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 27 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ find_package(Qt5Widgets 5.5)
add_subdirectory(lib)
add_subdirectory(src/core)
add_subdirectory(src/gui)
add_subdirectory(src/cli)

install(PROGRAMS tomate.desktop DESTINATION share/applications/)

4 changes: 4 additions & 0 deletions src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

add_executable(tomate_cli main.cpp)
target_link_libraries(tomate_cli tomate_core)

23 changes: 23 additions & 0 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include "xml.h"
#include "dataset.h"

#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char** argv)
{
auto data_file = get_datafile_default_path();
Dataset dataset;
if (xml_read_data(data_file, dataset) != 0) {
std::cerr << "Failed to load data file." << endl;
return -1;
}
for (const auto& crop: dataset.get_crops()) {
auto start_date = crop.get_date(Crop::DateSel::START);
cout << start_date << ": " << crop.get_repr() << endl;
}
return 0;
}
1 change: 1 addition & 0 deletions src/core/plots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "plot.h"
#include "crops.h"
#include "log.h"
using namespace std;

///////////////////////////////////////////////////////////////////////////////
Expand Down
25 changes: 25 additions & 0 deletions src/core/xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ void replaceAll(std::string& str, const std::string& from, const std::string& to
}
}

const string get_datafile_default_path()
{
string data_home;
char const* temp = getenv("XDG_DATA_HOME");
if (temp != nullptr)
{
data_home = string(temp);
}
else
{
string home_dir;
char const* temp = getenv("HOME");
if(temp != nullptr)
{
home_dir = string(temp);
} else {
return "";
}
data_home = home_dir + "/.local/share/";

}

return data_home + "/tomate/data.sfg";
}

int xml_read_data(string filename, Dataset& dataset)
{
xml_document doc;
Expand Down
24 changes: 21 additions & 3 deletions src/core/xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@
#define XML_H

#include <string>
using namespace std;

class Dataset;

int xml_read_data(string filename, Dataset& dataset);
int xml_write_data(string filename,const Dataset& dataset);
/**
* Returns the default path of xml file storing application data.
*
* @return empty if the path cannot be infered
*/
const std::string get_datafile_default_path();

/**
* Reads xml data from given file.
*
* @return 0 if success, or an <0 error code
*/
int xml_read_data(std::string filename, Dataset& dataset);

/**
* Writes xml data to given file.
*
* @return 0 if success, or an <0 error code
*/
int xml_write_data(std::string filename,const Dataset& dataset);

#endif //XML_H
7 changes: 6 additions & 1 deletion src/gui/gui_widgets/EditCropWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "dataset.h"
#include "plot.h"
#include "log.h"

#include <QMessageBox>
#include <string>
Expand Down Expand Up @@ -84,6 +85,10 @@ void EditCropWidget::set_crop_values(Crop* crop)
ui->varInput->setCurrentIndex(plants_model->GetVarietyRowIndex(crop->get_plant()));

Plot* plot_p = plots_model->get_plots().get_for_pos(crop->get_shape());
if (plot_p == nullptr) {
Log::Error("Failed to get plot for selected crop");
return;
}
ui->plotInput->setCurrentIndex(plots_model->GetRowIndex(*plot_p));
Rectangle crop_shape(crop->get_shape().rect());
crop_shape.translate(-plot_p->get_shape().get_min_x(), -plot_p->get_shape().get_min_y());
Expand Down Expand Up @@ -133,7 +138,7 @@ unique_ptr<Crop> EditCropWidget::get_described_crop()

QString note = ui->noteInput->text();

rect.fit_in_other(p_plot->get_shape());
//rect.fit_in_other(p_plot->get_shape());
rect.translate(p_plot->get_shape().get_min_x(), p_plot->get_shape().get_min_y());
CropLocation location(rect);

Expand Down
28 changes: 5 additions & 23 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,13 @@ MainWindow::MainWindow(QWidget *parent) :

void MainWindow::loadData()
{
string data_home;
char const* temp = getenv("XDG_DATA_HOME");
if (temp != NULL)
{
data_home = string(temp);
}
else
{
string home_dir;
char const* temp = getenv("HOME");
if(temp != NULL)
{
home_dir = string(temp);
} else {
QMessageBox::critical(NULL, QObject::tr("Error"),
QObject::tr("Error, HOME environment variable not set."));
return;
}
data_home = home_dir + "/.local/share/";

_data_file = get_datafile_default_path();
if (_data_file.empty()) {
QMessageBox::critical(NULL, QObject::tr("Error"),
QObject::tr("Error, cannot infer default datafile path."));
return;
}

_user_data_dir = data_home + "/tomate/";
_data_file = _user_data_dir + "/data.sfg";

int res = xml_read_data(_data_file, dataset_model.get_dataset());
if (res == -1)
{
Expand Down

0 comments on commit 54cfdc9

Please sign in to comment.