22 #include <DocumentManager.h>
26 #include <QSvgRenderer>
31 #include <KColorCombo>
32 #include <KPushButton>
40 DataTypePtr _dataType;
43 DataTypePropertyModel(
QObject* parent = 0)
49 QString propertyInRow(
int row) {
50 return _propertyList.
at(row);
53 void setDataType(DataTypePtr dataType) {
55 _propertyList = dataType->properties();
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");
84 return Qt::ItemIsEnabled;
89 return _propertyList.count();
102 if (role == Qt::DisplayRole) {
104 case 0:
return _propertyList.at(index.
row());
break;
105 case 1:
return _dataType->propertyDefaultValue(_propertyList.at(index.
row()));
break;
109 if (role == Qt::EditRole) {
111 case 0:
return _propertyList.at(index.
row());
break;
112 case 1:
return _dataType->propertyDefaultValue(_propertyList.at(index.
row()));
break;
116 if (role == Qt::CheckStateRole && index.
column() == 2) {
117 if (_dataType->isPropertyVisible(_propertyList.at(index.
row()))) {
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);
137 if (index.
isValid() && role == Qt::EditRole) {
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()));
180 this, SLOT(updateCurrentTypeName()));
182 this, SLOT(setTypeName()));
184 this, SLOT(setTypeDefaultColor()));
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()));
200 _document = document;
203 foreach (
int dataType, _document->dataTypeList()) {
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);
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();
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_");
312 if (!_document->dataType(type)->iconName().isEmpty()) {
313 QString icon = _document->dataType(type)->iconName();
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);
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);
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);
virtual int rowCount(const QModelIndex &parent) const =0
void render(QPainter *painter)
void setDocument(Document *document)
void setDataType(DataTypePtr dataType)
DataTypePage(QWidget *parent=0)
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QString & remove(int position, int n)
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
void fill(uint pixelValue)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
virtual QVariant data(const QModelIndex &index, int role) const =0
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
virtual QModelIndex parent(const QModelIndex &index) const =0
char * toString(const T &value)
const QChar at(int position) const
virtual int columnCount(const QModelIndex &parent) const =0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool begin(QPaintDevice *device)