From 80886d891b4af175b74a36d799cba2bf252d2e52 Mon Sep 17 00:00:00 2001 From: ribes Date: Wed, 12 Oct 2011 16:29:41 +0000 Subject: [PATCH] Finaly a correct management of selection in TableView --- src/genericgui/BL_GenericGui.cxx | 50 ++++++++++++++++++++++++++++---- src/genericgui/BL_GenericGui.hxx | 2 ++ src/genericgui/BL_JobTab.cxx | 7 ----- src/genericgui/BL_JobTab.hxx | 3 +- src/genericgui/BL_JobsTable.cxx | 45 +++++++++++++++++++++++----- src/genericgui/BL_JobsTable.hxx | 8 ++++- 6 files changed, 92 insertions(+), 23 deletions(-) diff --git a/src/genericgui/BL_GenericGui.cxx b/src/genericgui/BL_GenericGui.cxx index 8eb1fed..85603c1 100644 --- a/src/genericgui/BL_GenericGui.cxx +++ b/src/genericgui/BL_GenericGui.cxx @@ -112,6 +112,7 @@ BL::GenericGui::createCentralWidget() /* Jobs Table */ _jobs_table = new BL::JobsTable(_tab_parent); _jobs_table->setModel(_model); + _jobs_table->set_main_gui(this); /* Job Tab */ _job_tab = new BL::JobTab(_tab_parent, _jobs_manager); @@ -139,10 +140,8 @@ BL::GenericGui::createCentralWidget() connect(_model, SIGNAL(itemChanged(QStandardItem*)), _job_tab, SLOT(itemChanged(QStandardItem*))); // TableView -> X - connect(_jobs_table, SIGNAL(clicked(QModelIndex)), _job_tab, SLOT(job_selected(QModelIndex))); connect(_jobs_table, SIGNAL(clicked(QModelIndex)), this, SLOT(job_selected(QModelIndex))); connect(_jobs_table, SIGNAL(activated(QModelIndex)), this, SLOT(job_activated(QModelIndex))); - connect(_jobs_table, SIGNAL(activated(QModelIndex)), _job_tab, SLOT(job_activated(QModelIndex))); } void @@ -317,20 +316,36 @@ BL::GenericGui::delete_job_internal() } } +void +BL::GenericGui::reset_job_selection() +{ + _job_name_selected = ""; + _job_tab->reset(""); + updateButtonsStates(); +} + void BL::GenericGui::job_selected(const QModelIndex & index) { DEBTRACE("BL::GenericGui::job_selected"); - if (index.row() >= 0) + + //Find if a job is selected (ignore index) + //If Multiple job are selected, take first job + QItemSelectionModel * selection_model = _jobs_table->selectionModel(); + QModelIndexList selected_rows = selection_model->selectedRows(); + if (selected_rows.length() >= 1) { - QStandardItem * item = _model->itemFromIndex(index); + QModelIndex idx = selected_rows[0]; + QStandardItem * item = _model->itemFromIndex(idx); QStandardItem * item_name = _model->item(item->row()); _job_name_selected = item_name->text(); + _job_tab->job_selected(idx); DEBTRACE("BL::GenericGui::job_selected name is " << _job_name_selected.toStdString()); } else { _job_name_selected = ""; + _job_tab->reset(""); DEBTRACE("BL::GenericGui::job_selected - no jobs are selected"); } updateButtonsStates(); @@ -370,8 +385,31 @@ BL::GenericGui::updateButtonsStates() _delete_job_action->setEnabled(true); _buttons->enable_delete_button(); - _stop_job_action->setEnabled(false); - _buttons->disable_stop_button(); + // Enable stop ? + bool enable_stop = true; + QModelIndexList selected_rows = _jobs_table->selectionModel()->selectedRows(); + for (int i = 0; i < selected_rows.length(); ++i) + { + QString job_name = _model->itemFromIndex(selected_rows[i])->text(); + DEBTRACE(job_name.toStdString()); + + BL::Job * job = _jobs_manager->getJob(job_name.toStdString()); + BL::Job::State job_state = job->getState(); + if (job_state != BL::Job::QUEUED && + job_state != BL::Job::RUNNING && + job_state != BL::Job::PAUSED) + enable_stop = false; + } + if (enable_stop) + { + _stop_job_action->setEnabled(true); + _buttons->enable_stop_button(); + } + else + { + _stop_job_action->setEnabled(false); + _buttons->disable_stop_button(); + } _get_results_job_action->setEnabled(false); _buttons->disable_get_results_button(); diff --git a/src/genericgui/BL_GenericGui.hxx b/src/genericgui/BL_GenericGui.hxx index 3f4a33f..a308828 100644 --- a/src/genericgui/BL_GenericGui.hxx +++ b/src/genericgui/BL_GenericGui.hxx @@ -58,6 +58,8 @@ namespace BL void deleteDockWidget(); void delete_job_external(const QString & name); + void reset_job_selection(); + public slots: void create_job(); diff --git a/src/genericgui/BL_JobTab.cxx b/src/genericgui/BL_JobTab.cxx index d7b45d6..9b1baed 100644 --- a/src/genericgui/BL_JobTab.cxx +++ b/src/genericgui/BL_JobTab.cxx @@ -157,13 +157,6 @@ BL::JobTab::createJobFilesTab() insertTab(1, _files_tab, "Job Files"); } -void -BL::JobTab::job_activated(const QModelIndex & index) -{ - DEBTRACE("BL::JobTab::job_activated slot"); - job_selected(index); -} - void BL::JobTab::job_selected(const QModelIndex & index) { diff --git a/src/genericgui/BL_JobTab.hxx b/src/genericgui/BL_JobTab.hxx index 8d067f5..7587d2f 100644 --- a/src/genericgui/BL_JobTab.hxx +++ b/src/genericgui/BL_JobTab.hxx @@ -38,10 +38,9 @@ namespace BL void createJobSummaryTab(); void createJobFilesTab(); + void job_selected(const QModelIndex & index); public slots: - void job_selected(const QModelIndex & index); - void job_activated(const QModelIndex & index); void itemChanged(QStandardItem * item); void reset(QString job_name); diff --git a/src/genericgui/BL_JobsTable.cxx b/src/genericgui/BL_JobsTable.cxx index 60eba5b..5782465 100644 --- a/src/genericgui/BL_JobsTable.cxx +++ b/src/genericgui/BL_JobsTable.cxx @@ -19,12 +19,14 @@ #include "BL_JobsTable.hxx" #include "BL_Traces.hxx" +#include "BL_GenericGui.hxx" BL::JobsTable::JobsTable(QWidget *parent) : QTableView(parent) { DEBTRACE("Creating BL::JobsTable"); BL_ASSERT(parent); _parent = parent; + _main_gui = NULL; // Init setShowGrid(false); @@ -60,28 +62,57 @@ BL::JobsTable::selectCurrent() return false; } +void +BL::JobsTable::set_main_gui(BL::GenericGui * main_gui) +{ + _main_gui = main_gui; +} + +void +BL::JobsTable::selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected ) +{ + QTableView::selectionChanged(selected, deselected); + DEBTRACE("selection changed"); + QModelIndexList selected_rows = selectionModel()->selectedRows(); + if (selected_rows.length() == 0) + { + _main_gui->reset_job_selection(); + } + else if (selected_rows.length() == 1) + { + DEBTRACE("SELECTED CHANGED ACTIVATION !!!"); + DEBTRACE("ROW NUMBER: " << selected_rows[0].row()); + QModelIndex current = selected_rows[0]; + if (!selectionModel()->isSelected(current)) + { + setCurrentIndex(current); + } + activated(current); + } + DEBTRACE("Number of selected rows selection: " << selected_rows.length()); +} + void BL::JobsTable::currentChanged(const QModelIndex & current, const QModelIndex & previous) { + QTableView::currentChanged(current, previous); DEBTRACE("current changed"); DEBTRACE("current row: " << current.row()); if (current.row() > -1) if (!isMultipleSelected()) { DEBTRACE("CURRENT CHANGED ACTIVATION !!!"); - setCurrentIndex(current); - activated(current); + //setCurrentIndex(current); + //activated(current); } } bool BL::JobsTable::isMultipleSelected() { - QModelIndexList list_of_selected = selectedIndexes(); - int length = list_of_selected.length(); - length = length / model()->columnCount(); - DEBTRACE("Number of selected rows: " << length); - if (length > 1) + QModelIndexList selected_rows = selectionModel()->selectedRows(); + DEBTRACE("Number of selected rows: " << selected_rows.length()); + if (selected_rows.length() > 1) return true; else return false; diff --git a/src/genericgui/BL_JobsTable.hxx b/src/genericgui/BL_JobsTable.hxx index 9d48557..2f850bf 100644 --- a/src/genericgui/BL_JobsTable.hxx +++ b/src/genericgui/BL_JobsTable.hxx @@ -22,8 +22,9 @@ #include -namespace BL +namespace BL { + class GenericGui; class JobsTable: public QTableView { Q_OBJECT @@ -35,12 +36,17 @@ namespace BL bool selectCurrent(); bool isMultipleSelected(); QModelIndexList getSelectedIndexes(); + void set_main_gui(BL::GenericGui * main_gui); protected: QWidget* _parent; protected slots: + void selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected ); void currentChanged(const QModelIndex & current, const QModelIndex & previous); + + private: + BL::GenericGui * _main_gui; }; } -- 2.39.2