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 #include "pageitemattributes.h"
8 #include "commonstrings.h"
9 
10 #include <QString>
11 #include <QPushButton>
12 #include <QComboBox>
13 
14 #include "ui/sctablewidget.h"
15 
PageItemAttributes(QWidget * parent,const char * name,bool modal)16 PageItemAttributes::PageItemAttributes( QWidget* parent, const char* name, bool modal) : QDialog(parent)
17 {
18 	setupUi(this);
19 	setModal(modal);
20 
21 	relationships << tr("None", "relationship") << tr("Relates To") << tr("Is Parent Of") << tr("Is Child Of");
22 	relationshipsData << "none" << "relation" << "parent" << "child";
23 	types << tr("None", "types") << tr("Boolean") << tr("Integer") << tr("Real Number") << tr("String");
24 	typesData << "none" << "boolean" << "integer" << "double" << "string";
25 
26 	connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
27 	connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
28 	connect(attributesTable, SIGNAL(cellChanged(int,int)), this, SLOT(tableItemChanged(int,int)));
29 	connect(addButton, SIGNAL(clicked()), this, SLOT(addEntry()));
30 	connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteEntry()));
31 	connect(clearButton, SIGNAL(clicked()), this, SLOT(clearEntries()));
32 	connect(copyButton, SIGNAL(clicked()), this, SLOT(copyEntry()));
33 }
34 
~PageItemAttributes()35 PageItemAttributes::~PageItemAttributes()
36 {
37 }
38 
setup(ObjAttrVector * pageItemAttrs,ObjAttrVector * docItemAttrs)39 void PageItemAttributes::setup(ObjAttrVector *pageItemAttrs, ObjAttrVector *docItemAttrs)
40 {
41 	localAttributes=*pageItemAttrs;
42 	localDocAttributes=*docItemAttrs;
43 
44 	nameList.clear();
45 	nameList.append("");
46 	for (ObjAttrVector::Iterator it = localDocAttributes.begin(); it!= localDocAttributes.end(); ++it)
47 		nameList.append((*it).name);
48 
49 	updateTable();
50 }
51 
getNewAttributes()52 ObjAttrVector* PageItemAttributes::getNewAttributes()
53 {
54 	return &localAttributes;
55 }
56 
57 
tableItemChanged(int row,int col)58 void PageItemAttributes::tableItemChanged( int row, int col )
59 {
60 	switch (col)
61 	{
62 		case 0:
63 			{
64 				QComboBox* qcti=dynamic_cast<QComboBox*>(attributesTable->cellWidget(row,col));
65 				if (qcti!=nullptr)
66 					localAttributes[row].name=qcti->currentText();
67 			}
68 			break;
69 		case 1:
70 			{
71 				QComboBox* qcti=dynamic_cast<QComboBox*>(attributesTable->cellWidget(row,col));
72 				if (qcti != nullptr)
73 				{
74 					int index = qcti->currentIndex();
75 					if (index < typesData.count())
76 						localAttributes[row].type = typesData[index];
77 				}
78 			}
79 			break;
80 		case 2:
81 			localAttributes[row].value=attributesTable->item(row, col)->text();
82 			break;
83 		case 3:
84 			localAttributes[row].parameter=attributesTable->item(row, col)->text();
85 			break;
86 		case 4:
87 		{
88 			QComboBox* qcti=dynamic_cast<QComboBox*>(attributesTable->cellWidget(row,col));
89 			if (qcti!=nullptr)
90 			{
91 				int index=qcti->currentIndex();
92 				if (index<relationshipsData.count())
93 					localAttributes[row].relationship=relationshipsData[index];
94 			}
95 		}
96 		break;
97 		case 5:
98 			localAttributes[row].relationshipto=attributesTable->item(row, col)->text();
99 			break;
100 		case 6:
101 			//AutoAddTo is not used once this gets to the page items
102 			/*
103 			{
104 				QComboTableItem* qcti=dynamic_cast<QComboTableItem*>(attributesTable->item(row,col));
105 				if (qcti!=nullptr)
106 				{
107 					uint index=qcti->currentItem();
108 					if (index<autoAddToData.count())
109 						localAttributes[row].autoaddto=autoAddToData[index];
110 				}
111 			}
112 			*/
113 			break;
114 		default:
115 			break;
116 	}
117 }
118 
119 
addEntry()120 void PageItemAttributes::addEntry()
121 {
122 	ObjectAttribute blank;
123 	blank.name="";
124 	blank.relationship=CommonStrings::None;
125 	blank.autoaddto="none";
126 	localAttributes.append(blank);
127 	updateTable();
128 }
129 
130 
deleteEntry()131 void PageItemAttributes::deleteEntry()
132 {
133 	int currRow=attributesTable->currentRow();
134 	bool found=false;
135 	ObjAttrVector::Iterator it;
136 	int count=0;
137 	for (it = localAttributes.begin(); it!= localAttributes.end(); ++it)
138 	{
139 		if(count==currRow)
140 		{
141 			found=true;
142 			break;
143 		}
144 		++count;
145 	}
146 	if (found)
147 	{
148 		localAttributes.erase(it);
149 		updateTable();
150 	}
151 }
152 
153 
clearEntries()154 void PageItemAttributes::clearEntries()
155 {
156 	localAttributes.clear();
157 	updateTable();
158 }
159 
160 
copyEntry()161 void PageItemAttributes::copyEntry()
162 {
163 	int currRow=attributesTable->currentRow();
164 	bool found=false;
165 	ObjAttrVector::Iterator it;
166 	int count=0;
167 	for (it = localAttributes.begin(); it!= localAttributes.end(); ++it)
168 	{
169 		if(count==currRow)
170 		{
171 			found=true;
172 			break;
173 		}
174 		++count;
175 	}
176 	if (found)
177 	{
178 		localAttributes.append((*it));
179 		updateTable();
180 	}
181 }
182 
183 
updateTable()184 void PageItemAttributes::updateTable()
185 {
186 	attributesTable->setRowCount(localAttributes.count());
187 	for (int row = 0; row < localAttributes.count(); ++row)
188 	{
189 		uint col = 0;
190 		ObjectAttribute& objAttr = localAttributes[row];
191 
192 		//Name
193 		QComboBox *item1 = new QComboBox();
194 		item1->addItems(nameList);
195 		int listIndex = nameList.indexOf(objAttr.name);
196 		if (listIndex != -1)
197 			item1->setCurrentIndex(listIndex);
198 		else
199 		{
200 			item1->setCurrentIndex(0);
201 			item1->setItemText(0, objAttr.name);
202 		}
203 		item1->setEditable(true);
204 		attributesTable->setCellWidget(row, col++, item1);
205 		//Type
206 		QComboBox *item2 = new QComboBox();
207 		item2->addItems(types);
208 
209 		listIndex = typesData.indexOf(objAttr.type);
210 		if (listIndex==-1)
211 		{
212 			objAttr.type="none";
213 			listIndex = 0;
214 		}
215 		item2->setCurrentIndex(listIndex);
216 
217 		item2->setEditable(true);
218 		attributesTable->setCellWidget(row, col++, item2);
219 		//Default Value
220 		QTableWidgetItem *item3 = new QTableWidgetItem(objAttr.value);
221 		attributesTable->setItem(row, col++, item3);
222 		//Default Parameter
223 		QTableWidgetItem *item4 = new QTableWidgetItem(objAttr.parameter);
224 		attributesTable->setItem(row, col++, item4);
225 		//Relationship
226 		QComboBox *item5 = new QComboBox();
227 		item5->addItems(relationships);
228 		attributesTable->setCellWidget(row, col++, item5);
229 		int index=relationshipsData.indexOf(objAttr.relationship);
230 		if (index==-1)
231 		{
232 			objAttr.relationship="none";
233 			index=0;
234 		}
235 		item5->setCurrentIndex(index);
236 		//Relationship to
237 		QTableWidgetItem *item6 = new QTableWidgetItem(objAttr.relationshipto);
238 		attributesTable->setItem(row, col++, item6);
239 		//Auto Add to not used here
240 		/*
241 		QComboBox *item7 = new QComboBox();
242 		item7->addItems(autoAddTo);
243 		attributesTable->setCellWidget(row, col++, item7);
244 		int index2=autoAddToData.indexOf(objAttr.autoaddto);
245 		if (index2==-1)
246 		{
247 			objAttr.relationship="none";
248 			index2=0;
249 		}
250 		item7->setCurrentItem(index2);
251 		*/
252 		QTableWidgetItem *t=attributesTable->verticalHeaderItem(row);
253 		if (t != nullptr)
254 			t->setText(QString("%1").arg(row));
255 	}
256 	deleteButton->setEnabled(localAttributes.count()!=0);
257 	copyButton->setEnabled(localAttributes.count()!=0);
258 	clearButton->setEnabled(localAttributes.count()!=0);
259 }
260 
261 
okClicked()262 void PageItemAttributes::okClicked()
263 {
264 	//Qt hack as we will lose data if the user hasn't left a cell
265 	//http://www.qtforum.org/thread.php?threadid=9078
266 	if (attributesTable->rowCount()>0 && attributesTable->currentRow()!=-1)
267 	{
268 		//Avoid selecting 0,0 if 0,0 is current cell
269 		int selectCol;
270 		if(attributesTable->currentColumn()!=0)
271 			selectCol=0;
272 		else
273 			selectCol=1;
274 
275 		attributesTable->setCurrentCell(0,selectCol);
276 		tableItemChanged(attributesTable->currentRow(), attributesTable->currentColumn());
277 	}
278 	accept();
279 }
280 
languageChange()281 void PageItemAttributes::languageChange()
282 {
283 }
284