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();
155 TreeNode *item = this->itemFromIndex(index);
158 return mRepository->getNode(item->
getUid());
161 TreeNode* TreeItemModel::itemFromIndex(
const QModelIndex&
index)
const 163 if (!index.isValid())
164 return mRepository->getTopNode().get();
166 return static_cast<TreeNode*
>(index.internalPointer());
171 if (parent.isValid() && (parent.column() != mNameIndex))
178 if (parent.isValid() && (parent.column() != mNameIndex))
180 TreeNode *parentItem = this->itemFromIndex(parent);
186 if (role==Qt::DisplayRole)
188 if (index.column()==mNameIndex)
190 TreeNode *item = this->itemFromIndex(index);
194 if (role==Qt::CheckStateRole)
196 if (this->isViewGroupColumn(index.column()))
198 TreeNode *item = this->itemFromIndex(index);
202 if (role==Qt::ToolTipRole || role==Qt::StatusTipRole)
204 TreeNode *item = this->itemFromIndex(index);
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)
213 TreeNode *item = this->itemFromIndex(index);
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)
226 TreeNode *item = this->itemFromIndex(index);
230 if (role==Qt::ForegroundRole)
232 if (index.column()==mNameIndex)
234 TreeNode *item = this->itemFromIndex(index);
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()))
257 TreeNode *item = this->itemFromIndex(index);
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));
301 TreeNode *parentItem = this->itemFromIndex(parent);
303 if (row < children.size())
305 return createIndex(row, column, children[row].
get());
309 return QModelIndex();
315 TreeNode *childItem = this->itemFromIndex(index);
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);
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.
void changed(TreeNode *node=NULL)
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
Namespace for all CustusX production code.