combjs

diff gui/gui.cc @ 2:4e8f2bbe8426

doing the gui
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 15 Jul 2011 06:54:32 +0300
parents dd02002227a2
children
line diff
     1.1 --- a/gui/gui.cc	Thu Jul 14 15:40:20 2011 +0300
     1.2 +++ b/gui/gui.cc	Fri Jul 15 06:54:32 2011 +0300
     1.3 @@ -5,6 +5,10 @@
     1.4  #include "gui.h"
     1.5  #include "combjs.h"
     1.6  
     1.7 +static void add_to_list(int jsidx, QListWidget *list);
     1.8 +static QListWidgetItem *get_selected(QListWidget *list);
     1.9 +static int find_item(QListWidget *list, int jsidx);
    1.10 +
    1.11  static std::vector<Joystick> jsvec;
    1.12  
    1.13  MainWindow::MainWindow(QWidget *parent)
    1.14 @@ -16,14 +20,16 @@
    1.15  
    1.16  	list_js->clear();
    1.17  	for(int i=0; i<num_js; i++) {
    1.18 -		QListWidgetItem *item = new QListWidgetItem(jsvec[i].name.c_str());
    1.19 -		item->setData(Qt::UserRole, i);
    1.20 -		item->setToolTip(jsvec[i].dev.c_str());
    1.21 -
    1.22 -		list_js->addItem(item);
    1.23 +		add_to_list(i, list_js);
    1.24  	}
    1.25  
    1.26  	connect(list_js, SIGNAL(itemSelectionChanged()), this, SLOT(jslist_select()));
    1.27 +	connect(list_newdev, SIGNAL(itemSelectionChanged()), this, SLOT(clist_select()));
    1.28 +
    1.29 +	connect(action_addjs, SIGNAL(triggered()), this, SLOT(add_js()));
    1.30 +	connect(action_remove, SIGNAL(triggered()), this, SLOT(remove_js()));
    1.31 +	connect(action_combine, SIGNAL(triggered()), this, SLOT(start_virt()));
    1.32 +	connect(action_delete, SIGNAL(triggered()), this, SLOT(stop_virt()));
    1.33  }
    1.34  
    1.35  
    1.36 @@ -44,12 +50,97 @@
    1.37  /* slots */
    1.38  void MainWindow::jslist_select()
    1.39  {
    1.40 -	QList<QListWidgetItem*> sel_list = list_js->selectedItems();
    1.41 -	QListWidgetItem *item = sel_list.isEmpty() ? 0 : sel_list.first();
    1.42 +	QListWidgetItem *item = get_selected(list_js);
    1.43  
    1.44 -	if(item && jsvec[item->data(Qt::UserRole).toInt()].isvirt) {
    1.45 -		bn_del_virt->setEnabled(true);
    1.46 +	if(item) {
    1.47 +		int idx = item->data(Qt::UserRole).toInt();
    1.48 +
    1.49 +		bn_add->setEnabled(true);
    1.50 +		if(jsvec[idx].isvirt) {
    1.51 +			bn_del_virt->setEnabled(true);
    1.52 +		} else {
    1.53 +			bn_del_virt->setEnabled(false);
    1.54 +		}
    1.55 +
    1.56 +		lb_name->setText(jsvec[idx].name.c_str());
    1.57 +		lb_dev->setText(jsvec[idx].dev.c_str());
    1.58 +		lb_axes->setText(QString::number(jsvec[idx].num_axes));
    1.59 +		lb_buttons->setText(QString::number(jsvec[idx].num_bn));
    1.60 +
    1.61  	} else {
    1.62 +		bn_add->setEnabled(false);
    1.63  		bn_del_virt->setEnabled(false);
    1.64 +
    1.65 +		lb_name->setText("");
    1.66 +		lb_dev->setText("");
    1.67 +		lb_axes->setText("");
    1.68 +		lb_buttons->setText("");
    1.69  	}
    1.70  }
    1.71 +
    1.72 +void MainWindow::clist_select()
    1.73 +{
    1.74 +	QListWidgetItem *item = get_selected(list_newdev);
    1.75 +
    1.76 +	bn_remove->setEnabled(item != 0);
    1.77 +}
    1.78 +
    1.79 +void MainWindow::add_js()
    1.80 +{
    1.81 +	QListWidgetItem *item = get_selected(list_js);
    1.82 +	if(!item) {
    1.83 +		return;
    1.84 +	}
    1.85 +
    1.86 +	int idx = item->data(Qt::UserRole).toInt();
    1.87 +	if(find_item(list_newdev, idx) == -1) {
    1.88 +		add_to_list(idx, list_newdev);
    1.89 +		bn_create->setEnabled(true);
    1.90 +	}
    1.91 +}
    1.92 +
    1.93 +void MainWindow::remove_js()
    1.94 +{
    1.95 +	QListWidgetItem *item = get_selected(list_newdev);
    1.96 +	if(item) {
    1.97 +		list_newdev->takeItem(list_newdev->row(item));
    1.98 +		delete item;
    1.99 +
   1.100 +		if(list_newdev->count() == 0) {
   1.101 +			bn_create->setEnabled(false);
   1.102 +		}
   1.103 +	}
   1.104 +}
   1.105 +
   1.106 +void MainWindow::start_virt()
   1.107 +{
   1.108 +}
   1.109 +
   1.110 +void MainWindow::stop_virt()
   1.111 +{
   1.112 +}
   1.113 +
   1.114 +static void add_to_list(int jsidx, QListWidget *list)
   1.115 +{
   1.116 +	QListWidgetItem *item = new QListWidgetItem(jsvec[jsidx].name.c_str());
   1.117 +	item->setData(Qt::UserRole, jsidx);
   1.118 +	item->setToolTip(jsvec[jsidx].dev.c_str());
   1.119 +	list->addItem(item);
   1.120 +}
   1.121 +
   1.122 +static QListWidgetItem *get_selected(QListWidget *list)
   1.123 +{
   1.124 +	QList<QListWidgetItem*> sel_list = list->selectedItems();
   1.125 +	return sel_list.isEmpty() ? 0 : sel_list.first();
   1.126 +}
   1.127 +
   1.128 +static int find_item(QListWidget *list, int jsidx)
   1.129 +{
   1.130 +	int num = list->count();
   1.131 +	for(int i=0; i<num; i++) {
   1.132 +		if(list->item(i)->data(Qt::UserRole).toInt() == jsidx) {
   1.133 +			return i;
   1.134 +		}
   1.135 +	}
   1.136 +	return -1;
   1.137 +}