1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 
8 #include "smsccombobox.h"
9 
10 
SMScComboBox(QWidget * parent)11 SMScComboBox::SMScComboBox(QWidget *parent)
12 	: QComboBox(parent)
13 {
14 
15 }
16 
setCurrentItem(int i)17 void SMScComboBox::setCurrentItem(int i)
18 {
19 	disconnect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
20 	setFont(false);
21 	m_hasParent = false;
22 	m_pItem = 0;
23 	QComboBox::setCurrentIndex(i);
24 }
25 
setCurrentItem(int i,bool isParentValue)26 void SMScComboBox::setCurrentItem(int i, bool isParentValue)
27 {
28 	disconnect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
29 	m_hasParent = true;
30 	m_pItem = i;
31 	setFont(!isParentValue);
32 	if (!isParentValue && !m_useParentValue)
33 	{
34 		m_useParentValue = true;
35 		addItem( tr("Use Parent Value"));
36 	}
37 
38 	QComboBox::setCurrentIndex(i);
39 	connect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
40 }
41 
setCurrentItemByData(int i)42 void SMScComboBox::setCurrentItemByData(int i)
43 {
44 	disconnect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
45 	setFont(false);
46 	m_hasParent = false;
47 	m_pItem = 0;
48 	for (int idx(0); idx < count(); ++idx)
49 	{
50 		if (itemData(idx).toInt() == i)
51 			QComboBox::setCurrentIndex(idx);
52 	}
53 	connect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
54 }
55 
setCurrentItemByData(int i,bool isParentValue)56 void SMScComboBox::setCurrentItemByData(int i, bool isParentValue)
57 {
58 	disconnect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
59 	m_hasParent = true;
60 	setFont(!isParentValue);
61 	if (!isParentValue && !m_useParentValue)
62 	{
63 		m_useParentValue = true;
64 		addItem( tr("Use Parent Value"));
65 	}
66 
67 	for (int idx(0); idx < count(); ++idx)
68 	{
69 		if (itemData(idx).toInt() == i)
70 		{
71 			QComboBox::setCurrentIndex(idx);
72 			m_pItem = idx;
73 		}
74 	}
75 	connect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
76 }
77 
setCurrentItemByData(double d)78 void SMScComboBox::setCurrentItemByData(double d)
79 {
80 	disconnect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
81 	setFont(false);
82 	m_hasParent = false;
83 	m_pItem = 0;
84 	for (int idx(0); idx < count(); ++idx)
85 	{
86 		if (itemData(idx).toDouble() == d)
87 			QComboBox::setCurrentIndex(idx);
88 	}
89 	connect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
90 }
91 
setCurrentItemByData(double d,bool isParentValue)92 void SMScComboBox::setCurrentItemByData(double d, bool isParentValue)
93 {
94 	disconnect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
95 	m_hasParent = true;
96 	setFont(!isParentValue);
97 	if (!isParentValue && !m_useParentValue)
98 	{
99 		m_useParentValue = true;
100 		addItem( tr("Use Parent Value"));
101 	}
102 
103 	for (int idx(0); idx < count(); ++idx)
104 	{
105 		if (itemData(idx).toDouble() == d)
106 		{
107 			QComboBox::setCurrentIndex(idx);
108 			m_pItem = idx;
109 		}
110 	}
111 	connect(this, SIGNAL(highlighted(int)), this, SLOT(currentChanged()));
112 }
113 
getItemIndexForData(int i)114 int SMScComboBox::getItemIndexForData(int i)
115 {
116 	for (int idx(0); idx < count(); ++idx)
117 	{
118 		if (itemData(idx).toInt() == i)
119 		{
120 			return idx;
121 		}
122 	}
123 	return 0;
124 }
125 
getItemIndexForData(double d)126 int SMScComboBox::getItemIndexForData(double d)
127 {
128 	for (int idx(0); idx < count(); ++idx)
129 	{
130 		if (itemData(idx).toDouble() == d)
131 		{
132 			return idx;
133 		}
134 	}
135 	return 0;
136 }
137 
138 
139 
setParentItem(int i)140 void SMScComboBox::setParentItem(int i)
141 {
142 	m_hasParent = true;
143 	m_pItem = i;
144 }
145 
useParentValue()146 bool SMScComboBox::useParentValue()
147 {
148 	bool ret = false;
149 
150 	if (m_useParentValue && m_hasParent)
151 	{
152 		ret = currentIndex() == (count() - 1);
153 		if (ret)
154 		{
155 			removeItem(count() - 1);
156 			setFont(false);
157 			setCurrentItem(m_pItem, true);
158 			m_useParentValue = false;
159 		}
160 	}
161 
162 	return ret;
163 }
164 
setFont(bool wantBold)165 void SMScComboBox::setFont(bool wantBold)
166 {
167 	QFont f(font());
168 	f.setBold(wantBold);
169 	QComboBox::setFont(f);
170 }
171 
currentChanged()172 void SMScComboBox::currentChanged()
173 {
174 	if (m_hasParent && !m_useParentValue)
175 	{
176 		setFont(true);
177 		addItem( tr("Use Parent Value"));
178 		m_useParentValue = true;
179 	}
180 }
181