Skip to content

Commit

Permalink
Added bool delegate for True,False parameters for BlueSky
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Glowacki committed Dec 17, 2024
1 parent 64a80d9 commit a5f63dc
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/mvc/ComboBoxBoolDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*-----------------------------------------------------------------------------
* Copyright (c) 2012, UChicago Argonne, LLC
* See LICENSE file.
*---------------------------------------------------------------------------*/

#include <mvc/ComboBoxBoolDelegate.h>
#include <QComboBox>
#include <QWidget>
#include <QModelIndex>
#include <QApplication>
#include <QString>

#include <iostream>

//---------------------------------------------------------------------------

ComboBoxBoolDelegate::ComboBoxBoolDelegate(QObject *parent) : QItemDelegate(parent)
{
Items = {"False", "True"};
_custom_col = -1;
}

//---------------------------------------------------------------------------

QWidget *ComboBoxBoolDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
{
QComboBox* editor = new QComboBox(parent);
for(unsigned int i = 0; i < Items.size(); ++i)
{
editor->addItem(Items[i].c_str());
}
return editor;
}

//---------------------------------------------------------------------------

void ComboBoxBoolDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = index.model()->data(index, Qt::EditRole).toString();
if(value == "False")
{
comboBox->setCurrentIndex(0);
}
else
{
comboBox->setCurrentIndex(1);
}
}

//---------------------------------------------------------------------------

void ComboBoxBoolDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
model->setData(index, comboBox->currentIndex(), Qt::EditRole);
}

//---------------------------------------------------------------------------

void ComboBoxBoolDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

//---------------------------------------------------------------------------

void ComboBoxBoolDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem myOption = option;
myOption.text = index.data().toString();
if(_custom_col > -1 )
{
if(_custom_col == index.column())
{
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
else
{
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
}
}

//---------------------------------------------------------------------------
42 changes: 42 additions & 0 deletions src/mvc/ComboBoxBoolDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*-----------------------------------------------------------------------------
* Copyright (c) 2012, UChicago Argonne, LLC
* See LICENSE file.
*---------------------------------------------------------------------------*/

#ifndef COMBO_BOX_BOOL_DELEGATE_H
#define COMBO_BOX_BOOL_DELEGATE_H

//---------------------------------------------------------------------------

#include <string>
#include <vector>

#include <QItemDelegate>
#include <QModelIndex>
#include <QWidget>
#include <QVariant>

class ComboBoxBoolDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboBoxBoolDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

void setCustomCol(int col) {_custom_col = col;}

private:
std::vector<std::string> Items;
int _custom_col;
};

//---------------------------------------------------------------------------

#endif

//---------------------------------------------------------------------------

0 comments on commit a5f63dc

Please sign in to comment.