Skip to content

Commit

Permalink
finish dual listbox
Browse files Browse the repository at this point in the history
  • Loading branch information
KorGgenT committed Nov 10, 2020
1 parent 3834206 commit ac87ebf
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 59 deletions.
130 changes: 89 additions & 41 deletions object_creator/dual_list_box.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,54 @@
#include "dual_list_box.h"

void creator::dual_list_box::initialize( const QStringList &items, const QSize &default_text_box_size )
void creator::dual_list_box::initialize( const QStringList &items )
{
this->items = items;

const double button_width = 0.2;
const double box_width = 0.9;
const QSize box_size( box_width * default_text_box_size.width(), 4 * default_text_box_size.height() );
const QSize button_size( button_width * default_text_box_size.width(), default_text_box_size.height() );

included_box.resize( box_size );
excluded_box.resize( box_size );

include_all_button.resize( button_size );
exclude_all_button.resize( button_size );
include_sel_button.resize( button_size );
exclude_sel_button.resize( button_size );

included_box.setParent( this );
button_widget.setParent( this );
excluded_box.setParent( this );

include_all_button.setParent( this );
exclude_all_button.setParent( this );
include_sel_button.setParent( this );
exclude_sel_button.setParent( this );
include_all_button.setParent( &button_widget );
exclude_all_button.setParent( &button_widget );
include_sel_button.setParent( &button_widget );
exclude_sel_button.setParent( &button_widget );

QGridLayout *listbox_layout = new QGridLayout();
QVBoxLayout *button_widget_layout = new QVBoxLayout();
button_widget.setLayout( button_widget_layout );

button_widget_layout->addWidget( &include_all_button );
button_widget_layout->addWidget( &include_sel_button );
button_widget_layout->addWidget( &exclude_sel_button );
button_widget_layout->addWidget( &exclude_all_button );

QHBoxLayout *listbox_layout = new QHBoxLayout();
listbox_layout->setSpacing( 0 );
listbox_layout->setMargin( 0 );
setLayout( listbox_layout );

listbox_layout->addWidget( &included_box, 0, 0 );
listbox_layout->addWidget( &excluded_box, 0, 2 );

listbox_layout->addWidget( &include_all_button, 0, 1 );
listbox_layout->addWidget( &include_sel_button, 1, 1 );
listbox_layout->addWidget( &exclude_sel_button, 2, 1 );
listbox_layout->addWidget( &exclude_all_button, 3, 1 );
listbox_layout->addWidget( &included_box );
listbox_layout->addWidget( &button_widget );
listbox_layout->addWidget( &excluded_box );

// initialize the list as all excluded first
exclude_all();

QObject::connect( &include_all_button, &QPushButton::click, this, &dual_list_box::include_all );
QObject::connect( &exclude_all_button, &QPushButton::click, this, &dual_list_box::exclude_all );
QObject::connect( &include_sel_button, &QPushButton::click, this,
&dual_list_box::include_selected );
QObject::connect( &exclude_sel_button, &QPushButton::click, this,
&dual_list_box::exclude_selected );
QObject::connect( &include_all_button, &QPushButton::pressed, this, &dual_list_box::include_all );
QObject::connect( &exclude_all_button, &QPushButton::pressed, this, &dual_list_box::exclude_all );
QObject::connect( &include_sel_button, &QPushButton::pressed, this,
&dual_list_box::include_selected );
QObject::connect( &exclude_sel_button, &QPushButton::pressed, this,
&dual_list_box::exclude_selected );

include_all_button.setText( QString( "<<" ) );
exclude_all_button.setText( QString( ">>" ) );
include_sel_button.setText( QString( "<" ) );
exclude_sel_button.setText( QString( ">" ) );

QObject::connect( &include_all_button, &QPushButton::click, this, &dual_list_box::click );
QObject::connect( &exclude_all_button, &QPushButton::click, this, &dual_list_box::click );
QObject::connect( &include_sel_button, &QPushButton::click, this, &dual_list_box::click );
QObject::connect( &exclude_sel_button, &QPushButton::click, this, &dual_list_box::click );
QObject::connect( &include_all_button, &QPushButton::pressed, this, &dual_list_box::pressed );
QObject::connect( &exclude_all_button, &QPushButton::pressed, this, &dual_list_box::pressed );
QObject::connect( &include_sel_button, &QPushButton::pressed, this, &dual_list_box::pressed );
QObject::connect( &exclude_sel_button, &QPushButton::pressed, this, &dual_list_box::pressed );

included_box.show();
excluded_box.show();
Expand All @@ -63,6 +57,26 @@ void creator::dual_list_box::initialize( const QStringList &items, const QSize &
exclude_all_button.show();
include_sel_button.show();
exclude_sel_button.show();

included_box.setSelectionBehavior( QAbstractItemView::SelectionBehavior::SelectItems );
included_box.setSelectionMode( QAbstractItemView::SelectionMode::SingleSelection );
button_widget_layout->setSizeConstraint( QLayout::SizeConstraint::SetMaximumSize );
listbox_layout->setSizeConstraint( QLayout::SizeConstraint::SetMaximumSize );
}

void creator::dual_list_box::resize( const QSize &size )
{
const double button_width = 0.1;
const double box_width = 0.45;

included_box.setMaximumHeight( size.height() );
included_box.setMaximumWidth( size.width() * box_width );

button_widget.setMaximumHeight( size.height() );
button_widget.setMaximumWidth( size.width() * button_width );

excluded_box.setMaximumHeight( size.height() );
excluded_box.setMaximumWidth( size.width() * box_width );
}

void creator::dual_list_box::include_all()
Expand All @@ -81,23 +95,57 @@ void creator::dual_list_box::exclude_all()

void creator::dual_list_box::include_selected()
{
const QString selected = excluded_box.currentText();
const QList<QListWidgetItem *> selected_items = excluded_box.selectedItems();
if( selected_items.isEmpty() ) {
return;
}
const QString selected = selected_items.first()->text();
included_box.addItem( selected );
excluded_box.removeItem( excluded_box.currentIndex() );

int index = 0;
QStringList excluded;
for( int i = 0; i < excluded_box.count(); i++ ) {
const QString excluded_single = excluded_box.item( i )->text();
if( excluded_single != selected ) {
excluded.append( excluded_single );
} else {
index = i;
}
}
excluded_box.clear();
excluded_box.addItems( excluded );
excluded_box.item( index )->setSelected( true );
}

void creator::dual_list_box::exclude_selected()
{
const QString selected = included_box.currentText();
const QList<QListWidgetItem *> selected_items = included_box.selectedItems();
if( selected_items.isEmpty() ) {
return;
}
const QString selected = selected_items.first()->text();
excluded_box.addItem( selected );
included_box.removeItem( included_box.currentIndex() );

int index = 0;
QStringList included;
for( int i = 0; i < included_box.count(); i++ ) {
const QString included_single = included_box.item( i )->text();
if( included_single != selected ) {
included.append( included_single );
} else {
index = i;
}
}
included_box.clear();
included_box.addItems( included );
included_box.item( index )->setSelected( true );
}

QStringList creator::dual_list_box::get_included() const
{
QStringList ret;
for( int i = 0; i < included_box.count(); i++ ) {
ret.append( included_box.itemText( i ) );
ret.append( included_box.item( i )->text() );
}
return ret;
}
13 changes: 8 additions & 5 deletions object_creator/dual_list_box.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef CATA_OBJECT_CREATOR_DUAL_LIST_BOX_H
#define CATA_OBJECT_CREATOR_DUAL_LIST_BOX_H

#include "QtWidgets/qcombobox.h"
#include "QtWidgets/qgridlayout.h"
#include "QtWidgets/qlabel.h"
#include "QtWidgets/qlistwidget.h"
#include "QtWidgets/qpushbutton.h"

namespace creator
Expand All @@ -14,17 +14,20 @@ class dual_list_box : public QWidget
public:
dual_list_box() {}

void initialize( const QStringList &items, const QSize &default_text_box_size );
void initialize( const QStringList &items );
void resize( const QSize & );

QStringList get_included() const;
Q_SIGNALS:
void click();
void pressed();
private:
// the entire list of acceptable strings
QStringList items;

QComboBox included_box;
QComboBox excluded_box;
QListWidget included_box;
QListWidget excluded_box;

QWidget button_widget;

QPushButton include_all_button;
QPushButton include_sel_button;
Expand Down
6 changes: 3 additions & 3 deletions object_creator/moc_dual_list_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void creator::dual_list_box::qt_static_metacall( QObject *_o, QMetaObject::Call
Q_UNUSED( _t )
switch( _id ) {
case 0:
_t->click();
_t->pressed();
break;
default:
;
Expand All @@ -82,7 +82,7 @@ void creator::dual_list_box::qt_static_metacall( QObject *_o, QMetaObject::Call
int *result = reinterpret_cast<int *>( _a[0] );
{
using _t = void ( dual_list_box::* )();
if( *reinterpret_cast<_t *>( _a[1] ) == static_cast<_t>( &dual_list_box::click ) ) {
if( *reinterpret_cast<_t *>( _a[1] ) == static_cast<_t>( &dual_list_box::pressed ) ) {
*result = 0;
return;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ int creator::dual_list_box::qt_metacall( QMetaObject::Call _c, int _id, void **_
}

// SIGNAL 0
void creator::dual_list_box::click()
void creator::dual_list_box::pressed()
{
QMetaObject::activate( this, &staticMetaObject, 0, nullptr );
}
Expand Down
31 changes: 21 additions & 10 deletions object_creator/spell_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,27 @@ creator::spell_window::spell_window( QWidget *parent, Qt::WindowFlags flags )
row++ * default_text_box_height ) );
field_intensity_variance_label.show();

QStringList all_mtypes;
for( const mtype &mon : MonsterGenerator::generator().get_all_mtypes() ) {
all_mtypes.append( mon.id.c_str() );
}

targeted_monster_ids_box.initialize( all_mtypes );
targeted_monster_ids_box.resize( QSize( default_text_box_width * 4, default_text_box_height * 6 ) );
targeted_monster_ids_box.setParent( this );
targeted_monster_ids_box.move( QPoint( col * default_text_box_width,
row * default_text_box_height ) );
targeted_monster_ids_box.show();
QObject::connect( &targeted_monster_ids_box, &dual_list_box::pressed, [&]() {
const QStringList mon_ids = targeted_monster_ids_box.get_included();
editable_spell.targeted_monster_ids.clear();
for( const QString &id : mon_ids ) {
editable_spell.targeted_monster_ids.emplace( mtype_id( id.toStdString() ) );
}
write_json();
} );
row += 6;

// =========================================================================================
// fourth column of boxes
max_row = std::max( max_row, row );
Expand Down Expand Up @@ -872,16 +893,6 @@ creator::spell_window::spell_window( QWidget *parent, Qt::WindowFlags flags )
sound_ambient_label.move( QPoint( col * default_text_box_width, row++ * default_text_box_height ) );
sound_ambient_label.show();

QStringList all_mtypes;
for( const mtype &mon : MonsterGenerator::generator().get_all_mtypes() ) {
all_mtypes.append( mon.id.c_str() );
}

targeted_monster_ids_box.initialize( all_mtypes, default_text_box_size );
targeted_monster_ids_box.setParent( this );
targeted_monster_ids_box.move( QPoint( col * default_text_box_width, row++ * default_text_box_height ) );
targeted_monster_ids_box.show();

// =========================================================================================
// sixth column of boxes
max_row = std::max( max_row, row );
Expand Down

0 comments on commit ac87ebf

Please sign in to comment.