22 #include <DocumentManager.h>
26 #include <QSvgRenderer>
31 #include <KColorCombo>
32 #include <KPushButton>
38 class DataTypePropertyModel :
public QAbstractTableModel {
39 QList<QString> _propertyList;
40 DataTypePtr _dataType;
43 DataTypePropertyModel(
QObject* parent = 0)
44 : QAbstractTableModel(parent)
49 QString propertyInRow(
int row) {
50 return _propertyList.at(row);
53 void setDataType(DataTypePtr dataType) {
55 _propertyList = dataType->properties();
59 QVariant headerData(
int section, Qt::Orientation orientation,
int role)
const
62 if (role != Qt::DisplayRole) {
66 if (orientation == Qt::Horizontal) {
68 case 0:
return i18n(
"Name");
69 case 1:
return i18n(
"Default Value");
70 case 2:
return i18n(
"Display");
76 Qt::ItemFlags flags(
const QModelIndex &index)
const
78 if (index.isValid() && index.column() == 2) {
79 return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
81 if (index.isValid()) {
82 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
84 return Qt::ItemIsEnabled;
87 virtual int rowCount(
const QModelIndex & parent = QModelIndex())
const {
88 if(!parent.isValid()) {
89 return _propertyList.count();
94 virtual int columnCount(
const QModelIndex & parent = QModelIndex())
const {
95 if(!parent.isValid()) {
101 virtual QVariant data(
const QModelIndex & index,
int role = Qt::DisplayRole)
const {
102 if (role == Qt::DisplayRole) {
103 switch(index.column()) {
104 case 0:
return _propertyList.at(index.row());
break;
105 case 1:
return _dataType->propertyDefaultValue(_propertyList.at(index.row()));
break;
106 default:
return QVariant();
109 if (role == Qt::EditRole) {
110 switch(index.column()) {
111 case 0:
return _propertyList.at(index.row());
break;
112 case 1:
return _dataType->propertyDefaultValue(_propertyList.at(index.row()));
break;
113 default:
return QVariant();
116 if (role == Qt::CheckStateRole && index.column() == 2) {
117 if (_dataType->isPropertyVisible(_propertyList.at(index.row()))) {
118 return QVariant(Qt::Checked);
120 return QVariant(Qt::Unchecked);
126 bool setData(
const QModelIndex &index,
const QVariant &value,
int role)
129 if (index.isValid() && role == Qt::CheckStateRole && index.column() == 2) {
130 bool visible = _dataType->isPropertyVisible(_propertyList.at(index.row()));
131 _dataType->setPropertyVisible(_propertyList.at(index.row()), !visible);
132 emit dataChanged(index,index);
137 if (index.isValid() && role == Qt::EditRole) {
138 switch (index.column()) {
140 _dataType->renameProperty(_propertyList.at(index.row()), value.toString());
141 setDataType(_dataType);
144 case 1: _dataType->setPropertyDefaultValue(_propertyList.at(index.row()), value);
break;
145 default:
return false;
159 ui =
new Ui::DataTypePage;
163 ui->addPropertyButton->setIcon(KIcon(
"rocsnew"));
164 ui->removePropertyButton->setIcon(KIcon(
"rocsdelete"));
165 ui->addType->setIcon(KIcon(
"rocsnew"));
166 ui->deleteType->setIcon(KIcon(
"rocsdelete"));
169 _model =
new DataTypePropertyModel(
this);
170 ui->propertyList->setModel(_model);
171 ui->propertyList->horizontalHeader()->setProperty(
"stretchLastSection",
true);
173 connect(ui->typeSelector, SIGNAL(activated(
int)),
174 this, SLOT(setCurrentType(
int)));
175 connect(ui->addType, SIGNAL(clicked(
bool)),
176 this, SLOT(addType()));
177 connect(ui->deleteType, SIGNAL(clicked(
bool)),
178 this, SLOT(removeType()));
179 connect(ui->typeName, SIGNAL(textChanged(QString)),
180 this, SLOT(updateCurrentTypeName()));
181 connect(ui->typeName, SIGNAL(textEdited(QString)),
182 this, SLOT(setTypeName()));
183 connect(ui->typeDefaultColor, SIGNAL(activated(QColor)),
184 this, SLOT(setTypeDefaultColor()));
185 connect(ui->typeIcon, SIGNAL(activated(QString)),
186 this, SLOT(setIcon()));
187 connect(ui->addPropertyButton, SIGNAL(clicked(
bool)),
188 this, SLOT(addProperty()));
189 connect(ui->removePropertyButton, SIGNAL(clicked(
bool)),
190 this, SLOT(removeProperty()));
198 disconnect(document);
200 _document = document;
203 foreach (
int dataType, _document->dataTypeList()) {
204 QString item = i18nc(
207 _document->dataType(dataType)->name(),
208 _document->dataType(dataType)->identifier());
209 ui->typeSelector->addItem(item, QVariant(dataType));
212 ui->typeSelector->setCurrentIndex(0);
220 setDocument(DocumentManager::self().activeDocument());
221 ui->typeSelector->setCurrentIndex(ui->typeSelector->findData(QVariant(dataType->identifier())));
222 _model->setDataType(dataType);
225 void DataTypePage::setTypeName()
227 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
228 _document->dataType(type)->setName(ui->typeName->text());
231 void DataTypePage::setTypeDefaultColor()
233 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
234 _document->dataType(type)->setDefaultColor(ui->typeDefaultColor->color());
237 void DataTypePage::setIcon()
239 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
240 _document->dataType(type)->setIcon(
241 ui->typeIcon->itemData(ui->typeIcon->currentIndex()).toString()
245 void DataTypePage::addProperty()
247 int typeID = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
248 DataTypePtr type = _document->dataType(typeID);
250 QString newProperty = i18nc(
"Property identifier, only a-Z and no spaces",
"property%1", counter);
251 while (type->properties().contains(newProperty)) {
252 newProperty = i18nc(
"Property identifier, only a-Z and no spaces",
"property%1", ++counter);
254 type->addProperty(newProperty,
"");
255 _model->setDataType(type);
259 void DataTypePage::removeProperty()
261 int typeID = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
262 DataTypePtr type = _document->dataType(typeID);
263 int row = ui->propertyList->currentIndex().row();
267 type->removeProperty(_model->propertyInRow(row));
268 _model->setDataType(type);
272 void DataTypePage::setCurrentType(
int index)
274 int type = ui->typeSelector->itemData(index).toInt();
276 ui->deleteType->setDisabled(
true);
278 ui->deleteType->setDisabled(
false);
281 ui->typeName->setText(_document->dataType(type)->name());
282 ui->typeDefaultColor->setColor(_document->dataType(type)->defaultColor());
285 ui->typeIcon->clear();
286 if (!_document->iconPackage().isEmpty()) {
287 QFile svgFile(_document->iconPackage());
288 svgFile.open(QIODevice::ReadOnly | QIODevice::Text);
290 QXmlStreamReader reader(&svgFile);
291 QSvgRenderer *renderer = DocumentManager::self().sharedRenderer(svgFile.fileName());
293 while (!reader.atEnd()) {
295 if (!reader.attributes().hasAttribute(
"id")) {
298 QString attribute = reader.attributes().value(
"id").toString();
299 if (attribute.startsWith(QLatin1String(
"rocs_"))) {
300 QImage iconImage = QImage(80, 80, QImage::Format_ARGB32);
301 iconImage.fill(Qt::transparent);
304 painter.begin(&iconImage);
305 renderer->render(&painter, attribute);
308 attribute.remove(
"rocs_");
309 ui->typeIcon->addItem(KIcon(QPixmap::fromImage(iconImage)),
"", QVariant(attribute));
312 if (!_document->dataType(type)->iconName().isEmpty()) {
313 QString icon = _document->dataType(type)->iconName();
314 icon.remove(
"rocs_");
315 ui->typeIcon->setCurrentIndex(ui->typeIcon->findData(icon));
321 void DataTypePage::updateCurrentTypeName()
323 int typeID = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
324 DataTypePtr type = _document->dataType(typeID);
325 QString item = i18nc(
330 ui->typeSelector->setItemText(ui->typeSelector->currentIndex(), item);
334 void DataTypePage::addType()
336 int newTypeID = _document->registerDataType(i18nc(
"@item:inlistbox",
"Data Type"));
337 DataTypePtr newType = _document->dataType(newTypeID);
338 QString item = i18nc(
342 newType->identifier());
343 ui->typeSelector->addItem(item, QVariant(newTypeID));
344 ui->typeSelector->setCurrentIndex(ui->typeSelector->count()-1);
345 setCurrentType(ui->typeSelector->count()-1);
349 void DataTypePage::removeType()
351 int oldType = ui->typeSelector->currentIndex();
355 ui->typeSelector->removeItem(oldType);
356 _document->removeDataType(oldType);
357 ui->typeSelector->setCurrentIndex(0);
void setDocument(Document *document)
void setDataType(DataTypePtr dataType)
DataTypePage(QWidget *parent=0)