From aece8b299f94c8f59acf196b6f03be31fd8d8756 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 25 Nov 2019 19:54:24 +0100 Subject: [PATCH] WIP: generalized code. --- src/laybasic/laybasic/layLibrariesView.cc | 4 +- src/laybasic/laybasic/layWidgets.cc | 156 +++++++++++++++++ src/laybasic/laybasic/layWidgets.h | 65 +++++++ .../magic/lay_plugin/MAGReaderOptionPage.ui | 7 +- .../magic/lay_plugin/layMAGReaderPlugin.cc | 164 +----------------- .../magic/lay_plugin/layMAGReaderPlugin.h | 3 - 6 files changed, 236 insertions(+), 163 deletions(-) diff --git a/src/laybasic/laybasic/layLibrariesView.cc b/src/laybasic/laybasic/layLibrariesView.cc index 2e9d44483..5937baf6c 100644 --- a/src/laybasic/laybasic/layLibrariesView.cc +++ b/src/laybasic/laybasic/layLibrariesView.cc @@ -475,7 +475,7 @@ LibrariesView::search_editing_finished () } void -LibrariesView::middle_clicked (const QModelIndex &index) +LibrariesView::middle_clicked (const QModelIndex & /*index*/) { // ... nothing yet .. } @@ -493,7 +493,7 @@ LibrariesView::clicked (const QModelIndex & /*index*/) } void -LibrariesView::double_clicked (const QModelIndex &index) +LibrariesView::double_clicked (const QModelIndex & /*index*/) { // ... nothing yet .. } diff --git a/src/laybasic/laybasic/layWidgets.cc b/src/laybasic/laybasic/layWidgets.cc index 781a77671..4aaa48bf8 100644 --- a/src/laybasic/laybasic/layWidgets.cc +++ b/src/laybasic/laybasic/layWidgets.cc @@ -1143,5 +1143,161 @@ void DecoratedLineEdit::resizeEvent (QResizeEvent * /*event*/) } } +// ------------------------------------------------------------- +// InteractiveListWidget implementation + +InteractiveListWidget::InteractiveListWidget (QWidget *parent) + : QListWidget (parent), m_drag_and_drop_enabled (false) +{ + setSelectionMode (QAbstractItemView::ExtendedSelection); + setDragDropMode (QAbstractItemView::InternalMove); + enable_drag_and_drop (true); } +void +InteractiveListWidget::enable_drag_and_drop (bool f) +{ + if (f != m_drag_and_drop_enabled) { + m_drag_and_drop_enabled = f; + setDragEnabled (f); + refresh_flags (); + } +} + +void +InteractiveListWidget::set_values (const std::vector &values) +{ + clear (); + add_values (values); +} + +std::vector +InteractiveListWidget::get_values () +{ + std::vector v; + v.reserve ((size_t) count ()); + for (int i = 0; i < count (); ++i) { + v.push_back (tl::to_string (item (i)->text ())); + } + return v; +} + +void +InteractiveListWidget::add_value (const std::string &value) +{ + addItem (tl::to_qstring (value)); + refresh_flags (); + clearSelection (); + setCurrentItem (item (count () - 1)); +} + +void +InteractiveListWidget::add_values (const std::vector &values) +{ + for (std::vector::const_iterator i = values.begin (); i != values.end (); ++i) { + addItem (tl::to_qstring (*i)); + } + refresh_flags (); + clearSelection (); +} + +void +InteractiveListWidget::delete_selected_items () +{ + QStringList items; + for (int i = 0; i < count (); ++i) { + if (! item (i)->isSelected ()) { + items.push_back (item (i)->text ()); + } + } + + clear (); + for (QStringList::const_iterator f = items.begin (); f != items.end (); ++f) { + addItem (*f); + } + refresh_flags (); +} + +void +InteractiveListWidget::move_selected_items_up () +{ + std::set selected; + for (int i = 0; i < count (); ++i) { + if (item (i)->isSelected ()) { + selected.insert (item (i)->text ()); + } + } + + QStringList items; + int j = -1; + for (int i = 0; i < count (); ++i) { + if (item (i)->isSelected ()) { + items.push_back (item (i)->text ()); + } else { + if (j >= 0) { + items.push_back (item (j)->text ()); + } + j = i; + } + } + if (j >= 0) { + items.push_back (item (j)->text ()); + } + + clear (); + for (QStringList::const_iterator f = items.begin (); f != items.end (); ++f) { + addItem (*f); + if (selected.find (*f) != selected.end ()) { + item (count () - 1)->setSelected (true); + } + } + refresh_flags (); +} + +void +InteractiveListWidget::move_selected_items_down () +{ + std::set selected; + for (int i = 0; i < count (); ++i) { + if (item (i)->isSelected ()) { + selected.insert (item (i)->text ()); + } + } + + QStringList items; + int j = -1; + for (int i = count (); i > 0; ) { + --i; + if (item (i)->isSelected ()) { + items.push_back (item (i)->text ()); + } else { + if (j >= 0) { + items.push_back (item (j)->text ()); + } + j = i; + } + } + if (j >= 0) { + items.push_back (item (j)->text ()); + } + + clear (); + for (QStringList::const_iterator f = items.end (); f != items.begin (); ) { + --f; + addItem (*f); + if (selected.find (*f) != selected.end ()) { + item (count () - 1)->setSelected (true); + } + } + refresh_flags (); +} + +void +InteractiveListWidget::refresh_flags () +{ + for (int i = 0; i < count (); ++i) { + item (i)->setFlags (Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | (m_drag_and_drop_enabled ? Qt::ItemIsDragEnabled : Qt::ItemFlags ())); + } +} + +} diff --git a/src/laybasic/laybasic/layWidgets.h b/src/laybasic/laybasic/layWidgets.h index a18cb3337..b0bbe846c 100644 --- a/src/laybasic/laybasic/layWidgets.h +++ b/src/laybasic/laybasic/layWidgets.h @@ -31,6 +31,7 @@ #include #include #include +#include #include namespace db @@ -460,6 +461,70 @@ private: int m_default_left_margin, m_default_right_margin; }; +/** + * @brief An interactive liste widget which offers slots to delete and move items and interfaces to std::vector + */ +class LAYBASIC_PUBLIC InteractiveListWidget + : public QListWidget +{ +Q_OBJECT + +public: + /** + * @brief Constructor + */ + InteractiveListWidget (QWidget *parent = 0); + + /** + * @brief Configures this list for drag and drop + * + * Call this method with a "true" value to enable drag and drop or "false" to disable. + * By default, drag and drop is enabled. + */ + void enable_drag_and_drop (bool f); + + /** + * @brief Sets the items in the widget + */ + void set_values (const std::vector &values); + + /** + * @brief Gets the items in the widget + */ + std::vector get_values (); + + /** + * @brief Adds a value + */ + void add_value (const std::string &value); + + /** + * @brief Adds values + */ + void add_values (const std::vector &values); + +private slots: + /** + * @brief Deletes the selected items + */ + void delete_selected_items (); + + /** + * @brief Moves the selected items up + */ + void move_selected_items_up (); + + /** + * @brief Moves the selected items down + */ + void move_selected_items_down (); + +private: + void refresh_flags (); + + bool m_drag_and_drop_enabled; +}; + } // namespace lay #endif diff --git a/src/plugins/streamers/magic/lay_plugin/MAGReaderOptionPage.ui b/src/plugins/streamers/magic/lay_plugin/MAGReaderOptionPage.ui index 3a4d528ac..23c5971da 100644 --- a/src/plugins/streamers/magic/lay_plugin/MAGReaderOptionPage.ui +++ b/src/plugins/streamers/magic/lay_plugin/MAGReaderOptionPage.ui @@ -106,7 +106,7 @@ - + 0 @@ -282,6 +282,11 @@ You can use expressions inside the path components for variable paths enable_all_layers(bool) + + lay::InteractiveListWidget + QListWidget +
layWidgets.h
+
lambda_le diff --git a/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.cc b/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.cc index 3ef663e73..a901dcd33 100644 --- a/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.cc +++ b/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.cc @@ -33,117 +33,6 @@ namespace lay { -// --------------------------------------------------------------- -// List manipulation utilities -// @@@ TODO: move this to a central place -static void -refresh_item_flags (QListWidget *list) -{ - for (int i = 0; i < list->count (); ++i) { - list->item (i)->setFlags (Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled); - } -} - -static void -add_items_to_list (QListWidget *list, const QStringList &items) -{ - for (QStringList::const_iterator f = items.begin (); f != items.end (); ++f) { - list->addItem (*f); - } - refresh_item_flags (list); -} - -static void -delete_selected_items_from_list (QListWidget *list) -{ - QStringList items; - for (int i = 0; i < list->count (); ++i) { - if (! list->item (i)->isSelected ()) { - items.push_back (list->item (i)->text ()); - } - } - - list->clear (); - for (QStringList::const_iterator f = items.begin (); f != items.end (); ++f) { - list->addItem (*f); - } - refresh_item_flags (list); -} - -static void -move_selected_items_up (QListWidget *list) -{ - std::set selected; - for (int i = 0; i < list->count (); ++i) { - if (list->item (i)->isSelected ()) { - selected.insert (list->item (i)->text ()); - } - } - - QStringList items; - int j = -1; - for (int i = 0; i < list->count (); ++i) { - if (list->item (i)->isSelected ()) { - items.push_back (list->item (i)->text ()); - } else { - if (j >= 0) { - items.push_back (list->item (j)->text ()); - } - j = i; - } - } - if (j >= 0) { - items.push_back (list->item (j)->text ()); - } - - list->clear (); - for (QStringList::const_iterator f = items.begin (); f != items.end (); ++f) { - list->addItem (*f); - if (selected.find (*f) != selected.end ()) { - list->item (list->count () - 1)->setSelected (true); - } - } - refresh_item_flags (list); -} - -static void -move_selected_items_down (QListWidget *list) -{ - std::set selected; - for (int i = 0; i < list->count (); ++i) { - if (list->item (i)->isSelected ()) { - selected.insert (list->item (i)->text ()); - } - } - - QStringList items; - int j = -1; - for (int i = list->count (); i > 0; ) { - --i; - if (list->item (i)->isSelected ()) { - items.push_back (list->item (i)->text ()); - } else { - if (j >= 0) { - items.push_back (list->item (j)->text ()); - } - j = i; - } - } - if (j >= 0) { - items.push_back (list->item (j)->text ()); - } - - list->clear (); - for (QStringList::const_iterator f = items.end (); f != items.begin (); ) { - --f; - list->addItem (*f); - if (selected.find (*f) != selected.end ()) { - list->item (list->count () - 1)->setSelected (true); - } - } - refresh_item_flags (list); -} - // --------------------------------------------------------------- // MAGReaderOptionPage definition and implementation @@ -155,9 +44,9 @@ MAGReaderOptionPage::MAGReaderOptionPage (QWidget *parent) connect (mp_ui->add_lib_path, SIGNAL (clicked ()), this, SLOT (add_lib_path_clicked ())); connect (mp_ui->add_lib_path_with_choose, SIGNAL (clicked ()), this, SLOT (add_lib_path_clicked_with_choose ())); - connect (mp_ui->del_lib_path, SIGNAL (clicked ()), this, SLOT (del_lib_paths_clicked ())); - connect (mp_ui->move_lib_path_up, SIGNAL (clicked ()), this, SLOT (move_lib_paths_up_clicked ())); - connect (mp_ui->move_lib_path_down, SIGNAL (clicked ()), this, SLOT (move_lib_paths_down_clicked ())); + connect (mp_ui->del_lib_path, SIGNAL (clicked ()), mp_ui->lib_path, SLOT (delete_selected_items ())); + connect (mp_ui->move_lib_path_up, SIGNAL (clicked ()), mp_ui->lib_path, SLOT (move_selected_items_up ())); + connect (mp_ui->move_lib_path_down, SIGNAL (clicked ()), mp_ui->lib_path, SLOT (move_selected_items_down ())); } MAGReaderOptionPage::~MAGReaderOptionPage () @@ -181,12 +70,7 @@ MAGReaderOptionPage::setup (const db::FormatSpecificReaderOptions *o, const db:: mp_ui->read_all_cbx->setChecked (options->create_other_layers); mp_ui->keep_names_cbx->setChecked (options->keep_layer_names); - mp_ui->lib_path->clear (); - QStringList items; - for (std::vector ::const_iterator f = options->lib_paths.begin (); f != options->lib_paths.end (); ++f) { - items << tl::to_qstring (*f); - } - add_items_to_list (mp_ui->lib_path, items); + mp_ui->lib_path->set_values (options->lib_paths); } void @@ -209,11 +93,7 @@ MAGReaderOptionPage::commit (db::FormatSpecificReaderOptions *o, const db::Techn options->create_other_layers = mp_ui->read_all_cbx->isChecked (); options->keep_layer_names = mp_ui->keep_names_cbx->isChecked (); - options->lib_paths.clear (); - options->lib_paths.reserve (mp_ui->lib_path->count ()); - for (int i = 0; i < mp_ui->lib_path->count (); ++i) { - options->lib_paths.push_back (tl::to_string (mp_ui->lib_path->item (i)->text ())); - } + options->lib_paths = mp_ui->lib_path->get_values (); } } @@ -221,11 +101,7 @@ MAGReaderOptionPage::commit (db::FormatSpecificReaderOptions *o, const db::Techn void MAGReaderOptionPage::add_lib_path_clicked () { - QStringList dirs; - dirs << tr ("Enter your path here .."); - add_items_to_list (mp_ui->lib_path, dirs); - mp_ui->lib_path->clearSelection (); - mp_ui->lib_path->setCurrentItem (mp_ui->lib_path->item (mp_ui->lib_path->count () - 1)); + mp_ui->lib_path->add_value (tl::to_string (tr ("Enter your path here ..."))); } void @@ -233,32 +109,10 @@ MAGReaderOptionPage::add_lib_path_clicked_with_choose () { QString dir = QFileDialog::getExistingDirectory (this, QObject::tr ("Add library path")); if (! dir.isNull ()) { - QStringList dirs; - dirs << dir; - add_items_to_list (mp_ui->lib_path, dirs); - mp_ui->lib_path->clearSelection (); - mp_ui->lib_path->setCurrentItem (mp_ui->lib_path->item (mp_ui->lib_path->count () - 1)); + mp_ui->lib_path->add_value (tl::to_string (dir)); } } -void -MAGReaderOptionPage::del_lib_paths_clicked () -{ - delete_selected_items_from_list (mp_ui->lib_path); -} - -void -MAGReaderOptionPage::move_lib_paths_up_clicked () -{ - move_selected_items_up (mp_ui->lib_path); -} - -void -MAGReaderOptionPage::move_lib_paths_down_clicked () -{ - move_selected_items_down (mp_ui->lib_path); -} - // --------------------------------------------------------------- // MAGReaderPluginDeclaration definition and implementation @@ -287,7 +141,3 @@ static tl::RegisteredClass plugin_decl (new lay::MAGRead } - - - - diff --git a/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.h b/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.h index a0658cada..1dbfae662 100644 --- a/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.h +++ b/src/plugins/streamers/magic/lay_plugin/layMAGReaderPlugin.h @@ -51,9 +51,6 @@ public: private slots: void add_lib_path_clicked (); void add_lib_path_clicked_with_choose (); - void del_lib_paths_clicked (); - void move_lib_paths_up_clicked (); - void move_lib_paths_down_clicked (); private: Ui::MAGReaderOptionPage *mp_ui;