WIP: generalized code.

This commit is contained in:
Matthias Koefferlein 2019-11-25 19:54:24 +01:00
parent 2acb66182a
commit aece8b299f
6 changed files with 236 additions and 163 deletions

View File

@ -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 ..
}

View File

@ -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<std::string> &values)
{
clear ();
add_values (values);
}
std::vector<std::string>
InteractiveListWidget::get_values ()
{
std::vector<std::string> 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<std::string> &values)
{
for (std::vector<std::string>::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<QString> 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<QString> 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 ()));
}
}
}

View File

@ -31,6 +31,7 @@
#include <QLabel>
#include <QLineEdit>
#include <QProxyStyle>
#include <QListWidget>
#include <string>
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<std::string>
*/
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<std::string> &values);
/**
* @brief Gets the items in the widget
*/
std::vector<std::string> get_values ();
/**
* @brief Adds a value
*/
void add_value (const std::string &value);
/**
* @brief Adds values
*/
void add_values (const std::vector<std::string> &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

View File

@ -106,7 +106,7 @@
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" rowspan="5">
<widget class="QListWidget" name="lib_path">
<widget class="lay::InteractiveListWidget" name="lib_path">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Ignored">
<horstretch>0</horstretch>
@ -282,6 +282,11 @@ You can use expressions inside the path components for variable paths</string>
<signal>enable_all_layers(bool)</signal>
</slots>
</customwidget>
<customwidget>
<class>lay::InteractiveListWidget</class>
<extends>QListWidget</extends>
<header>layWidgets.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>lambda_le</tabstop>

View File

@ -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<QString> 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<QString> 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 <std::string>::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<lay::PluginDeclaration> plugin_decl (new lay::MAGRead
}

View File

@ -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;