6 #include <QTreeWidgetItem>
25 QAbstractItemModel(parent),
29 mSelectionModel = NULL;
35 this->createShowColumnsProperty();
36 this->onShowColumnsChanged();
44 void TreeItemModel::onRepositoryChanged()
46 emit dataChanged(QModelIndex(),QModelIndex());
53 this->beginResetModel();
54 mRepository->update();
55 this->endResetModel();
59 void TreeItemModel::createShowColumnsProperty()
61 QStringList range = QStringList() <<
"color" <<
"vg0" <<
"vg1" <<
"vg2" <<
"vg3";
62 QStringList defvals = range;
66 std::map<QString, QString> names;
67 names[
"color"] =
"Color";
68 for (
unsigned i=0; i<4; ++i)
69 names[QString(
"vg%1").arg(i)] = QString(
"View Group %1").arg(i);
78 "Select visible columns",
82 mShowColumnsProperty->setDisplayNames(names);
83 connect(mShowColumnsProperty.get(), &
Property::changed,
this, &TreeItemModel::onShowColumnsChanged);
86 void TreeItemModel::onShowColumnsChanged()
89 this->beginResetModel();
92 QStringList cols = mShowColumnsProperty->getValue();
95 mNameIndex = mColumnCount++;
96 mColorIndex = cols.contains(
"color") ? mColumnCount++ : none;
98 while (cols.contains(QString(
"vg%1").arg(mViewGroupCount)))
100 mViewGroupIndex = (mViewGroupCount>0) ? mColumnCount : none;
101 mColumnCount += mViewGroupCount;
117 mSelectionModel = selectionModel;
118 connect(mSelectionModel, &QItemSelectionModel::currentChanged,
119 this, &TreeItemModel::currentItemChangedSlot);
126 void TreeItemModel::currentItemChangedSlot(
const QModelIndex& current,
const QModelIndex& previous)
128 TreeNode *item = this->itemFromIndex(current);
137 QModelIndex mi = mSelectionModel->currentIndex();
143 TreeNode *item = this->itemFromIndex(index);
146 return mRepository->getNode(item->
getUid());
149 TreeNode* TreeItemModel::itemFromIndex(
const QModelIndex& index)
const
151 if (!index.isValid())
152 return mRepository->getTopNode().get();
154 return static_cast<TreeNode*
>(index.internalPointer());
168 TreeNode *parentItem = this->itemFromIndex(parent);
174 if (role==Qt::DisplayRole)
176 if (index.column()==mNameIndex)
178 TreeNode *item = this->itemFromIndex(index);
182 if (role==Qt::CheckStateRole)
184 if (this->isViewGroupColumn(index.column()))
186 TreeNode *item = this->itemFromIndex(index);
190 if (role==Qt::ToolTipRole || role==Qt::StatusTipRole)
192 TreeNode *item = this->itemFromIndex(index);
193 if (index.column()==mNameIndex)
194 return QString(
"%1 of type %2").arg(item->
getName()).arg(item->
getType());
195 if (this->isViewGroupColumn(index.column()))
196 return QString(
"Set visibility of %1 in view group %2")
197 .arg(item->
getName()).arg(this->viewGroupFromColumn(index.column()));
199 if (role==Qt::DecorationRole)
201 TreeNode *item = this->itemFromIndex(index);
202 if (index.column()==mNameIndex)
204 if (index.column()==mColorIndex)
206 if (item->
getColor().canConvert<QColor>())
207 return this->getColorIcon(item->
getColor().value<QColor>());
210 if (role==Qt::FontRole)
212 if (index.column()==mNameIndex)
214 TreeNode *item = this->itemFromIndex(index);
218 if (role==Qt::ForegroundRole)
220 if (index.column()==mNameIndex)
222 TreeNode *item = this->itemFromIndex(index);
226 if (color.canConvert<QColor>())
228 QColor oldColor = color.value<QColor>().toHsv();
229 QColor newColor = this->adjustColorToContrastWithWhite(oldColor);
243 if (role==Qt::CheckStateRole)
245 if (this->isViewGroupColumn(index.column()))
247 TreeNode *item = this->itemFromIndex(index);
257 if (index.column()>0)
258 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable;
260 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
263 bool TreeItemModel::isViewGroupColumn(
int col)
const
265 return ( (0<=(col-mViewGroupIndex))
267 ((col-mViewGroupIndex) < mViewGroupCount)
270 int TreeItemModel::viewGroupFromColumn(
int col)
const
272 return (col-mViewGroupIndex);
277 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
279 if (section==mNameIndex)
281 if (section==mColorIndex)
283 if (this->isViewGroupColumn(section))
284 return QString(
"V%1").arg(this->viewGroupFromColumn(section));
291 TreeNode *parentItem = this->itemFromIndex(parent);
293 if (row < children.size())
295 return createIndex(row, column, children[row].
get());
299 return QModelIndex();
305 TreeNode *childItem = this->itemFromIndex(index);
308 return QModelIndex();
312 if (parentItem == mRepository->getTopNode().get())
313 return QModelIndex();
320 std::vector<TreeNodePtr> parentSiblings = grandParent->getVisibleChildren();
321 for (row=0; row<parentSiblings.size(); ++row)
322 if (parentItem==parentSiblings[row].
get())
326 return createIndex(row, mNameIndex, parentItem);
329 QIcon TreeItemModel::getColorIcon(QColor color)
const
331 QImage image(QSize(128,128), QImage::Format_RGBA8888);
332 QPainter painter(&image);
333 painter.fillRect(image.rect(), QColor(
"white"));
334 painter.setBrush(QBrush(color));
335 painter.drawRoundedRect(image.rect(), 75, 75, Qt::RelativeSize);
337 return QIcon(QPixmap::fromImage(image));
340 QColor TreeItemModel::adjustColorToContrastWithWhite(QColor color)
const
348 double h = color.hueF();
349 double s = color.hsvSaturationF();
350 double v = color.valueF();
352 double isChromatic = s > 0.1;
359 v = std::min(0.6, v);
362 return QColor::fromHsvF(h,s,v);
void currentItemChanged()
boost::shared_ptr< TreeNode > TreeNodePtr
virtual std::vector< TreeNodePtr > getVisibleChildren() const =0
virtual QString getUid() const =0
boost::shared_ptr< class VisServices > VisServicesPtr
virtual Qt::ItemFlags flags(const QModelIndex &index) const
TreeNodePtr getCurrentItem()
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual QVariant getColor() const =0
virtual QModelIndex parent(const QModelIndex &index) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
virtual QIcon getIcon() const =0
QDomElement getElement()
return the current element
static StringListPropertyPtr initialize(const QString &uid, QString name, QString help, QStringList value, QStringList range, QDomNode root=QDomNode())
virtual QString getType() const =0
TreeItemModel(XmlOptionFile options, VisServicesPtr services, QObject *parent=0)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual void setViewGroupVisibility(int index, bool value)=0
virtual QVariant getViewGroupVisibility(int index) const =0
virtual void activate()=0
static TreeRepositoryPtr create(XmlOptionFile options, VisServicesPtr services)
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
void changed()
emit when the underlying data value is changed: The user interface will be updated.
virtual QString getName() const =0
virtual QVariant getFont() const =0
void setSelectionModel(QItemSelectionModel *selectionModel)
TreeNodePtr getNodeFromIndex(const QModelIndex &index)
virtual TreeNodePtr getVisibleParent() const =0
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Helper class for xml files used to store ssc/cx data.
XmlOptionFile descend(QString element) const
step one level down in the xml tree