Skip to content

Instantly share code, notes, and snippets.

Created January 13, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f203fb28adf22d8e241a to your computer and use it in GitHub Desktop.
Save anonymous/f203fb28adf22d8e241a to your computer and use it in GitHub Desktop.
// inside my open_project.cpp file (it is a QDialog window)
open_project::open_project(QWidget *parent) :
QDialog(parent),
ui_open_project(new Ui::open_project)
{
ui_open_project->setupUi(this);
locations_name = new QStandardItemModel;
user->initdb();
locations_refresh();
model = new QSqlQueryModel();
model->setQuery("SELECT id, name, comment, refproject, datecreate "
"FROM \"Projects\" "
"WHERE dateclose IS NULL "
"ORDER BY datecreate ;");
ui_open_project->projects_list->setModel(model);
ui_open_project->projects_list->setModelColumn(1);
ui_open_project->projects_list->show();
ui_open_project->checkBox_location->setChecked(false);
ui_open_project->treeView_locations->setEnabled(false);
ui_open_project->pushButton_location->setEnabled(false);
ui_open_project->checkBox_structure->setChecked(false);
ui_open_project->treeView_structures->setEnabled(false);
ui_open_project->pushButton_structure->setEnabled(false);
ui_open_project->treeView_locations->setModel(locations_name);
ui_open_project->treeView_locations->setHeaderHidden(true);
}
open_project::~open_project()
{
delete ui_open_project;
}
void open_project::on_projects_list_clicked(const QModelIndex &index)
{
QString sformat = QString("dd/MM/yyyy %1 hh:mm:ss").arg(tr("à"));
name = model->record(index.row()).value("name").toString();
comment = model->record(index.row()).value("comment").toString();
ref_project = model->record(index.row()).value("refproject").toString();
date_create = model->record(index.row()).value("datecreate").toString();
id = model->record(index.row()).value("id").toInt();
ui_open_project->label_name->setText(name);
ui_open_project->comment->setText(comment);
ui_open_project->ref_client->setText(ref_project);
ui_open_project->date_create->setText(QString("%1")
.arg(QDateTime::fromString(date_create,
QString("yyyy-MM-dd'T'hh:mm:ss"))
.toString(sformat)));
if (id != 0)
locations_refresh();
ui_open_project->treeView_locations->setColumnHidden(1,true);
}
// this is inside my open_location.cpp file... an other one QDialog window (this one is in construction, but you can open it and click the new location button):
open_Location::open_Location(QWidget *parent) :
QDialog(parent),
ui_open_Location(new Ui::open_Location)
{
ui_open_Location->setupUi(this);
}
open_Location::~open_Location()
{
delete ui_open_Location;
}
void open_Location::on_add_to_project_clicked()
{
}
void open_Location::on_create_new_location_clicked()
{
new_location *nl = new new_location(this);
nl->open();
QObject::connect(nl, &QDialog::finished, [nl, this](int result){
if (result)
int a = result;
nl->deleteLater();
});
}
// this is inside my new_location.cpp file... an other one QDialog window:
new_location::new_location(QWidget *parent) :
QDialog(parent),
ui_new_location(new Ui::new_location)
{
ui_new_location->setupUi(this);
locations_name = new QStandardItemModel;
//user->initdb();
database = QSqlDatabase::addDatabase("QPSQL");
database.setDatabaseName("recorder");
database.setHostName("localhost");
if(!database.open(QString("admin"), QString("overover")))
{
QString err = database.lastError().text();
errorBox = new QMessageBox();
errorBox->setInformativeText(QString("erreure lors de l'execution de recherche\ndans la base de donnée:\n%1")
.arg(err));
errorBox->exec();
//qDebug() << QString("La base de donnée ne s'est pas connectée correctement:\n%1")
// .arg(err);
}
locations_refresh();
ui_new_location->treeView_parent->setEnabled(false);
ui_new_location->treeView_parent->setModel(locations_name);
ui_new_location->treeView_parent->setHeaderHidden(true);
ui_new_location->treeView_parent->setColumnHidden(1,true);
}
new_location::~new_location()
{
delete ui_new_location;
}
void new_location::find_Childs(int child_id, QStandardItem *ref) {
QHash<int, QString> child_hash = store_DB_locations[child_id];
foreach(int childId, child_hash.keys()) {
QStandardItem *child_next_a = new QStandardItem(child_hash[childId]); // première colonne => nom du lieu
QStandardItem *child_next_b = new QStandardItem(QString("%1").arg(childId)); // seconde colonne => id
QList<QStandardItem *> child_next_list;
child_next_list << child_next_a << child_next_b;
ref->appendRow(child_next_list);
find_Childs(childId, child_next_a); }
}
void new_location::locations_refresh() {
QSqlQuery query;
query.prepare("SELECT name, id, id_parent FROM \"Locations\""
"ORDER by (id_parent, name) ASC;");
//user->execDB(query);
query.exec();
while(query.next())
store_DB_locations[query.value(2).toInt()].
insert(query.value(1).toInt(),
query.value(0).toString());
QStandardItem *rootNode = locations_name->invisibleRootItem();
find_Childs(0, rootNode);
}
QString new_location::name() const
{
return ui_new_location->lineEdit_name->text() ;
}
QString new_location::comment() const
{
return ui_new_location->textEdit_comment->toPlainText();
}
void new_location::on_pushButton_REC_clicked()
{
QSqlQuery query; //(user->currentDB());
query.prepare("INSERT INTO \"Locations\" "
"(name, comment, id_parent) "
"VALUES (:name, :comment, :parent);");
query.bindValue(":name", name());
query.bindValue(":comment", comment());
query.bindValue(":parent", id_parent);
//user->execDB(query);
query.exec();
accept();
}
void new_location::on_checkBox_parent_stateChanged(int arg1) {
if(ui_new_location->checkBox_parent->isChecked())
ui_new_location->treeView_parent->setEnabled(true);
else
ui_new_location->treeView_parent->setEnabled(false);
}
void new_location::on_treeView_parent_clicked(const QModelIndex &index)
{
int row = index.row();
QModelIndex the_parent = index.parent();
name_location = locations_name->data(locations_name->index(row, 0, the_parent), Qt::EditRole).toString();
id_parent = locations_name->data(locations_name->index(row, 1, the_parent), Qt::EditRole).toInt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment