22 #include <PointerType.h>
24 #include <DocumentManager.h>
31 #include <KColorCombo>
32 #include <KPushButton>
38 class PointerTypePropertyModel :
public QAbstractTableModel {
39 QList<QString> _propertyList;
40 PointerTypePtr _pointerType;
43 PointerTypePropertyModel(
QObject* parent = 0)
44 : QAbstractTableModel(parent)
49 QString propertyInRow(
int row) {
50 return _propertyList.at(row);
53 void setPointerType(PointerTypePtr pointerType) {
54 _pointerType = pointerType;
55 _propertyList = pointerType->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 _pointerType->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 _pointerType->propertyDefaultValue(_propertyList.at(index.row()));
break;
113 default:
return QVariant();
116 if (role == Qt::CheckStateRole && index.column() == 2) {
117 if (_pointerType->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 = _pointerType->isPropertyVisible(_propertyList.at(index.row()));
131 _pointerType->setPropertyVisible(_propertyList.at(index.row()), !visible);
132 emit dataChanged(index,index);
137 if (index.isValid() && role == Qt::EditRole) {
138 switch (index.column()) {
140 _pointerType->renameProperty(_propertyList.at(index.row()), value.toString());
141 setPointerType(_pointerType);
144 case 1: _pointerType->setPropertyDefaultValue(_propertyList.at(index.row()), value);
break;
145 default:
return false;
159 ui =
new Ui::PointerTypePage;
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 PointerTypePropertyModel(
this);
170 ui->propertyList->setModel(_model);
171 ui->propertyList->horizontalHeader()->setProperty(
"stretchLastSection",
true);
174 ui->typeLineStyle->addItem(i18nc(
"@item:inlistbox",
"solid line"), Qt::SolidLine);
175 ui->typeLineStyle->addItem(i18nc(
"@item:inlistbox",
"dash line"), Qt::DashLine);
176 ui->typeLineStyle->addItem(i18nc(
"@item:inlistbox",
"dot line "), Qt::DotLine);
177 ui->typeLineStyle->addItem(i18nc(
"@item:inlistbox",
"dash dot line"), Qt::DashDotLine);
180 ui->typePointerDirection->addItem(i18nc(
"@item:inlistbox",
"Unidirectional"), PointerType::Unidirectional);
181 ui->typePointerDirection->addItem(i18nc(
"@item:inlistbox",
"Bidirectional"), PointerType::Bidirectional);
183 connect(ui->typeSelector, SIGNAL(currentIndexChanged(
int)),
184 this, SLOT(setCurrentType(
int)));
185 connect(ui->addType, SIGNAL(clicked(
bool)),
186 this, SLOT(addType()));
187 connect(ui->deleteType, SIGNAL(clicked(
bool)),
188 this, SLOT(removeType()));
189 connect(ui->typeName, SIGNAL(textChanged(QString)),
190 this, SLOT(updateCurrentTypeName()));
191 connect(ui->typePointerDirection, SIGNAL(activated(
int)),
192 this, SLOT(updateCurrentTypeDirection()));
193 connect(ui->typeLineStyle, SIGNAL(activated(
int)),
194 this, SLOT(updateCurrentTypeLineStyle()));
195 connect(ui->typeName, SIGNAL(textEdited(QString)),
196 this, SLOT(setTypeName()));
197 connect(ui->typeDefaultColor, SIGNAL(activated(QColor)),
198 this, SLOT(setTypeDefaultColor()));
199 connect(ui->addPropertyButton, SIGNAL(clicked(
bool)),
200 this, SLOT(addProperty()));
201 connect(ui->removePropertyButton, SIGNAL(clicked(
bool)),
202 this, SLOT(removeProperty()));
210 disconnect(document);
212 _document = document;
215 foreach (
int pointerTypeID, _document->pointerTypeList()) {
216 PointerTypePtr pointerType = _document->pointerType(pointerTypeID);
217 QString item = i18nc(
221 pointerType->identifier());
222 ui->typeSelector->addItem(item, QVariant(pointerTypeID));
225 ui->typeSelector->setCurrentIndex(0);
233 setDocument(DocumentManager::self().activeDocument());
234 ui->typeSelector->setCurrentIndex(ui->typeSelector->findData(QVariant(pointerType->identifier())));
235 _model->setPointerType(pointerType);
239 void PointerTypePage::setCurrentType(
int index)
241 int type = ui->typeSelector->itemData(index).toInt();
243 ui->deleteType->setDisabled(
true);
245 ui->deleteType->setDisabled(
false);
248 ui->typeName->setText(_document->pointerType(type)->name());
249 ui->typeLineStyle->setCurrentIndex(
250 ui->typeLineStyle->findData(_document->pointerType(type)->lineStyle()));
251 ui->typePointerDirection->setCurrentIndex(
252 ui->typePointerDirection->findData(_document->pointerType(type)->direction()));
253 ui->typeDefaultColor->setColor(_document->pointerType(type)->defaultColor());
257 void PointerTypePage::setTypeName()
259 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
260 _document->pointerType(type)->setName(ui->typeName->text());
264 void PointerTypePage::setTypeDefaultColor()
266 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
267 _document->pointerType(type)->setDefaultColor(ui->typeDefaultColor->color());
271 void PointerTypePage::updateCurrentTypeName()
273 int typeID = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
274 PointerTypePtr type = _document->pointerType(typeID);
275 QString item = i18nc(
280 ui->typeSelector->setItemText(ui->typeSelector->currentIndex(), item);
284 void PointerTypePage::updateCurrentTypeDirection()
286 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
287 int direction = ui->typePointerDirection->itemData(ui->typePointerDirection->currentIndex()).toInt();
288 _document->pointerType(type)->setDirection(PointerType::Direction(direction));
292 void PointerTypePage::updateCurrentTypeLineStyle()
294 int type = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
295 int style = ui->typeLineStyle->itemData(ui->typeLineStyle->currentIndex()).toInt();
296 _document->pointerType(type)->setLineStyle(Qt::PenStyle(style));
300 void PointerTypePage::addProperty()
302 int typeID = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
303 PointerTypePtr type = _document->pointerType(typeID);
305 QString newProperty = i18nc(
"Property identifier, only a-Z and no spaces",
"property%1", counter);
306 while (type->properties().contains(newProperty)) {
307 newProperty = i18nc(
"Property identifier, only a-Z and no spaces",
"property%1", ++counter);
309 type->addProperty(newProperty,
"");
310 _model->setPointerType(type);
314 void PointerTypePage::removeProperty()
316 int typeID = ui->typeSelector->itemData(ui->typeSelector->currentIndex()).toInt();
317 PointerTypePtr type = _document->pointerType(typeID);
318 int row = ui->propertyList->currentIndex().row();
322 type->removeProperty(_model->propertyInRow(row));
323 _model->setPointerType(type);
326 void PointerTypePage::addType()
328 int newTypeID = _document->registerPointerType(i18nc(
"@item:inlistbox",
"Connection"));
329 PointerTypePtr newType = _document->pointerType(newTypeID);
330 QString item = i18nc(
334 newType->identifier());
335 ui->typeSelector->addItem(item, QVariant(newTypeID));
336 ui->typeSelector->setCurrentIndex(ui->typeSelector->count()-1);
337 setCurrentType(ui->typeSelector->count()-1);
341 void PointerTypePage::removeType()
343 int oldType = ui->typeSelector->currentIndex();
347 ui->typeSelector->removeItem(oldType);
348 _document->removePointerType(oldType);
349 ui->typeSelector->setCurrentIndex(0);
PointerTypePage(QWidget *parent=0)
void setDocument(Document *document)
void setPointerType(PointerTypePtr pointerType)