File: selectaction.cpp

package info (click to toggle)
ktikz 0.13.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,136 kB
  • sloc: cpp: 10,491; xml: 701; sh: 131; makefile: 19
file content (132 lines) | stat: -rw-r--r-- 3,879 bytes parent folder | download | duplicates (5)
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
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
 *   Copyright (C) 2009, 2012, 2014 by Glad Deschrijver                    *
 *     <glad.deschrijver@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; if not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include "selectaction.h"

#include "action.h"
#include "icon.h"

#ifdef KTIKZ_USE_KDE
#include <KActionCollection>

SelectAction::SelectAction(QObject *parent, const QString &name)
	: KSelectAction(parent)
{
	if (!name.isEmpty())
		Action::actionCollection()->addAction(name, this);
}

SelectAction::SelectAction(const QString &text, QObject *parent, const QString &name)
	: KSelectAction(text, parent)
{
	if (!name.isEmpty())
		Action::actionCollection()->addAction(name, this);
}

SelectAction::SelectAction(const Icon &icon, const QString &text, QObject *parent, const QString &name)
	: KSelectAction(icon, text, parent)
{
	if (!name.isEmpty())
		Action::actionCollection()->addAction(name, this);
}
#else
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets/QComboBox>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QWidgetAction>
#else
#include <QtGui/QComboBox>
#include <QtGui/QLineEdit>
#include <QtGui/QWidgetAction>
#endif

SelectAction::SelectAction(QObject *parent, const QString &name)
	: QWidgetAction(parent)
{
	init(name);
}

SelectAction::SelectAction(const QString &text, QObject *parent, const QString &name)
	: QWidgetAction(parent)
{
	init(name);
	setText(text);
}

SelectAction::SelectAction(const Icon &icon, const QString &text, QObject *parent, const QString &name)
	: QWidgetAction(parent)
{
	init(name);
	setIcon(icon);
	setText(text);
}

void SelectAction::init(const QString &name)
{
	if (!name.isEmpty())
		setObjectName(name);

	m_selectCombo = new QComboBox;
	setDefaultWidget(m_selectCombo);
	connect(m_selectCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentItem()));
}

SelectAction::~SelectAction()
{
	delete m_selectCombo;
}

void SelectAction::setEditable(bool editable)
{
	m_selectCombo->setEditable(editable);
	if (editable)
		connect(m_selectCombo->lineEdit(), SIGNAL(returnPressed()), this, SLOT(setCurrentItem()));
}

void SelectAction::setCurrentItem()
{
	const QString text = m_selectCombo->currentText();
	Q_EMIT triggered(text);
}

void SelectAction::removeAllActions()
{
	m_selectCombo->clear();
}

void SelectAction::setItems(const QStringList &items)
{
	m_selectCombo->clear();
	m_selectCombo->addItems(items);
}

void SelectAction::setCurrentItem(int index)
{
	m_selectCombo->setCurrentIndex(index);
	if (m_selectCombo->isEditable())
		m_selectCombo->lineEdit()->setText(m_selectCombo->currentText());
}

QStringList SelectAction::items() const
{
	QStringList itemList;
	for (int i = 0; i < m_selectCombo->count(); ++i)
		itemList << m_selectCombo->itemText(i);
	return itemList;
}
#endif