File: togglelist.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (120 lines) | stat: -rw-r--r-- 3,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "togglelist.h"
#include "gui/settings.h"
#include "support/utils.h"
#include "widgets/basicitemdelegate.h"
#include "widgets/icons.h"

ToggleList::ToggleList(QWidget* p)
	: QWidget(p)
{
	setupUi(this);
	connect(upButton, SIGNAL(clicked()), SLOT(moveUp()));
	connect(downButton, SIGNAL(clicked()), SLOT(moveDown()));
	connect(addButton, SIGNAL(clicked()), SLOT(add()));
	connect(removeButton, SIGNAL(clicked()), SLOT(remove()));
	connect(available, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), SLOT(availableChanged(QListWidgetItem*)));
	connect(selected, SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), SLOT(selectedChanged(QListWidgetItem*)));
	upButton->setIcon(Icons::self()->upIcon);
	downButton->setIcon(Icons::self()->downIcon);
	bool rtl = isRightToLeft();
	addButton->setIcon(rtl ? Icons::self()->leftIcon : Icons::self()->rightIcon);
	removeButton->setIcon(rtl ? Icons::self()->rightIcon : Icons::self()->leftIcon);

	upButton->setEnabled(false);
	downButton->setEnabled(false);
	addButton->setEnabled(false);
	removeButton->setEnabled(false);
	available->setAlternatingRowColors(false);
	available->setItemDelegate(new BasicItemDelegate(available));
	selected->setAlternatingRowColors(false);
	selected->setItemDelegate(new BasicItemDelegate(selected));
}

void ToggleList::moveUp()
{
	move(-1);
}

void ToggleList::moveDown()
{
	move(+1);
}

void ToggleList::add()
{
	int index = available->currentRow();
	if (index < 0 || index > available->count()) {
		return;
	}
	QListWidgetItem* item = available->takeItem(index);
	QListWidgetItem* newItem = new QListWidgetItem(selected);
	newItem->setText(item->text());
	newItem->setData(Qt::UserRole, item->data(Qt::UserRole));
	delete item;
}

void ToggleList::remove()
{
	int index = selected->currentRow();
	if (index < 0 || index > selected->count()) {
		return;
	}
	QListWidgetItem* item = selected->takeItem(index);
	QListWidgetItem* newItem = new QListWidgetItem(available);
	newItem->setText(item->text());
	newItem->setData(Qt::UserRole, item->data(Qt::UserRole));
	delete item;
}

void ToggleList::move(int d)
{
	const int row = selected->currentRow();
	QListWidgetItem* item = selected->takeItem(row);
	selected->insertItem(row + d, item);
	selected->setCurrentRow(row + d);
}

void ToggleList::availableChanged(QListWidgetItem* item)
{
	addButton->setEnabled(item);
}

void ToggleList::selectedChanged(QListWidgetItem* item)
{
	if (!item) {
		upButton->setEnabled(false);
		downButton->setEnabled(false);
		removeButton->setEnabled(false);
	}
	else {
		const int row = available->row(item);
		upButton->setEnabled(row != 0);
		downButton->setEnabled(row != available->count() - 1);
	}
	removeButton->setEnabled(item);
}

#include "moc_togglelist.cpp"