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
+2 -2
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 ..
}
+156
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 ()));
}
}
}
+65
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