6 #include <QTreeWidgetItem>
25 QAbstractItemModel(parent),
29 mSelectionModel = NULL;
35 this->createShowColumnsProperty();
36 this->onShowColumnsChanged();
44 void TreeItemModel::onRepositoryChanged(
TreeNode* node)
50 QModelIndex index0 = this->createIndex(0, 0, node);
51 QModelIndex index1 = this->createIndex(0, mColumnCount, node);
52 emit dataChanged(index0, index1);
57 emit dataChanged(QModelIndex(),QModelIndex());
65 this->beginResetModel();
66 mRepository->update();
67 this->endResetModel();
71 void TreeItemModel::createShowColumnsProperty()
73 QStringList range = QStringList() <<
"color" <<
"vg0" <<
"vg1" <<
"vg2" <<
"vg3";
74 QStringList defvals = range;
78 std::map<QString, QString> names;
79 names[
"color"] =
"Color";
80 for (
unsigned i=0; i<4; ++i)
81 names[QString(
"vg%1").arg(i)] = QString(
"View Group %1").arg(i);
90 "Select visible columns",
94 mShowColumnsProperty->setDisplayNames(names);
95 connect(mShowColumnsProperty.get(), &
Property::changed,
this, &TreeItemModel::onShowColumnsChanged);
98 void TreeItemModel::onShowColumnsChanged()
101 this->beginResetModel();
104 QStringList cols = mShowColumnsProperty->getValue();
107 mNameIndex = mColumnCount++;
108 mColorIndex = cols.contains(
"color") ? mColumnCount++ : none;
110 while (cols.contains(QString(
"vg%1").arg(mViewGroupCount)))
112 mViewGroupIndex = (mViewGroupCount>0) ? mColumnCount : none;
113 mColumnCount += mViewGroupCount;
129 mSelectionModel = selectionModel;
130 connect(mSelectionModel, &QItemSelectionModel::currentChanged,
131 this, &TreeItemModel::currentItemChangedSlot);
138 void TreeItemModel::currentItemChangedSlot(
const QModelIndex& current,
const QModelIndex& previous)
140 TreeNode *item = this->itemFromIndex(current);
149 QModelIndex mi = mSelectionModel->currentIndex();
158 return mRepository->getNode(item->
getUid());
161 TreeNode* TreeItemModel::itemFromIndex(
const QModelIndex& index)
const
163 if (!
index.isValid())
164 return mRepository->getTopNode().get();
171 if (
parent.isValid() && (
parent.column() != mNameIndex))
178 if (
parent.isValid() && (
parent.column() != mNameIndex))
186 if (role==Qt::DisplayRole)
188 if (
index.column()==mNameIndex)
194 if (role==Qt::CheckStateRole)
196 if (this->isViewGroupColumn(
index.column()))
202 if (role==Qt::ToolTipRole || role==Qt::StatusTipRole)
205 if (
index.column()==mNameIndex)
206 return QString(
"%1 of type %2").arg(item->
getName()).arg(item->
getType());
207 if (this->isViewGroupColumn(
index.column()))
208 return QString(
"Set visibility of %1 in view group %2")
209 .arg(item->
getName()).arg(this->viewGroupFromColumn(
index.column()));
211 if (role==Qt::DecorationRole)
214 if (
index.column()==mNameIndex)
216 if (
index.column()==mColorIndex)
218 if (item->
getColor().canConvert<QColor>())
219 return this->getColorIcon(item->
getColor().value<QColor>());
222 if (role==Qt::FontRole)
224 if (
index.column()==mNameIndex)
230 if (role==Qt::ForegroundRole)
232 if (
index.column()==mNameIndex)
238 if (color.canConvert<QColor>())
240 QColor oldColor = color.value<QColor>().toHsv();
241 QColor newColor = this->adjustColorToContrastWithWhite(oldColor);
253 if (role==Qt::CheckStateRole)
255 if (this->isViewGroupColumn(
index.column()))
267 if (
index.column()>0)
268 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
270 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
273 bool TreeItemModel::isViewGroupColumn(
int col)
const
275 return ( (0<=(col-mViewGroupIndex))
277 ((col-mViewGroupIndex) < mViewGroupCount)
280 int TreeItemModel::viewGroupFromColumn(
int col)
const
282 return (col-mViewGroupIndex);
287 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
289 if (section==mNameIndex)
291 if (section==mColorIndex)
293 if (this->isViewGroupColumn(section))
294 return QString(
"V%1").arg(this->viewGroupFromColumn(section));
303 if (row < children.size())
305 return createIndex(row, column, children[row].get());
309 return QModelIndex();
318 return QModelIndex();
322 if (parentItem == mRepository->getTopNode().get())
323 return QModelIndex();
330 std::vector<TreeNodePtr> parentSiblings = grandParent->getVisibleChildren();
331 for (row=0; row<parentSiblings.size(); ++row)
332 if (parentItem==parentSiblings[row].get())
336 return createIndex(row, mNameIndex, parentItem);
339 QIcon TreeItemModel::getColorIcon(QColor color)
const
341 QImage image(QSize(128,128), QImage::Format_RGBA8888);
342 QPainter painter(&image);
343 painter.fillRect(image.rect(), QColor(
"white"));
344 painter.setBrush(QBrush(color));
345 painter.drawRoundedRect(image.rect(), 75, 75, Qt::RelativeSize);
347 return QIcon(QPixmap::fromImage(image));
350 QColor TreeItemModel::adjustColorToContrastWithWhite(QColor color)
const
358 double h = color.hueF();
359 double s = color.hsvSaturationF();
360 double v = color.valueF();
362 double isChromatic = s > 0.1;
369 v = std::min(0.6, v);
372 return QColor::fromHsvF(h,s,v);